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