Tag physically/logically connected items differently in the "Find" action
[geda-pcb/pcjc2.git] / src / res_parse.y
blob23facf644e52227f757fb310063177427e6e2ba7
1 %{
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <stdio.h>
8 #include <stdlib.h>
10 #ifdef HAVE_STRING_H
11 #include <string.h>
12 #endif
14 #define YYDEBUG 0
15 #define YYERROR_VERBOSE 1
17 /* #define YYSTYPE void * */
19 #include "global.h"
20 #include "resource.h"
21 #include "res_parse.h"
23 #ifdef HAVE_LIBDMALLOC
24 #include <dmalloc.h>
25 #endif
27 static Resource *parsed_res;
28 static Resource *current_res;
30 int reserror(const char *);
31 int reslex();
33 #define f(x) current_res->flags |= x
37 %name-prefix="res"
38 %start top_res
40 %union {
41 int ival;
42 char *sval;
43 Resource *rval;
46 %token <sval> STRING INCLUDE
47 %type <rval> res
51 top_res
52 : { current_res = parsed_res = resource_create(NULL); }
53 res_item_zm
56 res
57 : { current_res = resource_create(current_res); }
58 '{' res_item_zm '}'
59 { $$ = current_res; current_res = current_res->parent; }
62 res_item_zm : res_item res_item_zm | ;
63 res_item
64 : STRING { resource_add_val(current_res, 0, $1, 0); f(FLAG_V); }
65 | STRING '=' STRING { resource_add_val(current_res, $1, $3, 0); f(FLAG_NV); }
66 | INCLUDE { resource_add_val(current_res, 0, $1, 0); f(FLAG_S); }
67 | res { resource_add_val(current_res, 0, 0, $1); f(FLAG_S); }
68 | STRING '=' res { resource_add_val(current_res, $1, 0, $3); f(FLAG_NS); }
69 | error
74 static const char *res_filename = 0;
75 static FILE *res_file = 0;
76 static const char **res_strings = 0;
77 static int res_string_idx = 0;
78 int res_lineno;
80 int
81 res_parse_getchars(char *buf, int max_size)
83 if (res_file)
85 int i = fgetc(res_file);
86 buf[0] = i;
87 if (i == EOF)
88 return 0;
90 else
92 if (res_strings[0] == 0)
93 return 0;
94 buf[0] = res_strings[0][res_string_idx];
95 res_string_idx ++;
96 if (buf[0] == 0)
98 res_strings ++;
99 res_string_idx = 0;
100 buf[0] = '\n';
101 return 1;
104 return 1;
107 Resource *
108 resource_parse(const char *filename, const char **strings)
110 res_lineno = 1;
111 if (filename)
113 res_filename = filename;
114 res_file = fopen (filename, "r");
115 if (res_file == NULL)
117 perror(filename);
118 return 0;
121 else if (strings)
123 res_filename = NULL;
124 res_strings = strings;
125 res_string_idx = 0;
127 else
128 return NULL;
129 #if YYDEBUG
130 yydebug = 1;
131 #endif
132 if (resparse())
133 return 0;
134 if (filename)
135 fclose (res_file);
136 else
137 res_strings = 0;
138 return parsed_res;
141 Resource *
142 resource_create(Resource *parent)
144 Resource *rv = (Resource *)malloc(sizeof(Resource));
145 rv->parent = parent;
146 rv->flags = 0;
147 rv->c = 0;
148 rv->v = (ResourceVal *)malloc(sizeof(ResourceVal));
149 return rv;
152 void
153 resource_add_val(Resource *n, char *name, char *value, Resource *subres)
155 n->v = (ResourceVal *)realloc(n->v, sizeof(ResourceVal)*(n->c+1));
156 n->v[n->c].name = name;
157 n->v[n->c].value = value;
158 n->v[n->c].subres = subres;
159 n->c++;
162 char *
163 resource_value(const Resource *res, char *name)
165 int i;
166 if (res == 0 || name == 0)
167 return 0;
168 for (i=0; i<res->c; i++)
169 if (res->v[i].name && res->v[i].value
170 && NSTRCMP(res->v[i].name, name) == 0)
171 return res->v[i].value;
172 return 0;
175 Resource *
176 resource_subres(const Resource *res, const char *name)
178 int i;
179 if (res == 0 || name == 0)
180 return 0;
181 for (i=0; i<res->c; i++)
182 if (res->v[i].name && res->v[i].subres
183 && NSTRCMP(res->v[i].name, name) == 0)
184 return res->v[i].subres;
185 return 0;
189 reserror(const char *str)
191 fprintf(stderr, "Error: %s around line %d: %s\n",
192 res_file ? res_filename : "internal strings",
193 res_lineno, str);
194 return 0;
197 static void
198 dump_res(Resource *n, int l)
200 int i;
201 for (i=0; i<n->c; i++)
203 if (n->v[i].subres)
205 printf("%*cn[%s] = {\n", l, ' ', n->v[i].name? n->v[i].name :"");
206 dump_res(n->v[i].subres, l+3);
207 printf("%*c}\n", l, ' ');
209 else
210 printf("%*cn[%s] = v[%s]\n", l, ' ', n->v[i].name? n->v[i].name :"", n->v[i].value? n->v[i].value :"");
214 void
215 resource_dump (Resource *r)
217 dump_res (r, 0);