aboutsummaryrefslogtreecommitdiff
path: root/src/lex.l
blob: 2964633530d77c7fae1c0c129e54a1e72c3edd36 (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
%{
/* This file is part of mailfromd.
   Copyright (C) 2005, 2006, 2007 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 2, 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, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301 USA */

#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"
	
struct input_file_ident {
	ino_t i_node;
	dev_t device;
};
 
static struct locus locus; /* Current input location */
static struct input_file_ident source_id; /* Identification of the input
					     file */
 
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_macro(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 */

#define xinput() (yyin ? getc(yyin) : EOF)

#undef YY_INPUT
#define YY_INPUT(buf,result,max_size)  do {                             \
        int i;                                                          \
        for (i = 0; i < max_size; i++) {                                \
                int ch = xinput();                                      \
                if (ch == EOF)                                          \
                        break;                                          \
                buf[i] = ch;                                            \
        }                                                               \
        result = i;                                                     \
} while (0)

#define LEX_BUFFER_STATE YY_BUFFER_STATE

#define SET_BUFFER_STATE(s) do {                                        \
        (s) = YY_CURRENT_BUFFER;                                        \
        yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));       \
} while (0)

#define RESTORE_BUFFER_STATE(s) do {                                    \
        yy_delete_buffer(YY_CURRENT_BUFFER);                            \
        yy_switch_to_buffer(s);                                         \
} while (0)

struct buffer_ctx {
	struct buffer_ctx *prev;
	struct locus locus;
	struct input_file_ident id;
	FILE *yyin;
	LEX_BUFFER_STATE state;
};
	
static struct buffer_ctx *context_stack;

#define STAT_ID_EQ(st,id) ((id).i_node == (st).st_ino       \
			   && (id).device == (st).st_dev)

struct buffer_ctx *
ctx_lookup(struct stat *st)
{
	struct buffer_ctx *ctx;

	for (ctx = context_stack; ctx; ctx = ctx->prev)
		if (STAT_ID_EQ(*st, ctx->id))
			break;
	return ctx;
}

static mu_list_t include_path;
static mu_list_t std_include_path;

struct file_data {
	const char *name;
	size_t namelen;
	char *buf;
	size_t buflen;
	int found;
};

static int
find_include_file(void *item, void *data)
{
	char *dir = item;
	struct file_data *dptr = data;
	size_t size = strlen(dir) + 1 + dptr->namelen + 1;
	if (size > dptr->buflen) {
		dptr->buflen = size;
		dptr->buf = xrealloc(dptr->buf, dptr->buflen);
	}
	strcpy(dptr->buf, dir);
	strcat(dptr->buf, "/");
	strcat(dptr->buf, dptr->name);
	return dptr->found = access(dptr->buf, F_OK) == 0;
}

void
lex_setup()
{
	mu_list_create(&include_path);
	mu_list_create(&std_include_path);
	mu_list_append(std_include_path, DEFAULT_VERSION_INCLUDE_DIR);
	mu_list_append(std_include_path, DEFAULT_INCLUDE_DIR);
	mu_list_append(std_include_path, "/usr/share/mailfromd/include");
	mu_list_append(std_include_path, "/usr/local/share/mailfromd/include");
}

static mu_list_t /* of struct input_file_ident */ incl_sources;

static int
input_file_ident_cmp(const void *item, const void *data)
{
	const struct input_file_ident *id = item;
	const struct stat *st = data;
	return !STAT_ID_EQ(*st, *id);
}

static void
input_file_ident_free(void *item)
{
	free(item);
}

static int
source_lookup(struct stat *st)
{
	int rc;
	if (!incl_sources) {
		mu_list_create(&incl_sources);
		mu_list_set_comparator(incl_sources, input_file_ident_cmp);
		mu_list_set_destroy_item(incl_sources, input_file_ident_free);
	}
	rc = mu_list_locate (incl_sources, st, NULL);
	if (rc == MU_ERR_NOENT) {
		struct input_file_ident *id = xmalloc(sizeof *id);
		id->i_node = st->st_ino;
		id->device = st->st_dev;
		mu_list_append(incl_sources, id);
	}
	return rc == 0;
}

int
push_source(const char *name, int once)
{
	FILE *fp;
	struct buffer_ctx *ctx;
	struct stat st;
	struct literal *lit;
	
	if (stat(name, &st)) {
		parse_error("cannot stat `%s': %s", name, mu_strerror(errno));
		return 1;
	}

	if (locus.file && STAT_ID_EQ(st, source_id)) {
		parse_error("recursive inclusion");
		return 1;
	}

	if ((ctx = ctx_lookup(&st))) {
		parse_error("recursive inclusion");
		if (ctx->prev)
			parse_error_locus(&ctx->prev->locus,
					  "`%s' already included here",
					  name);
		else
			parse_error("`%s' already included at top level",
				    name);
		return 1;
	}

	if (once && source_lookup(&st))
		return 0;
	
	fp = fopen(name, "r");
	if (!fp) {
		parse_error("cannot open `%s': %s", name, mu_strerror(errno));
		return 1;
	}

	/* Push current context */
	if (locus.file) {
		ctx = xmalloc (sizeof (*ctx));
		ctx->locus = locus;
		ctx->id = source_id;
		ctx->yyin = yyin;
		ctx->prev = context_stack;
		context_stack = ctx;
      
		/* Switch to the new context */
		yyin = fp;
		SET_BUFFER_STATE (ctx->state);
	} else {
		yyrestart (fp);
	}
	lit = string_alloc(name, strlen(name));
	lit->flags |= VAR_REFERENCED;
	locus.file = lit->text;
	locus.line = 1;
	source_id.i_node = st.st_ino;
	source_id.device = st.st_dev;

	if (yy_flex_debug)
		fprintf(stderr, "Processing file `%s'\n", name);

	return 0;
}

int
pop_source()
{
	struct buffer_ctx *ctx;

	if (yyin)
		fclose(yyin);
	if (!context_stack) {
		yyin = NULL;
		locus.file = NULL;
		return 1;
	}
	/* Restore previous context */
	locus = context_stack->locus;
	if (yy_flex_debug) 
		fprintf(stderr, "Resuming file `%s' at line %lu\n",
			locus.file, (unsigned long) locus.line);
	source_id = context_stack->id;
	RESTORE_BUFFER_STATE(context_stack->state);
	ctx = context_stack->prev;
	free(context_stack);
	context_stack = ctx;

	return 0;
}

int
try_file(const char *name, int allow_cwd, char **newp)
{
	static char *cwd = ".";
	struct file_data fd;
			
	fd.name = name;
	fd.namelen = strlen(name);
	fd.buf = NULL;
	fd.buflen = 0;
	fd.found = 0;
	
	if (allow_cwd) {
		mu_list_prepend(include_path, cwd);
		mu_list_do(include_path, find_include_file, &fd);
		mu_list_remove(std_include_path, cwd);
	} else
		mu_list_do(include_path, find_include_file, &fd);
			
	if (!fd.found) {
		mu_list_do(std_include_path, find_include_file, &fd);
					
		if (!fd.found) {
			parse_error("%s: No such file or directory", name);
			*newp = NULL;
		} 
	}
	if (fd.found)
		*newp = fd.buf;
	return fd.found;
}

void
parse_include(int once)
{
	int argc;
	char **argv;
	char *tmp = NULL;
	char *p = NULL;
	
	if (mu_argcv_get(yytext, "", NULL, &argc, &argv)) 
		parse_error("cannot parse include line");
	else if (argc != 2) 
		parse_error("invalid include statement");
	else {
		size_t len;
		int allow_cwd;

		p = argv[1];
		len = strlen(p);
		
		if (p[0] == '<' && p[len - 1] == '>') {
			allow_cwd = 0;
			p[len - 1] = 0;
			p++;
		} else 
			allow_cwd = 1;

		if (p[0] != '/' && try_file(p, allow_cwd, &tmp))
			p = tmp;
	}

	locus.line++;
	if (p)
		push_source(p, once);
	free(tmp);
	mu_argcv_free(argc, argv);
}

#define DEFAULT_SUFFIX ".mf"

void
parse_require()
{
	int argc;
	char **argv;
	char *tmp = NULL;
	char *p = NULL;
	
	if (mu_argcv_get(yytext, "", NULL, &argc, &argv)) 
		parse_error("cannot parse require line");
	else if (argc != 2) 
		parse_error("invalid require statement");
	else {
		size_t len;
		char *fname;
		
		p = argv[1];
		len = strlen(p);

		/* For compatibility with #include */
		if (p[0] == '<' && p[len - 1] == '>') {
			p[--len] = 0;
			p++;
		} 

		if (len < sizeof DEFAULT_SUFFIX
		    || memcmp(p + len - sizeof DEFAULT_SUFFIX - 1,
			      DEFAULT_SUFFIX,
			      sizeof DEFAULT_SUFFIX - 1)) {
			fname = xmalloc(len + sizeof DEFAULT_SUFFIX);
			strcpy(fname, p);
			strcat(fname, DEFAULT_SUFFIX);
			p = fname;
		} else
			fname = NULL;
			
		if (p[0] != '/') {
			if (try_file(p, 0, &tmp))
				p = tmp;
			else
				p = NULL;
			free(fname);
		} else if (fname) {
			tmp = fname;
			fname = NULL;
		}
	}
	locus.line++;
	if (p)
		push_source(p, 1);
	free(tmp);
	mu_argcv_free(argc, argv);
}

static int
get_const(const char *name)
{
	struct value *value_ptr;
	if (value_ptr = constant_lookup(name)) {
		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();
		}
	}
	return 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', `for', `from', and `poll' are
		  recognized as keywords.    	   
*/

%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__
VMACRO __package__|__version__|__major__|__minor__|__patch__
MACRO {LOCUS}|{VMACRO}|__statedir__
%%
         /* C-style comments */
"/*"         BEGIN(COMMENT);
<COMMENT>[^*\n]*        /* eat anything that's not a '*' */
<COMMENT>"*"+[^*/\n]*   /* eat up '*'s not followed by '/'s */
<COMMENT>\n             ++locus.line;
<COMMENT>"*"+"/"        BEGIN(INITIAL);
         /* Configuration directives */
^[ \t]*#[ \t]*pragma[ \t].*/\n parse_pragma(yytext);
^[ \t]*#[ \t]*include_once[ \t].*\n parse_include(1);
^[ \t]*#[ \t]*include[ \t].*\n parse_include(0);
^[ \t]*#[ \t]*require[ \t].*\n parse_require(yytext);
^[ \t]*#[ \t]*error[ \t].*\n  { parse_error("%s", yytext); locus.line++; }
         /* End-of-line comments */
#.*\n   { locus.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);

{MACRO}   { return builtin_macro(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>\%({MACRO}) { return builtin_macro(yytext+1); }
<INITIAL,ONBLOCK,ML,CML,STR>\%{IDENT}     {
	int rc;
	string(yytext + 1, yyleng - 1);
	if ((rc = get_const(yylval.literal->text)) != 0)
		return rc;
	else
		return VARIABLE; }
<INITIAL,ONBLOCK,ML,CML,STR>\%\{{IDENT}\} {
	int rc;
	string(yytext + 2, yyleng - 3);
	if ((rc = get_const(yylval.literal->text)) != 0)
		return rc;	
	else
		return VARIABLE; }
         /* Positional arguments */
<INITIAL,ONBLOCK,ML,CML,STR>\${P}         {
	yylval.number = strtol(yytext + 1, NULL, 0); return ARG; }
         /* 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; }
         /* Exception conditions */
&{IDENT}      { int rc;
                mf_status status;
		static int warned_once;
		
                /*DEPRECATION*/ 
                if ((rc = get_const(yytext + 1)) != 0) {
			if (!warned_once) {
				parse_warning("obsolete constant "
					      "form used: %s", yytext);
				parse_warning("remove leading '&'");
				warned_once = 1;
			}
			return rc;
		} else if (string_to_stat(yytext + 1, &status)) {
			parse_warning("Unknown exception code: %s", yytext + 1);
			string(yytext + 1, yyleng - 1);
			return IDENTIFIER;
		} else {
			if (!warned_once) {
				parse_warning("obsolete constant "
					      "form used: %s", yytext);
				parse_warning("remove leading '&' "
					      "and include <status.mfh>");
				yylval.number = status;
				warned_once = 1;
			}
			return NUMBER;
		}
         }
         /* Back-references */
<INITIAL,ONBLOCK,ML,CML,STR>\\{P} {
	 yylval.number = strtoul(yytext+1, NULL, 0);
	 return BACKREF; }
         /* Numeric strings */
{N}\.{N}\.{N} {	string(yytext, yyleng); return XCODE; }
[0-9]{3}      { string(yytext, yyleng); return CODE; }
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()) && isascii(c) && isspace(c))
			if (c == '\n')
				++locus.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    { ++locus.line;
                        BEGIN(STR);
                        line_begin();
                        line_add(yytext + 1, yyleng - 3); }     
\"[^\\\"$%\n]*/[\\$%] { BEGIN(STR);
                        string(yytext+1, yyleng-1);
                        line_begin();
                        return STRING; }
\"\\x{X}{X}/[\\$%] {
         BEGIN(STR);
         line_add_char(strtoul(yytext + 3, NULL, 16));
         line_finish();
         return STRING;
}
\"\\x{X}{X} {
         BEGIN(STR);
         line_add_char(strtoul(yytext + 3, NULL, 16));
}

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

\"\\[^1-9]/[\\$%]  {
         BEGIN(STR);
         line_add_char(mu_argcv_unquote_char(yytext[2]));
         line_finish();
         return STRING;
}
\"\\[^1-9]  {
         BEGIN(STR);
         line_add_char(mu_argcv_unquote_char(yytext[2]));
}
<STR>[^\\\"$%\n]*\\\n { ++locus.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}{O}{O}/[\\$%] {
         line_add_char(strtoul(yytext + 2, NULL, 8));
         line_finish();
         line_begin();
         return STRING;
}
<STR,ML,CML>\\0{O}{O}{O} {
         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;

        ++locus.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(ML);
        else
            BEGIN(QML);
}
        /* Quoted multilines */
<QML>[^\\\n]*\n {
        char *p;

        ++locus.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, yyleng);
        line_finish();
        return STRING;
}
<ML,CML,STR>[$%] {
        line_add(yytext, yyleng);
}
<ML>[^\\$%\n]*\n {
        char *p;

        ++locus.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 {
        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(ML);
}
         /* Oth