File: scanner.l

package info (click to toggle)
usepackage 1.13-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 332 kB
  • sloc: ansic: 847; yacc: 236; lex: 147; makefile: 45; sh: 8
file content (172 lines) | stat: -rw-r--r-- 4,395 bytes parent folder | download | duplicates (2)
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

/*****************************************************************************
 * 
 * Usepackage Environment Manager
 * Copyright (C) 1995-2015  Jonathan Hogg  <me@jonathanhogg.com>
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Name   : scanner.l
 * Author : Jonathan Hogg <me@jonathanhogg.com>
 * 
 ****************************************************************************/

%{
/* scanner.l */

#include <string.h>
#include "package.h"
#include "grammar.h"
#include "utils.h"

char litbuf[2048];
static char *bufp;

extern int stack_pointer;
extern int line_number[];
extern char file_name[][256];

extern linked_list* make_pathlist(char* path_string);
int include(char* filename);
int uninclude();
YY_BUFFER_STATE file[INCLUDE_STACK_DEPTH];

%}

%option noyywrap

%x lit scpt

name	[-A-Za-z0-9_\.]*
path	(\.|\~|\/|\$)[^ \t\n:,;]*

%%


"#".*		/* ignore */
":"		return(COLON);
";"		return(SEMICOLON);
","		return(COMMA);
"{"		return(LEFTPAREN);
"}"		return(RIGHTPAREN);
"*"		return(WILDCARD);
"="		return(EQUALS);
"+="		return(PLUSEQUALS);
"=+"		return(EQUALSPLUS);
"?="		return(QEQUALS);
"?+="		return(QPLUSEQUALS);
":="		return(ASSIGN);
"<="		return(WITH);
">>"		return(BEGIN_ANNOTATE);
"<<"		return(END_ANNOTATE);

"alias"		return(ALIAS);

"\""		bufp=litbuf; bufp[0] = '\0'; BEGIN(lit);
<lit>[^\"\n]*	strcpy(bufp, yytext); bufp += yyleng;
<lit>\n		{ strcpy(bufp, yytext); bufp += yyleng;
		  line_number[stack_pointer]++; }
<lit>"\""	BEGIN(INITIAL); return(LITERAL);

"<["		bufp=litbuf; bufp[0] = '\0'; BEGIN(scpt);
<scpt>[^\]\n]*	strcpy(bufp, yytext); bufp += yyleng;
<scpt>\n	{ strcpy(bufp, yytext); bufp += yyleng;
		  line_number[stack_pointer]++; }
<scpt>"]>"	BEGIN(INITIAL); return(SCRIPT);

{path}		strcpy(litbuf, yytext); return(PATH);
{name}		strcpy(litbuf, yytext); return(NAME);
{name}\*	{ strcpy(litbuf, yytext); litbuf[yyleng-1] = '\0';
		  return(PREFIX); }
[ \t]+		/* ignore */
\n		line_number[stack_pointer]++;

"(include "[^\)]+")"	yytext[yyleng-1] = '\0'; include(yytext+9);

.		fprintf(stderr, "usepackage: ignoring character `%s' on line %d of %s\n", yytext, line_number[stack_pointer], file_name[stack_pointer]);

<<EOF>>         if (uninclude()) yyterminate();

%%

int include(char* filename)
{
   static linked_list* include_path = NULL;
   list_node* node;
   char* dir;
   char the_file_name[256];
   FILE* the_file = NULL;
   char* path;

   if (!include_path)
   {
      path = getenv(PACKAGE_PATH_VAR);
      if (!path)
         path = DEFAULT_PACKAGE_PATH;

      include_path = make_pathlist(path);
   }

   strcpy(the_file_name, expand(filename));
   if (the_file_name[0] == '/')
   {
      the_file = fopen(the_file_name, "r");
   }
   else
   {
      for (node = head(include_path) ; !the_file && node ; node = next(node))
      {
         dir = (char*) get_value(node);
         sprintf(the_file_name, "%s/%s", expand(dir), filename);
         the_file = fopen(the_file_name, "r");
      }
   }

   if (!the_file)
   {
      DEBUG(stderr, "cannot open file `%s'\n", the_file_name);
      return(1);
   }

   if (stack_pointer == INCLUDE_STACK_DEPTH - 1)
   {
      DEBUG(stderr, "maximum include depth reached\n");
      return(1);
   }

   DEBUG(stderr, "reading from `%s'...\n", the_file_name);
   stack_pointer++;
   strcpy(file_name[stack_pointer], the_file_name);
   line_number[stack_pointer] = 1;
   yy_switch_to_buffer(file[stack_pointer] = yy_create_buffer(the_file, YY_BUF_SIZE));

   return(0);
}

int uninclude()
{
   DEBUG(stderr, "closing file %s\n", file_name[stack_pointer]);
   fclose(yyin);
   yy_delete_buffer(file[stack_pointer--]);

   if (stack_pointer != -1)
   {
      yy_switch_to_buffer(file[stack_pointer]);
      return(0);
   }

   return(1);
}