michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /* lex file for analyzing PKCS #11 Module installation instructions */ michael@0: michael@0: /*----------------------------- Definitions ---------------------------*/ michael@0: %{ michael@0: #include michael@0: michael@0: #include "install-ds.h" /* defines tokens and data structures */ michael@0: #include "installparse.h" /* produced by yacc -d */ michael@0: #include michael@0: static char *putSimpleString(char*); /* return copy of string */ michael@0: static char *putComplexString(char*); /* strip out quotes, deal with */ michael@0: /* escaped characters */ michael@0: michael@0: void Pk11Install_yyerror(char *); michael@0: michael@0: /* Overrides to use NSPR */ michael@0: #define malloc PR_Malloc michael@0: #define realloc PR_Realloc michael@0: #define free PR_Free michael@0: michael@0: int Pk11Install_yylinenum=1; michael@0: static char *err; michael@0: michael@0: #define YY_NEVER_INTERACTIVE 1 michael@0: #define yyunput Pkcs11Install_yyunput michael@0: michael@0: /* This is the default YY_INPUT modified for NSPR */ michael@0: #define YY_INPUT(buf,result,max_size) \ michael@0: if ( yy_current_buffer->yy_is_interactive ) { \ michael@0: char c; \ michael@0: int n; \ michael@0: for ( n = 0; n < max_size && \ michael@0: PR_Read(Pk11Install_FD, &c, 1)==1 && c != '\n'; ++n ) { \ michael@0: buf[n] = c; \ michael@0: } \ michael@0: if ( c == '\n' ) { \ michael@0: buf[n++] = c; \ michael@0: } \ michael@0: result = n; \ michael@0: } else { \ michael@0: result = PR_Read(Pk11Install_FD, buf, max_size); \ michael@0: } michael@0: michael@0: %} michael@0: michael@0: /*** Regular expression definitions ***/ michael@0: /* simple_string has no whitespace, quotes, or braces */ michael@0: simple_string [^ \t\r\n\""{""}"]+ michael@0: michael@0: /* complex_string is enclosed in quotes. Inside the quotes, quotes and michael@0: backslashes must be backslash-escaped. No newlines or carriage returns michael@0: are allowed inside the quotes. Otherwise, anything goes. */ michael@0: complex_string \"([^\"\\\r\n]|(\\\")|(\\\\))+\" michael@0: michael@0: /* Standard whitespace */ michael@0: whitespace [ \t\r]+ michael@0: michael@0: other . michael@0: michael@0: /*---------------------------- Actions --------------------------------*/ michael@0: %% michael@0: michael@0: "{" return OPENBRACE; michael@0: "}" return CLOSEBRACE; michael@0: {simple_string} {Pk11Install_yylval.string = michael@0: putSimpleString(Pk11Install_yytext); michael@0: return STRING;} michael@0: {complex_string} {Pk11Install_yylval.string = michael@0: putComplexString(Pk11Install_yytext); michael@0: return STRING;} michael@0: michael@0: "\n" Pk11Install_yylinenum++; michael@0: michael@0: {whitespace} ; michael@0: michael@0: {other} {err = PR_smprintf("Invalid lexeme: %s",Pk11Install_yytext); michael@0: Pk11Install_yyerror(err); michael@0: PR_smprintf_free(err); michael@0: return 1; michael@0: } michael@0: michael@0: %% michael@0: /*------------------------ Program Section ----------------------------*/ michael@0: michael@0: PRFileDesc *Pk11Install_FD=NULL; michael@0: michael@0: /*************************************************************************/ michael@0: /* dummy function required by lex */ michael@0: int Pk11Install_yywrap(void) { return 1;} michael@0: michael@0: /*************************************************************************/ michael@0: /* Return a copy of the given string */ michael@0: static char* michael@0: putSimpleString(char *str) michael@0: { michael@0: char *tmp = (char*) PR_Malloc(strlen(str)+1); michael@0: strcpy(tmp, str); michael@0: return tmp; michael@0: } michael@0: michael@0: /*************************************************************************/ michael@0: /* Strip out quotes, replace escaped characters with what they stand for. michael@0: This function assumes that what is passed in is actually a complex michael@0: string, so error checking is lax. */ michael@0: static char* michael@0: putComplexString(char *str) michael@0: { michael@0: int size, i,j; michael@0: char *tmp; michael@0: michael@0: if(!str) { michael@0: return NULL; michael@0: } michael@0: size = strlen(str); michael@0: michael@0: /* Allocate the new space. This string will actually be too big, michael@0: since quotes and backslashes will be stripped out. But that's ok. */ michael@0: tmp = (char*) PR_Malloc(size+1); michael@0: michael@0: /* Copy it over */ michael@0: for(i=0, j=0; i < size; i++) { michael@0: if(str[i]=='\"') { michael@0: continue; /* skip un-escaped quotes */ michael@0: } else if(str[i]=='\\') { michael@0: ++i; /* escaped character. skip the backslash */ michael@0: } michael@0: tmp[j++] = str[i]; michael@0: } michael@0: tmp[j] = '\0'; michael@0: michael@0: return tmp; michael@0: }