security/nss/cmd/modutil/installparse.l

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/cmd/modutil/installparse.l	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,137 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +
     1.9 +/* lex file for analyzing PKCS #11 Module installation instructions */
    1.10 +
    1.11 +/*----------------------------- Definitions ---------------------------*/
    1.12 +%{
    1.13 +#include <string.h>
    1.14 +
    1.15 +#include "install-ds.h"		/* defines tokens and data structures */
    1.16 +#include "installparse.h"	/* produced by yacc -d */
    1.17 +#include <prprf.h>
    1.18 +static char *putSimpleString(char*);	/* return copy of string */
    1.19 +static char *putComplexString(char*);	/* strip out quotes, deal with */
    1.20 +											/* escaped characters */
    1.21 +
    1.22 +void Pk11Install_yyerror(char *);
    1.23 +
    1.24 +/* Overrides to use NSPR */
    1.25 +#define malloc PR_Malloc
    1.26 +#define realloc PR_Realloc
    1.27 +#define free PR_Free
    1.28 +
    1.29 +int Pk11Install_yylinenum=1;
    1.30 +static char *err;
    1.31 +
    1.32 +#define YY_NEVER_INTERACTIVE 1
    1.33 +#define yyunput Pkcs11Install_yyunput
    1.34 +
    1.35 +/* This is the default YY_INPUT modified for NSPR */
    1.36 +#define YY_INPUT(buf,result,max_size) \
    1.37 +	if ( yy_current_buffer->yy_is_interactive ) { \
    1.38 +		char c; \
    1.39 +		int n; \
    1.40 +		for ( n = 0; n < max_size && \
    1.41 +		  PR_Read(Pk11Install_FD, &c, 1)==1 && c != '\n'; ++n ) { \
    1.42 +			buf[n] = c; \
    1.43 +		} \
    1.44 +        if ( c == '\n' ) { \
    1.45 +            buf[n++] = c; \
    1.46 +		} \
    1.47 +        result = n; \
    1.48 +	} else { \
    1.49 +		result = PR_Read(Pk11Install_FD, buf, max_size); \
    1.50 +	}
    1.51 +
    1.52 +%}
    1.53 +
    1.54 +/*** Regular expression definitions ***/
    1.55 +/* simple_string has no whitespace, quotes, or braces */
    1.56 +simple_string		[^ \t\r\n\""{""}"]+
    1.57 +
    1.58 +/* complex_string is enclosed in quotes. Inside the quotes, quotes and
    1.59 +   backslashes must be backslash-escaped. No newlines or carriage returns
    1.60 +   are allowed inside the quotes. Otherwise, anything goes. */
    1.61 +complex_string		\"([^\"\\\r\n]|(\\\")|(\\\\))+\"
    1.62 +
    1.63 +/* Standard whitespace */
    1.64 +whitespace			[ \t\r]+
    1.65 +
    1.66 +other				.
    1.67 +
    1.68 +/*---------------------------- Actions --------------------------------*/
    1.69 +%%
    1.70 +
    1.71 +"{"					return OPENBRACE;
    1.72 +"}"					return CLOSEBRACE;
    1.73 +{simple_string}		{Pk11Install_yylval.string =
    1.74 +						putSimpleString(Pk11Install_yytext);
    1.75 +						return STRING;}
    1.76 +{complex_string}	{Pk11Install_yylval.string =
    1.77 +						putComplexString(Pk11Install_yytext);
    1.78 +						return STRING;}
    1.79 +
    1.80 +"\n"				Pk11Install_yylinenum++;
    1.81 +
    1.82 +{whitespace}		;
    1.83 +
    1.84 +{other}				{err = PR_smprintf("Invalid lexeme: %s",Pk11Install_yytext);
    1.85 +						Pk11Install_yyerror(err);
    1.86 +						PR_smprintf_free(err);
    1.87 +						return 1;
    1.88 +					}
    1.89 +
    1.90 +%%
    1.91 +/*------------------------ Program Section ----------------------------*/
    1.92 +
    1.93 +PRFileDesc *Pk11Install_FD=NULL;
    1.94 +
    1.95 +/*************************************************************************/
    1.96 +/* dummy function required by lex */
    1.97 +int Pk11Install_yywrap(void) { return 1;}
    1.98 +
    1.99 +/*************************************************************************/
   1.100 +/* Return a copy of the given string */
   1.101 +static char*
   1.102 +putSimpleString(char *str)
   1.103 +{
   1.104 +	char *tmp = (char*) PR_Malloc(strlen(str)+1);
   1.105 +	strcpy(tmp, str);
   1.106 +	return tmp;
   1.107 +}
   1.108 +
   1.109 +/*************************************************************************/
   1.110 +/* Strip out quotes, replace escaped characters with what they stand for.
   1.111 +   This function assumes that what is passed in is actually a complex
   1.112 +   string, so error checking is lax. */
   1.113 +static char*
   1.114 +putComplexString(char *str)
   1.115 +{
   1.116 +	int size, i,j;
   1.117 +	char *tmp;
   1.118 +
   1.119 +	if(!str) {
   1.120 +		return NULL;
   1.121 +	}
   1.122 +	size = strlen(str);
   1.123 +
   1.124 +	/* Allocate the new space.  This string will actually be too big, 
   1.125 +		since quotes and backslashes will be stripped out.  But that's ok. */
   1.126 +	tmp = (char*) PR_Malloc(size+1);
   1.127 +
   1.128 +	/* Copy it over */
   1.129 +	for(i=0, j=0; i < size; i++) {
   1.130 +		if(str[i]=='\"') {
   1.131 +			continue;  /* skip un-escaped quotes */
   1.132 +		} else if(str[i]=='\\') {
   1.133 +			++i;       /* escaped character. skip the backslash */
   1.134 +		}
   1.135 +		tmp[j++] = str[i];
   1.136 +	}
   1.137 +	tmp[j] = '\0';
   1.138 +
   1.139 +	return tmp;
   1.140 +}

mercurial