1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
|
/* worklog */
/* program for tracking time spent on different projects */
/* Truxton Fulton */
/* March 27, 1995 */
/* Thanks to :
Tim Newsome <drz@cybercomm.net> (25 Dec 1995)
Egil Kvaleberg <egilk@sn.no> (5 Sep 1996)
Mark Sebastian Fischer <Mark.Sebastian.Fischer@horz.technopark.gmd.de> (1 Nov 1996)
Mike Butler <mgb@mitre.org> (22 Jun 1998)
Vaiva Bichnevicius <vaiva@vaiva.com> (28 Mar 1999)
Gary <glory@cnsp.com> (21 Apr 1999)
for suggestions, bug reports, bug fixes, and improvements
*/
#include "worklog.h"
struct project_list
{
char *name;
int key;
struct project_list *next;
long time;
} *project_head=NULL;
long fetch_total_time(struct project_list *project)
{
long result;
double partial;
char specific_timefile[STRLEN];
char line[STRLEN];
FILE *fa;
char timetype[STRLEN];
result=0.0;
sprintf(specific_timefile,"%s.%c",log_filename,project->key);
fa=fopen(specific_timefile,"r");
if(fa==NULL)
{
fa=fopen(specific_timefile,"w");
if(fa==NULL)
{
fprintf(stderr,"ERROR: cannot open specific project log file : %s\n",specific_timefile);
perror("fopen");
exit(-1);
}
}
else
{
for(;1==fscanf(fa,"%[^\n]",line);
fscanf(fa,"\n"))
{
// [Jon Daley: 2009-09-25] make file parsing more accurate
// by using the amounts within the brackets, rather than
// the (rounded) amounts at the beginning of the line
if(2==sscanf(line,"%*[^[][%lf %s :",&partial, timetype))
{
if(strcmp(timetype, "hours]") == 0)
result+=(long) rint(partial*60.0*60.0) ;
else if(strcmp(timetype, "minutes]") == 0)
result+=(long) rint(partial*60.0) ;
else if(strcmp(timetype, "seconds]") == 0)
result+=(long) rint(partial) ;
else
printf("Couldn't find timetype: %s, %s\n", line, timetype);
}
else
{
printf("Couldn't find timestamp: %s\n", line);
}
}
}
fclose(fa);
return(result);
}
void read_config_file(char *filename)
{
FILE *f;
char firstchar,colonchar;
struct project_list *project;
char names[STRLEN];
int linenum;
project=(struct project_list *)malloc(sizeof(struct project_list));
project->key=CR;
project->next=project_head;
project->time=0;
project->name=strdup("no category yet");
project_head=project;
no_category=project;
num_projects=1;
linenum=0;
project=project_head;
f=fopen(filename,"r");
if(f==NULL)
{
fprintf(stderr,"ERROR: cannot open project configuration file : %s\n",filename);
perror("fopen");
exit(-1);
}
while(!feof(f) && !ferror(f))
{
linenum++;
for(firstchar=0;!feof(f) && !ferror(f) && firstchar<=13;)
fscanf(f,"%c",&firstchar);
if(!feof(f) && !ferror(f))
{
if(firstchar=='#')
{
fscanf(f,"%*[^\n]");
}
else
{
/* must read colon seperator */
fscanf(f,"%c",&colonchar);
if(colonchar!=':')
{
fprintf(stderr,"ERROR: second character in project line must be a colon.\n");
fprintf(stderr," on line %d is '%c'\n",linenum,colonchar);
exit(-1);
}
project=(struct project_list *)malloc(sizeof(struct project_list));
project->key=firstchar;
project->next=project_head;
project->time=fetch_total_time(project);
project_head=project;
fscanf(f,"%[^\n]",names);
project->name=(char *)malloc(1+strlen(names));
sprintf(project->name,"%s",names);
num_projects++;
}
}
}
if (ferror(f))
{
fprintf(stderr,"ERROR: cannot read project configuration file : %s\n",filename);
perror("fscanf");
exit(-1);
}
fclose(f);
}
/* there are multiple interpretations of what the "Del" key is */
int isdelkey(int keypress)
{
return((keypress==127) || \
(keypress==8) || \
(keypress==263) || \
(keypress==330));
}
void draw_main_screen()
{
struct project_list *project;
int i;
/*
move(0,0);
clrtobot();
*/
sprintf(temps,"Worklog version %s",VERSIONS);
attron(A_BOLD);
mvaddstr(0,5,temps);
attroff(A_BOLD);
mvaddstr(1,1,"---------------------------"); /* this fixes a weirdness when using 'screen' !! */
for(i=2,project=project_head;project!=NULL;project=project->next,i++)
{
attron(A_BOLD);
if(project->key!=CR)
{
move(i,1);
sprintf(temps," %c",project->key);
mvaddstr(i,1,temps);
}
else
mvaddstr(i,1," CR");
attroff(A_BOLD);
mvaddstr(i,7,project->name);
}
i++;
move(i,0);
clrtoeol();
attron(A_BOLD);
mvaddstr(i,3,"DEL");
attroff(A_BOLD);
mvaddstr(i,7,"Quit");
i++;
move(i,0);
clrtoeol();
i++;
move(i,0);
clrtoeol();
i++;
move(i,0);
clrtoeol();
i++;
move(i,0);
clrtoeol();
}
void draw_run_options(int key)
{
int i;
attron(A_BOLD);
mvaddstr(y_update,1,"->");
attroff(A_BOLD);
i=num_projects+3;
if(key==CR)
{
attron(A_BOLD);
mvaddstr(i,1," ");
mvaddstr(i,7,"Press any of the category keys above to assign time");
attroff(A_BOLD);
i++;
}
attron(A_BOLD);
mvaddstr(i,1,"SPACE");
attroff(A_BOLD);
mvaddstr(i,7,"Pause clock ");
i++;
if(key!=CR)
{
attron(A_BOLD);
mvaddstr(i,1," CR");
attroff(A_BOLD);
mvaddstr(i,7,"Stop clock and optionally enter description");
i++;
}
attron(A_BOLD);
mvaddstr(i,1," +");
attroff(A_BOLD);
mvaddstr(i,7,"Adjust by increasing time");
i++;
attron(A_BOLD);
mvaddstr(i,1," -");
attroff(A_BOLD);
mvaddstr(i,7,"Adjust by decreasing time");
i++;
attron(A_BOLD);
mvaddstr(i,1," DEL");
attroff(A_BOLD);
mvaddstr(i,7,"Quit");
/* ----------- */
if(key==CR)
{
mvaddstr(1+num_projects,4," ");
}
else
{
move(1+num_projects,0);
clrtoeol();
}
}
void alarm_handler(int flag)
{
(void)flag;
double seconds,minutes,hours;
signal(SIGALRM,alarm_handler);
project_update->time+=1;
if(!update_skip_refresh)
{
delta_time=project_update->time-initial_time;
seconds=(double) delta_time;
if(fabs(seconds)<60.0)
sprintf(temps,"%0.2f seconds",seconds);
else
{
minutes=seconds / 60.0;
if(fabs(minutes)<60.0)
sprintf(temps,"%0.2f minutes",minutes);
else
{
hours = seconds / 3600.0;
sprintf(temps,"%0.2f hours",hours);
}
}
if(project_update!=no_category)
{
seconds=(double) project_update->time;
if(fabs(seconds)<60.0)
sprintf(temps2," (total %0.2f seconds)",seconds);
else
{
minutes=seconds / 60.0;
if(fabs(minutes)<60.0)
sprintf(temps2," (total %0.2f minutes)",minutes);
else
{
hours = seconds / 3600.0;
sprintf(temps2," (total %0.2f hours)",hours);
}
}
strcat(temps,temps2);
}
attron(A_UNDERLINE);
move(y_update,x_update);
clrtoeol();
mvaddstr(y_update,x_update,temps);
attroff(A_UNDERLINE);
}
}
void do_resize()
{
endwin();
/* usleep(10000); */
/* fprintf(stderr,"{RESIZE}"); */
/* resizeterm(y,x); */
initscr();
cbreak();
noecho();
keypad(stdscr,TRUE);
IDLE_Y = LINES-1;
draw_main_screen();
if(current_project!=NULL)
draw_run_options(current_project->key);
}
void resize_handler(int flag)
{
(void)flag;
signal(SIGWINCH,resize_handler);
ungetch(12);
}
void exit_handler(int flag)
{
(void)flag;
struct project_list *project;
FILE *f;
double seconds,minutes,hours;
time_t t;
f=fopen(log_filename,"a");
if(f==NULL)
{
fprintf(stderr,"ERROR: cannot open project log file : %s\n",log_filename);
perror("fopen");
exit(-1);
}
fprintf(f,"-- Worklog summary begins : %s --\n",starttimes);
for(project=project_head;project!=NULL;project=project->next)
{
if(!seconds)
continue;
seconds=(double) project->time;
if(fabs(seconds)<60.0)
sprintf(temps,"%0.2f seconds",seconds);
else
{
minutes=seconds / 60.0;
if(fabs(minutes)<60.0)
sprintf(temps,"%0.2f minutes",minutes);
else
{
hours = seconds / 3600.0;
sprintf(temps,"%0.2f hours",hours);
}
}
fprintf(f,"%s : total %s\n",project->name,temps);
}
time(&t);
sprintf(temps,"%s",asctime(localtime(&t)));
if(temps[strlen(temps)-1]<14)
temps[strlen(temps)-1]=0;
fprintf(f,"-- Worklog summary ends : %s --\n\n",temps);
fclose(f);
endwin();
fprintf(stderr,"\n");
exit(0);
}
int clock_on(int key)
{
struct project_list *project;
struct itimerval timer_value;
int i,j,found,quit;
int keypress;
int modification,success;
FILE *f,*fa;
time_t t;
double seconds,minutes,hours;
char comments[STRLEN];
char specific_timefile[STRLEN];
comments[0]=0;
keypress=key;
for(found=0,i=2,project=project_head;!found && project!=NULL;project=project->next,i++)
if(project->key==key) /* try to find exact match */
{
found=1;
break;
}
if(!found)
for(found=0,i=2,project=project_head;!found && project!=NULL;project=project->next,i++)
if(tolower(project->key)==tolower(key)) /* try to find case-insensitive match */
{
found=1;
key=project->key; /* remember exact key */
break;
}
if(found)
{
current_project=project;
initial_time=project->time;
if(key!=CR) /* transfer time from no category */
{
project->time+=no_category->time;
no_category->time=0;
}
project_update=project;
found=1;
j=strlen(project->name)+10;
y_update=i;
x_update=j;
draw_run_options(key);
timer_value.it_value.tv_sec=1;
timer_value.it_value.tv_usec=0;
timer_value.it_interval.tv_sec=1;
timer_value.it_interval.tv_usec=0;
setitimer(ITIMER_REAL,&timer_value,0);
for(quit=0;!quit;)
{
update_skip_refresh=0;
nodelay(stdscr,TRUE);
for(keypress=ERR;keypress==ERR;usleep(100000))
keypress=mvgetch(IDLE_Y,IDLE_X);
update_skip_refresh=1;
nodelay(stdscr,FALSE);
switch(keypress)
{
case ' ' :
timer_value.it_value.tv_sec=0;
timer_value.it_value.tv_usec=0;
timer_value.it_interval.tv_sec=0;
timer_value.it_interval.tv_usec=0;
setitimer(ITIMER_REAL,&timer_value,0);
attron(A_BLINK);
mvaddstr(y_update,1,"->");
attroff(A_BLINK);
i=num_projects+9;
mvaddstr(i,5,"-- Clock paused. Press any key to resume --");
move(i,49);
keypress=getch();
move(i,0);
clrtoeol();
attron(A_BOLD);
mvaddstr(y_update,1,"->");
attroff(A_BOLD);
timer_value.it_value.tv_sec=1;
timer_value.it_value.tv_usec=0;
timer_value.it_interval.tv_sec=1;
timer_value.it_interval.tv_usec=0;
setitimer(ITIMER_REAL,&timer_value,0);
break;
case '+' :
i=num_projects+9;
attrset(A_BOLD);
mvaddstr(i,5,"Enter number of minutes to add : ");
attrset(A_BOLD);
echo();
success=(mvscanw(i,38,"%d",&modification)==1);
noecho();
if(success)
if(modification<0)
success=0;
attrset(A_NORMAL);
move(i,0);
clrtoeol();
if(success)
{
project->time+=60*modification;
}
else
{
beep();
mvaddstr(i,5,"Error : input value must be a positive integer");
mvaddstr(i+1,5,"-- Press any key --");
move(i+1,24);
keypress=getch();
move(i,0);
clrtoeol();
move(i+1,0);
clrtoeol();
}
break;
case '-' :
i=num_projects+9;
attrset(A_BOLD);
mvaddstr(i,5,"Enter number of minutes to subtract : ");
attrset(A_BOLD);
echo();
success=(mvscanw(i,43,"%d",&modification)==1);
noecho();
if(success)
if(modification<0)
success=0;
attrset(A_NORMAL);
move(i,0);
clrtoeol();
if(success)
{
project->time-=60*modification;
}
else
{
beep();
mvaddstr(i,5,"Error : input value must be a positive integer");
mvaddstr(i+1,5,"-- Press any key --");
move(i+1,24);
keypress=getch();
move(i,0);
clrtoeol();
move(i+1,0);
clrtoeol();
}
break;
case CR :
if(key!=CR)
{
i=num_projects+9;
attrset(A_BOLD);
mvaddstr(i,5,"Enter comment : ");
attrset(A_BOLD);
echo();
mvgetstr(i,21,comments);
noecho();
attrset(A_NORMAL);
move(i,0);
clrtobot();
quit=1;
}
break;
case 12 : /* control-L */
#ifdef KEY_RESIZE
case KEY_RESIZE : /* sent by ncurses sometimes */
#endif
do_resize();
break;;;
default :
beep();
quit=1;
break;
}
}
if(key!=CR)
{
sprintf(specific_timefile,"%s.%c",log_filename,key);
f=fopen(log_filename,"a");
if(f==NULL)
{
fprintf(stderr,"ERROR: cannot open project log file : %s\n",log_filename);
perror("fopen");
exit(-1);
}
fa=fopen(specific_timefile,"a");
if(fa==NULL)
{
fprintf(stderr,"ERROR: cannot open specific project log file : %s\n",specific_timefile);
perror("fopen");
exit(-1);
}
fprintf(f,"%s ",project->name);
delta_time=project->time-initial_time;
seconds=(double) delta_time;
hours = seconds / 3600.0;
sprintf(temps,"%8.2f hours : ",hours);
fprintf(fa,"%s",temps);
if(strlen(comments)>0)
{
fprintf(f,"(%s) ",comments);
fprintf(fa,"%s",comments);
}
else
fprintf(fa,"<no description>");
seconds=(double) delta_time;
if(fabs(seconds)<60.0)
sprintf(temps,"%0.2f seconds",seconds);
else
{
minutes=seconds / 60.0;
if(fabs(minutes)<60.0)
sprintf(temps,"%0.2f minutes",minutes);
else
{
hours = seconds / 3600.0;
sprintf(temps,"%0.2f hours",hours);
}
}
fprintf(f,": %s : finished ",temps);
fprintf(fa," : [%s] : finished ",temps);
time(&t);
sprintf(temps,"%s",asctime(localtime(&t)));
if(temps[strlen(temps)-1]<14)
temps[strlen(temps)-1]=0;
fprintf(f,"%s\n",temps);
fprintf(fa,"%s\n",temps);
fclose(f);
fclose(fa);
}
timer_value.it_value.tv_sec=0;
timer_value.it_value.tv_usec=0;
timer_value.it_interval.tv_sec=0;
timer_value.it_interval.tv_usec=0;
setitimer(ITIMER_REAL,&timer_value,0);
update_skip_refresh=0;
// compensate for extra time about to be added incorrectly
project->time-=1;
alarm_handler(0);
mvaddstr(y_update,1," ");
}
else
if(!isdelkey(key) && key!=12)
{
/* fprintf(stderr,"\n%c%cunknown key=%d (press control-L to refresh screen)%c%c\n",10,13,key,10,13); */
beep();
keypress=ERR;
}
/* no current project now... */
current_project=NULL;
return(keypress);
}
int main(int argc, char **argv)
{
char *config_filename;
int keypress;
int quit,i;
FILE *lf;
time_t t;
time(&t);
if(argc>1)
{
if(argc>3 ||
(strstr(argv[1],"-h")==argv[1]) ||
(strstr(argv[1],"-?")==argv[1]))
{
fprintf(stderr,"USAGE: %s [<project config file> [<log file>]]\n",argv[0]);
exit(0);
}
}
#ifdef DEFAULT_DIR_ENVVAR
else
{ /* no args given */
const char* default_dir;
if((default_dir=getenv(DEFAULT_DIR_ENVVAR)))
{
if(chdir(default_dir))
{
fprintf(stderr,"WARNING: Could not change to default directory ($%s = %s)\n",DEFAULT_DIR_ENVVAR,default_dir);
perror("chdir");
}
else
{
fprintf(stderr,"INFO: Using default directory %s (environment variable %s)\n",default_dir,DEFAULT_DIR_ENVVAR);
}
}
}
#endif /* def DEFAULT_DIR_ENVVAR */
if (argc>2)
log_filename=argv[2];
else
log_filename=DEFAULT_LOG_FILE;
if (argc>1)
config_filename=argv[1];
else
config_filename=DEFAULT_CONFIG_FILE;
read_config_file(config_filename);
lf=fopen(log_filename,"a");
if(lf==NULL)
{
fprintf(stderr,"ERROR: cannot open project log file : %s\n",log_filename);
perror("fopen");
exit(-1);
}
fclose(lf);
sprintf(starttimes,"%s",asctime(localtime(&t)));
if(starttimes[strlen(starttimes)-1]<14)
starttimes[strlen(starttimes)-1]=0;
initscr();
cbreak();
noecho();
keypad(stdscr,TRUE);
IDLE_Y = LINES-1;
signal(SIGALRM,alarm_handler);
signal(SIGHUP,exit_handler);
signal(SIGINT,exit_handler);
signal(SIGQUIT,exit_handler);
signal(SIGTERM,exit_handler);
signal(SIGWINCH,resize_handler);
keypress=CR;
draw_main_screen();
for(quit=0;!quit;)
{
if(keypress==CR)
keypress=mvgetch(IDLE_Y,IDLE_X);
keypress=clock_on(keypress);
draw_main_screen();
if(keypress==ERR)
keypress=mvgetch(IDLE_Y,IDLE_X);
if(keypress==12)
{
do_resize();
keypress=CR;
}
if(isdelkey(keypress))
{
i=num_projects+5;
attron(A_BLINK);
mvaddstr(i,5,"Press DEL once more to quit");
beep();
attroff(A_BLINK);
keypress=mvgetch(IDLE_Y,IDLE_X);
mvaddstr(i,5," ");
if(isdelkey(keypress))
quit=1;
keypress=CR;
}
}
exit_handler(0);
return(0);
}
|