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