csh and tcsh work

Sources of csh/sh.h

csh と tcsh のソース

Bill Joy

First published: October, 1978.

Copyright © 1979 Regents of the University of California.

  1. 1
    /* Copyright (c) 1979 Regents of the University of California */
  2. 2
    #include "sh.local.h"
  3. 3
    /*
  4. 4
     * C shell
  5. 5
     *
  6. 6
     * Bill Joy, UC Berkeley
  7. 7
     * October, 1978
  8. 8
     */
  9. 9
    #include <sys/types.h>
  10. 10
    #include <sys/stat.h>
  11. 11
    #include <sys/dir.h>
  12. 12
  13. 13
    #define	isdir(d)	((d.st_mode & S_IFMT) == S_IFDIR)
  14. 14
  15. 15
    #include <errno.h>
  16. 16
    #include <setjmp.h>
  17. 17
    #include <signal.h>
  18. 18
  19. 19
    typedef	char	bool;
  20. 20
  21. 21
    #define	eq(a, b)	(strcmp(a, b) == 0)
  22. 22
  23. 23
    /*
  24. 24
     * Global flags
  25. 25
     */
  26. 26
    bool	didcch;			/* Have closed unused fd's for child */
  27. 27
    bool	didfds;			/* Have setup i/o fd's for child */
  28. 28
    bool	doneinp;		/* EOF indicator after reset from readc */
  29. 29
    bool	exiterr;		/* Exit if error or non-zero exit status */
  30. 30
    bool	child;			/* Child shell ... errors cause exit */
  31. 31
    bool	haderr;			/* Reset was because of an error */
  32. 32
    bool	intty;			/* Input is a tty */
  33. 33
    bool	intact;			/* We are interactive... therefore prompt */
  34. 34
    bool	justpr;			/* Just print because of :p hist mod */
  35. 35
    bool	loginsh;		/* We are a loginsh -> .login/.logout */
  36. 36
    bool	noexec;			/* Don't execute, just syntax check */
  37. 37
    bool	setintr;		/* Set interrupts on/off -> Wait intr... */
  38. 38
    bool	timflg;			/* Time the next waited for command */
  39. 39
  40. 40
    /*
  41. 41
     * Global i/o info
  42. 42
     */
  43. 43
    char	*arginp;		/* Argument input for sh -c and internal `xx` */
  44. 44
    int	onelflg;		/* 2 -> need line for -t, 1 -> exit on read */
  45. 45
    char	*file;			/* Name of shell file for $0 */
  46. 46
  47. 47
    char	*err;			/* Error message from scanner/parser */
  48. 48
    int	errno;			/* Error from C library routines */
  49. 49
    char	*shtemp;		/* Temp name for << shell files in /tmp */
  50. 50
    time_t	time0;			/* Time at which the shell started */
  51. 51
  52. 52
    /*
  53. 53
     * Miscellany
  54. 54
     */
  55. 55
    char	*doldol;		/* Character pid for $$ */
  56. 56
    int	uid;			/* Invokers uid */
  57. 57
    time_t	chktim;			/* Time mail last checked */
  58. 58
  59. 59
    /*
  60. 60
     * These are declared here because they want to be
  61. 61
     * initialized in sh.init.c (to allow them to be made readonly)
  62. 62
     */
  63. 63
    char	*mesg[];
  64. 64
  65. 65
    struct	biltins {
  66. 66
    	char	*bname;
  67. 67
    	int	(*bfunct)();
  68. 68
    	short	minargs, maxargs;
  69. 69
    } bfunc[];
  70. 70
  71. 71
    struct srch {
  72. 72
    	char	*s_name;
  73. 73
    	short	s_value;
  74. 74
    } srchn[];
  75. 75
  76. 76
    /*
  77. 77
     * To be able to redirect i/o for builtins easily, the shell moves the i/o
  78. 78
     * descriptors it uses away from 0,1,2.
  79. 79
     * Ideally these should be in units which are closed across exec's
  80. 80
     * (this saves work) but for version 6, this is not usually possible.
  81. 81
     * The desired initial values for these descriptors are defined in
  82. 82
     * sh.local.h.
  83. 83
     */
  84. 84
    short	SHIN;			/* Current shell input (script) */
  85. 85
    short	SHOUT;			/* Shell output */
  86. 86
    short	SHDIAG;			/* Diagnostic output... shell errs go here */
  87. 87
    short	OLDSTD;			/* Old standard input (def for cmds) */
  88. 88
  89. 89
    /*
  90. 90
     * Error control
  91. 91
     *
  92. 92
     * Errors in scanning and parsing set up an error message to be printed
  93. 93
     * at the end and complete.  Other errors always cause a reset.
  94. 94
     * Because of source commands and .cshrc we need nested error catches.
  95. 95
     */
  96. 96
  97. 97
    jmp_buf	reslab;
  98. 98
  99. 99
    #define	setexit()	setjmp(reslab)
  100. 100
    #define	reset()		longjmp(reslab)
  101. 101
    	/* Should use structure assignment here */
  102. 102
    #define	getexit(a)	copy(a, reslab, sizeof reslab)
  103. 103
    #define	resexit(a)	copy(reslab, a, sizeof reslab)
  104. 104
  105. 105
    char	*gointr;		/* Label for an onintr transfer */
  106. 106
    int	(*parintr)();		/* Parents interrupt catch */
  107. 107
    int	(*parterm)();		/* Parents terminate catch */
  108. 108
  109. 109
    /*
  110. 110
     * Lexical definitions.
  111. 111
     *
  112. 112
     * All lexical space is allocated dynamically.
  113. 113
     * The eighth bit of characters is used to prevent recognition,
  114. 114
     * and eventually stripped.
  115. 115
     */
  116. 116
    #define QUOTE 	0200		/* Eighth char bit used internally for 'ing */
  117. 117
    #define	TRIM	0177		/* Mask to strip quote bit */
  118. 118
  119. 119
    /*
  120. 120
     * Each level of input has a buffered input structure.
  121. 121
     * There are one or more blocks of buffered input for each level,
  122. 122
     * exactly one if the input is seekable and tell is available.
  123. 123
     * In other cases, the shell buffers enough blocks to keep all loops
  124. 124
     * in the buffer.
  125. 125
     */
  126. 126
    struct	Bin {
  127. 127
    	off_t	Bfseekp;		/* Seek pointer */
  128. 128
    	off_t	Bfbobp;			/* Seekp of beginning of buffers */
  129. 129
    	off_t	Bfeobp;			/* Seekp of end of buffers */
  130. 130
    	short	Bfblocks;		/* Number of buffer blocks */
  131. 131
    	char	**Bfbuf;		/* The array of buffer blocks */
  132. 132
    } B;
  133. 133
  134. 134
    #define	fseekp	B.Bfseekp
  135. 135
    #define	fbobp	B.Bfbobp
  136. 136
    #define	feobp	B.Bfeobp
  137. 137
    #define	fblocks	B.Bfblocks
  138. 138
    #define	fbuf	B.Bfbuf
  139. 139
  140. 140
    off_t	btell();
  141. 141
  142. 142
    /*
  143. 143
     * The shell finds commands in loops by reseeking the input
  144. 144
     * For whiles, in particular, it reseeks to the beginning of the
  145. 145
     * line the while was on; hence the while placement restrictions.
  146. 146
     */
  147. 147
    off_t	lineloc;
  148. 148
  149. 149
    #ifdef	TELL
  150. 150
    off_t	tell();
  151. 151
    bool	cantell;			/* Is current source tellable ? */
  152. 152
    #endif
  153. 153
  154. 154
    /*
  155. 155
     * Input lines are parsed into doubly linked circular
  156. 156
     * lists of words of the following form.
  157. 157
     */
  158. 158
    struct	wordent {
  159. 159
    	char	*word;
  160. 160
    	struct	wordent *prev;
  161. 161
    	struct	wordent *next;
  162. 162
    };
  163. 163
  164. 164
    /*
  165. 165
     * During word building, both in the initial lexical phase and
  166. 166
     * when expanding $ variable substitutions, expansion by `!' and `$'
  167. 167
     * must be inhibited when reading ahead in routines which are themselves
  168. 168
     * processing `!' and `$' expansion or after characters such as `\' or in
  169. 169
     * quotations.  The following flags are passed to the getC routines
  170. 170
     * telling them which of these substitutions are appropriate for the
  171. 171
     * next character to be returned.
  172. 172
     */
  173. 173
    #define	DODOL	1
  174. 174
    #define	DOEXCL	2
  175. 175
    #define	DOALL	DODOL|DOEXCL
  176. 176
  177. 177
    /*
  178. 178
     * Labuf implements a general buffer for lookahead during lexical operations.
  179. 179
     * Text which is to be placed in the input stream can be stuck here.
  180. 180
     * We stick parsed ahead $ constructs during initial input,
  181. 181
     * process id's from `$$', and modified variable values (from qualifiers
  182. 182
     * during expansion in sh.dol.c) here.
  183. 183
     */
  184. 184
    char	labuf[256];
  185. 185
    char	*lap;
  186. 186
  187. 187
    /*
  188. 188
     * Parser structure
  189. 189
     *
  190. 190
     * Each command is parsed to a tree of command structures and
  191. 191
     * flags are set bottom up during this process, to be propagated down
  192. 192
     * as needed during the semantics/exeuction pass (sh.sem.c).
  193. 193
     */
  194. 194
    struct	command {
  195. 195
    	short	t_dtyp;				/* Type of node */
  196. 196
    	short	t_dflg;				/* Flags, e.g. FAND|... */
  197. 197
    	union {
  198. 198
    		char	*T_dlef;		/* Input redirect word */
  199. 199
    		struct	command *T_dcar;	/* Left part of list/pipe */
  200. 200
    	} L;
  201. 201
    	union {
  202. 202
    		char	*T_drit;		/* Output redirect word */
  203. 203
    		struct	command *T_dcdr;	/* Right part of list/pipe */
  204. 204
    	} R;
  205. 205
    #define	t_dlef	L.T_dlef
  206. 206
    #define	t_dcar	L.T_dcar
  207. 207
    #define	t_drit	R.T_drit
  208. 208
    #define	t_dcdr	R.T_dcdr
  209. 209
    	char	**t_dcom;			/* Command/argument vector */
  210. 210
    	struct	command *t_dspr;		/* Pointer to ()'d subtree */
  211. 211
    };
  212. 212
  213. 213
    #define	TCOM	1		/* t_dcom <t_dlef >t_drit	*/
  214. 214
    #define	TPAR	2		/* ( t_dspr ) <t_dlef >t_drit	*/
  215. 215
    #define	TFIL	3		/* t_dlef | t_drit		*/
  216. 216
    #define	TLST	4		/* t_dlef ; t_drit		*/
  217. 217
    #define	TOR	5		/* t_dlef || t_drit		*/
  218. 218
    #define	TAND	6		/* t_dlef && t_drit		*/
  219. 219
  220. 220
    #define FAND	(1<<0)		/* executes in background	*/
  221. 221
    #define FCAT	(1<<1)		/* output is redirected >>	*/
  222. 222
    #define FPIN	(1<<2)		/* input is a pipe		*/
  223. 223
    #define FPOU	(1<<3)		/* output is a pipe		*/
  224. 224
    #define FPAR	(1<<4)		/* don't fork, last ()ized cmd	*/
  225. 225
    #define FINT	(1<<5)		/* don't make interruptible	*/
  226. 226
    #define FPRS	(1<<6)		/* print number when forked	*/
  227. 227
    #define FDIAG	(1<<7)		/* redirect unit 2 with unit 1	*/
  228. 228
    #define	FANY	(1<<8)		/* output was !			*/
  229. 229
    #define	FHERE	(1<<9)		/* input redirection is <<	*/
  230. 230
    #define	FREDO	(1<<10)		/* reexec aft if, repeat,...	*/
  231. 231
  232. 232
    /*
  233. 233
     * The keywords for the parser
  234. 234
     */
  235. 235
    #define	ZBREAK		0
  236. 236
    #define	ZBRKSW		1
  237. 237
    #define	ZCASE		2
  238. 238
    #define	ZDEFAULT 	3
  239. 239
    #define	ZELSE		4
  240. 240
    #define	ZEND		5
  241. 241
    #define	ZENDIF		6
  242. 242
    #define	ZENDSW		7
  243. 243
    #define	ZEXIT		8
  244. 244
    #define	ZFOREACH	9
  245. 245
    #define	ZGOTO		10
  246. 246
    #define	ZIF		11
  247. 247
    #define	ZLABEL		12
  248. 248
    #define	ZLET		13
  249. 249
    #define	ZSET		14
  250. 250
    #define	ZSWITCH		15
  251. 251
    #define	ZTEST		16
  252. 252
    #define	ZTHEN		17
  253. 253
    #define	ZWHILE		18
  254. 254
  255. 255
    /*
  256. 256
     * Structure defining the existing while/foreach loops at this
  257. 257
     * source level.  Loops are implemented by seeking back in the
  258. 258
     * input.  For foreach (fe), the word list is attached here.
  259. 259
     */
  260. 260
    struct	whyle {
  261. 261
    	off_t	w_start;		/* Point to restart loop */
  262. 262
    	off_t	w_end;			/* End of loop (0 if unknown) */
  263. 263
    	char	**w_fe, **w_fe0;	/* Current/initial wordlist for fe */
  264. 264
    	char	*w_fename;		/* Name for fe */
  265. 265
    	struct	whyle *w_next;		/* Next (more outer) loop */
  266. 266
    } *whyles;
  267. 267
  268. 268
    /*
  269. 269
     * Variable structure
  270. 270
     *
  271. 271
     * Lists of aliases and variables are sorted alphabetically by name
  272. 272
     */
  273. 273
    struct	varent {
  274. 274
    	char	**vec;		/* Array of words which is the value */
  275. 275
    	char	*name;		/* Name of variable/alias */
  276. 276
    	struct	varent *link;
  277. 277
    } shvhed, aliases;
  278. 278
  279. 279
    /*
  280. 280
     * The following are for interfacing redo substitution in
  281. 281
     * aliases to the lexical routines.
  282. 282
     */
  283. 283
    struct	wordent *alhistp;		/* Argument list (first) */
  284. 284
    struct	wordent *alhistt;		/* Node after last in arg list */
  285. 285
    char	**alvec;			/* The (remnants of) alias vector */
  286. 286
  287. 287
    /*
  288. 288
     * Filename/command name expansion variables
  289. 289
     */
  290. 290
    short	gflag;				/* After tglob -> is globbing needed? */
  291. 291
  292. 292
    /*
  293. 293
     * A reasonable limit on number of arguments would seem to be
  294. 294
     * the maximum number of characters in an arg list / 6.
  295. 295
     */
  296. 296
    #define	GAVSIZ	NCARGS / 6
  297. 297
  298. 298
    /*
  299. 299
     * Variables for filename expansion
  300. 300
     */
  301. 301
    char	**gargv;			/* Pointer to the (stack) arglist */
  302. 302
    short	gargc;				/* Number args in gargv */
  303. 303
    short	gnleft;
  304. 304
  305. 305
    /*
  306. 306
     * Variables for command expansion.
  307. 307
     */
  308. 308
    char	**pargv;			/* Pointer to the argv list space */
  309. 309
    char	*pargs;				/* Pointer to start current word */
  310. 310
    short	pargc;				/* Count of arguments in pargv */
  311. 311
    short	pnleft;				/* Number of chars left in pargs */
  312. 312
    char	*pargcp;			/* Current index into pargs */
  313. 313
  314. 314
    /*
  315. 315
     * History list
  316. 316
     *
  317. 317
     * Each history list entry contains an embedded wordlist
  318. 318
     * from the scanner, a number for the event, and a reference count
  319. 319
     * to aid in discarding old entries.
  320. 320
     *
  321. 321
     * Essentially "invisible" entries are put on the history list
  322. 322
     * when history substitution includes modifiers, and thrown away
  323. 323
     * at the next discarding since their event numbers are very negative.
  324. 324
     */
  325. 325
    struct	Hist {
  326. 326
    	struct	wordent Hlex;
  327. 327
    	int	Hnum;
  328. 328
    	int	Href;
  329. 329
    	struct	Hist *Hnext;
  330. 330
    } Histlist;
  331. 331
  332. 332
    int	eventno;			/* Next events number */
  333. 333
    int	lastev;				/* Last event reference (default) */
  334. 334
  335. 335
    char	*Dfix1();
  336. 336
    struct	varent *adrof(), *adrof1();
  337. 337
    char	**blkcat();
  338. 338
    char	**blkcpy();
  339. 339
    char	**blkend();
  340. 340
    char	**blkspl();
  341. 341
    char	*calloc();
  342. 342
    char	*cname();
  343. 343
    char	**copyblk();
  344. 344
    char	**dobackp();
  345. 345
    char	*domod();
  346. 346
    struct	wordent *dosub();
  347. 347
    char	*exp3();
  348. 348
    char	*exp3a();
  349. 349
    char	*exp4();
  350. 350
    char	*exp5();
  351. 351
    char	*exp6();
  352. 352
    struct	Hist *enthist();
  353. 353
    struct	Hist *findev();
  354. 354
    struct	wordent *freenod();
  355. 355
    char	*getenv();
  356. 356
    char	*getinx();
  357. 357
    struct	varent *getvx();
  358. 358
    struct	passwd *getpwnam();
  359. 359
    struct	wordent *gethent();
  360. 360
    struct	wordent *getsub();
  361. 361
    char	*globone();
  362. 362
    struct	biltins *isbfunc();
  363. 363
    char	**glob();
  364. 364
    char	*operate();
  365. 365
    int	pintr();
  366. 366
    char	*putn();
  367. 367
    char	**saveblk();
  368. 368
    char	*savestr();
  369. 369
    char	*strcat();
  370. 370
    char	*strcpy();
  371. 371
    char	*strend();
  372. 372
    char	*strings();
  373. 373
    char	*strip();
  374. 374
    char	*strspl();
  375. 375
    char	*subword();
  376. 376
    struct	command *syntax();
  377. 377
    struct	command *syn0();
  378. 378
    struct	command *syn1();
  379. 379
    struct	command *syn1a();
  380. 380
    struct	command *syn1b();
  381. 381
    struct	command *syn2();
  382. 382
    struct	command *syn3();
  383. 383
    int	tglob();
  384. 384
    int	trim();
  385. 385
    char	*value(), *value1();
  386. 386
    char	*xhome();
  387. 387
    char	*xname();
  388. 388
    char	*xset();
  389. 389
  390. 390
    #define	NOSTR	((char *) 0)
  391. 391
  392. 392
    /*
  393. 393
     * setname is a macro to save space (see sh.err.c)
  394. 394
     */
  395. 395
    char	*bname;
  396. 396
    #define	setname(a)	bname = (a);

To the top of this page