forked from rakitzis/rc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddon.c
More file actions
375 lines (330 loc) · 7.68 KB
/
addon.c
File metadata and controls
375 lines (330 loc) · 7.68 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include <ctype.h>
#include "sigmsgs.h"
#include "rc.h"
#include "addon.h"
/*
This file is NOT BUILT by default. It provides an example of how
to add new builtins to rc.
To define a new builtin, it must appear in the macro ADDONS, which
is a comma-separated list of pairs of function pointers (the
implementation of the new builtin) and string literals (the name of
the new builtin).
Any new builtin functions must have prototypes of the same form:
void b_NAME(char **av);
The first argument, av[0], is the name of the builtin. The last
argument is followed by a NULL pointer.
Builtins report their exit status using set(TRUE) or set(FALSE).
*/
#if RC_ADDON
#include "calc_decl.h"
/******************************************************/
/******************************************************/
#define KILL_USAGE "usage: kill [-signame|-signum] pid+\n"
/******************************************************/
void b_kill(char **av) {
int p, sig;
bool ret;
if (av[1] == NULL) {
fprint(2, "%s", KILL_USAGE);
set(FALSE);
return;
}
if ('-' != av[1][0]) {
sig = SIGTERM;
p = 1;
} else {
char *sigStr = av[1] + 1;
sig = a2u(sigStr);
if (sig < 0) {
int s;
for (s = 0; signals[s].name; ++s) {
if (0 == strcasecmp(sigStr, signals[s].name)) {
/* -sigkill */
sig = signals[s].signum;
break;
} else if (0 == strcasecmp(sigStr, signals[s].name + 3)) {
/* -kill */
sig = signals[s].signum;
break;
}
}
if (sig < 0) {
fprint(2, "bad signal %s\n", sigStr);
set(FALSE);
return;
}
}
p = 2;
}
if (!av[p]) { /* must have at least one process id */
fprint(2, "%s", KILL_USAGE);
set(FALSE);
return;
}
ret = TRUE;
for (/*empty*/; av[p]; ++p) {
const char *const pidStr = av[p];
const pid_t pid = a2u(pidStr);
if (pid > 0) {
const int r = kill(pid, sig);
if (r != 0) {
ret = FALSE;
fprint(2, "failed to kill process %d with signal %d, return value %d\n", pid, sig, r);
}
} else {
ret = FALSE;
fprint(2, "bad process id: %s\n", pidStr);
}
}
set(ret);
}
/* `words` is a null-ptr terminated array of char ptrs */
static int CalcDoParse(char **words, CalcValue *r, CalcLexData *lexData);
#if 0
static void set_var(char *varname, long R)
{
List *val = nnew(List); var->w = nprint("%ld", R);
varassign(varname, val, FALSE);
}
#endif
/******************************************************/
static int check_var_name(const char *p) {
if (!isalpha(*p) && (*p) != '_') {
return 0;
}
for (; *p; ++p) {
if (!(isalnum(*p) || (*p) == '_')) {
return 0;
}
}
return 1;
}
/******************************************************/
enum {
VAL_NONZERO = 0,
VAL_ZERO = 1,
BAD_EXP = 2,
};
/******************************************************/
static int ret_value(int parse_status, long value) {
if (0 == parse_status) {
if (0L == value) {
return VAL_ZERO;
} else {
return VAL_NONZERO;
}
} else {
return BAD_EXP;
}
}
/******************************************************/
void b_calc(char **av) {
const char *const calcCmdName = av[0];
int rc_status = BAD_EXP;
if (av[1] == 0) { /* no arg like parse error */
fprint(2, "usage: %s [-p] [expr|assignment]\n", calcCmdName);
rc_status = BAD_EXP;
} else {
int i;
bool doPrint = FALSE;
i = 1;
if (streq(av[i], "-p")) {
doPrint = TRUE;
++i;
}
if (av[i] == 0) {
fprint(2, "usage: %s [-p] [expr|assignment]\n", calcCmdName);
rc_status = BAD_EXP;
} else {
CalcLexData lexData;
CalcValue parse_value = 0;
int parse_status;
lexData.m_CalcCmdName = calcCmdName;
parse_status = CalcDoParse(av + i, &parse_value, &lexData);
if (0 == parse_status) {
const char *const varName = &lexData.m_Indent[0];
if ('\0' != varName[0]) { /* assignment */
if (check_var_name(varName)) {
List val;
val.w = nprint("%ld", parse_value);
val.n = NULL;
varassign(varName, &val, FALSE);
rc_status = ret_value(parse_status, parse_value);
if (doPrint) {
fprint(1, "%ld\n", parse_value);
}
} else {
/* should this be treated as bad parse? */
fprint(2, "%s: bad variable name '%s'\n", calcCmdName, varName);
rc_status = BAD_EXP;
}
} else { /* no variable => not assignment */
rc_status = ret_value(parse_status, parse_value);
if (doPrint) {
fprint(1, "%ld\n", parse_value);
}
}
} else { /* parse error */
rc_status = BAD_EXP;
}
}
}
setN(rc_status);
return;
}
/******************************************************/
CalcValue CalcPower(const CalcValue A, const CalcValue B) {
CalcValue a = A, b = B;
CalcValue z = 1; /* z * a^b = A^B */
while (b > 0) {
if (b & 1) { /* odd */
/* z * a^b = (z * a) * (a^(b-1)) */
z = z * a;
b = b - 1;
} else {
/* z * a^(2k) = z * (a*a)^k */
a = a * a;
b = b / 2;
}
}
return z;
}
/******************************************************/
static CalcToken CalcLexer(CalcLexData *lexData, YYSTYPE *calclval) {
const char *p;
CalcToken tok;
int c;
next_word:
p = lexData->m_CurrentChar;
while (*p == ' ' || *p == '\t') {
p++;
}
if (isalpha(*p) || '_' == *p) {
enum { NUM_CHARS = sizeof(lexData->m_Indent) / sizeof(lexData->m_Indent[0]) - 1 };
int i = 0;
int c = *p;
while (isalnum(c) || '_' == c) {
if (i >= NUM_CHARS) {
return CALC_BAD_TOKEN;
}
lexData->m_Indent[i++] = *(p++);
c = *p;
}
lexData->m_Indent[i] = '\0';
lexData->m_CurrentChar = p;
return CALC_VAR;
}
switch (*p) {
case '^':
case '+':
case '-':
case '*':
case '/':
case '%':
case '~':
case '@':
case '(':
case ')':
tok = (CalcToken)(*p);
p++;
break;
case '|':
case '&':
c = *p++;
if (*p == c) {
tok = (c == '|' ? CALC_OROR : CALC_ANDAND);
p++;
} else {
tok = (CalcToken)(c);
}
break;
case '<':
case '>':
c = *p++;
if (*p == '=') {
tok = (c == '<' ? CALC_LEQ : CALC_GEQ);
p++;
} else if (c == *p) {
tok = (c == '<' ? CALC_LSHIFT : CALC_RSHIFT);
p++;
} else {
tok = (CalcToken)(c);
}
break;
case '=':
c = *p++;
if (*p == '=') {
tok = CALC_EQEQ;
p++;
} else {
tok = (CalcToken)(c);
}
break;
case '!':
c = *p++;
if (*p == '=') {
tok = CALC_NEQ;
p++;
} else {
tok = (CalcToken)(c);
}
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': {
CalcValue val = 0;
while ('0' <= *p && *p <= '9') {
val = 10 * val + (*p++ - '0');
}
calclval->m_Val = val;
tok = CALC_NUMBER;
}
break;
case '\0':
++lexData->m_CurrentWord;
if (*lexData->m_CurrentWord == 0) {
tok = (CalcToken)(0);
} else {
lexData->m_CurrentChar = *lexData->m_CurrentWord;
goto next_word;
}
break;
default:
tok = CALC_BAD_TOKEN;
break;
}
lexData->m_CurrentChar = p;
return tok;
} /* CalcLexer */
/******************************************************/
/******************************************************/
#if YYDEBUG
extern int calcdebug;
#endif
static int CalcDoParse(char **words, CalcValue *r, CalcLexData *lexData) {
int status;
lexData->m_CurrentWord = words;
lexData->m_CurrentChar = *lexData->m_CurrentWord;
lexData->m_Indent[0] = '\0';
#if YYDEBUG
{
const char *const yys = getenv("CALCDEBUG");
if (yys != 0) {
const int yyn = *yys;
if ('0' <= yyn && yyn <= '9') {
calcdebug = yyn - '0';
}
}
}
#endif
status = CalcParser(CalcLexer, lexData);
*r = lexData->m_CalcResult;
return status;
}
/******************************************************/
int CalcError(const char *s, const CalcLexData *lexData) {
fprint(2, "%s: %s\n", lexData->m_CalcCmdName, s);
return 0;
}
/******************************************************/
/******************************************************/
#endif