parse.y revision 127807
150764Smarkm%{
250764Smarkm/*
3127807Snectar * Copyright (c) 1998 - 2000 Kungliga Tekniska H�gskolan
450764Smarkm * (Royal Institute of Technology, Stockholm, Sweden).
550764Smarkm * All rights reserved.
650764Smarkm *
750764Smarkm * Redistribution and use in source and binary forms, with or without
850764Smarkm * modification, are permitted provided that the following conditions
950764Smarkm * are met:
1050764Smarkm *
1150764Smarkm * 1. Redistributions of source code must retain the above copyright
1250764Smarkm *    notice, this list of conditions and the following disclaimer.
1350764Smarkm *
1450764Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1550764Smarkm *    notice, this list of conditions and the following disclaimer in the
1650764Smarkm *    documentation and/or other materials provided with the distribution.
1750764Smarkm *
18127807Snectar * 3. Neither the name of the Institute nor the names of its contributors
1950764Smarkm *    may be used to endorse or promote products derived from this software
2050764Smarkm *    without specific prior written permission.
2150764Smarkm *
2250764Smarkm * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2350764Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2450764Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2550764Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2650764Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2750764Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2850764Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2950764Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3050764Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3150764Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3250764Smarkm * SUCH DAMAGE.
3350764Smarkm */
34127807Snectar/* $FreeBSD: head/contrib/com_err/parse.y 127807 2004-04-03 21:17:01Z nectar $ */
3550764Smarkm
3650764Smarkm#include "compile_et.h"
37127807Snectar#include "lex.h"
3850880Smarkm#if 0
39127807SnectarRCSID("$Id: parse.y,v 1.11 2000/06/22 00:42:52 assar Exp $");
4050880Smarkm#endif
4150764Smarkm
4250764Smarkmvoid yyerror (char *s);
43127807Snectarstatic long name2number(const char *str);
4450764Smarkm
4550764Smarkmextern char *yytext;
4650764Smarkm
4750764Smarkm/* This is for bison */
4850764Smarkm
4950764Smarkm#if !defined(alloca) && !defined(HAVE_ALLOCA)
5050764Smarkm#define alloca(x) malloc(x)
5150764Smarkm#endif
5250764Smarkm
5350764Smarkm%}
5450764Smarkm
5550764Smarkm%union {
5650764Smarkm  char *string;
5750764Smarkm  int number;
5850764Smarkm}
5950764Smarkm
6050764Smarkm%token ET INDEX PREFIX EC ID END
6150764Smarkm%token <string> STRING
6250764Smarkm%token <number> NUMBER
6350764Smarkm
6450764Smarkm%%
6550764Smarkm
6650764Smarkmfile		: /* */
6750764Smarkm		| header statements
6850764Smarkm		;
6950764Smarkm
7050764Smarkmheader		: id et
7150764Smarkm		| et
7250764Smarkm		;
7350764Smarkm
7450764Smarkmid		: ID STRING
7550764Smarkm		{
7650764Smarkm		    id_str = $2;
7750764Smarkm		}
7850764Smarkm		;
7950764Smarkm
8050764Smarkmet		: ET STRING
8150764Smarkm		{
8250764Smarkm		    base = name2number($2);
8350764Smarkm		    strncpy(name, $2, sizeof(name));
8450764Smarkm		    name[sizeof(name) - 1] = '\0';
8550764Smarkm		    free($2);
8650764Smarkm		}
8750764Smarkm		| ET STRING STRING
8850764Smarkm		{
8950764Smarkm		    base = name2number($2);
9050764Smarkm		    strncpy(name, $3, sizeof(name));
9150764Smarkm		    name[sizeof(name) - 1] = '\0';
9250764Smarkm		    free($2);
9350764Smarkm		    free($3);
9450764Smarkm		}
9550764Smarkm		;
9650764Smarkm
9750764Smarkmstatements	: statement
9850764Smarkm		| statements statement
9950764Smarkm		;
10050764Smarkm
10150764Smarkmstatement	: INDEX NUMBER
10250764Smarkm		{
10350764Smarkm			number = $2;
10450764Smarkm		}
10550764Smarkm		| PREFIX STRING
10650764Smarkm		{
10750764Smarkm		    prefix = realloc(prefix, strlen($2) + 2);
10850764Smarkm		    strcpy(prefix, $2);
10950764Smarkm		    strcat(prefix, "_");
11050764Smarkm		    free($2);
11150764Smarkm		}
11250764Smarkm		| PREFIX
11350764Smarkm		{
11450764Smarkm		    prefix = realloc(prefix, 1);
11550764Smarkm		    *prefix = '\0';
11650764Smarkm		}
11750764Smarkm		| EC STRING ',' STRING
11850764Smarkm		{
11950764Smarkm		    struct error_code *ec = malloc(sizeof(*ec));
12050764Smarkm
12150764Smarkm		    ec->next = NULL;
12250764Smarkm		    ec->number = number;
12350764Smarkm		    if(prefix && *prefix != '\0') {
12450764Smarkm			asprintf (&ec->name, "%s%s", prefix, $2);
12550764Smarkm			free($2);
12650764Smarkm		    } else
12750764Smarkm			ec->name = $2;
12850764Smarkm		    ec->string = $4;
12950764Smarkm		    APPEND(codes, ec);
13050764Smarkm		    number++;
13150764Smarkm		}
13250764Smarkm		| END
13350764Smarkm		{
13450764Smarkm			YYACCEPT;
13550764Smarkm		}
13650764Smarkm		;
13750764Smarkm
13850764Smarkm%%
13950764Smarkm
140127807Snectarstatic long
14150764Smarkmname2number(const char *str)
14250764Smarkm{
14350764Smarkm    const char *p;
14450764Smarkm    long base = 0;
14550764Smarkm    const char *x = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
14650764Smarkm	"abcdefghijklmnopqrstuvwxyz0123456789_";
14750764Smarkm    if(strlen(str) > 4) {
14850764Smarkm	yyerror("table name too long");
14950764Smarkm	return 0;
15050764Smarkm    }
15150764Smarkm    for(p = str; *p; p++){
15250764Smarkm	char *q = strchr(x, *p);
15350764Smarkm	if(q == NULL) {
15450764Smarkm	    yyerror("invalid character in table name");
15550764Smarkm	    return 0;
15650764Smarkm	}
15750764Smarkm	base = (base << 6) + (q - x) + 1;
15850764Smarkm    }
15950764Smarkm    base <<= 8;
16050764Smarkm    if(base > 0x7fffffff)
16150764Smarkm	base = -(0xffffffff - base + 1);
16250764Smarkm    return base;
16350764Smarkm}
16450764Smarkm
16550764Smarkmvoid
16650764Smarkmyyerror (char *s)
16750764Smarkm{
16850764Smarkm     error_message ("%s\n", s);
16950764Smarkm}
170