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