hid/gtk: Clean up ghid_pan_fixup ()
[geda-pcb/pcjc2.git] / src / res_parse.y
blob373785cb62d1d9bea80803f1ba18def12e3149e9
1 /* $Id$ */
3 %{
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif
9 #include <stdio.h>
10 #include <stdlib.h>
12 #ifdef HAVE_STRING_H
13 #include <string.h>
14 #endif
16 #define YYDEBUG 0
17 #define YYERROR_VERBOSE 1
19 /* #define YYSTYPE void * */
21 #include "global.h"
22 #include "resource.h"
23 #include "res_parse.h"
25 #ifdef HAVE_LIBDMALLOC
26 #include <dmalloc.h>
27 #endif
30 RCSID("$Id$");
32 static Resource *parsed_res;
33 static Resource *current_res;
35 int reserror(char *);
36 int reslex();
38 #define f(x) current_res->flags |= x
42 %name-prefix="res"
43 %start top_res
45 %union {
46 int ival;
47 char *sval;
48 Resource *rval;
51 %token <sval> STRING INCLUDE
52 %type <rval> res
56 top_res
57 : { current_res = parsed_res = resource_create(NULL); }
58 res_item_zm
61 res
62 : { current_res = resource_create(current_res); }
63 '{' res_item_zm '}'
64 { $$ = current_res; current_res = current_res->parent; }
67 res_item_zm : res_item res_item_zm | ;
68 res_item
69 : STRING { resource_add_val(current_res, 0, $1, 0); f(FLAG_V); }
70 | STRING '=' STRING { resource_add_val(current_res, $1, $3, 0); f(FLAG_NV); }
71 | INCLUDE { resource_add_val(current_res, 0, $1, 0); f(FLAG_S); }
72 | res { resource_add_val(current_res, 0, 0, $1); f(FLAG_S); }
73 | STRING '=' res { resource_add_val(current_res, $1, 0, $3); f(FLAG_NS); }
74 | error
79 static char *res_filename = 0;
80 static FILE *res_file = 0;
81 static const char **res_strings = 0;
82 static int res_string_idx = 0;
83 int res_lineno;
85 int
86 res_parse_getchars(char *buf, int max_size)
88 if (res_file)
90 int i = fgetc(res_file);
91 buf[0] = i;
92 if (i == EOF)
93 return 0;
95 else
97 if (res_strings[0] == 0)
98 return 0;
99 buf[0] = res_strings[0][res_string_idx];
100 res_string_idx ++;
101 if (buf[0] == 0)
103 res_strings ++;
104 res_string_idx = 0;
105 buf[0] = '\n';
106 return 1;
109 return 1;
112 Resource *
113 resource_parse(char *filename, const char **strings)
115 res_lineno = 1;
116 if (filename)
118 res_filename = filename;
119 res_file = fopen (filename, "r");
120 if (res_file == NULL)
122 perror(filename);
123 return 0;
126 else if (strings)
128 res_filename = NULL;
129 res_strings = strings;
130 res_string_idx = 0;
132 else
133 return NULL;
134 #if YYDEBUG
135 yydebug = 1;
136 #endif
137 if (resparse())
138 return 0;
139 if (filename)
140 fclose (res_file);
141 else
142 res_strings = 0;
143 return parsed_res;
146 Resource *
147 resource_create(Resource *parent)
149 Resource *rv = (Resource *)malloc(sizeof(Resource));
150 rv->parent = parent;
151 rv->flags = 0;
152 rv->c = 0;
153 rv->v = (ResourceVal *)malloc(sizeof(ResourceVal));
154 return rv;
157 void
158 resource_add_val(Resource *n, char *name, char *value, Resource *subres)
160 n->v = (ResourceVal *)realloc(n->v, sizeof(ResourceVal)*(n->c+1));
161 n->v[n->c].name = name;
162 n->v[n->c].value = value;
163 n->v[n->c].subres = subres;
164 n->c++;
167 char *
168 resource_value(Resource *res, char *name)
170 int i;
171 if (res == 0 || name == 0)
172 return 0;
173 for (i=0; i<res->c; i++)
174 if (res->v[i].name && res->v[i].value
175 && NSTRCMP(res->v[i].name, name) == 0)
176 return res->v[i].value;
177 return 0;
180 Resource *
181 resource_subres(Resource *res, char *name)
183 int i;
184 if (res == 0 || name == 0)
185 return 0;
186 for (i=0; i<res->c; i++)
187 if (res->v[i].name && res->v[i].subres
188 && NSTRCMP(res->v[i].name, name) == 0)
189 return res->v[i].subres;
190 return 0;
194 reserror(char *str)
196 fprintf(stderr, "Error: %s around line %d: %s\n",
197 res_file ? res_filename : "internal strings",
198 res_lineno, str);
199 return 0;
202 static void
203 dump_res(Resource *n, int l)
205 int i;
206 for (i=0; i<n->c; i++)
208 if (n->v[i].subres)
210 printf("%*cn[%s] = {\n", l, ' ', n->v[i].name? n->v[i].name :"");
211 dump_res(n->v[i].subres, l+3);
212 printf("%*c}\n", l, ' ');
214 else
215 printf("%*cn[%s] = v[%s]\n", l, ' ', n->v[i].name? n->v[i].name :"", n->v[i].value? n->v[i].value :"");
219 void
220 resource_dump (Resource *r)
222 dump_res (r, 0);