forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacro.c
More file actions
executable file
·197 lines (171 loc) · 4.13 KB
/
Copy pathmacro.c
File metadata and controls
executable file
·197 lines (171 loc) · 4.13 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*************************************************************************
*
* m a k e : m a c r o . c
*
* Macro control for make
*========================================================================
* Edition history
*
* # Date Comments By
* --- -------- ---------------------------------------------------- ---
* 1 ?? ??
* 2 23.08.89 Error message corrected RAL
* 3 30.08.89 macro flags added, indention ch. PSH,RAL
* 4 03.09.89 fixed LZ eliminated, doexp(...) changed RAL
* 5 06.09.89 M_MAKE added, setDFmacro added RAL
* 6 20.09.89 work around for Minix PC ACK bug BE,RAL
* ------------ Version 2.0 released ------------------------------- RAL
*
*************************************************************************/
#include "h.h"
static char buf[256];
struct macro *getmp(name)
char *name;
{
register struct macro *rp;
for (rp = macrohead; rp; rp = rp->m_next)
if (strcmp(name, rp->m_name) == 0)
return rp;
return (struct macro *)0;
}
char *getmacro(name)
char *name;
{
struct macro *mp;
if (mp = getmp(name))
return mp->m_val;
/* else*/
return "";
}
struct macro *setmacro(name, val)
char *name;
char *val;
{
register struct macro *rp;
register char *cp;
/* Replace macro definition if it exists */
for (rp = macrohead; rp; rp = rp->m_next)
if (strcmp(name, rp->m_name) == 0) {
if(rp->m_flag & M_OVERRIDE) return rp; /* mustn't change */
free(rp->m_val); /* Free space from old */
break;
}
if (!rp) /* If not defined, allocate space for new */
{
if ((rp = (struct macro *)malloc(sizeof (struct macro)))
== (struct macro *)0)
fatal("No memory for macro",(char *)0,0);
rp->m_next = macrohead;
macrohead = rp;
rp->m_flag = FALSE;
if ((cp = (char *) malloc(strlen(name)+1)) == (char *)0)
fatal("No memory for macro",(char *)0,0);
strcpy(cp, name);
rp->m_name = cp;
}
if ((cp = (char *) malloc(strlen(val)+1)) == (char *)0)
fatal("No memory for macro",(char *)0,0);
strcpy(cp, val); /* Copy in new value */
rp->m_val = cp;
return rp;
}
void setDFmacro(name, val)
char *name;
char *val;
{
char *c,*tmp;
int len;
static char filename[]="@F";
static char dirname[] ="@D";
setmacro(name,val);
*filename = *name;
*dirname = *name;
/* Null string -- not defined macro */
if ( !(*val)) {
setmacro(filename,"");
setmacro(dirname,"");
return;
}
if (!(c = strrchr(val,(int)'/'))) {
setmacro(filename,val);
setmacro(dirname,"./");
return;
}
setmacro(filename,c+1);
len = c - val + 1;
if((tmp = (char *) malloc(len + 1)) == (char *) 0)
fatal("No memory for tmp",(char *)0,0);
strncpy(tmp,val,len);
tmp[len] = '\0';
setmacro(dirname,tmp);
free(tmp);
return;
}
/*
* Do the dirty work for expand
*/
void doexp(to, from)
struct str *to;
char *from;
{
register char *rp;
register char *p;
char *q;
struct macro *mp;
rp = from;
p = &(*to->ptr)[to->pos];
while (*rp) {
if (*rp != '$') {
*p++ = *rp++;
to->pos++;
}
else {
q = buf;
if (*++rp == '{')
while (*++rp && *rp != '}')
*q++ = *rp;
else if (*rp == '(')
while (*++rp && *rp != ')')
*q++ = *rp;
else if (!*rp) {
*p++ = '$';
to->pos++;
goto bail;
}
else
*q++ = *rp;
*q = '\0';
if (*rp)
rp++;
if (!(mp = getmp(buf)))
mp = setmacro(buf, "");
if (mp->m_flag & M_MARK)
fatal("Infinitely recursive macro %s", mp->m_name,0);
mp->m_flag |= M_MARK;
if ( mp->m_flag & M_MAKE) expmake = TRUE;
doexp(to, mp->m_val);
p = &(*to->ptr)[to->pos];
mp->m_flag &= ~M_MARK;
}
bail:
if (to->pos >= to->len) {
strrealloc(to);
p = &(*to->ptr)[to->pos];
}
}
*p = '\0';
}
/*
* Expand any macros in str.
*/
void expand(strs)
struct str *strs;
{
char *a;
if ((a = (char *) malloc(strlen(*strs->ptr)+1)) == (char *)0)
fatal("No memory for temporary string",(char *)0,0);
strcpy(a, *strs->ptr);
strs->pos = 0;
doexp(strs, a);
free(a);
}