| 1 | /*- |
| 2 | * Copyright (c) 1980, 1991 The Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * %sccs.include.redist.c% |
| 6 | */ |
| 7 | |
| 8 | #ifndef lint |
| 9 | static char sccsid[] = "@(#)misc.c 5.9 (Berkeley) %G%"; |
| 10 | #endif /* not lint */ |
| 11 | |
| 12 | #include "csh.h" |
| 13 | #include "extern.h" |
| 14 | |
| 15 | static int renum(); |
| 16 | |
| 17 | int |
| 18 | any(s, c) |
| 19 | register char *s; |
| 20 | register int c; |
| 21 | { |
| 22 | if (!s) |
| 23 | return (0); /* Check for nil pointer */ |
| 24 | while (*s) |
| 25 | if (*s++ == c) |
| 26 | return (1); |
| 27 | return (0); |
| 28 | } |
| 29 | |
| 30 | void |
| 31 | setzero(cp, i) |
| 32 | char *cp; |
| 33 | int i; |
| 34 | { |
| 35 | if (i != 0) |
| 36 | do |
| 37 | *cp++ = 0; |
| 38 | while (--i); |
| 39 | } |
| 40 | |
| 41 | char * |
| 42 | strsave(s) |
| 43 | register char *s; |
| 44 | { |
| 45 | char *n; |
| 46 | register char *p; |
| 47 | |
| 48 | if (s == NULL) |
| 49 | s = ""; |
| 50 | for (p = s; *p++;); |
| 51 | n = p = (char *) xmalloc((size_t) ((p - s) * sizeof(char))); |
| 52 | while (*p++ = *s++); |
| 53 | return (n); |
| 54 | } |
| 55 | |
| 56 | Char ** |
| 57 | blkend(up) |
| 58 | register Char **up; |
| 59 | { |
| 60 | |
| 61 | while (*up) |
| 62 | up++; |
| 63 | return (up); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | void |
| 68 | blkpr(av) |
| 69 | register Char **av; |
| 70 | { |
| 71 | |
| 72 | for (; *av; av++) { |
| 73 | xprintf("%s", short2str(*av)); |
| 74 | if (av[1]) |
| 75 | xprintf(" "); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | int |
| 80 | blklen(av) |
| 81 | register Char **av; |
| 82 | { |
| 83 | register int i = 0; |
| 84 | |
| 85 | while (*av++) |
| 86 | i++; |
| 87 | return (i); |
| 88 | } |
| 89 | |
| 90 | Char ** |
| 91 | blkcpy(oav, bv) |
| 92 | Char **oav; |
| 93 | register Char **bv; |
| 94 | { |
| 95 | register Char **av = oav; |
| 96 | |
| 97 | while (*av++ = *bv++) |
| 98 | continue; |
| 99 | return (oav); |
| 100 | } |
| 101 | |
| 102 | Char ** |
| 103 | blkcat(up, vp) |
| 104 | Char **up, **vp; |
| 105 | { |
| 106 | |
| 107 | (void) blkcpy(blkend(up), vp); |
| 108 | return (up); |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | blkfree(av0) |
| 113 | Char **av0; |
| 114 | { |
| 115 | register Char **av = av0; |
| 116 | |
| 117 | if (!av0) |
| 118 | return; |
| 119 | for (; *av; av++) |
| 120 | xfree((ptr_t) * av); |
| 121 | xfree((ptr_t) av0); |
| 122 | } |
| 123 | |
| 124 | Char ** |
| 125 | saveblk(v) |
| 126 | register Char **v; |
| 127 | { |
| 128 | register Char **newv = |
| 129 | (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **)); |
| 130 | Char **onewv = newv; |
| 131 | |
| 132 | while (*v) |
| 133 | *newv++ = Strsave(*v++); |
| 134 | return (onewv); |
| 135 | } |
| 136 | |
| 137 | #ifdef NOTUSED |
| 138 | char * |
| 139 | strstr(s, t) |
| 140 | register char *s, *t; |
| 141 | { |
| 142 | do { |
| 143 | register char *ss = s; |
| 144 | register char *tt = t; |
| 145 | |
| 146 | do |
| 147 | if (*tt == '\0') |
| 148 | return (s); |
| 149 | while (*ss++ == *tt++); |
| 150 | } while (*s++ != '\0'); |
| 151 | return ((char *) 0); |
| 152 | } |
| 153 | |
| 154 | #endif /* NOTUSED */ |
| 155 | |
| 156 | #ifndef SHORT_STRINGS |
| 157 | char * |
| 158 | strspl(cp, dp) |
| 159 | char *cp, *dp; |
| 160 | { |
| 161 | char *ep; |
| 162 | register char *p, *q; |
| 163 | |
| 164 | if (!cp) |
| 165 | cp = ""; |
| 166 | if (!dp) |
| 167 | dp = ""; |
| 168 | for (p = cp; *p++;); |
| 169 | for (q = dp; *q++;); |
| 170 | ep = (char *) xmalloc((size_t) (((p - cp) + (q - dp) - 1) * sizeof(char))); |
| 171 | for (p = ep, q = cp; *p++ = *q++;); |
| 172 | for (p--, q = dp; *p++ = *q++;); |
| 173 | return (ep); |
| 174 | } |
| 175 | |
| 176 | #endif |
| 177 | |
| 178 | Char ** |
| 179 | blkspl(up, vp) |
| 180 | register Char **up, **vp; |
| 181 | { |
| 182 | register Char **wp = |
| 183 | (Char **) xcalloc((size_t) (blklen(up) + blklen(vp) + 1), |
| 184 | sizeof(Char **)); |
| 185 | |
| 186 | (void) blkcpy(wp, up); |
| 187 | return (blkcat(wp, vp)); |
| 188 | } |
| 189 | |
| 190 | Char |
| 191 | lastchr(cp) |
| 192 | register Char *cp; |
| 193 | { |
| 194 | |
| 195 | if (!cp) |
| 196 | return (0); |
| 197 | if (!*cp) |
| 198 | return (0); |
| 199 | while (cp[1]) |
| 200 | cp++; |
| 201 | return (*cp); |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * This routine is called after an error to close up |
| 206 | * any units which may have been left open accidentally. |
| 207 | */ |
| 208 | void |
| 209 | closem() |
| 210 | { |
| 211 | register int f; |
| 212 | |
| 213 | for (f = 0; f < NOFILE; f++) |
| 214 | if (f != SHIN && f != SHOUT && f != SHDIAG && f != OLDSTD && |
| 215 | f != FSHTTY) |
| 216 | (void) close(f); |
| 217 | } |
| 218 | |
| 219 | void |
| 220 | donefds() |
| 221 | { |
| 222 | |
| 223 | (void) close(0); |
| 224 | (void) close(1); |
| 225 | (void) close(2); |
| 226 | didfds = 0; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Move descriptor i to j. |
| 231 | * If j is -1 then we just want to get i to a safe place, |
| 232 | * i.e. to a unit > 2. This also happens in dcopy. |
| 233 | */ |
| 234 | int |
| 235 | dmove(i, j) |
| 236 | register int i, j; |
| 237 | { |
| 238 | |
| 239 | if (i == j || i < 0) |
| 240 | return (i); |
| 241 | if (j >= 0) { |
| 242 | (void) dup2(i, j); |
| 243 | return (j); |
| 244 | } |
| 245 | j = dcopy(i, j); |
| 246 | if (j != i) |
| 247 | (void) close(i); |
| 248 | return (j); |
| 249 | } |
| 250 | |
| 251 | int |
| 252 | dcopy(i, j) |
| 253 | register int i, j; |
| 254 | { |
| 255 | |
| 256 | if (i == j || i < 0 || j < 0 && i > 2) |
| 257 | return (i); |
| 258 | if (j >= 0) { |
| 259 | (void) dup2(i, j); |
| 260 | return (j); |
| 261 | } |
| 262 | (void) close(j); |
| 263 | return (renum(i, j)); |
| 264 | } |
| 265 | |
| 266 | static int |
| 267 | renum(i, j) |
| 268 | register int i, j; |
| 269 | { |
| 270 | register int k = dup(i); |
| 271 | |
| 272 | if (k < 0) |
| 273 | return (-1); |
| 274 | if (j == -1 && k > 2) |
| 275 | return (k); |
| 276 | if (k != j) { |
| 277 | j = renum(k, j); |
| 278 | (void) close(k); |
| 279 | return (j); |
| 280 | } |
| 281 | return (k); |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * Left shift a command argument list, discarding |
| 286 | * the first c arguments. Used in "shift" commands |
| 287 | * as well as by commands like "repeat". |
| 288 | */ |
| 289 | void |
| 290 | lshift(v, c) |
| 291 | register Char **v; |
| 292 | register int c; |
| 293 | { |
| 294 | register Char **u = v; |
| 295 | |
| 296 | while (*u && --c >= 0) |
| 297 | xfree((ptr_t) * u++); |
| 298 | (void) blkcpy(v, u); |
| 299 | } |
| 300 | |
| 301 | int |
| 302 | number(cp) |
| 303 | Char *cp; |
| 304 | { |
| 305 | if (!cp) |
| 306 | return(0); |
| 307 | if (*cp == '-') { |
| 308 | cp++; |
| 309 | if (!Isdigit(*cp)) |
| 310 | return (0); |
| 311 | cp++; |
| 312 | } |
| 313 | while (*cp && Isdigit(*cp)) |
| 314 | cp++; |
| 315 | return (*cp == 0); |
| 316 | } |
| 317 | |
| 318 | Char ** |
| 319 | copyblk(v) |
| 320 | register Char **v; |
| 321 | { |
| 322 | Char **nv = (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **)); |
| 323 | |
| 324 | return (blkcpy(nv, v)); |
| 325 | } |
| 326 | |
| 327 | #ifndef SHORT_STRINGS |
| 328 | char * |
| 329 | strend(cp) |
| 330 | register char *cp; |
| 331 | { |
| 332 | if (!cp) |
| 333 | return (cp); |
| 334 | while (*cp) |
| 335 | cp++; |
| 336 | return (cp); |
| 337 | } |
| 338 | |
| 339 | #endif /* SHORT_STRINGS */ |
| 340 | |
| 341 | Char * |
| 342 | strip(cp) |
| 343 | Char *cp; |
| 344 | { |
| 345 | register Char *dp = cp; |
| 346 | |
| 347 | if (!cp) |
| 348 | return (cp); |
| 349 | while (*dp++ &= TRIM) |
| 350 | continue; |
| 351 | return (cp); |
| 352 | } |
| 353 | |
| 354 | void |
| 355 | udvar(name) |
| 356 | Char *name; |
| 357 | { |
| 358 | |
| 359 | setname(short2str(name)); |
| 360 | stderror(ERR_NAME | ERR_UNDVAR); |
| 361 | } |
| 362 | |
| 363 | int |
| 364 | prefix(sub, str) |
| 365 | register Char *sub, *str; |
| 366 | { |
| 367 | |
| 368 | for (;;) { |
| 369 | if (*sub == 0) |
| 370 | return (1); |
| 371 | if (*str == 0) |
| 372 | return (0); |
| 373 | if (*sub++ != *str++) |
| 374 | return (0); |
| 375 | } |
| 376 | } |