-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer.c
More file actions
164 lines (157 loc) · 3.35 KB
/
tokenizer.c
File metadata and controls
164 lines (157 loc) · 3.35 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "tokenizer.h"
// inserts a token into a linked list
token *insert(char *tok, token *head) {
// if linked list is empty, make a new one
if (head == NULL) {
token *new_token = (token*) malloc(sizeof(token));
new_token->data = tok;
new_token->next = NULL;
return new_token;
}
// otherwise, add to list
struct token *p = head;
while (p != NULL) {
if (p->next == NULL) {
token *new_token = (token*) malloc(sizeof(token));
new_token->data = tok;
new_token->next = NULL;
p->next = new_token;
break;
}
p = p->next;
}
return head;
}
// prints a list of all tokens
// eg. [{a},{b},{c}]
void print_tokens(token *head) {
token *p = head;
printf("[");
while (p != NULL) {
if (p->next == NULL)
printf("{%s}", p->data);
else
printf("{%s},", p->data);
p = p->next;
}
printf("]\n");
}
// removes all occurences of a char from a string
char *remove_chars(char *p, char remove) {
int len = strlen(p);
int count = 0;
char *new_str = malloc(len);
for (int j = 0; j < len; j++) {
// handle escape character in double quoted strings
if (remove == DOUBLE_QUOTE) {
if (*p == DOUBLE_QUOTE && *(p - 1) != BACKSLASH) {
p++;
count++;
continue;
}
}
else {
if (*p == remove) {
p++;
count++;
continue;
}
}
*new_str = *p;
new_str++;
p++;
}
*new_str = TERMINATE;
return new_str - len + count;
}
// formats a char pointer for insert()
token *make_token(int len, char *tok, token *head, char quote) {
char *p = malloc(len);
strncpy(p, tok - len, len);
char *null = p + len;
*null = TERMINATE;
if (quote == BACKSLASH) {
p = remove_chars(p, BACKSLASH);
}
if (quote == SINGLE_QUOTE) {
p = remove_chars(p, SINGLE_QUOTE);
}
if (quote == DOUBLE_QUOTE) {
p = remove_chars(p, DOUBLE_QUOTE);
p = remove_chars(p, BACKSLASH);
}
quote = BACKSLASH;
return insert(p, head);
}
/*
* parses a char ptr and returns as a token
* splits tokens on "|;<>&", removes whitespace,
* handles escape chars and quotes
*/
token *tokenize_input(token *head, char *input, char quote) {
char *p = input;
int len = 0;
// increment pointer along our input string until terminate char
while (*p != TERMINATE) {
// special delimiters, turn them into 1 char tokens
if (strchr("|;<>&", *p)) {
if (len > 0) {
head = make_token(len, p, head, quote);
len = 0;
}
head = make_token(1, p + 1, head, quote);
p++;
}
// back slash, skip 2 chars, the backslash and whatever char follows
else if (strchr("\\", *p)) {
p += 2;
len += 2;
}
// double quote, handles escape chars
else if (strchr("\"", *p)) {
p++;
len++;
while (*p != TERMINATE && *p != DOUBLE_QUOTE ||
(*(p - 1) == BACKSLASH && *p == DOUBLE_QUOTE)) {
p++;
len++;
}
quote = DOUBLE_QUOTE;
len++;
p++;
}
// single quote, does not handle escape chars
else if (strchr("\'", *p)) {
p++;
len++;
while (*p != TERMINATE && *p != SINGLE_QUOTE) {
p++;
len++;
}
quote = SINGLE_QUOTE;
len++;
p++;
}
// space
else if (isspace(*p)) {
if (len > 0) {
head = make_token(len, p, head, quote);
len = 0;
}
while (isspace(*p)) {
p++;
}
}
else {
len++;
p++;
}
}
// if there's still a token waiting to be made, make it
if (len > 0) head = make_token(len, p, head, quote);
return head;
}