150764Smarkm%{
250764Smarkm/*
3233294Sstas * Copyright (c) 1998 - 2000 Kungliga Tekniska H��gskolan
4233294Sstas * (Royal Institute of Technology, Stockholm, Sweden).
5233294Sstas * All rights reserved.
650764Smarkm *
7233294Sstas * Redistribution and use in source and binary forms, with or without
8233294Sstas * modification, are permitted provided that the following conditions
9233294Sstas * are met:
1050764Smarkm *
11233294Sstas * 1. Redistributions of source code must retain the above copyright
12233294Sstas *    notice, this list of conditions and the following disclaimer.
1350764Smarkm *
14233294Sstas * 2. Redistributions in binary form must reproduce the above copyright
15233294Sstas *    notice, this list of conditions and the following disclaimer in the
16233294Sstas *    documentation and/or other materials provided with the distribution.
1750764Smarkm *
18233294Sstas * 3. Neither the name of the Institute nor the names of its contributors
19233294Sstas *    may be used to endorse or promote products derived from this software
20233294Sstas *    without specific prior written permission.
2150764Smarkm *
22233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
23233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
26233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32233294Sstas * SUCH DAMAGE.
3350764Smarkm */
3450764Smarkm
3550764Smarkm#include "compile_et.h"
36127807Snectar#include "lex.h"
3750764Smarkm
3850764Smarkmvoid yyerror (char *s);
39127807Snectarstatic long name2number(const char *str);
4050764Smarkm
4150764Smarkmextern char *yytext;
4250764Smarkm
4350764Smarkm/* This is for bison */
4450764Smarkm
4550764Smarkm#if !defined(alloca) && !defined(HAVE_ALLOCA)
4650764Smarkm#define alloca(x) malloc(x)
4750764Smarkm#endif
4850764Smarkm
49233294Sstas#define YYMALLOC malloc
50233294Sstas#define YYFREE free
51233294Sstas
5250764Smarkm%}
5350764Smarkm
5450764Smarkm%union {
5550764Smarkm  char *string;
5650764Smarkm  int number;
5750764Smarkm}
5850764Smarkm
5950764Smarkm%token ET INDEX PREFIX EC ID END
6050764Smarkm%token <string> STRING
6150764Smarkm%token <number> NUMBER
6250764Smarkm
6350764Smarkm%%
6450764Smarkm
65233294Sstasfile		: /* */
6650764Smarkm		| header statements
6750764Smarkm		;
6850764Smarkm
6950764Smarkmheader		: id et
7050764Smarkm		| et
7150764Smarkm		;
7250764Smarkm
7350764Smarkmid		: ID STRING
7450764Smarkm		{
7550764Smarkm		    id_str = $2;
7650764Smarkm		}
7750764Smarkm		;
7850764Smarkm
7950764Smarkmet		: ET STRING
8050764Smarkm		{
81178846Sdfr		    base_id = name2number($2);
82178846Sdfr		    strlcpy(name, $2, sizeof(name));
8350764Smarkm		    free($2);
8450764Smarkm		}
8550764Smarkm		| ET STRING STRING
8650764Smarkm		{
87178846Sdfr		    base_id = name2number($2);
88178846Sdfr		    strlcpy(name, $3, sizeof(name));
8950764Smarkm		    free($2);
9050764Smarkm		    free($3);
9150764Smarkm		}
9250764Smarkm		;
9350764Smarkm
9450764Smarkmstatements	: statement
9550764Smarkm		| statements statement
9650764Smarkm		;
9750764Smarkm
98233294Sstasstatement	: INDEX NUMBER
9950764Smarkm		{
10050764Smarkm			number = $2;
10150764Smarkm		}
10250764Smarkm		| PREFIX STRING
10350764Smarkm		{
104178846Sdfr		    free(prefix);
105178846Sdfr		    asprintf (&prefix, "%s_", $2);
106178846Sdfr		    if (prefix == NULL)
107178846Sdfr			errx(1, "malloc");
10850764Smarkm		    free($2);
10950764Smarkm		}
11050764Smarkm		| PREFIX
11150764Smarkm		{
11250764Smarkm		    prefix = realloc(prefix, 1);
113178846Sdfr		    if (prefix == NULL)
114178846Sdfr			errx(1, "malloc");
11550764Smarkm		    *prefix = '\0';
11650764Smarkm		}
11750764Smarkm		| EC STRING ',' STRING
11850764Smarkm		{
11950764Smarkm		    struct error_code *ec = malloc(sizeof(*ec));
120233294Sstas
121178846Sdfr		    if (ec == NULL)
122178846Sdfr			errx(1, "malloc");
12350764Smarkm
12450764Smarkm		    ec->next = NULL;
12550764Smarkm		    ec->number = number;
12650764Smarkm		    if(prefix && *prefix != '\0') {
12750764Smarkm			asprintf (&ec->name, "%s%s", prefix, $2);
128178846Sdfr			if (ec->name == NULL)
129178846Sdfr			    errx(1, "malloc");
13050764Smarkm			free($2);
13150764Smarkm		    } else
13250764Smarkm			ec->name = $2;
13350764Smarkm		    ec->string = $4;
13450764Smarkm		    APPEND(codes, ec);
13550764Smarkm		    number++;
13650764Smarkm		}
13750764Smarkm		| END
13850764Smarkm		{
13950764Smarkm			YYACCEPT;
14050764Smarkm		}
14150764Smarkm		;
14250764Smarkm
14350764Smarkm%%
14450764Smarkm
145127807Snectarstatic long
14650764Smarkmname2number(const char *str)
14750764Smarkm{
14850764Smarkm    const char *p;
149178846Sdfr    long num = 0;
15050764Smarkm    const char *x = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
15150764Smarkm	"abcdefghijklmnopqrstuvwxyz0123456789_";
15250764Smarkm    if(strlen(str) > 4) {
15350764Smarkm	yyerror("table name too long");
15450764Smarkm	return 0;
15550764Smarkm    }
15650764Smarkm    for(p = str; *p; p++){
15750764Smarkm	char *q = strchr(x, *p);
15850764Smarkm	if(q == NULL) {
15950764Smarkm	    yyerror("invalid character in table name");
16050764Smarkm	    return 0;
16150764Smarkm	}
162178846Sdfr	num = (num << 6) + (q - x) + 1;
16350764Smarkm    }
164178846Sdfr    num <<= 8;
165178846Sdfr    if(num > 0x7fffffff)
166178846Sdfr	num = -(0xffffffff - num + 1);
167178846Sdfr    return num;
16850764Smarkm}
16950764Smarkm
17050764Smarkmvoid
17150764Smarkmyyerror (char *s)
17250764Smarkm{
173233294Sstas     _lex_error_message ("%s\n", s);
17450764Smarkm}
175