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