]> Pileus Git - lackey/blobdiff - src/args.c
Refactor main and add print mode
[lackey] / src / args.c
index afc0b481870c01f488edc21e16e195c3fa65fbe6..49fe2f104ace9edd92180ac5ae20137df57f0c46 100644 (file)
 #include <stdio.h>
 #include <getopt.h>
 
-#include "args.h"
 #include "util.h"
+#include "args.h"
+#include "date.h"
+#include "cal.h"
 
-/* Setup info */
-static int    argc;
-static char **argv;
+/* Global data */
+int PRINT  = 0;
+
+/* Local data */
+char **calendars = NULL;
 
 /* Options */
-const char *short_options = "h";
+static char *short_options = "hp::d";
 
 struct option long_options[] = {
-       {"help", 0, NULL, 'h'},
+       {"help",   0, NULL, 'h'},
+       {"print",  2, NULL, 'p'},
 };
 
 /* Usage */
 static void usage(char *name)
 {
        printf("Usage:\n");
-       printf("  %s [OPTION...]\n", name);
+       printf("  %s [OPTION...] [CALENDAR]\n", name);
        printf("\n");
        printf("Options:\n");
-       printf("  -h, --help  Print usage information\n");
-}
-
-/* Initialize */
-void args_setup(int _argc, char **_argv)
-{
-       argc = _argc;
-       argv = _argv;
+       printf("  -h, --help        Print usage information\n");
+       printf("  -p, --print=[dw]  Show upcomming events\n");
 }
 
 /* Initialize */
-void args_init(void)
+void args_setup(int argc, char **argv)
 {
+       /* Parse arguments */
        while (1) {
                int c = getopt_long(argc, argv,
                                short_options, long_options, NULL);
@@ -63,6 +63,43 @@ void args_init(void)
                                usage(argv[0]);
                                exit(0);
                                break;
+                       case 'p':
+                               PRINT = match(optarg, NULL)   ? 1 :
+                                       match(optarg, "d")    ? 1 :
+                                       match(optarg, "day")  ? 1 :
+                                       match(optarg, "w")    ? 7 :
+                                       match(optarg, "week") ? 7 : -1;
+                               break;
                }
        }
+
+       /* Save default calendar */
+       calendars = &argv[optind];
+
+       /* Validate arguments */
+       if (PRINT < 0)
+               error("Unknown print: %s\n", optarg);
+
+       /* Load calendars */
+       for (int i = 0; calendars[i]; i++)
+               cal_config("ical", calendars[i], "location", calendars[i]);
+}
+
+void args_start(void)
+{
+       /* Focus the default calendar */
+       if (calendars[0]) {
+               event_t *event = EVENTS;
+               date_t   start = {SEL.year, SEL.month, SEL.day, 0, 0};
+               while (event && compare(&start, &event->start) > 0)
+                       event = event->next;
+               while (event && !match(calendars[0], event->cal->name))
+                       event = event->next;
+               if (!event)
+                       event = EVENTS;
+               while (event && !match(calendars[0], event->cal->name))
+                       event = event->next;
+               if (event)
+                       SEL = event->start;
+       }
 }