bday
changeset 15:032af48d590b
reformating of the source code
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Mon, 24 Feb 2014 17:46:57 +0100 |
parents | a56120a4678f |
children | 79d22407a6be |
files | bday.c |
diffstat | 1 files changed, 210 insertions(+), 168 deletions(-) [+] |
line diff
1.1 --- a/bday.c Thu Nov 13 13:31:33 2008 +0100 1.2 +++ b/bday.c Mon Feb 24 17:46:57 2014 +0100 1.3 @@ -1,55 +1,54 @@ 1.4 /* 1.5 - bday 1.6 +bday -- Birthday/Anniversary reminder 1.7 1.8 - Birthday/Anniversary reminder 1.9 +(c) 2007,2014 markus schnalke <meillo@marmaro.de> 1.10 +(c) 1994-1999 AS Mortimer 1.11 1.12 - (c) 2007 markus schnalke <meillo@marmaro.de> 1.13 - (c) 1994-1999 AS Mortimer 1.14 +This program is free software; you can redistribute it and/or 1.15 +modify it under the terms of the GNU General Public License as 1.16 +published by the Free Software Foundation; either version 2 of the 1.17 +License, or (at your option) any later version. You may also 1.18 +distribute it under the Artistic License, as comes with Perl. 1.19 1.20 - This program is free software; you can redistribute it and/or 1.21 - modify it under the terms of the GNU General Public License as 1.22 - published by the Free Software Foundation; either version 2 of the 1.23 - License, or (at your option) any later version. You may also 1.24 - distribute it under the Artistic License, as comes with Perl. 1.25 +This program is distributed in the hope that it will be useful, 1.26 +but WITHOUT ANY WARRANTY; without even the implied warranty of 1.27 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 1.28 1.29 - This program is distributed in the hope that it will be useful, 1.30 - but WITHOUT ANY WARRANTY; without even the implied warranty of 1.31 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 1.32 +You should have received a copy of the GNU General Public License 1.33 +along with this program; if not, write to the Free Software 1.34 +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 1.35 1.36 - You should have received a copy of the GNU General Public License 1.37 - along with this program; if not, write to the Free Software 1.38 - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 1.39 +You should also have recieved a copy of the Artistic license with 1.40 +this program. 1.41 1.42 - You should also have recieved a copy of the Artistic license with 1.43 - this program. 1.44 +===================================================================== 1.45 1.46 -=============================================================================== 1.47 +Input is read through standard input. For example: bday < ~/.birthdays 1.48 +The input (file) has to have the following format: 1.49 1.50 - Input is read through standard input. For example: bday < ~/.birthdays 1.51 - The input (file) has to have the following format: 1.52 +text=date flags 1.53 1.54 - text=date flags 1.55 +where: 1.56 + date is yyyy-mm-dd 1.57 + flags is ONE or ZERO of 1.58 + bd for a birthday (default) 1.59 + ann for an anniversary 1.60 + ev for an event 1.61 + and zero or more of 1.62 + w <n> to set the warn-in-advance time to n days 1.63 + (don't include the brackets! :) 1.64 + to <date> 1.65 + for <days> 1.66 + to specify the length of time taken by an 1.67 + event, for example a holiday. 1.68 1.69 - where: 1.70 - date is yyyy-mm-dd 1.71 - flags is ONE or ZERO of 1.72 - bd for a birthday (default) 1.73 - ann for an anniversary 1.74 - ev for an event 1.75 - and zero or more of 1.76 - w <n> to set the warn-in-advance time to n days (don't include the 1.77 - brackets! :) 1.78 - to <date> 1.79 - for <days> 1.80 - to specify the length of time taken by an event, for example a holiday. 1.81 +Lines preceeded by # are treated as comments. 1.82 1.83 - Lines preceeded by # are treated as comments. 1.84 +Note: If you deviate from this format, I cannot guarantee anything about 1.85 + it's behaviour. In most cases, it will just quietly ignore the 1.86 + error, which probably isn't ideal behaviour. Oh, well. 1.87 1.88 - Note: If you deviate from this format, I cannot guarantee anything about 1.89 - it's behaviour. In most cases, it will just quietly ignore the error, 1.90 - which probably isn't ideal behaviour. Oh, well. 1.91 - 1.92 -=============================================================================== 1.93 +===================================================================== 1.94 */ 1.95 1.96 1.97 @@ -94,37 +93,37 @@ 1.98 const struct _ftable FTABLE[]; 1.99 1.100 struct date { 1.101 - unsigned day; 1.102 - unsigned month; 1.103 - unsigned year; 1.104 + unsigned day; 1.105 + unsigned month; 1.106 + unsigned year; 1.107 }; 1.108 1.109 struct event { 1.110 - char* text; 1.111 - struct date date; 1.112 - struct date enddate; 1.113 - int warn; 1.114 + char* text; 1.115 + struct date date; 1.116 + struct date enddate; 1.117 + int warn; 1.118 }; 1.119 1.120 typedef int (*prnfunc)(const char *); 1.121 1.122 /* ========== Global Variables */ 1.123 1.124 -struct event* readlist(void); 1.125 +struct event *readlist(void); 1.126 void gettoday(void); 1.127 -unsigned delta(struct date*); 1.128 -unsigned ddiff(struct date* D1, struct date* D2); 1.129 -void liststrings(struct event* evl, prnfunc outf); 1.130 -char* tdelta(struct date* d); 1.131 -char* ttime(int yr, int mn, int wk, int dy); 1.132 -int skptok(int j, char* ptr); 1.133 -int evcmp(const void* e1, const void* e2); 1.134 +unsigned delta(struct date *); 1.135 +unsigned ddiff(struct date *D1, struct date *D2); 1.136 +void liststrings(struct event *evl, prnfunc outf); 1.137 +char *tdelta(struct date *d); 1.138 +char *ttime(int yr, int mn, int wk, int dy); 1.139 +int skptok(int j, char *ptr); 1.140 +int evcmp(const void *e1, const void *e2); 1.141 1.142 1.143 struct date today; 1.144 -int iDWarn = DEF_WARN; 1.145 +int iDWarn = DEF_WARN; 1.146 1.147 -const unsigned MLENDAT[]={31,-1,31,30,31,30,31,31,30,31,30,31}; 1.148 +const unsigned MLENDAT[] = {31,-1,31,30,31,30,31,31,30,31,30,31}; 1.149 1.150 const struct _ftable FTABLE[] = { 1.151 {"bd", F_TBIRTHDAY}, 1.152 @@ -142,13 +141,15 @@ 1.153 1.154 1.155 /* 1.156 - xmalloc/xrealloc functions 1.157 - Note: the x* functions are lifted straight from the GNU libc info docs 1.158 - $Id: xmalloc.c,v 1.2 1999/01/16 17:08:59 andy Exp $ 1.159 +xmalloc/xrealloc functions 1.160 +Note: the x* functions are lifted straight from the GNU libc info docs 1.161 +$Id: xmalloc.c,v 1.2 1999/01/16 17:08:59 andy Exp $ 1.162 */ 1.163 1.164 -void* xmalloc (size_t size) { 1.165 - register void* value = malloc (size); 1.166 +void * 1.167 +xmalloc(size_t size) 1.168 +{ 1.169 + register void *value = malloc (size); 1.170 if (value == 0) { 1.171 fprintf(stderr, "virtual memory exhausted\n"); 1.172 exit(1); 1.173 @@ -156,8 +157,10 @@ 1.174 return value; 1.175 } 1.176 1.177 -void* xrealloc (void* ptr, size_t size) { 1.178 - register void* value = realloc (ptr, size); 1.179 +void * 1.180 +xrealloc(void *ptr, size_t size) 1.181 +{ 1.182 + register void *value = realloc (ptr, size); 1.183 if (value == 0) { 1.184 fprintf(stderr, "virtual memory exhausted\n"); 1.185 exit(1); 1.186 @@ -169,8 +172,10 @@ 1.187 /* ========== */ 1.188 1.189 1.190 -/* like strcat(), but lets the buffer automagically grow :-) 1.191 - * (needs local variable "size" with the buffer size) */ 1.192 +/* 1.193 +like strcat(), but lets the buffer automagically grow :-) 1.194 +(needs local variable "size" with the buffer size) 1.195 +*/ 1.196 #define append(where, what) do { \ 1.197 if (strlen(what) > (size - strlen(where))) { \ 1.198 xrealloc(where, size + 128 + strlen(what)); \ 1.199 @@ -181,46 +186,50 @@ 1.200 1.201 /* ========== */ 1.202 1.203 -/* returns delta(d) in days, weeks, months, etc 1.204 - * the returned buffer is malloc()ed, do not forget to free() it */ 1.205 -char* tdelta(struct date* d) { 1.206 +/* 1.207 +returns delta(d) in days, weeks, months, etc 1.208 +the returned buffer is malloc()ed, do not forget to free() it 1.209 +*/ 1.210 +char * 1.211 +tdelta(struct date *d) 1.212 +{ 1.213 int dy, wk, mn, yr; 1.214 - char* tmp; 1.215 - char* buf = xmalloc(128); 1.216 + char *tmp; 1.217 + char *buf = xmalloc(128); 1.218 int size = 128; 1.219 + 1.220 *buf = 0; 1.221 + switch (delta(d)) { 1.222 + case 0: 1.223 + append(buf, "today"); 1.224 + return buf; 1.225 + case 1: 1.226 + append(buf, "tomorrow"); 1.227 + return buf; 1.228 + default: 1.229 + /* like delta(), we ignore the year */ 1.230 + yr = -before(*d, today); 1.231 + mn = d->month - today.month; 1.232 + dy = d->day - today.day; 1.233 1.234 - switch (delta(d)) { 1.235 - case 0: 1.236 - append(buf, "today"); 1.237 - return buf; 1.238 - case 1: 1.239 - append(buf, "tomorrow"); 1.240 - return buf; 1.241 - default: 1.242 - /* like delta(), we ignore the year */ 1.243 - yr = -before(*d, today); 1.244 - mn = d->month - today.month; 1.245 - dy = d->day - today.day; 1.246 + if (dy < 0) { 1.247 + dy += mlen(today.month, today.year); 1.248 + mn--; 1.249 + } 1.250 + if (mn < 0) { 1.251 + mn += 12; 1.252 + yr++; 1.253 + } 1.254 1.255 - if (dy < 0) { 1.256 - dy += mlen(today.month, today.year); 1.257 - mn--; 1.258 - } 1.259 - if (mn < 0) { 1.260 - mn += 12; 1.261 - yr++; 1.262 - } 1.263 + wk = (dy / 7); 1.264 + dy %= 7; 1.265 1.266 - wk = (dy / 7); 1.267 - dy %= 7; 1.268 + append(buf, "in "); 1.269 + tmp = ttime(yr, mn, wk, dy); 1.270 + append(buf, tmp); 1.271 + free(tmp); 1.272 1.273 - append(buf, "in "); 1.274 - tmp = ttime(yr, mn, wk, dy); 1.275 - append(buf, tmp); 1.276 - free(tmp); 1.277 - 1.278 - return buf; 1.279 + return buf; 1.280 } 1.281 } 1.282 1.283 @@ -229,23 +238,23 @@ 1.284 1.285 1.286 /* 1.287 -void donum(n,txt) { 1.288 - do { 1.289 - if (n > 0) { 1.290 - snprintf(tmp, sizeof(tmp), "%d", n); 1.291 - append(buf, tmp); 1.292 - append(buf, " " txt); 1.293 - if (n != 1) 1.294 - append(buf, "s"); 1.295 - terms--; 1.296 - if (orgterms > 1) { 1.297 - if (terms == 1) 1.298 - append(buf, " and "); 1.299 - else if (terms > 1) 1.300 - append(buf, ", "); 1.301 - } 1.302 +void 1.303 +donum(n,txt) 1.304 +{ 1.305 + if (n > 0) { 1.306 + snprintf(tmp, sizeof(tmp), "%d", n); 1.307 + append(buf, tmp); 1.308 + append(buf, " " txt); 1.309 + if (n != 1) 1.310 + append(buf, "s"); 1.311 + terms--; 1.312 + if (orgterms > 1) { 1.313 + if (terms == 1) 1.314 + append(buf, " and "); 1.315 + else if (terms > 1) 1.316 + append(buf, ", "); 1.317 } 1.318 - } while(0) 1.319 + } 1.320 } 1.321 */ 1.322 1.323 @@ -269,8 +278,10 @@ 1.324 1.325 1.326 /* returns allocated buffer, don't forget to free() */ 1.327 -char* ttime(int yr, int mn, int wk, int dy) { 1.328 - char* buf = xmalloc(128); 1.329 +char * 1.330 +ttime(int yr, int mn, int wk, int dy) 1.331 +{ 1.332 + char *buf = xmalloc(128); 1.333 int size = 128; 1.334 int terms, orgterms; 1.335 char tmp[128]; 1.336 @@ -292,8 +303,13 @@ 1.337 1.338 1.339 1.340 -/* lists the birthdays in their string format, one by one, and passes the string to a function. */ 1.341 -void liststrings(struct event* evl, prnfunc outf) { 1.342 +/* 1.343 +lists the birthdays in their string format, one by one, and passes 1.344 +the string to a function. 1.345 +*/ 1.346 +void 1.347 +liststrings(struct event *evl, prnfunc outf) 1.348 +{ 1.349 int i,j; 1.350 char *buf, *tmp; 1.351 int size; 1.352 @@ -356,16 +372,24 @@ 1.353 1.354 1.355 1.356 -/* sort the events by the time before the next time they come up, putting those 1.357 - where the start has passed but we are still in the time-period first */ 1.358 -int evcmp(const void* p1, const void* p2) { 1.359 - struct event* e1=(struct event*) p1; 1.360 - struct event* e2=(struct event*) p2; 1.361 +/* 1.362 +sort the events by the time before the next time they come up, 1.363 +putting those where the start has passed but we are still in the 1.364 +time-period first 1.365 +*/ 1.366 +int 1.367 +evcmp(const void *p1, const void *p2) 1.368 +{ 1.369 + struct event *e1=(struct event *) p1; 1.370 + struct event *e2=(struct event *) p2; 1.371 unsigned d1, d2; 1.372 1.373 - /* if the delta for the enddate is less than that for the start date, then we 1.374 - have passed the start date but not yet the end date, and so we should 1.375 - display the enddate; otherwise, we should display the start date */ 1.376 + /* 1.377 + if the delta for the enddate is less than that for the start 1.378 + date, then we have passed the start date but not yet the end 1.379 + date, and so we should display the enddate; otherwise, we 1.380 + should display the start date 1.381 + */ 1.382 1.383 d1=delta(&(e1->date)); 1.384 if (e1->enddate.day && delta(&(e1->enddate)) < d1) 1.385 @@ -386,9 +410,13 @@ 1.386 1.387 1.388 1.389 -/* difference in days between two dates */ 1.390 -/* it is assumed that D1 < D2, and so the result is always positive */ 1.391 -unsigned ddiff(struct date* D1, struct date* D2) { 1.392 +/* 1.393 +difference in days between two dates 1.394 +it is assumed that D1 < D2, and so the result is always positive 1.395 +*/ 1.396 +unsigned 1.397 +ddiff(struct date *D1, struct date *D2) 1.398 +{ 1.399 struct date d1, d2; 1.400 int dd, m; 1.401 1.402 @@ -427,8 +455,10 @@ 1.403 for (m=d1.month; m < d2.month + (d2.year-d1.year)*12; m++) 1.404 dd += mlen(((m-1)%12)+1, d1.year + m/12); 1.405 1.406 - /* and then we renormalise for the days within the months */ 1.407 - /* the first month was included in our calculations */ 1.408 + /* 1.409 + and then we renormalise for the days within the months 1.410 + the first month was included in our calculations 1.411 + */ 1.412 dd -= d1.day; 1.413 /* but the last one wasn't */ 1.414 dd += d2.day; 1.415 @@ -443,8 +473,12 @@ 1.416 1.417 1.418 1.419 -/* actually until the next anniversary of ... */ 1.420 -unsigned delta(struct date *date) { 1.421 +/* 1.422 +actually until the next anniversary of ... 1.423 +*/ 1.424 +unsigned 1.425 +delta(struct date *date) 1.426 +{ 1.427 struct date d; 1.428 unsigned dt, mn; 1.429 1.430 @@ -472,7 +506,9 @@ 1.431 1.432 1.433 1.434 -void gettoday(void) { 1.435 +void 1.436 +gettoday(void) 1.437 +{ 1.438 struct tm *tm; 1.439 time_t t; 1.440 1.441 @@ -492,7 +528,9 @@ 1.442 1.443 1.444 1.445 -struct event* readlist() { 1.446 +struct event * 1.447 +readlist() 1.448 +{ 1.449 int i, j, k, l, d; 1.450 struct event *evl; 1.451 char buf[1024], buf2[1024]; 1.452 @@ -524,7 +562,8 @@ 1.453 1.454 *(ptr++) = 0; 1.455 1.456 - j = sscanf(ptr, "%u-%u-%u", &(evl[i].date.year), &(evl[i].date.month), &(evl[i].date.day)); 1.457 + j = sscanf(ptr, "%u-%u-%u", &(evl[i].date.year), 1.458 + &(evl[i].date.month), &(evl[i].date.day)); 1.459 /* ... unless it wasn't read, in which case set it to zero */ 1.460 if (j==2) { 1.461 evl[i].date.year = 0; 1.462 @@ -546,35 +585,35 @@ 1.463 } 1.464 1.465 switch (FTABLE[k].flag) { 1.466 - case F_WTIME_P: /* w <n> -- sets warning time */ 1.467 - sscanf(ptr + j, "w %u", &(evl[i].warn)); 1.468 - break; 1.469 - case F_FORDAYS: /* for <days> -- sets the duration of the event */ 1.470 - sscanf(ptr + j, "for %u", &d); 1.471 - evl[i].enddate=evl[i].date; 1.472 - for (l = 1; l < d; l++) { 1.473 - evl[i].enddate.day++; 1.474 - if (evl[i].enddate.day > mlen(evl[i].enddate.month, evl[i].enddate.year)) { 1.475 - evl[i].enddate.month++; 1.476 - evl[i].enddate.day = 1; 1.477 - } 1.478 - if (evl[i].enddate.month > 12) { 1.479 - evl[i].enddate.year++; 1.480 - evl[i].enddate.month = 1; 1.481 - } 1.482 + case F_WTIME_P: /* w <n> -- sets warning time */ 1.483 + sscanf(ptr + j, "w %u", &(evl[i].warn)); 1.484 + break; 1.485 + case F_FORDAYS: /* for <days> -- sets the duration of the event */ 1.486 + sscanf(ptr + j, "for %u", &d); 1.487 + evl[i].enddate=evl[i].date; 1.488 + for (l = 1; l < d; l++) { 1.489 + evl[i].enddate.day++; 1.490 + if (evl[i].enddate.day > mlen(evl[i].enddate.month, evl[i].enddate.year)) { 1.491 + evl[i].enddate.month++; 1.492 + evl[i].enddate.day = 1; 1.493 } 1.494 - break; 1.495 - case F_TODATE: /* to <date> -- sets the end date of the event */ 1.496 - l = sscanf(ptr + j, "to %u-%u-%u", &(evl[i].enddate.year), &(evl[i].enddate.month), &(evl[i].enddate.day)); 1.497 - if (l == 2) { 1.498 - evl[i].enddate.year = 0; 1.499 + if (evl[i].enddate.month > 12) { 1.500 + evl[i].enddate.year++; 1.501 + evl[i].enddate.month = 1; 1.502 } 1.503 - break; 1.504 - case 0: 1.505 - break; 1.506 - default: 1.507 - flags |= FTABLE[k].flag; 1.508 - break; 1.509 + } 1.510 + break; 1.511 + case F_TODATE: /* to <date> -- sets the end date of the event */ 1.512 + l = sscanf(ptr + j, "to %u-%u-%u", &(evl[i].enddate.year), &(evl[i].enddate.month), &(evl[i].enddate.day)); 1.513 + if (l == 2) { 1.514 + evl[i].enddate.year = 0; 1.515 + } 1.516 + break; 1.517 + case 0: 1.518 + break; 1.519 + default: 1.520 + flags |= FTABLE[k].flag; 1.521 + break; 1.522 } 1.523 } 1.524 1.525 @@ -632,7 +671,9 @@ 1.526 1.527 1.528 1.529 -int skptok(int j, char *ptr) { 1.530 +int 1.531 +skptok(int j, char *ptr) 1.532 +{ 1.533 for (; ptr[j] != 0 && ptr[j] != ' ' && ptr[j] != '\t' ; j++); 1.534 for (; ptr[j] != 0 && (ptr[j] == ' ' || ptr[j] == '\t'); j++); 1.535 1.536 @@ -644,9 +685,10 @@ 1.537 1.538 1.539 1.540 -int main(int argc, char* argv[]) { 1.541 - 1.542 - while (--argc > 0 && (*++argv)[0] == '-') { 1.543 +int 1.544 +main(int argc, char *argv[]) 1.545 +{ 1.546 + while (--argc > 0 && (*++argv)[0] == '-') { 1.547 if (strcmp(argv[0], "-W") == 0) { 1.548 /* TODO: catch if no value given */ 1.549 iDWarn = atoi((++argv)[0]); 1.550 @@ -659,5 +701,5 @@ 1.551 1.552 liststrings(readlist(), puts); 1.553 1.554 - return 0; 1.555 + return 0; 1.556 }