]> Pileus Git - lackey/blob - src/args.c
49fe2f104ace9edd92180ac5ae20137df57f0c46
[lackey] / src / args.c
1 /*
2  * Copyright (C) 2016 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <getopt.h>
21
22 #include "util.h"
23 #include "args.h"
24 #include "date.h"
25 #include "cal.h"
26
27 /* Global data */
28 int PRINT  = 0;
29
30 /* Local data */
31 char **calendars = NULL;
32
33 /* Options */
34 static char *short_options = "hp::d";
35
36 struct option long_options[] = {
37         {"help",   0, NULL, 'h'},
38         {"print",  2, NULL, 'p'},
39 };
40
41 /* Usage */
42 static void usage(char *name)
43 {
44         printf("Usage:\n");
45         printf("  %s [OPTION...] [CALENDAR]\n", name);
46         printf("\n");
47         printf("Options:\n");
48         printf("  -h, --help        Print usage information\n");
49         printf("  -p, --print=[dw]  Show upcomming events\n");
50 }
51
52 /* Initialize */
53 void args_setup(int argc, char **argv)
54 {
55         /* Parse arguments */
56         while (1) {
57                 int c = getopt_long(argc, argv,
58                                 short_options, long_options, NULL);
59                 if (c == -1)
60                         break;
61                 switch (c) {
62                         case 'h':
63                                 usage(argv[0]);
64                                 exit(0);
65                                 break;
66                         case 'p':
67                                 PRINT = match(optarg, NULL)   ? 1 :
68                                         match(optarg, "d")    ? 1 :
69                                         match(optarg, "day")  ? 1 :
70                                         match(optarg, "w")    ? 7 :
71                                         match(optarg, "week") ? 7 : -1;
72                                 break;
73                 }
74         }
75
76         /* Save default calendar */
77         calendars = &argv[optind];
78
79         /* Validate arguments */
80         if (PRINT < 0)
81                 error("Unknown print: %s\n", optarg);
82
83         /* Load calendars */
84         for (int i = 0; calendars[i]; i++)
85                 cal_config("ical", calendars[i], "location", calendars[i]);
86 }
87
88 void args_start(void)
89 {
90         /* Focus the default calendar */
91         if (calendars[0]) {
92                 event_t *event = EVENTS;
93                 date_t   start = {SEL.year, SEL.month, SEL.day, 0, 0};
94                 while (event && compare(&start, &event->start) > 0)
95                         event = event->next;
96                 while (event && !match(calendars[0], event->cal->name))
97                         event = event->next;
98                 if (!event)
99                         event = EVENTS;
100                 while (event && !match(calendars[0], event->cal->name))
101                         event = event->next;
102                 if (event)
103                         SEL = event->start;
104         }
105 }