xref: /original-bsd/usr.bin/pascal/src/subr.c (revision ec35a16d)
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[] = "@(#)subr.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include "whoami.h"
13 #include "0.h"
14 
15 #ifndef PI1
16 /*
17  * Does the string fp end in '.' and the character c ?
18  */
19 dotted(fp, c)
20 	register char *fp;
21 	char c;
22 {
23 	register int i;
24 
25 	i = strlen(fp);
26 	return (i > 1 && fp[i - 2] == '.' && fp[i - 1] == c);
27 }
28 
29 /*
30  * Toggle the option c.
31  */
32 togopt(c)
33 	char c;
34 {
35 	register char *tp;
36 
37 	tp = &opt( c );
38 	*tp = 1 - *tp;
39 }
40 
41 /*
42  * Set the time vector "tvec" to the
43  * modification time stamp of a file.
44  */
45 gettime( filename )
46     char *filename;
47 {
48 #include <sys/stat.h>
49 	struct stat stb;
50 
51 	stat(filename, &stb);
52 	tvec = stb.st_mtime;
53 }
54 
55 /*
56  * Convert a "ctime" into a Pascal styple time line
57  */
58 char *
59 myctime(tv)
60 	int *tv;
61 {
62 	register char *cp, *dp;
63 	extern char *ctime();
64 	char *cpp;
65 	static char mycbuf[26];
66 
67 	cpp = ctime(tv);
68 	dp = mycbuf;
69 	cp = cpp;
70 	cpp[16] = 0;
71 	while (*dp++ = *cp++);
72 	dp--;
73 	cp = cpp+19;
74 	cpp[24] = 0;
75 	while (*dp++ = *cp++);
76 	return (mycbuf);
77 }
78 
79 /*
80  * Is "fp" in the command line list of names ?
81  */
82 inpflist(fp)
83 	char *fp;
84 {
85 	register i;
86 	register char **pfp;
87 
88 	pfp = pflist;
89 	for (i = pflstc; i > 0; i--)
90 		if (pstrcmp(fp, *pfp++) == 0)
91 			return (1);
92 	return (0);
93 }
94 #endif
95 
96 /*
97  * Boom!
98  */
99 Perror(file, error)
100 	char *file, *error;
101 {
102 
103 	fprintf( stderr , "%s: %s\n" , file , error );
104 }
105 
106 int *
107 pcalloc(num, size)
108 	int num, size;
109 {
110 	register int *p1, *p2, nbyte;
111 
112 	nbyte = (num*size+( ( sizeof ( int ) ) - 1 ) ) & ~( ( sizeof ( int ) ) - 1 );
113 	if ((p1 = (int *) malloc((unsigned) nbyte)) == 0)
114 		return (0);
115 	p2 =  p1;
116 	nbyte /= sizeof ( int );
117 	do {
118 		*p2++ = 0;
119 	} while (--nbyte);
120 	return (p1);
121 }
122 
123 /*
124  * Compare strings:  s1>s2: >0  s1==s2: 0  s1<s2: <0
125  */
126 pstrcmp(s1, s2)
127 	register char *s1, *s2;
128 {
129 
130 	while (*s1 == *s2++)
131 		if (*s1++=='\0')
132 			return (0);
133 	return (*s1 - *--s2);
134 }
135 
136 /*
137  * Copy string s2 to s1.
138  * S1 must be large enough.
139  * Return s1.
140  */
141 char *
142 pstrcpy(s1, s2)
143 	register char *s1, *s2;
144 {
145 	register char *os1;
146 
147 	os1 = s1;
148 	while (*s1++ = *s2++)
149 		continue;
150 	return (os1);
151 }
152 
153 /*
154  * Strlen is currently a freebie of perror
155  * Take the length of a string.
156  * Note that this does not include the trailing null!
157 strlen(cp)
158 	register char *cp;
159 {
160 	register int i;
161 
162 	for (i = 0; *cp != 0; cp++)
163 		i++;
164 	return (i);
165 }
166  */
167 copy(to, from, bytes)
168 	register char *to, *from;
169 	register int bytes;
170 {
171 
172 	if (bytes != 0)
173 		do
174 			*to++ = *from++;
175 		while (--bytes);
176 }
177 
178 /*
179  * Is ch one of the characters in the string cp ?
180  */
181 any(cp, ch)
182 	register char *cp;
183 	char ch;
184 {
185 
186 	while (*cp)
187 		if (*cp++ == ch)
188 			return (1);
189 	return (0);
190 }
191 
192 opush(c)
193 	register CHAR c;
194 {
195 
196 	c -= 'A';
197 	optstk[c] <<= 1;
198 	optstk[c] |= opts[c];
199 	opts[c] = 1;
200 #ifdef PI0
201 	send(ROPUSH, c);
202 #endif
203 }
204 
205 opop(c)
206 	register CHAR c;
207 {
208 
209 	c -= 'A';
210 	opts[c] = optstk[c] & 1;
211 	optstk[c] >>= 1;
212 #ifdef PI0
213 	send(ROPOP, c);
214 #endif
215 }
216