|
|
1.1 root 1: /*
2: * Public Domain Bourne/Korn shell
3: */
4: /* allow for non-Unix linkers. main.c has a "#define Extern " */
5: #ifndef Extern
6: #define Extern extern
7: #endif
8:
9: typedef int bool_t;
10: #define FALSE 0
11: #define TRUE 1
12:
13: #ifndef SHELL
14: #define SHELL "/bin/sh" /* shell to exec scripts */
15: #endif
16:
17: #if __STDC__
18: #define ARGS(args) args /* prototype declaration */
19: #define Void void /* generic pointer */
20: #define Const const /* constant data */
21: #define Volatile volatile /* you know */
22: #else
23: #define ARGS(args) () /* K&R declaration */
24: #define Void char
25: #define Const
26: #define Volatile
27: #endif
28:
29: #define sizeofN(type, n) (sizeof(type) * (n))
30: #define BIT(i) (1<<(i)) /* define bit in flag */
31:
32: #define NUFILE 10 /* Number of user-accessible files */
33: #define FDBASE 10 /* First file usable by Shell */
34:
35: /* you're not going to run setuid shell scripts, are you? */
36:
37: #define MAGIC (char)0x80 /* prefix for ~*?[ during expand */
38:
39: #define LINE 256 /* input line size */
40: #define PATH 256 /* pathname size */
41:
42: Extern int exstat; /* exit status */
43: Extern int async; /* $!, last &'d pid */
44:
45: /*
46: * Area-based allocation built on malloc/free
47: */
48:
49: typedef struct Area {
50: struct Block *free; /* free list */
51: } Area;
52:
53: extern Area aperm; /* permanent object space */
54: #define APERM &aperm
55: #define ATEMP &e.area
56:
57: Area *ainit ARGS((Area *ap)); /* initialize Area */
58: void afreeall ARGS((Area *ap)); /* free Area's contents */
59: void ashrink ARGS((Area *ap)); /* unimplimented */
60: void aerror ARGS((Area *ap, const char *msg)); /* error handler */
61:
62: Void *alloc ARGS((size_t size, Area *ap)); /* alloc object from Area */
63: Void *aresize ARGS((Void *ptr, size_t size, Area *ap)); /* shrink object */
64: void afree ARGS((Void *ptr, Area *ap)); /* free ojbect in Area */
65:
66: /*
67: * parsing & execution environment
68: */
69: Extern struct env {
70: int type; /* enviroment type - see below */
71: Area area; /* temporary allocation area */
72: struct block *loc; /* local variables and functions */
73: short *savefd; /* original redirected fd's */
74: struct env *oenv; /* link to previous enviroment */
75: jmp_buf jbuf; /* long jump back to env creator */
76: int interactive; /* fd's 0,1,2 are tty */
77: struct temp *temps; /* temp files */
78: } e;
79:
80: #define E_NONE 0 /* dummy enviroment */
81: #define E_PARSE 1 /* parsing command # */
82: #define E_EXEC 2 /* executing command tree */
83: #define E_LOOP 3 /* executing for/while # */
84: #define E_TCOM 5 /* executing simple command */
85: #define E_FUNC 6 /* executing function */
86: #define E_ERRH 7 /* general error handler # */
87: /* # indicates env has valid jbuf */
88:
89: /*
90: * flags
91: */
92: #define FEXPORT FLAG('a') /* -a: allexport */
93: #define FERREXIT FLAG('e') /* -e: errexit (quit on error) */
94: #define FBGNICE 29 /* bgnice */
95: #define FEMACS 30 /* emacs */
96: #define FIGNEOF 27 /* ignoreeof (eof does not exit) */
97: #define FHASHALL FLAG('h') /* -h: trackall, hashall */
98: #define FTALKING FLAG('i') /* -i: interactive (talking type wireless) */
99: #define FKEYWORD FLAG('k') /* -k: keyword (name=value anywhere) */
100: #define FMARKDIRS 28 /* markdirs */
101: #define FMONITOR FLAG('m') /* -m: monitor */
102: #define FNOEXEC FLAG('n') /* -n: noexec */
103: #define FNOGLOB FLAG('f') /* -f: noglob */
104: #define FPRIVILEGED FLAG('p') /* -p: privileged */
105: #define FSTDIN FLAG('s') /* -s (invocation): parse stdin */
106: #define FNOUNSET FLAG('u') /* -u: nounset (unset vars is error) */
107: #define FVERBOSE FLAG('v') /* -v: verbose (echo input) */
108: #define FXTRACE FLAG('x') /* -x: (execute) xtrace */
109:
110: #define FLAG(c) (1 + c - 'a') /* map char to flags index */
111: #define FLAGS 32
112: Extern char flag [FLAGS];
113: int option ARGS((Const char *name));
114: char *getoptions ARGS((void));
115: void printoptions ARGS((void));
116: void mail ARGS((void));
117:
118: extern char null []; /* null value for variable */
119:
120: /*
121: * other functions
122: */
123: char *search();
124: struct tbl *findcom();
125: char *strsave ARGS((char *, Area *));
126: char *ulton ARGS((unsigned long n, int base));
127: int xstrcmp();
128: void qsortp ARGS((void **base, size_t n, int (*compare)(void *, void *)));
129: long evaluate ARGS((Const char *expr));
130: void resetopts();
131: void histsave();
132: void histlist();
133:
134: #if EDIT
135: void x_init();
136: void x_bind();
137: int x_read();
138: #endif
139:
140: void j_init ARGS((void));
141: void j_exit ARGS((void));
142: void j_notify ARGS((void));
143: void j_kill ARGS((int job, int sig));
144: #ifdef JOBS
145: void j_change ARGS((void));
146: int j_resume ARGS((int job, int bg));
147: #endif
148:
149: /*
150: * error handling
151: */
152: void leave(); /* abort shell (or fail in subshell) */
153:
154: /*
155: * library functions
156: */
157: typedef void (*handler_t)(); /* signal handler */
158:
159: /* temp/here files. the file is removed when the struct is freed */
160: struct temp {
161: struct temp * next;
162: char *name;
163: };
164: struct temp *maketemp ARGS((Area *ap));
165:
166: /*
167: * stdio and our IO routines
168: */
169:
170: #ifdef BUFSIZ /* <stdio.h> included? */
171: extern FILE * shf [NUFILE]; /* map shell fd to FILE */
172: #endif
173: void fopenshf();
174: void flushshf();
175:
176: #undef stdin
177: #undef stdout
178:
179: #define stdin shf[0] /* standard input */
180: #define stdout shf[1] /* standard output */
181: #define shlout shf[2] /* shell output */
182:
183: int shellf ARGS((Const char *fmt, ...)); /* fprintf(shlout, ); */
184: int errorf ARGS((Const char *fmt, ...)); /* fprintf(shlout, ); error(); */
185:
186: /*
187: * IO control
188: */
189: extern int ttyfd; /* tty fd (original fd 0) */
190:
191: int savefd ARGS((int fd)); /* save user fd */
192: void restfd ARGS((int fd, int ofd));
193: void openpipe ARGS((int [2]));
194: void closepipe ARGS((int [2]));;
195:
196: /*
197: * trap handlers
198: */
199: typedef struct trap {
200: int signal; /* signal number */
201: char *name; /* short name */
202: char *mess; /* descriptive name */
203: char *trap; /* trap command */
204: short Volatile set; /* trap pending */
205: char ourtrap; /* not ignored (?) */
206: char sig_dfl; /* originally SIG_DFL */
207: } Trap;
208:
209: #define SIGNALS 32
210:
211: Extern int Volatile trap; /* traps pending? */
212: extern Trap sigtraps[SIGNALS];
213: Trap *gettrap ARGS((char *)); /* search for struct trap by number or name */
214: void trapsig ARGS((int sig)); /* trap signal handler */
215:
216: /*
217: * fast character classes
218: */
219: #define C_ALPHA 0x01 /* a-z_A-Z */
220: #define C_DIGIT 0x02 /* 0-9 */
221: #define C_LEX1 0x04 /* \0 \t\n|&;<>() */
222: #define C_VAR1 0x08 /* *@#!$-? */
223: #define C_SUBOP 0x40 /* "=-+?#%" */
224: #define C_IFS 0x80 /* $IFS */
225:
226: extern char ctypes [];
227: void initctypes ARGS((void));
228: void setctypes ARGS((Const char*, int type));
229:
230: #define ctype(c, t) !!(ctypes[(unsigned char)(c)]&(t))
231: #define letter(c) ctype(c, C_ALPHA)
232: #define digit(c) ctype(c, C_DIGIT)
233: #define letnum(c) ctype(c, C_ALPHA|C_DIGIT)
234:
235: #ifndef _IOFBF
236: #define _IOFBF 000000
237: #endif
238:
239: /*
240: * Compatibility constants and declarations
241: */
242: #if COHERENT
243: #define tms tbuffer /* for calls to times() */
244: #define tms_utime tb_utime
245: #define tms_stime tb_stime
246: #define tms_cutime tb_cutime
247: #define tms_cstime tb_cstime
248: #define setvbuf(fp,buf,type,size) /* ignore for now */
249: typedef long clock_t;
250: #define remove(fn) unlink(fn)
251: #endif
252:
253: #ifndef MAILINTERVAL
254: #define MAILINTERVAL 60 /* interval between MAIL checks */
255: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.