Fix typos in ChangeLogs and update copyright notices
[external/binutils.git] / binutils / sysinfo.y
1 /* Copyright 2001 Free Software Foundation, Inc.
2    Written by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
3
4 This file is part of GNU binutils.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 %{
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 extern char *word;
25 extern char writecode;
26 extern int number;
27 extern int unit;
28 char nice_name[1000];
29 char *it;
30 int sofar;
31 int width;
32 int code;
33 char * repeat;
34 char *oldrepeat;
35 char *name;
36 int rdepth;
37 char *loop [] = {"","n","m","/*BAD*/"};
38 char *names[] = {" ","[n]","[n][m]"};
39 char *pnames[]= {"","*","**"};
40 %}
41
42
43 %union {
44  int i;
45  char *s;
46
47 %token COND
48 %token REPEAT
49 %token '(' ')'
50 %token <s> TYPE
51 %token <s> NAME
52 %token <i> NUMBER UNIT
53 %type <i> attr_size 
54 %type <s> attr_desc attr_id attr_type
55 %%
56
57 top:  {
58   switch (writecode)
59     {
60     case 'i':
61       printf("#ifdef SYSROFF_SWAP_IN\n");
62       break; 
63     case 'p':
64       printf("#ifdef SYSROFF_p\n");
65       break; 
66     case 'd':
67       break;
68     case 'g':
69       printf("#ifdef SYSROFF_SWAP_OUT\n");
70       break;
71     case 'c':
72       printf("#ifdef SYSROFF_PRINT\n");
73       printf("#include <stdio.h>\n");
74       printf("#include <stdlib.h>\n");
75       break;
76     }
77  } 
78 it_list {
79   switch (writecode) {
80   case 'i':
81   case 'p':
82   case 'g':
83   case 'c':
84     printf("#endif\n");
85     break; 
86   case 'd':
87     break;
88   }
89 }
90
91   ;
92
93
94 it_list: it it_list
95   |
96   ;
97
98 it:
99         '(' NAME NUMBER 
100       {
101         it = $2; code = $3;
102         switch (writecode) 
103           {
104           case 'd':
105             printf("\n\n\n#define IT_%s_CODE 0x%x\n", it,code);
106             printf("struct IT_%s { \n", it);
107             break;
108           case 'i':
109             printf("void sysroff_swap_%s_in(ptr)\n",$2);
110             printf("struct IT_%s *ptr;\n", it);
111             printf("{\n");
112             printf("char raw[255];\n");
113             printf("\tint idx = 0 ;\n");
114             printf("\tint size;\n");
115             printf("memset(raw,0,255);\n");     
116             printf("memset(ptr,0,sizeof(*ptr));\n");
117             printf("size = fillup(raw);\n");
118             break;
119           case 'g':
120             printf("void sysroff_swap_%s_out(file,ptr)\n",$2);
121             printf("FILE * file;\n");
122             printf("struct IT_%s *ptr;\n", it);
123             printf("{\n");
124             printf("\tchar raw[255];\n");
125             printf("\tint idx = 16 ;\n");
126             printf("\tmemset (raw, 0, 255);\n");
127             printf("\tcode = IT_%s_CODE;\n", it);
128             break;
129           case 'o':
130             printf("void sysroff_swap_%s_out(abfd,ptr)\n",$2);
131             printf("bfd * abfd;\n");
132             printf("struct IT_%s *ptr;\n",it);
133             printf("{\n");
134             printf("int idx = 0 ;\n");
135             break;
136           case 'c':
137             printf("void sysroff_print_%s_out(ptr)\n",$2);
138             printf("struct IT_%s *ptr;\n", it);
139             printf("{\n");
140             printf("itheader(\"%s\", IT_%s_CODE);\n",$2,$2);
141             break;
142
143           case 't':
144             break;
145           }
146
147       } 
148         it_field_list 
149 ')'
150 {
151   switch (writecode) {
152   case 'd': 
153     printf("};\n");
154     break;
155   case 'g':
156     printf("\tchecksum(file,raw, idx, IT_%s_CODE);\n", it);
157     
158   case 'i':
159
160   case 'o':
161   case 'c':
162     printf("}\n");
163   }
164 }
165 ;
166
167
168
169 it_field_list:
170                 it_field it_field_list
171         |       cond_it_field it_field_list     
172         |       repeat_it_field it_field_list
173         |
174         ;
175
176 repeat_it_field: '(' REPEAT NAME
177         {
178           rdepth++;
179           switch (writecode) 
180             {
181             case 'c':
182               if (rdepth==1)
183               printf("\tprintf(\"repeat %%d\\n\", %s);\n",$3);
184               if (rdepth==2)
185               printf("\tprintf(\"repeat %%d\\n\", %s[n]);\n",$3);
186             case 'i':
187             case 'g':
188             case 'o':
189
190               if (rdepth==1) 
191                 {
192               printf("\t{ int n; for (n = 0; n < %s; n++) {\n",    $3);
193             }
194               if (rdepth == 2) {
195               printf("\t{ int m; for (m = 0; m < %s[n]; m++) {\n",    $3);
196             }           
197
198               break;
199             }
200
201           oldrepeat = repeat;
202          repeat = $3;
203         }
204
205          it_field_list ')' 
206
207         {
208           repeat = oldrepeat;
209           oldrepeat =0;
210           rdepth--;
211           switch (writecode)
212             {
213             case 'i':
214             case 'g':
215             case 'o':
216             case 'c':
217           printf("\t}}\n");
218         }
219         }
220        ;
221
222
223 cond_it_field: '(' COND NAME
224         {
225           switch (writecode) 
226             {
227             case 'i':
228             case 'g':
229             case 'o':
230             case 'c':
231               printf("\tif (%s) {\n", $3);
232               break;
233             }
234         }
235
236          it_field_list ')' 
237         {
238           switch (writecode)
239             {
240             case 'i':
241             case 'g':
242             case 'o':
243             case 'c':
244           printf("\t}\n");
245         }
246         }
247        ;
248
249 it_field:
250         '(' attr_desc '(' attr_type attr_size ')' attr_id 
251         {name = $7; } 
252         enums ')'
253         {
254           char *desc = $2;
255           char *type = $4;
256           int size = $5;
257           char *id = $7;
258 char *p = names[rdepth];
259 char *ptr = pnames[rdepth];
260           switch (writecode) 
261             {
262             case 'g':
263               if (size % 8) 
264                 {
265                   
266                   printf("\twriteBITS(ptr->%s%s,raw,&idx,%d);\n",
267                          id,
268                          names[rdepth], size);
269
270                 }
271               else {
272                 printf("\twrite%s(ptr->%s%s,raw,&idx,%d,file);\n",
273                        type,
274                        id,
275                        names[rdepth],size/8);
276                 }
277               break;          
278             case 'i':
279               {
280
281                 if (rdepth >= 1)
282
283                   {
284                     printf("if (!ptr->%s) ptr->%s = (%s*)xcalloc(%s, sizeof(ptr->%s[0]));\n", 
285                            id, 
286                            id,
287                            type,
288                            repeat,
289                            id);
290                   }
291
292                 if (rdepth == 2)
293                   {
294                     printf("if (!ptr->%s[n]) ptr->%s[n] = (%s**)xcalloc(%s[n], sizeof(ptr->%s[n][0]));\n", 
295                            id, 
296                            id,
297                            type,
298                            repeat,
299                            id);
300                   }
301
302               }
303
304               if (size % 8) 
305                 {
306                   printf("\tptr->%s%s = getBITS(raw,&idx, %d,size);\n",
307                          id,
308                          names[rdepth], 
309                          size);
310                 }
311               else {
312                 printf("\tptr->%s%s = get%s(raw,&idx, %d,size);\n",
313                        id,
314                        names[rdepth],
315                        type,
316                        size/8);
317                 }
318               break;
319             case 'o':
320               printf("\tput%s(raw,%d,%d,&idx,ptr->%s%s);\n", type,size/8,size%8,id,names[rdepth]);
321               break;
322             case 'd':
323               if (repeat) 
324                 printf("\t/* repeat %s */\n", repeat);
325
326                   if (type[0] == 'I') {
327                   printf("\tint %s%s; \t/* %s */\n",ptr,id, desc);
328                 }
329                   else if (type[0] =='C') {
330                   printf("\tchar %s*%s;\t /* %s */\n",ptr,id, desc);
331                 }
332               else {
333                 printf("\tbarray %s%s;\t /* %s */\n",ptr,id, desc);
334               }
335                   break;
336                 case 'c':
337               printf("tabout();\n");
338                   printf("\tprintf(\"/*%-30s*/ ptr->%s = \");\n", desc, id);
339
340                   if (type[0] == 'I')
341                   printf("\tprintf(\"%%d\\n\",ptr->%s%s);\n", id,p);
342                   else   if (type[0] == 'C')
343                   printf("\tprintf(\"%%s\\n\",ptr->%s%s);\n", id,p);
344
345                   else   if (type[0] == 'B') 
346                     {
347                   printf("\tpbarray(&ptr->%s%s);\n", id,p);
348                 }
349               else abort();
350                   break;
351                 }
352         }
353
354         ;
355
356
357 attr_type:      
358          TYPE { $$ = $1; }
359         |  { $$ = "INT";}
360         ;
361
362 attr_desc: 
363         '(' NAME ')'    
364         { $$ = $2; }
365         ;
366
367 attr_size:
368          NUMBER UNIT 
369         { $$ = $1 * $2; }
370         ;
371
372
373 attr_id:
374                 '(' NAME ')'    { $$ = $2; }
375         |       { $$ = "dummy";}
376         ;       
377         
378 enums: 
379         | '(' enum_list ')' ;
380
381 enum_list:
382         |
383         enum_list '(' NAME NAME ')' { 
384           switch (writecode) 
385             {
386             case 'd':
387               printf("#define %s %s\n", $3,$4);
388               break;
389             case 'c':
390                 printf("if (ptr->%s%s == %s) { tabout(); printf(\"%s\\n\");}\n", name, names[rdepth],$4,$3);
391             }
392         }
393
394         ;
395
396
397
398 %%
399 /* four modes
400
401    -d write structure defintions for sysroff in host format
402    -i write functions to swap into sysroff format in
403    -o write functions to swap into sysroff format out
404    -c write code to print info in human form */
405
406 int yydebug;
407 char writecode;
408
409 int 
410 main(ac,av)
411 int ac;
412 char **av;
413 {
414   yydebug=0;
415   if (ac > 1)
416     writecode = av[1][1];
417 if (writecode == 'd')
418   {
419     printf("typedef struct { unsigned char *data; int len; } barray; \n");
420     printf("typedef  int INT;\n");
421     printf("typedef  char * CHARS;\n");
422
423   }
424   yyparse();
425 return 0;
426 }
427
428 int
429 yyerror(s)
430      char *s;
431 {
432   fprintf(stderr, "%s\n" , s);
433   return 0;
434 }