~ubuntu-branches/ubuntu/precise/pcb/precise

« back to all changes in this revision

Viewing changes to src/res_parse.y

  • Committer: Bazaar Package Importer
  • Author(s): Hamish Moffatt
  • Date: 2005-02-20 13:14:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050220131400-pfz66g5vhx0azl8f
Tags: 1.99j+20050127-2
* Improved package description: (closes: #295405)
* Fixed dependency: tk84 -> tk8.4 (closes: #295404)
* Updated README.debian (closes: #269578)
* Applied patch to src/djopt.c to allow compilation with gcc-4.0
  (closes: #294319), thanks to Andreas Jochens for the patch.
* Prevent example files from being compressed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: res_parse.y,v 1.8 2004/11/19 03:08:10 haceaton Exp $ */
 
2
 
 
3
%{
 
4
 
 
5
#ifdef HAVE_CONFIG_H
 
6
#include "config.h"
 
7
#endif
 
8
 
 
9
#include <stdio.h>
 
10
#include <stdlib.h>
 
11
 
 
12
#ifdef HAVE_STRING_H
 
13
#include <string.h>
 
14
#endif
 
15
 
 
16
#define YYDEBUG 0
 
17
#define YYERROR_VERBOSE 1
 
18
 
 
19
  /* #define YYSTYPE void * */
 
20
 
 
21
#include "global.h"
 
22
#include "resource.h"
 
23
#include "res_parse.h"
 
24
 
 
25
#ifdef HAVE_LIBDMALLOC
 
26
#include <dmalloc.h>
 
27
#endif
 
28
 
 
29
RCSID("$Id: res_parse.y,v 1.8 2004/11/19 03:08:10 haceaton Exp $");
 
30
 
 
31
static Resource *parsed_res;
 
32
static Resource *current_res;
 
33
 
 
34
static void resource_do_include(Resource *parent, char *);
 
35
 
 
36
int reserror(char *);
 
37
 
 
38
#define f(x) current_res->flags |= x
 
39
 
 
40
%}
 
41
 
 
42
%name-prefix="res"
 
43
%start top_res
 
44
 
 
45
%union {
 
46
  int ival;
 
47
  char *sval;
 
48
  Resource *rval;
 
49
};
 
50
 
 
51
%token <sval> STRING INCLUDE
 
52
%type <rval> res
 
53
 
 
54
%%
 
55
 
 
56
top_res
 
57
 : { current_res = parsed_res = resource_create(NULL); }
 
58
   res_item_zm
 
59
 ;
 
60
 
 
61
res
 
62
 : { current_res = resource_create(current_res); }
 
63
   '{' res_item_zm '}'
 
64
   { $$ = current_res; current_res = current_res->parent; }
 
65
 ;
 
66
 
 
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
 | res                  { resource_add_val(current_res, 0, 0, $1); f(FLAG_S); }
 
72
 | STRING '=' res       { resource_add_val(current_res, $1, 0, $3); f(FLAG_NS); }
 
73
 | INCLUDE              { resource_do_include(current_res, $1); }
 
74
 | error
 
75
 ;
 
76
 
 
77
%%
 
78
 
 
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;
 
84
 
 
85
int
 
86
res_parse_getchars(char *buf, int max_size)
 
87
{
 
88
  if (res_file)
 
89
    {
 
90
      int i = fgetc(res_file);
 
91
      buf[0] = i;
 
92
      if (i == EOF)
 
93
        return 0;
 
94
    }
 
95
  else
 
96
    {
 
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)
 
102
        {
 
103
          res_strings ++;
 
104
          res_string_idx = 0;
 
105
          buf[0] = '\n';
 
106
          return 1;
 
107
        }
 
108
    }
 
109
  return 1;
 
110
}
 
111
 
 
112
Resource *
 
113
resource_parse(char *filename, const char **strings)
 
114
{
 
115
  res_lineno = 1;
 
116
  if (filename)
 
117
    {
 
118
      res_filename = filename;
 
119
      res_file = fopen (filename, "r");
 
120
      if (res_file == NULL)
 
121
        {
 
122
          perror(filename);
 
123
          return 0;
 
124
        }
 
125
    }
 
126
  else if (strings)
 
127
    {
 
128
      res_filename = NULL;
 
129
      res_strings = strings;
 
130
      res_string_idx = 0;
 
131
    }
 
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;
 
144
}
 
145
 
 
146
Resource *
 
147
resource_create(Resource *parent)
 
148
{
 
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;
 
155
}
 
156
 
 
157
void
 
158
resource_add_val(Resource *n, char *name, char *value, Resource *subres)
 
159
{
 
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++;
 
165
}
 
166
 
 
167
char *
 
168
resource_value(Resource *res, char *name)
 
169
{
 
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;
 
178
}
 
179
 
 
180
Resource *
 
181
resource_subres(Resource *res, char *name)
 
182
{
 
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;
 
191
}
 
192
 
 
193
int
 
194
reserror(char *str)
 
195
{
 
196
  fprintf(stderr, "Error: %s around line %d: %s\n",
 
197
          res_file ? res_filename : "internal strings",
 
198
          res_lineno, str);
 
199
  return 0;
 
200
}
 
201
 
 
202
static void
 
203
dump_res(Resource *n, int l)
 
204
{
 
205
  int i;
 
206
  for (i=0; i<n->c; i++)
 
207
    {
 
208
      if (n->v[i].subres)
 
209
        {
 
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, ' ');
 
213
        }
 
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 :"");
 
216
    }
 
217
}
 
218
 
 
219
void
 
220
resource_dump (Resource *r)
 
221
{
 
222
  dump_res (r, 0);
 
223
}
 
224
 
 
225
extern struct {
 
226
  char *name;
 
227
  int (*func)(Resource *);
 
228
} MenuFuncList[];
 
229
extern int MenuFuncListSize;
 
230
 
 
231
void
 
232
resource_do_include(Resource *r, char *str)
 
233
{
 
234
  int i;
 
235
 
 
236
  for (i=0; i<MenuFuncListSize; i++)
 
237
    if (NSTRCMP (MenuFuncList[i].name, str) == 0)
 
238
      MenuFuncList[i].func(r);
 
239
}