7 * PCB, interactive printed circuit board design
8 * Copyright (C) 1994,1995,1996,2006 Thomas Nau
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.
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.
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.
24 * Contact addresses for paper mail and Email:
25 * Thomas Nau, Schlehenweg 15, 88471 Baustetten, Germany
26 * Thomas.Nau@rz.uni-ulm.de
30 /* lexical definitions to parse ASCII input of PCB and Element description
41 #if defined(_POSIX_SOURCE) || defined(_HPUX_SOURCE)
47 #ifdef HAVE_LIBDMALLOC
48 # include <dmalloc.h> /* see http://dmalloc.com */
55 #include "crosshair.h"
65 /* ---------------------------------------------------------------------------
66 * some shared parser identifiers
69 int yylineno; /* linenumber */
70 #define yyunput ATTRIBUTE_UNUSED yyunput
73 char *yyfilename; /* in this file */
74 PCBTypePtr yyPCB; /* used by parser */
76 ElementTypePtr yyElement;
79 static int parse_number (double);
81 /* ---------------------------------------------------------------------------
82 * an external prototypes
86 /* ---------------------------------------------------------------------------
87 * some local prototypes
89 static int Parse(char *, char *, char *, char *);
94 DECIMAL -?[1-9][0-9]*|0
95 FLOATING [+-]?[0-9]*("."[0-9]*)?
96 FLOATINGMM {FLOATING}[mM][mM]
97 FLOATINGUM {FLOATING}[uU][mM]
98 FLOATINGIN {FLOATING}[iI][nN]
99 FLOATINGMIL {FLOATING}[mM][iI][lL]
100 CARDINAL [1-9][0-9]*|0
101 STRINGCHAR ([^"\n\r\\]|\\.)
107 FileVersion { return(T_FILEVERSION); }
108 PCB { return(T_PCB); }
109 Grid { return(T_GRID); }
110 Cursor { return(T_CURSOR); }
111 Thermal { return(T_THERMAL); }
112 PolyArea { return(T_AREA); }
113 DRC { return(T_DRC); }
114 Flags { return(T_FLAGS); }
115 Layer { return(T_LAYER); }
116 Pin { return(T_PIN); }
117 Pad { return(T_PAD); }
118 Via { return(T_VIA); }
119 Line { return(T_LINE); }
120 Rat { return(T_RAT); }
121 Rectangle { return(T_RECTANGLE); }
122 Text { return(T_TEXT); }
123 ElementLine { return(T_ELEMENTLINE); }
124 ElementArc { return(T_ELEMENTARC); }
125 Element { return(T_ELEMENT); }
126 SymbolLine { return(T_SYMBOLLINE); }
127 Symbol { return(T_SYMBOL); }
128 Mark { return(T_MARK); }
129 Groups { return(T_GROUPS); }
130 Styles { return(T_STYLES); }
131 Polygon { return(T_POLYGON); }
132 Arc { return(T_ARC); }
133 NetList { return(T_NETLIST); }
134 Net { return(T_NET); }
135 Connect { return(T_CONN); }
136 Attribute { return(T_ATTRIBUTE); }
139 yylval.number = (unsigned) *(yytext+1);
142 {FLOATINGMM} { return parse_number(3937.0079); }
143 {FLOATINGUM} { return parse_number(3.9370079); }
144 {FLOATINGIN} { return parse_number(100000.0); }
145 {FLOATINGMIL} { return parse_number(100.0); }
146 {DECIMAL} { return parse_number(1.0); }
149 sscanf((char *) yytext, "%x", &yylval.number);
153 yylval.floating = c_strtod (yytext);
159 /* return NULL on empty string */
162 yylval.string = NULL;
166 /* allocate memory and copy string;
167 * stringlength is counted and copied without
168 * leading and trailing '"'
171 yylval.string = MyCalloc(yyleng+1, sizeof(char), "LEX");
172 p1 = (char *) (yytext +1);
176 /* check for special character */
196 . { return(*yytext); }
200 /* ---------------------------------------------------------------------------
201 * sets up the preprocessor command
203 static int Parse(char *Executable, char *Path, char *Filename, char *Parameter)
205 static char *command = NULL;
211 static Boolean firsttime = True;
214 if (EMPTY_STRING_P (Executable))
220 l += strlen (Filename);
222 if ( (tmps = (char *) malloc ( l * sizeof (char))) == NULL)
224 fprintf (stderr, "Parse(): malloc failed\n");
228 if ( Path != NULL && *Path != '\0')
229 sprintf (tmps, "%s%s%s", Path, PCB_DIR_SEPARATOR_S, Filename);
231 sprintf (tmps, "%s", Filename);
233 yyin = fopen (tmps, "r");
236 Message("Can't open %s for reading\n", tmps);
244 /* release old command and create new from template */
247 command = EvaluateFilename(Executable, Path, Filename, Parameter);
249 /* open pipe to stdout of command */
250 if (*command == '\0' || (yyin = popen(command, "r")) == NULL)
252 PopenErrorMessage(command);
258 /* reset parser if not called the first time */
264 /* init linenumber and filename for yyerror() */
266 yyfilename = Filename;
268 /* We need to save the data temporarily because lex-yacc are able
269 * to break the application if the input file has an illegal format.
270 * It's not necessary if the system supports the call of functions
274 #if !defined(HAS_ATEXIT) && !defined(HAS_ON_EXIT)
277 returncode = yyparse();
280 returncode = yyparse();
282 /* clean up parse buffer */
283 yy_delete_buffer(YY_CURRENT_BUFFER);
286 return(pclose(yyin) ? 1 : returncode);
287 return(fclose(yyin) ? 1 : returncode);
290 /* ---------------------------------------------------------------------------
291 * initializes LEX and calls parser for a single element file
293 int ParseElementFile(DataTypePtr Ptr, char *Filename)
299 return(Parse(NULL,NULL,Filename,NULL));
302 /* ---------------------------------------------------------------------------
303 * initializes LEX and calls parser for a single library entry
305 int ParseLibraryEntry(DataTypePtr Ptr, char *Template)
311 return(Parse(Settings.LibraryCommand, Settings.LibraryPath,
312 Settings.LibraryFilename, Template));
315 /* ---------------------------------------------------------------------------
316 * initializes LEX and calls parser for a complete board
318 int ParsePCB(PCBTypePtr Ptr, char *Filename)
324 return(Parse(Settings.FileCommand, Settings.FilePath, Filename, NULL));
327 /* ---------------------------------------------------------------------------
328 * initializes LEX and calls parser for a font
330 int ParseFont(FontTypePtr Ptr, char *Filename)
338 path = strdup (Settings.FontPath);
340 /* search through the font path for a font file */
341 for (p = strtok (path, PCB_PATH_DELIMETER); p && *p;
342 p = strtok (NULL, PCB_PATH_DELIMETER))
344 Message ("Looking for %s in %s\n", Filename, p);
345 r = Parse(Settings.FontCommand, p, Filename, NULL);
348 Message ("Found %s in %s\n", Filename, p);
358 parse_number (double scale)
361 sscanf ((char *) yytext, "%lf", &val);
363 /* Round towards nearest means we have to check the sign first. */
368 yylval.number = (int)(val);