14 #define CONFIGFILE "/.timeplanrc" /* HOME prepended */
15 #define DEF_TIMEPLAN "/.timeplan" /* HOME prepended */
16 #define MARK_DAY "_days"
17 #define DEF_FORMTOTAL ":6"
18 #define LENGTH(x) (sizeof((x))/sizeof(*(x)))
19 #define WHERE1 " in \"%s\" on line %d!\n"
20 #define WHERE2 finame,line
22 #define ERRH1 stderr,"%s: "
24 #define ERRNO1 ": %s!\n"
25 #define ERRNO2 strerror(errno)
28 static const char version[]="This is TimePlan, version 1.0\n";
29 static const char *pname;
32 static int verbose=0,tree=0,doaverage=0;
33 static char *formtotal=DEF_FORMTOTAL;
34 static char buf[LINE_MAX];
36 static enum { SORT_NO,SORT_TIMETOT,SORT_STORES } sortby=SORT_TIMETOT;
37 static unsigned lifetime_days=0;
39 static void usage(void)
43 This command summarizes the timelog information:\n\
45 Usage: timeplan [-u|--unsort] [-s|--stores] [-T|--timetot] [-t|--tree]\n\
46 [-c|--condition <cond>] [-f|--formtotal <fmtstring>]\n\
47 [-r|--rule <ruleL>:<ruleR>]\n\
48 [-v|--verbose] [-h|--help] [-V|--version]\n\
50 -u, --unsort\t\tDon't sort the result in any way\n\
51 -s, --stores\t\tSort the result by stores count\n\
52 -T, --timetot\t\tSort the result by total time (default)\n\
53 -t, --tree\t\tOrganize data as hierarchy tree\n\
54 -a, --average\t\tDisplay all times as average-per-day value\n\
55 -c, --condition\tDefine condition variable\n\
56 -f, --formtotal\tFormat \"Total\" column (\"text *val/val:width text\")\n\
57 -r, --rule\t\tAdd to the end of .rc file this rule (':'->'\\t')\n\
58 -v, --verbose\t\tInform about phases of transfer\n\
59 -h, --help\t\tPrint a summary of the options\n\
60 -V, --version\t\tPrint the version number\n\
65 static const struct option longopts[]={
71 {"condition",1,0,'c'},
72 {"formtotal",1,0,'f'},
76 {"version" ,0,0,'V'}};
78 static int calctime(int timetot,int at,int of)
80 /* FIXME: better distribution */
84 static struct action {
88 } *hashtable[HASH_SIZE];
89 static int hashtable_tot=0;
91 static int dumpaction(const struct action *action)
93 int tot,mins FAKEUSE,hours FAKEUSE,days FAKEUSE,origtot;
97 tot=(action->timetot+30)/60;
98 if (doaverage && lifetime_days)
100 mins=tot; hours=mins/60; days=hours/24;
105 for (s=formtotal;*s;s++)
107 case '*': case '/': {
111 if (s[1]==*s) { s++; goto dump; }
112 for (s2=s+1;isdigit(*s2);s2++);
113 l=strtol(s+1,&end,10);
115 fprintf(ERRH1"Number parse error at column %d of formtotal string!\n",ERRH2,s-formtotal);
123 if (s[1]==*s) { s++; goto dump; }
124 if (!isdigit(s[1])) goto dump;
130 else origtot+=s[1]-'0';
135 if (action) putchar(*s);
139 printf("=%03d/%02d:%02d %4d\t%s\n",days,hours,mins,
140 action->stores,action->what);
146 #define FUNC(which) \
147 static int sort_##which(const struct action **Ap,const struct action **Bp) \
148 { return (B->which>A->which)-(A->which>B->which); }
155 static void dumphashtable(void)
158 struct action *action;
159 struct action **sorta FAKEUSE,**sorti FAKEUSE;
161 if (sortby!=SORT_NO) {
162 int totalwidth=dumpaction(NULL);
164 while (totalwidth-->5) putchar(' ');
165 puts("Total Day Hr Mi Stor\tDescription");
166 if (!(sorta=malloc(sizeof(*sorta)*hashtable_tot))) {
167 fprintf(ERRH1"malloc() of %d pointers"ERRNO1,ERRH2,hashtable_tot,ERRNO2);
172 for (item=0;item<LENGTH(hashtable);item++)
173 for (action=hashtable[item];action;action=action->next)
178 if (sortby==SORT_NO) return;
179 assert(sorti==sorta+hashtable_tot);
180 qsort(sorta,hashtable_tot,sizeof(*sorta),
181 (int (*)(const void *,const void *))(sortby==SORT_TIMETOT ? sort_timetot : sort_stores));
182 for (sorti=sorta;sorti<sorta+hashtable_tot;sorti++)
187 static unsigned calchash(const char *s)
191 while (*s) r=r*7+11*toupper(*s++);
195 static void storeone(char *what,int length)
197 struct action **actionp,*action;
199 if (verbose) printf("storeone: %d: %s\n",length,what);
200 for (actionp=hashtable+(calchash(what)%HASH_SIZE);(action=*actionp);actionp=&action->next)
201 if (!strcasecmp(action->what,what)) break;
203 if (!(action=malloc(sizeof(*action)+strlen(what)))) {
204 fprintf(ERRH1"malloc() for \"%s\""ERRNO1,ERRH2,what,ERRNO2);
210 strcpy(action->what,what);
214 action->timetot+=length;
219 struct textlist *next;
223 struct textlist *conditions,**conditionstail=&conditions;
225 static int iscondition(const char *text)
227 struct textlist *cond;
229 for (cond=conditions;cond;cond=cond->next)
230 if (!strcasecmp(text,cond->text)) return(1);
234 static void addlist(struct textlist ***tail,const char *text,int line)
236 struct textlist *item;
238 if (!(item=malloc(sizeof(*item)+strlen(text)))) {
239 fprintf(ERRH1"malloc() for \"%s\""ERRNO1,ERRH2,text,ERRNO2);
242 strcpy(item->text,text);
248 static struct textlist *modifies,**modifiestail=&modifies,*modifiescmdl,**modifiescmdltail=&modifiescmdl;
249 static int modifies_tot;
250 static struct modistruct {
255 static void modify_load(void)
261 struct textlist *item;
266 if (asprintf(&finame,"%s"CONFIGFILE,getenv("HOME"))==-1) {
267 fprintf(ERRH1"Config filename allocation",ERRH2);
271 if (!(fi=fopen(finame,"rt"))) {
273 fprintf(ERRH1"Config file \"%s\" read"ERRNO1,ERRH2,finame,ERRNO2);
277 while (clearerr(fi),fgets(buf,sizeof(buf),fi)==buf) {
279 if ((s=strchr(buf,'\n')) && !s[1]) *s='\0';
282 fprintf(ERRH1"fgets(3) results not newline-terminated"WHERE1,ERRH2,WHERE2);
285 if (!*buf || *buf=='#') {
290 fprintf(ERRH1"Unrecognized syntax"WHERE1,ERRH2,WHERE2);
297 while (isspace(*s)) s++;
300 fprintf(ERRH1"Invalid character at offset %d"WHERE1,ERRH2,s-buf+1,WHERE2);
303 for (s2=s+1;isalpha(*s2);s2++);
304 if (*s2!='+' && *s2!='-') {
305 fprintf(ERRH1"Only plus ('+') or minus ('-'), not '%c' expected at offset %d"WHERE1,ERRH2,*s2,s2-buf+1,WHERE2);
308 isplus=(*s2=='+'); *s2++='\0';
309 if (iscondition(s)!=isplus)
314 addlist(&modifiestail,s,line);
317 if (!feof(fi) || ferror(fi)) {
318 fprintf(ERRH1"fgets(3) \"%s\""ERRNO1,ERRH2,finame,ERRNO2);
322 fprintf(ERRH1"fclose(3) \"%s\""ERRNO1,ERRH2,finame,ERRNO2);
323 *modifiestail=modifiescmdl; /* add all command-line arguments to the end */
324 if (!(modistructs=malloc(sizeof(*modistructs)*modifies_tot))) {
325 fprintf(ERRH1"malloc() of %d modistructs's"ERRNO1,ERRH2,modifies_tot,ERRNO2);
328 for (m=0,item=modifies;m<modifies_tot;m++,item=item->next) {
329 #define line (item->line)
330 if (!(s=strchr(item->text,'\t'))) {
331 fprintf(ERRH1"No delimiting tab-character found"WHERE1,ERRH2,WHERE2);
334 *s++='\0'; modistructs[m].dst=s;
335 if (verbose) printf("regcomp: %s -> %s\n",item->text,s);
336 if (regcomp(&modistructs[m].regex,item->text,REG_EXTENDED|REG_ICASE)) {
337 fprintf(ERRH1"regcomp() failed for \"%s\" (%s)"WHERE1,ERRH2,item->text,ERRNO2,WHERE2);
346 static char *modify(char *what)
348 regmatch_t matches[10];
350 static char modbuf1[sizeof(buf)],modbuf2[sizeof(modbuf1)];
351 char *src=what,*dstbase=modbuf1,*dst=dstbase;
352 const char *start FAKEUSE,*end FAKEUSE,*patt;
353 const struct textlist *item;
355 if (verbose) printf("modify: %s\n",what);
357 for (m=0,item=modifies;item;m++,item=item->next) {
358 enum { PATT_START,PATT_MID,PATT_END,PATT_TERM } pattpos;
360 i=regexec(&modistructs[m].regex,src,LENGTH(matches),matches,0);
361 if (i==REG_NOMATCH) continue;
363 fprintf(ERRH1"regexec() failed for \"%s\""ERRNO1,ERRH2,item->text,ERRNO2);
366 if (verbose) printf("matched: %s -> %s\n",item->text,modistructs[m].dst);
367 pattpos=PATT_START; patt=NULL;
368 while (pattpos!=PATT_TERM) {
372 end=src+matches->rm_so;
373 pattpos=PATT_MID; patt=modistructs[m].dst;
377 pattpos=PATT_END; patt=NULL;
380 else if (*patt=='@') {
381 start=src; end=src+strlen(src);
383 else if (*patt>='0' && *patt<='9') {
384 regmatch_t *match=matches+(*patt-'0');
386 || match->rm_eo==-1) {
387 fprintf(ERRH1"Trying to substitute '%c' but no \"matches\" entry not set for \"%s\""WHERE1,
388 ERRH2,*patt,item->text,WHERE2);
391 if (match->rm_so>match->rm_eo) {
392 fprintf(ERRH1"Trying to substitute '%c' start>end (%d>%d) for \"%s\""WHERE1,
393 ERRH2,*patt,match->rm_so,match->rm_eo,item->text,WHERE2);
396 start=src+match->rm_so; end=src+match->rm_eo;
399 start=patt; end=patt+1;
404 start=src+matches->rm_eo;
406 pattpos=PATT_TERM; /* assumed: patt=NULL; */
411 if ((dst-dstbase+(end-start))>=sizeof(modbuf1)) {
412 fprintf(ERRH1"Maximum buffer size exceeded during substition for \"%s\""WHERE1,
413 ERRH2,item->text,WHERE2);
416 memcpy(dst,start,end-start);
419 if (dst==dstbase) return(NULL);
422 assert(dstbase==modbuf1);
428 assert((src==modbuf1 && dstbase==modbuf2)
429 ||(src==modbuf2 && dstbase==modbuf1));
430 swap=src; src=dstbase; dst=dstbase=swap;
436 static void store(char *what,int length)
440 if (!(what=modify(what))) {
441 if (verbose) puts("discarded.");
444 if (verbose) printf("store: %d: %s\n",length,what);
445 while ((ce=(tree?strrchr:strchr)(what,'-'))) {
447 storeone(what,length);
448 if (!tree) what=ce+1;
451 storeone(what,length);
454 static void hit(time_t t,char *bufaction)
456 static time_t last=-1;
457 static char bufbackup[sizeof(buf)];
458 char *acts[ACTS_MAX],*s;
461 if (verbose) printf("hit: %ld: %s\n",t,bufaction);
464 fprintf(ERRH1"Time goes backward"WHERE1,ERRH2,WHERE2);
469 while ((s=strchr(acts[acti-1],'+'))) {
472 if (acti>=LENGTH(acts)) {
473 fprintf(ERRH1"Too many '+'-delimited actions (%d)"WHERE1,ERRH2,acti,WHERE2);
479 store(acts[i],calctime(timetot,i,acti));
481 strcpy(bufbackup,bufaction);
485 int main(int argc,char **argv)
488 time_t basetime=-1,currtime;
492 while ((optc=getopt_long(argc,argv,"b:pw:qusTtac:f:r:vhV",longopts,NULL))!=EOF) switch (optc) {
510 if (iscondition(optarg))
511 fprintf(ERRH1"Condition \"%s\" already set!\n",ERRH2,optarg);
512 else addlist(&conditionstail,optarg,-1);
520 if (!(s=strchr(optarg,':'))) {
521 fprintf(ERRH1"No delimiting ':'-character found in -r option \"%s\"!\n",ERRH2,optarg);
524 if (!(dup=strdup(optarg))) {
525 fprintf(ERRH1"malloc() of \"%s\" string"ERRNO1,ERRH2,optarg,ERRNO2);
529 addlist(&modifiescmdltail,dup,0);
536 fprintf(stderr,version);
538 default: /* also 'h' */
543 if (asprintf(&finame,"%s"DEF_TIMEPLAN,getenv("HOME"))==-1) {
544 fprintf(ERRH1"Default timeplan filename allocation",ERRH2);
548 else if (optind+1!=argc) usage();
549 else if (!strcmp(argv[optind],"-")) {
553 else finame=argv[optind];
554 if (!fi && !(fi=fopen(finame,"r"))) {
555 fprintf(ERRH1"open \"%s\" for reading"ERRNO1,ERRH2,finame,ERRNO2);
559 while (clearerr(fi),fgets(buf,sizeof(buf),fi)==buf) {
563 if ((s=strchr(buf,'\n')) && !s[1]) *s='\0';
565 fprintf(ERRH1"fgets(3) results not newline-terminated"WHERE1,ERRH2,WHERE2);
569 if (*buf==':') { /* ":12.7.2000 (St)" */
574 const char *days[]={"Ne","Po","Ut","St","Ct","Pa","So"};
576 assert(LENGTH(days)==7);
577 i=sscanf(buf,":%d.%d.%d (%2s)%n",&tm.tm_mday,&tm.tm_mon,&tm.tm_year,wday,&parsed);
578 if ((i!=4 && i!=5) || parsed!=strlen(buf)) { /* See note in sscanf(3) man page */
579 fprintf(ERRH1"Timestamp with incorrect format"WHERE1,ERRH2,WHERE2);
582 for (i=0;i<7;i++) if (!strcmp(days[i],wday)) break;
584 fprintf(ERRH1"Non-parsable week-day name \"%s\""WHERE1,ERRH2,wday,WHERE2);
585 tm.tm_sec=tm.tm_min=tm.tm_hour=0;
586 tm.tm_wday=tm.tm_yday=-1;
587 tm.tm_isdst=0; /* FIXME */
591 if (t==-1 || tm.tm_wday<0 || tm.tm_wday>6
592 || tm.tm_mday<1 || tm.tm_mday>31
593 || tm.tm_mon <1 || tm.tm_mon >12
594 || tm.tm_year<80 || tm.tm_year>150
596 fprintf(ERRH1"Incorrect timestamp \"%s\""WHERE1,ERRH2,buf,WHERE2);
599 if (i<7 && tm.tm_wday!=i)
600 fprintf(ERRH1"Non-matching week-day, given \"%s\", calculated \"%s\""WHERE1,ERRH2,days[i],days[tm.tm_wday],WHERE2);
601 #define WANTED (60*60*24)
602 if (basetime!=-1 && basetime+WANTED!=t)
603 fprintf(ERRH1"Non-continuous timestamp (%ld, wanted %d) \"%s\""WHERE1,ERRH2,t-basetime,WANTED,buf,WHERE2);
611 if (!(isdigit(buf[0]) && isdigit(buf[1]) && buf[2]==':' && isdigit(buf[3]) && isdigit(buf[4])
612 && (buf[5]=='-' || (buf[5]==':' && isdigit(buf[6]) && isdigit(buf[7]) && buf[8]=='-'))
614 || (sec=0,sscanf(buf,(buf[5]=='-'?"%d:%d-":"%d:%d:%d-"),&hour,&min,&sec)!=2+(buf[5]==':'))
619 fprintf(ERRH1"Incorrect day-time \"%s\""WHERE1,ERRH2,buf,WHERE2);
623 fprintf(ERRH1"Day-time found but no basetime timestamp set"WHERE1,ERRH2,WHERE2);
626 currtime=basetime+(hour*60+min)*60+sec;
627 hit(currtime,buf+(buf[5]=='-'?6:9));
630 if (!feof(fi) || ferror(fi)) {
631 fprintf(ERRH1"fgets(3) \"%s\""ERRNO1,ERRH2,finame,ERRNO2);
634 if (fi!=stdin && fclose(fi))
635 fprintf(ERRH1"fclose(3) \"%s\""ERRNO1,ERRH2,finame,ERRNO2);
638 return(EXIT_SUCCESS);