summaryrefslogtreecommitdiffabout
path: root/mimetypes/lexer.l
blob: dbe3bde784c3d45a4c9a92b7cb16c3d226a5668a (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
%top {
/* This file is part of fileserv.
   Copyright (C) 2017-2019 Sergey Poznyakoff

   Fileserv is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   Fileserv is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with fileserv.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
}

%{
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <assert.h>
#include <ctype.h>
#include "mtint.h"
#include "grammar.h"

#define yylval yymtlval
#define yylloc yymtlloc
#define yyerror yymterror

int yyerror (char *s);

static linetrack_t trk;
struct locus_point string_beg;

char *string_buffer;
size_t string_level;
size_t string_size;

static void
string_moremem (size_t size)
{
  if (string_size - string_level < size)
    {
      while (string_size - string_level < size)
	{
	  if ((size_t) -1 / 3 * 2 <= string_size)
	    abort ();
	  string_size += (string_size + 1) / 2 + 1;
	}
      string_buffer = realloc (string_buffer, string_size);
      if (!string_buffer)
	mimetypes_nomem ();
    }
}

static void
string_append (char const *b, size_t n)
{
  string_moremem (n);
  memcpy (string_buffer + string_level, b, n);
  string_level += n;
}

static void
string_append_char (int c)
{
  string_moremem (1);
  string_buffer[string_level++] = c;
}
 
static unsigned 
digit_to_number (char c)
{
  return (unsigned) (c >= '0' && c <= '9' ? c-'0' :
                     c >= 'A' && c <= 'Z' ? c-'A'+10 :
                     c-'a'+10);
}

static void
drop_string (void)
{
  string_level = 0;
}
 
static void
finish_string (void)
{
  yylval.string.ptr = malloc (string_level + 1);
  if (!yylval.string.ptr)
    mimetypes_nomem ();
  memcpy (yylval.string.ptr, string_buffer, string_level);
  yylval.string.ptr[string_level] = 0;
  yylval.string.len = string_level;
  string_level = 0;
}  

static void
finish_astring (void)
{
  yylval.string.ptr = malloc (string_level + 1);
  if (!yylval.string.ptr)
    mimetypes_nomem ();
  memcpy (yylval.string.ptr, string_buffer, string_level);
  yylval.string.ptr[string_level] = 0;
  yylval.string.len = string_level;
      
  locus_point_copy (&yylloc.end, &yylloc.beg);
  yylloc.end.col--;
  locus_point_copy (&yylloc.beg, &string_beg);
  locus_point_deinit (&string_beg);

  string_level = 0;
    
  if (yy_flex_debug)
    {
      size_t i;
      printf ("string %zu: ", yylval.string.len);
      for (i = 0; i < yylval.string.len; i++)
	if (isprint (yylval.string.ptr[i]))
	  printf ("%c", yylval.string.ptr[i]);
        else
	  printf ("\\%03o", yylval.string.ptr[i]);
      putchar ('\n');
    }
#if 0
  YY_LOCATION_PRINT (stderr, yylloc);
  fprintf (stderr, ": %s\n", yylval.string.ptr);
#endif
}  
 
#define YY_USER_ACTION							\
  linetrack_advance (trk, &yylloc, yytext, yyleng);

%}

%x RULE ARGS ASTRING
X [0-9a-fA-F]
IDENT [a-zA-Z_\.][a-zA-Z0-9_\.-]*
WS [ \t][ \t]*
%%

<INITIAL>{
     /* Comments */
^#.*\n               ;
\n                   ;
^[^ \t\n/]+"/"[^ \t\n]+ {
  string_append (yytext, yyleng);
  finish_string ();
  BEGIN (RULE);
  return TYPE;
}

. {
  yyerror ("type/subtype is missing"); 
  return BOGUS;
}
}

<RULE>{
\\\n                 ;
\n                   {
  BEGIN (INITIAL);
  return EOL;
}
{WS}                 ;

   /* Operators */
"!"|"+"|","|"("|")"|"/"  return yytext[0];
  /* Special cases: && and ||. Docs don't say anything about them, but
     I've found them in my mime.types file...         --Sergey */
"&&"  return '+';
"||"  return ',';

"priority"/"(" {
  return PRIORITY;
}

{IDENT}/"(" {
  string_append (yytext, yyleng);
  finish_string ();
  BEGIN (ARGS);
  return IDENT;
} 

[a-zA-Z0-9_.-]+/[^(] {
  string_append (yytext, yyleng);
  finish_string ();
  return STRING;
}

. {
  struct locus_range lr = LOCUS_RANGE_INITIALIZER;
  linetrack_locus (trk, &lr.beg);
  mimetypes_error_at (&lr, "unexpected character '%c'", yytext[0]);
  locus_range_deinit (&lr);
}

}

<ARGS>{
"("|","    return yytext[0];
")"  {
  BEGIN (RULE);
  return yytext[0];
}
{WS} yyerror ("unexpected whitespace in argument list");
\n   {
  yyerror ("unexpected newline in argument list");
  return BOGUS;
}
. {
  locus_point_copy (&string_beg, &yylloc.beg);
  linetrack_retreat (trk, 1); 
  yyless (0);
  BEGIN (ASTRING);
}
}

<ASTRING>{
  /* Quoted string */
\"[^"\n]*\"        {
  string_append (yytext+1, yyleng-2);
}
"'"[^'\n]*"'" {
  string_append (yytext+1, yyleng-2);
}
  
  /* Hex string */
"<"({X}{X})+">" {
  int i;
  for (i = 1; i < yyleng - 2; i += 2)
    {
      string_append_char (digit_to_number (yytext[i])*16
                                  + digit_to_number (yytext[i+1]));
    }  
}

  /* Unquoted character sequence */
[^ \t\n,)<"']+/[^"'<] {
  string_append (yytext, yyleng);
}

[^ \t\n,)<"]+/< {
  string_append (yytext, yyleng);
}

[^ \t\n,)<"]+/["'] {
  string_append (yytext, yyleng);
}

\n   {
  yyerror ("unexpected newline in argument");
  drop_string ();
  return BOGUS;
}

. {
  linetrack_retreat (trk, 1);
  yyless (0);
  BEGIN (ARGS);
  finish_astring ();  
  return STRING;
}
}

%%
int
mimetypes_open (const char *name)
{
  struct stat st;
  char *filename;
  char *p;

  p = getenv ("MIMETYPES_DEBUG_LEX");
  yy_flex_debug = p ? (*p - '0') : 0;

  if (stat (name, &st))
    {
      mimetypes_error ("cannot stat `%s': %s", name, strerror (errno));
      return -1;
    }
  
  if (S_ISDIR (st.st_mode))
    {
      size_t blen = strlen (name);
      static char *fname = "mime.types";
      while (blen && name[blen-1] == '/')
	blen--;
      if (!blen)
	abort ();
      filename = malloc (blen + 1 + strlen (fname) + 1);
      if (!filename)
	mimetypes_nomem ();
      strcpy (filename, name);
      strcat (filename, "/");
      strcat (filename, fname);
    }
  else
    {
      filename = strdup (name);
      if (!filename)
	abort ();
    }
  
  yyin = fopen (filename, "r");
  if (!yyin)
    {
      mimetypes_error ("cannot open `%s': %s", filename, strerror (errno));
      free (filename);
      return -1;
    }

  assert (linetrack_create (&trk, filename, 3) == 0);
  free (filename);

  drop_string ();
  return 0;
}

void
mimetypes_close (void)
{
  fclose (yyin);
  locus_range_deinit (&yylloc);
  linetrack_destroy (&trk);
}

int
yyerror (char *s)
{
  struct locus_range lr = LOCUS_RANGE_INITIALIZER;
  linetrack_locus (trk, &lr.beg);
  mimetypes_error_at (&lr, "%s", s);
  locus_range_deinit (&lr);
  return 0;
}

int
yywrap (void)
{
  return 1;
}

/* Position input at the beginning of the next rule as a final part of error
   recovery */
void
lex_next_rule (void)
{
  int c;
  
  if (yy_flex_debug)
    {
      YY_LOCATION_PRINT (stderr, yylloc);
      fprintf (stderr, ": started error recovery\n");
    }
  while ((c = input ()) != EOF)
    {
      char ch = c;
      if (!isspace (c) && linetrack_at_bol (trk))
	{
	  unput (c);
	  break;
 	}
      linetrack_advance (trk, &yylloc, &ch, 1);
    }
  if (yy_flex_debug)
    {
      struct locus_range lr = LOCUS_RANGE_INITIALIZER;
      linetrack_locus (trk, &lr.beg);
      YY_LOCATION_PRINT (stderr, lr);
      fprintf (stderr, ": finished error recovery\n");
      locus_point_deinit (&lr.beg);
    }
  BEGIN (RULE);
  unput ('\n');
  linetrack_retreat (trk, 1);
}

Return to:

Send suggestions and report system problems to the System administrator.