]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bin/csh/str.c
This commit was generated by cvs2svn to compensate for changes in r57093,
[FreeBSD/FreeBSD.git] / bin / csh / str.c
1 /*-
2  * Copyright (c) 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)str.c       8.1 (Berkeley) 5/31/93";
37 #else
38 static const char rcsid[] =
39   "$FreeBSD$";
40 #endif
41 #endif /* not lint */
42
43 #define MALLOC_INCR     128
44
45 /*
46  * tc.str.c: Short string package
47  *           This has been a lesson of how to write buggy code!
48  */
49
50 #include <sys/types.h>
51 #if __STDC__
52 # include <stdarg.h>
53 #else
54 # include <varargs.h>
55 #endif
56 #include <vis.h>
57
58 #include "csh.h"
59 #include "extern.h"
60
61 #ifdef SHORT_STRINGS
62
63 Char  **
64 blk2short(src)
65     char **src;
66 {
67     size_t     n;
68     Char **sdst, **dst;
69
70     /*
71      * Count
72      */
73     for (n = 0; src[n] != NULL; n++)
74         continue;
75     sdst = dst = (Char **) xmalloc((size_t) ((n + 1) * sizeof(Char *)));
76
77     for (; *src != NULL; src++)
78         *dst++ = SAVE(*src);
79     *dst = NULL;
80     return (sdst);
81 }
82
83 char  **
84 short2blk(src)
85     Char **src;
86 {
87     size_t     n;
88     char **sdst, **dst;
89
90     /*
91      * Count
92      */
93     for (n = 0; src[n] != NULL; n++)
94         continue;
95     sdst = dst = (char **) xmalloc((size_t) ((n + 1) * sizeof(char *)));
96
97     for (; *src != NULL; src++)
98         *dst++ = strsave(short2str(*src));
99     *dst = NULL;
100     return (sdst);
101 }
102
103 Char   *
104 str2short(src)
105     char *src;
106 {
107     static Char *sdst;
108     static size_t dstsize = 0;
109     Char *dst, *edst;
110
111     if (src == NULL)
112         return (NULL);
113
114     if (sdst == (NULL)) {
115         dstsize = MALLOC_INCR;
116         sdst = (Char *) xmalloc((size_t) dstsize * sizeof(Char));
117     }
118
119     dst = sdst;
120     edst = &dst[dstsize];
121     while (*src) {
122         *dst++ = (Char) ((unsigned char) *src++);
123         if (dst == edst) {
124             dstsize += MALLOC_INCR;
125             sdst = (Char *) xrealloc((ptr_t) sdst,
126                                      (size_t) dstsize * sizeof(Char));
127             edst = &sdst[dstsize];
128             dst = &edst[-MALLOC_INCR];
129         }
130     }
131     *dst = 0;
132     return (sdst);
133 }
134
135 char   *
136 short2str(src)
137     Char *src;
138 {
139     static char *sdst = NULL;
140     static size_t dstsize = 0;
141     char *dst, *edst;
142
143     if (src == NULL)
144         return (NULL);
145
146     if (sdst == NULL) {
147         dstsize = MALLOC_INCR;
148         sdst = (char *) xmalloc((size_t) dstsize * sizeof(char));
149     }
150     dst = sdst;
151     edst = &dst[dstsize];
152     while (*src) {
153         *dst++ = (char) *src++;
154         if (dst == edst) {
155             dstsize += MALLOC_INCR;
156             sdst = (char *) xrealloc((ptr_t) sdst,
157                                      (size_t) dstsize * sizeof(char));
158             edst = &sdst[dstsize];
159             dst = &edst[-MALLOC_INCR];
160         }
161     }
162     *dst = 0;
163     return (sdst);
164 }
165
166 Char   *
167 s_strcpy(dst, src)
168     Char *dst, *src;
169 {
170     Char *sdst;
171
172     sdst = dst;
173     while ((*dst++ = *src++) != '\0')
174         continue;
175     return (sdst);
176 }
177
178 Char   *
179 s_strncpy(dst, src, n)
180     Char *dst, *src;
181     size_t n;
182 {
183     Char *sdst;
184
185     if (n == 0)
186         return(dst);
187
188     sdst = dst;
189     do
190         if ((*dst++ = *src++) == '\0') {
191             while (--n != 0)
192                 *dst++ = '\0';
193             return(sdst);
194         }
195     while (--n != 0);
196     return (sdst);
197 }
198
199 Char   *
200 s_strcat(dst, src)
201     Char *dst, *src;
202 {
203     short *sdst;
204
205     sdst = dst;
206     while (*dst++)
207         continue;
208     --dst;
209     while ((*dst++ = *src++) != '\0')
210         continue;
211     return (sdst);
212 }
213
214 #ifdef NOTUSED
215 Char   *
216 s_strncat(dst, src, n)
217     Char *dst, *src;
218     size_t n;
219 {
220     Char *sdst;
221
222     if (n == 0)
223         return (dst);
224
225     sdst = dst;
226
227     while (*dst++)
228         continue;
229     --dst;
230
231     do
232         if ((*dst++ = *src++) == '\0')
233             return(sdst);
234     while (--n != 0)
235         continue;
236
237     *dst = '\0';
238     return (sdst);
239 }
240
241 #endif
242
243 Char   *
244 s_strchr(str, ch)
245     Char *str;
246     int ch;
247 {
248     do
249         if (*str == ch)
250             return (str);
251     while (*str++);
252     return (NULL);
253 }
254
255 Char   *
256 s_strrchr(str, ch)
257     Char *str;
258     int ch;
259 {
260     Char *rstr;
261
262     rstr = NULL;
263     do
264         if (*str == ch)
265             rstr = str;
266     while (*str++);
267     return (rstr);
268 }
269
270 size_t
271 s_strlen(str)
272     Char *str;
273 {
274     size_t n;
275
276     for (n = 0; *str++; n++)
277         continue;
278     return (n);
279 }
280
281 int
282 s_strcmp(str1, str2)
283     Char *str1, *str2;
284 {
285     for (; *str1 && *str1 == *str2; str1++, str2++)
286         continue;
287     /*
288      * The following case analysis is necessary so that characters which look
289      * negative collate low against normal characters but high against the
290      * end-of-string NUL.
291      */
292     if (*str1 == '\0' && *str2 == '\0')
293         return (0);
294     else if (*str1 == '\0')
295         return (-1);
296     else if (*str2 == '\0')
297         return (1);
298     else
299         return (*str1 - *str2);
300 }
301
302 int
303 s_strncmp(str1, str2, n)
304     Char *str1, *str2;
305     size_t n;
306 {
307     if (n == 0)
308         return (0);
309     do {
310         if (*str1 != *str2) {
311             /*
312              * The following case analysis is necessary so that characters
313              * which look negative collate low against normal characters
314              * but high against the end-of-string NUL.
315              */
316             if (*str1 == '\0')
317                 return (-1);
318             else if (*str2 == '\0')
319                 return (1);
320             else
321                 return (*str1 - *str2);
322             break;
323         }
324         if (*str1 == '\0')
325             return(0);
326         str1++, str2++;
327     } while (--n != 0);
328     return(0);
329 }
330
331 Char   *
332 s_strsave(s)
333     Char *s;
334 {
335     Char   *n;
336     Char *p;
337
338     if (s == 0)
339         s = STRNULL;
340     for (p = s; *p++;)
341         continue;
342     n = p = (Char *) xmalloc((size_t) ((p - s) * sizeof(Char)));
343     while ((*p++ = *s++) != '\0')
344         continue;
345     return (n);
346 }
347
348 Char   *
349 s_strspl(cp, dp)
350     Char   *cp, *dp;
351 {
352     Char   *ep;
353     Char *p, *q;
354
355     if (!cp)
356         cp = STRNULL;
357     if (!dp)
358         dp = STRNULL;
359     for (p = cp; *p++;)
360         continue;
361     for (q = dp; *q++;)
362         continue;
363     ep = (Char *) xmalloc((size_t)
364                           (((p - cp) + (q - dp) - 1) * sizeof(Char)));
365     for (p = ep, q = cp; (*p++ = *q++) != '\0';)
366         continue;
367     for (p--, q = dp; (*p++ = *q++) != '\0';)
368         continue;
369     return (ep);
370 }
371
372 Char   *
373 s_strend(cp)
374     Char *cp;
375 {
376     if (!cp)
377         return (cp);
378     while (*cp)
379         cp++;
380     return (cp);
381 }
382
383 Char   *
384 s_strstr(s, t)
385     Char *s, *t;
386 {
387     do {
388         Char *ss = s;
389         Char *tt = t;
390
391         do
392             if (*tt == '\0')
393                 return (s);
394         while (*ss++ == *tt++);
395     } while (*s++ != '\0');
396     return (NULL);
397 }
398 #endif                          /* SHORT_STRINGS */
399
400 char   *
401 short2qstr(src)
402     Char *src;
403 {
404     static char *sdst = NULL;
405     static size_t dstsize = 0;
406     char *dst, *edst;
407
408     if (src == NULL)
409         return (NULL);
410
411     if (sdst == NULL) {
412         dstsize = MALLOC_INCR;
413         sdst = (char *) xmalloc((size_t) dstsize * sizeof(char));
414     }
415     dst = sdst;
416     edst = &dst[dstsize];
417     while (*src) {
418         if (*src & QUOTE) {
419             *dst++ = '\\';
420             if (dst == edst) {
421                 dstsize += MALLOC_INCR;
422                 sdst = (char *) xrealloc((ptr_t) sdst,
423                                          (size_t) dstsize * sizeof(char));
424                 edst = &sdst[dstsize];
425                 dst = &edst[-MALLOC_INCR];
426             }
427         }
428         *dst++ = (char) *src++;
429         if (dst == edst) {
430             dstsize += MALLOC_INCR;
431             sdst = (char *) xrealloc((ptr_t) sdst,
432                                      (size_t) dstsize * sizeof(char));
433             edst = &sdst[dstsize];
434             dst = &edst[-MALLOC_INCR];
435         }
436     }
437     *dst = 0;
438     return (sdst);
439 }
440
441 /*
442  * XXX: Should we worry about QUOTE'd chars?
443  */
444 char *
445 vis_str(cp)
446     Char *cp;
447 {
448     static char *sdst = NULL;
449     static size_t dstsize = 0;
450     size_t n;
451     Char *dp;
452
453     if (cp == NULL)
454         return (NULL);
455
456     for (dp = cp; *dp++;)
457         continue;
458     n = ((dp - cp) << 2) + 1; /* 4 times + NULL */
459     if (dstsize < n) {
460         sdst = (char *) (dstsize ?
461                             xrealloc(sdst, (size_t) n * sizeof(char)) :
462                             xmalloc((size_t) n * sizeof(char)));
463         dstsize = n;
464     }
465     /*
466      * XXX: When we are in AsciiOnly we want all characters >= 0200 to
467      * be encoded, but currently there is no way in vis to do that.
468      */
469     (void) strvis(sdst, short2str(cp), VIS_NOSLASH);
470     return (sdst);
471 }
472