-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.lex
More file actions
95 lines (77 loc) · 1.43 KB
/
Copy pathshell.lex
File metadata and controls
95 lines (77 loc) · 1.43 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
%{
#include<stdio.h>
#include "variables.h"
#include "strings.h"
#include "parsetypes.h"
/* yacc -d */
#include "y.tab.h"
#include "error.h"
#define MAX_STR_CONST 1024
char string_buf[MAX_STR_CONST];
char* string_buf_ptr;
void assemble(char c);
void replace_with_var(const char* text);
%}
WS [ \t\n]+
VAR [a-zA-Z_][a-zA-Z_0-9]+
WORD [^ \t\n\<\>\|\&]+
SPECIAL [\<\>\|&]
/* Declare start conditon */
%x STRING
/* make cc happy */
%option noyywrap
%%
{WS} {}
{SPECIAL} {
return *yytext;
}
${VAR} {
char* var;
var = getvar(yytext + 1);
if(var == NULL) {
yylval.str = NULL;
} else {
yylval.str = add_string(var);
}
return STR;
}
<INITIAL>\" {
BEGIN(STRING);
string_buf_ptr = string_buf;
}
<STRING>${VAR} {
// dealing with vars
replace_with_var(yytext);
}
<STRING>\" {
BEGIN(INITIAL);
assemble('\0');
yylval.str = add_string(string_buf);
return STR;
}
<STRING>. {
// assemble the string literal
assemble(*yytext);
}
{WORD} {
yylval.str = add_string(yytext);
return STR;
}
%%
void assemble(char c) {
if(string_buf_ptr < string_buf + 1024) {
*string_buf_ptr = c;
string_buf_ptr++;
} else {
fatal_error("string const too long");
}
}
void replace_with_var(const char* text) {
char* var = getvar(text + 1);
if(var == NULL) {
return;
}
for(int i = 0; var[i] != '\0'; i++) {
assemble(var[i]);
}
}