-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlenstatus.c
More file actions
190 lines (159 loc) · 3.51 KB
/
Copy pathlenstatus.c
File metadata and controls
190 lines (159 loc) · 3.51 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
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/wait.h>
#ifndef NO_X
#include <X11/Xlib.h>
#endif
typedef void (*cfunc_t)(char *, int *);
union func {
char *const *cmd;
cfunc_t func;
};
struct segment {
int interval;
int sig;
const char *prefix;
bool iscmd;
union func f;
};
#include "config.h"
#define LEN(x) (sizeof(x) / sizeof(x[0]))
int delimlen;
#ifndef NO_X
Display *dpy;
Window root;
#endif
char bufs[LEN(segs)][64];
char *self;
int lens[LEN(segs)];
char out[2][256];
int outlen[2];
void format() {
int len = 0, pflen;
for (int i = 0; i < LEN(segs); i++) {
pflen = segs[i].prefix ? strlen(segs[i].prefix) : 0;
if (len + pflen >= 255)
goto end;
memcpy(out[0] + len, segs[i].prefix, pflen);
len += pflen;
for (int j = 0; j < lens[i]; j++) {
if (len == 255)
goto end;
if (bufs[i][j] == '\n')
break;
out[0][len++] = bufs[i][j];
}
if (len + delimlen >= 255 || i + 1 == LEN(segs))
goto end;
memcpy(out[0] + len, delim, delimlen);
len += delimlen;
}
end:
outlen[0] = len;
}
void execcmd(int seg) {
int fds[2];
if (pipe(fds) != 0)
return;
if (fork() != 0) {
wait(NULL);
lens[seg] = read(fds[0], bufs[seg], 64);
close(fds[0]);
close(fds[1]);
return;
}
dup2(fds[1], STDOUT_FILENO);
execvp(segs[seg].f.cmd[0], segs[seg].f.cmd);
close(fds[0]);
close(fds[1]);
}
void execute(int seg) {
if (segs[seg].iscmd) {
execcmd(seg);
} else {
segs[seg].f.func(bufs[seg], lens + seg);
}
}
void update(int seg) {
execute(seg);
format();
}
void writebuf(void) {
if (outlen[0] == outlen[1]) {
if (memcmp(out[0], out[1], outlen[0]) == 0)
return;
}
memcpy(out[1], out[0], outlen[0]);
outlen[1] = outlen[0];
#ifndef NO_X
out[0][outlen[0]] = '\0';
XStoreName(dpy, root, out[0]);
XFlush(dpy);
#else
printf("%.*s\n", outlen[0], out[0]);
#endif
}
void sighandler(int sig) {
if (sig == SIGHUP) {
fprintf(stderr, "restarting...\n");
exit(execvp(self, (char *const[]){self, NULL}));
}
sig = sig - SIGRTMIN;
for (int i = 0; i < LEN(segs); i++)
if (segs[i].sig == sig)
update(i);
writebuf();
}
void setup(void) {
struct sigaction sa = {{0}, .sa_handler = sighandler};
#ifndef NO_X
dpy = XOpenDisplay(NULL);
if (dpy == NULL) {
fprintf(stderr, "error opening x\n");
exit(1);
}
root = DefaultRootWindow(dpy);
#endif
sigaction(SIGHUP, &sa, NULL);
for (int i = 0; i < LEN(segs); i++) {
if (segs[i].sig)
sigaction(SIGRTMIN + segs[i].sig, &sa, NULL);
execute(i);
}
format();
writebuf();
}
void process(uint64_t t) {
for (int i = 0; i < LEN(segs); i++) {
if (segs[i].interval && t % segs[i].interval == 0) {
update(i);
writebuf();
}
}
}
int main(int argc, char *argv[]) {
uint64_t i = 0;
struct timespec t, orig = {1, 0};
self = argv[0];
delimlen = strlen(delim);
setup();
t = orig;
while (1) {
if (nanosleep(&t, &t) == -1)
continue;
process(i);
i++;
t = orig;
}
#ifndef NO_X
XCloseDisplay(dpy);
#endif
}