-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprac.l
More file actions
77 lines (63 loc) · 1.86 KB
/
prac.l
File metadata and controls
77 lines (63 loc) · 1.86 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
%{
#include <stdio.h>
#include <string.h>
#include "y.tab.h"
#include "envar.h"
%}
%option stack
%x quotedword envExpansion
%%
[0-9]+ yylval.number = atoi(yytext); return NUMBER;
cd yylval.string = strdup(yytext);return TOKCD;
setenv yylval.string = strdup(yytext);return TOKSETENV;
unsetenv yylval.string = strdup(yytext);return TOKCLEARENV;
printenv yylval.string = strdup(yytext);return TOKPRINTENV;
alias yylval.string = strdup(yytext);return TOKALIAS;
unalias yylval.string = strdup(yytext);return TOKUNALIAS;
bye yylval.string = strdup(yytext);return TOKBYE;
[^<>|\&" \t\n$]+ yylval.string = strdup(yytext);return WORD;
\" yy_push_state(quotedword);
<quotedword>{
[^"\n\{\}]+ yylval.string = strdup(yytext);return WORD;
\" yy_pop_state();
\n printf(":p \">");
[\n\{\}] /* ignore certain metacharacters*/
}
<*>\$\{ yy_push_state(envExpansion);
<envExpansion>{
[^<>|\&" \t\n\{\}]+ yylval.string = getenv(yytext);return WORD;
\} yy_pop_state();
\" yy_push_state(quotedword);
[<>|\& \t\n] /* ignore certain metacharacters*/
}
\| return TOKPIPE;
\< return TOK_IO_REDIR_IN;
\> return TOK_IO_REDIR_OUT;
\>\> return TOK_IO_REDIR_OUT_APPEND;
\n return TOKENDEXP;
[ \t]+ /* ignore whitespace */;
%%
void flush_buffer(){
yy_flush_buffer(YY_CURRENT_BUFFER);
}
void scan_string(char* str){
//to accomodate the yacc grammar
//we copied the string and added a newline character
char* new_str;
new_str = strdup(str);
strcat(new_str, "\n");
/*FILE *f = fopen("file.txt", "w+");
if (f == NULL)
{
printf("Error scanning alias!\n");
return;
}
fprintf(f, "%s", new_str);*/
YY_BUFFER_STATE old = YY_CURRENT_BUFFER;
YY_BUFFER_STATE buf = yy_scan_string(new_str);
yyparse();
yy_flush_buffer(buf);
yylex_destroy();
yy_switch_to_buffer(old);
//fclose(f);
}