Mercurial > bday
annotate bday.c @ 16:79d22407a6be
a lot of refactoring
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Mon, 24 Feb 2014 21:11:38 +0100 |
parents | 032af48d590b |
children | d18a3b2b76bd |
rev | line source |
---|---|
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
1 /* |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
2 bday -- Birthday/Anniversary reminder |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
3 |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
4 (c) 2007,2014 markus schnalke <meillo@marmaro.de> |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
5 (c) 1994-1999 AS Mortimer |
8 | 6 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
7 This program is free software; you can redistribute it and/or |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
8 modify it under the terms of the GNU General Public License as |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
9 published by the Free Software Foundation; either version 2 of the |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
10 License, or (at your option) any later version. You may also |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
11 distribute it under the Artistic License, as comes with Perl. |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
12 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
13 This program is distributed in the hope that it will be useful, |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
16 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
17 You should have received a copy of the GNU General Public License |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
18 along with this program; if not, write to the Free Software |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
8 | 20 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
21 You should also have recieved a copy of the Artistic license with |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
22 this program. |
8 | 23 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
24 ===================================================================== |
9
4f48b4f86e3d
added COPYRIGHT and COPYING; adjusted copyright hints in the code
meillo@marmaro.de
parents:
8
diff
changeset
|
25 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
26 Input is read through standard input. For example: bday < ~/.birthdays |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
27 The input (file) has to have the following format: |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
28 |
16 | 29 date flags text |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
30 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
31 where: |
16 | 32 date is YYYY-MM-DD |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
33 flags is ONE or ZERO of |
16 | 34 #ann for an anniversary |
35 #ev for an event | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
36 and zero or more of |
16 | 37 #w<n> to set the warn-in-advance time to n days |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
38 (don't include the brackets! :) |
16 | 39 #to<date> |
40 #for<days> | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
41 to specify the length of time taken by an |
16 | 42 event, for example a holiday |
43 separated by spaces. | |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
44 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
45 Lines preceeded by # are treated as comments. |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
46 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
47 Note: If you deviate from this format, I cannot guarantee anything about |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
48 it's behaviour. In most cases, it will just quietly ignore the |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
49 error, which probably isn't ideal behaviour. Oh, well. |
8 | 50 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
51 ===================================================================== |
8 | 52 */ |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
53 |
7 | 54 |
8 | 55 /* standard time to warn in advance, when no explicit w flag is given. */ |
56 #define DEF_WARN 14 | |
7 | 57 |
58 | |
59 #include <stdarg.h> | |
60 #include <stdio.h> | |
61 #include <stdlib.h> | |
62 #include <string.h> | |
63 #include <sys/types.h> | |
64 #include <time.h> | |
65 #include <unistd.h> | |
66 | |
67 | |
68 | |
69 /* ========== Global constants and data types */ | |
70 | |
71 | |
72 /* -------- modifier flags */ | |
73 #define F_MTYPE 0x07 | |
74 #define F_TANNIVERSARY 2 | |
75 #define F_TEVENT 3 | |
76 | |
77 /* flags processed immediately on encountering */ | |
78 #define F_MIMMEDIATE 0x24 | |
79 #define F_WTIME_P 0x08 | |
80 #define F_FORDAYS 0x16 | |
81 #define F_TODATE 0x24 | |
82 | |
16 | 83 struct _ftable { |
84 char* txt; | |
85 unsigned flag; | |
86 }; | |
7 | 87 const struct _ftable FTABLE[]; |
88 | |
89 struct date { | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
90 unsigned day; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
91 unsigned month; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
92 unsigned year; |
7 | 93 }; |
94 | |
95 struct event { | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
96 char* text; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
97 struct date date; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
98 struct date enddate; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
99 int warn; |
7 | 100 }; |
101 | |
102 /* ========== Global Variables */ | |
103 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
104 struct event *readlist(void); |
7 | 105 void gettoday(void); |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
106 unsigned delta(struct date *); |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
107 unsigned ddiff(struct date *D1, struct date *D2); |
16 | 108 void liststrings(struct event *evl); |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
109 char *tdelta(struct date *d); |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
110 char *ttime(int yr, int mn, int wk, int dy); |
16 | 111 char *skptok(char *ptr); |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
112 int evcmp(const void *e1, const void *e2); |
7 | 113 |
114 | |
115 struct date today; | |
16 | 116 int def_warn = DEF_WARN; |
7 | 117 |
118 | |
119 const struct _ftable FTABLE[] = { | |
16 | 120 {"#ann",F_TANNIVERSARY}, |
121 {"#ev", F_TEVENT}, | |
122 {"#w", F_WTIME_P}, | |
123 {"#to", F_TODATE}, | |
124 {"#for", F_FORDAYS}, | |
7 | 125 {NULL, 0} |
126 }; | |
127 | |
128 | |
129 | |
130 | |
131 | |
132 | |
133 /* | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
134 xmalloc/xrealloc functions |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
135 Note: the x* functions are lifted straight from the GNU libc info docs |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
136 $Id: xmalloc.c,v 1.2 1999/01/16 17:08:59 andy Exp $ |
7 | 137 */ |
138 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
139 void * |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
140 xmalloc(size_t size) |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
141 { |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
142 register void *value = malloc (size); |
7 | 143 if (value == 0) { |
144 fprintf(stderr, "virtual memory exhausted\n"); | |
145 exit(1); | |
146 } | |
147 return value; | |
148 } | |
149 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
150 void * |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
151 xrealloc(void *ptr, size_t size) |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
152 { |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
153 register void *value = realloc (ptr, size); |
7 | 154 if (value == 0) { |
155 fprintf(stderr, "virtual memory exhausted\n"); | |
156 exit(1); | |
157 } | |
158 return value; | |
159 } | |
160 | |
8 | 161 |
7 | 162 /* ========== */ |
163 | |
164 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
165 /* |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
166 like strcat(), but lets the buffer automagically grow :-) |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
167 */ |
16 | 168 int |
169 append(char *where, int size, char *what) | |
170 { | |
171 if (strlen(what) > ((size) - strlen(where))) { | |
172 xrealloc(where, (size) + 128 + strlen(what)); | |
173 size += 128 + strlen(what); | |
174 } | |
175 strcat(where, what); | |
176 return size; | |
177 } | |
7 | 178 |
179 /* ========== */ | |
180 | |
16 | 181 |
182 int | |
183 before(struct date a, struct date b) | |
184 { | |
185 if (a.month < b.month) { | |
186 return 1; | |
187 } else if (a.month == b.month && a.day < b.day) { | |
188 return 1; | |
189 } else { | |
190 return 0; | |
191 } | |
192 } | |
193 | |
194 int | |
195 ydelta(struct date a, struct date b) | |
196 { | |
197 return b.year - a.year + before(a, b); | |
198 } | |
199 | |
200 /* | |
201 returns the length of the given month | |
202 */ | |
203 int | |
204 mlen(int month, int year) | |
205 { | |
206 unsigned mlendat[] = {31,0,31,30,31,30,31,31,30,31,30,31}; | |
207 | |
208 if (mlendat[month - 1]) { | |
209 return mlendat[month - 1]; | |
210 } else { | |
211 if (year%4==0 && (year%100!=0 || year%400==0)) { | |
212 return 29; | |
213 } else { | |
214 return 28; | |
215 } | |
216 } | |
217 } | |
218 | |
219 | |
220 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
221 /* |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
222 returns delta(d) in days, weeks, months, etc |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
223 the returned buffer is malloc()ed, do not forget to free() it |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
224 */ |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
225 char * |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
226 tdelta(struct date *d) |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
227 { |
7 | 228 int dy, wk, mn, yr; |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
229 char *tmp; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
230 char *buf = xmalloc(128); |
7 | 231 int size = 128; |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
232 |
7 | 233 *buf = 0; |
234 switch (delta(d)) { | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
235 case 0: |
16 | 236 size = append(buf, size, "TODAY"); |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
237 return buf; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
238 case 1: |
16 | 239 size = append(buf, size, "Tomorrow"); |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
240 return buf; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
241 default: |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
242 /* like delta(), we ignore the year */ |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
243 yr = -before(*d, today); |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
244 mn = d->month - today.month; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
245 dy = d->day - today.day; |
7 | 246 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
247 if (dy < 0) { |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
248 dy += mlen(today.month, today.year); |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
249 mn--; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
250 } |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
251 if (mn < 0) { |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
252 mn += 12; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
253 yr++; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
254 } |
7 | 255 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
256 wk = (dy / 7); |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
257 dy %= 7; |
7 | 258 |
16 | 259 size = append(buf, size, "In "); |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
260 tmp = ttime(yr, mn, wk, dy); |
16 | 261 size = append(buf, size, tmp); |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
262 free(tmp); |
7 | 263 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
264 return buf; |
7 | 265 } |
266 } | |
267 | |
268 | |
269 | |
270 | |
271 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
272 void |
16 | 273 donum(char *buf, int size, int n, char *txt, int *terms) |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
274 { |
16 | 275 char tmp[128]; |
276 | |
277 if (n <= 0) { | |
278 return; | |
279 } | |
280 snprintf(tmp, sizeof(tmp), "%d", n); | |
281 size = append(buf, size, tmp); | |
282 size = append(buf, size, " "); | |
283 size = append(buf, size, txt); | |
284 if (n != 1) { | |
285 size = append(buf, size, "s"); | |
286 } | |
287 if (--*terms == 1) { | |
288 size = append(buf, size, " and "); | |
289 } else if (*terms > 1) { | |
290 size = append(buf, size, ", "); | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
291 } |
7 | 292 } |
293 | |
294 | |
295 /* returns allocated buffer, don't forget to free() */ | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
296 char * |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
297 ttime(int yr, int mn, int wk, int dy) |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
298 { |
7 | 299 int size = 128; |
16 | 300 char *buf = xmalloc(size); |
301 int terms = (yr!=0) + (mn!=0) + (wk!=0) + (dy!=0); | |
7 | 302 |
16 | 303 *buf = '\0'; /* Initialize buffer */ |
7 | 304 |
16 | 305 donum(buf, size, yr, "year", &terms); |
306 donum(buf, size, mn, "month", &terms); | |
307 donum(buf, size, wk, "week", &terms); | |
308 donum(buf, size, dy, "day", &terms); | |
7 | 309 |
310 return buf; | |
311 } | |
312 | |
313 | |
314 | |
315 | |
316 | |
317 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
318 /* |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
319 lists the birthdays in their string format, one by one, and passes |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
320 the string to a function. |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
321 */ |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
322 void |
16 | 323 liststrings(struct event *evl) |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
324 { |
7 | 325 int i,j; |
326 char *buf, *tmp; | |
327 int size; | |
328 | |
16 | 329 for (i=0; evl[i].text; i++) { |
330 size = 128; | |
331 buf = xmalloc(size); | |
7 | 332 *buf = '\0'; |
333 | |
334 if (evl[i].warn == -1 && delta(&(evl[i].date))==0) { | |
16 | 335 size = append(buf, size, evl[i].text); |
7 | 336 } else if (evl[i].enddate.day == 0) { |
16 | 337 |
7 | 338 if (delta(&(evl[i].date)) <= evl[i].warn) { |
339 tmp = tdelta(&(evl[i].date)); | |
16 | 340 size = append(buf, size, tmp); |
341 size = append(buf, size, ": "); | |
342 size = append(buf, size, evl[i].text); | |
7 | 343 free(tmp); |
344 } | |
345 } else { | |
346 if (delta(&(evl[i].date)) <= evl[i].warn) { | |
16 | 347 size = append(buf, size, evl[i].text); |
348 size = append(buf, size, " for "); | |
349 /* +1 because, if the difference between | |
350 two dates is one day, then the length of | |
351 an event on those days is two days */ | |
7 | 352 j = ddiff(&(evl[i].date),&(evl[i].enddate)) + 1; |
353 tmp = ttime(0, 0, j/7, j%7); | |
16 | 354 size = append(buf, size, tmp); |
7 | 355 free(tmp); |
16 | 356 size = append(buf, size, " "); |
7 | 357 tmp = tdelta(&(evl[i].date)); |
16 | 358 size = append(buf, size, tmp); |
7 | 359 } else if (delta(&(evl[i].enddate)) <= evl[i].warn) { |
16 | 360 size = append(buf, size, evl[i].text); |
361 size = append(buf, size, " "); | |
7 | 362 j = delta(&(evl[i].enddate)); |
363 if (j) { | |
16 | 364 size = append(buf, size, "for "); |
7 | 365 tmp = ttime(0, 0, j/7, j%7); |
16 | 366 size = append(buf, size, tmp); |
7 | 367 free(tmp); |
16 | 368 size = append(buf, size, " longer"); |
7 | 369 } else { |
16 | 370 size = append(buf, size, "finishes today"); |
7 | 371 } |
372 } | |
373 } | |
374 if (*buf) { | |
16 | 375 size = append(buf, size, "."); |
376 puts(buf); | |
7 | 377 } |
378 free(buf); | |
379 } | |
380 } | |
381 | |
382 | |
383 | |
384 | |
385 | |
386 | |
387 | |
388 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
389 /* |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
390 sort the events by the time before the next time they come up, |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
391 putting those where the start has passed but we are still in the |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
392 time-period first |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
393 */ |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
394 int |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
395 evcmp(const void *p1, const void *p2) |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
396 { |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
397 struct event *e1=(struct event *) p1; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
398 struct event *e2=(struct event *) p2; |
8 | 399 unsigned d1, d2; |
7 | 400 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
401 /* |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
402 if the delta for the enddate is less than that for the start |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
403 date, then we have passed the start date but not yet the end |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
404 date, and so we should display the enddate; otherwise, we |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
405 should display the start date |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
406 */ |
7 | 407 |
408 d1=delta(&(e1->date)); | |
409 if (e1->enddate.day && delta(&(e1->enddate)) < d1) | |
410 d1=delta(&(e1->enddate)); | |
411 | |
412 d2=delta(&(e2->date)); | |
413 if (e2->enddate.day && delta(&(e2->enddate)) < d2) | |
414 d2=delta(&(e2->enddate)); | |
415 | |
416 if (d1 < d2) return -1; | |
8 | 417 if (d1 > d2) return 1; |
7 | 418 |
419 return strcmp(e1->text, e2->text); | |
420 } | |
421 | |
422 | |
423 | |
424 | |
425 | |
426 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
427 /* |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
428 difference in days between two dates |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
429 it is assumed that D1 < D2, and so the result is always positive |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
430 */ |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
431 unsigned |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
432 ddiff(struct date *D1, struct date *D2) |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
433 { |
8 | 434 struct date d1, d2; |
435 int dd, m; | |
7 | 436 |
437 /* make working copies */ | |
8 | 438 d1 = *D1; |
439 d2 = *D2; | |
7 | 440 |
441 /* sort out zero years */ | |
442 if (d1.year == 0 || d2.year==0) { | |
443 if (d1.year != d2.year) { | |
444 if (d1.year == 0) { | |
8 | 445 if (before(d1,d2)) |
446 d1.year = d2.year; | |
447 else | |
448 d1.year = d2.year - 1; | |
449 } else { | |
450 if (before(d1, d2)) | |
451 d2.year = d1.year; | |
452 else | |
453 d2.year = d1.year + 1; | |
7 | 454 } |
455 } else { /* both years zero */ | |
8 | 456 if (before(d1, d2)) |
457 d1.year = d2.year = today.year; | |
7 | 458 else { |
8 | 459 d1.year = today.year; |
460 d2.year = d1.year + 1; | |
7 | 461 } |
462 } | |
463 } | |
464 | |
465 /* now we can actually do the comparison ... */ | |
8 | 466 dd = 0; |
7 | 467 |
468 /* to start with, we work in months */ | |
469 for (m=d1.month; m < d2.month + (d2.year-d1.year)*12; m++) | |
470 dd += mlen(((m-1)%12)+1, d1.year + m/12); | |
471 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
472 /* |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
473 and then we renormalise for the days within the months |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
474 the first month was included in our calculations |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
475 */ |
7 | 476 dd -= d1.day; |
477 /* but the last one wasn't */ | |
478 dd += d2.day; | |
479 | |
480 return dd; | |
481 } | |
482 | |
483 | |
484 | |
485 | |
486 | |
487 | |
488 | |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
489 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
490 /* |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
491 actually until the next anniversary of ... |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
492 */ |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
493 unsigned |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
494 delta(struct date *date) |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
495 { |
7 | 496 struct date d; |
497 unsigned dt, mn; | |
498 | |
499 memcpy(&d, date, sizeof(struct date)); | |
500 | |
501 /* past the end of the year */ | |
502 if (before(d, today)) { | |
503 d.year = 1; | |
504 } else { | |
505 d.year = 0; | |
506 } | |
507 | |
8 | 508 for (mn = today.month, dt=0; mn < d.month + 12*d.year; mn++) { |
7 | 509 dt += mlen(((mn-1)%12) + 1,today.year + mn/12); |
8 | 510 } |
7 | 511 |
512 dt -= today.day; | |
513 dt += d.day; | |
514 | |
515 return dt; | |
516 } | |
517 | |
518 | |
519 | |
520 | |
521 | |
522 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
523 void |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
524 gettoday(void) |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
525 { |
7 | 526 struct tm *tm; |
527 time_t t; | |
528 | |
529 time(&t); | |
530 tm = localtime(&t); | |
531 today.day = tm->tm_mday; | |
532 today.month = tm->tm_mon + 1; /* 1-12 instead of 0-11 */ | |
533 today.year = tm->tm_year + 1900; | |
534 } | |
535 | |
536 | |
537 | |
538 | |
539 | |
540 | |
541 | |
542 | |
543 | |
544 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
545 struct event * |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
546 readlist() |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
547 { |
7 | 548 int i, j, k, l, d; |
549 struct event *evl; | |
550 char buf[1024], buf2[1024]; | |
16 | 551 char *ptr, *cp; |
7 | 552 unsigned flags; |
553 | |
554 /* initialise */ | |
555 gettoday(); | |
556 | |
557 for (i = 0, evl = NULL; fgets(buf, sizeof(buf), stdin) != NULL; i++) { | |
558 evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1)); | |
559 | |
560 /* ignore comments and empty lines */ | |
561 if (*buf == '#' || *buf == '\n') { | |
562 i--; | |
563 continue; | |
564 } | |
565 | |
566 /* parse string in buf */ | |
16 | 567 |
568 ptr = strchr(buf, ' '); /* start of text */ | |
7 | 569 |
570 /* not a valid line, so ignore it! Cool, huh? */ | |
571 /* Attention: only recognizes lines without '=' */ | |
16 | 572 if (!ptr) { |
573 fprintf(stderr, "WARNING: Invalid input line:\n\t%s", buf); | |
7 | 574 i--; |
575 continue; | |
576 } | |
577 | |
16 | 578 *(ptr++) = '\0'; |
579 ptr[strlen(ptr)-1] = '\0'; | |
7 | 580 |
16 | 581 j = sscanf(buf, "%u-%u-%u", &(evl[i].date.year), |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
582 &(evl[i].date.month), &(evl[i].date.day)); |
16 | 583 if (j != 3) { |
584 fprintf(stderr, "Error: Invalid date:\t%s\n", buf); | |
585 i--; | |
586 continue; | |
7 | 587 } |
588 | |
589 /* parse flags */ | |
590 | |
16 | 591 evl[i].warn = def_warn; |
7 | 592 evl[i].enddate.day = 0; |
593 evl[i].enddate.month = 0; | |
594 evl[i].enddate.year = 0; | |
595 | |
596 flags = 0; | |
597 j = 0; | |
16 | 598 cp = skptok(ptr); |
599 for (cp=ptr; *cp && *cp=='#'; cp=skptok(cp)) { | |
600 for (k = 0; FTABLE[k].txt && strncmp(FTABLE[k].txt, cp, strlen(FTABLE[k].txt)); k++) { | |
7 | 601 } |
602 | |
603 switch (FTABLE[k].flag) { | |
16 | 604 case F_WTIME_P: /* #w<n> -- sets warning time */ |
605 sscanf(cp, "#w%u", &(evl[i].warn)); | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
606 break; |
16 | 607 case F_FORDAYS: /* #for<days> -- sets the duration of the event */ |
608 sscanf(cp, "#for%u", &d); | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
609 evl[i].enddate=evl[i].date; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
610 for (l = 1; l < d; l++) { |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
611 evl[i].enddate.day++; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
612 if (evl[i].enddate.day > mlen(evl[i].enddate.month, evl[i].enddate.year)) { |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
613 evl[i].enddate.month++; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
614 evl[i].enddate.day = 1; |
7 | 615 } |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
616 if (evl[i].enddate.month > 12) { |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
617 evl[i].enddate.year++; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
618 evl[i].enddate.month = 1; |
7 | 619 } |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
620 } |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
621 break; |
16 | 622 case F_TODATE: /* #to<date> -- sets the end date of the event */ |
623 l = sscanf(cp, "#to%u-%u-%u", &(evl[i].enddate.year), &(evl[i].enddate.month), &(evl[i].enddate.day)); | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
624 if (l == 2) { |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
625 evl[i].enddate.year = 0; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
626 } |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
627 break; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
628 case 0: |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
629 break; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
630 default: |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
631 flags |= FTABLE[k].flag; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
632 break; |
7 | 633 } |
634 } | |
635 | |
636 | |
637 /* construct event text */ | |
638 | |
639 switch(flags & F_MTYPE) { | |
640 default: /* assume it's a birthday */ | |
16 | 641 if (!evl[i].date.year) { |
642 sprintf(buf2, "%s has a birthday", cp); | |
643 break; | |
7 | 644 } |
16 | 645 int tmp_age = ydelta(evl[i].date, today); |
646 sprintf(buf2, "%s is %d year%s old", | |
647 cp, tmp_age, (tmp_age>1)?"s":""); | |
7 | 648 break; |
649 case F_TANNIVERSARY: | |
16 | 650 if (evl[i].date.year) { |
651 sprintf(buf2, "%s %d years ago", | |
652 cp, ydelta(evl[i].date, today)); | |
7 | 653 } else { |
16 | 654 strcpy(buf2, cp); |
7 | 655 } |
656 break; | |
657 case F_TEVENT: | |
16 | 658 /* if a year was specified, and this |
659 warning isn't for it, ignore! */ | |
660 if ((evl[i].date.year && ydelta(evl[i].date, today)) | |
661 && (!evl[i].enddate.year || ydelta(evl[i].enddate, today))) { | |
7 | 662 i--; |
663 continue; | |
664 } | |
16 | 665 strcpy(buf2, cp); |
7 | 666 break; |
667 } | |
668 evl[i].text = strdup(buf2); | |
669 } | |
670 | |
671 evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1)); | |
672 evl[i].date.day = 0; | |
673 evl[i].date.month = 0; | |
674 evl[i].date.year = 0; | |
675 evl[i].text = (char *) NULL; | |
676 | |
677 fclose(stdin); | |
678 | |
679 /* NB uses i from above */ | |
680 qsort(evl, i, sizeof(struct event), evcmp); | |
681 return evl; | |
682 } | |
683 | |
684 | |
685 | |
686 | |
687 | |
16 | 688 char * |
689 skptok(char *ptr) | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
690 { |
16 | 691 while (*ptr && (*ptr!=' ' && *ptr!='\t')) { |
692 ptr++; | |
693 } | |
694 while (*ptr && (*ptr==' ' || *ptr=='\t')) { | |
695 ptr++; | |
696 } | |
697 return ptr; | |
7 | 698 } |
699 | |
700 | |
701 | |
702 | |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
703 |
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
704 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
705 int |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
706 main(int argc, char *argv[]) |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
707 { |
16 | 708 while (--argc > 0 && **++argv == '-') { |
5
5af6bf2cb271
reads only stdin now, no files anymore; removed -f option aswell; code beatifing
meillo@marmaro.de
parents:
3
diff
changeset
|
709 if (strcmp(argv[0], "-W") == 0) { |
6 | 710 /* TODO: catch if no value given */ |
16 | 711 def_warn = atoi((++argv)[0]); |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
712 argc--; |
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
713 } else { |
3
dc2f94280b01
new Makefile; removed MinWarn and MaxWarn; adjusted manpage
meillo@marmaro.de
parents:
0
diff
changeset
|
714 fprintf(stderr, "unknown option %s\n", argv[0]); |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
715 exit(1); |
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
716 } |
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
717 } |
16 | 718 liststrings(readlist()); |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
719 return 0; |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
720 } |