security/nss/cmd/modutil/installparse.l

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5
michael@0 6 /* lex file for analyzing PKCS #11 Module installation instructions */
michael@0 7
michael@0 8 /*----------------------------- Definitions ---------------------------*/
michael@0 9 %{
michael@0 10 #include <string.h>
michael@0 11
michael@0 12 #include "install-ds.h" /* defines tokens and data structures */
michael@0 13 #include "installparse.h" /* produced by yacc -d */
michael@0 14 #include <prprf.h>
michael@0 15 static char *putSimpleString(char*); /* return copy of string */
michael@0 16 static char *putComplexString(char*); /* strip out quotes, deal with */
michael@0 17 /* escaped characters */
michael@0 18
michael@0 19 void Pk11Install_yyerror(char *);
michael@0 20
michael@0 21 /* Overrides to use NSPR */
michael@0 22 #define malloc PR_Malloc
michael@0 23 #define realloc PR_Realloc
michael@0 24 #define free PR_Free
michael@0 25
michael@0 26 int Pk11Install_yylinenum=1;
michael@0 27 static char *err;
michael@0 28
michael@0 29 #define YY_NEVER_INTERACTIVE 1
michael@0 30 #define yyunput Pkcs11Install_yyunput
michael@0 31
michael@0 32 /* This is the default YY_INPUT modified for NSPR */
michael@0 33 #define YY_INPUT(buf,result,max_size) \
michael@0 34 if ( yy_current_buffer->yy_is_interactive ) { \
michael@0 35 char c; \
michael@0 36 int n; \
michael@0 37 for ( n = 0; n < max_size && \
michael@0 38 PR_Read(Pk11Install_FD, &c, 1)==1 && c != '\n'; ++n ) { \
michael@0 39 buf[n] = c; \
michael@0 40 } \
michael@0 41 if ( c == '\n' ) { \
michael@0 42 buf[n++] = c; \
michael@0 43 } \
michael@0 44 result = n; \
michael@0 45 } else { \
michael@0 46 result = PR_Read(Pk11Install_FD, buf, max_size); \
michael@0 47 }
michael@0 48
michael@0 49 %}
michael@0 50
michael@0 51 /*** Regular expression definitions ***/
michael@0 52 /* simple_string has no whitespace, quotes, or braces */
michael@0 53 simple_string [^ \t\r\n\""{""}"]+
michael@0 54
michael@0 55 /* complex_string is enclosed in quotes. Inside the quotes, quotes and
michael@0 56 backslashes must be backslash-escaped. No newlines or carriage returns
michael@0 57 are allowed inside the quotes. Otherwise, anything goes. */
michael@0 58 complex_string \"([^\"\\\r\n]|(\\\")|(\\\\))+\"
michael@0 59
michael@0 60 /* Standard whitespace */
michael@0 61 whitespace [ \t\r]+
michael@0 62
michael@0 63 other .
michael@0 64
michael@0 65 /*---------------------------- Actions --------------------------------*/
michael@0 66 %%
michael@0 67
michael@0 68 "{" return OPENBRACE;
michael@0 69 "}" return CLOSEBRACE;
michael@0 70 {simple_string} {Pk11Install_yylval.string =
michael@0 71 putSimpleString(Pk11Install_yytext);
michael@0 72 return STRING;}
michael@0 73 {complex_string} {Pk11Install_yylval.string =
michael@0 74 putComplexString(Pk11Install_yytext);
michael@0 75 return STRING;}
michael@0 76
michael@0 77 "\n" Pk11Install_yylinenum++;
michael@0 78
michael@0 79 {whitespace} ;
michael@0 80
michael@0 81 {other} {err = PR_smprintf("Invalid lexeme: %s",Pk11Install_yytext);
michael@0 82 Pk11Install_yyerror(err);
michael@0 83 PR_smprintf_free(err);
michael@0 84 return 1;
michael@0 85 }
michael@0 86
michael@0 87 %%
michael@0 88 /*------------------------ Program Section ----------------------------*/
michael@0 89
michael@0 90 PRFileDesc *Pk11Install_FD=NULL;
michael@0 91
michael@0 92 /*************************************************************************/
michael@0 93 /* dummy function required by lex */
michael@0 94 int Pk11Install_yywrap(void) { return 1;}
michael@0 95
michael@0 96 /*************************************************************************/
michael@0 97 /* Return a copy of the given string */
michael@0 98 static char*
michael@0 99 putSimpleString(char *str)
michael@0 100 {
michael@0 101 char *tmp = (char*) PR_Malloc(strlen(str)+1);
michael@0 102 strcpy(tmp, str);
michael@0 103 return tmp;
michael@0 104 }
michael@0 105
michael@0 106 /*************************************************************************/
michael@0 107 /* Strip out quotes, replace escaped characters with what they stand for.
michael@0 108 This function assumes that what is passed in is actually a complex
michael@0 109 string, so error checking is lax. */
michael@0 110 static char*
michael@0 111 putComplexString(char *str)
michael@0 112 {
michael@0 113 int size, i,j;
michael@0 114 char *tmp;
michael@0 115
michael@0 116 if(!str) {
michael@0 117 return NULL;
michael@0 118 }
michael@0 119 size = strlen(str);
michael@0 120
michael@0 121 /* Allocate the new space. This string will actually be too big,
michael@0 122 since quotes and backslashes will be stripped out. But that's ok. */
michael@0 123 tmp = (char*) PR_Malloc(size+1);
michael@0 124
michael@0 125 /* Copy it over */
michael@0 126 for(i=0, j=0; i < size; i++) {
michael@0 127 if(str[i]=='\"') {
michael@0 128 continue; /* skip un-escaped quotes */
michael@0 129 } else if(str[i]=='\\') {
michael@0 130 ++i; /* escaped character. skip the backslash */
michael@0 131 }
michael@0 132 tmp[j++] = str[i];
michael@0 133 }
michael@0 134 tmp[j] = '\0';
michael@0 135
michael@0 136 return tmp;
michael@0 137 }

mercurial