/* * Copyright (C) 2016 Andy Spencer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include #include "util.h" #include "args.h" #include "date.h" #include "cal.h" /* Global data */ int PRINT = 0; /* Local data */ char **calendars = NULL; /* Options */ static char *short_options = "hp::d"; struct option long_options[] = { {"help", 0, NULL, 'h'}, {"print", 2, NULL, 'p'}, }; /* Usage */ static void usage(char *name) { printf("Usage:\n"); printf(" %s [OPTION...] [CALENDAR]\n", name); printf("\n"); printf("Options:\n"); printf(" -h, --help Print usage information\n"); printf(" -p, --print=[dw] Show upcomming events\n"); } /* Initialize */ void args_setup(int argc, char **argv) { /* Parse arguments */ while (1) { int c = getopt_long(argc, argv, short_options, long_options, NULL); if (c == -1) break; switch (c) { case 'h': 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; } }