-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
321 lines (312 loc) · 9.93 KB
/
parse.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include "romcal.h"
static void usage()
{
version();
fprintf(stderr, "Usage: romcal [options] [year]\n");
fprintf(stderr, " where [year] is the 4-digit year (>1582)\n");
fprintf(stderr, " and [options] are as follows:\n");
fprintf(stderr, "-a: Ascension on Sunday\n");
fprintf(stderr, "-e: Epiphany on Jan. 6\n");
fprintf(stderr, "-c: Corpus Christi on Thursday\n");
fprintf(stderr, "-o: Do not print Optional Memorials or Commemorations\n");
fprintf(stderr, "-v: Print Version Number and Help\n");
fprintf(stderr, "-t: Print today's celebration\n");
fprintf(stderr, "-l: List directed I/O\n");
fprintf(stderr, "-L: List directed I/O (same as -l)\n");
fprintf(stderr, "-I: iCalendar output\n");
fprintf(stderr, "-P: PostScript output\n");
fprintf(stderr, "-C: Color PostScript output\n");
fprintf(stderr, "-H: HTML Output\n");
fprintf(stderr, "-R: RTF Output\n");
fprintf(stderr, "-W {directory}: Web Page Output\n");
fprintf(stderr, "-d {yyyymmdd}: Print the celebration of a given date\n");
fprintf(stderr, "Note: Current year is assumed if not provided.\n");
fprintf(stderr, "Note: The -t and -d options can be used with the -l option.\n");
}
struct Info *parse(int argc,
char *argv[])
{
/*----------------------------------------------------------------------*
*
* @(#)ROMCAL parse.c 6.2 02/12/06 17:25:35
*
* Copyright (C) 1993-2002, Kenneth G. Bath
* Permission to use and modify this software and its
* documentation for any purpose other than its incorporation
* into a commercial product is hereby granted without fee.
*
* Permission to copy and distribute this software and its
* documentation only for non-commercial use is also granted
* without fee, provided, however, that the above copyright
* notice appear in all copies, that both that copyright
* notice and this permission notice appear in supporting
* documentation. The author makes no representations about
* the suitability of this software for any purpose. It is
* provided "as is" without express or implied warranty.
*
*-------------------- Software Unit Description -----------------------*
*
* This module will parse the command line of the 'romcal' program
* and initialize the Info structure. On successful completion, it
* will return a pointer to the Info structure. Otherwise, it will
* return NULL.
*
*---------------------- Software Unit History -------------------------*
*
* 14Mar93 : created, kgb
* 19Dec93 : added "options.h" and "usage.h", kgb
* 20Jul94 : changed to return Info structure, removed options.h;
* added options to struct Info; added "today only"
* option, kgb
* 22Jul94 : added list directed I/O capability, kgb
* 13Feb95 : added PostScript I/O options, kgb
* 18Feb95 : removed "usage.h"; added "version.h", kgb
* 28Nov01 : added the "-d" option to allow the printing of
* a particular date, kgb
* 08Feb02 : added WEB_IO; removed version.h; added call to
* version(), kgb
*
*----------------------------------------------------------------------*/
int emonth,
eday,
yyyymmdd,
c;
extern int optind;
extern char *optarg;
static struct Info info;
struct tm *tp, tmstruct;
time_t clock;
/*----------------------------------------------------------------------*
* begin code *
*----------------------------------------------------------------------*/
/*
* Get the current time in a tm structure. Use local time.
*/
time(&clock);
tp = localtime(&clock);
/*
* Assume that Corpus Christi and Epiphany will be on Sundays and that
* Optional Memorials will be printed.
*/
info.cc_on_thurs = L_FALSE;
info.ep_on_jan6 = L_FALSE;
info.as_on_sun = L_FALSE;
info.print_optionals = L_TRUE;
info.today_only = -1;
info.io_option = TEXT_IO;
/*
* Parse options string(s). See getopt(3).
*/
while ((c = getopt(argc, argv, "ad:vceotlLIPCHRW:")) != EOF) {
if (c == 'c') {
info.cc_on_thurs = L_TRUE;
}
else if (c == 'e') {
info.ep_on_jan6 = L_TRUE;
}
else if (c == 'a') {
info.as_on_sun = L_TRUE;
}
else if (c == 'o') {
info.print_optionals = L_FALSE;
}
else if (c == 't') {
if (optarg == NULL) {
info.today_only = tp->tm_yday;
info.year = tp->tm_year + 1900;
}
else {
/*
* Clear out the temporary tm structure.
*/
memset(&tmstruct, 0, sizeof(tmstruct));
/*
* Expect the date in yyyymmdd format to be given as
* an argument.
*/
yyyymmdd = atoi(optarg);
/*
* Strip off the day of the month.
*/
tmstruct.tm_mday = yyyymmdd % 100;
yyyymmdd = yyyymmdd/100;
/*
* Strip off the month of the year. Note that one is subtracted
* because the tm structure defines its tm_mon member to be
* in the range 0 to 11.
*/
tmstruct.tm_mon = (yyyymmdd % 100) - 1;
yyyymmdd = yyyymmdd/100;
/*
* Strip off the year. Note that 1900 is subtracted because
* the tm structure defines its tm_year member to be the year
* minus 1900. For example, the year 2001 should be 101.
*/
tmstruct.tm_year = (yyyymmdd % 10000) - 1900;
/*
* According to the mktime() man page:
*
* In addition to computing the calendar time, mktime() normal-
* izes the supplied tm structure. The original values of the
* tm_wday and tm_yday components of the structure are ignored,
* and the original values of the other components are not res-
* tricted to the ranges indicated in the definition of the
* structure. On successful completion, the values of the
* tm_wday and tm_yday components are set appropriately, and
* the other components are set to represent the specified
* calendar time, but with their values forced to be within the
* appropriate ranges. The final value of tm_mday is not set
* until tm_mon and tm_year are determined.
*/
clock = mktime(&tmstruct);
/*
* Set up the info structure from the returned values of
* mktime().
*/
info.today_only = tmstruct.tm_yday;
info.year = tmstruct.tm_year + 1900;
}
}
else if (c == 'l' || c == 'L') {
info.io_option = LIST_IO;
}
else if (c == 'I') {
info.io_option = ICAL_IO;
}
else if (c == 'P') {
info.io_option = PSGREY_IO;
}
else if (c == 'C') {
info.io_option = PSCOLOR_IO;
}
else if (c == 'H') {
info.io_option = HTML_IO;
}
else if (c == 'R') {
info.io_option = RTF_IO;
}
else if (c == 'W') {
info.io_option = WEB_IO;
info.webio_dir = optarg;
fprintf(stderr,
"Web Output Directory: %s\n",
info.webio_dir);
}
else if (c == 'd') {
/*
* Clear out the temporary tm structure.
*/
memset(&tmstruct, 0, sizeof(tmstruct));
/*
* Expect the date in yyyymmdd format to be given as
* an argument.
*/
yyyymmdd = atoi(optarg);
/*
* Strip off the day of the month.
*/
tmstruct.tm_mday = yyyymmdd % 100;
yyyymmdd = yyyymmdd/100;
/*
* Strip off the month of the year. Note that one is subtracted
* because the tm structure defines its tm_mon member to be
* in the range 0 to 11.
*/
tmstruct.tm_mon = (yyyymmdd % 100) - 1;
yyyymmdd = yyyymmdd/100;
/*
* Strip off the year. Note that 1900 is subtracted because
* the tm structure defines its tm_year member to be the year
* minus 1900. For example, the year 2001 should be 101.
*/
tmstruct.tm_year = (yyyymmdd % 10000) - 1900;
/*
* According to the mktime() man page:
*
* In addition to computing the calendar time, mktime() normal-
* izes the supplied tm structure. The original values of the
* tm_wday and tm_yday components of the structure are ignored,
* and the original values of the other components are not res-
* tricted to the ranges indicated in the definition of the
* structure. On successful completion, the values of the
* tm_wday and tm_yday components are set appropriately, and
* the other components are set to represent the specified
* calendar time, but with their values forced to be within the
* appropriate ranges. The final value of tm_mday is not set
* until tm_mon and tm_year are determined.
*/
clock = mktime(&tmstruct);
/*
* Set up the info structure from the returned values of mktime().
*/
info.today_only = tmstruct.tm_yday;
info.year = tmstruct.tm_year + 1900;
}
else if (c == '?' || c == 'v') {
usage();
return (NULL);
}
}
if (info.today_only < 0) {
/*
* If no year was given, assume the current year.
*/
if (optind == argc - 1) {
info.year = atoi(argv[optind]);
}
else {
info.year = tp->tm_year + 1900;
}
/*
* Validate the year.
*/
if (info.year < 1583) {
fprintf(stderr,
"Year must be in Gregorian Calendar (> 1582).\n");
return (NULL);
}
}
else {
if (info.io_option == PSGREY_IO ||
info.io_option == PSCOLOR_IO ||
info.io_option == HTML_IO ||
info.io_option == RTF_IO) {
fprintf(stderr,
"PostScript, HTML, and RTF options not allowed with -t option\n");
return (NULL);
}
}
/*
* Compute the number of days.
*/
if (leapyear(info.year)) {
info.numdays = 366;
}
else {
info.numdays = 365;
}
/*
* Compute the day number of Easter Sunday.
*/
eastdate(info.year, &emonth, &eday);
info.edoy = doy(info.year, emonth, eday);
/*
* Compute the day number of Christmas Day.
*/
info.cdoy = doy(info.year, 12, 25);
/*
* Compute the Sunday Modulo. This is the value of the day number of any
* given Sunday, modulo 7. (Easter is always on a Sunday, so we'll use that
* one.)
*/
info.sunmod = info.edoy % 7;
/*
* Return pointer to Info structure.
*/
return (&info);
}