source: trunk/minix/commands/cawf/macsup.c@ 10

Last change on this file since 10 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

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