aux-item-def-parse.y 18.7 KB
Newer Older
1 2
%{
/*
3 4
 * $Id: aux-item-def-parse.y,v 1.25 2005/12/18 22:18:12 ceder Exp $
 * Copyright (C) 1994-1996, 1999, 2001-2005  Lysator Academic Computer Association.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM 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 1, or (at your option) 
 * any later version.
 * 
 * LysKOM 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 LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
Per Cederqvist's avatar
Per Cederqvist committed
24
 * Please report bugs at http://bugzilla.lysator.liu.se/. 
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
/* Rename hack from the automake 1.4 manual. */

#define	yymaxdepth aid_maxdepth
#define	yyparse	aid_parse
#define	yylex	aid_lex
#define	yyerror	aid_error
#define	yylval	aid_lval
#define	yychar	aid_char
#define	yydebug	aid_debug
#define	yypact	aid_pact
#define	yyr1	aid_r1
#define	yyr2	aid_r2
#define	yydef	aid_def
#define	yychk	aid_chk
#define	yypgo	aid_pgo
#define	yyact	aid_act
#define	yyexca	aid_exca
#define yyerrflag aid_errflag
#define yynerrs	aid_nerrs
#define	yyps	aid_ps
#define	yypv	aid_pv
#define	yys	aid_s
#define	yy_yys	aid_yys
#define	yystate	aid_state
#define	yytmp	aid_tmp
#define	yyv	aid_v
#define	yy_yyv	aid_yyv
#define	yyval	aid_val
#define	yylloc	aid_lloc
#define yyreds	aid_reds
#define yytoks	aid_toks
#define yylhs	aid_yylhs
#define yylen	aid_yylen
#define yydefred aid_yydefred
#define yydgoto	aid_yydgoto
#define yysindex aid_yysindex
#define yyrindex aid_yyrindex
#define yygindex aid_yygindex
#define yytable	 aid_yytable
#define yycheck	 aid_yycheck
#define yyname   aid_yyname
#define yyrule   aid_yyrule
69 70 71 72 73 74 75 76 77 78 79 80 81

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#if STDC_HEADERS || HAVE_STRING_H
#  include <string.h>
#else
#  include <strings.h>
#endif
#ifdef HAVE_STDARG_H
#  include <stdarg.h>
#endif
82
#include <stdlib.h>
David Byers's avatar
Server  
David Byers committed
83
#include <setjmp.h>
84
#include "timewrap.h"
85 86

#include "kom-types.h"
David Byers's avatar
Server  
David Byers committed
87 88 89
#include "com.h"
#include "async.h"
#include "connections.h"
90 91 92 93 94 95
#include "aux-items.h"
#include "s-string.h"
#include "server/smalloc.h"
#include "lyskomd.h"
#include "debug.h"
#include "log.h"
David Byers's avatar
David Byers committed
96
#include "string-malloc.h"
97
#include "eintr.h"
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

BUGDECL;    

#define YYDEBUG 1
    
static Aux_item_definition      def;
static Bool                     errorFlag = FALSE;

extern int yylex(void);

static char *inputFile;
int aux_item_def_error_line;
extern int yylineno;

#if defined(HAVE_VFPRINTF) && defined(HAVE_STDARG_H)
void yyerror(const char * format, ...)
#  if HAVE_ATTRIBUTE_FORMAT_PRINTF
    __attribute__ ((format (printf, 1, 2)))
#  endif
    ;
#else
void yyerror();
#endif

struct aux_item_def_value_type;
123 124 125 126 127 128 129 130 131 132 133 134 135 136
static void chk_assign(int *,
		       int *,
		       const char  *,
		       void  *,
		       int,
		       String,
		       struct aux_item_def_value_type *,
		       int);
static Aux_item_flags * chk_flag_a(int         *found,
				   int         *good,
				   const char  *id,
				   String       data,
				   struct aux_item_def_value_type *val,
				   int          lineno);
137
static const char *aux_item_def_typename(int);
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
static void
aux_item_def_check_trigger(int *found,
			   int *good,
			   const char *check_name,
			   int type,
			   String trigger_name,
			   String function_name,
			   unsigned long *counter,
			   Aux_item_trigger **triggers);

static void
aux_item_def_check_validate(int *found,
			    int *good,
			    const char *check_name,
			    String field_name,
			    int type,
			    String data,
			    Aux_item_definition *def);
David Byers's avatar
David Byers committed
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

#define YYERROR_VERBOSE
%}

%union
{
    String          str;
    unsigned long   num;
    struct aux_item_def_value_type
    {
        int         type;
        union
        {
            String          str;
            unsigned long   num;
        } val;
    } value;
}

%token <num>    NUMBER BOOLEAN
%token <str>    ID STRING
%token          DISABLED TEXT CONFERENCE LETTERBOX TOK_SERVER TOK_ANY
%token          VOID CREATE MODIFY

%type <value>   value
%type <num>     action

%%

items       :       items item
            |       /* Empty */
            ;

item        :       head '{' body '}'
            {
                if (def.tag != 0)
                {
                    aux_item_definition_add(&def);
                }
                def = empty_aux_item_definition;
            }
            ;

head        :       NUMBER ':' ID '(' targets ')'
            {
                def.tag = $1;
                def.name = s_crea_c_str($3);
                if (buglevel)
                {
Per Cederqvist's avatar
Per Cederqvist committed
206
                    kom_log("Parsing definition of aux-item %ld (%s)\n",
David Byers's avatar
David Byers committed
207
                            def.tag, def.name);
208 209 210 211 212 213 214
                }
                s_clear(&($3));
                $3 = EMPTY_STRING;
                yylval.str = EMPTY_STRING;
            }
            |       NUMBER ':' ID '(' targets ')' DISABLED
            {
David Byers's avatar
David Byers committed
215
                def.tag = $1;
216
                def.name = s_crea_c_str($3);
David Byers's avatar
David Byers committed
217
                def.disabled = TRUE;
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
                s_clear(&($3));
                $3 = EMPTY_STRING;
                yylval.str = EMPTY_STRING;
            }
            ;

targets     :       targets ',' target
            |       target
            ;

target      :       action TEXT       { def.texts = TRUE; def.text_a = $1; }
            |       action CONFERENCE { def.confs = TRUE; def.conf_a = $1; }
            |       action LETTERBOX  { def.letterboxes = TRUE;
                                        def.conf_a = $1; }
            |       TOK_SERVER        { def.system = TRUE; }
            |       action TOK_ANY    
            {
                def.texts       = TRUE; def.text_a = $1;
                def.confs       = TRUE; def.conf_a = $1;
                def.letterboxes = TRUE; 
                def.system      = TRUE;
            }
            ;

action      :       action CREATE     { $$ = $1 | AUX_ITEM_ADD_ON_CREATE; }
            |       action MODIFY     { $$ = $1 | AUX_ITEM_ADD_ON_MODIFY; }
            |       /* Empty */       { $$ = 0; }
245
            ;
246 247 248 249 250 251 252 253 254

body        :       body assign
            |       body error
            |       /* Empty */
            ;

assign      :   ID '=' value ';'
            {
                int found = 0;
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
		int good = 0;
		Aux_item_flags *f;

                chk_assign(&found, &good, "author-only", &def.author_only,
			   BOOLEAN, $1, &$3, @3.first_line);
                chk_assign(&found, &good, "supervisor-only",
			   &def.supervisor_only,
                           BOOLEAN, $1, &$3, @3.first_line);
                chk_assign(&found, &good, "system-only", &def.system_only,
                           BOOLEAN, $1, &$3, @3.first_line);
                chk_assign(&found, &good, "inherit-limit", &def.inherit_limit,
			   NUMBER, $1, &$3, @3.first_line);
                chk_assign(&found, &good, "unique", &def.one_per_person,
			   BOOLEAN, $1, &$3, @3.first_line);
                chk_assign(&found, &good, "unique-data", &def.unique_data,
			   BOOLEAN, $1, &$3, @3.first_line);
                chk_assign(&found, &good, "permanent", &def.may_not_delete,
			   BOOLEAN, $1, &$3, @3.first_line);
                chk_assign(&found, &good, "owner-delete", &def.owner_delete,
			   BOOLEAN, $1, &$3, @3.first_line);

                if ((f = chk_flag_a(&found, &good, "inherit",
				    $1, &$3, @3.first_line)) != NULL)
		    f->inherit = 1;

		if ((f = chk_flag_a(&found, &good, "secret",
				    $1, &$3, @3.first_line)) != NULL)
		    f->secret = 1;

		if ((f = chk_flag_a(&found, &good, "hide-creator",
				    $1, &$3, @3.first_line)) != NULL)
		    f->hide_creator = 1;

		if ((f = chk_flag_a(&found, &good, "dont-garb",
				    $1, &$3, @3.first_line)) != NULL)
		    f->dont_garb = 1;

		if ((f = chk_flag_a(&found, &good, "reserved-2",
				    $1, &$3, @3.first_line)) != NULL)
		    f->reserved3 = 1;

		if ((f = chk_flag_a(&found, &good, "reserved-3",
				    $1, &$3, @3.first_line)) != NULL)
		    f->reserved4 = 1;

		if ((f = chk_flag_a(&found, &good, "reserved-4",
				    $1, &$3, @3.first_line)) != NULL)
		    f->reserved5 = 1;

		aux_item_def_check_validate(&found, &good,
					    "validate",
					    $1,
					    $3.type,
					    $3.val.str,
					    &def);

		aux_item_def_check_trigger(&found, &good, "delete-trigger",
					   $3.type,
					   $1,
					   $3.val.str,
					   &def.num_delete_triggers,
					   &def.delete_triggers);


		aux_item_def_check_trigger(&found, &good, "undelete-trigger",
					   $3.type,
					   $1,
					   $3.val.str,
					   &def.num_undelete_triggers,
					   &def.undelete_triggers);


		aux_item_def_check_trigger(&found, &good, "add-trigger",
					   $3.type,
					   $1,
					   $3.val.str,
					   &def.num_add_triggers,
					   &def.add_triggers);
333 334 335 336 337 338 339 340 341


                if (found == 0)
                {
                    char *tmp;

                    tmp = s_crea_c_str($1);
                    aux_item_def_error_line = @1.first_line;
                    yyerror("invalid field name: %s", tmp);
David Byers's avatar
David Byers committed
342
                    string_free(tmp);
343 344
                }

David Byers's avatar
David Byers committed
345
                if ($3.type == STRING || $3.type == ID)
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
                {
                    s_clear(&($3).val.str);
                    $3.val.str = EMPTY_STRING;
                    yylval.value.val.str = EMPTY_STRING;
                }
                s_clear(&($1));
                $1 = EMPTY_STRING;
                yylval.str = EMPTY_STRING;
            }
            ;

value       :       BOOLEAN             { $$.val.num = $1; $$.type = BOOLEAN; }
            |       STRING              { $$.val.str = $1; $$.type = STRING;  }
            |       NUMBER              { $$.val.num = $1; $$.type = NUMBER;  }
            |       ID '(' ')'          { $$.val.str = $1; $$.type = ID;}
            |       VOID                { YYERROR; }
            ;

%%

extern FILE *yyin;

#if defined(HAVE_VFPRINTF) && defined(HAVE_STDARG_H)
void yyerror(const char *format, ...)
{
    va_list     AP;

    va_start(AP, format);
    fprintf(stderr, "%s: %d: ", inputFile, aux_item_def_error_line);
    vfprintf(stderr, format, AP);
    fprintf(stderr, "\n");
    fflush(stderr);

    errorFlag = TRUE;
    va_end(AP);
}
#else
void yyerror(const char *s, 
             int a, int b, int c, int d, int e, int f, int g)
{
    fprintf(stderr, "%s: %d: ",  inputFile, aux_item_def_error_line);
    fprintf(stderr, format, a, b, c, d, e, f, g);
    fprintf(stderr, "\n");
    fflush(stderr);

    errorFlag = TRUE;
}
#endif

static const char *aux_item_def_typename(int type)
{
    switch (type)
    {
    case STRING:  return "string";
    case NUMBER:  return "number";
    case BOOLEAN: return "boolean";
    case ID:      return "identifier";
    default:
        return "unknown";
    }
}

408 409 410 411 412 413 414 415 416
static void
chk_assign(int         *found,
	   int         *good,
	   const char  *id,
	   void        *data,
	   int          type,
	   String       field,
	   struct aux_item_def_value_type *val,
	   int          lineno)
417
{
418 419 420
    if (*found)
	return;

421 422
    if (!s_strcmp(s_fcrea_str(id), field))
    {
423
	*found = 1;
424 425 426 427 428 429
        if (type != val->type)
        {
            aux_item_def_error_line = lineno;
            yyerror("invalid type: expected %s, got %s",
                     aux_item_def_typename(type),
                     aux_item_def_typename(val->type));
430 431
	    *good = 0;
	    return;
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
        }
        else if (type == STRING)
        {
            *((char **)data) = s_crea_c_str(val->val.str);
        }
        else if (type == NUMBER)
        {
            *((unsigned long *)data) = val->val.num;
        }
        else if (type == BOOLEAN)
        {
            *((Bool *)data) = val->val.num ? TRUE : FALSE;
        }
        else
        {
            restart_kom("Internal error: bad type in aux-item definition "
                        "assignment (can't happen.)\n");
        }
450
        *good = 1;
451
    }
452 453 454 455 456 457 458 459 460 461 462 463 464 465
}

static Aux_item_flags *
chk_flag_a(int         *found,
	   int         *good,
	   const char  *id,
	   String       field,
	   struct aux_item_def_value_type *val,
	   int          lineno)
{
    if (*found)
	return NULL;

    if (!s_strcmp(s_fcrea_str(id), field))
466
    {
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
	*found = 1;
	if (val->type != BOOLEAN )
	{
	    aux_item_def_error_line = lineno;
	    yyerror("invalid type: expected %s, got %s",
		    aux_item_def_typename(BOOLEAN),
		    aux_item_def_typename(val->type));
	    *good = 0;
	}
	else
	{
	    *good = 1;
	    if (val->val.num)
		return &def.set_flags;
	    else
		return &def.clear_flags;
	}
484
    }
485 486

    return NULL;
487 488
}

489 490 491 492 493 494 495 496 497 498

static void
aux_item_def_check_trigger(int *found,
			   int *good,
			   const char *check_name,
			   int type,
			   String trigger_name,
			   String function_name,
			   unsigned long *counter,
			   Aux_item_trigger **triggers)
499 500 501 502
{
    Aux_item_trigger trigger;
    char *tmp_string;

503 504 505
    if (*found)
	return;

506 507
    if (s_strcmp(s_fcrea_str(check_name), trigger_name) == 0)
    {
508
	*found = 1;
509 510 511 512 513
        if (type != ID)
        {
            yyerror("invalid type: expected %s, got %s", 
                    aux_item_def_typename(ID), 
                    aux_item_def_typename(type)); 
514
            return;
515 516 517 518 519 520 521 522 523
        }

        tmp_string = s_crea_c_str(function_name);
        trigger = aux_item_find_trigger(tmp_string);

        if (trigger == NULL)
        {
            yyerror("undefined function: %s",
                    tmp_string);
524
	    string_free(tmp_string);
525
            return;
526 527
        }

528
        string_free(tmp_string);
529 530 531 532 533 534 535
        *counter += 1;
        *triggers =
            srealloc(*triggers,
                     *counter *
                     sizeof(Aux_item_trigger));
        *triggers[*counter-1] = trigger;

536
	*good = 1;
537 538 539 540 541 542
    }
}

void parse_aux_item_definitions(char *file)
{
    inputFile = file;
543
    yyin = i_fopen(file, "r");
544 545 546 547 548 549 550 551
    if (yyin == NULL)
    {
        perror(file);
        restart_kom("Unable to open aux-item definition file\n");
    }

    def = empty_aux_item_definition;
    yyparse();
552
    i_fclose(yyin);
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

    if (errorFlag == TRUE)
    {
        restart_kom("Errors reading aux-item definition file\n");
    }

    /*
      {
        extern Aux_item_definition *aux_item_definition_list;
        extern unsigned long num_aux_item_definitions;
        Aux_item_definition *def;

        fprintf(stderr, "Number of defs: %ld\n", num_aux_item_definitions);
        def = aux_item_definition_list;

        while (def != NULL)
        {
            fprintf(stderr, "Name:            '%s'\n", def->name);
            fprintf(stderr, "Tag:             %ld\n", def->tag);
            fprintf(stderr, "Clear flags:     ");
            if (def->clear_flags.deleted) fprintf(stderr, "deleted ");
            if (def->clear_flags.inherit) fprintf(stderr, "inherit ");
            if (def->clear_flags.secret) fprintf(stderr, "secret ");
            if (def->clear_flags.hide_creator) fprintf(stderr,"hide_creator ");
            if (def->clear_flags.reserved2) fprintf(stderr, "reserved2 ");
            if (def->clear_flags.reserved3) fprintf(stderr, "reserved3 ");
            if (def->clear_flags.reserved4) fprintf(stderr, "reserved4 ");
            if (def->clear_flags.reserved5) fprintf(stderr, "reserved5 ");
            fprintf(stderr, "\n");
            fprintf(stderr, "Set flags:       ");
            if (def->set_flags.deleted) fprintf(stderr, "deleted ");
            if (def->set_flags.inherit) fprintf(stderr, "inherit ");
            if (def->set_flags.secret) fprintf(stderr, "secret ");
            if (def->set_flags.hide_creator) fprintf(stderr,"hide_creator ");
            if (def->set_flags.reserved2) fprintf(stderr, "reserved2 ");
            if (def->set_flags.reserved3) fprintf(stderr, "reserved3 ");
            if (def->set_flags.reserved4) fprintf(stderr, "reserved4 ");
            if (def->set_flags.reserved5) fprintf(stderr, "reserved5 ");
            fprintf(stderr, "\n");
            fprintf(stderr, "Author only:     %d\n", def->author_only);
            fprintf(stderr, "Supervisor only: %d\n", def->supervisor_only);
            fprintf(stderr, "Unique:          %d\n", def->one_per_person);
Per Cederqvist's avatar
Per Cederqvist committed
595
            fprintf(stderr, "Unique-data:     %d\n", def->unique_data);
596 597 598 599 600 601 602 603 604 605 606 607
            fprintf(stderr, "Permanent:       %d\n", def->may_not_delete);
            fprintf(stderr, "Inherit limit:   %ld\n", def->inherit_limit);
            fprintf(stderr, "Texts:           %d\n", def->texts);
            fprintf(stderr, "Conferences:     %d\n", def->confs);
            fprintf(stderr, "Letterboxes:     %d\n", def->letterboxes);
            fprintf(stderr, "Validate regexp: '%s'\n",
                    def->validate_regexp?def->validate_regexp:"0x0");
            def = def->next;
        }
    }
    */
}
David Byers's avatar
David Byers committed
608 609


610 611 612 613 614 615 616
static void aux_item_def_check_validate(int *found,
					int *good,
					const char *check_name,
					String field_name,
					int type,
					String data,
					Aux_item_definition *def)
David Byers's avatar
David Byers committed
617 618 619 620
{
    Aux_item_validation_function        validator;
    char                               *tmp_string;

621 622 623
    if (*found)
	return;

David Byers's avatar
David Byers committed
624 625
    if (s_strcmp(s_fcrea_str(check_name), field_name) == 0)
    {
626 627
	*found = 1;

David Byers's avatar
David Byers committed
628 629 630 631 632 633 634 635
        /*
         * Validator is a function
         */

        if (type == ID)
        {
            tmp_string = s_crea_c_str(data);
            validator = aux_item_find_validator(tmp_string);
David Byers's avatar
David Byers committed
636
            string_free(tmp_string);
David Byers's avatar
David Byers committed
637 638 639 640 641

            if (validator == NULL)
            {
                yyerror("undefined function: %s",
                        tmp_string);
642
                return;
David Byers's avatar
David Byers committed
643 644 645 646 647 648 649 650 651
            }

            def->num_validators += 1;
            def->validators = srealloc(def->validators,
                                       def->num_validators *
                                       sizeof(*def->validators));

            def->validators[def->num_validators-1].type = AUX_VALIDATE_FUNCTION;
            def->validators[def->num_validators-1].v.fn.function = validator;
652
	    *good = 1;
David Byers's avatar
David Byers committed
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
        }
        else if (type == STRING)
        {
            /*
             * Validator is a string (regexp)
             */

            def->num_validators += 1;
            def->validators = srealloc(def->validators,
                                       def->num_validators *
                                       sizeof(*def->validators));


            def->validators[def->num_validators-1].type = AUX_VALIDATE_REGEXP;
            def->validators[def->num_validators-1].v.re.regexp = s_crea_c_str(data);
            def->validators[def->num_validators-1].v.re.cached_re_buf = NULL;
669
	    *good = 1;
David Byers's avatar
David Byers committed
670 671 672 673 674 675 676 677 678 679
        }
        else
        {
            yyerror("invalid type: expected %s or %s, got %s", 
                    aux_item_def_typename(ID), 
                    aux_item_def_typename(STRING), 
                    aux_item_def_typename(type)); 
        }
    }
}