forked from AnaTofuZ/Perl-1.0
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstab.c
409 lines (392 loc) · 8.13 KB
/
stab.c
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/* $Header: stab.c,v 1.0.1.6 88/03/10 16:49:11 root Exp $
*
* $Log: stab.c,v $
* Revision 1.0.1.6 88/03/10 16:49:11 root
* patch29: made $! more magic than ever
* patch29: $< and $> are uid and euid, $( and $) are gid and egid
*
* Revision 1.0.1.5 88/02/04 11:16:57 root
* patch18: regularized includes.
*
* Revision 1.0.1.4 88/02/04 10:26:31 root
* patch17: now prints warning if no signal handler defined.
*
* Revision 1.0.1.3 88/02/04 00:13:15 root
* patch16: stabset didn't copy some strings it should have.
*
* Revision 1.0.1.2 88/02/02 11:25:53 root
* patch13: moved extern int out of function for a poor Xenix machine.
*
* Revision 1.0.1.1 88/01/28 10:35:17 root
* patch8: changed some stabents to support eval operator.
*
* Revision 1.0 87/12/18 13:06:14 root
* Initial revision
*
*/
#include "EXTERN.h"
#include "perl.h"
#include <signal.h>
static char *sig_name[] = {
"",
"HUP",
"INT",
"QUIT",
"ILL",
"TRAP",
"IOT",
"EMT",
"FPE",
"KILL",
"BUS",
"SEGV",
"SYS",
"PIPE",
"ALRM",
"TERM",
"???"
#ifdef SIGTSTP
,"STOP",
"TSTP",
"CONT",
"CHLD",
"TTIN",
"TTOU",
"TINT",
"XCPU",
"XFSZ"
#ifdef SIGPROF
,"VTALARM",
"PROF"
#ifdef SIGWINCH
,"WINCH"
#ifdef SIGLOST
,"LOST"
#ifdef SIGUSR1
,"USR1"
#endif
#ifdef SIGUSR2
,"USR2"
#endif /* SIGUSR2 */
#endif /* SIGLOST */
#endif /* SIGWINCH */
#endif /* SIGPROF */
#endif /* SIGTSTP */
,0
};
void sighandler(int);
int whichsig(char *);
STR *
stab_str(stab)
STAB *stab;
{
register int paren;
register char *s;
register int i;
switch (*stab->stab_name) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': case '&':
if (curspat) {
paren = atoi(stab->stab_name);
if (curspat->spat_compex.subend[paren] &&
(s = getparen(&curspat->spat_compex,paren))) {
curspat->spat_compex.subend[paren] = Nullch;
str_set(stab->stab_val,s);
}
}
break;
case '+':
if (curspat) {
paren = curspat->spat_compex.lastparen;
if (curspat->spat_compex.subend[paren] &&
(s = getparen(&curspat->spat_compex,paren))) {
curspat->spat_compex.subend[paren] = Nullch;
str_set(stab->stab_val,s);
}
}
break;
case '.':
if (last_in_stab) {
str_numset(stab->stab_val,(double)last_in_stab->stab_io->lines);
}
break;
case '?':
str_numset(stab->stab_val,(double)statusvalue);
break;
case '^':
s = curoutstab->stab_io->top_name;
str_set(stab->stab_val,s);
break;
case '~':
s = curoutstab->stab_io->fmt_name;
str_set(stab->stab_val,s);
break;
case '=':
str_numset(stab->stab_val,(double)curoutstab->stab_io->lines);
break;
case '-':
str_numset(stab->stab_val,(double)curoutstab->stab_io->lines_left);
break;
case '%':
str_numset(stab->stab_val,(double)curoutstab->stab_io->page);
break;
case '/':
*tokenbuf = record_separator;
tokenbuf[1] = '\0';
str_set(stab->stab_val,tokenbuf);
break;
case '[':
str_numset(stab->stab_val,(double)arybase);
break;
case '|':
str_numset(stab->stab_val,
(double)((curoutstab->stab_io->flags & IOF_FLUSH) != 0) );
break;
case ',':
str_set(stab->stab_val,ofs);
break;
case '\\':
str_set(stab->stab_val,ors);
break;
case '#':
str_set(stab->stab_val,ofmt);
break;
case '!':
str_numset(stab->stab_val, (double)errno);
/* Get a string describing the error number. */
str_set(stab->stab_val, strerror(errno));
stab->stab_val->str_nok = 1; /* what a wonderful hack! */
break;
case '<':
str_numset(stab->stab_val,(double)uid);
break;
case '>':
str_numset(stab->stab_val,(double)euid);
break;
case '(':
s = tokenbuf;
sprintf(s,"%d",(int)getgid());
goto add_groups;
case ')':
s = tokenbuf;
sprintf(s,"%d",(int)getegid());
add_groups:
while (*s) s++;
#ifdef HAS_GETGROUPS
#ifndef NGROUPS
#define NGROUPS 32
#endif
{
Gid_t gary[NGROUPS];
i = getgroups(NGROUPS,gary);
while (i >= 0) {
sprintf(s," %d", gary[i--]);
while (*s) s++;
}
}
#endif
str_set(stab->stab_val,tokenbuf);
break;
}
return stab->stab_val;
}
void
stabset(stab,str)
register STAB *stab;
STR *str;
{
char *s;
int i;
if (stab->stab_flags & SF_VMAGIC) {
switch (stab->stab_name[0]) {
case '^':
safefree(curoutstab->stab_io->top_name);
curoutstab->stab_io->top_name = s = savestr(str_get(str));
curoutstab->stab_io->top_stab = stabent(s,TRUE);
break;
case '~':
safefree(curoutstab->stab_io->fmt_name);
curoutstab->stab_io->fmt_name = s = savestr(str_get(str));
curoutstab->stab_io->fmt_stab = stabent(s,TRUE);
break;
case '=':
curoutstab->stab_io->page_len = (long)str_gnum(str);
break;
case '-':
curoutstab->stab_io->lines_left = (long)str_gnum(str);
break;
case '%':
curoutstab->stab_io->page = (long)str_gnum(str);
break;
case '|':
curoutstab->stab_io->flags &= ~IOF_FLUSH;
if (str_gnum(str) != 0.0) {
curoutstab->stab_io->flags |= IOF_FLUSH;
}
break;
case '*':
multiline = (int)str_gnum(str) != 0;
break;
case '/':
record_separator = *str_get(str);
break;
case '\\':
if (ors)
safefree(ors);
ors = savestr(str_get(str));
break;
case ',':
if (ofs)
safefree(ofs);
ofs = savestr(str_get(str));
break;
case '#':
if (ofmt)
safefree(ofmt);
ofmt = savestr(str_get(str));
break;
case '[':
arybase = (int)str_gnum(str);
break;
case '?':
statusvalue = (unsigned short)str_gnum(str);
break;
case '!':
errno = (int)str_gnum(str); /* will anyone ever use this? */
break;
case '<':
{
int r;
uid = (int)str_gnum(str);
#ifdef HAS_SETRUID
r = setruid(uid);
#else
#ifdef HAS_SETREUID
r = setreuid(uid, (Uid_t)-1);
#else
fatal("setruid() not implemented");
#endif
#endif
if (r < 0)
uid = (int)getuid();
break;
}
case '>':
euid = (int)str_gnum(str);
if (seteuid(euid) < 0)
euid = (int)geteuid();
break;
case '(':
{
int a = (int)str_gnum(str);
#ifdef HAS_SETRGID
setrgid(a);
#else
#ifdef HAS_SETREGID
setregid(a, (Uid_t)-1);
#else
fatal("setrgid() not implemented");
#endif
#endif
break;
}
case ')':
setegid((int)str_gnum(str));
break;
case '.':
case '+':
case '&':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
break; /* "read-only" registers */
}
}
else if (stab == envstab && envname) {
xsetenv(envname,str_get(str));
/* And you'll never guess what the dog had */
safefree(envname); /* in its mouth... */
envname = Nullch;
}
else if (stab == sigstab && signame) {
s = str_get(str);
i = whichsig(signame); /* ...no, a brick */
if (strEQ(s,"IGNORE"))
signal(i,SIG_IGN);
else if (strEQ(s,"DEFAULT") || !*s)
signal(i,SIG_DFL);
else
signal(i,sighandler);
safefree(signame);
signame = Nullch;
}
}
int
whichsig(signame)
char *signame;
{
register char **sigv;
for (sigv = sig_name+1; *sigv; sigv++)
if (strEQ(signame,*sigv))
return sigv - sig_name;
return 0;
}
void
sighandler(sig)
int sig;
{
STAB *stab;
ARRAY *savearray;
STR *str;
stab = stabent(str_get(hfetch(sigstab->stab_hash,sig_name[sig])),TRUE);
if (stab->stab_sub) {
savearray = defstab->stab_array;
defstab->stab_array = anew();
str = str_new(0);
str_set(str,sig_name[sig]);
apush(defstab->stab_array,str);
str = cmd_exec(stab->stab_sub);
afree(defstab->stab_array); /* put back old $_[] */
defstab->stab_array = savearray;
}
else
fprintf(stderr,"perl warning: SIG%s handler \"%s\" not defined.\n",
sig_name[sig], stab->stab_name );
}
char *
reg_get(name)
char *name;
{
return STAB_GET(stabent(name,TRUE));
}
#ifdef NOTUSED
reg_set(name,value)
char *name;
char *value;
{
str_set(STAB_STR(stabent(name,TRUE)),value);
}
#endif
STAB *
aadd(stab)
register STAB *stab;
{
if (!stab->stab_array)
stab->stab_array = anew();
return stab;
}
STAB *
hadd(stab)
register STAB *stab;
{
if (!stab->stab_hash)
stab->stab_hash = hnew();
return stab;
}