summaryrefslogtreecommitdiffabout
path: root/mfd/lex.l
blob: c1f9c3e85811d07126e6727742cd789bbd17ae06 (plain)
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
%{
/* This file is part of Mailfromd.
   Copyright (C) 2005, 2006, 2007, 2008 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   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 <http://www.gnu.org/licenses/>. */

#define MF_SOURCE_NAME  MF_SOURCE_LEX
	
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>	
#include <sys/stat.h>
#define obstack_chunk_alloc malloc
#define obstack_chunk_free free
#include <obstack.h>
#include "mailfromd.h"
#include "gram.h"
#include "prog.h"
	
static struct locus locus; /* Current input location */
static struct locus start_locus; /* Location when the last state switch
				    occurred */
static struct obstack string_stk; /* Obstack for constructing string values */
static char *multiline_delimiter; /* End of here-document delimiter */ 
static size_t multiline_delimiter_len; /* Length of multiline_delimiter_len */
static int multiline_unescape;         /* Unescape here-document contents */
static int (*char_to_strip)(char);     /* Strip matching characters of each
					  here-document line */

static int
is_tab(char c)
{
	return c == '\t';
}
 
static int
is_space(char c)
{
	return c == '\t' || c == ' ';
}

#define line_begin string_begin
#define line_add string_add
#define line_add_char string_add_char
static void line_finish(void);
static void string(const char *str, size_t len);
static int isemptystr(char *text);
static int builtin_const(const char *s);

/* External interface to locus */ 
const struct locus *
get_locus()
{
	return &locus;
}

/* Auxiliary function for returning keyword tokens */ 
static int
keyword(int kw)
{
	yylval.locus = locus;
	return kw;
}


/* Input stack support */

#undef YY_INPUT
#define YY_INPUT(buf,result,max_size) do {                  \
	if (ext_pp)                                         \
		result = fread(buf, 1, max_size, yyin);     \
	else                                                \
		result = pp_fill_buffer(buf, max_size);     \
} while (0)

static int
variable_or_const()
{
	union {
		struct variable *vptr;
		struct constant *cptr;
	} v;
	const struct value *value_ptr;

	switch (variable_or_constant_lookup(yylval.literal->text, (void**)&v)) {
	case SYM_UNDEF:
		parse_error(_("Variable %s is not defined"),
			    yylval.literal->text);
		return BOGUS;
		
	case SYM_VARIABLE:
		yylval.var = v.vptr;
		return VARIABLE;

	case SYM_CONSTANT:
		value_ptr = &v.cptr->value;
		switch (value_ptr->type) {
		case dtype_number:
			yylval.number = value_ptr->v.number;
			return NUMBER;

		case dtype_string:
			yylval.literal = value_ptr->v.literal;
			return STRING;
			
		default:
			abort();
		}
	}
}
	

static size_t input_line;

static void
advance_line()
{
	++locus.line;
	++input_line;
}

static int
is_stdin(const char *name)
{
	return name == NULL
		|| strcmp(name, "stdin") == 0
		|| strcmp(name, "<stdin>") == 0
		|| strcmp(name, "-") == 0;
}

#define BEGIN_X(s) do { BEGIN(s); start_locus = locus; } while(0)

%}

/* Exclusive states:

   COMMENT        Within a C-style comment;
   STR            Processing a complex string;
   ML             Within a multi-line aggregator.  The line being built
                  requires stripping leading whitespace (if requested).
   CML            Continuation within a multi-line aggregator.  The line
                  being built does not require stripping leading whitespace.
   QML            Quoted multi-line aggregator.  No variable substitution and
                  unquoting is needed.

   Inclusive states:
   
   ONBLOCK	  Lexical tie-in after an `on' keyword.  In ONBLOCK state
                  the strigns `as', `host', `from', and `poll' are
		  recognized as keywords.  The string `for' also acquires
		  special meaning.
*/

%x COMMENT STR ML CML QML
%s ONBLOCK

N [0-9][0-9]*
P [1-9][0-9]*
X [0-9a-fA-F]
O [0-7]
WS [ \t][ \t]*
IDENT [a-zA-Z_][a-zA-Z_0-9]*
LOCUS __file__|__line__|__function__
VCONST __package__|__version__|__major__|__minor__|__patch__
STATEDIR __(def)?statedir__
PREPROC  __(def)?preproc__	
ICONST {LOCUS}|{VCONST}|{STATEDIR}|{PREPROC}
%%
         /* C-style comments */
"/*"         BEGIN_X(COMMENT);
<COMMENT>[^*\n]*        /* eat anything that's not a '*' */
<COMMENT>"*"+[^*/\n]*   /* eat up '*'s not followed by '/'s */
<COMMENT>\n             advance_line();
<COMMENT>"*"+"/"        BEGIN(INITIAL);
         /* Configuration directives */
^[ \t]*#[ \t]*{P}[ \t]+\".*\".*\n parse_line_cpp(yytext, &locus);
        /* Normally, everything within a comment should be ignored, so
	   the exclusive condition for the rule below is an error.
	   Unfortunately, GNU m4 (versions up to 1.4.9) outputs line
	   synchronisation directives in comments, which makes any decent
	   compiler lost trace of which input line it is on.  To avoid
	   this, mfd handles #line directives even within a C-comment
	   block.

	   The bug report with a patch was sent to bug-m4@gnu.org on
	   2007-05-24:

	     http://lists.gnu.org/archive/html/bug-m4/2007-05/msg00018.html
	     
	   Hopefully it will be fixed in the next release.
	*/
<INITIAL,COMMENT>^[ \t]*#[ \t]*line[ \t].*\n {
	struct locus newloc;
	memset(&newloc, 0, sizeof newloc);
	parse_line(yytext, &newloc);

	if (is_stdin(newloc.file)) {
		if (newloc.line > input_line)
			locus.line += newloc.line - input_line;
		else {
			size_t diff = input_line - newloc.line;
			if (diff < locus.line)
				locus.line -= diff;
		}
		input_line = newloc.line;
	} else {
		locus = newloc;
		input_line++;
	}
}
^[ \t]*#[ \t]*pragma[ \t].*/\n parse_pragma(yytext);
^[ \t]*#[ \t]*error[ \t].*\n  { parse_error("%s", yytext); advance_line(); }
         /* End-of-line comments */
#.*\n   { advance_line(); }
#.*     /* end-of-file comment */;
         /* Reserved words */
accept    return keyword(ACT_ACCEPT);
reject    return keyword(ACT_REJECT);
tempfail  return keyword(ACT_TEMPFAIL);
continue  return keyword(ACT_CONTINUE);
discard   return keyword(ACT_DISCARD);
add       return keyword(ADD);
replace   return keyword(REPLACE);
delete    return keyword(DELETE);
if        return keyword(IF);
fi        return keyword(FI);
else      return keyword(ELSE);
elif      return keyword(ELIF);
on        return keyword(ON);
do        return keyword(DO);
done      return keyword(DONE);
matches   return keyword(MATCHES);
fnmatches return keyword(FNMATCHES);
mx{WS}matches return keyword(MXMATCHES);
mx{WS}fnmatches return keyword(MXFNMATCHES);
when      return keyword(WHEN);
or        return keyword(OR);
and       return keyword(AND);
not       return keyword(NOT);
next      return keyword(NEXT);
prog      return keyword(PROG);
set       return keyword(SET);
catch     return keyword(CATCH);
echo      return keyword(KW_ECHO);
return    return keyword(RETURN);
returns   return keyword(RETURNS);
func      return keyword(FUNC);
switch    return keyword(SWITCH);
case      return keyword(CASE);
default   return keyword(DEFAULT);
string    { yylval.type = dtype_string; return TYPE; }
number    { yylval.type = dtype_number; return TYPE; }
const     return keyword(CONST);
throw     return keyword(THROW);
loop      return keyword(LOOP);
while     return keyword(WHILE);
for       return keyword(FOR);
break     return keyword(BREAK);
pass      return keyword(PASS);
begin     return keyword(KW_BEGIN);
end       return keyword(KW_END);
alias     return keyword(ALIAS);
vaptr     return keyword(VAPTR);

{ICONST}   { return builtin_const(yytext); } 
<ONBLOCK>poll      return keyword(POLL);
<ONBLOCK>host      return keyword(HOST);
<ONBLOCK>as        return keyword(AS);
<ONBLOCK>from      return keyword(FROM);
         /* Variables */
<INITIAL,ONBLOCK,ML,CML,STR>\%({ICONST}) { return builtin_const(yytext+1); }
<INITIAL,ONBLOCK,ML,CML,STR>\%{IDENT}     {
	string(yytext + 1, yyleng - 1);
	return variable_or_const();
}
<INITIAL,ONBLOCK,ML,CML,STR>\%\{{IDENT}\} {
	string(yytext + 2, yyleng - 3);
	return variable_or_const();
}

         /* Positional arguments */
<INITIAL,ONBLOCK,ML,CML,STR>\$# {
	return ARGCOUNT;
}
<INITIAL,ONBLOCK,ML,CML,STR>\${P}        {
	yylval.number = strtol(yytext + 1, NULL, 0);
	return ARG; }
<INITIAL,ONBLOCK>\$/"("        { return keyword(ARGX); }
         /* Sendmail variables */
<INITIAL,ONBLOCK,ML,CML,STR>\${IDENT} {
	        if (yyleng == 2)
	           string(yytext + 1, 1);
                else {
                   line_begin();
                   line_add("{", 1);
                   line_add(yytext + 1, yyleng - 1);
                   line_add("}", 1);
                   line_finish();
                }
                return SYMBOL;
              }
<INITIAL,ONBLOCK,ML,CML,STR>\$\{{IDENT}\} {
	string(yytext+1, yyleng - 1); return SYMBOL; }
         /* Back-references */
<INITIAL,ONBLOCK,ML,CML,STR>\\{P} {
	 yylval.number = strtoul(yytext+1, NULL, 0);
	 return BACKREF; }
         /* Numeric strings */
0[xX]{X}{X}*  { yylval.number = strtoul(yytext, NULL, 16); return NUMBER; };
0{O}{O}*      { yylval.number = strtoul(yytext, NULL, 8); return NUMBER; };
0|{P}         { yylval.number = strtoul(yytext, NULL, 10); return NUMBER; };
         /* Strings */
{IDENT}  {
	int paren_follows = 0;
	enum { is_ident, is_builtin, is_func } state;

	if (yylval.builtin = builtin_lookup(yytext))
		state = is_builtin;
	else if (yylval.function = function_lookup(yytext))
		state = is_func;
	else
		state = is_ident;

	if (state != is_ident) {
		int c;
		
		while ((c = input()) && c_isascii(c) && c_isspace(c))
			if (c == '\n')
				advance_line();
	
		paren_follows = (c == '(');
		unput(c);
	}
	
	if (state == is_builtin) 
		return yylval.builtin->rettype == dtype_unspecified ?
			BUILTIN_PROC :
			(paren_follows ? BUILTIN : BUILTIN_P);
	else if (state == is_func)
		return yylval.function->rettype == dtype_unspecified ?
			FUNCTION_PROC :
			(paren_follows ? FUNCTION : FUNCTION_P);
	else {
		string(yytext, yyleng);
		return IDENTIFIER;
	}
  }
'[^\n']*'             { string(yytext+1, yyleng-2); return STRING; }
\"[^\\\"$%\n]*\"      { string(yytext+1, yyleng-2); return STRING; }
\"[^\\\"$%\n]*\\\n    { advance_line();
                        BEGIN_X(STR);
                        line_begin();
                        line_add(yytext + 1, yyleng - 3); }     
\"[^\\\"$%\n]*/[\\$%] { BEGIN_X(STR);
                        string(yytext+1, yyleng-1);
                        line_begin();
                        return STRING; }
\"\\x{X}{X}/[\\$%] {
         BEGIN_X(STR);
         line_add_char(strtoul(yytext + 3, NULL, 16));
         line_finish();
         return STRING;
}
\"\\x{X}{X} {
         BEGIN_X(STR);
         line_add_char(strtoul(yytext + 3, NULL, 16));
}

\"\\0{O}{1,3}/[\\$%] {
         BEGIN_X(STR);  
         line_add_char(strtoul(yytext + 3, NULL, 8));
         line_finish();
         return STRING;
}
\"\\0{O}{1,3} {
         BEGIN_X(STR);  
         line_add_char(strtoul(yytext + 3, NULL, 8));
}

\"\\[^1-9]/[\\$%]  {
         BEGIN_X(STR);
         line_add_char(mu_argcv_unquote_char(yytext[2]));
         line_finish();
         return STRING;
}
\"\\[^1-9]  {
         BEGIN_X(STR);
         line_add_char(mu_argcv_unquote_char(yytext[2]));
}
<STR>[^\\\"$%\n]*\\\n { advance_line(); line_add(yytext, yyleng - 2); }
<STR>[^\\\"$%\n]*\"   { BEGIN(INITIAL);
                        if (yyleng > 1) 
                          line_add(yytext, yyleng - 1); 
                        line_finish();
		        return STRING; }
<STR>[^\\\"$%\n]+/[\\$%] {
                       line_add(yytext, yyleng);
                       line_finish();
                       line_begin();
                       return STRING;
                 }
<STR,ML,CML>\\x{X}{X}/[\\$%] {
         line_add_char(strtoul(yytext + 2, NULL, 16));
         line_finish();
         line_begin();
         return STRING;
}
<STR,ML,CML>\\x{X}{X} {
         line_add_char(strtoul(yytext + 2, NULL, 16));
}
<STR,ML,CML>\\0{O}{1,3}/[\\$%] {
         line_add_char(strtoul(yytext + 2, NULL, 8));
         line_finish();
         line_begin();
         return STRING;
}
<STR,ML,CML>\\0{O}{1,3} {
         line_add_char(strtoul(yytext + 2, NULL, 8));
}
<STR,ML,CML>\\[^1-9]/[\\$%] {
         line_add_char(mu_argcv_unquote_char(yytext[1]));
         line_finish();
         line_begin();
         return STRING;
}
<STR,ML,CML>\\[^1-9] {
         line_add_char(mu_argcv_unquote_char(yytext[1]));
}

         /* Multi-line strings */
"<<"(-" "?)?\\?{IDENT}[ \t]*.*\n |
"<<"(-" "?)?'{IDENT}'[ \t]*.*\n {
        char *p;

        advance_line();
        char_to_strip = NULL;
        multiline_unescape = 1;

        line_begin();
        p = yytext + 2;
        if (*p == '-') {
            ++p;
            if (*p == ' ') {
                ++p;
                char_to_strip = is_space;
            } else
                char_to_strip = is_tab;
        }
        
        if (*p == '\\') {
            p++;
            multiline_unescape = 0;
        }
        if (*p == '\'') {
            char *q;
 
            p++;
            multiline_unescape = 0;
            q = strchr(p, '\'');
            multiline_delimiter_len = q - p;
        } else
            multiline_delimiter_len = strcspn(p, " \t");

        multiline_delimiter = xmalloc(multiline_delimiter_len + 1);
        memcpy(multiline_delimiter, p, multiline_delimiter_len);
        multiline_delimiter[multiline_delimiter_len] = 0;
        if (multiline_unescape)
            BEGIN_X(ML);
        else
            BEGIN_X(QML);
}
        /* Quoted multilines */
<QML>[^\\\n]*\n {
        char *p;

        advance_line();
        p = yytext;
        if (char_to_strip)
               for (; char_to_strip (*p); p++)
                      ;

        if (strlen(p) >= multiline_delimiter_len
            && memcmp(p, multiline_delimiter, multiline_delimiter_len) == 0
            && isemptystr(p + multiline_delimiter_len)) {
	       free (multiline_delimiter);
	       multiline_delimiter = NULL;
               multiline_delimiter_len = 0;
	       BEGIN(INITIAL);
               line_finish();
               return STRING;
        }
        line_add(p, strlen(p));
}

        /* Unquoted multilines */
<ML>[^\\$%\n]+/[\\$%] {
        char *p = yytext;
        if (char_to_strip)
               for (; char_to_strip (*p); p++)
                      ;
        line_add(p, strlen(p));
        line_finish();
        BEGIN(CML);
        return STRING;
}
<CML>[^\\$%\n]+/[\\$%] {
        line_add(yytext, yyleng);
        line_finish();
        line_begin();
        return STRING;
}
<CML,STR>"%%"|"$$" {
        line_add(yytext, 1);
        line_finish();
        return STRING;
}
<ML,CML,STR>[$%] {
        line_add(yytext, yyleng);
}
<ML>[^\\$%\n]*\n {
        char *p;

        advance_line();
        p = yytext;
        if (char_to_strip)
               for (; char_to_strip (*p); p++)
                      ;

        if (strlen(p) >= multiline_delimiter_len
            && memcmp(p, multiline_delimiter, multiline_delimiter_len) == 0
            && isemptystr(p + multiline_delimiter_len)) {
	       free (multiline_delimiter);
	       multiline_delimiter = NULL;
               multiline_delimiter_len = 0;
	       BEGIN(INITIAL);
               line_finish();
               return STRING;
        }
        line_add(p, strlen(p));
}
<CML>[^\\$%\n]*\n {
        advance_line();
        if (yyleng >= multiline_delimiter_len
            && memcmp(yytext, multiline_delimiter,
                      multiline_delimiter_len) == 0
            && isemptystr(yytext + multiline_delimiter_len)) {
	       free (multiline_delimiter);
	       multiline_delimiter = NULL;
               multiline_delimiter_len = 0;
	       BEGIN(INITIAL);
               line_finish();
               return STRING;
        }
        line_add(yytext, yyleng);
        BEGIN_X(ML);
}
         /* Other tokens */
{WS}     ;
\n { advance_line(); }
"="|"==" return keyword(EQ);
"!=" return keyword(NE);
"<"  return keyword(LT);
"<=" return keyword(LE);
">"  return keyword(GT);
">=" return keyword(GE);
"&"  return keyword(LOGAND);
"|"  return keyword(LOGOR);
"^"  return keyword(LOGXOR);
"~"  return keyword(LOGNOT);
"..." return keyword(DOTS);
<ML,QML>.   /* If a here-document is not closed and its next line does not
               end with a \n, prevent it from being displayed by ECHO */;
. return yytext[0];

%%

void
init_string_space()
{
	obstack_init(&string_stk);
}

void
free_string_space()
{
	obstack_free(&string_stk, NULL);
}

char *
mf_strdup(const char *str)
{
	string_add(str, strlen(str) + 1);
	return obstack_finish(&string_stk);
}

struct literal *
string_alloc(const char *str, size_t len)
{
	string_begin();
	string_add(str, len);
	return string_finish();
}

static void
string(const char *str, size_t len)
{
	yylval.literal = string_alloc(str, len);
}

void
string_begin()
{
	/* nothing */
}

struct literal *
string_finish()
{
	char *ptr;
	struct literal *lit;
	obstack_1grow(&string_stk, 0);
	ptr = obstack_finish(&string_stk);
	lit = literal_lookup(ptr);
	if (lit->text != ptr)
		obstack_free(&string_stk, ptr);
	return lit;
}

static void
line_finish()
{	
	yylval.literal = string_finish();
	if (yy_flex_debug)
		fprintf(stderr, "constructed line: %s\n",
			yylval.literal->text);
}		

void
string_add(const char *str, size_t len)
{
	obstack_grow(&string_stk, str, len);
}

void
string_add_char(unsigned char c)
{
	obstack_1grow(&string_stk, c);
}

static void
print_parse_message(const struct locus *locus,
		    const char *prefix, const char *fmt, va_list ap)
{
	int n;
	char buf[512]; /* FIXME */

	if (locus && locus->file)
		n = snprintf(buf, sizeof buf, "%s:%lu: ",
			     locus->file, (unsigned long) locus->line);
	else
		n = 0;
	if (prefix) 
		n += snprintf(buf + n, sizeof buf - n, "%s: ", prefix);
	vsnprintf(buf + n, sizeof buf - n, gettext(fmt), ap);
	mu_error(buf);
}

void
parse_warning(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	print_parse_message(&locus, _("Warning"), fmt, ap);
	va_end(ap);
}

void
parse_warning_locus(const struct locus *loc, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	print_parse_message(loc, _("Warning"), fmt, ap);
	va_end(ap);
}

void
parse_error(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	print_parse_message(&locus, NULL, fmt, ap);
	va_end(ap);
	error_count++;
}

void
parse_error_locus(const struct locus *loc, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	print_parse_message(loc, NULL, fmt, ap);
	va_end(ap);
	error_count++;
}

static pid_t pp_pid;

int
source(char *name)
{
	if (ext_pp) {
		int fd;
		char *ppcmd;
		int argc;
		char **argv;
		
		fd = open(name, O_RDONLY);
		if (fd == -1) {
			mu_error(_("Cannot open `%s': %s"),
				 name, mu_strerror(errno));
			return EX_NOINPUT;
		}
		close(fd);

		pp_make_argcv(&argc, &argv);
		yyin = pp_extrn_start(argc, argv, &pp_pid);
		if (!yyin) {
			mu_error(_("Unable to start external preprocessor `%s': %s"),
				 ppcmd,
				 mu_strerror(errno));
			return EX_OSFILE;
		}
	} else 
		return pp_init(name) ? EX_NOINPUT : EX_OK;

	return EX_OK;
}

int
yywrap()
{
	if (yyin)
 	        pp_extrn_shutdown(pp_pid);
	else
		pp_done();

	switch (YYSTATE) {
	case INITIAL:
		/* ok */
		break;

	case COMMENT:
		parse_error_locus(&start_locus, _("End of file in comment"));
		break;

	case STR:
	case ML:
	case CML:
	case QML:
		parse_error_locus(&start_locus, _("End of file in string"));
	}
	
	locus.file = NULL;
	return 1;
}

static int
isemptystr(char *text)
{
	for (; *text && c_isspace (*text); text++)
		;
	return *text == 0;
}

void
tie_in_onblock(int enable)
{
	if (enable)
		BEGIN(ONBLOCK);
	else
		BEGIN(INITIAL);
}

static int
builtin_const(const char *s)
{
	if (strcmp(s, "__file__") == 0) {
		yylval.literal = literal_lookup(locus.file);
		return STRING;
	} else if (strcmp(s, "__line__") == 0) {
		yylval.number = locus.line;
		return NUMBER;
	} else if (strcmp(s, "__function__") == 0) {
		s = function_name();
		string(s, strlen(s));
		return STRING;
	} else if (strcmp(s, "__package__") == 0) {
		string(PACKAGE_TARNAME, sizeof PACKAGE_TARNAME - 1);
		return STRING;
	} else if (strcmp(s, "__version__") == 0) {
		string(PACKAGE_VERSION, sizeof PACKAGE_VERSION - 1);
		return STRING;
	} else if (strcmp(s, "__major__") == 0) {
		yylval.number = MAILFROMD_VERSION_MAJOR;
		return NUMBER;
	} else if (strcmp(s, "__minor__") == 0) {
		yylval.number = MAILFROMD_VERSION_MINOR;
		return NUMBER;
	} else if (strcmp(s, "__patch__") == 0) {
		yylval.number = MAILFROMD_VERSION_PATCH;
		return NUMBER;
	} else if (strcmp(s, "__statedir__") == 0) {
                string(mailfromd_state_dir, strlen(mailfromd_state_dir));
		return STRING;
	} else if (strcmp(s, "__defstatedir__") == 0) {
		string(DEFAULT_STATE_DIR, sizeof DEFAULT_STATE_DIR - 1);
		return STRING;
	} else if (strcmp(s, "__preproc__") == 0) {
		if (!ext_pp) 
			string("", 0);
		else
			string(ext_pp, strlen(ext_pp));
		return STRING;
	} else if (strcmp(s, "__defpreproc__") == 0) {
		if (DEFAULT_PREPROCESSOR)
			string(DEFAULT_PREPROCESSOR,
			       sizeof DEFAULT_PREPROCESSOR - 1);
		else
			string("", 0);
		return STRING;
	}
	abort();
}

/* End of lex.l */

Return to:

Send suggestions and report system problems to the System administrator.