tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

installparse.l (3729B)


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