str.c revision 1.15 1 1.15 christos /* $NetBSD: str.c,v 1.15 2013/07/16 17:47:43 christos 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.15 christos __RCSID("$NetBSD: str.c,v 1.15 2013/07/16 17:47:43 christos 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.15 christos sdst = dst = xmalloc((size_t)((n + 1) * sizeof(*dst)));
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.14 christos short2blk(Char *const *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.15 christos sdst = dst = xmalloc((size_t)((n + 1) * sizeof(*dst)));
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.15 christos sdst = xmalloc((size_t)dstsize * sizeof(*sdst));
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.15 christos sdst = xrealloc((ptr_t)sdst,
118 1.15 christos (size_t)dstsize * sizeof(*sdst));
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.14 christos short2str(const 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.15 christos sdst = xmalloc((size_t)dstsize * sizeof(*sdst));
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.15 christos sdst = xrealloc((ptr_t)sdst,
148 1.15 christos (size_t)dstsize * sizeof(*sdst));
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.14 christos s_strcpy(Char *dst, const 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.14 christos s_strncpy(Char *dst, const 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.14 christos s_strcat(Char *dst, const 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.14 christos s_strchr(const Char *str, int ch)
230 1.1 cgd {
231 1.1 cgd do
232 1.1 cgd if (*str == ch)
233 1.14 christos return __UNCONST(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.14 christos s_strrchr(const Char *str, int ch)
240 1.1 cgd {
241 1.14 christos const 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.14 christos return __UNCONST(rstr);
249 1.1 cgd }
250 1.1 cgd
251 1.1 cgd size_t
252 1.14 christos s_strlen(const 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.14 christos s_strcmp(const Char *str1, const 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.14 christos s_strncmp(const Char *str1, const 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.14 christos s_strsave(const Char *s)
309 1.1 cgd {
310 1.14 christos const Char *p;
311 1.14 christos Char *n;
312 1.1 cgd
313 1.1 cgd if (s == 0)
314 1.1 cgd s = STRNULL;
315 1.5 mycroft for (p = s; *p++;)
316 1.5 mycroft continue;
317 1.15 christos p = n = xmalloc((size_t)(p - s) * sizeof(*n));
318 1.14 christos while ((*n++ = *s++) != '\0')
319 1.5 mycroft continue;
320 1.14 christos return __UNCONST(p);
321 1.1 cgd }
322 1.1 cgd
323 1.11 wiz Char *
324 1.14 christos s_strspl(const Char *cp, const Char *dp)
325 1.1 cgd {
326 1.14 christos Char *ep, *d;
327 1.14 christos const Char *p, *q;
328 1.1 cgd
329 1.1 cgd if (!cp)
330 1.1 cgd cp = STRNULL;
331 1.1 cgd if (!dp)
332 1.1 cgd dp = STRNULL;
333 1.5 mycroft for (p = cp; *p++;)
334 1.5 mycroft continue;
335 1.5 mycroft for (q = dp; *q++;)
336 1.5 mycroft continue;
337 1.15 christos ep = xmalloc((size_t)((p - cp) + (q - dp) - 1) * sizeof(*ep));
338 1.14 christos for (d = ep, q = cp; (*d++ = *q++) != '\0';)
339 1.5 mycroft continue;
340 1.14 christos for (d--, q = dp; (*d++ = *q++) != '\0';)
341 1.5 mycroft continue;
342 1.1 cgd return (ep);
343 1.1 cgd }
344 1.1 cgd
345 1.11 wiz Char *
346 1.14 christos s_strend(const Char *cp)
347 1.1 cgd {
348 1.1 cgd if (!cp)
349 1.14 christos return __UNCONST(cp);
350 1.1 cgd while (*cp)
351 1.1 cgd cp++;
352 1.14 christos return __UNCONST(cp);
353 1.1 cgd }
354 1.1 cgd
355 1.11 wiz Char *
356 1.14 christos s_strstr(const Char *s, const Char *t)
357 1.1 cgd {
358 1.1 cgd do {
359 1.14 christos const Char *ss = s;
360 1.14 christos const Char *tt = t;
361 1.1 cgd
362 1.1 cgd do
363 1.1 cgd if (*tt == '\0')
364 1.14 christos return __UNCONST(s);
365 1.1 cgd while (*ss++ == *tt++);
366 1.1 cgd } while (*s++ != '\0');
367 1.1 cgd return (NULL);
368 1.1 cgd }
369 1.5 mycroft #endif /* SHORT_STRINGS */
370 1.5 mycroft
371 1.11 wiz char *
372 1.14 christos short2qstr(const Char *src)
373 1.5 mycroft {
374 1.5 mycroft static char *sdst = NULL;
375 1.5 mycroft static size_t dstsize = 0;
376 1.7 tls char *dst, *edst;
377 1.5 mycroft
378 1.5 mycroft if (src == NULL)
379 1.5 mycroft return (NULL);
380 1.5 mycroft
381 1.5 mycroft if (sdst == NULL) {
382 1.5 mycroft dstsize = MALLOC_INCR;
383 1.15 christos sdst = xmalloc((size_t)dstsize * sizeof(*sdst));
384 1.5 mycroft }
385 1.5 mycroft dst = sdst;
386 1.5 mycroft edst = &dst[dstsize];
387 1.5 mycroft while (*src) {
388 1.14 christos
389 1.5 mycroft if (*src & QUOTE) {
390 1.5 mycroft *dst++ = '\\';
391 1.5 mycroft if (dst == edst) {
392 1.5 mycroft dstsize += MALLOC_INCR;
393 1.15 christos sdst = xrealloc((ptr_t) sdst,
394 1.15 christos (size_t)dstsize * sizeof(*sdst));
395 1.5 mycroft edst = &sdst[dstsize];
396 1.5 mycroft dst = &edst[-MALLOC_INCR];
397 1.5 mycroft }
398 1.5 mycroft }
399 1.5 mycroft *dst++ = (char) *src++;
400 1.5 mycroft if (dst == edst) {
401 1.5 mycroft dstsize += MALLOC_INCR;
402 1.15 christos sdst = xrealloc((ptr_t) sdst,
403 1.15 christos (size_t)dstsize * sizeof(*sdst));
404 1.5 mycroft edst = &sdst[dstsize];
405 1.5 mycroft dst = &edst[-MALLOC_INCR];
406 1.5 mycroft }
407 1.5 mycroft }
408 1.5 mycroft *dst = 0;
409 1.5 mycroft return (sdst);
410 1.5 mycroft }
411 1.5 mycroft
412 1.5 mycroft /*
413 1.5 mycroft * XXX: Should we worry about QUOTE'd chars?
414 1.5 mycroft */
415 1.5 mycroft char *
416 1.14 christos vis_str(const Char *cp)
417 1.5 mycroft {
418 1.5 mycroft static char *sdst = NULL;
419 1.5 mycroft static size_t dstsize = 0;
420 1.14 christos const Char *dp;
421 1.5 mycroft size_t n;
422 1.1 cgd
423 1.5 mycroft if (cp == NULL)
424 1.5 mycroft return (NULL);
425 1.5 mycroft
426 1.5 mycroft for (dp = cp; *dp++;)
427 1.5 mycroft continue;
428 1.15 christos n = ((size_t)(dp - cp) << 2) + 1; /* 4 times + NULL */
429 1.5 mycroft if (dstsize < n) {
430 1.15 christos sdst = (dstsize ?
431 1.15 christos xrealloc(sdst, (size_t)n * sizeof(*sdst)) :
432 1.15 christos xmalloc((size_t)n * sizeof(*sdst)));
433 1.5 mycroft dstsize = n;
434 1.5 mycroft }
435 1.5 mycroft /*
436 1.5 mycroft * XXX: When we are in AsciiOnly we want all characters >= 0200 to
437 1.5 mycroft * be encoded, but currently there is no way in vis to do that.
438 1.5 mycroft */
439 1.11 wiz (void)strvis(sdst, short2str(cp), VIS_NOSLASH);
440 1.5 mycroft return (sdst);
441 1.5 mycroft }
442