-
Notifications
You must be signed in to change notification settings - Fork 0
/
alarm.c
294 lines (274 loc) · 7.99 KB
/
alarm.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
/* alarm.c
*
* Copyright 2016 Bob Parker <rlp1938@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <libgen.h>
#include <ctype.h>
#include <time.h>
#include "fileops.h"
#include "firstrun.h"
char *getactdata(char *start, char *end, char *prm);
void failure (const char *emsg);
int countchars(char *str, int chr);
void runit(char *prog, char *param);
unsigned int addclocktime(time_t start, int hours, int minutes,
int seconds);
char *helpmsg = "NAME\n\talarm - a program to notify when a user "
" specified time has been reached\n"
"\nSYNOPSIS\n\talarm [option] hh:mm:ss [A|a|P|p]\n"
"\talarm [option] mm:ss [A|a|P|p]\n"
"\talarm [option] hh:mm: [A|a|P|p]\n"
"\talarm [option] mm [A|a|P|p]\n"
"\talarm [option] :ss [A|a|P|p]\n"
"\nDESCRIPTION\n\tThe program notifies the user when the input time is"
" reached. \n"
"\tBy default the input time is added to the start time, in other words\n\t"
"cooking timer mode, but optionally an actual clock"
" time may be input,\n\tie alarm clock mode.\n"
"See OPTIONS below.\n"
"\nOPTIONS\n\t-h - prints helpfile.\n"
"\t-a - Interprets the input time as clock time, not an increment\n\tfrom program start. "
"You should use the hh:mm:ss or hh:mm: time\n\tspecifiers when using this option.\n"
"NOTES"
"\n\tTimes may be specified in 24 hour or AMPM format, case insensitive.\n\tIf PM is input"
" then 12 hours will be added to the hours field if it's\n\tless than 12 but not otherwise."
" Apart from that no range checking will\n\tbe done. Consequently a 90 second interval"
" may be input as :90 or 1:30.\n"
"\tIn alarm mode the time specified will be the next occurrence of that\n\ttime so if you"
" input 6:00 at 9 am then the alarm will be scheduled for\n\t6 am tomorrow."
"\n\tThe actual alarm notification is done by running a movie player to\n\tdisplay "
"a short musical clip. Such clip together with the player is\n\tnamed in the file:\n"
"\t.config/alarm/alarm.txt or if that does not exist,\n\t/usr./local/etc/alarm/alarm.txt\n"
;
void dohelp(int forced);
char *memabslimit; // set by readfile, it's memmargin more than the data
int main(int argc, char **argv)
{
int opt, numcolon;
int totsec, seconds, minutes, hours;
fdata sfd;
time_t now;
char *user, *start, *end, *prog2run, *mov2show, *timespec;
char userbuf[FILENAME_MAX];
char *fmt = "/home/%s/.config/alarm/alarm.txt";
int cookingmode;
int ampm = 0;
// defaults
cookingmode = 1;
while((opt = getopt(argc, argv, ":ha")) != -1) {
switch(opt) {
case 'h':
dohelp(0);
break;
case 'a':
// absolute time of day ie alarm clock mode;
cookingmode = 0;
break;
case ':':
fprintf(stderr, "Option %c requires an argument\n",optopt);
dohelp(1);
break;
case '?':
fprintf(stderr, "Unknown option: %c\n",optopt);
dohelp(1);
break;
} //switch()
}//while()
// now process the non-option arguments
now = time(NULL);
// 1.Check that argv[???] exists.
if (!(argv[optind])) {
fprintf(stderr, "No time specifier provided.\n");
dohelp(1);
}
timespec = argv[optind];
optind++;
// go look for am or pm
if (argv[optind]) {
char *cp = argv[optind];
if (*cp == 'P' || *cp == 'p') ampm = 1;
// and it just doesn't matter if there is a following M|m or not
}
// Read my config file
user = getenv("USER");
sprintf(userbuf, fmt, user);
sfd = readfile(userbuf, 0, 0);
if(sfd.from) {
start = sfd.from;
end = sfd.to;
} else {
firstrun("alarm", "alarm.txt");
user = getenv("HOME");
char advice[NAME_MAX];
sprintf(advice, "Installed alarm.txt at %s.config/alarm/\n",
user);
fputs(advice, stderr);
fputs("Please edit that file to suit your needs.\n", stderr);
exit(EXIT_SUCCESS);
}
prog2run = getactdata(start, end, "program");
mov2show = getactdata(start, end, "data");
// now deal with the users timespec
numcolon = countchars(timespec, ':');
hours = minutes = seconds = 0;
char *wstr = strdup(timespec);
char *tofree = wstr; // wstr gets altered.
switch(numcolon) {
char *cp;
case 0:
// the number is minutes;
if (strlen(wstr)) minutes = strtol(wstr, NULL, 10);
break;
case 1:
// have mm:ss or :ss or mm:
cp = strchr(wstr, ':');
*cp = '\0';
if (strlen(wstr)) minutes = strtol(wstr, NULL, 10);
cp++; // look at the second half
wstr = cp;
if (strlen(wstr)) seconds = strtol(wstr, NULL, 10);
break;
case 2:
// have hh:mm:ss or hh:mm:
cp = strchr(wstr, ':');
*cp = '\0';
if (strlen(wstr)) hours = strtol(wstr, NULL, 10);
cp++; // next part
wstr = cp;
cp = strchr(wstr, ':');
*cp = '\0';
if (strlen(wstr)) minutes = strtol(wstr, NULL, 10);
cp++;
wstr = cp;
if (strlen(wstr)) seconds = strtol(wstr, NULL, 10);
break;
default:
fprintf(stderr,
"Badly formed time specification: %s\n", timespec);
exit(EXIT_FAILURE);
break;
}
free(tofree);
// total seconds to elapse before alarm
if (ampm) {
if (!(cookingmode)) {
if (hours < 12) hours += 12;
}
}
if(!(cookingmode)) { // used as an alarm clock, not cooking timer
totsec = addclocktime(now, hours, minutes, seconds);
} else {
totsec = seconds + 60 * minutes + 3600 * hours;
}
printf("Total seconds: %d\n", totsec);
// report every 60 seconds
int aminute = 60; // seconds
while (aminute < totsec) {
sleep(aminute);
totsec -= aminute;
if (cookingmode) {
fprintf(stdout, "Remaining: %d seconds.\n", totsec);
}
}
sleep(totsec);
runit(prog2run, mov2show);
return 0;
}//main()
void dohelp(int forced)
{
fputs(helpmsg, stderr);
exit(forced);
}
void failure(const char *emsg) {
/*
* The only exit point in the event of something invalid in command
*/
fprintf(stderr, "%s\n", emsg);
exit(EXIT_FAILURE);
} // failure()
char *getactdata(char *start, char *end, char *prm)
{
// look for prm between start and end if found return the string
// after '='
// else return NULL
static char *result;
char *cp;
result = memmem(start, end - start, prm, strlen(prm));
if(!(result)) return NULL;
cp = memchr(result, '=', end - result);
if (!(cp)) {
fprintf(stderr, "Badly formed parameter line for %s\n", prm);
exit(EXIT_FAILURE);
}
result = cp + 1; // point past '='
cp = memchr(result, '\n', end - result);
if (!(cp)) {
fprintf(stderr, "No line end for parameter: %s\n", prm);
exit(EXIT_FAILURE);
}
*cp = '\0';
return result;
}
int countchars(char *str, int chr)
{
// counts the occurences of chr in str
char *cp;
int res = 0;
cp = str;
while ((cp = strchr(cp, chr))) {
res++;
cp++;
}
return res;
}
void runit(char *prog, char *param)
{
char buf[FILENAME_MAX];
strcpy(buf, prog);
strcat(buf, " ");
strcat(buf, param);
dosystem(buf);
}
unsigned int addclocktime(time_t start, int hours, int minutes,
int seconds)
{
// add on the the hours etc to start
time_t alarmtime;
struct tm *st, *end;
st = localtime(&start);
end = localtime(&start); // initialise it the lazy way
if (hours < st->tm_hour) { // it's tomorrow
end->tm_hour = hours + 24; // I think I can get away with this
} else if (hours == st->tm_hour && minutes < st->tm_min) {
end->tm_hour = hours + 24;
} else { // will ignore the pathological case of hours and minutes == but seconds less
end->tm_hour = hours;
}
end->tm_sec = seconds;
end->tm_min = minutes;
alarmtime = mktime(end);
return (alarmtime - start);
} // addclocktime()