xref: /csrg-svn/local/toolchest/ksh/sh/string.c (revision 35166)
1*35166Smarc /*
2*35166Smarc 
3*35166Smarc  *      Copyright (c) 1984, 1985, 1986 AT&T
4*35166Smarc  *      All Rights Reserved
5*35166Smarc 
6*35166Smarc  *      THIS IS UNPUBLISHED PROPRIETARY SOURCE
7*35166Smarc  *      CODE OF AT&T.
8*35166Smarc  *      The copyright notice above does not
9*35166Smarc  *      evidence any actual or intended
10*35166Smarc  *      publication of such source code.
11*35166Smarc 
12*35166Smarc  */
13*35166Smarc /*
14*35166Smarc  * string processing routines for Korn shell
15*35166Smarc  *
16*35166Smarc  */
17*35166Smarc 
18*35166Smarc #include	"defs.h"
19*35166Smarc #ifdef MULTIBYTE
20*35166Smarc #include	"national.h"
21*35166Smarc #endif /* MULTIBYTE */
22*35166Smarc 
23*35166Smarc /* The following routines are defined by this module */
24*35166Smarc char	*itos();
25*35166Smarc char	*movstr();
26*35166Smarc char	*substitute();
27*35166Smarc char	*simple();
28*35166Smarc void	trim();
29*35166Smarc 
30*35166Smarc /* The following routines are referenced by this module */
31*35166Smarc extern char	*utos();
32*35166Smarc extern char	*strrchr();
33*35166Smarc 
34*35166Smarc /*
35*35166Smarc  * converts integer n into an unsigned decimal string
36*35166Smarc  */
37*35166Smarc 
itos(n)38*35166Smarc char *itos(n)
39*35166Smarc int n;
40*35166Smarc {
41*35166Smarc #ifdef pdp11
42*35166Smarc 	return(utos((long)n,10));
43*35166Smarc #else
44*35166Smarc 	return(utos((unsigned long)n,10));
45*35166Smarc #endif /* pdp11 */
46*35166Smarc }
47*35166Smarc 
48*35166Smarc 
49*35166Smarc /*
50*35166Smarc  * look for the substring <old> in <string> and replace with <new>
51*35166Smarc  * The new string is put on top of the stack
52*35166Smarc  */
53*35166Smarc 
substitute(string,old,new,newstring)54*35166Smarc char *substitute(string,old,new,newstring)
55*35166Smarc char *string;
56*35166Smarc char *old;
57*35166Smarc char *new;
58*35166Smarc char *newstring;
59*35166Smarc {
60*35166Smarc 	register char *sp = string;
61*35166Smarc 	register char *dp;
62*35166Smarc 	register char *cp;
63*35166Smarc 	char *savesp = NIL;
64*35166Smarc 	dp = newstring;
65*35166Smarc 	if(*sp==0)
66*35166Smarc 		return(NIL);
67*35166Smarc 	if(*(cp=old) == 0)
68*35166Smarc 		goto found;
69*35166Smarc 	do
70*35166Smarc 	{
71*35166Smarc 	/* skip to first character which matches start of old */
72*35166Smarc 		while(*sp && (savesp==sp || *sp != *cp))
73*35166Smarc 		{
74*35166Smarc #ifdef MULTIBYTE
75*35166Smarc 			/* skip a whole character at a time */
76*35166Smarc 			int c = *sp;
77*35166Smarc 			c = echarset(c);
78*35166Smarc 			c = in_csize(c) + (c>=2);
79*35166Smarc 			while(c-- > 0)
80*35166Smarc #endif /* MULTIBYTE */
81*35166Smarc 			*dp++ = *sp++;
82*35166Smarc 		}
83*35166Smarc 		if(*sp == 0)
84*35166Smarc 			return(NIL);
85*35166Smarc 		savesp = sp;
86*35166Smarc 	        for(;*cp;cp++)
87*35166Smarc 		{
88*35166Smarc 			if(*cp != *sp++)
89*35166Smarc 				break;
90*35166Smarc 		}
91*35166Smarc 		if(*cp==0)
92*35166Smarc 		/* match found */
93*35166Smarc 			goto found;
94*35166Smarc 		sp = savesp;
95*35166Smarc 		cp = old;
96*35166Smarc 	}
97*35166Smarc 	while(*sp);
98*35166Smarc 	return(NIL);
99*35166Smarc 
100*35166Smarc found:
101*35166Smarc 	/* copy new */
102*35166Smarc 	dp = movstr(new,dp);
103*35166Smarc 	/* copy rest of string */
104*35166Smarc 	movstr(sp,dp);
105*35166Smarc 	return(newstring);
106*35166Smarc }
107*35166Smarc 
108*35166Smarc /*
109*35166Smarc  * given a pathname return the base name
110*35166Smarc  */
111*35166Smarc 
simple(name)112*35166Smarc char *simple(name)
113*35166Smarc register char *name;
114*35166Smarc {
115*35166Smarc 	char *start = name;
116*35166Smarc 	while (*name)
117*35166Smarc 		if ((*name++ == '/') && *name)	/* don't trim trailing / */
118*35166Smarc 			start = name;
119*35166Smarc 	return (start);
120*35166Smarc }
121*35166Smarc 
122*35166Smarc /*
123*35166Smarc  * TRIM(at)
124*35166Smarc  * Remove quote bit from characters in at and eliminate quoted nulls.
125*35166Smarc  */
126*35166Smarc 
trim(at)127*35166Smarc void	trim(at)
128*35166Smarc char *	at;
129*35166Smarc {
130*35166Smarc 	register char *sp = at;
131*35166Smarc 	register char *dp;
132*35166Smarc 	register int c;
133*35166Smarc 	if(sp)
134*35166Smarc 	{
135*35166Smarc 		dp = sp;
136*35166Smarc 		while(c= *sp++)
137*35166Smarc 		{
138*35166Smarc 			if(c == ESCAPE)
139*35166Smarc 				c = *sp++;
140*35166Smarc 			if(c)
141*35166Smarc 				*dp++ = c;
142*35166Smarc 		}
143*35166Smarc 		*dp = 0;
144*35166Smarc 	}
145*35166Smarc }
146*35166Smarc 
147*35166Smarc /*
148*35166Smarc  * copy string a to string b and return a pointer to the end of the string
149*35166Smarc  */
150*35166Smarc 
movstr(a,b)151*35166Smarc char *movstr(a,b)
152*35166Smarc register char *a,*b;
153*35166Smarc {
154*35166Smarc 	while(*b++ = *a++);
155*35166Smarc 	return(--b);
156*35166Smarc }
157*35166Smarc 
158