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