File: newscan.c

package info (click to toggle)
www-sql 0.5.7-18
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 648 kB
  • ctags: 769
  • sloc: ansic: 6,053; lex: 200; sh: 152; yacc: 151; makefile: 116; sql: 41
file content (278 lines) | stat: -rw-r--r-- 6,428 bytes parent folder | download | duplicates (3)
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
#include "newscan.h"
#include "if.h"

#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#define BUFSIZE 8192

static inline void scanner_read_cmd(Scanner *self);

void *xmalloc(int size);
char *xstrdup(const char *str);

Scanner *scanner_new(FILE *in, FILE *out, ExecFunc exec) {
  Scanner *self;

  self = xmalloc(sizeof(Scanner));

  self->in = in;
  self->out = out;
  self->exec = exec;
  self->argc = 0;
  self->argv[0] = NULL;
  return self;
}

void scanner_destroy(Scanner *self) {
  free(self);
}

static char cmd_lead[] = "<! sql ";
static int cmd_lead_length = 7 /* = strlen(cmd_lead) */;

void scanner_scan(Scanner *self) {
  char buf[64];
  int ch, lead_pos = 0, pos = 0, i;
  
  while ((ch = getc(self->in)) != EOF) {
    buf[pos++] = ch;
    if (cmd_lead[lead_pos] == ' ') {
      pos--;
      do {
	if (ch == ' ' || ch == '\t' || ch == '\n')
	  buf[pos++] = ch;
	else {
	  ungetc(ch, self->in);
	  break;
	}
      } while ((ch = getc(self->in)) != EOF);
      lead_pos++;
      /* also allow <!SQL command lead.  Also, some systems have weird
       * implementations of tolower that give unexpected results on non
       * alpha characters */
    } else if (cmd_lead[lead_pos] == ch ||
	       (isalpha(ch) && cmd_lead[lead_pos] == tolower(ch))) {
      if (lead_pos == 0)
	self->cmd_pos = ftell(self->in) - 1; /* possible start of command */
      lead_pos++;
    } else {
      if (checkIf)
	for (i = 0; i < pos; i++)
	  putc(buf[i], self->out);
      pos = 0;
      lead_pos = 0;
    }

    if (lead_pos == 1) { /* we just read the start of the lead ... */
      self->cmd_pos = ftell(self->in) - 1;
    }

    /* now check if we have got a correct command lead ... */
    if (lead_pos == cmd_lead_length) {
      scanner_read_cmd(self);
      if (self->exec)
	self->exec(self);
      pos = 0;
      lead_pos = 0;
      for (i = 0; i < self->argc; i++)
	free(self->argv[i]);
      self->argc = 0;
    }
  }
}

static inline char *scanner_read_word(Scanner *self) {
  static char buf[BUFSIZE];  /* static so it can be return'd */
  int ch, pos = 0;
  int in_quote = 0, escaped = 0;

  /*eat white space */
  while ((ch = getc(self->in)) != EOF && (ch == ' ' || ch == '\t' ||
					  ch == '\n'))
    ;
  if (ch == EOF || ch == '>')
    return NULL;
  ungetc(ch, self->in);
  while ((ch = getc(self->in)) != EOF && pos < BUFSIZE) {
    if (!in_quote && !escaped && (ch==' '||ch=='\t'||ch=='\n'||ch=='>')) {
      ungetc(ch, self->in);
      break;
    }
    if (escaped) {
      if (ch == '"')
	buf[pos-1] = ch;
      else
	buf[pos++] = ch;
      escaped = 0;
    } else if (ch == '"')
      in_quote = ! in_quote;
    else if (ch == '\\') {
      escaped = 1;
      buf[pos++] = ch;
    } else
      buf[pos++] = ch;
  }
  buf[pos] = '\0';
  return buf;
}

static inline char *scanner_read_expr(Scanner *self) {
  static char buf[BUFSIZE];
  int ch, i = 0;
  int escaped = 0;

  /* consume whitespace */
  while ((ch = getc(self->in)) != EOF && (ch == ' ' || ch == '\t' ||
					  ch == '\n'))
    ;
  if (ch == EOF || ch == '>')
    return NULL;

  switch (ch) {
  case ')':
  case '(':
  case '%':
  case '+':
  case '-':
  case '*':
  case '/':
  case ':':
    buf[0] = ch; buf[1] = '\0';
    return buf;
  case '<': /* make sense on its own, and with an '=' after */
  case '!':
  case '=':
    buf[0] = ch; buf[1] = buf[2] = '\0';
    ch = getc(self->in);
    if (ch == EOF) return buf;
    if (ch != '=') { ungetc(ch, self->in); return buf; }
    buf[1] = ch; /* '=' */
    return buf;
  case '&':
  case '|':
    buf[0] = ch; buf[1] = buf[2] = '\0';
    ch = getc(self->in);
    if (ch == EOF) return buf;
    if (ch != buf[0]) { ungetc(ch, self->in); return buf; }
    buf[1] = ch;
    return buf;
  case '$': /* a variable */
  case '@':
  case '#':
  case '?':
  case '~':
    i = 0;
    do {
      buf[i++] = ch;
      ch = getc(self->in);
    } while (ch != EOF && ((ch>='a' && ch<='z') || (ch>='A' && ch<='Z') ||
			   (ch>='0' && ch<='9') || ch=='.' || ch=='_'));
    if (ch != EOF) ungetc(ch, self->in);
    buf[i] = '\0';
    return buf;
  case '0':
  case '1':
  case '2':
  case '3':
  case '4':
  case '5':
  case '6':
  case '7':
  case '8':
  case '9':
    i = 0;
    do {
      buf[i++] = ch;
      ch = getc(self->in);
    } while (ch != EOF && ((ch >= '0' && ch <= '9') || ch == '.'));
    if (ch != EOF) ungetc(ch, self->in);
    buf[i] = '\0';
    return buf;
  case '"':
    i = escaped = 0;
    while ((ch = getc(self->in)) != EOF && i < BUFSIZE) {
      if (ch == '"' && ! escaped)
	break;
      if (escaped) {
	if (ch == '"')
	  buf[i-1] = ch;
	else
	  buf[i++] = ch;
	escaped = 0;
      } else if (ch == '\\') {
	buf[i++] = ch;
	escaped = 1;
      } else
	buf[i++] = ch;
    }
    buf[i] = '\0';
    return buf;
  case '\\': /* special handling for > -- because it also closes a command */
    buf[0] = ch;
    buf[1] = buf[2] = '\0';
    ch = getc(self->in);
    if (ch != '>') return buf;
    buf[0] = ch;
    ch = getc(self->in);
    if (ch == '=') buf[1] = ch;
    else if (ch != EOF) ungetc(ch, self->in);
    return buf;
  default:
    buf[0] = ch; buf[1] = '\0';
    return buf;
  }
}

static inline void scanner_read_cmd(Scanner *self) {
  char *tmp;
  int is_expr = 0;

  tmp = scanner_read_word(self);
  if (tmp == NULL)
    return;
  self->argv[0] = xstrdup(tmp);
  self->argc = 1;

  if (!strcasecmp(self->argv[0], "setexpr")) {
    tmp = scanner_read_word(self); /* var name */
    if (tmp == NULL)
      return;
    self->argv[self->argc++] = xstrdup(tmp);
    is_expr = 1;
  } else if (!strcasecmp(self->argv[0], "if") ||
	     !strcasecmp(self->argv[0], "elsif") ||
	     !strcasecmp(self->argv[0], "eval") ||
	     !strcasecmp(self->argv[0], "while"))
    is_expr = 1;
  else
    is_expr = 0;

  if (is_expr)
    while ((tmp = scanner_read_expr(self)) != NULL && self->argc < MAXARGS) {
      self->argv[self->argc++] = xstrdup(tmp);
    }
  else
    while ((tmp = scanner_read_word(self)) != NULL && self->argc < MAXARGS)
      self->argv[self->argc++] = xstrdup(tmp);
  /* this holds the position of the end of command */
  self->cur_pos = ftell(self->in);
}

FILE *yyout;
Scanner *cur_scan;

void exec_f(Scanner *s) {
  void executeSql(int argc, char *argv[]);

  yyout = s->out;
  cur_scan = s;
  executeSql(s->argc, s->argv);
}

void parse(FILE *is, FILE *os) {
  Scanner *s = scanner_new(is, os, exec_f);
  scanner_scan(s);
  scanner_destroy(s);
}