1/*	$NetBSD: sel-lex.l,v 1.1.1.2 2011/04/14 14:08:57 elric Exp $	*/
2
3%{
4/*
5 * Copyright (c) 2004, 2008 Kungliga Tekniska Högskolan
6 * (Royal Institute of Technology, Stockholm, Sweden).
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of the Institute nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37/* Id */
38
39#ifdef HAVE_CONFIG_H
40#include <config.h>
41#endif
42
43#undef ECHO
44
45#include <stdio.h>
46#include <string.h>
47#include <stdarg.h>
48#include <stdlib.h>
49#include "sel.h"
50#include "sel-gram.h"
51unsigned lineno = 1;
52
53static char * handle_string(void);
54static int lex_input(char *, int);
55
56struct hx_expr_input _hx509_expr_input;
57
58#ifndef YY_NULL
59#define YY_NULL 0
60#endif
61
62#define YY_NO_UNPUT 1
63
64#undef YY_INPUT
65#define YY_INPUT(buf,res,maxsize) (res = lex_input(buf, maxsize))
66
67#undef ECHO
68
69%}
70%%
71
72TRUE			{ return kw_TRUE; }
73FALSE			{ return kw_FALSE; }
74AND			{ return kw_AND; }
75OR			{ return kw_OR; }
76IN			{ return kw_IN; }
77TAILMATCH		{ return kw_TAILMATCH; }
78
79[A-Za-z][-A-Za-z0-9_]*	{
80			  yylval.string = strdup ((const char *)yytext);
81			  return IDENTIFIER;
82			}
83"\""			{ yylval.string = handle_string(); return STRING; }
84\n			{ ++lineno; }
85[,.!={}()%]		{ return *yytext; }
86[ \t]			;
87%%
88
89static char *
90handle_string(void)
91{
92    char x[1024];
93    int i = 0;
94    int c;
95    int quote = 0;
96    while((c = input()) != EOF){
97	if(quote) {
98	    x[i++] = '\\';
99	    x[i++] = c;
100	    quote = 0;
101	    continue;
102	}
103	if(c == '\n'){
104	    _hx509_sel_yyerror("unterminated string");
105	    lineno++;
106	    break;
107	}
108	if(c == '\\'){
109	    quote++;
110	    continue;
111	}
112	if(c == '\"')
113	    break;
114	x[i++] = c;
115    }
116    x[i] = '\0';
117    return strdup(x);
118}
119
120int
121yywrap ()
122{
123     return 1;
124}
125
126static int
127lex_input(char *buf, int max_size)
128{
129    int n;
130
131    n = _hx509_expr_input.length - _hx509_expr_input.offset;
132    if (max_size < n)
133        n = max_size;
134    if (n <= 0)
135	return YY_NULL;
136
137    memcpy(buf, _hx509_expr_input.buf + _hx509_expr_input.offset, n);
138    _hx509_expr_input.offset += n;
139
140    return n;
141}
142