-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathm4.c
2509 lines (1995 loc) · 58.7 KB
/
m4.c
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2023, 2024 Logan Ryan McLintock
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* An implementation of the m4 macro processor.
*
* Trust in the LORD with all your heart.
* Proverbs 3:5 GNT
*/
#include "toucanlib.h"
/* Number of buckets in hash table */
#define NUM_BUCKETS 1024
/*
* Macros can collect any number of arguments, but only args 0 to 9
* can be referenced. Arg 0 is the macro name.
*/
#define NUM_ARGS 10
#define INIT_BUF_SIZE 512
#define DEFAULT_LEFT_COMMENT "#"
#define DEFAULT_RIGHT_COMMENT "\n"
#define DEFAULT_LEFT_QUOTE "`"
#define DEFAULT_RIGHT_QUOTE "'"
/*
* Do not change.
* Diversion 0 continuously flushes to stdout.
* Diversion -1 is index 10 which is continuously discarded.
*/
#define NUM_DIVS 11
#define DIVERSION_NEGATIVE_1 10
/* Message */
#define ms(desc) fprintf(stderr, "%s:%lu [%s:%d]: %s: %s", \
m4->input->nm, (unsigned long) m4->input->rn, \
__FILE__, __LINE__, arg(0), desc)
/* Messagem, no arg zero */
#define ms_na0(desc) fprintf(stderr, "%s:%lu [%s:%d]: %s", \
m4->input->nm, (unsigned long) m4->input->rn, \
__FILE__, __LINE__, desc)
/* Usage warning */
#define uw(...) do { \
ms("Usage warning: "); \
fprintf(stderr, __VA_ARGS__); \
if (m4->warn_to_error) \
return USAGE_ERROR; \
} while (0)
/* Syntax warning */
#define sw(...) do { \
ms("Syntax warning: "); \
fprintf(stderr, __VA_ARGS__); \
if (m4->warn_to_error) \
return SYNTAX_ERROR; \
} while (0)
/* Usage error */
#define ue(...) do { \
ms("Usage error: "); \
fprintf(stderr, __VA_ARGS__); \
return USAGE_ERROR; \
} while (0)
/* Syntax error, without using m4 struct */
#define se(...) do { \
fprintf(stderr, "[%s:%d]: Syntax error: ", \
__FILE__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \
return SYNTAX_ERROR; \
} while (0)
/* User overflow error */
#define uofe do { \
ms("User overflow error\n"); \
return USER_OVERFLOW_ERROR; \
} while (0)
/* Location macro return - specifies location in file input */
#define l_mreturn(rv) do { \
ms("Error\n"); \
return (rv); \
} while (0)
/*
* When there is no stack, the output will be the active diversion.
* Otherwise, during argument collection, the output will be the store buffer.
* (Definitions and argument strings in the store are referenced by the stack).
*/
#define output (m4->stack == NULL ? m4->div[m4->active_div] : m4->store)
#define num_args_collected (m4->str_start->i - (m4->stack->m_i + 2))
#define m_def (m4->store->a + *(m4->str_start->a + m4->stack->m_i))
#define arg(n) (m4->store->a + *(m4->str_start->a + m4->stack->m_i + 1 + (n)))
struct macro_call {
Fptr mfp; /* Macro file pointer (built-ins) */
size_t m_i; /* Index into str_start */
size_t bracket_depth; /* Depth of unquoted brackets */
struct macro_call *next; /* For nested macro calls */
};
typedef struct m4_info *M4ptr;
struct m4_info {
int req_exit_val; /* User requested exit value */
struct ht *ht; /* Hash table for macros */
struct ht *trace_ht; /* Trace list hash table */
/* There is only one input. Characters are stored in reverse order. */
struct ibuf *input;
struct obuf *token;
/*
* store:
* def, macro name, arg 1, arg 2, def, macro name, arg 1, arg 2, ...
* ^ ^ ^ ^ ^ ^ ^ ^
* | | | | | | | |
* str_start
* ^ ^
* | |
* m_i m_i
*/
struct obuf *store;
struct sbuf *str_start; /* Indices to the start of strings in store */
struct macro_call *stack; /* Head node of the macro call stack */
size_t stack_depth; /* For trace */
Fptr tmp_mfp; /* For passing back the defn of a built-in */
/* Used for substituting arguments and for esyscmd and translit */
struct obuf *tmp;
struct obuf *wrap; /* Used for m4wrap */
struct obuf *div[NUM_DIVS];
size_t active_div;
char *left_comment;
char *right_comment;
size_t comment_on;
char *left_quote;
char *right_quote;
size_t quote_depth;
/*
* Pass through the name of a built-in macro to output when called without
* arguments. Otherwise an infinite loop would occur if the name was placed
* back in the input.
*/
int pass_through;
/*
* Delayed copy of the file pointer from inside the input. Used to detect
* file pointer changes to generate #line directives.
*/
FILE *sticky_fp;
int line_direct; /* Print #line directives for C preprocessor */
int tty_output; /* Indicates if stdout is a terminal */
int sys_val; /* Return value of last syscmd or esyscmd */
/* Exit upon user related error. Turned off by default. */
int error_exit; /* Exit upon the first error */
int warn_to_error; /* Treat warnings as errors */
int trace_on;
int help; /* Print help information for a macro */
};
/* Used for translit */
struct range {
unsigned char on;
unsigned char i;
unsigned char stop; /* Inclusive */
unsigned char decr;
};
void set_range(char **str, struct range *r)
{
char *p;
p = *str;
if (*p != '\0' && *(p + 1) == '-' && *(p + 2) != '\0') {
/* Range */
r->on = 1;
r->i = *p;
r->stop = *(p + 2);
if (r->stop < r->i)
r->decr = 1;
else
r->decr = 0;
/* Eat */
p += 3;
*str = p;
}
}
char read_range_ch(char **str, struct range *r)
{
char *p;
char ch;
if (!r->on)
set_range(str, r);
p = *str;
if (r->on) {
ch = r->i;
if (r->i == r->stop) {
r->on = 0;
} else {
if (r->decr)
--r->i;
else
++r->i;
}
} else {
ch = *p;
if (ch != '\0')
++p;
}
*str = p;
return ch;
}
struct macro_call *init_mc(void)
{
struct macro_call *mc;
if ((mc = calloc(1, sizeof(struct macro_call))) == NULL)
mreturn(NULL);
return mc;
}
int stack_mc(struct macro_call **head, size_t *stack_depth)
{
struct macro_call *mc;
if ((mc = init_mc()) == NULL)
mreturn(GEN_ERROR);
mc->next = *head;
*head = mc;
++*stack_depth;
return 0;
}
void pop_mc(struct macro_call **head, size_t *stack_depth)
{
struct macro_call *mc;
if (*head != NULL) {
mc = (*head)->next;
free(*head);
*head = mc;
--*stack_depth;
}
}
void free_mc_stack(struct macro_call **head)
{
size_t sd;
if (head != NULL)
while (*head != NULL)
pop_mc(head, &sd);
}
int sub_args(M4ptr m4)
{
const char *p;
char ch, next_ch;
size_t x, i;
char num[NUM_BUF_SIZE];
int r;
int all_args_accessed = 0;
char accessed[NUM_ARGS];
memset(accessed, 'N', NUM_ARGS);
m4->tmp->i = 0;
p = m_def;
while (1) {
ch = *p;
if (ch != '$') {
if (put_ch(m4->tmp, ch))
mreturn(GEN_ERROR);
} else {
next_ch = *(p + 1);
if (isdigit(next_ch)) {
/* $0 is the macro name. $1 to $9 are the collected args. */
x = next_ch - '0';
accessed[x] = 'Y';
/* Can only access args that were collected */
if (x > num_args_collected) {
uw("Uncollected argument number %lu accessed\n",
(unsigned long) x);
} else {
if (put_str(m4->tmp, arg(x)))
mreturn(GEN_ERROR);
}
++p; /* Eat an extra char */
} else if (next_ch == '#') {
/* $# is the number arguments collected */
r = snprintf(num, NUM_BUF_SIZE, "%lu",
(unsigned long) num_args_collected);
if (r < 0 || r >= NUM_BUF_SIZE)
mreturn(GEN_ERROR);
if (put_str(m4->tmp, num))
mreturn(GEN_ERROR);
++p; /* Eat an extra char */
} else if (next_ch == '*' || next_ch == '@') {
/*
* $* is all arguments, comma separated.
* $@ is the same, but the individual arguments are quoted.
*/
all_args_accessed = 1;
for (i = 1; i <= num_args_collected; ++i) {
if (next_ch == '@' && put_str(m4->tmp, m4->left_quote))
mreturn(GEN_ERROR);
if (put_str(m4->tmp, arg(i)))
mreturn(GEN_ERROR);
if (next_ch == '@'
&& put_str(m4->tmp, m4->right_quote))
mreturn(GEN_ERROR);
if (i != num_args_collected && put_ch(m4->tmp, ','))
mreturn(GEN_ERROR);
}
++p; /* Eat an extra char */
} else {
if (put_ch(m4->tmp, ch))
mreturn(GEN_ERROR);
}
}
if (ch == '\0')
break;
++p;
}
if (!all_args_accessed)
for (i = 1; i <= num_args_collected; ++i)
if ((i < NUM_ARGS && accessed[i] == 'N') || i >= NUM_ARGS)
uw("Collected argument number %lu not accessed\n",
(unsigned long) i);
if (unget_str(m4->input, m4->tmp->a))
mreturn(GEN_ERROR);
return 0;
}
int end_macro(M4ptr m4)
{
int ret;
char *nm;
if (m4->stack->mfp != NULL) {
if ((ret = (*m4->stack->mfp) (m4)))
ms("Failed\n");
} else {
ret = sub_args(m4);
}
/* Truncate */
m4->str_start->i = m4->stack->m_i;
m4->store->i = *(m4->str_start->a + m4->str_start->i);
/* Store the marco name */
nm = arg(0);
/* Pop redirectes output to the next node (if any) */
pop_mc(&m4->stack, &m4->stack_depth);
if (m4->pass_through) {
if (put_str(output, nm))
return GEN_ERROR;
m4->pass_through = 0;
}
return ret;
}
void free_m4(M4ptr m4)
{
size_t i;
if (m4 != NULL) {
free_ht(m4->ht);
free_ht(m4->trace_ht);
free_ibuf(m4->input);
free_obuf(m4->token);
free_obuf(m4->store);
free_sbuf(m4->str_start);
free_mc_stack(&m4->stack);
free_obuf(m4->tmp);
free_obuf(m4->wrap);
for (i = 0; i < NUM_DIVS; ++i)
free_obuf(m4->div[i]);
free(m4->left_comment);
free(m4->right_comment);
free(m4->left_quote);
free(m4->right_quote);
free(m4);
}
}
M4ptr init_m4(void)
{
M4ptr m4;
size_t i;
if ((m4 = calloc(1, sizeof(struct m4_info))) == NULL)
mreturn(NULL);
m4->req_exit_val = -1;
for (i = 0; i < NUM_DIVS; ++i)
m4->div[i] = NULL;
if ((m4->ht = init_ht(NUM_BUCKETS)) == NULL)
mgoto(error);
if ((m4->trace_ht = init_ht(NUM_BUCKETS)) == NULL)
mgoto(error);
if ((m4->token = init_obuf(INIT_BUF_SIZE)) == NULL)
mgoto(error);
if ((m4->store = init_obuf(INIT_BUF_SIZE)) == NULL)
mgoto(error);
if ((m4->str_start = init_sbuf(INIT_BUF_SIZE)) == NULL)
mgoto(error);
if ((m4->tmp = init_obuf(INIT_BUF_SIZE)) == NULL)
mgoto(error);
if ((m4->wrap = init_obuf(INIT_BUF_SIZE)) == NULL)
mgoto(error);
for (i = 0; i < NUM_DIVS; ++i)
if ((m4->div[i] = init_obuf(INIT_BUF_SIZE)) == NULL)
mgoto(error);
if ((m4->left_comment = strdup(DEFAULT_LEFT_COMMENT)) == NULL)
mgoto(error);
if ((m4->right_comment = strdup(DEFAULT_RIGHT_COMMENT)) == NULL)
mgoto(error);
if ((m4->left_quote = strdup(DEFAULT_LEFT_QUOTE)) == NULL)
mgoto(error);
if ((m4->right_quote = strdup(DEFAULT_RIGHT_QUOTE)) == NULL)
mgoto(error);
return m4;
error:
free_m4(m4);
mreturn(NULL);
}
void dump_stack(M4ptr m4)
{
struct macro_call *t;
size_t i, num_arg, j;
t = m4->stack;
i = m4->str_start->i;
fprintf(stderr, "Stack dump:\n");
while (t != NULL) {
num_arg = i - (t->m_i + 2);
fprintf(stderr, "%s macro:\n",
t->mfp == NULL ? "User-defined" : "Built-in");
fprintf(stderr, "Bracket depth: %lu\n",
(unsigned long) t->bracket_depth);
fprintf(stderr, "Def: %s\n",
m4->store->a + *(m4->str_start->a + t->m_i));
fprintf(stderr, "Macro: %s\n",
m4->store->a + *(m4->str_start->a + t->m_i + 1));
for (j = 1; j <= num_arg; ++j)
fprintf(stderr, "Arg %lu: %s\n", (unsigned long) j,
m4->store->a + *(m4->str_start->a + t->m_i + 1 + j));
i = t->m_i;
t = t->next;
}
}
int validate_quote_or_comment(M4ptr m4, const char *quote_or_comment)
{
size_t i;
char ch;
i = 0;
while (1) {
ch = *(quote_or_comment + i);
if (ch == '\0')
break;
/* All chars should be graph non-comma and non-parentheses */
if (!isgraph(ch) || ch == ',' || ch == '(' || ch == ')') {
uw("All characters in a quote or comment string "
"should be graph non-comma and non-parentheses: %s\n",
arg(1));
break;
}
++i;
}
return 0;
}
int validate_def(const char *def)
{
const char *p;
char ch, next_ch;
size_t x, i;
char present[NUM_ARGS];
memset(present, 'N', NUM_ARGS);
present[0] = 'Y'; /* Macro name is always present */
if (def == NULL)
return 0; /* OK */
p = def;
while (1) {
ch = *p;
if (ch == '\0') {
break;
} else if (ch == '$') {
next_ch = *(p + 1);
if (isdigit(next_ch)) {
x = next_ch - '0';
present[x] = 'Y';
++p; /* Eat an extra char */
}
}
++p;
}
/* Check for holes in argument references */
for (i = 0; i < NUM_ARGS; ++i)
if (i && present[i] == 'Y' && present[i - 1] == 'N')
return 1;
return 0;
}
int validate_macro_name(const char *macro_name)
{
const char *p;
char ch;
p = macro_name;
/* Check first character */
if (!isalpha(*p) && *p != '_')
se("Invalid macro name: %s\n", macro_name);
/* Check remaining characters */
++p;
while ((ch = *p) != '\0') {
if (!isalnum(ch) && ch != '_')
se("Invalid macro name: %s\n", macro_name);
++p;
}
return 0;
}
int add_macro(M4ptr m4, char *macro_name, char *macro_def, int push_hist)
{
/*
* Adds a user-defined macro.
* Need to check macro name fully as it might have been passed in on the
* command line.
*/
int r;
if ((r = validate_macro_name(macro_name)))
mreturn(r);
if (*macro_def == '\0' && m4->tmp_mfp != NULL) {
/* Passed back built-in macro function pointer from defn */
if (upsert(m4->ht, macro_name, NULL, m4->tmp_mfp, push_hist))
mreturn(GEN_ERROR);
m4->tmp_mfp = NULL;
} else {
/* User-defined text macro */
if (validate_def(macro_def))
sw("Macro definition has gaps in argument references\n");
if (upsert(m4->ht, macro_name, macro_def, NULL, push_hist))
mreturn(GEN_ERROR);
}
return 0;
}
int output_line_directive(M4ptr m4)
{
/*
* #line directives are tricky. They need to be printed whenever the
* underlying file pointer in the input changes. However, they can only be
* printed when the output is at the start of the line. So this then
* becomes related to the flushing of diversion 0. The easy solution is to
* only flush upon newline, so that an empty buffer indicates the
* start of the line.
* The #line directives also state what is to come. So they need to give
* information about the next read, not the current state. Furthermore,
* the default right comment token is \n. So \n might be intercepted by
* the comment checking, and thus, it is not a good idea to use the input
* as a flush trigger.
* Hence, #line directives need to be processed (this function called)
* after the input is read, but before the output is written.
*/
char num[NUM_BUF_SIZE];
int r;
/* Output at start of line and the file pointer has changed */
if (m4->line_direct
&& (!output->i || *(output->a + output->i - 1) == '\n')
&& m4->sticky_fp != m4->input->fp) {
r = snprintf(num, NUM_BUF_SIZE, "%lu",
(unsigned long) m4->input->rn);
if (r < 0 || r >= NUM_BUF_SIZE)
mgoto(error);
if (put_str(output, "#line "))
mgoto(error);
if (put_str(output, num))
mgoto(error);
if (put_str(output, " \""))
mgoto(error);
if (put_str(output, m4->input->nm))
mgoto(error);
if (put_str(output, "\"\n"))
mgoto(error);
m4->sticky_fp = m4->input->fp;
}
return 0;
error:
return GEN_ERROR;
}
#define print_help if (m4->help) { \
fprintf(stderr, "%s\n", PAR_DESC); \
return 0; \
}
#define allow_pass_through if (!num_args_collected) { \
m4->pass_through = 1; \
return 0; \
}
#define max_pars(n) if (num_args_collected > n) \
uw("Unused arguments collected: %s\n", PAR_DESC)
#define min_pars(n) if (num_args_collected < n) { \
fprintf(stderr, "%s:%lu [%s:%d]: Usage Error: " \
"Required arguments not collected: %s%s\n", \
m4->input->nm, (unsigned long) m4->input->rn, \
__FILE__, __LINE__, arg(0), PAR_DESC); \
return USAGE_ERROR; \
} \
/* ********** Built-in macros ********** */
/* See README.md for the syntax of the built-in macros */
#define NM define
#define PAR_DESC "(macro_name, macro_def)"
int econc(m4_, NM) (void *v) {
M4ptr m4 = (M4ptr) v;
print_help;
allow_pass_through;
max_pars(2);
min_pars(2);
return add_macro(m4, arg(1), arg(2), 0);
}
#undef NM
#undef PAR_DESC
#define NM pushdef
#define PAR_DESC "(macro_name, macro_def)"
int econc(m4_, NM) (void *v) {
M4ptr m4 = (M4ptr) v;
print_help;
allow_pass_through;
max_pars(2);
min_pars(2);
return add_macro(m4, arg(1), arg(2), 1);
}
#undef NM
#undef PAR_DESC
#define NM undefine
#define PAR_DESC "(macro_name)"
int econc(m4_, NM) (void *v) {
M4ptr m4 = (M4ptr) v;
print_help;
allow_pass_through;
max_pars(1);
min_pars(1);
if (delete_entry(m4->ht, arg(1), 0))
uw("Macro does not exist: %s\n", arg(1));
return 0;
}
#undef NM
#undef PAR_DESC
#define NM popdef
#define PAR_DESC "(macro_name)"
int econc(m4_, NM) (void *v) {
M4ptr m4 = (M4ptr) v;
print_help;
allow_pass_through;
max_pars(1);
min_pars(1);
/* Pop history */
if (delete_entry(m4->ht, arg(1), 1))
uw("Macro does not exist: %s\n", arg(1));
return 0;
}
#undef NM
#undef PAR_DESC
#define NM changecom
#define PAR_DESC "[(left_comment[, right_comment])]"
int econc(m4_, NM) (void *v) {
M4ptr m4 = (M4ptr) v;
char *lc, *rc;
char *tmp_lc = NULL, *tmp_rc = NULL;
print_help;
max_pars(2);
if (!num_args_collected) {
/* Disable comments */
free(m4->left_comment);
m4->left_comment = NULL;
free(m4->right_comment);
m4->right_comment = NULL;
return 0;
}
if (num_args_collected >= 1) {
if (*arg(1) == '\0')
ue("Empty left comment\n");
if (validate_quote_or_comment(m4, arg(1)))
uw("Poor choice of left comment: %s\n", arg(1));
lc = arg(1);
}
if (num_args_collected >= 2) {
if (*arg(2) == '\0')
ue("Empty right comment\n");
if (validate_quote_or_comment(m4, arg(2)))
uw("Poor choice of right comment: %s\n", arg(2));
rc = arg(2);
} else {
rc = DEFAULT_RIGHT_COMMENT;
}
/* Comments should not be the same */
if (!strcmp(lc, rc))
uw("Left and right comments should not be the same\n");
if ((tmp_lc = strdup(lc)) == NULL)
mreturn(GEN_ERROR);
if ((tmp_rc = strdup(rc)) == NULL) {
free(tmp_lc);
l_mreturn(GEN_ERROR);
}
free(m4->left_comment);
m4->left_comment = tmp_lc;
free(m4->right_comment);
m4->right_comment = tmp_rc;
return 0;
}
#undef NM
#undef PAR_DESC
#define NM changequote
#define PAR_DESC "[(left_quote, right_quote)]"
int econc(m4_, NM) (void *v) {
M4ptr m4 = (M4ptr) v;
char *lq, *rq;
char *tmp_lq = NULL, *tmp_rq = NULL;
print_help;
max_pars(2);
if (num_args_collected >= 2) {
if (*arg(1) == '\0')
ue("Empty left quote\n");
if (validate_quote_or_comment(m4, arg(1)))
uw("Poor choice of left quote: %s\n", arg(1));
if (*arg(2) == '\0')
ue("Empty right quote\n");
if (validate_quote_or_comment(m4, arg(2)))
uw("Poor choice of right quote: %s\n", arg(2));
/* Quotes should not be the same */
if (!strcmp(arg(1), arg(2)))
uw("Left and right quotes should not be the same\n");
lq = arg(1);
rq = arg(2);
} else {
lq = DEFAULT_LEFT_QUOTE;
rq = DEFAULT_RIGHT_QUOTE;
}
if ((tmp_lq = strdup(lq)) == NULL)
mreturn(GEN_ERROR);
if ((tmp_rq = strdup(rq)) == NULL) {
free(tmp_lq);
l_mreturn(GEN_ERROR);
}
free(m4->left_quote);
m4->left_quote = tmp_lq;
free(m4->right_quote);
m4->right_quote = tmp_rq;
return 0;
}
#undef NM
#undef PAR_DESC
#define NM shift
#define PAR_DESC "(arg1[, ... ])"
int econc(m4_, NM) (void *v) {
M4ptr m4 = (M4ptr) v;
size_t i;
print_help;
if (!num_args_collected) {
m4->pass_through = 1;
return 0;
}
/*
* $@ comma separated quoted args, except for the first.
* Needs to be done in reverse as ungetting.
*/
for (i = num_args_collected; i >= 2; --i) {
if (unget_str(m4->input, m4->right_quote))
mreturn(GEN_ERROR);
if (unget_str(m4->input, arg(i)))
mreturn(GEN_ERROR);
if (unget_str(m4->input, m4->left_quote))
mreturn(GEN_ERROR);
if (i != 2 && unget_ch(m4->input, ','))
mreturn(GEN_ERROR);
}
return 0;
}
#undef NM
#undef PAR_DESC
#define NM divert
#define PAR_DESC "[(div_num)]"
int econc(m4_, NM) (void *v) {
M4ptr m4 = (M4ptr) v;
print_help;
max_pars(1);
if (num_args_collected >= 1) {
if (!strcmp(arg(1), "-1")) {
m4->active_div = 10;
return 0;
}
if (strlen(arg(1)) == 1 && isdigit(*arg(1))) {
m4->active_div = *arg(1) - '0';
return 0;
}
} else {
m4->active_div = 0;
return 0;
}
mreturn(GEN_ERROR);
}
#undef NM
#undef PAR_DESC
#define NM undivert
#define PAR_DESC "[(div_num_or_filename)]"
int econc(m4_, NM) (void *v) {
M4ptr m4 = (M4ptr) v;
char ch, *p;
size_t x, i;
print_help;
if (num_args_collected) {
for (i = 1; i <= num_args_collected; ++i) {
ch = *arg(i);
if (ch == '\0') {
ue("Argument is empty string\n");
} else if (isdigit(ch) && strlen(arg(i)) == 1
&& (x = ch - '0') != m4->active_div) {
if (put_obuf(m4->div[m4->active_div], m4->div[x]))
mreturn(GEN_ERROR);
} else {
p = arg(i);
while (isdigit(*p++));
if (*p == '\0')
ue("Invalid diversion number\n");
/*
* Assume a filename. Outputs directly to the active diversion,
* even during argument collection.
*/
if (put_file(m4->div[m4->active_div], arg(i)))
mreturn(GEN_ERROR);
}
}
} else {
/* No args, so undivert all into the current diversion */
for (i = 0; i < NUM_DIVS - 1; ++i)