aboutsummaryrefslogtreecommitdiff
path: root/src/meta1gram.y
blob: 44751f3db7ef4f390d41da7537956bd192048257 (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
%{
/* MeTA1 configuration parser for GNU Pies.
   Copyright (C) 2008, 2009, 2010, 2011, 2013 Sergey Poznyakoff

   GNU Pies is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GNU Pies is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Pies.  If not, see <http://www.gnu.org/licenses/>. */

/* This file implements a grammar for parsing MeTA1 main configuration file,
   and a set of functions for converting its statements into Pies
   configuration statements. */
  
#include "pies.h"
#include "grecs-locus.h"
#include "meta1lex.h"
#include "meta1gram.h"

#define META1_QUEUE_DIR() \
	(meta1_queue_dir ? meta1_queue_dir : "/var/spool/meta1")

enum meta1_stmt_type
{
  meta1_simple,
  meta1_block
};
 
struct meta1_stmt
{
  struct meta1_stmt *next;
  grecs_locus_t locus;
  enum meta1_stmt_type type;
  const char *ident;
  union
  {
    grecs_value_t *value;
    struct meta1_stmt *list;
  } v;
};

struct meta1_stmt *
meta1_stmt_create (enum meta1_stmt_type type, const char *ident)  
{
  struct meta1_stmt *p = grecs_malloc (sizeof (*p));
  p->next = NULL;
  p->type = type;
  p->ident = ident;
  p->locus = meta1lloc;
  return p;
}

static struct meta1_stmt *
_reverse (struct meta1_stmt *list, struct meta1_stmt **root)
{
  struct meta1_stmt *next;

  if (list->next == NULL)
    {
      *root = list;
      return list;
    }
  next = _reverse (list->next, root);
  next->next = list;
  list->next = NULL;
  return list;
}

static struct meta1_stmt *
reverse (struct meta1_stmt *in)
{
  struct meta1_stmt *root;
  if (!in)
    return in;
  _reverse (in, &root);
  return root;
}

static void meta1_translate (struct meta1_stmt *);
%}

%error-verbose
%locations

%union {
  char *string;
  struct grecs_list *list;
  grecs_value_t *value;
  struct meta1_stmt *stmt;
}

%token <string> META1_IDENT META1_STRING META1_NUMBER
%type <list> slist values list
%type <value> value
%type <string> string ident tag
%type <stmt> stmtlist stmt simple block
%%

input   : stmtlist
          {
	    struct meta1_stmt *stmt;
	    for (stmt = $1; stmt; stmt = stmt->next)
	      meta1_translate (stmt);
	  }
        ;

stmtlist: stmt
          {
	    $$ = $1;
	  }
        | stmtlist stmt
          {
	    $2->next = $1;
	    $$ = $2;
	  }
        ;

stmt    : simple
        | block
        ;

simple  : ident '=' value opt_sc
          {
	    $$ = meta1_stmt_create (meta1_simple, $1);
	    $$->v.value = $3;
	  }
        ;

block   : ident tag '{' stmtlist '}' opt_sc
          {
	    $$ = meta1_stmt_create (meta1_block, $2 ? $2 : $1);
	    $$->v.list = reverse ($4);
	  }
        ;

tag     : /* empty */
          {
	    $$ = NULL;
	  }
        | META1_IDENT
	;

ident   : META1_IDENT
        ;

value   : string
          {
	    $$ = grecs_malloc (sizeof (*$$));
	    $$->type = GRECS_TYPE_STRING;
	    $$->v.string = $1;
	  }
        | list
          {
	    $$ = grecs_malloc (sizeof (*$$));
	    $$->type = GRECS_TYPE_LIST;
	    $$->v.list = $1;
	  }
        | META1_NUMBER
          {
	    $$ = grecs_malloc (sizeof (*$$));
	    $$->type = GRECS_TYPE_STRING;
	    $$->v.string = $1;
	  }
        ;

string  : META1_IDENT
        | slist
          {
	    struct grecs_list_entry *ep;
	    meta1_line_begin ();
	    for (ep = $1->head; ep; ep = ep->next)
	      {
		const char *p = ep->data;
		meta1_line_add (p, strlen (p));
	      }
	    $$ = meta1_line_finish ();
	  }
        ;

slist   : META1_STRING
          {
	    $$ = grecs_list_create ();
	    grecs_list_append ($$, $1);
	  }
        | slist META1_STRING
          {
	    grecs_list_append ($1, $2);
	    $$ = $1;
	  }
        ;

list    : '{' values '}'
          {
	    $$ = $2;
	  }
        | '{' values ',' '}'
          {
	    $$ = $2;
	  }
        ;

values  : value
          {
	    $$ = grecs_list_create ();
	    grecs_list_append ($$, $1);
	  }
        | values ',' value
          {
	    grecs_list_append ($1, $3);
	    $$ = $1;
	  }
        ;

opt_sc  : /* empty */
        | ';'
        ;
	  
%%
int
yyerror (char const *s)
{
  grecs_error (&yylloc, 0, "%s", s);
  return 0;
}

void
meta1_parser_set_debug ()
{
  char *p = getenv ("META1_DEBUG_YACC");
  yydebug = p && (*p - '0') > 0;
}

static struct meta1_stmt *
find_stmt (struct meta1_stmt *stmt, const char *ident)
{
  for (; stmt; stmt = stmt->next)
    if (strcmp (stmt->ident, ident) == 0)
      break;
  return stmt;
}


struct node_trans
{
  char *name;
  char *new_name;
  int (*xlat) (struct meta1_stmt *stmt, struct component *comp);
};

static struct node_trans *
find_node_trans (struct node_trans *tab, const char *name)
{
  for (; tab->name; tab++)
    if (strcmp (tab->name, name) == 0)
      return tab;
  return NULL;
}

static int
xlat_listen_socket (struct meta1_stmt *stmt, struct component *comp)
{
  struct meta1_stmt *p;
  grecs_value_t *val;
  
  p = find_stmt (stmt->v.list, "type");
  if (!p || !p->v.value || p->v.value->type != GRECS_TYPE_STRING)
    return 1;
  meta1_line_begin ();
  meta1_line_add (p->v.value->v.string, strlen (p->v.value->v.string));
  meta1_line_add ("://", 3);
  if (strcmp (p->v.value->v.string, "inet") == 0)
    {
      const char *addr;
      p = find_stmt (stmt->v.list, "address");
      if (p)
	{
	  if (p->v.value->type != GRECS_TYPE_STRING)
	    return 1;
	  addr = p->v.value->v.string;
	}
      else
	addr = "0.0.0.0";
      meta1_line_add (addr, strlen (addr));
      meta1_line_add (":", 1);
      p = find_stmt (stmt->v.list, "port");
      if (p->v.value->type != GRECS_TYPE_STRING)
	return 1;
      meta1_line_add (p->v.value->v.string, strlen (p->v.value->v.string));
    }
  else if (strcmp (p->v.value->v.string, "unix") == 0)
    {
      /*  listen_socket {
	         type=unix;
		 path = /tmp/socket;
		 umask = 077;
		 user = user;
                 group = group;
	   }
      */
      p = find_stmt (stmt->v.list, "path");
      if (!p || p->v.value->type != GRECS_TYPE_STRING)
	return 1;
      meta1_line_add (p->v.value->v.string, strlen (p->v.value->v.string));

      p = find_stmt (stmt->v.list, "user");
      if (p && p->v.value->type == GRECS_TYPE_STRING)
	{
	  meta1_line_add (";user=", 6);
	  meta1_line_add (p->v.value->v.string, strlen (p->v.value->v.string));
	}

      p = find_stmt (stmt->v.list, "group");
      if (p && p->v.value->type == GRECS_TYPE_STRING)
	{
	  meta1_line_add (";group=", 7);
	  meta1_line_add (p->v.value->v.string, strlen (p->v.value->v.string));
	}

      p = find_stmt (stmt->v.list, "umask");
      if (p && p->v.value->type == GRECS_TYPE_STRING)
	{
	  meta1_line_add (";umask=", 7);
	  meta1_line_add (p->v.value->v.string, strlen (p->v.value->v.string));
	}
    }
  val = grecs_malloc (sizeof (*val));
  val->type = GRECS_TYPE_STRING;
  val->v.string = meta1_line_finish ();
  stmt->type = meta1_simple;
  stmt->v.value = val;
  return 0;
}


static struct node_trans root_node_trans[] = {
  { "listen_socket", "socket", xlat_listen_socket },
  { "start_action", "mode" },
  { "pass_fd_socket", "pass-fd-socket" },
  { "user", "user" },
  { "path", "program" },
  { "arguments", "command" },
  { "restart_dependencies", "dependents", },
  { NULL }
};

static void
meta1_translate_stmt (struct meta1_stmt *stmt, struct component *comp)
{
  struct grecs_keyword *kwp;
  
  struct node_trans *nt = find_node_trans (root_node_trans, stmt->ident);
  if (!nt)
    return;

  kwp = find_component_keyword (nt->new_name);
  if (!kwp)
    abort ();

  if (nt->xlat && nt->xlat (stmt, comp))
    return;
  
  grecs_process_ident (kwp, stmt->v.value, comp, &stmt->locus);
}

static void
meta1_translate (struct meta1_stmt *stmt)
{
  struct component *comp;
  struct meta1_stmt *p;
  size_t len;
  
  if (stmt->type != meta1_block)
    return;
  
  comp = component_create (stmt->ident);
  for (p = stmt->v.list; p; p = p->next)
    {
      meta1_translate_stmt (p, comp);
    }
  comp->privs.allgroups = 1;
  comp->dir = META1_QUEUE_DIR ();
  comp->redir[RETR_ERR].type = redir_file;
  comp->redir[RETR_ERR].v.file = NULL;
  len = 0;
  grecs_asprintf (&comp->redir[RETR_ERR].v.file, &len,
		  "%s/%s.log", META1_QUEUE_DIR (), comp->tag);
  component_finish (comp, &stmt->locus);
}

Return to:

Send suggestions and report system problems to the System administrator.