Revert "Fix locale-dependent gerber output"
[geda-pcb/whiteaudio.git] / src / parse_l.l
blob33a115d4a196be71e499797727597a38f4b69cae
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
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 (void);
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 INTEGER                 [+-]?([1-9][0-9]*|0)
98 FLOATING                {INTEGER}"."[0-9]*
99 STRINGCHAR              ([^"\n\r\\]|\\.)
101 %option yylineno
105 FileVersion     { return(T_FILEVERSION); }
106 PCB                     { return(T_PCB); }
107 Grid            { return(T_GRID); }
108 Cursor          { return(T_CURSOR); }
109 Thermal         { return(T_THERMAL); }
110 PolyArea        { return(T_AREA); }
111 DRC             { return(T_DRC); }
112 Flags           { return(T_FLAGS); }
113 Layer           { return(T_LAYER); }
114 Pin                     { return(T_PIN); }
115 Pad                     { return(T_PAD); }
116 Via                     { return(T_VIA); }
117 Line            { return(T_LINE); }
118 Rat             { return(T_RAT); }
119 Rectangle       { return(T_RECTANGLE); }
120 Text            { return(T_TEXT); }
121 ElementLine     { return(T_ELEMENTLINE); }
122 ElementArc      { return(T_ELEMENTARC); }
123 Element         { return(T_ELEMENT); }
124 SymbolLine      { return(T_SYMBOLLINE); }
125 Symbol          { return(T_SYMBOL); }
126 Mark            { return(T_MARK); }
127 Groups          { return(T_GROUPS); }
128 Styles          { return(T_STYLES); }
129 Polygon         { return(T_POLYGON); }
130 Hole            { return(T_POLYGON_HOLE); }
131 Arc             { return(T_ARC); }
132 NetList         { return(T_NETLIST); }
133 Net             { return(T_NET); }
134 Connect         { return(T_CONN); }
135 Attribute       { return(T_ATTRIBUTE); }
137 nm      { return T_NM; }
138 um      { return T_UM; }
139 mm      { return T_MM; }
140 m       { return T_M; }
141 km      { return T_KM; }
142 umil    { return T_UMIL; }
143 cmil    { return T_CMIL; }
144 mil     { return T_MIL; }
145 in      { return T_IN; }
147 \'.\'                           {
148                                                 yylval.integer = (unsigned) *(yytext+1);
149                                                 return(CHAR_CONST);
150                                         }
151 {FLOATING}              {       return parse_number(); }
152 {INTEGER}               {       yylval.integer = round (g_ascii_strtod (yytext, NULL)); return INTEGER; }
154 {HEX}                   {       unsigned n;
155                                 sscanf((char *) yytext, "%x", &n);
156                                 yylval.integer = n;
157                                 return INTEGER;
158                                         }
159 \"{STRINGCHAR}*\"       {
160                                                 char    *p1, *p2;
162                                                         /* return NULL on empty string */
163                                                 if (yyleng == 2)
164                                                 {
165                                                         yylval.string = NULL;
166                                                         return(STRING);
167                                                 }
169                                                         /* allocate memory and copy string;
170                                                          * stringlength is counted and copied without
171                                                          * leading and trailing '"'
172                                                          */
173                                                 yyleng -= 2;
174                                                 yylval.string = (char *)calloc (yyleng+1, sizeof (char));
175                                                 p1 = (char *) (yytext +1);
176                                                 p2 = yylval.string;
177                                                 while(yyleng--)
178                                                 {
179                                                                 /* check for special character */
180                                                         if (*p1 == '\\')
181                                                         {
182                                                                 yyleng--;
183                                                                 p1++;
185                                                         }
186                                                         *p2++ = *p1++;
187                                                 }
188                                                 *p2 = '\0';
189                                                 return(STRING);
190                                         }
191 #.*                                     {}
192 [ \t]+                          {}
193 [\n]                            {
194 #ifndef FLEX_SCANNER
195                                                 yylineno++;
196 #endif
197                                         }
198 [\r]                            {}
199 .                                       { return(*yytext); }
203 /* ---------------------------------------------------------------------------
204  * sets up the preprocessor command
205  */
206 static int Parse(char *Executable, char *Path, char *Filename, char *Parameter)
208         static  char    *command = NULL;
209         int             returncode;
210         int             used_popen = 0;
211         char *tmps;
212         size_t l;
213 #ifdef FLEX_SCANNER
214         static  bool    firsttime = true;
215 #endif
217         if (EMPTY_STRING_P (Executable))
218           {
219             l = 2;
220             if ( Path != NULL )
221               l += strlen (Path);
223             l += strlen (Filename);
225             if ( (tmps = (char *) malloc ( l * sizeof (char))) == NULL)
226               {
227                 fprintf (stderr, "Parse():  malloc failed\n");
228                 exit (1);
229               }
231             if ( Path != NULL && *Path != '\0')
232               sprintf (tmps, "%s%s%s", Path, PCB_DIR_SEPARATOR_S, Filename);
233             else
234               sprintf (tmps, "%s", Filename);
236             yyin = fopen (tmps, "r");
237             if (!yyin)
238               {
239                 /* Special case this one, we get it all the time... */
240                 if (strcmp (tmps, "./default_font"))
241                   Message("Can't open %s for reading\n", tmps);
242                 return(1);
243               }
244             free (tmps);
245           }
246         else
247           {
248             used_popen = 1;
249             /* release old command and create new from template */
250             free (command);
251             command = EvaluateFilename(Executable, Path, Filename, Parameter);
253             /* open pipe to stdout of command */
254             if (*command == '\0' || (yyin = popen(command, "r")) == NULL)
255               {
256                 PopenErrorMessage(command);
257                 return(1);
258               }
259           }
261 #ifdef FLEX_SCANNER
262                 /* reset parser if not called the first time */
263         if (!firsttime)
264                 yyrestart(yyin);
265         firsttime = false;
266 #endif
268                 /* init linenumber and filename for yyerror() */
269         yylineno = 1;
270         yyfilename = Filename;
272                 /* We need to save the data temporarily because lex-yacc are able
273                  * to break the application if the input file has an illegal format.
274                  * It's not necessary if the system supports the call of functions
275                  * on termination.
276                  */
278         CreateBeLenient (true);
280 #if !defined(HAS_ATEXIT) && !defined(HAS_ON_EXIT)
281         if (PCB && PCB->Data)
282           SaveTMPData();
283         returncode = yyparse();
284         RemoveTMPData();
285 #else
286         returncode = yyparse();
287 #endif
288         /* clean up parse buffer */
289         yy_delete_buffer(YY_CURRENT_BUFFER);
291         CreateBeLenient (false);
293         if (used_popen)
294           return(pclose(yyin) ? 1 : returncode);
295         return(fclose(yyin) ? 1 : returncode);
298 /* ---------------------------------------------------------------------------
299  * initializes LEX and calls parser for a single element file
300  */
301 int ParseElementFile(DataTypePtr Ptr, char *Filename)
303         yyPCB = NULL;
304         yyData = Ptr;
305         yyFont = &PCB->Font;
306         yyElement = NULL;
307         return(Parse(NULL,NULL,Filename,NULL));
310 /* ---------------------------------------------------------------------------
311  * initializes LEX and calls parser for a single library entry
312  */
313 int ParseLibraryEntry(DataTypePtr Ptr, char *Template)
315         yyPCB = NULL;
316         yyData = Ptr;
317         yyFont = &PCB->Font;
318         yyElement = NULL;
319         return(Parse(Settings.LibraryCommand, Settings.LibraryPath,
320                 Settings.LibraryFilename, Template));
323 /* ---------------------------------------------------------------------------
324  * initializes LEX and calls parser for a complete board
325  */
326 int ParsePCB(PCBTypePtr Ptr, char *Filename)
328         yyPCB = Ptr;
329         yyData = NULL;
330         yyFont = NULL;
331         yyElement = NULL;
332         return(Parse(Settings.FileCommand, Settings.FilePath, Filename, NULL));
335 /* ---------------------------------------------------------------------------
336  * initializes LEX and calls parser for a font
337  */
338 int ParseFont(FontTypePtr Ptr, char *Filename)
340         int r = 0;
341         char *path, *p;
342         yyPCB = NULL;
343         yyFont = Ptr;
344         yyElement = NULL;
346         path = strdup (Settings.FontPath);
348         /* search through the font path for a font file */
349         for (p = strtok (path, PCB_PATH_DELIMETER); p && *p;
350                 p = strtok (NULL, PCB_PATH_DELIMETER))
351           {
352 #ifdef DEBUG
353             Message ("Looking for %s in %s\n", Filename, p);
354 #endif
355             r = Parse(Settings.FontCommand, p, Filename, NULL);
356             if (r == 0)
357               {
358 #ifdef DEBUG
359                 Message ("Found %s in %s\n", Filename, p);
360 #endif
361                 break;
362               }
363           }
364         free (path);
366         return r;
369 static int
370 parse_number ()
372   yylval.number = g_ascii_strtod ((gchar *) yytext, NULL);
373   return FLOATING;