-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_lex2.c
77 lines (62 loc) · 1.17 KB
/
test_lex2.c
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
/*% cc % -Wall -Wextra -g -o #
*/
#include <stdio.h>
#include <stdbool.h>
bool quoted;
void
lex_handle_apostrophe(int nc){
if (nc == '\'' && quoted == true){
putchar('\'');
}
else {
quoted = !quoted;
}
}
void
lex_handle_blank(int nc){
if (nc != '\t' && nc != ' ' && quoted != true)
putchar('\n');
}
void
lex(char string[]){
char *c;
char *nc;
extern bool quoted;
quoted = false;
for (c=&string[0], nc=&string[1]; /* Todo: handle empty */
*nc != '\0'; c++, nc++){
switch(*c){
case '\'':
lex_handle_apostrophe(*nc);
break;
case ' ':
lex_handle_blank(*nc);
break;
case '\t':
lex_handle_blank(*nc);
break;
default:
putchar(*c);
break;
}
}
}
int
main(void){
char test_string1[]="date";
char test_string2[]="cat /lib/news/build";
char test_string3[]="who >user.names";
char test_string4[]="who >>user.name";
char test_string5[]="wc <file";
char test_string6[]="who | wc";
char test_string7[]="who; date";
char test_string8[]="rm -r junk || echo rm failed!";
lex(test_string1);
lex(test_string2);
lex(test_string3);
lex(test_string4);
lex(test_string5);
lex(test_string6);
lex(test_string7);
lex(test_string8);
}