sel-lex.l revision 226031
11556Srgrimes%{
21556Srgrimes/*
31556Srgrimes * Copyright (c) 2004, 2008 Kungliga Tekniska H��gskolan
41556Srgrimes * (Royal Institute of Technology, Stockholm, Sweden).
51556Srgrimes * All rights reserved.
61556Srgrimes *
71556Srgrimes * Redistribution and use in source and binary forms, with or without
81556Srgrimes * modification, are permitted provided that the following conditions
91556Srgrimes * are met:
101556Srgrimes *
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes *
141556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151556Srgrimes *    notice, this list of conditions and the following disclaimer in the
161556Srgrimes *    documentation and/or other materials provided with the distribution.
171556Srgrimes *
181556Srgrimes * 3. Neither the name of the Institute nor the names of its contributors
191556Srgrimes *    may be used to endorse or promote products derived from this software
201556Srgrimes *    without specific prior written permission.
211556Srgrimes *
221556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
231556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
261556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321556Srgrimes * SUCH DAMAGE.
331556Srgrimes */
3436150Scharnier
3536150Scharnier/* $Id$ */
3636150Scharnier
371556Srgrimes#ifdef HAVE_CONFIG_H
3899110Sobrien#include <config.h>
3999110Sobrien#endif
401556Srgrimes
4117987Speter#undef ECHO
4217987Speter
43114763Sobrien#include <stdio.h>
4417987Speter#include <string.h>
451556Srgrimes#include <stdarg.h>
461556Srgrimes#include <stdlib.h>
471556Srgrimes#include "sel.h"
481556Srgrimes#include "sel-gram.h"
4917525Sacheunsigned lineno = 1;
50221559Sjilles
5117525Sachestatic char * handle_string(void);
521556Srgrimesstatic int lex_input(char *, int);
531556Srgrimes
541556Srgrimesstruct hx_expr_input _hx509_expr_input;
551556Srgrimes
561556Srgrimes#ifndef YY_NULL
571556Srgrimes#define YY_NULL 0
581556Srgrimes#endif
591556Srgrimes
601556Srgrimes#define YY_NO_UNPUT 1
611556Srgrimes
621556Srgrimes#undef YY_INPUT
631556Srgrimes#define YY_INPUT(buf,res,maxsize) (res = lex_input(buf, maxsize))
641556Srgrimes
6520425Ssteve#undef ECHO
66223060Sjilles
6717987Speter%}
6817987Speter%%
6917987Speter
701556SrgrimesTRUE			{ return kw_TRUE; }
711556SrgrimesFALSE			{ return kw_FALSE; }
721556SrgrimesAND			{ return kw_AND; }
731556SrgrimesOR			{ return kw_OR; }
741556SrgrimesIN			{ return kw_IN; }
751556SrgrimesTAILMATCH		{ return kw_TAILMATCH; }
761556Srgrimes
771556Srgrimes[A-Za-z][-A-Za-z0-9_]*	{
78201056Sjilles			  yylval.string = strdup ((const char *)yytext);
7990111Simp			  return IDENTIFIER;
801556Srgrimes			}
811556Srgrimes"\""			{ yylval.string = handle_string(); return STRING; }
821556Srgrimes\n			{ ++lineno; }
8317987Speter[,.!={}()%]		{ return *yytext; }
841556Srgrimes[ \t]			;
85208755Sjilles%%
8617987Speter
871556Srgrimesstatic char *
881556Srgrimeshandle_string(void)
891556Srgrimes{
901556Srgrimes    char x[1024];
911556Srgrimes    int i = 0;
921556Srgrimes    int c;
93159632Sstefanf    int quote = 0;
94213760Sobrien    while((c = input()) != EOF){
95230998Sjilles	if(quote) {
961556Srgrimes	    x[i++] = '\\';
97279569Sjilles	    x[i++] = c;
98223024Sjilles	    quote = 0;
99223024Sjilles	    continue;
100213760Sobrien	}
10117987Speter	if(c == '\n'){
102201056Sjilles	    _hx509_sel_yyerror("unterminated string");
10320425Ssteve	    lineno++;
10417987Speter	    break;
105201056Sjilles	}
10620425Ssteve	if(c == '\\'){
107201056Sjilles	    quote++;
10820425Ssteve	    continue;
109201056Sjilles	}
11020425Ssteve	if(c == '\"')
111201056Sjilles	    break;
11220425Ssteve	x[i++] = c;
1138855Srgrimes    }
1141556Srgrimes    x[i] = '\0';
1151556Srgrimes    return strdup(x);
116201056Sjilles}
11720425Ssteve
118201056Sjillesint
119159632Sstefanfyywrap ()
120208755Sjilles{
121208755Sjilles     return 1;
122208755Sjilles}
123208755Sjilles
124201056Sjillesstatic int
12520425Sstevelex_input(char *buf, int max_size)
126230998Sjilles{
127230998Sjilles    int n;
12820425Ssteve
12920425Ssteve    n = _hx509_expr_input.length - _hx509_expr_input.offset;
1301556Srgrimes    if (max_size < n)
1311556Srgrimes        n = max_size;
132213760Sobrien    if (n <= 0)
1331556Srgrimes	return YY_NULL;
134213760Sobrien
135207678Sjilles    memcpy(buf, _hx509_expr_input.buf + _hx509_expr_input.offset, n);
136207678Sjilles    _hx509_expr_input.offset += n;
137207678Sjilles
138213760Sobrien    return n;
139207678Sjilles}
140207678Sjilles