diff options
Diffstat (limited to 'src/args.c')
-rw-r--r-- | src/args.c | 320 |
1 files changed, 141 insertions, 179 deletions
@@ -45,14 +45,23 @@ #include "calcurse.h" +/* Long options */ +enum { + OPT_FMT_APT = 1000, + OPT_FMT_RAPT, + OPT_FMT_EV, + OPT_FMT_REV, + OPT_FMT_TODO +}; + /* * Print Calcurse usage and exit. */ static void usage () { - char *arg_usage = - _("Usage: calcurse [-h|-v] [-N] [-an] [-t[num]] [-i<file>] [-x[format]]\n" + const char *arg_usage = + _("Usage: calcurse [-g|-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); @@ -61,7 +70,7 @@ usage () static void usage_try () { - char *arg_usage_try = _("Try 'calcurse -h' for more information.\n"); + const char *arg_usage_try = _("Try 'calcurse -h' for more information.\n"); fputs (arg_usage_try, stdout); } @@ -71,14 +80,11 @@ usage_try () static void version_arg () { - char vtitle[BUFSIZ]; - char *vtext = + const char *vtext = _("\nCopyright (c) 2004-2011 calcurse Development Team.\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); + fprintf (stdout, _("Calcurse %s - text-based organizer\n"), VERSION); fputs (vtext, stdout); } @@ -88,8 +94,7 @@ version_arg () static void help_arg () { - char htitle[BUFSIZ]; - char *htext = + const char *htext = _("\nMiscellaneous:\n" " -h, --help\n" " print this help and exit.\n" @@ -110,6 +115,8 @@ help_arg () " 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 -g, --gc\n" + " run the garbage collector for note files and exit. \n" "\n -i <file>, --import <file>\n" " import the icalendar data contained in <file>. \n" "\n -n, --next\n" @@ -146,9 +153,7 @@ help_arg () "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); + fprintf (stdout, _("Calcurse %s - text-based organizer\n"), VERSION); usage (); fputs (htext, stdout); } @@ -183,59 +188,7 @@ status_arg (void) 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; - - if (nbtab < BUFSIZ) - { - for (i = 0; i < nbtab; i++) - linestarter[i] = '\t'; - linestarter[nbtab] = '\0'; - } - else - linestarter[0] = '\0'; - - (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); - } + puts (_("calcurse is not running\n")); } /* @@ -246,11 +199,11 @@ print_notefile (FILE *out, char *filename, int nbtab) * If regex is not null, only the matching todos are printed. */ static void -todo_arg (int priority, int print_note, regex_t *regex) +todo_arg (int priority, const char *format, regex_t *regex) { llist_item_t *i; int title = 1; - char *titlestr, priority_str[BUFSIZ] = ""; + char *titlestr; char *all_todos_title = _("to do:\n"); char *completed_title = _("completed tasks:\n"); @@ -264,15 +217,6 @@ todo_arg (int priority, int print_note, regex_t *regex) } \ } 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); @@ -284,7 +228,7 @@ todo_arg (int priority, int print_note, regex_t *regex) if (priority == 0) { DISPLAY_TITLE; - DISPLAY_TODO; + print_todo (format, todo); } } else @@ -292,13 +236,12 @@ todo_arg (int priority, int print_note, regex_t *regex) if (priority < 0 || todo->id == priority) { DISPLAY_TITLE; - DISPLAY_TODO; + print_todo (format, todo); } } } #undef DISPLAY_TITLE -#undef DISPLAY_TODO } /* Print the next appointment within the upcoming 24 hours. */ @@ -323,8 +266,8 @@ next_arg (void) 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); + snprintf (mesg, BUFSIZ, " [%02d:%02d] %s\n", hours_left, min_left, + next_app.txt); fputs (mesg, stdout); mem_free (next_app.txt); } @@ -334,7 +277,7 @@ next_arg (void) * Print the date on stdout. */ static void -arg_print_date (long date, struct conf *conf) +arg_print_date (long date) { char date_str[BUFSIZ]; time_t t; @@ -342,7 +285,7 @@ arg_print_date (long date, struct conf *conf) t = date; lt = localtime (&t); - strftime (date_str, BUFSIZ, conf->output_datefmt, lt); + strftime (date_str, BUFSIZ, conf.output_datefmt, lt); fputs (date_str, stdout); fputs (":\n", stdout); } @@ -354,15 +297,14 @@ arg_print_date (long date, struct conf *conf) * 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) +app_arg (int add_line, struct date *day, long date, const char *fmt_apt, + const char *fmt_rapt, const char *fmt_ev, const char *fmt_rev, + regex_t *regex) { llist_item_t *i, *j; 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); @@ -388,17 +330,13 @@ app_arg (int add_line, struct date *day, long date, int print_note, } if (print_date) { - arg_print_date (today, conf); + arg_print_date (today); print_date = 0; } - fputs (" * ", stdout); - fputs (re->mesg, stdout); - fputs ("\n", stdout); - if (print_note && re->note) - print_notefile (stdout, re->note, 2); + print_recur_event (fmt_rev, today, re); } - LLIST_FIND_FOREACH (&eventlist, today, event_inday, i) + LLIST_FIND_FOREACH_CONT (&eventlist, today, event_inday, i) { struct event *ev = LLIST_TS_GET_DATA (i); if (regex && regexec (regex, ev->mesg, 0, 0, 0) != 0) @@ -412,14 +350,10 @@ app_arg (int add_line, struct date *day, long date, int print_note, } if (print_date) { - arg_print_date (today, conf); + arg_print_date (today); print_date = 0; } - fputs (" * ", stdout); - fputs (ev->mesg, stdout); - fputs ("\n", stdout); - if (print_note && ev->note) - print_notefile (stdout, ev->note, 2); + print_event (fmt_ev, today, ev); } /* Same process is performed but this time on the appointments. */ @@ -436,6 +370,7 @@ app_arg (int add_line, struct date *day, long date, int print_note, { struct apoint *apt = LLIST_TS_GET_DATA (i); struct recur_apoint *ra = LLIST_TS_GET_DATA (j); + unsigned occurrence; while (i && regex && regexec (regex, apt->mesg, 0, 0, 0) != 0) { @@ -451,7 +386,8 @@ app_arg (int add_line, struct date *day, long date, int print_note, if (apt && ra) { - if (apt->start <= recur_apoint_inday (ra, today)) + if (recur_apoint_find_occurrence (ra, today, &occurrence) && + apt->start <= occurrence) ra = NULL; else apt = NULL; @@ -467,19 +403,10 @@ app_arg (int add_line, struct date *day, long date, int print_note, } if (print_date) { - arg_print_date (today, conf); + arg_print_date (today); 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); + print_apoint (fmt_apt, today, apt); i = LLIST_TS_FIND_NEXT (i, today, apoint_inday); } else if (ra) @@ -492,23 +419,11 @@ app_arg (int add_line, struct date *day, long date, int print_note, } if (print_date) { - arg_print_date (today, conf); + arg_print_date (today); 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); + recur_apoint_find_occurrence (ra, today, &occurrence); + print_recur_apoint (fmt_rapt, today, occurrence, ra); apt = NULL; j = LLIST_TS_FIND_NEXT (j, today, recur_apoint_inday); } @@ -517,7 +432,7 @@ app_arg (int add_line, struct date *day, long date, int print_note, LLIST_TS_UNLOCK (&recur_alist_p); LLIST_TS_UNLOCK (&alist_p); - return (app_found); + return app_found; } static void @@ -535,8 +450,9 @@ more_info (void) * 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) +display_app (struct tm *t, int numdays, int add_line, const char *fmt_apt, + const char *fmt_rapt, const char *fmt_ev, const char *fmt_rev, + regex_t *regex) { int i, app_found; struct date day; @@ -546,11 +462,12 @@ display_app (struct tm *t, int numdays, int add_line, int print_note, 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); + app_found = app_arg (add_line, &day, 0, fmt_apt, fmt_rapt, fmt_ev, + fmt_rev, regex); if (app_found) add_line = 1; t->tm_mday++; - (void)mktime (t); + mktime (t); } } @@ -559,8 +476,8 @@ display_app (struct tm *t, int numdays, int add_line, int print_note, * days. */ static void -date_arg (char *ddate, int add_line, int print_note, struct conf *conf, - regex_t *regex) +date_arg (char *ddate, int add_line, const char *fmt_apt, const char *fmt_rapt, + const char *fmt_ev, const char *fmt_rev, regex_t *regex) { int i; struct date day; @@ -591,22 +508,23 @@ date_arg (char *ddate, int add_line, int print_note, struct conf *conf, */ timer = time (NULL); t = *localtime (&timer); - display_app (&t, numdays, add_line, print_note, conf, regex); + display_app (&t, numdays, add_line, fmt_apt, fmt_rapt, fmt_ev, fmt_rev, + regex); } else { /* a date was entered */ - if (parse_date (ddate, conf->input_datefmt, (int *)&day.yyyy, + 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); + app_arg (add_line, &day, 0, fmt_apt, fmt_rapt, fmt_ev, fmt_rev, 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)); + snprintf (outstr, BUFSIZ, + "Possible argument format are: '%s' or 'n'\n", + DATEFMT_DESC (conf.input_datefmt)); fputs (_(outstr), stdout); more_info (); } @@ -622,8 +540,9 @@ date_arg (char *ddate, int add_line, int print_note, struct conf *conf, * 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) +date_arg_extended (char *startday, char *range, int add_line, + const char *fmt_apt, const char *fmt_rapt, + const char *fmt_ev, const char *fmt_rev, regex_t *regex) { int i, numdays = 1, error = 0, arg_len = 0; static struct tm t; @@ -647,12 +566,12 @@ date_arg_extended (char *startday, char *range, int add_line, int print_note, t = *localtime (&timer); if (startday != NULL) { - if (parse_date (startday, conf->input_datefmt, (int *)&t.tm_year, + 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); + mktime (&t); } else { @@ -661,15 +580,16 @@ date_arg_extended (char *startday, char *range, int add_line, int print_note, } if (!error) { - display_app (&t, numdays, add_line, print_note, conf, regex); + display_app (&t, numdays, add_line, fmt_apt, fmt_rapt, fmt_ev, fmt_rev, + 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)); + 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 (); @@ -682,7 +602,7 @@ date_arg_extended (char *startday, char *range, int add_line, int print_note, * routines to handle those arguments. Also initialize the data paths. */ int -parse_args (int argc, char **argv, struct conf *conf) +parse_args (int argc, char **argv) { int ch, add_line = 0; int unknown_flag = 0; @@ -692,15 +612,21 @@ parse_args (int argc, char **argv, struct conf *conf) 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 gflag = 0; /* -g: run garbage collector */ 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 */ + /* Format strings */ + const char *fmt_apt = " - %S -> %E\n\t%m\n"; + const char *fmt_rapt = " - %S -> %E\n\t%m\n"; + const char *fmt_ev = " * %m\n"; + const char *fmt_rev = " * %m\n"; + const char *fmt_todo = "%p. %m\n"; int tnum = 0, xfmt = 0, non_interactive = 0, multiple_flag = 0, load_data = 0; char *ddate = "", *cfile = NULL, *range = NULL, *startday = NULL; @@ -714,13 +640,14 @@ parse_args (int argc, char **argv, struct conf *conf) STATUS_OPT = CHAR_MAX + 1 }; - static char *optstr = "hvnNax::t::d:c:r::s::S:D:i:"; + static char *optstr = "ghvnNax::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'}, + {"gc", no_argument, NULL, 'g'}, {"help", no_argument, NULL, 'h'}, {"import", required_argument, NULL, 'i'}, {"next", no_argument, NULL, 'n'}, @@ -732,6 +659,12 @@ parse_args (int argc, char **argv, struct conf *conf) {"todo", optional_argument, NULL, 't'}, {"version", no_argument, NULL, 'v'}, {"export", optional_argument, NULL, 'x'}, + + {"format-apt", required_argument, NULL, OPT_FMT_APT}, + {"format-recur-apt", required_argument, NULL, OPT_FMT_RAPT}, + {"format-event", required_argument, NULL, OPT_FMT_EV}, + {"format-recur-event", required_argument, NULL, OPT_FMT_REV}, + {"format-todo", required_argument, NULL, OPT_FMT_TODO}, {NULL, no_argument, NULL, 0} }; @@ -766,6 +699,9 @@ parse_args (int argc, char **argv, struct conf *conf) case 'h': hflag = 1; break; + case 'g': + gflag = 1; + break; case 'i': iflag = 1; multiple_flag++; @@ -777,9 +713,6 @@ parse_args (int argc, char **argv, struct conf *conf) multiple_flag++; load_data++; break; - case 'N': - Nflag = 1; - break; case 'r': rflag = 1; multiple_flag++; @@ -812,7 +745,7 @@ parse_args (int argc, char **argv, struct conf *conf) { usage (); usage_try (); - return (EXIT_FAILURE); + return EXIT_FAILURE; } } else @@ -845,6 +778,21 @@ parse_args (int argc, char **argv, struct conf *conf) xfmt = IO_EXPORT_ICAL; } break; + case OPT_FMT_APT: + fmt_apt = optarg; + break; + case OPT_FMT_RAPT: + fmt_rapt = optarg; + break; + case OPT_FMT_EV: + fmt_ev = optarg; + break; + case OPT_FMT_REV: + fmt_rev = optarg; + break; + case OPT_FMT_TODO: + fmt_todo = optarg; + break; default: usage (); usage_try (); @@ -900,75 +848,89 @@ parse_args (int argc, char **argv, struct conf *conf) status_arg (); non_interactive = 1; } + else if (gflag) + { + io_init (cfile, datadir); + io_check_dir (path_dir, NULL); + io_check_dir (path_notes, NULL); + io_check_file (path_apts, NULL); + io_check_file (path_todo, NULL); + io_load_app (); + io_load_todo (); + note_gc (); + 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); + io_check_dir (path_dir, NULL); + io_check_dir (path_notes, NULL); } if (iflag) { - io_check_file (path_apts, (int *)0); - io_check_file (path_todo, (int *)0); + io_check_file (path_apts, NULL); + io_check_file (path_todo, NULL); /* Get default pager in case we need to show a log file. */ - vars_init (conf); + vars_init (); io_load_app (); io_load_todo (); - io_import_data (IO_IMPORT_ICAL, conf, ifile); + io_import_data (IO_IMPORT_ICAL, 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_check_file (path_apts, NULL); + io_check_file (path_todo, NULL); io_load_app (); io_load_todo (); - io_export_data (xfmt, conf); + io_export_data (xfmt); non_interactive = 1; return non_interactive; } if (tflag) { - io_check_file (path_todo, (int *)0); + io_check_file (path_todo, NULL); io_load_todo (); - todo_arg (tnum, Nflag, preg); + todo_arg (tnum, fmt_todo, preg); non_interactive = 1; } if (nflag) { - io_check_file (path_apts, (int *)0); + io_check_file (path_apts, NULL); 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_check_file (path_apts, NULL); + io_check_file (path_conf, NULL); io_load_app (); - custom_load_conf (conf, 0); /* To get output date format. */ + custom_load_conf (); /* To get output date format. */ if (dflag) - date_arg (ddate, add_line, Nflag, conf, preg); + date_arg (ddate, add_line, fmt_apt, fmt_rapt, fmt_ev, fmt_rev, + preg); if (rflag || sflag) - date_arg_extended (startday, range, add_line, Nflag, conf, - preg); + date_arg_extended (startday, range, add_line, fmt_apt, + fmt_rapt, fmt_ev, fmt_rev, 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_check_file (path_apts, NULL); + io_check_file (path_conf, NULL); + vars_init (); + custom_load_conf (); /* 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); + app_arg (add_line, &day, 0, fmt_apt, fmt_rapt, fmt_ev, fmt_rev, + preg); non_interactive = 1; } } |