xref: /netbsd-src/bin/csh/misc.c (revision 1f2744e6e4915c9da2a3f980279398c4cf7d5e6d)
1 /*	$NetBSD: misc.c,v 1.6 1995/03/21 09:03:09 cgd Exp $	*/
2 
3 /*-
4  * Copyright (c) 1980, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)misc.c	8.1 (Berkeley) 5/31/93";
39 #else
40 static char rcsid[] = "$NetBSD: misc.c,v 1.6 1995/03/21 09:03:09 cgd Exp $";
41 #endif
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #if __STDC__
48 # include <stdarg.h>
49 #else
50 # include <varargs.h>
51 #endif
52 
53 #include "csh.h"
54 #include "extern.h"
55 
56 static int	renum __P((int, int));
57 
58 int
59 any(s, c)
60     register char *s;
61     register int c;
62 {
63     if (!s)
64 	return (0);		/* Check for nil pointer */
65     while (*s)
66 	if (*s++ == c)
67 	    return (1);
68     return (0);
69 }
70 
71 void
72 setzero(cp, i)
73     char   *cp;
74     int     i;
75 {
76     if (i != 0)
77 	do
78 	    *cp++ = 0;
79 	while (--i);
80 }
81 
82 char   *
83 strsave(s)
84     register char *s;
85 {
86     char   *n;
87     register char *p;
88 
89     if (s == NULL)
90 	s = "";
91     for (p = s; *p++;)
92 	continue;
93     n = p = (char *) xmalloc((size_t) ((p - s) * sizeof(char)));
94     while ((*p++ = *s++) != '\0')
95 	continue;
96     return (n);
97 }
98 
99 Char  **
100 blkend(up)
101     register Char **up;
102 {
103 
104     while (*up)
105 	up++;
106     return (up);
107 }
108 
109 
110 void
111 blkpr(fp, av)
112     FILE *fp;
113     register Char **av;
114 {
115 
116     for (; *av; av++) {
117 	(void) fprintf(fp, "%s", vis_str(*av));
118 	if (av[1])
119 	    (void) fprintf(fp, " ");
120     }
121 }
122 
123 int
124 blklen(av)
125     register Char **av;
126 {
127     register int i = 0;
128 
129     while (*av++)
130 	i++;
131     return (i);
132 }
133 
134 Char  **
135 blkcpy(oav, bv)
136     Char  **oav;
137     register Char **bv;
138 {
139     register Char **av = oav;
140 
141     while ((*av++ = *bv++) != NULL)
142 	continue;
143     return (oav);
144 }
145 
146 Char  **
147 blkcat(up, vp)
148     Char  **up, **vp;
149 {
150 
151     (void) blkcpy(blkend(up), vp);
152     return (up);
153 }
154 
155 void
156 blkfree(av0)
157     Char  **av0;
158 {
159     register Char **av = av0;
160 
161     if (!av0)
162 	return;
163     for (; *av; av++)
164 	xfree((ptr_t) * av);
165     xfree((ptr_t) av0);
166 }
167 
168 Char  **
169 saveblk(v)
170     register Char **v;
171 {
172     register Char **newv =
173     (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
174     Char  **onewv = newv;
175 
176     while (*v)
177 	*newv++ = Strsave(*v++);
178     return (onewv);
179 }
180 
181 #ifdef NOTUSED
182 char   *
183 strstr(s, t)
184     register char *s, *t;
185 {
186     do {
187 	register char *ss = s;
188 	register char *tt = t;
189 
190 	do
191 	    if (*tt == '\0')
192 		return (s);
193 	while (*ss++ == *tt++);
194     } while (*s++ != '\0');
195     return (NULL);
196 }
197 
198 #endif /* NOTUSED */
199 
200 #ifndef SHORT_STRINGS
201 char   *
202 strspl(cp, dp)
203     char   *cp, *dp;
204 {
205     char   *ep;
206     register char *p, *q;
207 
208     if (!cp)
209 	cp = "";
210     if (!dp)
211 	dp = "";
212     for (p = cp; *p++;)
213 	continue;
214     for (q = dp; *q++;)
215 	continue;
216     ep = (char *) xmalloc((size_t) (((p - cp) + (q - dp) - 1) * sizeof(char)));
217     for (p = ep, q = cp; *p++ = *q++;)
218 	continue;
219     for (p--, q = dp; *p++ = *q++;)
220 	continue;
221     return (ep);
222 }
223 
224 #endif
225 
226 Char  **
227 blkspl(up, vp)
228     register Char **up, **vp;
229 {
230     register Char **wp =
231     (Char **) xcalloc((size_t) (blklen(up) + blklen(vp) + 1),
232 		      sizeof(Char **));
233 
234     (void) blkcpy(wp, up);
235     return (blkcat(wp, vp));
236 }
237 
238 Char
239 lastchr(cp)
240     register Char *cp;
241 {
242 
243     if (!cp)
244 	return (0);
245     if (!*cp)
246 	return (0);
247     while (cp[1])
248 	cp++;
249     return (*cp);
250 }
251 
252 /*
253  * This routine is called after an error to close up
254  * any units which may have been left open accidentally.
255  */
256 void
257 closem()
258 {
259     register int f;
260 
261     for (f = 0; f < NOFILE; f++)
262 	if (f != SHIN && f != SHOUT && f != SHERR && f != OLDSTD &&
263 	    f != FSHTTY)
264 	    (void) close(f);
265 }
266 
267 void
268 donefds()
269 {
270     (void) close(0);
271     (void) close(1);
272     (void) close(2);
273 
274     didfds = 0;
275 }
276 
277 /*
278  * Move descriptor i to j.
279  * If j is -1 then we just want to get i to a safe place,
280  * i.e. to a unit > 2.  This also happens in dcopy.
281  */
282 int
283 dmove(i, j)
284     register int i, j;
285 {
286 
287     if (i == j || i < 0)
288 	return (i);
289     if (j >= 0) {
290 	(void) dup2(i, j);
291 	if (j != i)
292 	    (void) close(i);
293 	return (j);
294     }
295     j = dcopy(i, j);
296     if (j != i)
297 	(void) close(i);
298     return (j);
299 }
300 
301 int
302 dcopy(i, j)
303     register int i, j;
304 {
305 
306     if (i == j || i < 0 || (j < 0 && i > 2))
307 	return (i);
308     if (j >= 0) {
309 	(void) dup2(i, j);
310 	return (j);
311     }
312     (void) close(j);
313     return (renum(i, j));
314 }
315 
316 static int
317 renum(i, j)
318     register int i, j;
319 {
320     register int k = dup(i);
321 
322     if (k < 0)
323 	return (-1);
324     if (j == -1 && k > 2)
325 	return (k);
326     if (k != j) {
327 	j = renum(k, j);
328 	(void) close(k);
329 	return (j);
330     }
331     return (k);
332 }
333 
334 /*
335  * Left shift a command argument list, discarding
336  * the first c arguments.  Used in "shift" commands
337  * as well as by commands like "repeat".
338  */
339 void
340 lshift(v, c)
341     register Char **v;
342     register int c;
343 {
344     register Char **u;
345 
346     for (u = v; *u && --c >= 0; u++)
347 	xfree((ptr_t) *u);
348     (void) blkcpy(v, u);
349 }
350 
351 int
352 number(cp)
353     Char   *cp;
354 {
355     if (!cp)
356 	return(0);
357     if (*cp == '-') {
358 	cp++;
359 	if (!Isdigit(*cp))
360 	    return (0);
361 	cp++;
362     }
363     while (*cp && Isdigit(*cp))
364 	cp++;
365     return (*cp == 0);
366 }
367 
368 Char  **
369 copyblk(v)
370     register Char **v;
371 {
372     Char  **nv = (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
373 
374     return (blkcpy(nv, v));
375 }
376 
377 #ifndef SHORT_STRINGS
378 char   *
379 strend(cp)
380     register char *cp;
381 {
382     if (!cp)
383 	return (cp);
384     while (*cp)
385 	cp++;
386     return (cp);
387 }
388 
389 #endif /* SHORT_STRINGS */
390 
391 Char   *
392 strip(cp)
393     Char   *cp;
394 {
395     register Char *dp = cp;
396 
397     if (!cp)
398 	return (cp);
399     while ((*dp++ &= TRIM) != '\0')
400 	continue;
401     return (cp);
402 }
403 
404 void
405 udvar(name)
406     Char   *name;
407 {
408 
409     setname(vis_str(name));
410     stderror(ERR_NAME | ERR_UNDVAR);
411 }
412 
413 int
414 prefix(sub, str)
415     register Char *sub, *str;
416 {
417 
418     for (;;) {
419 	if (*sub == 0)
420 	    return (1);
421 	if (*str == 0)
422 	    return (0);
423 	if (*sub++ != *str++)
424 	    return (0);
425     }
426 }
427