summaryrefslogtreecommitdiffstats
path: root/minitimer.c
blob: 10e081a4856ab07d95d6fdef8e2e5397777b715e (plain)
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
/* See LICENSE file for copyright and license details. */

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <termios.h>
#include <unistd.h>

static char *argv0; /* Required here by arg.h */
#include "arg.h"
#include "config.h"

#define FIFONAME_SIZE 64

enum {
	PAUSRES_EV,
	INCR_EV,
	LAP_EV,
	QUIT_EV
};

struct time {
	int hrs;
	int mins;
	int secs;
};

static void die(const char *fmt, ...);
static void usage(void);

static int time_lt_zero(struct time the_time);
static void time_inc(struct time *the_time, int secs);
static int parse_time(char *time_str, struct time *the_time);

static struct termios ui_setup(struct termios *old);
static void ui_update(const struct time *the_time, int run_status, 
                      int lap_status);
static int poll_event(int fifofd);

static void
die(const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);

	vfprintf(stderr, fmt, ap);
	fputc('\n', stderr);

	va_end(ap);

	exit(1);
}

static void
usage(void)
{
	die("usage: minitimer [-sv] [HH:MM:SS]");
}

static int
time_lt_zero(struct time the_time)
{
	if (the_time.hrs < 0)
		return 1;
	else
		return 0;
}

static void
time_inc(struct time *the_time, int secs)
{
	int car;

	the_time->secs += secs;

	/* Basic sexagesimal arithmetics follow */

	if (the_time->secs < 0) {
		car = the_time->secs / 60 + 1;
		the_time->secs += 60 * car;
		the_time->mins -= car;
	}

	if (the_time->secs > 59) {
		the_time->mins += the_time->secs / 60;
		the_time->secs %= 60;
	}

	if (the_time->mins < 0) {
		car = the_time->mins / 60 + 1;
		the_time->mins += 60 * car;
		the_time->hrs -= car;
	}

	if (the_time->mins > 59) {
		the_time->hrs += the_time->mins / 60;
		the_time->mins %= 60;
	}
}

static int
parse_time(char *time_str, struct time *the_time)
{
	char *strptr, *errptr;

	strptr = strtok(time_str, ":");
	if (strptr == NULL)
		return -1;

	/* Hours */
	the_time->hrs = strtoul(strptr, &errptr, 10);
	if (*errptr != 0)
		return -1;

	/* Minutes */
	if ((strptr = strtok(NULL, ":")) != NULL) {
		the_time->mins = strtoul(strptr, &errptr, 10);
		if (*errptr != 0)
			return -1;
	} else {
		return -1;
	}

	/* Secs */
	if ((strptr = strtok(NULL, "\0")) != NULL) {
		the_time->secs = strtoul(strptr, &errptr, 10);
		if (*errptr != 0)
			return -1;
	} else {
		return -1;
	}

	/* Disallow input of negative values */
	if ((the_time->hrs < 0) || (the_time->mins < 0) ||
	    (the_time->secs < 0))
		return -1;
	else
		return 0;
}

static struct termios
ui_setup(struct termios *old)
{
	struct termios new;

	if (tcgetattr(STDIN_FILENO, old) < 0)
		die("Terminal attributes could not be read.");

	new = *old;
	new.c_lflag &= ~ICANON & ~ECHO;
	if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &new) < 0)
		die("Terminal attributes could not be set.");

	return new;
}

static void
ui_update(const struct time *the_time, int run_status, int lap_status)
{
	static struct time output;

	if (the_time != NULL)
		output = *the_time;

	printf("\r");
	putchar((run_status > 0) ? run_ind : ' ');
	putchar((lap_status > 0) ? lap_ind : ' ');
	printf(outputfmt, output.hrs, output.mins, output.secs);

	fflush(stdout);
}

static int
poll_event(int fifofd)
{
	fd_set fds;
	struct timeval tv;
	int n;
	char cmd_buf;

	FD_ZERO(&fds);
	FD_SET(STDIN_FILENO, &fds);
	FD_SET(fifofd, &fds);

	cmd_buf = 0;
	tv.tv_sec = 0L;
	tv.tv_usec = 0L;
	if ((n = select(fifofd + 1, &fds, NULL, NULL, &tv)) > 0) {
		if (FD_ISSET(STDIN_FILENO, &fds))
			read(STDIN_FILENO, &cmd_buf, 1);
		if (FD_ISSET(fifofd, &fds)) {
			read(fifofd, &cmd_buf, 1);
		}
	}

	switch (tolower(cmd_buf)) {
	case 'p':
		return PAUSRES_EV;
	case 'l':
		return LAP_EV;
	case '+':
		return INCR_EV;
	case 'q':
		return QUIT_EV;
	default:
		return -1;
	}
}

int
main(int argc, char *argv[])
{
	struct time the_time;
	int delta, parse_status, fifofd, timer_runs, lap_status;
	char fifoname[FIFONAME_SIZE];
	struct termios oldterm;

	delta = -1; /* Default is counting time down. */

	ARGBEGIN {
	case 's':
		delta = 1;
		break;
	case 'v':
		die("minitimer %s. See LICENSE file for copyright and license"
		    " details.", VERSION);
		break;
	default:
		usage();
		break;
	} ARGEND;

	/* the_time is set by default to 00:00:00 */
	memset(&the_time, 0, sizeof(struct time));
	if (argc > 0) {
		parse_status = parse_time(argv[0], &the_time);
		if (parse_status < 0)
			die("Invalid or ill-formed time (must be HH:MM:SS)");
	}

	/*
	 * Setting the named pipe up. This is based on ideas from the Býblos 
	 * text editor project (https://sr.ht/~ribal/byblos/)
	 */
	snprintf(fifoname, FIFONAME_SIZE, "%s%d", fifobase, getpid());
	if (mkfifo(fifoname, (S_IRUSR | S_IWUSR)) < 0)
		die("File %s not able to be created: %s.", fifoname,
		    strerror(errno));

	if ((fifofd = open(fifoname, (O_RDONLY | O_NONBLOCK))) < 0) {
		unlink(fifoname);
		die("File %s not able to be read: %s.", fifoname,
		    strerror(errno));
	}

	ui_setup(&oldterm);

	timer_runs = 1;
	lap_status = 0;
	while ((time_lt_zero(the_time) == 0)) {
		switch (poll_event(fifofd)) {
		case PAUSRES_EV:
			timer_runs ^= 1;
			break;
		case LAP_EV:
			lap_status ^= 1;
			break;
		case INCR_EV:
			time_inc(&the_time, time_incr_secs);
			break;
		case QUIT_EV:
			/* C is just syntactic sugar for ASM, isn't it? ;) */
			goto exit;
		}

		if (timer_runs > 0) {
			ui_update((lap_status > 0) ? NULL : &the_time, 
			          timer_runs, lap_status);
			time_inc(&the_time, delta);
		} else {
			ui_update(NULL, timer_runs, lap_status);
		}
		sleep(1);
	}

exit:
	fputc('\n', stdout);
	close(fifofd);
	unlink(fifoname);
	tcsetattr(STDIN_FILENO, TCSAFLUSH, &oldterm);

	return 0;
}