select.c: Remove Draw() call from SelectConnection
[geda-pcb/leaky.git] / src / parse_l.l
blob97992500bd724c93b364a50e534871001dfc1692
1 /* $Id$ */
3 %{
4 /*
5  *                            COPYRIGHT
6  *
7  *  PCB, interactive printed circuit board design
8  *  Copyright (C) 1994,1995,1996,2006 Thomas Nau
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  *  Contact addresses for paper mail and Email:
25  *  Thomas Nau, Schlehenweg 15, 88471 Baustetten, Germany
26  *  Thomas.Nau@rz.uni-ulm.de
27  *
28  */
30 /* lexical definitions to parse ASCII input of PCB and Element description
31  */
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <ctype.h>
41 #if defined(_POSIX_SOURCE) || defined(_HPUX_SOURCE)
42 #include <unistd.h>
43 #endif
45 #include "global.h"
47 #ifdef HAVE_LIBDMALLOC
48 # include <dmalloc.h> /* see http://dmalloc.com */
49 #endif
51 RCSID("$Id$");
54 #include "global.h"
55 #include "crosshair.h"
56 #include "data.h"
57 #include "error.h"
58 #include "file.h"
59 #include "mymem.h"
60 #include "misc.h"
61 #include "strflags.h"
62 #include "parse_l.h"
63 #include "parse_y.h"
64 #include "create.h"
66 #define YY_NO_INPUT
68 /* ---------------------------------------------------------------------------
69  * some shared parser identifiers
70  */
71 #ifdef FLEX_SCANNER
72 int                             yylineno;               /* linenumber */
73 #define yyunput ATTRIBUTE_UNUSED yyunput
74 #endif
76 char                    *yyfilename;    /* in this file */
77 PCBTypePtr              yyPCB;                  /* used by parser */
78 DataTypePtr             yyData;
79 ElementTypePtr          yyElement;
80 FontTypePtr             yyFont;
82 static int parse_number (double);
84 /* ---------------------------------------------------------------------------
85  * an external prototypes
86  */
87 int     yyparse(void);
89 /* ---------------------------------------------------------------------------
90  * some local prototypes
91  */
92 static  int             Parse(char *, char *, char *, char *);
96 HEX                             0x[0-9a-fA-F]+
97 DECIMAL                 -?[1-9][0-9]*|0
98 FLOATING                [+-]?[0-9]*("."[0-9]*)?
99 FLOATINGMM              {FLOATING}[mM][mM]
100 FLOATINGUM              {FLOATING}[uU][mM]
101 FLOATINGIN              {FLOATING}[iI][nN]
102 FLOATINGMIL             {FLOATING}[mM][iI][lL]
103 CARDINAL                [1-9][0-9]*|0
104 STRINGCHAR              ([^"\n\r\\]|\\.)
106 %option yylineno
110 FileVersion     { return(T_FILEVERSION); }
111 PCB                     { return(T_PCB); }
112 Grid            { return(T_GRID); }
113 Cursor          { return(T_CURSOR); }
114 Thermal         { return(T_THERMAL); }
115 PolyArea        { return(T_AREA); }
116 DRC             { return(T_DRC); }
117 Flags           { return(T_FLAGS); }
118 Layer           { return(T_LAYER); }
119 Pin                     { return(T_PIN); }
120 Pad                     { return(T_PAD); }
121 Via                     { return(T_VIA); }
122 Line            { return(T_LINE); }
123 Rat             { return(T_RAT); }
124 Rectangle       { return(T_RECTANGLE); }
125 Text            { return(T_TEXT); }
126 ElementLine     { return(T_ELEMENTLINE); }
127 ElementArc      { return(T_ELEMENTARC); }
128 Element         { return(T_ELEMENT); }
129 SymbolLine      { return(T_SYMBOLLINE); }
130 Symbol          { return(T_SYMBOL); }
131 Mark            { return(T_MARK); }
132 Groups          { return(T_GROUPS); }
133 Styles          { return(T_STYLES); }
134 Polygon         { return(T_POLYGON); }
135 Hole            { return(T_POLYGON_HOLE); }
136 Arc             { return(T_ARC); }
137 NetList         { return(T_NETLIST); }
138 Net             { return(T_NET); }
139 Connect         { return(T_CONN); }
140 Attribute       { return(T_ATTRIBUTE); }
142 \'.\'                           {
143                                                 yylval.number = (unsigned) *(yytext+1);
144                                                 return(CHAR_CONST);
145                                         }
146 {FLOATINGMM}            {       return parse_number(3937.0079); }
147 {FLOATINGUM}            {       return parse_number(3.9370079); }
148 {FLOATINGIN}            {       return parse_number(100000.0); }
149 {FLOATINGMIL}           {       return parse_number(100.0); }
150 {DECIMAL}               {       return parse_number(1.0); }
152 {HEX}                   {
153                                                 sscanf((char *) yytext, "%x", &yylval.number);
154                                                 return(NUMBER);
155                                         }
156 {FLOATING}                      {
157                                                 yylval.floating = c_strtod (yytext);
158                                                 return(FLOAT);
159                                         }
160 \"{STRINGCHAR}*\"       {
161                                                 char    *p1, *p2;
163                                                         /* return NULL on empty string */
164                                                 if (yyleng == 2)
165                                                 {
166                                                         yylval.string = NULL;
167                                                         return(STRING);
168                                                 }
170                                                         /* allocate memory and copy string;
171                                                          * stringlength is counted and copied without
172                                                          * leading and trailing '"'
173                                                          */
174                                                 yyleng -= 2;
175                                                 yylval.string = calloc (yyleng+1, sizeof (char));
176                                                 p1 = (char *) (yytext +1);
177                                                 p2 = yylval.string;
178                                                 while(yyleng--)
179                                                 {
180                                                                 /* check for special character */
181                                                         if (*p1 == '\\')
182                                                         {
183                                                                 yyleng--;
184                                                                 p1++;
186                                                         }
187                                                         *p2++ = *p1++;
188                                                 }
189                                                 *p2 = '\0';
190                                                 return(STRING);
191                                         }
192 #.*                                     {}
193 [ \t]+                          {}
194 [\n]                            {
195 #ifndef FLEX_SCANNER
196                                                 yylineno++;
197 #endif
198                                         }
199 [\r]                            {}
200 .                                       { return(*yytext); }
204 /* ---------------------------------------------------------------------------
205  * sets up the preprocessor command
206  */
207 static int Parse(char *Executable, char *Path, char *Filename, char *Parameter)
209         static  char    *command = NULL;
210         int             returncode;
211         int             used_popen = 0;
212         char *tmps;
213         size_t l;
214 #ifdef FLEX_SCANNER
215         static  bool    firsttime = true;
216 #endif
218         if (EMPTY_STRING_P (Executable))
219           {
220             l = 2;
221             if ( Path != NULL )
222               l += strlen (Path);
224             l += strlen (Filename);
226             if ( (tmps = (char *) malloc ( l * sizeof (char))) == NULL)
227               {
228                 fprintf (stderr, "Parse():  malloc failed\n");
229                 exit (1);
230               }
232             if ( Path != NULL && *Path != '\0')
233               sprintf (tmps, "%s%s%s", Path, PCB_DIR_SEPARATOR_S, Filename);
234             else
235               sprintf (tmps, "%s", Filename);
237             yyin = fopen (tmps, "r");
238             if (!yyin)
239               {
240                 /* Special case this one, we get it all the time... */
241                 if (strcmp (tmps, "./default_font"))
242                   Message("Can't open %s for reading\n", tmps);
243                 return(1);
244               }
245             free (tmps);
246           }
247         else
248           {
249             used_popen = 1;
250             /* release old command and create new from template */
251             free (command);
252             command = EvaluateFilename(Executable, Path, Filename, Parameter);
254             /* open pipe to stdout of command */
255             if (*command == '\0' || (yyin = popen(command, "r")) == NULL)
256               {
257                 PopenErrorMessage(command);
258                 return(1);
259               }
260           }
262 #ifdef FLEX_SCANNER
263                 /* reset parser if not called the first time */
264         if (!firsttime)
265                 yyrestart(yyin);
266         firsttime = false;
267 #endif
269                 /* init linenumber and filename for yyerror() */
270         yylineno = 1;
271         yyfilename = Filename;
273                 /* We need to save the data temporarily because lex-yacc are able
274                  * to break the application if the input file has an illegal format.
275                  * It's not necessary if the system supports the call of functions
276                  * on termination.
277                  */
279         CreateBeLenient (true);
281 #if !defined(HAS_ATEXIT) && !defined(HAS_ON_EXIT)
282         if (PCB)
283           SaveTMPData();
284         returncode = yyparse();
285         RemoveTMPData();
286 #else
287         returncode = yyparse();
288 #endif
289         /* clean up parse buffer */
290         yy_delete_buffer(YY_CURRENT_BUFFER);
292         CreateBeLenient (false);
294         if (used_popen)
295           return(pclose(yyin) ? 1 : returncode);
296         return(fclose(yyin) ? 1 : returncode);
299 /* ---------------------------------------------------------------------------
300  * initializes LEX and calls parser for a single element file
301  */
302 int ParseElementFile(DataTypePtr Ptr, char *Filename)
304         yyPCB = NULL;
305         yyData = Ptr;
306         yyFont = &PCB->Font;
307         yyElement = NULL;
308         return(Parse(NULL,NULL,Filename,NULL));
311 /* ---------------------------------------------------------------------------
312  * initializes LEX and calls parser for a single library entry
313  */
314 int ParseLibraryEntry(DataTypePtr Ptr, char *Template)
316         yyPCB = NULL;
317         yyData = Ptr;
318         yyFont = &PCB->Font;
319         yyElement = NULL;
320         return(Parse(Settings.LibraryCommand, Settings.LibraryPath,
321                 Settings.LibraryFilename, Template));
324 /* ---------------------------------------------------------------------------
325  * initializes LEX and calls parser for a complete board
326  */
327 int ParsePCB(PCBTypePtr Ptr, char *Filename)
329         yyPCB = Ptr;
330         yyData = NULL;
331         yyFont = NULL;
332         yyElement = NULL;
333         return(Parse(Settings.FileCommand, Settings.FilePath, Filename, NULL));
336 /* ---------------------------------------------------------------------------
337  * initializes LEX and calls parser for a font
338  */
339 int ParseFont(FontTypePtr Ptr, char *Filename)
341         int r = 0;
342         char *path, *p;
343         yyPCB = NULL;
344         yyFont = Ptr;
345         yyElement = NULL;
347         path = strdup (Settings.FontPath);
349         /* search through the font path for a font file */
350         for (p = strtok (path, PCB_PATH_DELIMETER); p && *p;
351                 p = strtok (NULL, PCB_PATH_DELIMETER))
352           {
353 #ifdef DEBUG
354             Message ("Looking for %s in %s\n", Filename, p);
355 #endif
356             r = Parse(Settings.FontCommand, p, Filename, NULL);
357             if (r == 0)
358               {
359 #ifdef DEBUG
360                 Message ("Found %s in %s\n", Filename, p);
361 #endif
362                 break;
363               }
364           }
365         free (path);
367         return r;
370 static int
371 parse_number (double scale)
373   double val;
374   sscanf ((char *) yytext, "%lf", &val);
375   val *= scale;
376   /* Round towards nearest means we have to check the sign first.  */
377   if (val < 0)
378     val -= 0.49;
379   else
380     val += 0.49;
381   yylval.number = (int)(val);
382   return NUMBER;