-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcpp4.c
More file actions
845 lines (790 loc) · 28.4 KB
/
cpp4.c
File metadata and controls
845 lines (790 loc) · 28.4 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
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
/******************************************************************************
Copyright (c) 2023 - present AinsleySnow
Copyright (c) 1999 Daniel Stenberg
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#include <stdio.h>
#include <ctype.h>
#include "cppdef.h"
#include "cpp.h"
FILE_LOCAL ReturnCode checkparm(struct Global *, int, DEFBUF *, int, bool);
FILE_LOCAL ReturnCode stparmscan(struct Global *, int);
FILE_LOCAL ReturnCode textput(struct Global *, char *);
FILE_LOCAL ReturnCode charput(struct Global *, int);
FILE_LOCAL ReturnCode expcollect(struct Global *);
FILE_LOCAL char *doquoting(char *, char *);
ReturnCode dodefine(struct Global *global)
{
/*
* Called from control when a #define is scanned. This module
* parses formal parameters and the replacement string. When
* the formal parameter name is encountered in the replacement
* string, it is replaced by a character in the range 128 to
* 128+NPARAM (this allows up to 32 parameters within the
* Dec Multinational range). If cpp is ported to an EBCDIC
* machine, you will have to make other arrangements.
*
* There is some special case code to distinguish
* #define foo bar
* from #define foo() bar
*
* Also, we make sure that
* #define foo foo
* expands to "foo" but doesn't put cpp into an infinite loop.
*
* A warning message is printed if you redefine a symbol to a
* different text. I.e,
* #define foo 123
* #define foo 123
* is ok, but
* #define foo 123
* #define foo +123
* is not.
*
* The following subroutines are called from define():
* checkparm called when a token is scanned. It checks through the
* array of formal parameters. If a match is found, the
* token is replaced by a control byte which will be used
* to locate the parameter when the macro is expanded. If the last
* parameter is variadic, the function directly replace all the
* occurances of the last parameter with the VA_ARGS magic cookie.
* textput puts a string in the macro work area (parm[]), updating
* parmp to point to the first free byte in parm[].
* textput() tests for work buffer overflow.
* charput puts a single character in the macro work area (parm[])
* in a manner analogous to textput().
*/
int c;
DEFBUF *dp; /* -> new definition */
int isredefine; /* true if redefined */
char *old; /* Remember redefined */
bool valast = false; /* If this field is set, the occurances of the last */
/* parameter will be replaced with magic cookie VA_ARG */
bool ellipsis = false; /* Have we seen the ellipsis? */
ReturnCode ret;
#if OK_CONCAT
int quoting; /* Remember we saw a # */
#endif
if (type[(c = skipws(global))] != LET)
{
cerror(global, ERROR_DEFINE_SYNTAX);
global->inmacro = false; /* Stop <newline> hack */
return (FPP_OK);
}
isredefine = false; /* Set if redefining */
if ((dp = lookid(global, c)) == NULL)
{ /* If not known now */
dp = defendel(global, global->tokenbuf, false); /* Save the name */
if (!dp)
return (FPP_OUT_OF_MEMORY);
}
else
{ /* It's known: */
isredefine = true; /* Remember this fact */
old = dp->repl; /* Remember replacement */
dp->repl = NULL; /* No replacement now */
}
global->parlist[0] = global->parmp = global->parm; /* Setup parm buffer */
if ((c = get(global)) == '(')
{ /* With arguments? */
global->nargs = 0; /* Init formals counter */
global->variadic = 0; /* and things for check whether the macro is variadic */
do
{ /* Collect formal parms */
if (global->nargs >= LASTPARM)
{
cfatal(global, FATAL_TOO_MANY_ARGUMENTS_MACRO);
return (FPP_TOO_MANY_ARGUMENTS);
}
else if ((c = skipws(global)) == ')')
break; /* Got them all */
else if (ellipsis) /* Something behind ellipsis? */
{
cerror(global, ERROR_MIDLIST_ELLIPSIS);
return FPP_OK;
}
if (c == '.')
{ // variadic macro
for (int i = 0; i < 2; ++i)
{
c = get(global);
if (c != '.')
goto error;
}
ellipsis = true;
global->variadic = true;
continue;
}
else if (type[c] != LET)
{ /* Bad formal syntax */
error:
cerror(global, ERROR_DEFINE_SYNTAX);
global->inmacro = false; /* Stop <newline> hack */
return (FPP_OK);
}
scanid(global, c); /* Get the formal param */
c = skipws(global); /* Trailing with ellipsis? */
if (c == '.') /* Got ya! Ellipsis! */
{
for (int k = 0; k < 2; ++k)
{
c = get(global);
if (c != '.')
goto error;
}
ellipsis = true;
valast = true;
}
else
unget(global);
global->parlist[global->nargs++] = global->parmp; /* Save its start */
ret = textput(global, global->tokenbuf); /* Save text in parm[] */
if (ret)
return (ret);
} while ((c = skipws(global)) == ','); /* Get another argument */
if (c != ')')
{ /* Must end at ) */
cerror(global, ERROR_DEFINE_SYNTAX);
global->inmacro = false; /* Stop <newline> hack */
return (FPP_OK);
}
c = ' '; /* Will skip to body */
}
else
{
/*
* DEF_NOARGS is needed to distinguish between
* "#define foo" and "#define foo()".
*/
global->nargs = DEF_NOARGS; /* No () parameters */
}
if (type[c] == SPA) /* At whitespace? */
c = skipws(global); /* Not any more. */
global->workp = global->work; /* Replacement put here */
global->inmacro = true; /* Keep \<newline> now */
quoting = 0; /* No # seen yet. */
/* Compile macro body */
while (c != EOF_CHAR && c != '\n')
{
#if OK_CONCAT
if (c == '#')
{ /* Token concatenation? */
if ((c = get(global)) != '#')
{ /* No, not really */
quoting = 1; /* Maybe quoting op. */
if (isspace(c))
c = skipws(global);
continue;
}
while (global->workp > global->work && type[global->workp[-1]] == SPA)
--global->workp; /* Erase leading spaces */
if (ret = save(global, TOK_SEP)) /* Stuff a delimiter */
return (ret);
c = skipws(global); /* Eat whitespace */
continue;
}
#endif
switch (type[c])
{
case LET:
#if OK_CONCAT
/* Might be a formal or magic identifiers (__VA_ARGS__ or __VA_OPT__) */
ret = checkparm(global, c, dp, quoting, valast);
#else
/* Might be a formal or magic identifiers (__VA_ARGS__ or __VA_OPT__) */
ret = checkparm(c, dp, valast);
#endif
if (ret)
return (ret);
break;
case DIG: /* Number in mac. body */
case DOT: /* Maybe a float number */
ret = scannumber(global, c, save); /* Scan it off */
if (ret)
return (ret);
break;
case QUO: /* String in mac. body */
ret = stparmscan(global, c);
if (ret)
return (ret);
break;
case BSH: /* Backslash */
ret = save(global, '\\');
if (ret)
return (ret);
if ((c = get(global)) == '\n')
global->wrongline = true;
ret = save(global, c);
if (ret)
return (ret);
break;
case SPA: /* Absorb whitespace */
/*
* Note: the "end of comment" marker is passed on
* to allow comments to separate tokens.
*/
if (global->workp[-1] == ' ') /* Absorb multiple */
break; /* spaces */
else if (c == '\t')
c = ' '; /* Normalize tabs */
/* Fall through to store character */
default: /* Other character */
ret = save(global, c);
if (ret)
return (ret);
break;
}
c = get(global);
quoting = 0; /* Only when immediately*/
/* preceding a formal */
}
global->inmacro = false; /* Stop newline hack */
unget(global); /* For control check */
if (global->workp > global->work && global->workp[-1] == ' ') /* Drop trailing blank */
global->workp--;
*global->workp = EOS; /* Terminate work */
dp->repl = savestring(global, global->work);/* Save the string */
dp->nargs = valast ?
global->nargs - 1 : global->nargs; /* Save arg count */
dp->variadic = global->variadic || valast; /* Save whether the macro is variadic */
if (isredefine)
{ /* Error if redefined */
if ((old != NULL && dp->repl != NULL && !streq(old, dp->repl)) ||
(old == NULL && dp->repl != NULL) ||
(old != NULL && dp->repl == NULL))
cerror(global, ERROR_REDEFINE, dp->name);
if (old != NULL) /* We don't need the */
free(old); /* old definition now. */
}
return (FPP_OK);
}
FILE_LOCAL
ReturnCode checkparm(struct Global *global,
int c,
DEFBUF *dp,
int quoting, /* Preceded by a # ? */
bool valast) /* The last formal trailed by ellipsis? */
{
/*
* Replace this param if it's defined. Note that the macro name is a
* possible replacement token. We stuff DEF_MAGIC in front of the token
* which is treated as a LETTER by the token scanner and eaten by
* the output routine. This prevents the macro expander from
* looping if someone writes "#define foo foo".
*/
int i;
char *cp;
ReturnCode ret = FPP_OK;
#if OK_CONCAT
if (quoting)
{ /* Special handling of */
ret = save(global, QUOTE_PARM); /* #formal inside defn */
if (ret)
return (ret);
}
#endif
scanid(global, c); /* Get parm to tokenbuf */
if (streq("__VA_ARGS__", global->tokenbuf))
return save(global, VA_ARGS);
else if (streq("__VA_OPT__", global->tokenbuf))
return save(global, VA_OPT);
for (i = 0; i < global->nargs; i++)
{ /* For each argument */
if (streq(global->parlist[i], global->tokenbuf))
{ /* If it's known */
if (i == global->nargs - 1 && valast) /* Variadic lastest parameter? */
ret = save(global, VA_ARGS);
else
ret = save(global, i + MAC_PARM); /* Save a magic cookie */
return (ret); /* And exit the search */
}
}
if (quoting) // This means... '#' is not followed by a parameter name
{
cerror(global, ERROR_NOT_PARAMETER_NAME, global->tokenbuf);
return FPP_OK;
}
if (streq(dp->name, global->tokenbuf)) /* Macro name in body? */
ret = save(global, DEF_MAGIC); /* Save magic marker */
for (cp = global->tokenbuf; *cp != EOS;) /* And save */
ret = save(global, *cp++); /* The token itself */
return (ret);
}
FILE_LOCAL
ReturnCode stparmscan(struct Global *global, int delim)
{
/*
* Normal string parameter scan.
*/
unsigned char *wp;
int i;
ReturnCode ret;
wp = (unsigned char *)global->workp; /* Here's where it starts */
ret = scanstring(global, delim, save);
if (ret)
return (ret); /* Exit on scanstring error */
global->workp[-1] = EOS; /* Erase trailing quote */
wp++; /* -> first string content byte */
for (i = 0; i < global->nargs; i++)
{
if (streq(global->parlist[i], (char *)wp))
{
*wp++ = MAC_PARM + PAR_MAC; /* Stuff a magic marker */
*wp++ = (i + MAC_PARM); /* Make a formal marker */
*wp = wp[-3]; /* Add on closing quote */
global->workp = (char *)wp + 1; /* Reset string end */
return (FPP_OK);
}
}
global->workp[-1] = wp[-1]; /* Nope, reset end quote. */
return (FPP_OK);
}
void doundef(struct Global *global)
/*
* Remove the symbol from the defined list.
* Called from the #control processor.
*/
{
int c;
if (type[(c = skipws(global))] != LET)
cerror(global, ERROR_ILLEGAL_UNDEF);
else
{
scanid(global, c); /* Get name to tokenbuf */
(void)defendel(global, global->tokenbuf, true);
}
}
FILE_LOCAL
ReturnCode textput(struct Global *global, char *text)
{
/*
* Put the string in the parm[] buffer.
*/
int size;
size = strlen(text) + 1;
if ((global->parmp + size) >= &global->parm[NPARMWORK])
{
cfatal(global, FATAL_MACRO_AREA_OVERFLOW);
return (FPP_WORK_AREA_OVERFLOW);
}
else
{
strcpy(global->parmp, text);
global->parmp += size;
}
return (FPP_OK);
}
FILE_LOCAL
ReturnCode charput(struct Global *global, int c)
{
/*
* Put the byte in the parm[] buffer.
*/
if (global->parmp >= &global->parm[NPARMWORK])
{
cfatal(global, FATAL_MACRO_AREA_OVERFLOW);
return (FPP_WORK_AREA_OVERFLOW);
}
*global->parmp++ = c;
return (FPP_OK);
}
/*
* M a c r o E x p a n s i o n
*/
ReturnCode expand(struct Global *global, DEFBUF *tokenp)
{
/*
* Expand a macro. Called from the cpp mainline routine (via subroutine
* macroid()) when a token is found in the symbol table. It calls
* expcollect() to parse actual parameters, checking for the correct number.
* It then creates a "file" containing a single line containing the
* macro with actual parameters inserted appropriately. This is
* "pushed back" onto the input stream. (When the get() routine runs
* off the end of the macro line, it will dismiss the macro itself.)
*/
int c;
FILEINFO *file;
ReturnCode ret = FPP_OK;
/*
* If no macro is pending, save the name of this macro
* for an eventual error message.
*/
if (global->recursion++ == 0)
global->macro = tokenp;
else if (global->recursion == RECURSION_LIMIT)
{
cerror(global, ERROR_RECURSIVE_MACRO, tokenp->name, global->macro->name);
if (global->rec_recover)
{
do
{
c = get(global);
} while (global->infile != NULL && global->infile->fp == NULL);
unget(global);
global->recursion = 0;
return (FPP_OK);
}
}
/*
* Here's a macro to expand.
*/
global->nargs = 0; /* Formals counter */
global->parmp = global->parm; /* Setup parm buffer */
switch (tokenp->nargs)
{
case (-2): /* __LINE__ */
if (global->infile->fp)
/* This is a file */
sprintf(global->work, "%d", global->line);
else
/* This is a macro! Find out the file line number! */
for (file = global->infile; file != NULL; file = file->parent)
{
if (file->fp != NULL)
{
sprintf(global->work, "%d", file->line);
break;
}
}
ret = ungetstring(global, global->work);
if (ret)
return (ret);
break;
case (-3): /* __FILE__ */
for (file = global->infile; file != NULL; file = file->parent)
{
if (file->fp != NULL)
{
sprintf(global->work, "\"%s\"", (file->progname != NULL) ? file->progname : file->filename);
ret = ungetstring(global, global->work);
if (ret)
return (ret);
break;
}
}
break;
case (-4): /* __FUNC__ */
sprintf(global->work, "\"%s\"", global->functionname[0] ? global->functionname : "<unknown function>");
ret = ungetstring(global, global->work);
if (ret)
return (ret);
break;
case (-5): /* __FUNC_LINE__ */
sprintf(global->work, "%d", global->funcline);
ret = ungetstring(global, global->work);
if (ret)
return (ret);
break;
default:
/*
* Nothing funny about this macro.
*/
if (tokenp->nargs < 0)
{
cfatal(global, FATAL_ILLEGAL_MACRO, tokenp->name);
return (FPP_ILLEGAL_MACRO);
}
while ((c = skipws(global)) == '\n') /* Look for (, skipping */
global->wrongline = true; /* spaces and newlines */
if (c != '(')
{
/*
* If the programmer writes
* #define foo() ...
* ...
* foo [no ()]
* just write foo to the output stream.
*/
unget(global);
cwarn(global, WARN_MACRO_NEEDS_ARGUMENTS, tokenp->name);
/* fputs(tokenp->name, stdout); */
Putstring(global, tokenp->name);
return (FPP_OK);
}
else if (!(ret = expcollect(global)))
{ /* Collect arguments */
if (tokenp->nargs != global->nargs && !tokenp->variadic)
{ /* Should be an error? */
cwarn(global, WARN_WRONG_NUMBER_ARGUMENTS, tokenp->name);
}
}
else
{ /* Collect arguments */
return (ret); /* We failed in argument colleting! */
}
/* fall through */
case DEF_NOARGS: /* No parameters just stuffs */
ret = expstuff(global, tokenp->name, tokenp->repl,
tokenp->nargs, global->nargs); /* expand macro */
} /* nargs switch */
return (ret);
}
FILE_LOCAL
ReturnCode expcollect(struct Global *global)
{
/*
* Collect the actual parameters for this macro.
*/
int c;
int paren; /* For embedded ()'s */
ReturnCode ret;
for (;;)
{
paren = 0; /* Collect next arg. */
while ((c = skipws(global)) == '\n') /* Skip over whitespace */
global->wrongline = true; /* and newlines. */
if (c == ')')
{ /* At end of all args? */
/*
* Note that there is a guard byte in parm[]
* so we don't have to check for overflow here.
*/
*global->parmp = EOS; /* Make sure terminated */
break; /* Exit collection loop */
}
else if (global->nargs >= LASTPARM)
{
cfatal(global, FATAL_TOO_MANY_ARGUMENTS_EXPANSION);
return (FPP_TOO_MANY_ARGUMENTS);
}
global->parlist[global->nargs++] = global->parmp; /* At start of new arg */
for (;; c = cget(global))
{ /* Collect arg's bytes */
if (c == EOF_CHAR)
{
cerror(global, ERROR_EOF_IN_ARGUMENT);
return (FPP_EOF_IN_MACRO); /* Sorry. */
}
else if (c == '\\')
{ /* Quote next character */
charput(global, c); /* Save the \ for later */
charput(global, cget(global)); /* Save the next char. */
continue; /* And go get another */
}
else if (type[c] == QUO)
{ /* Start of string? */
ret = scanstring(global, c, (ReturnCode(*)(struct Global *, int))charput); /* Scan it off */
if (ret)
return (ret);
continue; /* Go get next char */
}
else if (c == '(') /* Worry about balance */
paren++; /* To know about commas */
else if (c == ')')
{ /* Other side too */
if (paren == 0)
{ /* At the end? */
unget(global); /* Look at it later */
break; /* Exit arg getter. */
}
paren--; /* More to come. */
}
else if (c == ',' && paren == 0) /* Comma delimits args */
break;
else if (c == '\n') /* Newline inside arg? */
global->wrongline = true; /* We'll need a #line */
charput(global, c); /* Store this one */
} /* Collect an argument */
charput(global, EOS); /* Terminate argument */
} /* Collect all args. */
return (FPP_OK); /* Normal return */
}
#if OK_CONCAT
FILE_LOCAL
char *doquoting(char *to, char *from)
{
*to++ = '"';
while (*from)
{
if (*from == '\\' || *from == '"')
*to++ = '\\';
*to++ = *from++;
}
*to++ = '"';
return to;
}
#endif
#define CHK_OUT_OF_SPACE(s) \
do \
{ \
if ((defp + s) >= defend) \
{ \
cfatal(global, FATAL_OUT_OF_SPACE_IN_ARGUMENT, MacroName); \
return (FPP_OUT_OF_SPACE_IN_MACRO_EXPANSION); \
} \
} while (0)
FILE_LOCAL
ReturnCode dovaargs(struct Global *global, char *MacroName,
char **pdefp, char *defend, char quoting,
int listn, int argn)
{
char *defp = *pdefp;
if (quoting)
{
CHK_OUT_OF_SPACE(0);
*defp++ = '"';
}
for (int i = listn; i < argn - 1; ++i)
{
int size = strlen(global->parlist[i]);
CHK_OUT_OF_SPACE(size + 2);
strncpy(defp, global->parlist[i], size);
defp += size;
*defp++ = ',';
*defp++ = ' ';
}
int size = strlen(global->parlist[argn - 1]);
CHK_OUT_OF_SPACE(size);
strncpy(defp, global->parlist[argn - 1], size);
defp += size;
if (quoting)
{
CHK_OUT_OF_SPACE(1);
*defp++ = '"';
}
*defp = '\0';
*pdefp = defp;
return FPP_OK;
}
ReturnCode expstuff(struct Global *global,
char *MacroName,
char *MacroReplace,
int listn, // argument number in argument list
int argn) // argument number when using the macro
{
/*
* Stuff the macro body, replacing formal parameters by actual parameters.
* Note that we have to handle '#' but not '##' in this function.
*/
int c; /* Current character */
char *inp; /* -> repl string */
char *defp; /* -> macro output buff */
int size; /* Actual parm. size */
char *defend; /* -> output buff end */
int string_magic; /* String formal hack */
FILEINFO *file; /* Funny #include */
ReturnCode ret;
bool vaopt = false; /* in __VA_OPT__ */
bool skipopt = false; /* dump things in __VA_OPT__? */
int paren = 0; /* layer of parenthese seen in __VA_OPT__ */
#if OK_CONCAT
char quoting; /* Quote macro argument */
#endif
ret = getfile(global, NBUFF, MacroName, &file);
if (ret)
return (ret);
inp = MacroReplace; /* -> macro replacement */
defp = file->buffer; /* -> output buffer */
defend = defp + (NBUFF - 1); /* Note its end */
if (inp != NULL)
{
quoting = 0;
while ((c = (*inp++ & 0xFF)) != EOS)
{
#if OK_CONCAT
if (c == QUOTE_PARM)
{ /* Special token for # */
quoting = 1; /* set flag, for later */
continue; /* Get next character */
}
#endif
if (c == VA_OPT)
{
vaopt = 1;
skipopt = listn == argn;
continue;
}
if (vaopt)
{
if (c == '(')
++paren;
else if (c == ')')
--paren;
if (paren == 0)
{
vaopt = false;
skipopt = false;
continue;
}
else if (paren == 1 && c == '(')
continue;
if (skipopt)
continue;
}
if (c >= MAC_PARM && c <= (MAC_PARM + PAR_MAC))
{
string_magic = (c == (MAC_PARM + PAR_MAC));
if (string_magic)
c = (*inp++ & 0xFF);
/*
* Replace formal parameter by actual parameter string.
*/
if ((c - MAC_PARM) < global->nargs)
{
int i = c - MAC_PARM;
size = strlen(global->parlist[i]);
#if OK_CONCAT
if (quoting)
{
size++;
size *= 2; /* worst case condition */
}
#endif
CHK_OUT_OF_SPACE(size);
/*
* Erase the extra set of quotes.
*/
if (string_magic && defp[-1] == global->parlist[i][0])
{
strcpy(defp - 1, global->parlist[i]);
defp += (size - 2);
}
#if OK_CONCAT
else if (quoting)
defp = doquoting(defp, global->parlist[i]);
#endif
else
{
strcpy(defp, global->parlist[i]);
defp += size;
}
}
}
else if (c == VA_ARGS)
{
if (listn != argn)
{
ReturnCode ret = dovaargs(global, MacroName,
&defp, defend, quoting, listn, argn);
if (ret)
return (ret);
}
}
else if (defp >= defend)
{
cfatal(global, FATAL_OUT_OF_SPACE_IN_ARGUMENT, MacroName);
return (FPP_OUT_OF_SPACE_IN_MACRO_EXPANSION);
}
else
*defp++ = c;
quoting = 0;
}
}
*defp = EOS;
return (FPP_OK);
}
#undef CHK_OUT_OF_SPACE