xref: /original-bsd/usr.bin/pascal/src/string.c (revision 37071c60)
1 /*-
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)string.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include "whoami.h"
13 #include "align.h"
14 #include "0.h"
15 #ifndef PI01
16 #ifndef PXP
17 #include "send.h"
18 #endif
19 #endif
20 
21 /*
22  * STRING SPACE DECLARATIONS
23  *
24  * Strng is the base of the current
25  * string space and strngp the
26  * base of the free area therein.
27  * Strp is the array of descriptors.
28  */
29 #ifndef PI0
30 STATIC	char strings[STRINC];
31 STATIC	char *strng = strings;
32 STATIC	char *strngp = strings;
33 #else
34 char	*strng, *strngp;
35 #endif
36 #ifndef PI01
37 #ifndef PXP
38 STATIC	char *strp[20];
39 STATIC	char **stract strp;
40 int	strmax;
41 #endif
42 #endif
43 
44 #ifndef PI01
45 #ifndef PXP
46 #ifndef PI0
47 initstring()
48 #else
49 initstring(strings)
50 	char *strings;
51 #endif
52 {
53 
54 	*stract++ = strings;
55 #ifdef PI0
56 	strng = strngp = strings;
57 #endif
58 	strmax = STRINC * 2;
59 }
60 #endif
61 #endif
62 
63 /*
64  * Copy a string into the string area.
65  */
66 char *
67 savestr(cp)
68 	register char *cp;
69 {
70 	register int i;
71 
72 	i = strlen(cp) + 1;
73 	if (strngp + i >= strng + STRINC) {
74 		strngp = malloc(STRINC);
75 		if (strngp == 0) {
76 			yerror("Ran out of memory (string)");
77 			pexit(DIED);
78 		}
79 #ifndef PI01
80 #ifndef PXP
81 		*stract++ = strngp;
82 		strmax += STRINC;
83 #endif
84 #endif
85 		strng = strngp;
86 	}
87 	(void) pstrcpy(strngp, cp);
88 	cp = strngp;
89 	strngp = cp + i;
90 #ifdef PI0
91 	send(RSTRING, cp);
92 #endif
93 	return (cp);
94 }
95 
96 #ifndef PI1
97 #ifndef PXP
98 char *
99 esavestr(cp)
100 	char *cp;
101 {
102 
103 #ifdef PI0
104 	send(REVENIT);
105 #endif
106 	strngp = ( (char *) roundup( strngp, A_LONG ) );
107 	return (savestr(cp));
108 }
109 #endif
110 #endif
111 
112 #ifndef PI01
113 #ifndef PXP
114 soffset(cp)
115 	register char *cp;
116 {
117 	register char **sp;
118 	register int i;
119 
120 	if (cp == NIL || cp == OCT || cp == HEX)
121 		return (-cp);
122 	for (i = STRINC, sp = strp; sp < stract; sp++) {
123 		if (cp >= *sp && cp < (*sp + STRINC))
124 			return (i + (cp - *sp));
125 		i += STRINC;
126 	}
127 	i = nlfund(cp);
128 	if (i != 0)
129 		return (i);
130 	panic("soffset");
131 }
132 #ifdef PI1
133 sreloc(i)
134 	register int i;
135 {
136 
137 	if (i == 0 || i == -OCT || i == -HEX)
138 		return (-i);
139 	if (i < STRINC) {
140 		if (i >= INL)
141 			panic("sreloc INL");
142 		i = nl[i].symbol;
143 		if (i == 0)
144 			panic("sreloc nl[i]");
145 		return (i);
146 	}
147 	if (i > strmax || i < 0)
148 		panic("sreloc");
149 	return (strp[(i / STRINC) - 1] + (i % STRINC));
150 }
151 #endif
152 #endif
153 #endif
154