ics2tree.c - ics2txt - convert icalendar .ics file to plain text HTML git clone git://bitreich.org/ics2txt git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/ics2txt DIR Log DIR Files DIR Refs DIR Tags DIR README --- ics2tree.c (1959B) --- 1 #include <unistd.h> 2 #include <errno.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 #include <strings.h> 7 #include "ical.h" 8 #include "util.h" 9 10 #ifndef __OpenBSD__ 11 #define pledge(...) 0 12 #endif 13 14 static void 15 print_ruler(int level) 16 { 17 while (level-- > 0) 18 fprintf(stdout, ": "); 19 } 20 21 static int 22 fn_field_name(IcalParser *p, char *name) 23 { 24 print_ruler(ical_get_level(p)); 25 printf("name %s\n", name); 26 fflush(stdout); 27 return 0; 28 } 29 30 static int 31 fn_block_begin(IcalParser *p, char *name) 32 { 33 print_ruler(ical_get_level(p) - 1); 34 printf("begin %s\n", name); 35 fflush(stdout); 36 return 0; 37 } 38 39 static int 40 fn_param_value(IcalParser *p, char *name, char *value) 41 { 42 print_ruler(ical_get_level(p) + 1); 43 printf("param %s=%s\n", name, value); 44 fflush(stdout); 45 return 0; 46 } 47 48 static int 49 fn_field_value(IcalParser *p, char *name, char *value) 50 { 51 size_t len; 52 (void)name; 53 54 if (ical_get_value(p, value, &len) < 0) 55 return -1; 56 print_ruler(ical_get_level(p) + 1); 57 if (strcasecmp(name, "DTSTART") == 0 || 58 strcasecmp(name, "DTSTAMP") == 0 || 59 strcasecmp(name, "DTEND") == 0) { 60 time_t t; 61 if (ical_get_time(p, value, &t) != 0) 62 warn("%s: %s", p->errmsg, value); 63 printf("epoch %lld\n", t); 64 } else { 65 printf("value %s\n", value); 66 } 67 fflush(stdout); 68 return 0; 69 } 70 71 int 72 main(int argc, char **argv) 73 { 74 IcalParser p = {0}; 75 arg0 = *argv++; 76 77 if (pledge("stdio", "") < 0) 78 err(1, "pledge: %s", strerror(errno)); 79 80 p.fn_field_name = fn_field_name; 81 p.fn_block_begin = fn_block_begin; 82 p.fn_param_value = fn_param_value; 83 p.fn_field_value = fn_field_value; 84 85 if (*argv == NULL) { 86 if (ical_parse(&p, stdin) < 0) 87 err(1, "parsing stdin:%d: %s", p.linenum, p.errmsg); 88 } 89 90 for (; *argv != NULL; argv++, argc--) { 91 FILE *fp; 92 93 debug("converting \"%s\"", *argv); 94 if ((fp = fopen(*argv, "r")) == NULL) 95 err(1, "opening %s", *argv); 96 if (ical_parse(&p, fp) < 0) 97 err(1, "parsing %s:%d: %s", *argv, p.linenum, p.errmsg); 98 fclose(fp); 99 } 100 return 0; 101 }