aboutsummaryrefslogtreecommitdiffstats
path: root/timer.c
blob: 87c90a3a28798fd854739c754c37069d7d45efdc (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
/*
 * Countdown timer
 *
 * Copyright (C) 2014  Tomasz Kramkowski <tk@the-tk.com>
 *
 * This program is free software. It is licensed under version 3 of the
 * GNU General Public License.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see [http://www.gnu.org/licenses/].
 */

#include <errno.h>
#include <error.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <time.h>
#include <unistd.h>
#include <limits.h>

#define SEC_SECOND        1
#define SEC_MINUTE       60
#define SEC_HOUR       3600
#define SEC_DAY       86400
#define SEC_MONTH   2629746
#define SEC_YEAR   31556940

static const unsigned long interval_nsec = 1000000000 / 4;
unsigned short term_width = 0; /* Maybe volatile? */

void sigwinch(int sig) {
	if (sig != SIGWINCH)
		return;

	struct winsize ws;

	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1)
		error(1, errno, "Failed to get terminal width");

	term_width = ws.ws_col;
}

unsigned long int get_seconds(char *code)
{
	int length = strlen(code), multiplier = 0;
	char suffix = code[length - 1], value[length + 1];
	unsigned long retval;

	if (length < 2) {
		return 0;
	}

	switch (suffix) {
		case 's': multiplier = SEC_SECOND; break; // 1 second
		case 'm': multiplier = SEC_MINUTE; break; // 1 minute
		case 'h': multiplier = SEC_HOUR;   break; // 1 hour
		case 'D': multiplier = SEC_DAY;    break; // 1 day
		case 'M': multiplier = SEC_MONTH;  break; // 30.4368 days
		case 'Y': multiplier = SEC_YEAR;   break; // 365.242 days
		default : return 0;
	}

	strncpy(value, code, length + 1);

	value[length - 1] = '\0';

	errno = 0;

	retval = strtoul(value, NULL, 10) * multiplier;

	if (retval == ULONG_MAX && errno != 0)
		error(1, errno, "Error converting time specifier %s", code);

	return retval;
}

void clear_line(void)
{
	if (term_width <= 1)
		return;

	for (unsigned short i = 0; i < term_width - 1; i++)
		putchar(' ');

	putchar('\r');

	if (fflush(stdout) != 0)
		error(1, errno, "Failed to flush stdout");
}


void print_time(unsigned long total_sec, bool show_colon)
{
	unsigned long years, months, days, hours, minutes, seconds;
	char colon = show_colon ? ':' : ' ', syears[128], smonths[16],
	     sdays[16];

	years = total_sec / SEC_YEAR;
	total_sec %= SEC_YEAR;

	if (years == 1)
		snprintf(syears, sizeof syears, "%lu year, ", years);
	else if (years > 1)
		snprintf(syears, sizeof syears, "%lu years, ", years);
	else
		syears[0] = '\0';

	months = total_sec / SEC_MONTH;
	total_sec %= SEC_MONTH;

	if (months == 1)
		snprintf(smonths, sizeof smonths, "%lu month, ", months);
	else if (months > 1)
		snprintf(smonths, sizeof smonths, "%lu months, ", months);
	else
		smonths[0] = '\0';

	days = total_sec / SEC_DAY;
	total_sec %= SEC_DAY;

	if (days == 1)
		snprintf(sdays, sizeof sdays, "%lu day, ", days);
	else if (days > 1)
		snprintf(sdays, sizeof sdays, "%lu days, ", days);
	else
		sdays[0] = '\0';

	hours = total_sec / SEC_HOUR;
	total_sec %= SEC_HOUR;

	minutes = total_sec / SEC_MINUTE;
	total_sec %= SEC_MINUTE;

	seconds = total_sec / SEC_SECOND;
	total_sec %= SEC_SECOND;

	if (total_sec != 0)
		error(1, 0, "An error occured during time formatting");

	printf(" %s%s%s%.2lu%c%.2lu%c%.2lu\r", syears, smonths, sdays, hours,
	       colon, minutes, colon, seconds);

	if (fflush(stdout) != 0)
		error(1, errno, "Failed to flush stdout");
}

void usage(char *cmd)
{
	printf("Usage:\n\t%s <n>{s,m,h,D,M,Y} ...\n", cmd);
}

int main(int argc, char **argv)
{
	bool blink = false;
	int sig;
	sigset_t sigset;
	struct itimerspec its;
	struct sigevent sev;
	struct sigaction sigact;
	timer_t timerid;

	unsigned long total_seconds = 0;

	if (argc < 2) {
		printf("Not enough arguments.\n");
		usage(argv[0]);
		exit(1);
	}

	for (int i = 1; i < argc; i++)
		total_seconds += get_seconds(argv[i]);

	sigact.sa_handler = sigwinch;

	if (sigaction(SIGWINCH, &sigact, NULL) != 0)
		error(1, errno, "Unable to set SIGWINCH signal action");

	sev.sigev_notify = SIGEV_SIGNAL;
	sev.sigev_signo = SIGRTMIN;
	sev.sigev_value.sival_ptr = &timerid;

	if (timer_create(CLOCK_MONOTONIC, &sev, &timerid) != 0)
		error(1, errno, "Could not create timer");

	its = (struct itimerspec){
		{0, interval_nsec},
		{0, interval_nsec}
	};

	if (sigemptyset(&sigset) != 0)
		error(1, errno, "Could not empty signal set");

	if (sigaddset(&sigset, SIGRTMIN) != 0)
		error(1, errno, "Could not add SIGRTMIN to signal set");

	if (sigprocmask(SIG_BLOCK, &sigset, NULL) != 0)
		error(1, errno, "Could not block SIGRTMIN");

	if (timer_settime(timerid, 0, &its, NULL) != 0)
		error(1, errno, "Could not set timer");

	sigwinch(SIGWINCH);

	for (unsigned long i = 0; i < total_seconds; i++)
		for (int ii = 0; ii < 4; ii++) {
			if (sigwait(&sigset, &sig), sig != SIGRTMIN)
				error(1, 0, "sigwait returned unexpected signal %d", sig);
			if (sigaddset(&sigset, SIGRTMIN) != 0)
				error(1, errno, "Could not add signal to set");

			clear_line();
			print_time(total_seconds - i, ii < 2);
		}

	if (sigaddset(&sigset, SIGINT) != 0)
		error(1, errno, "Could not add SIGINT to signal set");

	while (true) {
		blink = !blink;
		sigwait(&sigset, &sig);
		if (sig != SIGRTMIN)
			break;

		if (sigaddset(&sigset, SIGRTMIN) != 0)
			error(1, errno, "Could not add signal to set");

		clear_line();

		if (blink)
			printf(" -- BEEP -- \a\r");

		if (fflush(stdout) != 0)
			error(1, errno, "Failed to flush stdout");
	}

	timer_delete(timerid);

	clear_line();

	return 0;
}