-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
460 lines (346 loc) · 12.3 KB
/
Copy pathmain.c
File metadata and controls
460 lines (346 loc) · 12.3 KB
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
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <strings.h>
#include <stdbool.h>
#include <time.h>
#include "lib/calendar.h"
#include "lib/operations.h"
#include "lib/util.h"
static int scan_month_day(char *input, uint8_t *output, bool clamp);
static int scan_specific_month_day(char *input, uint8_t *output, uint8_t month, bool clamp);
static int scan_exact_month_day(char *input, uint8_t *output, uint8_t month, int32_t year);
static int scan_month(char *input, uint8_t *output);
static int scan_year(char *input, int32_t *output);
static int scan_hour(char *input, Hour *output);
static int scan_week_day(char *input, uint8_t *output);
static int scan_id(char* input, Id *output);
static int parse_alarm(int len, char *args[], Alarm *out);
static int parse_alarm_add(int len, char *args[]);
static int parse_alarm_edit(int len, char *args[]);
static int parse_alarm_list(char *filter);
static int parse_alarm_remove(int len, char *args[]);
static int parse_alarm_clear(int len, char *args[]);
static int parse_import(int len, char *args[]);
static int check_description(char *description);
int main(int argc, char *argv[]) {
srand(time(NULL));
if (argc == 1) {
print_current_month();
return 0;
}
char *option = argv[1];
if (strcasecmp(option, "now") == 0)
print_now();
else if (strcasecmp(option, "help") == 0)
help();
else if (strcasecmp(option, "version") == 0)
version();
else if (strcasecmp(option, "watch") == 0)
watch();
else if (strcasecmp(option, "import") == 0 && argc >= 3)
parse_import(argc, argv);
else if (strcasecmp(option, "export") == 0 && argc == 3)
export_alarms(argv[2]);
else if (strcasecmp(option, "alarm") == 0 && argc > 2) {
char *suboption = argv[2];
// adjust args to point to the relevant information
argc -= 3;
argv += 3;
if (strcasecmp(suboption, "add") == 0 && argc >= 2)
parse_alarm_add(argc, argv);
else if (strcasecmp(suboption, "edit") == 0 && argc >= 1)
parse_alarm_edit(argc, argv);
else if (strcasecmp(suboption, "list") == 0) {
if (argc >= 1)
parse_alarm_list(argv[0]);
else
alarm_list_all();
}
else if (strcasecmp(suboption, "remove") == 0 && argc >= 1)
parse_alarm_remove(argc, argv);
else if (strcasecmp(suboption, "clear") == 0)
parse_alarm_clear(argc, argv);
else
ERROR("Invalid alarm option or incorrect number of arguments.");
}
else if (argc == 2) {
int32_t year;
if (scan_year(option, &year) != 0)
return 1;
print_year(year);
}
else if (argc == 3) {
uint8_t month;
int32_t year;
if (scan_month(argv[1], &month) != 0 || scan_year(argv[2], &year) != 0)
return 1;
print_month(month, year);
}
else
ERROR("Invalid option.");
return 0;
}
static int scan_month_day(char *input, uint8_t *output, bool clamp) {
if (sscanf(input, "%hhu", output) != 1)
ERROR("Invalid day of the month.");
if (*output < 1 || *output > 31)
ERROR("Day of the month must be between 1 and 31.");
if (*output > 28 && !clamp)
ERROR("Not all months have this day of the month. Please specify the '--clamp' flag to clamp the day in months that don't have it.");
return 0;
}
// checks if the day of a specific month exists in all years.
// all months have predefined days, except for February, which in leap years it has one more day. this is why we need 'clamp'.
// 'month' is base 1.
static int scan_specific_month_day(char *input, uint8_t *output, uint8_t month, bool clamp) {
// we won't consider February with 29 days in leap years, because this checks in all years, and February has at least 28 days.
static const uint8_t days_in_month[] = {
31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31,
};
if (sscanf(input, "%hhu", output) != 1)
ERROR("Invalid day of the month.");
if (*output < 1 || *output > 31)
ERROR("Day of the month must be between 1 and 31.");
// check if clamp is necessary for February
if (month == 2 && *output == 29) {
if (!clamp)
ERROR("Not all February months have 29 days. Use the '--clamp' flag to clamp the day.");
}
else if (*output > days_in_month[month - 1])
ERROR("This month doesn't have this amount of days.");
// TODO: change clamp to false in these cases,
// which requires it to be a pointer instead.
if (*output <= days_in_month[month - 1] && clamp)
WARN("Clamping isn't necessary in this case.");
return 0;
}
// checks if the exact day of a specific year exists.
// this doesn't need to clamp, because this checks for an specific date.
static int scan_exact_month_day(char *input, uint8_t *output, uint8_t month, int32_t year) {
static uint8_t days_in_month[] = {
31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31,
};
if (is_leap_year(year))
days_in_month[1] = 29;
if (sscanf(input, "%hhu", output) != 1)
ERROR("Invalid day of the month.");
if (*output > days_in_month[month])
ERROR("This month doesn't have this amount of days.");
return 0;
}
static int scan_month(char *input, uint8_t *output) {
if (sscanf(input, "%hhu", output) != 1)
ERROR("Invalid month.");
if (*output < 1 || *output > 12)
ERROR("Month must be between 1 and 12.");
return 0;
}
static int scan_year(char *input, int32_t *output) {
if (sscanf(input, "%d", output) != 1)
ERROR("Invalid year.");
if (*output <= 0)
ERROR("Year must be greater than 0.");
return 0;
}
static int scan_hour(char *input, Hour *output) {
uint8_t hours, minutes;
if (sscanf(input, "%hhu:%hhu", &hours, &minutes) != 2)
ERROR("Please specify the hour correctly: 'hh:mm'.");
*output = (Hour) {
.hours = hours,
.minutes = minutes,
};
if (!is_valid_hour(*output))
ERROR("Please specify a valid hour.");
return 0;
}
static int scan_week_day(char *input, uint8_t *output) {
if (sscanf(input, "%hhu", output) != 1)
ERROR("Invalid day of the week.");
if (*output < 1 || *output > 7)
ERROR("Day of the week must be between 1 and 7.");
return 0;
}
static int scan_id(char* input, Id *output) {
if (sscanf(input, "%hu", output) != 1)
ERROR("Invalid ID.");
return 0;
}
// it's not this function's responsibility to free 'out'
static int parse_alarm(int len, char *args[], Alarm *out) {
if (len == 0)
ERROR("Usage: <description> <daily | weekly | monthly | yearly | once>.");
char *description = args[0]; // guaranteed
if (check_description(description) != 0)
return 1;
out->description = duplicate_str(description); // heap-allocate it
if (len <= 1)
ERROR("Please specify the frequency.");
char *freq = args[1];
if (strcasecmp(freq, "daily") == 0) {
if (len < 3)
ERROR("Usage: daily <hour>.");
Hour hour;
if (scan_hour(args[2], &hour) != 0)
return 1;
out->type = (AlarmType) {
.id = ALARM_DAILY,
.alarm.daily = {
.hour = hour,
},
};
}
else if (strcasecmp(freq, "weekly") == 0) {
if (len < 4)
ERROR("Usage: weekly <day of the week> <hour>.");
uint8_t week_day;
Hour hour;
if (scan_week_day(args[2], &week_day) != 0 || scan_hour(args[3], &hour) != 0)
return 1;
out->type = (AlarmType) {
.id = ALARM_WEEKLY,
.alarm.weekly = {
.week_day = week_day,
.hour = hour,
},
};
}
else if (strcasecmp(freq, "monthly") == 0) {
if (len < 4)
ERROR("Usage: monthly <day of the month> <hour> [--clamp].");
uint8_t month_day;
Hour hour;
bool clamp = false;
if (len >= 5 && strcasecmp(args[4], "--clamp") == 0)
clamp = true;
if (scan_month_day(args[2], &month_day, clamp) != 0 || scan_hour(args[3], &hour) != 0)
return 1;
out->type = (AlarmType) {
.id = ALARM_MONTHLY,
.alarm.monthly = {
.month_day = month_day,
.hour = hour,
.clamp = clamp,
},
};
}
else if (strcasecmp(freq, "yearly") == 0) {
if (len < 5)
ERROR("Usage: yearly <month> <day of the month> <hour> [--clamp].");
uint8_t month, month_day;
Hour hour;
bool clamp = false;
if (len >= 6 && strcasecmp(args[5], "--clamp") == 0)
clamp = true;
if (scan_month(args[2], &month) != 0 ||
scan_specific_month_day(args[3], &month_day, month, clamp) != 0 ||
scan_hour(args[4], &hour) != 0)
return 1;
out->type = (AlarmType) {
.id = ALARM_YEARLY,
.alarm.yearly = {
.month = month,
.month_day = month_day,
.hour = hour,
.clamp = clamp,
},
};
}
else if (strcasecmp(freq, "once") == 0) {
if (len < 6)
ERROR("Usage: once <year> <month> <day of the month> <hour>.");
uint8_t month, month_day;
int32_t year;
Hour hour;
if (scan_year(args[2], &year) != 0 ||
scan_month(args[3], &month) != 0 ||
scan_exact_month_day(args[4], &month_day, month, year) != 0 ||
scan_hour(args[5], &hour) != 0)
return 1;
out->type = (AlarmType) {
.id = ALARM_ONCE,
.alarm.once = {
.year = year,
.month = month,
.month_day = month_day,
.hour = hour,
},
};
if (date_has_passed(out->type.alarm.once, now()))
WARN("This date has already passed, so the alarm for it won't ring.");
}
else
ERROR("Usage: <description> <daily | weekly | monthly | yearly | once>.");
return 0;
}
static int parse_alarm_add(int len, char *args[]) {
Alarm alarm;
if (parse_alarm(len, args, &alarm) != 0) {
free_alarm(&alarm);
return 1;
}
bool yes = strcasecmp(args[len - 1], "-y") == 0;
alarm_add(alarm, yes);
return 0;
}
static int parse_alarm_edit(int len, char *args[]) {
Id id;
if (scan_id(args[0], &id) != 0)
return 1;
args++;
len--;
Alarm alarm;
if (parse_alarm(len, args, &alarm) != 0) {
free_alarm(&alarm);
return 1;
}
alarm_edit(id, alarm);
return 0;
}
static int parse_alarm_list(char *filter) {
AlarmFilter filt;
if (strcasecmp(filter, "daily") == 0)
filt = ALARM_DAILY;
else if (strcasecmp(filter, "weekly") == 0)
filt = ALARM_WEEKLY;
else if (strcasecmp(filter, "monthly") == 0)
filt = ALARM_MONTHLY;
else if (strcasecmp(filter, "yearly") == 0)
filt = ALARM_YEARLY;
else if (strcasecmp(filter, "once") == 0)
filt = ALARM_ONCE;
else
ERROR("Invalid filter.");
alarm_list(filt);
return 0;
}
static int parse_alarm_remove(int len, char *args[]) {
bool yes = len == 2 && strcasecmp(args[1], "-y") == 0;
Id out_id;
if (scan_id(args[0], &out_id) != 0)
return 1;
alarm_remove(out_id, yes);
return 0;
}
static int parse_alarm_clear(int len, char *args[]) {
bool yes = len == 1 && strcasecmp(args[0], "-y") == 0;
alarm_clear(yes);
return 0;
}
static int parse_import(int len, char *args[]) {
char *filename = args[2];
bool yes = len > 3 && strcasecmp(args[3], "-y") == 0;
import_alarms(filename, yes);
return 0;
}
static int check_description(char *description) {
if (strchr(description, SEPARATOR_CHAR) != NULL)
ERROR("The description must not contain '" SEPARATOR "\'.");
if (strlen(description) > MAX_DESCRIPTION_LENGTH)
ERROR("Description too long. Must have less than 1024 characters.");
return 0;
}