-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputlex.l
433 lines (337 loc) · 6.94 KB
/
inputlex.l
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
%{
#include <string.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "common.h"
extern int yylex();
/* basic internal commands */
#define STARTUP 1
#define SHUTDOWN 2
#define RESTART 3
#define X_DU 10
#define X_D 11
#define X_DB 12
#define X_QM 13
#define X_DT 14
#define X_S 15
#define X_DP 16
#define X_DD 17
#define X_H 18
#define X_DS 19
#define X_V 20
/* and their arguments */
#define NOMOUNT 100
#define MOUNT 101
#define IMMEDIATE 102
#define ABORT 103
#define RESTRICT 104
#define NORMAL 105
#define OTHER 9000
#define EOQ 9001
%}
%option nounput nodefault noyywrap
/* %option debug */
%%
STARTUP return STARTUP;
NOMOUNT return NOMOUNT;
MOUNT return MOUNT;
RESTRICT return RESTRICT;
SHUTDOWN return SHUTDOWN;
IMMEDIATE return IMMEDIATE;
ABORT return ABORT;
NORMAL return NORMAL;
RESTART return RESTART;
\\db return X_DB;
\\dd return X_DD;
\\dp return X_DP;
\\ds return X_DS;
\\dt return X_DT;
\\du return X_DU;
\\d return X_D;
\\d[[:alnum:]]* return OTHER;
\\s return X_S;
\\s[[:alnum:]]* return OTHER;
\\v return X_V;
\\v[[:alnum:]]* return OTHER;
\\h return X_H;
\\h[[:alnum:]]* return OTHER;
\\\? return X_QM;
DESC return X_D; /* sqlplus "comfort" */
SHOW[[:space:]]+PARAMETER return X_DP; /* sqlplus "comfort" */
[ \t]+ /* noop */;
[^[:space:]]* return OTHER;
. return OTHER;
\n return EOQ;
<<EOF>> return EOQ;
%%
void input_parse_startup() {
int mode = 0;
int token;
token = yylex();
switch (token) {
case NOMOUNT:
mode = STARTUP_NOMOUNT;
break;
case MOUNT:
mode = STARTUP_MOUNT;
break;
case RESTRICT:
mode = STARTUP_RESTRICT;
break;
case EOQ:
mode = STARTUP_DEFAULT;
break;
default:
fprintf(stderr, "Invalid argument.\n");
return;
}
if (yylex() != EOQ) {
fprintf(stderr, "Syntax error.\n");
return;
}
ostart(mode);
}
void input_parse_shutdown() {
int mode = 0;
int token;
token = yylex();
switch (token) {
case IMMEDIATE:
mode = SHUTDOWN_IMMEDIATE;
break;
case NORMAL:
mode = SHUTDOWN_NORMAL;
break;
case ABORT:
mode = SHUTDOWN_ABORT;
break;
case EOQ:
mode = SHUTDOWN_DEFAULT;
break;
default:
fprintf(stderr, "Invalid argument.\n");
return;
}
if (yylex() != EOQ) {
fprintf(stderr, "Syntax error.\n");
return;
}
oshutdown(mode);
}
void input_parse_restart() {
/* this is pretty ugly duplication */
int mode = 0;
int token;
token = yylex();
switch (token) {
case NOMOUNT:
mode = STARTUP_NOMOUNT;
break;
case MOUNT:
mode = STARTUP_MOUNT;
break;
case RESTRICT:
mode = STARTUP_RESTRICT;
break;
case EOQ:
mode = STARTUP_DEFAULT;
break;
default:
fprintf(stderr, "Invalid argument.\n");
return;
}
if (yylex() != EOQ) {
fprintf(stderr, "Syntax error.\n");
return;
}
oshutdown(SHUTDOWN_IMMEDIATE);
ostart(mode);
}
void input_x_db() {
char s[1024];
if (yylex() == EOQ)
snprintf(s, 1023, SQL_X_DB_SUMMARY);
else
snprintf(s, 1023, SQL_X_DB_DETAIL, yytext);
process_query(s, 1);
}
void input_x_dd() {
char s[1024];
char *search;
if (yylex() == EOQ)
search = "";
else
search = yytext;
snprintf(s, 1023, SQL_X_DD, search, search);
process_query(s, 1);
}
void input_x_dp() {
char s[1024];
char *search;
if (yylex() == EOQ)
search = "";
else
search = yytext;
snprintf(s, 1023, SQL_X_DP, search, search);
process_query(s, 1);
}
void input_x_ds() {
process_query(SQL_X_DS, 1);
}
void input_x_dt() {
char s[1024];
char *search;
if (yylex() == EOQ)
search = "";
else
search = yytext;
snprintf(s, 1023, SQL_X_DT, search, search);
process_query(s, 1);
}
void input_x_du() {
process_query(SQL_X_DU, 1);
}
void input_x_h() {
HIST_ENTRY *hist;
char timestamp[30];
time_t ts_time;
int i;
for (i = 1; i <= history_length; i++) {
hist = history_get(i);
ts_time = history_get_time(hist);
strftime(timestamp, 29, "%Y-%m-%d %H:%M:%S", localtime(&ts_time));
printf("%5d %-21s %s\n", i, timestamp, hist->line);
}
}
void input_x_s() {
char s[1024];
if (yylex() == EOQ) {
fprintf(stderr, "Schema name required.\n");
return;
}
snprintf(s, 1023, SQL_X_S, yytext);
process_query(s, 0);
}
void input_x_v() {
printf("\n");
printf("Database versions:\n");
process_query(SQL_X_V_BANNER, 0);
printf("Registry history:\n");
process_query(SQL_X_V_REGISTRY, 0);
printf("Client version: %s\n", OSQL_VERSION);
printf("\n");
}
void input_x_d() {
/* describe object */
if (yylex() == EOQ) {
fprintf(stderr, "Object name required.\n");
return;
}
describe(yytext);
}
void input_x_qm() {
/* question mark: help! */
char *fmt = " %-15s %s\n";
printf("\n");
printf("Internal Oracle commands:\n");
printf("\n");
printf(" STARTUP [ { NOMOUNT | MOUNT | RESTRICT} ]\n");
printf(" Start Oracle; default mode is to mount and open the database.\n");
printf("\n");
printf(" SHUTDOWN [ { NORMAL | IMMEDIATE | ABORT } ]\n");
printf(" Shut down Oracle.\n");
printf(" WARNING: Default mode is IMMEDIATE instead of NORMAL.\n");
printf("\n");
printf(" RESTART [ { NOMOUNT | MOUNT | RESTRICT} ]\n");
printf(" Shut down Oracle (in IMMEDIATE mode),\n");
printf(" then STARTUP as specified (see above).\n");
printf("\n");
printf("Internal backslash commands:\n");
printf("\n");
printf(fmt, "\\d <object>",
"describe object (currently: table, view)");
printf(fmt, "\\db [<tspace>]",
"list tablespaces, or list datafiles for given tspace");
printf(fmt, "\\dd [<search>]",
"show dictionary entry [matching <search>]");
printf(fmt, "\\dp [<search>]",
"show parameters [matching <search>]");
printf(fmt, "\\ds",
"show current activity");
printf(fmt, "\\dt [<schema>]",
"list tables [in <schema>]");
printf(fmt, "\\du",
"list users");
printf("\n");
printf(fmt, "\\h",
"show history");
printf("\n");
printf(fmt, "\\s <schema>",
"change current schema");
printf("\n");
printf(fmt, "\\v",
"version information");
printf("\n");
printf(fmt, "\\?",
"this help");
printf("\n");
}
void input_process_internal(const char *statement) {
int token;
yy_scan_string(statement);
switch ((token = yylex())) {
case RESTART:
input_parse_restart();
break;
case STARTUP:
input_parse_startup();
break;
case SHUTDOWN:
input_parse_shutdown();
break;
case X_DB:
input_x_db();
break;
case X_DD:
input_x_dd();
break;
case X_DP:
input_x_dp();
break;
case X_DS:
input_x_ds();
break;
case X_DT:
input_x_dt();
break;
case X_DU:
input_x_du();
break;
case X_D:
input_x_d();
break;
case X_H:
input_x_h();
break;
case X_S:
input_x_s();
break;
case X_V:
input_x_v();
break;
case X_QM:
input_x_qm();
break;
default:
fprintf(stderr, "ERROR: Internal error: Unknown internal command?\n");
}
yy_delete_buffer(YY_CURRENT_BUFFER);
}
int input_is_internal(const char *cmd) {
int ret;
yy_scan_string(cmd);
ret = yylex();
ret = (ret > 0 && ret < 100);
yy_delete_buffer(YY_CURRENT_BUFFER);
return ret;
}