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