csh and tcsh work

Sources of csh/sh.misc.c

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.h"
  3. 3
  4. 4
    /*
  5. 5
     * C Shell
  6. 6
     */
  7. 7
  8. 8
    letter(c)
  9. 9
    	register char c;
  10. 10
    {
  11. 11
  12. 12
    	return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '_');
  13. 13
    }
  14. 14
  15. 15
    digit(c)
  16. 16
    	register char c;
  17. 17
    {
  18. 18
  19. 19
    	return (c >= '0' && c <= '9');
  20. 20
    }
  21. 21
  22. 22
    any(c, s)
  23. 23
    	register int c;
  24. 24
    	register char *s;
  25. 25
    {
  26. 26
  27. 27
    	while (*s)
  28. 28
    		if (*s++ == c)
  29. 29
    			return(1);
  30. 30
    	return(0);
  31. 31
    }
  32. 32
  33. 33
    char *
  34. 34
    calloc(i, j)
  35. 35
    	register int i;
  36. 36
    	int j;
  37. 37
    {
  38. 38
    	register char *cp, *dp;
  39. 39
  40. 40
    	i *= j;
  41. 41
    	cp = (char *) malloc(i);
  42. 42
    	if (cp == 0) {
  43. 43
    		child++;
  44. 44
    		error("Out of memory");
  45. 45
    	}
  46. 46
    	dp = cp;
  47. 47
    	if (i > 0)
  48. 48
    		do
  49. 49
    			*dp++ = 0;
  50. 50
    		while (--i);
  51. 51
    	return (cp);
  52. 52
    }
  53. 53
  54. 54
    cfree(p)
  55. 55
    	char *p;
  56. 56
    {
  57. 57
  58. 58
    	free(p);
  59. 59
    }
  60. 60
  61. 61
    char **
  62. 62
    blkend(up)
  63. 63
    	register char **up;
  64. 64
    {
  65. 65
  66. 66
    	while (*up)
  67. 67
    		up++;
  68. 68
    	return (up);
  69. 69
    }
  70. 70
  71. 71
    blkpr(av)
  72. 72
    	register int *av;
  73. 73
    {
  74. 74
  75. 75
    	for (; *av; av++) {
  76. 76
    		printf("%s", *av);
  77. 77
    		if (av[1])
  78. 78
    			printf(" ");
  79. 79
    	}
  80. 80
    }
  81. 81
  82. 82
    blklen(av)
  83. 83
    	register char **av;
  84. 84
    {
  85. 85
    	register int i = 0;
  86. 86
  87. 87
    	while (*av++)
  88. 88
    		i++;
  89. 89
    	return (i);
  90. 90
    }
  91. 91
  92. 92
    char **
  93. 93
    blkcpy(oav, bv)
  94. 94
    	char **oav;
  95. 95
    	register char **bv;
  96. 96
    {
  97. 97
    	register char **av = oav;
  98. 98
  99. 99
    	while (*av++ = *bv++)
  100. 100
    		continue;
  101. 101
    	return (oav);
  102. 102
    }
  103. 103
  104. 104
    char **
  105. 105
    blkcat(up, vp)
  106. 106
    	char **up, **vp;
  107. 107
    {
  108. 108
  109. 109
    	blkcpy(blkend(up), vp);
  110. 110
    	return (up);
  111. 111
    }
  112. 112
  113. 113
    blkfree(av0)
  114. 114
    	char **av0;
  115. 115
    {
  116. 116
    	register char **av = av0;
  117. 117
  118. 118
    	while (*av)
  119. 119
    		xfree(*av++);
  120. 120
    	xfree(av0);
  121. 121
    }
  122. 122
  123. 123
    char **
  124. 124
    saveblk(v)
  125. 125
    	register char **v;
  126. 126
    {
  127. 127
    	register int len = blklen(v) + 1;
  128. 128
    	register char **newv = (char **) calloc(len, sizeof (char **));
  129. 129
    	char **onewv = newv;
  130. 130
  131. 131
    	while (*v)
  132. 132
    		*newv++ = savestr(*v++);
  133. 133
    	return (onewv);
  134. 134
    }
  135. 135
  136. 136
    char *
  137. 137
    strspl(cp, dp)
  138. 138
    	register char *cp, *dp;
  139. 139
    {
  140. 140
    	register char *ep = calloc(1, strlen(cp) + strlen(dp) + 1);
  141. 141
  142. 142
    	strcpy(ep, cp);
  143. 143
    	return (strcat(ep, dp));
  144. 144
    }
  145. 145
  146. 146
    char **
  147. 147
    blkspl(up, vp)
  148. 148
    	register char **up, **vp;
  149. 149
    {
  150. 150
    	register char **wp = (char **) calloc(blklen(up) + blklen(vp) + 1, sizeof (char **));
  151. 151
  152. 152
    	blkcpy(wp, up);
  153. 153
    	return (blkcat(wp, vp));
  154. 154
    }
  155. 155
  156. 156
    lastchr(cp)
  157. 157
    	register char *cp;
  158. 158
    {
  159. 159
  160. 160
    	if (!*cp)
  161. 161
    		return (0);
  162. 162
    	while (cp[1])
  163. 163
    		cp++;
  164. 164
    	return (*cp);
  165. 165
    }
  166. 166
  167. 167
    /*
  168. 168
     * This routine is called after an error to close up
  169. 169
     * any units which may have been left open accidentally.
  170. 170
     */
  171. 171
    closem()
  172. 172
    {
  173. 173
    	register int f;
  174. 174
  175. 175
    	for (f = 0; f < NOFILE; f++)
  176. 176
    		if (f != SHIN && f != SHOUT && f != SHDIAG && f != OLDSTD)
  177. 177
    			close(f);
  178. 178
    }
  179. 179
  180. 180
    /*
  181. 181
     * Close files before executing a file.
  182. 182
     * We could be MUCH more intelligent, since (on a version 7 system)
  183. 183
     * we need only close files here during a source, the other
  184. 184
     * shell fd's being in units 16-19 which are closed automatically!
  185. 185
     */
  186. 186
    closech()
  187. 187
    {
  188. 188
    	register int f;
  189. 189
  190. 190
    	if (didcch)
  191. 191
    		return;
  192. 192
    	didcch = 1;
  193. 193
    	SHIN = 0; SHOUT = 1; SHDIAG = 2; OLDSTD = 0;
  194. 194
    	for (f = 3; f < NOFILE; f++)
  195. 195
    		close(f);
  196. 196
    }
  197. 197
  198. 198
    donefds()
  199. 199
    {
  200. 200
  201. 201
    	close(0), close(1), close(2);
  202. 202
    	didfds = 0;
  203. 203
    }
  204. 204
  205. 205
    /*
  206. 206
     * Move descriptor i to j.
  207. 207
     * If j is -1 then we just want to get i to a safe place,
  208. 208
     * i.e. to a unit > 2.  This also happens in dcopy.
  209. 209
     */
  210. 210
    dmove(i, j)
  211. 211
    	register int i, j;
  212. 212
    {
  213. 213
  214. 214
    	if (i == j || i < 0)
  215. 215
    		return (i);
  216. 216
    	j = dcopy(i, j);
  217. 217
    	if (j != i)
  218. 218
    		close(i);
  219. 219
    	return (j);
  220. 220
    }
  221. 221
  222. 222
    dcopy(i, j)
  223. 223
    	register int i, j;
  224. 224
    {
  225. 225
  226. 226
    	if (i == j || i < 0 || j < 0 && i > 2)
  227. 227
    		return (i);
  228. 228
    	close(j);
  229. 229
    	return (renum(i, j));
  230. 230
    }
  231. 231
  232. 232
    renum(i, j)
  233. 233
    	register int i, j;
  234. 234
    {
  235. 235
    	register int k = dup(i);
  236. 236
  237. 237
    	if (k < 0)
  238. 238
    		return (-1);
  239. 239
    	if (j == -1 && k > 2)
  240. 240
    		return (k);
  241. 241
    	if (k != j) {
  242. 242
    		j = renum(k, j);
  243. 243
    		close(k);
  244. 244
    		return (j);
  245. 245
    	}
  246. 246
    	return (k);
  247. 247
    }
  248. 248
  249. 249
    copy(to, from, size)
  250. 250
    	register char *to, *from;
  251. 251
    	register int size;
  252. 252
    {
  253. 253
  254. 254
    	if (size)
  255. 255
    		do
  256. 256
    			*to++ = *from++;
  257. 257
    		while (--size != 0);
  258. 258
    }
  259. 259
  260. 260
    /*
  261. 261
     * Left shift a command argument list, discarding
  262. 262
     * the first c arguments.  Used in "shift" commands
  263. 263
     * as well as by commands like "repeat".
  264. 264
     */
  265. 265
    lshift(v, c)
  266. 266
    	register char **v;
  267. 267
    	register int c;
  268. 268
    {
  269. 269
    	register char **u = v;
  270. 270
  271. 271
    	while (*u && --c >= 0)
  272. 272
    		xfree(*u++);
  273. 273
    	blkcpy(v, u);
  274. 274
    }
  275. 275
  276. 276
    number(cp)
  277. 277
    	char *cp;
  278. 278
    {
  279. 279
  280. 280
    	if (*cp == '-') {
  281. 281
    		cp++;
  282. 282
    		if (!digit(*cp++))
  283. 283
    			return (0);
  284. 284
    	}
  285. 285
    	while (*cp && digit(*cp))
  286. 286
    		cp++;
  287. 287
    	return (*cp == 0);
  288. 288
    }
  289. 289
  290. 290
    char **
  291. 291
    copyblk(v)
  292. 292
    	register char **v;
  293. 293
    {
  294. 294
    	register char **nv = (char **) calloc(blklen(v) + 1, sizeof (char **));
  295. 295
  296. 296
    	return (blkcpy(nv, v));
  297. 297
    }
  298. 298
  299. 299
    char *
  300. 300
    strend(cp)
  301. 301
    	register char *cp;
  302. 302
    {
  303. 303
  304. 304
    	while (*cp)
  305. 305
    		cp++;
  306. 306
    	return (cp);
  307. 307
    }
  308. 308
  309. 309
    char *
  310. 310
    strip(cp)
  311. 311
    	char *cp;
  312. 312
    {
  313. 313
    	register char *dp = cp;
  314. 314
  315. 315
    	while (*dp++ &= TRIM)
  316. 316
    		continue;
  317. 317
    	return (cp);
  318. 318
    }
  319. 319
  320. 320
    udvar(name)
  321. 321
    	char *name;
  322. 322
    {
  323. 323
  324. 324
    	setname(name);
  325. 325
    	bferr("Undefined variable");
  326. 326
    }

To the top of this page