Repository navigation
Expand file tree
/
Copy pathmacsup.c
More file actions
181 lines (161 loc) · 3.89 KB
/
Copy pathmacsup.c
File metadata and controls
181 lines (161 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* macsup.c - macro processing support functions for cawf(1)
*/
/*
* Copyright (c) 1991 Purdue University Research Foundation,
* West Lafayette, Indiana 47907. All rights reserved.
*
* Written by Victor A. Abell <abe@mace.cc.purdue.edu>, Purdue
* University Computing Center. Not derived from licensed software;
* derived from awf(1) by Henry Spencer of the University of Toronto.
*
* Permission is granted to anyone to use this software for any
* purpose on any computer system, and to alter it and redistribute
* it freely, subject to the following restrictions:
*
* 1. The author is not responsible for any consequences of use of
* this software, even if they arise from flaws in it.
*
* 2. The origin of this software must not be misrepresented, either
* by explicit claim or by omission. Credits must appear in the
* documentation.
*
* 3. Altered versions must be plainly marked as such, and must not
* be misrepresented as being the original software. Credits must
* appear in the documentation.
*
* 4. This notice may not be removed or altered.
*/
#include "cawf.h"
/*
* Delmacro(mx) - delete macro
*/
Delmacro(mx)
int mx; /* macro index */
{
unsigned char buf[MAXLINE]; /* error message buffer */
int i, j; /* temporary indexes */
if (mx >= Nmac) {
(void) sprintf((char *)buf, " bad Delmacro(%d) index", mx);
Error(FATAL, LINE, (char *)buf, NULL);
}
for (i = Macrotab[mx].bx, j = i + Macrotab[mx].ct; i < j; i++) {
Free(&Macrotxt[i]);
}
for (i = mx; i < (Nmac - 1); i++) {
Macrotab[i] = Macrotab[i+1];
}
Nmac--;
}
/*
* Field(n, p, c) - skip to field n in p and optionally return a copy
*/
unsigned char *
Field(n, p, c)
int n; /* field number */
unsigned char *p; /* pointer to line containing fields */
int c; /* 1: make a copy of the field */
{
unsigned char *fs, *fe, *s;
if (c)
Free(&F);
fe = p;
while (n) {
while (*fe == ' ' || *fe == '\t')
fe++;
fs = fe;
while (*fe && *fe != ' ' && *fe != '\t')
fe++;
if (fs == fe)
return(NULL);
if (n == 1) {
if ( ! c)
return(fs);
if ((F = (unsigned char *)malloc((size_t)(fe - fs + 1)))
== NULL)
Error(FATAL, LINE, " Field out of string space",
NULL);
(void) strncpy((char *)F, (char *)fs, (fe - fs));
F[fe -fs] = '\0';
return(F);
}
n--;
}
return(NULL);
}
/*
* Findmacro(p, e) - find macro and optionally enter it
*
* return = Macrotab[] index or -1 if not found
*/
Findmacro(p, e)
unsigned char *p; /* pointer to 2 character macro name */
int e; /* 0 = find, don't enter
* 1 = enter, don't find */
{
unsigned char c[3];
int cmp, hi, low, mid;
c[0] = p[0];
c[1] = (p[1] == ' ' || p[1] == '\t') ? '\0' : p[1];
c[2] = '\0';
low = mid = 0;
hi = Nmac - 1;
while (low <= hi) {
mid = (low + hi) / 2;
if ((cmp = strncmp((char *)c, (char *)Macrotab[mid].name, 2))
< 0)
hi = mid - 1;
else if (cmp > 0)
low = mid + 1;
else {
if ( ! e)
return(mid);
Error(WARN, LINE, " duplicate macro ", (char *)c);
hi = Macrotab[mid].bx + Macrotab[mid].ct;
for (low = Macrotab[mid].bx; low < hi; low++) {
Free(&Macrotxt[low]);
}
goto new_macro;
}
}
if ( ! e)
return(-1);
if (Nmac >= MAXMACRO)
Error(FATAL, LINE, " macro table full at ", (char *)c);
if (Nmac) {
if (cmp > 0)
mid++;
for (hi = Nmac - 1; hi >= mid; hi--)
Macrotab[hi+1] = Macrotab[hi];
}
Nmac++;
Macrotab[mid].name[0] = c[0];
Macrotab[mid].name[1] = c[1];
new_macro:
Macrotab[mid].bx = -1;
Macrotab[mid].ct = 0;
return(mid);
}
void
Free(p)
unsigned char **p;
{
if (*p != NULL) {
(void) free(*p);
*p = NULL;
}
}
/*
* Newstr(s) - allocate space for string
*/
unsigned char *
Newstr(s)
unsigned char *s;
{
unsigned char *ns;
if ((ns = (unsigned char *)malloc((size_t)(strlen((char *)s) + 1)))
== NULL)
Error(FATAL, LINE, " Newstr out of malloc space at ", (char *)s);
(void) strcpy((char *)ns, (char *)s);
return(ns);
}