Home | History | Annotate | Line # | Download | only in csh
      1  1.17       nia /* $NetBSD: str.c,v 1.17 2024/04/24 15:49:03 nia 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.17       nia __RCSID("$NetBSD: str.c,v 1.17 2024/04/24 15:49:03 nia 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.17       nia     sdst = dst = xreallocarray(NULL, 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.17       nia     sdst = dst = xreallocarray(NULL, 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.17       nia 	sdst = xreallocarray(NULL, (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.17       nia 	    sdst = xreallocarray(sdst, (size_t)dstsize, sizeof(*sdst));
    118   1.1       cgd 	    edst = &sdst[dstsize];
    119   1.1       cgd 	    dst = &edst[-MALLOC_INCR];
    120   1.1       cgd 	}
    121   1.1       cgd     }
    122   1.1       cgd     *dst = 0;
    123   1.1       cgd     return (sdst);
    124   1.1       cgd }
    125   1.1       cgd 
    126  1.11       wiz char *
    127  1.14  christos short2str(const Char *src)
    128   1.1       cgd {
    129   1.1       cgd     static char *sdst = NULL;
    130   1.1       cgd     static size_t dstsize = 0;
    131   1.7       tls     char *dst, *edst;
    132   1.1       cgd 
    133   1.1       cgd     if (src == NULL)
    134   1.1       cgd 	return (NULL);
    135   1.1       cgd 
    136   1.1       cgd     if (sdst == NULL) {
    137   1.1       cgd 	dstsize = MALLOC_INCR;
    138  1.17       nia 	sdst = xreallocarray(NULL, dstsize, sizeof(*sdst));
    139   1.1       cgd     }
    140   1.1       cgd     dst = sdst;
    141   1.1       cgd     edst = &dst[dstsize];
    142   1.1       cgd     while (*src) {
    143   1.1       cgd 	*dst++ = (char) *src++;
    144   1.1       cgd 	if (dst == edst) {
    145   1.1       cgd 	    dstsize += MALLOC_INCR;
    146  1.17       nia 	    sdst = xreallocarray(sdst, dstsize, sizeof(*sdst));
    147   1.1       cgd 	    edst = &sdst[dstsize];
    148   1.1       cgd 	    dst = &edst[-MALLOC_INCR];
    149   1.1       cgd 	}
    150   1.1       cgd     }
    151   1.1       cgd     *dst = 0;
    152   1.1       cgd     return (sdst);
    153   1.1       cgd }
    154   1.1       cgd 
    155  1.11       wiz Char *
    156  1.14  christos s_strcpy(Char *dst, const Char *src)
    157   1.1       cgd {
    158   1.7       tls     Char *sdst;
    159   1.1       cgd 
    160   1.1       cgd     sdst = dst;
    161   1.5   mycroft     while ((*dst++ = *src++) != '\0')
    162   1.5   mycroft 	continue;
    163   1.1       cgd     return (sdst);
    164   1.1       cgd }
    165   1.1       cgd 
    166  1.11       wiz Char *
    167  1.14  christos s_strncpy(Char *dst, const Char *src, size_t n)
    168   1.1       cgd {
    169   1.7       tls     Char *sdst;
    170   1.1       cgd 
    171   1.1       cgd     if (n == 0)
    172   1.1       cgd 	return(dst);
    173   1.1       cgd 
    174   1.1       cgd     sdst = dst;
    175   1.5   mycroft     do
    176   1.1       cgd 	if ((*dst++ = *src++) == '\0') {
    177   1.1       cgd 	    while (--n != 0)
    178   1.1       cgd 		*dst++ = '\0';
    179   1.1       cgd 	    return(sdst);
    180   1.1       cgd 	}
    181   1.1       cgd     while (--n != 0);
    182   1.1       cgd     return (sdst);
    183   1.1       cgd }
    184   1.1       cgd 
    185  1.11       wiz Char *
    186  1.14  christos s_strcat(Char *dst, const Char *src)
    187   1.1       cgd {
    188   1.7       tls     short *sdst;
    189   1.1       cgd 
    190   1.1       cgd     sdst = dst;
    191   1.5   mycroft     while (*dst++)
    192   1.5   mycroft 	continue;
    193   1.1       cgd     --dst;
    194   1.5   mycroft     while ((*dst++ = *src++) != '\0')
    195   1.5   mycroft 	continue;
    196   1.1       cgd     return (sdst);
    197   1.1       cgd }
    198   1.1       cgd 
    199   1.1       cgd #ifdef NOTUSED
    200  1.11       wiz Char *
    201  1.11       wiz s_strncat(Char *dst, Char *src, size_t n)
    202   1.1       cgd {
    203   1.7       tls     Char *sdst;
    204   1.1       cgd 
    205   1.5   mycroft     if (n == 0)
    206   1.1       cgd 	return (dst);
    207   1.1       cgd 
    208   1.1       cgd     sdst = dst;
    209   1.1       cgd 
    210   1.5   mycroft     while (*dst++)
    211   1.5   mycroft 	continue;
    212   1.1       cgd     --dst;
    213   1.1       cgd 
    214   1.5   mycroft     do
    215   1.1       cgd 	if ((*dst++ = *src++) == '\0')
    216   1.1       cgd 	    return(sdst);
    217   1.5   mycroft     while (--n != 0)
    218   1.5   mycroft 	continue;
    219   1.1       cgd 
    220   1.1       cgd     *dst = '\0';
    221   1.1       cgd     return (sdst);
    222   1.1       cgd }
    223   1.1       cgd 
    224   1.1       cgd #endif
    225   1.1       cgd 
    226  1.11       wiz Char *
    227  1.14  christos s_strchr(const Char *str, int ch)
    228   1.1       cgd {
    229   1.1       cgd     do
    230   1.1       cgd 	if (*str == ch)
    231  1.14  christos 	    return __UNCONST(str);
    232   1.1       cgd     while (*str++);
    233   1.1       cgd     return (NULL);
    234   1.1       cgd }
    235   1.1       cgd 
    236  1.11       wiz Char *
    237  1.14  christos s_strrchr(const Char *str, int ch)
    238   1.1       cgd {
    239  1.14  christos     const Char *rstr;
    240   1.1       cgd 
    241   1.1       cgd     rstr = NULL;
    242   1.1       cgd     do
    243   1.1       cgd 	if (*str == ch)
    244   1.1       cgd 	    rstr = str;
    245   1.1       cgd     while (*str++);
    246  1.14  christos     return __UNCONST(rstr);
    247   1.1       cgd }
    248   1.1       cgd 
    249   1.1       cgd size_t
    250  1.14  christos s_strlen(const Char *str)
    251   1.1       cgd {
    252   1.7       tls     size_t n;
    253   1.1       cgd 
    254   1.5   mycroft     for (n = 0; *str++; n++)
    255   1.5   mycroft 	continue;
    256   1.1       cgd     return (n);
    257   1.1       cgd }
    258   1.1       cgd 
    259   1.1       cgd int
    260  1.14  christos s_strcmp(const Char *str1, const Char *str2)
    261   1.1       cgd {
    262   1.5   mycroft     for (; *str1 && *str1 == *str2; str1++, str2++)
    263   1.5   mycroft 	continue;
    264   1.1       cgd     /*
    265   1.1       cgd      * The following case analysis is necessary so that characters which look
    266   1.1       cgd      * negative collate low against normal characters but high against the
    267   1.1       cgd      * end-of-string NUL.
    268   1.1       cgd      */
    269   1.1       cgd     if (*str1 == '\0' && *str2 == '\0')
    270   1.1       cgd 	return (0);
    271   1.1       cgd     else if (*str1 == '\0')
    272   1.1       cgd 	return (-1);
    273   1.1       cgd     else if (*str2 == '\0')
    274   1.1       cgd 	return (1);
    275   1.1       cgd     else
    276   1.1       cgd 	return (*str1 - *str2);
    277   1.1       cgd }
    278   1.1       cgd 
    279   1.1       cgd int
    280  1.14  christos s_strncmp(const Char *str1, const Char *str2, size_t n)
    281   1.1       cgd {
    282   1.1       cgd     if (n == 0)
    283   1.1       cgd 	return (0);
    284   1.1       cgd     do {
    285   1.5   mycroft 	if (*str1 != *str2) {
    286   1.5   mycroft 	    /*
    287   1.5   mycroft 	     * The following case analysis is necessary so that characters
    288   1.5   mycroft 	     * which look negative collate low against normal characters
    289   1.5   mycroft 	     * but high against the end-of-string NUL.
    290   1.5   mycroft 	     */
    291   1.5   mycroft 	    if (*str1 == '\0')
    292   1.5   mycroft 		return (-1);
    293   1.5   mycroft 	    else if (*str2 == '\0')
    294   1.5   mycroft 		return (1);
    295   1.5   mycroft 	    else
    296   1.5   mycroft 		return (*str1 - *str2);
    297   1.5   mycroft 	}
    298   1.5   mycroft         if (*str1 == '\0')
    299   1.5   mycroft 	    return(0);
    300   1.1       cgd 	str1++, str2++;
    301   1.1       cgd     } while (--n != 0);
    302   1.1       cgd     return(0);
    303   1.1       cgd }
    304   1.1       cgd 
    305  1.11       wiz Char *
    306  1.14  christos s_strsave(const Char *s)
    307   1.1       cgd {
    308  1.14  christos     const Char *p;
    309  1.14  christos     Char *n;
    310   1.1       cgd 
    311   1.1       cgd     if (s == 0)
    312   1.1       cgd 	s = STRNULL;
    313   1.5   mycroft     for (p = s; *p++;)
    314   1.5   mycroft 	continue;
    315  1.17       nia     p = n = xreallocarray(NULL, (size_t)(p - s), sizeof(*n));
    316  1.14  christos     while ((*n++ = *s++) != '\0')
    317   1.5   mycroft 	continue;
    318  1.14  christos     return __UNCONST(p);
    319   1.1       cgd }
    320   1.1       cgd 
    321  1.11       wiz Char *
    322  1.14  christos s_strspl(const Char *cp, const Char *dp)
    323   1.1       cgd {
    324  1.14  christos     Char *ep, *d;
    325  1.14  christos     const Char *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.17       nia     ep = xreallocarray(NULL, (size_t)((p - cp) + (q - dp) - 1), sizeof(*ep));
    336  1.14  christos     for (d = ep, q = cp; (*d++ = *q++) != '\0';)
    337   1.5   mycroft 	continue;
    338  1.14  christos     for (d--, q = dp; (*d++ = *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.14  christos s_strend(const Char *cp)
    345   1.1       cgd {
    346   1.1       cgd     if (!cp)
    347  1.14  christos 	return __UNCONST(cp);
    348   1.1       cgd     while (*cp)
    349   1.1       cgd 	cp++;
    350  1.14  christos     return __UNCONST(cp);
    351   1.1       cgd }
    352   1.1       cgd 
    353  1.11       wiz Char *
    354  1.14  christos s_strstr(const Char *s, const Char *t)
    355   1.1       cgd {
    356   1.1       cgd     do {
    357  1.14  christos 	const Char *ss = s;
    358  1.14  christos 	const Char *tt = t;
    359   1.1       cgd 
    360   1.1       cgd 	do
    361   1.1       cgd 	    if (*tt == '\0')
    362  1.14  christos 		return __UNCONST(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.14  christos short2qstr(const 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.17       nia 	sdst = xreallocarray(NULL, dstsize, sizeof(*sdst));
    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.14  christos 
    387   1.5   mycroft 	if (*src & QUOTE) {
    388   1.5   mycroft 	    *dst++ = '\\';
    389   1.5   mycroft 	    if (dst == edst) {
    390   1.5   mycroft 		dstsize += MALLOC_INCR;
    391  1.17       nia 		sdst = xreallocarray(sdst, (size_t)dstsize, sizeof(*sdst));
    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.17       nia 	    sdst = xreallocarray(sdst, (size_t)dstsize, sizeof(*sdst));
    400   1.5   mycroft 	    edst = &sdst[dstsize];
    401   1.5   mycroft 	    dst = &edst[-MALLOC_INCR];
    402   1.5   mycroft 	}
    403   1.5   mycroft     }
    404   1.5   mycroft     *dst = 0;
    405   1.5   mycroft     return (sdst);
    406   1.5   mycroft }
    407   1.5   mycroft 
    408   1.5   mycroft /*
    409   1.5   mycroft  * XXX: Should we worry about QUOTE'd chars?
    410   1.5   mycroft  */
    411   1.5   mycroft char *
    412  1.14  christos vis_str(const Char *cp)
    413   1.5   mycroft {
    414   1.5   mycroft     static char *sdst = NULL;
    415   1.5   mycroft     static size_t dstsize = 0;
    416  1.14  christos     const Char *dp;
    417   1.5   mycroft     size_t n;
    418   1.1       cgd 
    419   1.5   mycroft     if (cp == NULL)
    420   1.5   mycroft 	return (NULL);
    421   1.5   mycroft 
    422   1.5   mycroft     for (dp = cp; *dp++;)
    423   1.5   mycroft 	continue;
    424  1.15  christos     n = ((size_t)(dp - cp) << 2) + 1; /* 4 times + NULL */
    425   1.5   mycroft     if (dstsize < n) {
    426  1.15  christos 	sdst = (dstsize ?
    427  1.17       nia 	    xreallocarray(sdst, (size_t)n, sizeof(*sdst)) :
    428  1.17       nia 	    xreallocarray(NULL, (size_t)n, sizeof(*sdst)));
    429   1.5   mycroft 	dstsize = n;
    430   1.5   mycroft     }
    431   1.5   mycroft     /*
    432   1.5   mycroft      * XXX: When we are in AsciiOnly we want all characters >= 0200 to
    433   1.5   mycroft      * be encoded, but currently there is no way in vis to do that.
    434   1.5   mycroft      */
    435  1.11       wiz     (void)strvis(sdst, short2str(cp), VIS_NOSLASH);
    436   1.5   mycroft     return (sdst);
    437   1.5   mycroft }
    438