This source file includes following definitions.
- opdtok
- addcerrlist_tok
- linetok
1 #include "token.h"
2
3
4
5
6
7
8
9
10 OPD *opdtok(const char *str);
11
12
13
14
15 CERR cerr_linetok[] = {
16 { 104, "label length is too long" },
17 { 105, "no command in the line" },
18 };
19
20
21
22
23 static CERR cerr_opdtok[] = {
24 { 117, "operand too many in DC" },
25 { 118, "operand length too long" },
26 { 121, "cannot get operand token" },
27 { 123, "unclosed quote" },
28 };
29
30 OPD *opdtok(const char *str)
31 {
32 OPD *opd = malloc_chk(sizeof(OPD), "opd");
33 char *tok, *p, sepc = ',';
34 int i = 0, cnt_quote = 0;
35 bool quoting = false;
36
37 opd->opdc = 0;
38 if(!str || !str[0]) {
39 return opd;
40 }
41 tok = p = strdup_chk(str, "opdtok.p");
42 do {
43
44 if(opd->opdc >= OPDSIZE) {
45 setcerr(117, "");
46 break;
47 }
48
49 if(p[i] == '=') {
50 i++;
51 }
52
53 if(p[i] == '\'') {
54
55 if(p[i+1] != '\'' && (quoting == false || p[i-1] != '\'')) {
56 quoting = !quoting;
57 }
58
59 if(p[i+1] != '\'') {
60 cnt_quote++;
61 }
62 }
63 if(quoting == true) {
64
65 if(!p[i]) {
66 setcerr(123, str);
67 break;
68 }
69 i++;
70 } else {
71 i += strcspn(p + i, ", ");
72 if(i == 0) {
73 setcerr(121, "");
74 break;
75 }
76 if(i - cnt_quote > OPDSIZE) {
77 setcerr(118, "");
78 break;
79 }
80 sepc = p[i];
81 opd->opdv[(opd->opdc)++] = strndup_chk(p, i, "opd->opdv[]");
82 p += i + 1;
83 i = cnt_quote = 0;
84 }
85 } while(sepc == ',');
86 FREE(tok);
87 return opd;
88 }
89
90
91 void addcerrlist_tok()
92 {
93 addcerrlist(ARRAYSIZE(cerr_linetok), cerr_linetok);
94 addcerrlist(ARRAYSIZE(cerr_opdtok), cerr_opdtok);
95 }
96
97 CMDLINE *linetok(const char *line)
98 {
99 char *tok = NULL, *p = NULL;
100 int i;
101 CMDLINE *cmdl = NULL;
102
103 assert(line);
104 if(!line[0] || line[0] == '\n') {
105 return NULL;
106 }
107 tok = p = strdup_chk(line, "tok");
108
109 strip_casl2_comment(p);
110
111 strip_end(p);
112
113 if(!p[0]) {
114 goto linetokfin;
115 }
116 cmdl = malloc_chk(sizeof(CMDLINE), "cmdl");
117
118
119
120 if((i = strcspn(p, " \t")) == 0) {
121 cmdl->label = strdup_chk("", "cmdl->label");
122 } else {
123 cmdl->label = strndup_chk(p, i, "cmdl->label");
124
125 if(i > LABELSIZE) {
126 setcerr(104, cmdl->label);
127 FREE(cmdl->label);
128 goto linetokfin;
129 }
130 }
131
132
133
134 p += i;
135
136 p += strspn(p, " \t");
137
138 if(!p[0]) {
139 if(cmdl->label) {
140 setcerr(105, "");
141 }
142 FREE(cmdl->label);
143 FREE(cmdl);
144 goto linetokfin;
145 }
146
147 i = strcspn(p, " \t");
148 cmdl->cmd = strndup_chk(p, i, "cmdl.cmd");
149
150
151
152 p += i;
153
154 p += strspn(p, " \t");
155
156 cmdl->opd = opdtok(p);
157 linetokfin:
158 FREE(tok);
159 return cmdl;
160 }