/* * 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 "args.h" #include "util.h" #include "conf.h" #include "date.h" #include "cal.h" /* Setup info */ static int argc; static char **argv; /* Options */ const char *short_options = "hdw"; struct option long_options[] = { {"help", 0, NULL, 'h'}, {"day", 2, NULL, 'd'}, {"week", 2, NULL, 'w'}, }; static int print_day = 0; static int print_week = 0; static char *calendar = NULL; /* 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(" -d, --day Show today's events\n"); printf(" -w, --week Show this week's events\n"); } /* Initialize */ void args_setup(int _argc, char **_argv) { argc = _argc; argv = _argv; } /* Initialize */ void args_init(void) { /* 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 'd': print_day = 1; break; case 'w': print_week = 1; break; } } /* Save default calendar */ calendar = argv[optind]; /* Load calendars */ for (int i = optind; i < argc; i++) cal_config("ical", argv[i], "location", argv[i]); /* Validate arguments */ if (print_day && print_week) error("Cannot print both day and week"); } void args_main(void) { /* Focus the default calendar */ if (calendar) { 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(calendar, event->cal->name)) event = event->next; if (!event) event = EVENTS; while (event && !match(calendar, event->cal->name)) event = event->next; if (event) SEL = event->start; } /* Print days or week */ if (print_day || print_week) { event_t *event = EVENTS; for (int d = 0; d < (print_day ? 1 : 7); d++) { /* Get start and end of the day */ date_t start = {SEL.year, SEL.month, SEL.day, 0, 0}; date_t end = {SEL.year, SEL.month, SEL.day, 24, 0}; add_days(&start.year, &start.month, &start.day, d); add_days(&end.year, &end.month, &end.day, d); /* Print day header */ wday_t wday = day_of_week(start.year, start.month, start.day); if (d > 0) printf("\n"); printf("%s, %s %d, %d\n", day_to_string(wday), month_to_string(start.month), start.day+1, start.year); /* Skip forward to the day */ while (event && compare(&start, &event->start) > 0) event = event->next; /* Print event info */ int printed = 0; while (event && compare(&end, &event->start) > 0) { if (printed > 0) printf("\n"); printf("* %02d:%02d - %02d:%02d", event->start.hour, event->start.min, event->end.hour, event->end.min); if (!event->name) printf("\n"); if (event->name) printf(" %s\n", event->name); if (event->loc) printf(" Location: %s\n", event->loc); if (event->desc) printf(" Description: %s\n", event->desc); printed++; event = event->next; } /* Print no events */ if (!printed) printf(" No events\n"); } exit(0); } }