xref: /minix3/minix/commands/cawf/macsup.c (revision d0055759dd8892194db7fce6acc5085d5c9aeaee)
1433d6423SLionel Sambuc /*
2433d6423SLionel Sambuc  *	macsup.c - macro processing support functions for cawf(1)
3433d6423SLionel Sambuc  */
4433d6423SLionel Sambuc 
5433d6423SLionel Sambuc /*
6433d6423SLionel Sambuc  *	Copyright (c) 1991 Purdue University Research Foundation,
7433d6423SLionel Sambuc  *	West Lafayette, Indiana 47907.  All rights reserved.
8433d6423SLionel Sambuc  *
9433d6423SLionel Sambuc  *	Written by Victor A. Abell <abe@mace.cc.purdue.edu>,  Purdue
10433d6423SLionel Sambuc  *	University Computing Center.  Not derived from licensed software;
11433d6423SLionel Sambuc  *	derived from awf(1) by Henry Spencer of the University of Toronto.
12433d6423SLionel Sambuc  *
13433d6423SLionel Sambuc  *	Permission is granted to anyone to use this software for any
14433d6423SLionel Sambuc  *	purpose on any computer system, and to alter it and redistribute
15433d6423SLionel Sambuc  *	it freely, subject to the following restrictions:
16433d6423SLionel Sambuc  *
17433d6423SLionel Sambuc  *	1. The author is not responsible for any consequences of use of
18433d6423SLionel Sambuc  *	   this software, even if they arise from flaws in it.
19433d6423SLionel Sambuc  *
20433d6423SLionel Sambuc  *	2. The origin of this software must not be misrepresented, either
21433d6423SLionel Sambuc  *	   by explicit claim or by omission.  Credits must appear in the
22433d6423SLionel Sambuc  *	   documentation.
23433d6423SLionel Sambuc  *
24433d6423SLionel Sambuc  *	3. Altered versions must be plainly marked as such, and must not
25433d6423SLionel Sambuc  *	   be misrepresented as being the original software.  Credits must
26433d6423SLionel Sambuc  *	   appear in the documentation.
27433d6423SLionel Sambuc  *
28433d6423SLionel Sambuc  *	4. This notice may not be removed or altered.
29433d6423SLionel Sambuc  */
30433d6423SLionel Sambuc 
31433d6423SLionel Sambuc #include "cawf.h"
32433d6423SLionel Sambuc 
33433d6423SLionel Sambuc 
34433d6423SLionel Sambuc /*
35433d6423SLionel Sambuc  * Delmacro(mx) - delete macro
36433d6423SLionel Sambuc  */
37433d6423SLionel Sambuc 
Delmacro(int mx)38*d0055759SDavid van Moolenbroek void Delmacro(int mx) {
39d9494baaSJacob Adams /* macro index mx */
40433d6423SLionel Sambuc 	unsigned char buf[MAXLINE];	/* error message buffer */
41433d6423SLionel Sambuc 	int i, j;			/* temporary indexes */
42433d6423SLionel Sambuc 
43433d6423SLionel Sambuc 	if (mx >= Nmac) {
44433d6423SLionel Sambuc 		(void) sprintf((char *)buf, " bad Delmacro(%d) index", mx);
45433d6423SLionel Sambuc 		Error(FATAL, LINE, (char *)buf, NULL);
46433d6423SLionel Sambuc 	}
47433d6423SLionel Sambuc 	for (i = Macrotab[mx].bx, j = i + Macrotab[mx].ct; i < j; i++) {
48433d6423SLionel Sambuc 		Free(&Macrotxt[i]);
49433d6423SLionel Sambuc 	}
50433d6423SLionel Sambuc 	for (i = mx; i < (Nmac - 1); i++) {
51433d6423SLionel Sambuc 		Macrotab[i] = Macrotab[i+1];
52433d6423SLionel Sambuc 	}
53433d6423SLionel Sambuc 	Nmac--;
54433d6423SLionel Sambuc }
55433d6423SLionel Sambuc 
56433d6423SLionel Sambuc 
57433d6423SLionel Sambuc /*
58433d6423SLionel Sambuc  * Field(n, p, c) - skip to field n in p and optionally return a copy
59433d6423SLionel Sambuc  */
60433d6423SLionel Sambuc 
Field(int n,unsigned char * p,int c)61d9494baaSJacob Adams unsigned char *Field(int n, unsigned char *p, int c) {
62d9494baaSJacob Adams /* field number n
63d9494baaSJacob Adams  * pointer to line containing fields p
64d9494baaSJacob Adams  * c = 1: make a copy of the field
65d9494baaSJacob Adams  */
66433d6423SLionel Sambuc 	unsigned char *fs, *fe, *s;
67433d6423SLionel Sambuc 
68433d6423SLionel Sambuc 	if (c)
69433d6423SLionel Sambuc 		Free(&F);
70433d6423SLionel Sambuc 	fe = p;
71433d6423SLionel Sambuc 	while (n) {
72433d6423SLionel Sambuc 		while (*fe == ' ' || *fe == '\t')
73433d6423SLionel Sambuc 			fe++;
74433d6423SLionel Sambuc 		fs = fe;
75433d6423SLionel Sambuc 		while (*fe && *fe != ' ' && *fe != '\t')
76433d6423SLionel Sambuc 			fe++;
77433d6423SLionel Sambuc 		if (fs == fe)
78433d6423SLionel Sambuc 			return(NULL);
79433d6423SLionel Sambuc 		if (n == 1) {
80433d6423SLionel Sambuc 			if ( ! c)
81433d6423SLionel Sambuc 				return(fs);
82433d6423SLionel Sambuc 			if ((F = (unsigned char *)malloc((size_t)(fe - fs + 1)))
83433d6423SLionel Sambuc 			== NULL)
84433d6423SLionel Sambuc 				Error(FATAL, LINE, " Field out of string space",
85433d6423SLionel Sambuc 					NULL);
86433d6423SLionel Sambuc 			(void) strncpy((char *)F, (char *)fs, (fe - fs));
87433d6423SLionel Sambuc 			F[fe -fs] = '\0';
88433d6423SLionel Sambuc 			return(F);
89433d6423SLionel Sambuc 		}
90433d6423SLionel Sambuc 		n--;
91433d6423SLionel Sambuc 	}
92433d6423SLionel Sambuc 	return(NULL);
93433d6423SLionel Sambuc }
94433d6423SLionel Sambuc 
95433d6423SLionel Sambuc /*
96433d6423SLionel Sambuc  * Findmacro(p, e) - find macro and optionally enter it
97433d6423SLionel Sambuc  *
98433d6423SLionel Sambuc  * return = Macrotab[] index or -1 if not found
99433d6423SLionel Sambuc  */
100433d6423SLionel Sambuc 
101433d6423SLionel Sambuc 
Findmacro(unsigned char * p,int e)102d9494baaSJacob Adams int Findmacro(unsigned char *p, int e) {
103d9494baaSJacob Adams /* pointer to 2 character macro name p
104d9494baaSJacob Adams  * e = 0 = find, don't enter
105d9494baaSJacob Adams  * e = 1 = enter, don't find
106d9494baaSJacob Adams  */
107433d6423SLionel Sambuc 	unsigned char c[3];
108433d6423SLionel Sambuc 	int cmp, hi, low, mid;
109433d6423SLionel Sambuc 
110433d6423SLionel Sambuc 	c[0] = p[0];
111433d6423SLionel Sambuc 	c[1] = (p[1] == ' ' || p[1] == '\t') ? '\0' : p[1];
112433d6423SLionel Sambuc 	c[2] = '\0';
113433d6423SLionel Sambuc 	low = mid = 0;
114433d6423SLionel Sambuc 	hi = Nmac - 1;
115433d6423SLionel Sambuc 	while (low <= hi) {
116433d6423SLionel Sambuc 		mid = (low + hi) / 2;
117433d6423SLionel Sambuc 		if ((cmp = strncmp((char *)c, (char *)Macrotab[mid].name, 2))
118433d6423SLionel Sambuc 		< 0)
119433d6423SLionel Sambuc 			hi = mid - 1;
120433d6423SLionel Sambuc 		else if (cmp > 0)
121433d6423SLionel Sambuc 			low = mid + 1;
122433d6423SLionel Sambuc 		else {
123433d6423SLionel Sambuc 			if ( ! e)
124433d6423SLionel Sambuc 				return(mid);
125433d6423SLionel Sambuc 			 Error(WARN, LINE, " duplicate macro ", (char *)c);
126433d6423SLionel Sambuc 			 hi = Macrotab[mid].bx + Macrotab[mid].ct;
127433d6423SLionel Sambuc 			 for (low = Macrotab[mid].bx; low < hi; low++) {
128433d6423SLionel Sambuc 				Free(&Macrotxt[low]);
129433d6423SLionel Sambuc 			 }
130433d6423SLionel Sambuc 			 goto new_macro;
131433d6423SLionel Sambuc 		}
132433d6423SLionel Sambuc 	}
133433d6423SLionel Sambuc 	if ( ! e)
134433d6423SLionel Sambuc 		return(-1);
135433d6423SLionel Sambuc 	if (Nmac >= MAXMACRO)
136433d6423SLionel Sambuc 		Error(FATAL, LINE, " macro table full at ", (char *)c);
137433d6423SLionel Sambuc 	if (Nmac) {
138433d6423SLionel Sambuc 		if (cmp > 0)
139433d6423SLionel Sambuc 			mid++;
140433d6423SLionel Sambuc 		for (hi = Nmac - 1; hi >= mid; hi--)
141433d6423SLionel Sambuc 			Macrotab[hi+1] = Macrotab[hi];
142433d6423SLionel Sambuc 	}
143433d6423SLionel Sambuc 	Nmac++;
144433d6423SLionel Sambuc 	Macrotab[mid].name[0] = c[0];
145433d6423SLionel Sambuc 	Macrotab[mid].name[1] = c[1];
146433d6423SLionel Sambuc 
147433d6423SLionel Sambuc new_macro:
148433d6423SLionel Sambuc 
149433d6423SLionel Sambuc 	Macrotab[mid].bx = -1;
150433d6423SLionel Sambuc 	Macrotab[mid].ct = 0;
151433d6423SLionel Sambuc 	return(mid);
152433d6423SLionel Sambuc }
153433d6423SLionel Sambuc 
Free(unsigned char ** p)154d9494baaSJacob Adams void Free(unsigned char **p) {
155433d6423SLionel Sambuc 	if (*p != NULL) {
156433d6423SLionel Sambuc 		(void) free(*p);
157433d6423SLionel Sambuc 		*p = NULL;
158433d6423SLionel Sambuc 	}
159433d6423SLionel Sambuc }
160433d6423SLionel Sambuc 
161433d6423SLionel Sambuc /*
162433d6423SLionel Sambuc  * Newstr(s) - allocate space for string
163433d6423SLionel Sambuc  */
164433d6423SLionel Sambuc 
Newstr(unsigned char * s)165d9494baaSJacob Adams unsigned char *Newstr(unsigned char *s) {
166433d6423SLionel Sambuc 	unsigned char *ns;
167433d6423SLionel Sambuc 
168433d6423SLionel Sambuc 	if ((ns = (unsigned char *)malloc((size_t)(strlen((char *)s) + 1)))
169433d6423SLionel Sambuc 	== NULL)
170433d6423SLionel Sambuc 	    Error(FATAL, LINE, " Newstr out of malloc space at ", (char *)s);
171433d6423SLionel Sambuc 	(void) strcpy((char *)ns, (char *)s);
172433d6423SLionel Sambuc 	return(ns);
173433d6423SLionel Sambuc }
174