-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpushmac.pshm
432 lines (377 loc) · 8.82 KB
/
pushmac.pshm
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
pushmac - a stack-based macro expander extendable with Guile Scheme
Copyright Zach Flynn <zlflynn@gmail.com>
This program is free software: you can redistribute it and/or modify it under
the terms of version 3 of the GNU General Public License as published by the
Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/
`
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
';
/* parser state */
static int inquote = 0;
static int ignore = 0;
/* buffer stack */
struct buffer { char* text; int location; int size; };
struct buffer_stack { struct buffer* buf; int level; int n_pages; };
static struct buffer_stack stack;
/* macro table */
struct macro { char* name; char* value; };
struct macro_stack { struct macro* table; int n_pages; int n_macros; };
static struct macro_stack m;
/* characters not to print */
static char* dnp;
static int n_dnp = 0;
int
check_dnp (int c)
{
for (int i=1; i < n_dnp; i++)
if (dnp[i]==c)
return 1;
return 0;
}
/* pops buffer off stack */
struct buffer*
pop_buffer_stack ()
{
stack.level--;
return &stack.buf[stack.level];
}
/* writes character to buffer */
void
push_to_buffer (struct buffer* b, int c)
{
if (b->size <= b->location)
{
b->text = realloc(b->text,
sizeof(char)*(b->size + PAGE_BUFFER));
b->size = b->size + PAGE_BUFFER;
}
b->text[b->location] = c;
b->location++;
}
/* init buffer */
void
init_buffer (struct buffer* b)
{
b->text = malloc(sizeof(char)*PAGE_BUFFER);
b->size = PAGE_BUFFER;
b->location = 0;
}
/* copy buffer to string */
char*
copy_from_buffer (struct buffer* src)
{
char* dest = malloc(sizeof(char)*(src->location+1));
for (int i=0; i < src->location; i++)
dest[i] = src->text[i];
dest[src->location] = '\0';
return dest;
}
/* pushes text to buffer or screen */
void
output (int c)
{
if (stack.level == 0 && check_dnp(c))
return;
if (stack.level == 0)
printf("`%'c", c);
else
push_to_buffer(&stack.buf[stack.level-1], c);
}
/* null terminate a buffer */
void
null_terminate (struct buffer* b)
{
push_to_buffer(b, '\0');
b->location--;
}
/* clean up macro table */
void
free_macro_table ()
{
int i;
for (i=0; i < m.n_macros; i++)
{
free(m.table[i].name);
free(m.table[i].value);
}
free(m.table);
}
/* clean up stack */
void
free_stack ()
{
int i;
for (i=0; i < (PAGE_STACK*stack.n_pages); i++)
free(stack.buf[i].text);
free(stack.buf);
}
/* look up macro name */
int
look_up_name (char* name)
{
int i;
for (i = 0; i < m.n_macros; i++)
if (strcmp(m.table[i].name, name)==0)
return i;
return -1;
}
/* add macro to table */
void
add_macro_to_table (char* name, char* value)
{
int loc;
loc = look_up_name(name);
if (loc < 0)
{
if (m.n_macros >= (PAGE_MACRO*m.n_pages))
{
m.n_pages++;
m.table = realloc(m.table,
sizeof(struct macro)*PAGE_MACRO*m.n_pages);
}
m.table[m.n_macros].name = name;
m.table[m.n_macros].value = value;
m.n_macros++;
}
else
{
free(m.table[loc].value);
free(name);
m.table[loc].value = value;
}
}
#popbuf=`#buf~=pop_buffer_stack(); null_terminate(#buf~);'@
/* add macro given top two elements in buffer */
void
push_macro ()
{
struct buffer* value;
struct buffer* name;
#buf=value@ ##popbuf~$;
#buf=name@ ##popbuf~$;
add_macro_to_table(copy_from_buffer(name),
copy_from_buffer(value));
}
/* look at top three buffers. if third buffer is "yes", print second buffer. otherwise, print top buffer. */
void
ask_question ()
{
struct buffer* answer, *yesreply, *noreply;
#buf=noreply@ ##popbuf~$;
#buf=yesreply@ ##popbuf~$;
#buf=answer@ ##popbuf~$;
if (strcmp(answer->text,"yes") == 0)
for (int i=0; i < yesreply->location; i++)
output(yesreply->text[i]);
else
for (int i=0; i < noreply->location; i++)
output(noreply->text[i]);
}
/* append characters from buffer input to an array input, parsing
escape seq, returns ending position */
int
escape_and_add_chars (struct buffer* buf, char** array, int pos)
{
/* make this UTF-8'able */
*array = realloc(*array, sizeof(char)*(pos+buf->location));
int isslash = 0;
int j = pos;
for (int i=0; i < buf->location; i++)
{
if (isslash)
{
switch (buf->text[i])
{
case '\\':
(*array)[j] = '\\';
break;
case 'n':
(*array)[j] = '\n';
break;
case 't':
(*array)[j] = '\t';
break;
case 'r':
(*array)[j] = '\r';
break;
default:
(*array)[j] = '\\';
j++;
(*array)[j] = buf->text[i];
break;
}
j++;
}
else
{
if (buf->text[i] == '\\')
isslash = 1;
else
{
(*array)[j] = buf->text[i];
j++;
}
}
}
return j;
}
#default=output(c);@
#cmd=`if (stack.level >= #stack_reqd~) { #logic~ } else { #default~ } break;'@
#buf=buf@
/* expands macros from a file stream */
void
expand_macros (FILE* f)
{
int i,c,loc;
struct buffer* buf;
FILE* f2;
while (((c = fgetc(f)) != EOF) && c != '\0')
{
if (inquote)
{
if (c == '\'') inquote = 0; else #default~;
}
else if (ignore)
{
if (c == '\n') ignore = 0;
}
else
{
switch (c)
{
case PUSH2:
if (stack.level != 1)
{
#default~
break;
}
case PUSH:
{
if (stack.level >= (PAGE_STACK*stack.n_pages))
{
stack.n_pages++;
stack.buf = realloc(stack.buf,
sizeof(struct buffer)*
PAGE_STACK*stack.n_pages);
for (i=(stack.n_pages-1)*PAGE_STACK;
i < (PAGE_STACK*stack.n_pages); i++)
init_buffer(stack.buf+i);
}
stack.buf[stack.level].location = 0;
stack.level++;
}
break;
case DEFINE:
#stack_reqd=2@
#logic=push_macro();@
##cmd~$;
case REF:
#stack_reqd=1@
#logic=
##popbuf~$;
loc = look_up_name(buf->text);
if (loc >= 0)
for (i=0; i < strlen(m.table[loc].value); i++)
output(m.table[loc].value[i]);
@;
##cmd~$;
case EXPAND:
#stack_reqd=1@
#logic=
##popbuf~$;
f2 = fmemopen(buf->text, buf->size, "r");
expand_macros(f2);
fclose(f2);@;
##cmd~$;
case SHELL:
#stack_reqd=1@
#logic=
##popbuf~$;
f2 = popen(buf->text, "r");
while (((c=fgetc(f2)) `!'= EOF) && c `!'= '\0')
output(c);
pclose(f2);@;
##cmd~$;
case QUESTION:
#stack_reqd=3@
#logic=ask_question();@
##cmd~$;
case IGNORE:
#stack_reqd=1@
#logic=
##popbuf~$;@;
##cmd~$;
break;
case SILENCE:
#stack_reqd=1@
#logic=
##popbuf~$;
n_dnp = escape_and_add_chars(buf, &dnp, n_dnp);
dnp = realloc(dnp, sizeof(char)*n_dnp);
@;
##cmd~$;
break;
case SPEAK:
#stack_reqd=1@
#logic=
##popbuf~$;
free(dnp);
dnp = malloc(sizeof(char));
dnp[0] = '\0';
n_dnp = 1;
@;
##cmd~$;
break;
case '``'':
inquote = 1;
break;
default:
#default~
break;
}
}
}
}
/* main program */
int
main (int argc, char** argv)
{
int i;
char* val;
char* num;
stack.level = 0;
stack.n_pages = 1;
stack.buf = malloc(sizeof(struct buffer)*PAGE_STACK);
for (i=0; i < PAGE_STACK; i++)
init_buffer(stack.buf+i);
m.n_macros = 0;
m.n_pages = 1;
m.table = malloc(sizeof(struct macro)*PAGE_MACRO);
dnp = malloc(sizeof(char));
dnp[0] = '\0';
n_dnp = 1;
/* expand #0~, #1~, ... to the command line args */
for (i=1; i < argc; i++)
{
num = malloc(sizeof(char)*((i / 10) + 1+1));
sprintf(num, "`%'d", i-1);
val = malloc(sizeof(char)*(strlen(argv[i])+1));
strcpy(val, argv[i]);
add_macro_to_table(num,val);
}
expand_macros(stdin);
free_stack();
free_macro_table();
free(dnp);
return 0;
}