summaryrefslogtreecommitdiffstats
path: root/src/args.c
blob: 18a888775eb58f3bb1bc996302196f3044cb53f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
/*
 * Calcurse - text-based organizer
 *
 * Copyright (c) 2004-2010 Frederic Culot <frederic@culot.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the
 *        following disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the
 *        following disclaimer in the documentation and/or other
 *        materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Send your feedback or comments to : misc@calcurse.org
 * Calcurse home page : http://calcurse.org
 *
 */

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <limits.h>
#include <getopt.h>
#include <time.h>
#include <regex.h>

#include "calcurse.h"

/*
 * Print Calcurse usage and exit.
 */
static void
usage ()
{
  char *arg_usage =
    _("Usage: calcurse [-h|-v] [-N] [-an] [-t[num]] [-i<file>] [-x[format]]\n"
      "                [-d <date>|<num>] [-s[date]] [-r[range]]\n"
      "                [-c<file> | -D<dir>] [-S<regex>] [--status]\n");
  fputs (arg_usage, stdout);
}

static void
usage_try ()
{
  char *arg_usage_try = _("Try 'calcurse -h' for more information.\n");
  fputs (arg_usage_try, stdout);
}

/*
 * Print Calcurse version with a short copyright text and exit.
 */
static void
version_arg ()
{
  char vtitle[BUFSIZ];
  char *vtext =
      _("\nCopyright (c) 2004-2010 Frederic Culot.\n"
        "This is free software; see the source for copying conditions.\n");

  (void)snprintf (vtitle, BUFSIZ, _("Calcurse %s - text-based organizer\n"),
                  VERSION);
  fputs (vtitle, stdout);
  fputs (vtext, stdout);
}

/*
 * Print the command line options and exit.
 */
static void
help_arg ()
{
  char htitle[BUFSIZ];
  char *htext =
    _("\nMiscellaneous:\n"
      "  -h, --help\n"
      "	print this help and exit.\n"
      "\n  -v, --version\n"
      "	print calcurse version and exit.\n"
      "\n  --status\n"
      "	display the status of running instances of calcurse.\n"
      "\nFiles:\n"
      "  -c <file>, --calendar <file>\n"
      "	specify the calendar <file> to use (incompatible with '-D').\n"
      "\n  -D <dir>, --directory <dir>\n"
      "	specify the data directory to use (incompatible with '-c').\n"
      "\tIf not specified, the default directory is ~/.calcurse\n"
      "\nNon-interactive:\n"
      "  -a, --appointment\n"
      " 	print events and appointments for current day and exit.\n"
      "\n  -d <date|num>, --day <date|num>\n"
      "	print events and appointments for <date> or <num> upcoming days and"
      "\n\texit. To specify both a starting date and a range, use the\n"
      "\t'--startday' and the '--range' option.\n"
      "\n  -i <file>, --import <file>\n"
      "	import the icalendar data contained in <file>. \n"
      "\n  -n, --next\n"
      "	print next appointment within upcoming 24 hours "
      "and exit. Also given\n\tis the remaining time before this "
      "next appointment.\n"
      "\n  -N, --note\n"
      "	when used with the '-a' or '-t' flag, also print note content\n"
      "	if one is associated with the displayed item.\n"
      "\n  -r[num], --range[=num]\n"
      "	print events and appointments for the [num] number of days"
      "\n\tand exit. If no [num] is given, a range of 1 day is considered.\n"
      "\n  -s[date], --startday[=date]\n"
      "	print events and appointments from [date] and exit.\n"
      "\tIf no [date] is given, the current day is considered.\n"
      "\n  -S<regex>, --search=<regex>\n"
      "	search for the given regular expression within events, appointments,\n"
      "\tand todos description.\n"
      "\n  -t[num], --todo[=num]\n"
      "	print todo list and exit. If the optional number [num] is given,\n"
      "\tthen only todos having a priority equal to [num] will be returned.\n"
      "\tThe priority number must be between 1 (highest) and 9 (lowest).\n"
      "\tIt is also possible to specify '0' for the priority, in which case\n"
      "\tonly completed tasks will be shown.\n"
      "\n  -x[format], --export[=format]\n"
      "	export user data to the specified format. Events, appointments and\n"
      "\ttodos are converted and echoed to stdout.\n"
      "\tTwo possible formats are available: 'ical' and 'pcal'.\n"
      "\tIf the optional argument format is not given, ical format is\n"
      "\tselected by default.\n"
      "\tnote: redirect standard output to export data to a file,\n"
      "\tby issuing a command such as: calcurse --export > calcurse.dat\n"
      "\nFor more information, type '?' from within Calcurse, "
      "or read the manpage.\n"
      "Mail bug reports and suggestions to <misc@calcurse.org>.\n");

  (void)snprintf (htitle, BUFSIZ, _("Calcurse %s - text-based organizer\n"),
                  VERSION);
  fputs (htitle, stdout);
  usage ();
  fputs (htext, stdout);
}

/*
 * Used to display the status of running instances of calcurse.
 * The displayed message will look like one of the following ones:
 *
 *   calcurse is running (pid #)
 *   calcurse is running in background (pid #)
 *   calcurse is not running
 *
 * The status is obtained by looking at pid files in user data directory
 * (.calcurse.pid and .daemon.pid).
 */
static void
status_arg (void)
{
  int cpid, dpid;

  cpid = io_get_pid (path_cpid);
  dpid = io_get_pid (path_dpid);

  EXIT_IF (cpid && dpid,
           _("Error: both calcurse (pid: %d) and its daemon (pid: %d)\n"
             "seem to be running at the same time!\n"
             "Please check manually and restart calcurse.\n"),
           cpid, dpid);

  if (cpid)
    fprintf (stdout, _("calcurse is running (pid %d)\n"), cpid);
  else if (dpid)
    fprintf (stdout, _("calcurse is running in background (pid %d)\n"), dpid);
  else
    fprintf (stdout, _("calcurse is not running\n"));
}

/*
 * Display note contents if one is asociated with the currently displayed item
 * (to be used together with the '-a' or '-t' flag in non-interactive mode).
 * Each line begins with nbtab tabs.
 * Print "No note file found", if the notefile does not exists.
 *
 * (patch submitted by Erik Saule).
 */
static void
print_notefile (FILE *out, char *filename, int nbtab)
{
  char path_to_notefile[BUFSIZ];
  FILE *notefile;
  char linestarter[BUFSIZ] = "";
  char buffer[BUFSIZ];
  int i;
  int printlinestarter = 1;

  for (i = 0; i < nbtab; i++)
    (void)snprintf(linestarter, BUFSIZ, "%s\t", linestarter);

  (void)snprintf (path_to_notefile, BUFSIZ, "%s/%s", path_notes, filename);
  notefile = fopen (path_to_notefile, "r");
  if (notefile)
    {
      while (fgets (buffer, BUFSIZ, notefile) != 0)
        {
          if (printlinestarter)
            {
              fputs (linestarter, out);
              printlinestarter = 0;
            }
          fputs (buffer, out);
          if (buffer[strlen (buffer) - 1] == '\n')
            printlinestarter = 1;
        }
      fputs ("\n", out);
      file_close (notefile, __FILE_POS__);
    }
  else
    {
      fputs (linestarter, out);
      fputs (_("No note file found\n"), out);
    }
}

/*
 * Print todo list and exit. If a priority number is given, then only todo
 * then only todo items that have this priority will be displayed.
 * If priority is < 0, all todos will be displayed.
 * If priority == 0, only completed tasks will be displayed.
 * If regex is not null, only the matching todos are printed.
 */
static void
todo_arg (int priority, int print_note, regex_t *regex)
{
  llist_item_t *i;
  int title = 1;
  char *titlestr, priority_str[BUFSIZ] = "";
  char *all_todos_title = _("to do:\n");
  char *completed_title = _("completed tasks:\n");

  titlestr = priority == 0 ? completed_title : all_todos_title;

#define DISPLAY_TITLE  do {                                             \
  if (title)                                                            \
    {                                                                   \
      fputs (titlestr, stdout);                                         \
      title = 0;                                                        \
    }                                                                   \
  } while (0)

#define DISPLAY_TODO  do {                                              \
  (void)snprintf (priority_str, BUFSIZ, "%d. ", abs (todo->id));        \
  fputs (priority_str, stdout);                                         \
  fputs (todo->mesg, stdout);                                           \
  fputs ("\n", stdout);                                                 \
  if (print_note && todo->note)                                         \
    print_notefile (stdout, todo->note, 1);                             \
  } while (0)

  LLIST_FOREACH (&todolist, i)
    {
      struct todo *todo = LLIST_TS_GET_DATA (i);
      if (regex && regexec (regex, todo->mesg, 0, 0, 0) != 0)
        continue;

      if (todo->id < 0) /* completed task */
        {
          if (priority == 0)
            {
              DISPLAY_TITLE;
              DISPLAY_TODO;
            }
        }
      else
        {
          if (priority < 0 || todo->id == priority)
            {
              DISPLAY_TITLE;
              DISPLAY_TODO;
            }
        }
    }

#undef DISPLAY_TITLE
#undef DISPLAY_TODO
}

/* Print the next appointment within the upcoming 24 hours. */
static void
next_arg (void)
{
  struct notify_app next_app;
  const long current_time = now ();
  int time_left, hours_left, min_left;
  char mesg[BUFSIZ];

  next_app.time = current_time + DAYINSEC;
  next_app.got_app = 0;
  next_app.txt = NULL;

  next_app = *recur_apoint_check_next (&next_app, current_time, get_today ());
  next_app = *apoint_check_next (&next_app, current_time);

  if (next_app.got_app)
    {
      time_left = next_app.time - current_time;
      hours_left = (time_left / HOURINSEC);
      min_left = (time_left - hours_left * HOURINSEC) / MININSEC;
      fputs (_("next appointment:\n"), stdout);
      (void)snprintf (mesg, BUFSIZ, "   [%02d:%02d] %s\n", hours_left, min_left,
                      next_app.txt);
      fputs (mesg, stdout);
      mem_free (next_app.txt);
    }
}

/*
 * Print the date on stdout.
 */
static void
arg_print_date (long date, struct conf *conf)
{
  char date_str[BUFSIZ];
  time_t t;
  struct tm *lt;

  t = date;
  lt = localtime (&t);
  strftime (date_str, BUFSIZ, conf->output_datefmt, lt);
  fputs (date_str, stdout);
  fputs (":\n", stdout);
}

/*
 * Print appointments for given day and exit.
 * If no day is given, the given date is used.
 * If there is also no date given, current date is considered.
 * If regex is not null, only the matching appointments or events are printed.
 */
static int
app_arg (int add_line, struct date *day, long date, int print_note,
         struct conf *conf, regex_t *regex)
{
  llist_item_t *i;
  struct recur_event *re;
  struct recur_apoint *ra;
  long today;
  unsigned print_date = 1;
  int app_found = 0;
  char apoint_start_time[HRMIN_SIZE];
  char apoint_end_time[HRMIN_SIZE];

  if (date == 0)
    today = get_sec_date (*day);
  else
    today = date;

  /*
   * Calculate and print the selected date if there is an event for
   * that date and it is the first one, and then print all the events for
   * that date.
   */
  for (re = recur_elist; re != NULL; re = re->next)
    {
      if (recur_item_inday (re->day, re->exc, re->rpt->type, re->rpt->freq,
                            re->rpt->until, today))
        {
          if (regex && regexec (regex, re->mesg, 0, 0, 0) != 0)
            continue;

          app_found = 1;
          if (add_line)
            {
              fputs ("\n", stdout);
              add_line = 0;
            }
          if (print_date)
            {
              arg_print_date (today, conf);
              print_date = 0;
            }
          fputs (" * ", stdout);
          fputs (re->mesg, stdout);
          fputs ("\n", stdout);
          if (print_note && re->note)
            print_notefile (stdout, re->note, 2);
        }
    }

  LLIST_FIND_FOREACH (&eventlist, today, event_inday, i)
    {
      struct apoint *ev = LLIST_TS_GET_DATA (i);
      if (regex && regexec (regex, ev->mesg, 0, 0, 0) != 0)
        continue;

      app_found = 1;
      if (add_line)
        {
          fputs ("\n", stdout);
          add_line = 0;
        }
      if (print_date)
        {
          arg_print_date (today, conf);
          print_date = 0;
        }
      fputs (" * ", stdout);
      fputs (ev->mesg, stdout);
      fputs ("\n", stdout);
      if (print_note && ev->note)
        print_notefile (stdout, ev->note, 2);
    }

  /* Same process is performed but this time on the appointments. */
  pthread_mutex_lock (&(recur_alist_p->mutex));
  for (ra = recur_alist_p->root; ra != NULL; ra = ra->next)
    {
      if (recur_item_inday (ra->start, ra->exc, ra->rpt->type, ra->rpt->freq,
                            ra->rpt->until, today))
        {
          struct apoint *apt;

          if (regex && regexec (regex, ra->mesg, 0, 0, 0) != 0)
            continue;

          app_found = 1;
          if (add_line)
            {
              fputs ("\n", stdout);
              add_line = 0;
            }
          if (print_date)
            {
              arg_print_date (today, conf);
              print_date = 0;
            }
          apt = apoint_recur_s2apoint_s (ra);
          apoint_sec2str (apt, RECUR_APPT, today, apoint_start_time,
                          apoint_end_time);
          mem_free (apt->mesg);
          mem_free (apt);
          fputs (" - ", stdout);
          fputs (apoint_start_time, stdout);
          fputs (" -> ", stdout);
          fputs (apoint_end_time, stdout);
          fputs ("\n\t", stdout);
          fputs (ra->mesg, stdout);
          fputs ("\n", stdout);
          if (print_note && ra->note)
            print_notefile (stdout, ra->note, 2);
        }
    }
  pthread_mutex_unlock (&(recur_alist_p->mutex));

  LLIST_TS_LOCK (&alist_p);
  LLIST_TS_FIND_FOREACH (&alist_p, today, apoint_inday, i)
    {
      struct apoint *apt = LLIST_TS_GET_DATA (i);
      if (regex && regexec (regex, apt->mesg, 0, 0, 0) != 0)
        continue;

      app_found = 1;
      if (add_line)
        {
          fputs ("\n", stdout);
          add_line = 0;
        }
      if (print_date)
        {
          arg_print_date (today, conf);
          print_date = 0;
        }
      apoint_sec2str (apt, APPT, today, apoint_start_time, apoint_end_time);
      fputs (" - ", stdout);
      fputs (apoint_start_time, stdout);
      fputs (" -> ", stdout);
      fputs (apoint_end_time, stdout);
      fputs ("\n\t", stdout);
      fputs (apt->mesg, stdout);
      fputs ("\n", stdout);
      if (print_note && apt->note)
        print_notefile (stdout, apt->note, 2);
    }
  LLIST_TS_UNLOCK (&alist_p);

  return (app_found);
}

static void
more_info (void)
{
  fputs (_("\nFor more information, type '?' from within Calcurse, "
           "or read the manpage.\n"), stdout);
  fputs (_("Mail bug reports and suggestions to "
           "<misc@calcurse.org>.\n"), stdout);
}

/*
 * For a given date, print appointments for each day
 * in the chosen interval. app_found and add_line are used
 * to format the output correctly.
 */
static void
display_app (struct tm *t, int numdays, int add_line, int print_note,
             struct conf *conf, regex_t *regex)
{
  int i, app_found;
  struct date day;

  for (i = 0; i < numdays; i++)
    {
      day.dd = t->tm_mday;
      day.mm = t->tm_mon + 1;
      day.yyyy = t->tm_year + 1900;
      app_found = app_arg (add_line, &day, 0, print_note, conf, regex);
      if (app_found)
        add_line = 1;
      t->tm_mday++;
      (void)mktime (t);
    }
}

/*
 * Print appointment for the given date or for the given n upcoming
 * days.
 */
static void
date_arg (char *ddate, int add_line, int print_note, struct conf *conf,
          regex_t *regex)
{
  int i;
  struct date day;
  int numdays = 0, num_digit = 0;
  int arg_len = 0;
  static struct tm t;
  time_t timer;

  /*
   * Check (with the argument length) if a date or a number of days
   * was entered, and then call app_arg() to print appointments
   */
  arg_len = strlen (ddate);
  if (arg_len <= 4)
    {				/* a number of days was entered */
      for (i = 0; i <= arg_len - 1; i++)
        {
          if (isdigit (ddate[i]))
            num_digit++;
        }
      if (num_digit == arg_len)
        numdays = atoi (ddate);

      /*
       * Get current date, and print appointments for each day
       * in the chosen interval. app_found and add_line are used
       * to format the output correctly.
       */
      timer = time (NULL);
      t = *localtime (&timer);
      display_app (&t, numdays, add_line, print_note, conf, regex);
    }
  else
    {				/* a date was entered */
      if (parse_date (ddate, conf->input_datefmt, (int *)&day.yyyy,
                      (int *)&day.mm, (int *)&day.dd, NULL))
        {
          (void)app_arg (add_line, &day, 0, print_note, conf, regex);
        }
      else
        {
          char outstr[BUFSIZ];
          fputs (_("Argument to the '-d' flag is not valid\n"), stderr);
          (void)snprintf (outstr, BUFSIZ,
                          "Possible argument format are: '%s' or 'n'\n",
                          DATEFMT_DESC (conf->input_datefmt));
          fputs (_(outstr), stdout);
          more_info ();
        }
    }
}

/*
 * Print appointment from the given date 'startday' for the 'range' upcoming
 * days.
 * If no starday is given (NULL), today is considered
 * If no range is given (NULL), 1 day is considered
 *
 * Many thanks to Erik Saule for providing this function.
 */
static void
date_arg_extended (char *startday, char *range, int add_line, int print_note,
                   struct conf *conf, regex_t *regex)
{
  int i, numdays = 1, error = 0, arg_len = 0;
  static struct tm t;
  time_t timer;

  /*
   * Check arguments and extract information
   */
  if (range != NULL)
    {
      arg_len = strlen (range);
      for (i = 0; i <= arg_len - 1; i++)
        {
          if (!isdigit (range[i]))
            error = 1;
        }
      if (!error)
        numdays = atoi (range);
    }
  timer = time (NULL);
  t = *localtime (&timer);
  if (startday != NULL)
    {
      if (parse_date (startday, conf->input_datefmt, (int *)&t.tm_year,
                      (int *)&t.tm_mon, (int *)&t.tm_mday, NULL))
        {
          t.tm_year -= 1900;
          t.tm_mon--;
          (void)mktime (&t);
        }
      else
        {
          error = 1;
        }
    }
  if (!error)
    {
      display_app (&t, numdays, add_line, print_note, conf, regex);
    }
  else
    {
      char outstr[BUFSIZ];
      fputs (_("Argument is not valid\n"), stderr);
      (void)snprintf (outstr, BUFSIZ,
                      "Argument format for -s and --startday is: '%s'\n",
                      DATEFMT_DESC (conf->input_datefmt));
      fputs (_(outstr), stdout);
      fputs (_("Argument format for -r and --range is: 'n'\n"), stdout);
      more_info ();
    }
}


/*
 * Parse the command-line arguments and call the appropriate
 * routines to handle those arguments. Also initialize the data paths.
 */
int
parse_args (int argc, char **argv, struct conf *conf)
{
  int ch, add_line = 0;
  int unknown_flag = 0;
  /* Command-line flags */
  int aflag = 0;    /* -a: print appointments for current day */
  int cflag = 0;    /* -c: specify the calendar file to use */
  int dflag = 0;    /* -d: print appointments for a specified days */
  int Dflag = 0;    /* -D: specify data directory to use */
  int hflag = 0;    /* -h: print help text */
  int iflag = 0;    /* -i: import data */
  int nflag = 0;    /* -n: print next appointment */
  int Nflag = 0;    /* -N: also print note content with apps and todos */
  int rflag = 0;    /* -r: specify the range of days to consider */
  int sflag = 0;    /* -s: specify the first day to consider */
  int Sflag = 0;    /* -S: specify a regex to search for */
  int tflag = 0;    /* -t: print todo list */
  int vflag = 0;    /* -v: print version number */
  int xflag = 0;    /* -x: export data */

  int tnum = 0, xfmt = 0, non_interactive = 0, multiple_flag = 0, load_data = 0;
  char *ddate = "", *cfile = NULL, *range = NULL, *startday = NULL;
  char *datadir = NULL, *ifile = NULL;
  regex_t reg, *preg = NULL;

  /* Long options only */
  int statusflag = 0; /* --status: get the status of running instances */
  enum
  {
    STATUS_OPT = CHAR_MAX + 1
  };

  static char *optstr = "hvnNax::t::d:c:r::s::S:D:i:";

  struct option longopts[] = {
    {"appointment", no_argument, NULL, 'a'},
    {"calendar", required_argument, NULL, 'c'},
    {"day", required_argument, NULL, 'd'},
    {"directory", required_argument, NULL, 'D'},
    {"help", no_argument, NULL, 'h'},
    {"import", required_argument, NULL, 'i'},
    {"next", no_argument, NULL, 'n'},
    {"note", no_argument, NULL, 'N'},
    {"range", optional_argument, NULL, 'r'},
    {"startday", optional_argument, NULL, 's'},
    {"search", required_argument, NULL, 'S'},
    {"status", no_argument, NULL, STATUS_OPT},
    {"todo", optional_argument, NULL, 't'},
    {"version", no_argument, NULL, 'v'},
    {"export", optional_argument, NULL, 'x'},
    {NULL, no_argument, NULL, 0}
  };

  while ((ch = getopt_long (argc, argv, optstr, longopts, NULL)) != -1)
    {
      switch (ch)
        {
        case STATUS_OPT:
          statusflag = 1;
          break;
        case 'a':
          aflag = 1;
          multiple_flag++;
          load_data++;
          break;
        case 'c':
          cflag = 1;
          multiple_flag++;
          cfile = optarg;
          load_data++;
          break;
        case 'd':
          dflag = 1;
          multiple_flag++;
          load_data++;
          ddate = optarg;
          break;
        case 'D':
          Dflag = 1;
          datadir = optarg;
          break;
        case 'h':
          hflag = 1;
          break;
        case 'i':
          iflag = 1;
          multiple_flag++;
          load_data++;
          ifile = optarg;
          break;
        case 'n':
          nflag = 1;
          multiple_flag++;
          load_data++;
          break;
        case 'N':
          Nflag = 1;
          break;
        case 'r':
          rflag = 1;
          multiple_flag++;
          load_data++;
          range = optarg;
          break;
        case 's':
          sflag = 1;
          multiple_flag++;
          load_data++;
          startday = optarg;
          break;
        case 'S':
          EXIT_IF (Sflag > 0,
                   _("Can not handle more than one regular expression."));
          Sflag = 1;
          if (regcomp (&reg, optarg, REG_EXTENDED))
            EXIT (_("Could not compile regular expression."));
          preg = &reg;
          break;
        case 't':
          tflag = 1;
          multiple_flag++;
          load_data++;
          add_line = 1;
          if (optarg != NULL)
            {
              tnum = atoi (optarg);
              if (tnum < 0 || tnum > 9)
                {
                  usage ();
                  usage_try ();
                  return (EXIT_FAILURE);
                }
            }
          else
            tnum = -1;
          break;
        case 'v':
          vflag = 1;
          break;
        case 'x':
          xflag = 1;
          multiple_flag++;
          load_data++;
          if (optarg != NULL)
            {
              if (strcmp (optarg, "ical") == 0)
                xfmt = IO_EXPORT_ICAL;
              else if (strcmp (optarg, "pcal") == 0)
                xfmt = IO_EXPORT_PCAL;
              else
                {
                  fputs (_("Argument for '-x' should be either "
                           "'ical' or 'pcal'\n"), stderr);
                  usage ();
                  usage_try ();
                  return EXIT_FAILURE;
                }
            }
          else
            {
              xfmt = IO_EXPORT_ICAL;
            }
          break;
        default:
          usage ();
          usage_try ();
          unknown_flag = 1;
          non_interactive = 1;
          /* NOTREACHED */
        }
    }
  argc -= optind;

  if (argc >= 1)
    {
      usage ();
      usage_try ();
      return EXIT_FAILURE;
      /* Incorrect arguments */
    }
  else if (Dflag && cflag)
    {
      fputs (_("Options '-D' and '-c' cannot be used at the same time\n"),
               stderr);
      usage ();
      usage_try ();
      return EXIT_FAILURE;
    }
  else if (Sflag && !(aflag || dflag || rflag || sflag || tflag))
    {
      fputs (_("Option '-S' must be used with either '-d', '-r', '-s', "
               "'-a' or '-t'\n"), stderr);
      usage ();
      usage_try ();
      return EXIT_FAILURE;
    }
  else
    {
      if (unknown_flag)
        {
          non_interactive = 1;
        }
      else if (hflag)
        {
          help_arg ();
          non_interactive = 1;
        }
      else if (vflag)
        {
          version_arg ();
          non_interactive = 1;
        }
      else if (statusflag)
        {
          io_init (cfile, datadir);
          status_arg ();
          non_interactive = 1;
        }
      else if (multiple_flag)
        {
          if (load_data)
            {
              io_init (cfile, datadir);
              io_check_dir (path_dir, (int *)0);
              io_check_dir (path_notes, (int *)0);
            }
          if (iflag)
            {
              io_check_file (path_apts, (int *)0);
              io_check_file (path_todo, (int *)0);
              io_load_app ();
              io_load_todo ();
              io_import_data (IO_IMPORT_ICAL, conf, ifile);
              io_save_apts ();
              io_save_todo ();
              non_interactive = 1;
            }
          if (xflag)
            {
              io_check_file (path_apts, (int *)0);
              io_check_file (path_todo, (int *)0);
              io_load_app ();
              io_load_todo ();
              io_export_data (xfmt, conf);
              non_interactive = 1;
              return non_interactive;
            }
          if (tflag)
            {
              io_check_file (path_todo, (int *)0);
              io_load_todo ();
              todo_arg (tnum, Nflag, preg);
              non_interactive = 1;
            }
          if (nflag)
            {
              io_check_file (path_apts, (int *)0);
              io_load_app ();
              next_arg ();
              non_interactive = 1;
            }
          if (dflag || rflag || sflag)
            {
              io_check_file (path_apts, (int *)0);
              io_check_file (path_conf, (int *)0);
              io_load_app ();
              custom_load_conf (conf, 0); /* To get output date format. */
              if (dflag)
                date_arg (ddate, add_line, Nflag, conf, preg);
              if (rflag || sflag)
                date_arg_extended (startday, range, add_line, Nflag, conf,
                                   preg);
              non_interactive = 1;
            }
          else if (aflag)
            {
              struct date day;

              io_check_file (path_apts, (int *)0);
              io_check_file (path_conf, (int *)0);
              vars_init (conf);
              custom_load_conf (conf, 0); /* To get output date format. */
              io_load_app ();
              day.dd = day.mm = day.yyyy = 0;
              (void)app_arg (add_line, &day, 0, Nflag, conf, preg);
              non_interactive = 1;
            }
        }
      else
        {
          non_interactive = 0;
          io_init (cfile, datadir);
        }
    }

  if (preg)
    regfree (preg);

  return non_interactive;
}