summaryrefslogtreecommitdiff
path: root/src/configparser.y
blob: 102414568a821207eb3a4d945c47d730703f5d06 (plain)
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
%token_prefix TK_
%extra_argument {config_t *ctx}
%name configparser

%include {
#include "configfile.h"
#include "buffer.h"
#include "array.h"

#include <assert.h>
#include <stdio.h>
#include <string.h>

static void configparser_push(config_t *ctx, data_config *dc, int isnew) {
  if (isnew) {
    dc->context_ndx = ctx->all_configs->used;
    force_assert(dc->context_ndx > ctx->current->context_ndx);
    array_insert_unique(ctx->all_configs, (data_unset *)dc);
    dc->parent = ctx->current;
    array_insert_unique(dc->parent->children, (data_unset *)dc);
  }
  if (ctx->configs_stack->used > 0 && ctx->current->context_ndx == 0) {
    fprintf(stderr, "Cannot use conditionals inside a global { ... } block\n");
    exit(-1);
  }
  array_insert_unique(ctx->configs_stack, (data_unset *)ctx->current);
  ctx->current = dc;
}

static data_config *configparser_pop(config_t *ctx) {
  data_config *old = ctx->current;
  ctx->current = (data_config *) array_pop(ctx->configs_stack);
  return old;
}

/* return a copied variable */
static data_unset *configparser_get_variable(config_t *ctx, const buffer *key) {
  data_unset *du;
  data_config *dc;

#if 0
  fprintf(stderr, "get var %s\n", key->ptr);
#endif
  for (dc = ctx->current; dc; dc = dc->parent) {
#if 0
    fprintf(stderr, "get var on block: %s\n", dc->key->ptr);
    array_print(dc->value, 0);
#endif
    if (NULL != (du = array_get_element(dc->value, key->ptr))) {
      return du->copy(du);
    }
  }
  return NULL;
}

/* op1 is to be eat/return by this function if success, op1->key is not cared
   op2 is left untouch, unreferenced
 */
data_unset *configparser_merge_data(data_unset *op1, const data_unset *op2) {
  /* type mismatch */
  if (op1->type != op2->type) {
    if (op1->type == TYPE_STRING && op2->type == TYPE_INTEGER) {
      data_string *ds = (data_string *)op1;
      buffer_append_int(ds->value, ((data_integer*)op2)->value);
      return op1;
    } else if (op1->type == TYPE_INTEGER && op2->type == TYPE_STRING) {
      data_string *ds = data_string_init();
      buffer_append_int(ds->value, ((data_integer*)op1)->value);
      buffer_append_string_buffer(ds->value, ((data_string*)op2)->value);
      op1->free(op1);
      return (data_unset *)ds;
    } else {
      fprintf(stderr, "data type mismatch, cannot merge\n");
      return NULL;
    }
  }

  switch (op1->type) {
    case TYPE_STRING:
      buffer_append_string_buffer(((data_string *)op1)->value, ((data_string *)op2)->value);
      break;
    case TYPE_INTEGER:
      ((data_integer *)op1)->value += ((data_integer *)op2)->value;
      break;
    case TYPE_ARRAY: {
      array *dst = ((data_array *)op1)->value;
      array *src = ((data_array *)op2)->value;
      data_unset *du;
      size_t i;

      for (i = 0; i < src->used; i ++) {
        du = (data_unset *)src->data[i];
        if (du) {
          array_insert_unique(dst, du->copy(du));
        }
      }
      break;
    default:
      force_assert(0);
      break;
    }
  }
  return op1;
}

}

%parse_failure {
  ctx->ok = 0;
}

input ::= metalines.
metalines ::= metalines metaline.
metalines ::= .
metaline ::= varline.
metaline ::= global.
metaline ::= condlines(A) EOL. { A = NULL; }
metaline ::= include.
metaline ::= include_shell.
metaline ::= EOL.

%type       value                  {data_unset *}
%type       expression             {data_unset *}
%type       aelement               {data_unset *}
%type       condline               {data_config *}
%type       condlines              {data_config *}
%type       aelements              {array *}
%type       array                  {array *}
%type       key                    {buffer *}
%type       stringop               {buffer *}

%type       cond                   {config_cond_t }

%destructor value                  { if ($$) $$->free($$); }
%destructor expression             { if ($$) $$->free($$); }
%destructor aelement               { if ($$) $$->free($$); }
%destructor aelements              { array_free($$); }
%destructor array                  { array_free($$); }
%destructor key                    { buffer_free($$); }
%destructor stringop               { buffer_free($$); }

%token_type                        {buffer *}
%token_destructor                  { buffer_free($$); }

varline ::= key(A) ASSIGN expression(B). {
  if (ctx->ok) {
    buffer_copy_buffer(B->key, A);
    if (strncmp(A->ptr, "env.", sizeof("env.") - 1) == 0) {
      fprintf(stderr, "Setting env variable is not supported in conditional %d %s: %s\n",
          ctx->current->context_ndx,
          ctx->current->key->ptr, A->ptr);
      ctx->ok = 0;
    } else if (NULL == array_get_element(ctx->current->value, B->key->ptr)) {
      array_insert_unique(ctx->current->value, B);
      B = NULL;
    } else {
      fprintf(stderr, "Duplicate config variable in conditional %d %s: %s\n",
              ctx->current->context_ndx,
              ctx->current->key->ptr, B->key->ptr);
      ctx->ok = 0;
      B->free(B);
      B = NULL;
    }
  }
  buffer_free(A);
  A = NULL;
}

varline ::= key(A) APPEND expression(B). {
  if (ctx->ok) {
    array *vars = ctx->current->value;
    data_unset *du;

    if (strncmp(A->ptr, "env.", sizeof("env.") - 1) == 0) {
      fprintf(stderr, "Appending env variable is not supported in conditional %d %s: %s\n",
          ctx->current->context_ndx,
          ctx->current->key->ptr, A->ptr);
      ctx->ok = 0;
    } else if (NULL != (du = array_get_element(vars, A->ptr))) {
      /* exists in current block */
      if (du->type != B->type) {
        /* might create new data when merging different types; need to replace old array entry */
        /* also merging will kill the old data */
        du = du->copy(du);
        du = configparser_merge_data(du, B);
        if (NULL != du) {
          buffer_copy_buffer(du->key, A);
          array_replace(vars, du);
        }
      } else {
        data_unset *old_du = du;
        du = configparser_merge_data(old_du, B);
        force_assert((NULL == du) || (du == old_du)); /* must not create new data when types match */
      }
      if (NULL == du) {
        ctx->ok = 0;
      }
      B->free(B);
    } else if (NULL != (du = configparser_get_variable(ctx, A))) {
      du = configparser_merge_data(du, B);
      if (NULL == du) {
        ctx->ok = 0;
      }
      else {
        buffer_copy_buffer(du->key, A);
        array_insert_unique(ctx->current->value, du);
      }
      B->free(B);
    } else {
      buffer_copy_buffer(B->key, A);
      array_insert_unique(ctx->current->value, B);
    }
    buffer_free(A);
    A = NULL;
    B = NULL;
  }
}

key(A) ::= LKEY(B). {
  if (strchr(B->ptr, '.') == NULL) {
    A = buffer_init_string("var.");
    buffer_append_string_buffer(A, B);
    buffer_free(B);
    B = NULL;
  } else {
    A = B;
    B = NULL;
  }
}

expression(A) ::= expression(B) PLUS value(C). {
  A = NULL;
  if (ctx->ok) {
    A = configparser_merge_data(B, C);
    if (NULL == A) {
      ctx->ok = 0;
    }
    B = NULL;
    C->free(C);
    C = NULL;
  }
}

expression(A) ::= value(B). {
  A = B;
  B = NULL;
}

value(A) ::= key(B). {
  A = NULL;
  if (ctx->ok) {
    if (strncmp(B->ptr, "env.", sizeof("env.") - 1) == 0) {
      char *env;

      if (NULL != (env = getenv(B->ptr + 4))) {
        data_string *ds;
        ds = data_string_init();
        buffer_append_string(ds->value, env);
        A = (data_unset *)ds;
      }
      else {
        fprintf(stderr, "Undefined env variable: %s\n", B->ptr + 4);
        ctx->ok = 0;
      }
    } else if (NULL == (A = configparser_get_variable(ctx, B))) {
      fprintf(stderr, "Undefined config variable: %s\n", B->ptr);
      ctx->ok = 0;
    }
    buffer_free(B);
    B = NULL;
  }
}

value(A) ::= STRING(B). {
  A = (data_unset *)data_string_init();
  buffer_copy_buffer(((data_string *)(A))->value, B);
  buffer_free(B);
  B = NULL;
}

value(A) ::= INTEGER(B). {
  A = (data_unset *)data_integer_init();
  ((data_integer *)(A))->value = strtol(B->ptr, NULL, 10);
  buffer_free(B);
  B = NULL;
}
value(A) ::= array(B). {
  A = (data_unset *)data_array_init();
  array_free(((data_array *)(A))->value);
  ((data_array *)(A))->value = B;
  B = NULL;
}
array(A) ::= LPARAN RPARAN. {
  A = array_init();
}
array(A) ::= LPARAN aelements(B) RPARAN. {
  A = B;
  B = NULL;
}

aelements(A) ::= aelements(C) COMMA aelement(B). {
  A = NULL;
  if (ctx->ok) {
    if (buffer_is_empty(B->key) ||
        NULL == array_get_element(C, B->key->ptr)) {
      array_insert_unique(C, B);
      B = NULL;
    } else {
      fprintf(stderr, "Error: duplicate array-key: %s. Please get rid of the duplicate entry.\n",
              B->key->ptr);
      ctx->ok = 0;
      B->free(B);
      B = NULL;
    }

    A = C;
    C = NULL;
  }
}

aelements(A) ::= aelements(C) COMMA. {
  A = C;
  C = NULL;
}

aelements(A) ::= aelement(B). {
  A = NULL;
  if (ctx->ok) {
    A = array_init();
    array_insert_unique(A, B);
    B = NULL;
  }
}

aelement(A) ::= expression(B). {
  A = B;
  B = NULL;
}
aelement(A) ::= stringop(B) ARRAY_ASSIGN expression(C). {
  A = NULL;
  if (ctx->ok) {
    buffer_copy_buffer(C->key, B);
    buffer_free(B);
    B = NULL;

    A = C;
    C = NULL;
  }
}

eols ::= EOL.
eols ::= .

globalstart ::= GLOBAL. {
  data_config *dc;
  dc = (data_config *)array_get_element(ctx->srv->config_context, "global");
  force_assert(dc);
  configparser_push(ctx, dc, 0);
}

global ::= globalstart LCURLY metalines RCURLY. {
  force_assert(ctx->current);
  configparser_pop(ctx);
  force_assert(ctx->current);
}

condlines(A) ::= condlines(B) eols ELSE condline(C). {
  A = NULL;
  if (ctx->ok) {
    if (B->context_ndx >= C->context_ndx) {
      fprintf(stderr, "unreachable else condition\n");
      ctx->ok = 0;
    }
    C->prev = B;
    B->next = C;
    A = C;
    B = NULL;
    C = NULL;
  }
}

condlines(A) ::= condline(B). {
  A = B;
  B = NULL;
}

condline(A) ::= context LCURLY metalines RCURLY. {
  A = NULL;
  if (ctx->ok) {
    data_config *cur;

    cur = ctx->current;
    configparser_pop(ctx);

    force_assert(cur && ctx->current);

    A = cur;
  }
}

context ::= DOLLAR SRVVARNAME(B) LBRACKET stringop(C) RBRACKET cond(E) expression(D). {
  data_config *dc;
  buffer *b, *rvalue, *op;

  if (ctx->ok && D->type != TYPE_STRING) {
    fprintf(stderr, "rvalue must be string");
    ctx->ok = 0;
  }

  if (ctx->ok) {
    switch(E) {
    case CONFIG_COND_NE:
      op = buffer_init_string("!=");
      break;
    case CONFIG_COND_EQ:
      op = buffer_init_string("==");
      break;
    case CONFIG_COND_NOMATCH:
      op = buffer_init_string("!~");
      break;
    case CONFIG_COND_MATCH:
      op = buffer_init_string("=~");
      break;
    default:
      force_assert(0);
      return; /* unreachable */
    }

    b = buffer_init();
    buffer_copy_buffer(b, ctx->current->key);
    buffer_append_string(b, "/");
    buffer_append_string_buffer(b, B);
    buffer_append_string_buffer(b, C);
    buffer_append_string_buffer(b, op);
    rvalue = ((data_string*)D)->value;
    buffer_append_string_buffer(b, rvalue);

    if (NULL != (dc = (data_config *)array_get_element(ctx->all_configs, b->ptr))) {
      configparser_push(ctx, dc, 0);
    } else {
      static const struct {
        comp_key_t comp;
        char *comp_key;
        size_t len;
      } comps[] = {
        { COMP_SERVER_SOCKET,      CONST_STR_LEN("SERVER[\"socket\"]"   ) },
        { COMP_HTTP_URL,           CONST_STR_LEN("HTTP[\"url\"]"        ) },
        { COMP_HTTP_HOST,          CONST_STR_LEN("HTTP[\"host\"]"       ) },
        { COMP_HTTP_REFERER,       CONST_STR_LEN("HTTP[\"referer\"]"    ) },
        { COMP_HTTP_USER_AGENT,    CONST_STR_LEN("HTTP[\"useragent\"]"  ) },
        { COMP_HTTP_USER_AGENT,    CONST_STR_LEN("HTTP[\"user-agent\"]"  ) },
        { COMP_HTTP_LANGUAGE,      CONST_STR_LEN("HTTP[\"language\"]"   ) },
        { COMP_HTTP_COOKIE,        CONST_STR_LEN("HTTP[\"cookie\"]"     ) },
        { COMP_HTTP_REMOTE_IP,     CONST_STR_LEN("HTTP[\"remoteip\"]"   ) },
        { COMP_HTTP_REMOTE_IP,     CONST_STR_LEN("HTTP[\"remote-ip\"]"   ) },
        { COMP_HTTP_QUERY_STRING,  CONST_STR_LEN("HTTP[\"querystring\"]") },
        { COMP_HTTP_QUERY_STRING,  CONST_STR_LEN("HTTP[\"query-string\"]") },
        { COMP_HTTP_REQUEST_METHOD, CONST_STR_LEN("HTTP[\"request-method\"]") },
        { COMP_HTTP_SCHEME,        CONST_STR_LEN("HTTP[\"scheme\"]"     ) },
        { COMP_UNSET, NULL, 0 },
      };
      size_t i;

      dc = data_config_init();

      buffer_copy_buffer(dc->key, b);
      buffer_copy_buffer(dc->op, op);
      buffer_copy_buffer(dc->comp_key, B);
      buffer_append_string_len(dc->comp_key, CONST_STR_LEN("[\""));
      buffer_append_string_buffer(dc->comp_key, C);
      buffer_append_string_len(dc->comp_key, CONST_STR_LEN("\"]"));
      dc->cond = E;

      for (i = 0; comps[i].comp_key; i ++) {
        if (buffer_is_equal_string(
              dc->comp_key, comps[i].comp_key, comps[i].len)) {
          dc->comp = comps[i].comp;
          break;
        }
      }
      if (COMP_UNSET == dc->comp) {
        fprintf(stderr, "error comp_key %s", dc->comp_key->ptr);
        ctx->ok = 0;
      }
      else switch(E) {
      case CONFIG_COND_NE:
      case CONFIG_COND_EQ:
        dc->string = buffer_init_buffer(rvalue);
        break;
      case CONFIG_COND_NOMATCH:
      case CONFIG_COND_MATCH: {
#ifdef HAVE_PCRE_H
        const char *errptr;
        int erroff, captures;

        if (NULL == (dc->regex =
            pcre_compile(rvalue->ptr, 0, &errptr, &erroff, NULL))) {
          dc->string = buffer_init_string(errptr);
          dc->cond = CONFIG_COND_UNSET;

          fprintf(stderr, "parsing regex failed: %s -> %s at offset %d\n",
              rvalue->ptr, errptr, erroff);

          ctx->ok = 0;
        } else if (NULL == (dc->regex_study =
            pcre_study(dc->regex, 0, &errptr)) &&
                   errptr != NULL) {
          fprintf(stderr, "studying regex failed: %s -> %s\n",
              rvalue->ptr, errptr);
          ctx->ok = 0;
        } else if (0 != (pcre_fullinfo(dc->regex, dc->regex_study, PCRE_INFO_CAPTURECOUNT, &captures))) {
          fprintf(stderr, "getting capture count for regex failed: %s\n",
              rvalue->ptr);
          ctx->ok = 0;
        } else if (captures > 9) {
          fprintf(stderr, "Too many captures in regex, use (?:...) instead of (...): %s\n",
              rvalue->ptr);
          ctx->ok = 0;
        } else {
          dc->string = buffer_init_buffer(rvalue);
        }
#else
        fprintf(stderr, "can't handle '$%s[%s] =~ ...' as you compiled without pcre support. \n"
                        "(perhaps just a missing pcre-devel package ?) \n",
                        B->ptr, C->ptr);
        ctx->ok = 0;
 #endif
        break;
      }

      default:
        fprintf(stderr, "unknown condition for $%s[%s]\n",
                        B->ptr, C->ptr);
        ctx->ok = 0;
        break;
      }

      if (ctx->ok) configparser_push(ctx, dc, 1);
    }

    buffer_free(b);
    buffer_free(op);
    buffer_free(B);
    B = NULL;
    buffer_free(C);
    C = NULL;
    D->free(D);
    D = NULL;
  }
}
cond(A) ::= EQ. {
  A = CONFIG_COND_EQ;
}
cond(A) ::= MATCH. {
  A = CONFIG_COND_MATCH;
}
cond(A) ::= NE. {
  A = CONFIG_COND_NE;
}
cond(A) ::= NOMATCH. {
  A = CONFIG_COND_NOMATCH;
}

stringop(A) ::= expression(B). {
  A = NULL;
  if (ctx->ok) {
    if (B->type == TYPE_STRING) {
      A = buffer_init_buffer(((data_string*)B)->value);
    } else if (B->type == TYPE_INTEGER) {
      A = buffer_init();
      buffer_copy_int(A, ((data_integer *)B)->value);
    } else {
      fprintf(stderr, "operand must be string");
      ctx->ok = 0;
    }
  }
  B->free(B);
  B = NULL;
}

include ::= INCLUDE stringop(A). {
  if (ctx->ok) {
    if (0 != config_parse_file(ctx->srv, ctx, A->ptr)) {
      ctx->ok = 0;
    }
    buffer_free(A);
    A = NULL;
  }
}

include_shell ::= INCLUDE_SHELL stringop(A). {
  if (ctx->ok) {
    if (0 != config_parse_cmd(ctx->srv, ctx, A->ptr)) {
      ctx->ok = 0;
    }
    buffer_free(A);
    A = NULL;
  }
}