File: proplist.l

package info (click to toggle)
libproplist 0.7.1-4
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 684 kB
  • ctags: 452
  • sloc: ansic: 3,684; sh: 1,856; lex: 181; yacc: 118; makefile: 106; tcl: 40
file content (186 lines) | stat: -rw-r--r-- 4,041 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* proplist.l - for parsing NSString property lists -*- c -*- */

/* definition section */

        /* literal block */
%{
#include "proplist.h"
#include "proplist.tab.h"
#include "util.h"
#include "util.h"


#ifdef yywrap
#undef yywrap
#endif

int yywrap(void) {
  return 1;
}

#define return_data(str) yylval.obj = str2data(str); return DATA

char * strdup(char * src);
char * unescstr(char * src);
proplist_t str2data(char * str);

extern char plinput[];
extern char *plinputptr;
extern char *plinputlim;
int pl_line_count = 1; 


static int level;
%}

        /* pattern definitions */
ws              [ \t\r]
nl              \n
wsnl            [ \t\r\n]
hexdigit        [0-9A-Fa-f]
hexbyte {hexdigit}{2}
hexword {hexbyte}{4}
hexnum  {hexbyte}{1,3}
qstring [^"]*\"
noquote [$./0-9A-Z_a-z]
unqstring       {noquote}+
hexdata \<{ws}*({hexword}{ws}*)*{hexnum}{0,1}{ws}*\>
        /* exclusive states */
%x QUOTE COMMENT LINE_COMMENT

                /* rules section */
%%

"/*"		{++level; BEGIN COMMENT;}
"//"		{BEGIN LINE_COMMENT;}

<QUOTE>{qstring}        {
                          if(yyleng==1) {
			    BEGIN INITIAL;
			    yylval.obj = PLMakeString("");
			    return STRING;
			  }
			  if(yytext[yyleng-2] == '\\') {
                             yymore();
                          } else {
			    char *unesc;
                            BEGIN INITIAL;
		      	    unesc = unescstr(yytext);
		       	    yylval.obj = PLMakeString(unesc);
		       	    MyFree(__FILE__, __LINE__, unesc);
		       	    return STRING;
			  }
                        }

<COMMENT>"*/"		{if(!--level) BEGIN INITIAL;}
<COMMENT>.              ;
<COMMENT><<EOF>>	{return 0;}

<LINE_COMMENT>\n	{BEGIN INITIAL; pl_line_count++;}
<LINE_COMMENT>.         ;

{unqstring}     {yylval.obj = PLMakeString(yytext); return STRING;}

{hexdata}               {return_data(yytext);}

\"      {BEGIN QUOTE;}

{ws}+ ;
{nl}+ {pl_line_count += yyleng;}       /* skip whitespace */

<<EOF>> {return 0;}

.       {return yytext[0];}             /* return unmatched characters
literally*/
%%

                /* C code section */
char * strdup(char *src)
{
  char *dest=(char *)MyMalloc(__FILE__, __LINE__, strlen(src)+5);

  strcpy(dest,src);
  return dest;
}

#define inrange(ch,min,max) ((ch)>=(min) && (ch)<=(max))
#define char2num(ch) \
inrange(ch,'0','9') \
? ((ch)-0x30) \
: (inrange(ch,'a','f') \
? ((ch)-0x57) : ((ch)-0x37))

proplist_t
str2data (char *str)
{
  unsigned char *buf, *str_ptr, *buf_ptr;
  char ch;
  int len;
  proplist_t data;

  buf=(unsigned char *)MyMalloc(__FILE__, __LINE__, strlen(str));

  for (str_ptr=(unsigned char *)str+1, buf_ptr=buf,len=0;
       (ch = *str_ptr) != '>';
       str_ptr++)
    {
      if(ch==' ' || ch=='\n' || ch=='\t' || ch=='\r')
	continue; /* ignore whitespace */
      *buf_ptr = (char2num(ch))<<4;
      ch = *++str_ptr;
      *buf_ptr |= char2num(ch);
      len++; buf_ptr++;
  }

  data = PLMakeData(buf,len);
  MyFree(__FILE__, __LINE__, buf);

  return data;
}

char *
unescstr (char *src)
{
  char *dest=(char*) MyMalloc (__FILE__, __LINE__, strlen(src));
  char *src_ptr, *dest_ptr;
  char ch;

  /* blow away that terminating quote for good measure */
  src[strlen(src)-1] = '\0';

  for (src_ptr=src,dest_ptr=dest; 
       *src_ptr;
       src_ptr++, dest_ptr++)
    {
      if(*src_ptr != '\\')
	*dest_ptr = *src_ptr;
      else
	{
          ch = *(++src_ptr);
          if((ch>='0') && (ch<='1')) /* assume next 2 chars are octal too */
	    {
	      *dest_ptr = (ch & 07) << 3;
              *dest_ptr |= (*(++src_ptr)&07)<<3;
              *dest_ptr |= *(++src_ptr)&07;
	    }
	  else
	    {
	      switch(ch)
		{
		case 'a' : *dest_ptr = '\a'; break;
		case 'b' : *dest_ptr = '\b'; break;
		case 't' : *dest_ptr = '\t'; break;
		case 'r' : *dest_ptr = '\r'; break;
		case 'n' : *dest_ptr = '\n'; break;
		case 'v' : *dest_ptr = '\v'; break;
		case 'f' : *dest_ptr = '\f'; break;
		default  : *dest_ptr = *src_ptr;
		}
	    }
	}
    }

  *dest_ptr = '\0';     /* terminate dest */

  return dest;
}