aboutsummaryrefslogtreecommitdiff
path: root/watch.c
blob: b2d21748574997d59bb17d4d31354abb50b965b2 (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
/*
 * Copyright (c) 2003 Dimitri Sokolyuk <demon@dim13.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/wait.h>

#include <curses.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sysexits.h>

int	die_flag = 0;
int	title_flag = 1;
int	resize_flag = 0;

extern	char *__progname;
extern	int LINES, COLS;

void	catchsig(int);
int	readargs(char **, char *, size_t);
int	readcmd(char *, size_t);
int	display(WINDOW *, char *, char *, size_t);
void	title(WINDOW *, char *, int);
void	resize(void);
void	settimer(int);
void	usage(void);

void
catchsig(int sig)
{
	switch (sig) {
	case SIGWINCH:
	case SIGALRM:
		resize_flag = 1;
		break;
	case SIGINT:
	case SIGTERM:
	case SIGHUP:
	default:
		die_flag = 1;
		break;
	}
}

int
main(int argc, char **argv)
{
	WINDOW	*titlew = NULL;
	WINDOW	*outw = stdscr;
	struct	sigaction sa;
	char	buf[_POSIX_MAX_INPUT];
	char	cmd[_POSIX_MAX_INPUT + 5];
	char	out[_POSIX_MAX_INPUT];
	int	hold_curs;
	int	ret = EX_SOFTWARE;
	int	delay = 2;	/* refresh every 2 seconds per default */
	int	ch;

	while ((ch = getopt(argc, argv, "+hn:t")) != -1)
		switch (ch) {
		case 'n':
			delay = atoi(optarg);
			if (delay < 1)
				usage();
				/* NOTREACHED */
			break;
		case 't':
			title_flag = 0;
			break;
		case 'h':
		case '?':
		default:
			usage();
			/* NOTREACHED */
		}

	argc -= optind;
	argv += optind;

	memset(buf, 0, sizeof(buf));

	if (readargs(argv, buf, sizeof(buf)) && readcmd(buf, sizeof(buf)))
		usage();
		/* NOTREACHED */

	memcpy(cmd, buf, sizeof(buf));
	strlcat(cmd, " 2>&1", sizeof(cmd));

	sigemptyset(&sa.sa_mask);
	sa.sa_flags = 0;
	sa.sa_handler = catchsig;

	sigaction(SIGINT, &sa, NULL);
	sigaction(SIGTERM, &sa, NULL);
	sigaction(SIGHUP, &sa, NULL);
	sigaction(SIGALRM, &sa, NULL);
	sigaction(SIGWINCH, &sa, NULL);

	initscr();
	hold_curs = curs_set(0);

	if (title_flag) {
		titlew = newwin(1, 0, 0, 0);
		wattron(titlew, A_BOLD);
		outw = newwin(LINES - 2, 0, 2, 0);
	}

	settimer(delay);

	do {
		if (resize_flag) {
			resize();
			if (title_flag) {
				wresize(titlew, 1, COLS); 
				clearok(titlew, TRUE);
				wresize(outw, LINES - 2, COLS); 
			}
			clearok(outw, TRUE);
			resize_flag = 0;
		}

		if (title_flag)
			title(titlew, buf, delay);

		ret = display(outw, cmd, out, sizeof(out));
		doupdate();

		sigsuspend(&sa.sa_mask);
	} while (!die_flag);

	if (title_flag) {
		delwin(outw);
		delwin(titlew);
	}

	curs_set(hold_curs);
	endwin();

	if (ret)
		(void)fprintf(stderr, "%s: %s", __progname, out);

	return ret;
}

int
readargs(char **argv, char *buf, size_t sz)
{
	if (!*argv)
		return -1;

	while (*argv) {
		strlcat(buf, *argv++, sz);
		strlcat(buf, " ", sz);
	}

	return 0;
}

int
readcmd(char *buf, size_t sz)
{
	char	*s;

	if (!isatty(fileno(stdin)))
		return -1;

	(void)fprintf(stderr, "command: ");

	if (fgets(buf, sz - 1, stdin) && (s = strchr(buf, '\n')))
		*s = '\0';

	return 0;
}

int
display(WINDOW *outw, char *cmd, char *out, size_t sz)
{
	FILE	*pipe;
	int	ret = EX_OSERR;
	int	y, x;

	pipe = popen(cmd, "r");

	if (pipe) {
		wmove(outw, 0, 0);
		getmaxyx(outw, y, x);

		while (fgets(out, sz - 1, pipe) && y--)
			waddnstr(outw, out, x);

		ret = pclose(pipe);
		if (ret)
			die_flag = 1;

		wclrtobot(outw);
		wnoutrefresh(outw);
	}

	return WEXITSTATUS(ret);
}

void
title(WINDOW *titlew, char *buf, int delay)
{
	time_t	tval;
	struct	tm *tm;
	int	y, x;

	tval = time(NULL);
	tm = localtime(&tval);

	werase(titlew);
	mvwprintw(titlew, 0, 0, "Every %ds: %s", delay, buf);

	getyx(titlew, y, x);
	if (x > COLS - 8)
		mvwaddstr(titlew, 0, COLS - 12, "... ");

	mvwprintw(titlew, 0, COLS - 8, "%.2d:%.2d:%.2d",
	    tm->tm_hour, tm->tm_min, tm->tm_sec);

	wnoutrefresh(titlew);
}

void
resize(void)
{
	struct	winsize ws;

	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1)
		resizeterm(ws.ws_row, ws.ws_col);
}

void
settimer(int sec)
{
	struct	itimerval itv;

	itv.it_value.tv_sec = sec;
	itv.it_value.tv_usec = 0;
	itv.it_interval = itv.it_value;

	setitimer(ITIMER_REAL, &itv, NULL);
}

void
usage(void)
{
	(void)fprintf(stderr, "usage: %s [-ht] [-n time] [command]\n",
	    __progname);
	exit(EX_USAGE);
}