sysinfo.y revision 33965
133965Sjdp%{ 233965Sjdp#include <stdio.h> 333965Sjdp#include <stdlib.h> 433965Sjdp 533965Sjdpextern char *word; 633965Sjdpextern char writecode; 733965Sjdpextern int number; 833965Sjdpextern int unit; 933965Sjdpchar nice_name[1000]; 1033965Sjdpchar *it; 1133965Sjdpint sofar; 1233965Sjdpint width; 1333965Sjdpint code; 1433965Sjdpchar * repeat; 1533965Sjdpchar *oldrepeat; 1633965Sjdpchar *name; 1733965Sjdpint rdepth; 1833965Sjdpchar *loop [] = {"","n","m","/*BAD*/"}; 1933965Sjdpchar *names[] = {" ","[n]","[n][m]"}; 2033965Sjdpchar *pnames[]= {"","*","**"}; 2133965Sjdp%} 2233965Sjdp 2333965Sjdp 2433965Sjdp%union { 2533965Sjdp int i; 2633965Sjdp char *s; 2733965Sjdp} 2833965Sjdp%token COND 2933965Sjdp%token REPEAT 3033965Sjdp%token '(' ')' 3133965Sjdp%token <s> TYPE 3233965Sjdp%token <s> NAME 3333965Sjdp%token <i> NUMBER UNIT 3433965Sjdp%type <i> attr_size 3533965Sjdp%type <s> attr_desc attr_id attr_type 3633965Sjdp%% 3733965Sjdp 3833965Sjdptop: { 3933965Sjdp switch (writecode) 4033965Sjdp { 4133965Sjdp case 'i': 4233965Sjdp printf("#ifdef SYSROFF_SWAP_IN\n"); 4333965Sjdp break; 4433965Sjdp case 'p': 4533965Sjdp printf("#ifdef SYSROFF_p\n"); 4633965Sjdp break; 4733965Sjdp case 'd': 4833965Sjdp break; 4933965Sjdp case 'g': 5033965Sjdp printf("#ifdef SYSROFF_SWAP_OUT\n"); 5133965Sjdp break; 5233965Sjdp case 'c': 5333965Sjdp printf("#ifdef SYSROFF_PRINT\n"); 5433965Sjdp printf("#include <stdio.h>\n"); 5533965Sjdp printf("#include <stdlib.h>\n"); 5633965Sjdp break; 5733965Sjdp } 5833965Sjdp } 5933965Sjdpit_list { 6033965Sjdp switch (writecode) { 6133965Sjdp case 'i': 6233965Sjdp case 'p': 6333965Sjdp case 'g': 6433965Sjdp case 'c': 6533965Sjdp printf("#endif\n"); 6633965Sjdp break; 6733965Sjdp case 'd': 6833965Sjdp break; 6933965Sjdp } 7033965Sjdp} 7133965Sjdp 7233965Sjdp ; 7333965Sjdp 7433965Sjdp 7533965Sjdpit_list: it it_list 7633965Sjdp | 7733965Sjdp ; 7833965Sjdp 7933965Sjdpit: 8033965Sjdp '(' NAME NUMBER 8133965Sjdp { 8233965Sjdp it = $2; code = $3; 8333965Sjdp switch (writecode) 8433965Sjdp { 8533965Sjdp case 'd': 8633965Sjdp printf("\n\n\n#define IT_%s_CODE 0x%x\n", it,code); 8733965Sjdp printf("struct IT_%s { \n", it); 8833965Sjdp break; 8933965Sjdp case 'i': 9033965Sjdp printf("void sysroff_swap_%s_in(ptr)\n",$2); 9133965Sjdp printf("struct IT_%s *ptr;\n", it); 9233965Sjdp printf("{\n"); 9333965Sjdp printf("char raw[255];\n"); 9433965Sjdp printf("\tint idx = 0 ;\n"); 9533965Sjdp printf("\tint size;\n"); 9633965Sjdp printf("memset(raw,0,255);\n"); 9733965Sjdp printf("memset(ptr,0,sizeof(*ptr));\n"); 9833965Sjdp printf("size = fillup(raw);\n"); 9933965Sjdp break; 10033965Sjdp case 'g': 10133965Sjdp printf("void sysroff_swap_%s_out(file,ptr)\n",$2); 10233965Sjdp printf("FILE * file;\n"); 10333965Sjdp printf("struct IT_%s *ptr;\n", it); 10433965Sjdp printf("{\n"); 10533965Sjdp printf("\tchar raw[255];\n"); 10633965Sjdp printf("\tint idx = 16 ;\n"); 10733965Sjdp printf("\tmemset (raw, 0, 255);\n"); 10833965Sjdp printf("\tcode = IT_%s_CODE;\n", it); 10933965Sjdp break; 11033965Sjdp case 'o': 11133965Sjdp printf("void sysroff_swap_%s_out(abfd,ptr)\n",$2); 11233965Sjdp printf("bfd * abfd;\n"); 11333965Sjdp printf("struct IT_%s *ptr;\n",it); 11433965Sjdp printf("{\n"); 11533965Sjdp printf("int idx = 0 ;\n"); 11633965Sjdp break; 11733965Sjdp case 'c': 11833965Sjdp printf("void sysroff_print_%s_out(ptr)\n",$2); 11933965Sjdp printf("struct IT_%s *ptr;\n", it); 12033965Sjdp printf("{\n"); 12133965Sjdp printf("itheader(\"%s\", IT_%s_CODE);\n",$2,$2); 12233965Sjdp break; 12333965Sjdp 12433965Sjdp case 't': 12533965Sjdp break; 12633965Sjdp } 12733965Sjdp 12833965Sjdp } 12933965Sjdp it_field_list 13033965Sjdp')' 13133965Sjdp{ 13233965Sjdp switch (writecode) { 13333965Sjdp case 'd': 13433965Sjdp printf("};\n"); 13533965Sjdp break; 13633965Sjdp case 'g': 13733965Sjdp printf("\tchecksum(file,raw, idx, IT_%s_CODE);\n", it); 13833965Sjdp 13933965Sjdp case 'i': 14033965Sjdp 14133965Sjdp case 'o': 14233965Sjdp case 'c': 14333965Sjdp printf("}\n"); 14433965Sjdp } 14533965Sjdp} 14633965Sjdp; 14733965Sjdp 14833965Sjdp 14933965Sjdp 15033965Sjdpit_field_list: 15133965Sjdp it_field it_field_list 15233965Sjdp | cond_it_field it_field_list 15333965Sjdp | repeat_it_field it_field_list 15433965Sjdp | 15533965Sjdp ; 15633965Sjdp 15733965Sjdprepeat_it_field: '(' REPEAT NAME 15833965Sjdp { 15933965Sjdp rdepth++; 16033965Sjdp switch (writecode) 16133965Sjdp { 16233965Sjdp case 'c': 16333965Sjdp if (rdepth==1) 16433965Sjdp printf("\tprintf(\"repeat %%d\\n\", %s);\n",$3); 16533965Sjdp if (rdepth==2) 16633965Sjdp printf("\tprintf(\"repeat %%d\\n\", %s[n]);\n",$3); 16733965Sjdp case 'i': 16833965Sjdp case 'g': 16933965Sjdp case 'o': 17033965Sjdp 17133965Sjdp if (rdepth==1) 17233965Sjdp { 17333965Sjdp printf("\t{ int n; for (n = 0; n < %s; n++) {\n", $3); 17433965Sjdp } 17533965Sjdp if (rdepth == 2) { 17633965Sjdp printf("\t{ int m; for (m = 0; m < %s[n]; m++) {\n", $3); 17733965Sjdp } 17833965Sjdp 17933965Sjdp break; 18033965Sjdp } 18133965Sjdp 18233965Sjdp oldrepeat = repeat; 18333965Sjdp repeat = $3; 18433965Sjdp } 18533965Sjdp 18633965Sjdp it_field_list ')' 18733965Sjdp 18833965Sjdp { 18933965Sjdp repeat = oldrepeat; 19033965Sjdp oldrepeat =0; 19133965Sjdp rdepth--; 19233965Sjdp switch (writecode) 19333965Sjdp { 19433965Sjdp case 'i': 19533965Sjdp case 'g': 19633965Sjdp case 'o': 19733965Sjdp case 'c': 19833965Sjdp printf("\t}}\n"); 19933965Sjdp } 20033965Sjdp } 20133965Sjdp ; 20233965Sjdp 20333965Sjdp 20433965Sjdpcond_it_field: '(' COND NAME 20533965Sjdp { 20633965Sjdp switch (writecode) 20733965Sjdp { 20833965Sjdp case 'i': 20933965Sjdp case 'g': 21033965Sjdp case 'o': 21133965Sjdp case 'c': 21233965Sjdp printf("\tif (%s) {\n", $3); 21333965Sjdp break; 21433965Sjdp } 21533965Sjdp } 21633965Sjdp 21733965Sjdp it_field_list ')' 21833965Sjdp { 21933965Sjdp switch (writecode) 22033965Sjdp { 22133965Sjdp case 'i': 22233965Sjdp case 'g': 22333965Sjdp case 'o': 22433965Sjdp case 'c': 22533965Sjdp printf("\t}\n"); 22633965Sjdp } 22733965Sjdp } 22833965Sjdp ; 22933965Sjdp 23033965Sjdpit_field: 23133965Sjdp '(' attr_desc '(' attr_type attr_size ')' attr_id 23233965Sjdp {name = $7; } 23333965Sjdp enums ')' 23433965Sjdp { 23533965Sjdp char *desc = $2; 23633965Sjdp char *type = $4; 23733965Sjdp int size = $5; 23833965Sjdp char *id = $7; 23933965Sjdpchar *p = names[rdepth]; 24033965Sjdpchar *ptr = pnames[rdepth]; 24133965Sjdp switch (writecode) 24233965Sjdp { 24333965Sjdp case 'g': 24433965Sjdp if (size % 8) 24533965Sjdp { 24633965Sjdp 24733965Sjdp printf("\twriteBITS(ptr->%s%s,raw,&idx,%d);\n", 24833965Sjdp id, 24933965Sjdp names[rdepth], size); 25033965Sjdp 25133965Sjdp } 25233965Sjdp else { 25333965Sjdp printf("\twrite%s(ptr->%s%s,raw,&idx,%d,file);\n", 25433965Sjdp type, 25533965Sjdp id, 25633965Sjdp names[rdepth],size/8); 25733965Sjdp } 25833965Sjdp break; 25933965Sjdp case 'i': 26033965Sjdp { 26133965Sjdp 26233965Sjdp if (rdepth >= 1) 26333965Sjdp 26433965Sjdp { 26533965Sjdp printf("if (!ptr->%s) ptr->%s = (%s*)xcalloc(%s, sizeof(ptr->%s[0]));\n", 26633965Sjdp id, 26733965Sjdp id, 26833965Sjdp type, 26933965Sjdp repeat, 27033965Sjdp id); 27133965Sjdp } 27233965Sjdp 27333965Sjdp if (rdepth == 2) 27433965Sjdp { 27533965Sjdp printf("if (!ptr->%s[n]) ptr->%s[n] = (%s**)xcalloc(%s[n], sizeof(ptr->%s[n][0]));\n", 27633965Sjdp id, 27733965Sjdp id, 27833965Sjdp type, 27933965Sjdp repeat, 28033965Sjdp id); 28133965Sjdp } 28233965Sjdp 28333965Sjdp } 28433965Sjdp 28533965Sjdp if (size % 8) 28633965Sjdp { 28733965Sjdp printf("\tptr->%s%s = getBITS(raw,&idx, %d,size);\n", 28833965Sjdp id, 28933965Sjdp names[rdepth], 29033965Sjdp size); 29133965Sjdp } 29233965Sjdp else { 29333965Sjdp printf("\tptr->%s%s = get%s(raw,&idx, %d,size);\n", 29433965Sjdp id, 29533965Sjdp names[rdepth], 29633965Sjdp type, 29733965Sjdp size/8); 29833965Sjdp } 29933965Sjdp break; 30033965Sjdp case 'o': 30133965Sjdp printf("\tput%s(raw,%d,%d,&idx,ptr->%s%s);\n", type,size/8,size%8,id,names[rdepth]); 30233965Sjdp break; 30333965Sjdp case 'd': 30433965Sjdp if (repeat) 30533965Sjdp printf("\t/* repeat %s */\n", repeat); 30633965Sjdp 30733965Sjdp if (type[0] == 'I') { 30833965Sjdp printf("\tint %s%s; \t/* %s */\n",ptr,id, desc); 30933965Sjdp } 31033965Sjdp else if (type[0] =='C') { 31133965Sjdp printf("\tchar %s*%s;\t /* %s */\n",ptr,id, desc); 31233965Sjdp } 31333965Sjdp else { 31433965Sjdp printf("\tbarray %s%s;\t /* %s */\n",ptr,id, desc); 31533965Sjdp } 31633965Sjdp break; 31733965Sjdp case 'c': 31833965Sjdp printf("tabout();\n"); 31933965Sjdp printf("\tprintf(\"/*%-30s*/ ptr->%s = \");\n", desc, id); 32033965Sjdp 32133965Sjdp if (type[0] == 'I') 32233965Sjdp printf("\tprintf(\"%%d\\n\",ptr->%s%s);\n", id,p); 32333965Sjdp else if (type[0] == 'C') 32433965Sjdp printf("\tprintf(\"%%s\\n\",ptr->%s%s);\n", id,p); 32533965Sjdp 32633965Sjdp else if (type[0] == 'B') 32733965Sjdp { 32833965Sjdp printf("\tpbarray(&ptr->%s%s);\n", id,p); 32933965Sjdp } 33033965Sjdp else abort(); 33133965Sjdp break; 33233965Sjdp } 33333965Sjdp } 33433965Sjdp 33533965Sjdp ; 33633965Sjdp 33733965Sjdp 33833965Sjdpattr_type: 33933965Sjdp TYPE { $$ = $1; } 34033965Sjdp | { $$ = "INT";} 34133965Sjdp ; 34233965Sjdp 34333965Sjdpattr_desc: 34433965Sjdp '(' NAME ')' 34533965Sjdp { $$ = $2; } 34633965Sjdp ; 34733965Sjdp 34833965Sjdpattr_size: 34933965Sjdp NUMBER UNIT 35033965Sjdp { $$ = $1 * $2; } 35133965Sjdp ; 35233965Sjdp 35333965Sjdp 35433965Sjdpattr_id: 35533965Sjdp '(' NAME ')' { $$ = $2; } 35633965Sjdp | { $$ = "dummy";} 35733965Sjdp ; 35833965Sjdp 35933965Sjdpenums: 36033965Sjdp | '(' enum_list ')' ; 36133965Sjdp 36233965Sjdpenum_list: 36333965Sjdp | 36433965Sjdp enum_list '(' NAME NAME ')' { 36533965Sjdp switch (writecode) 36633965Sjdp { 36733965Sjdp case 'd': 36833965Sjdp printf("#define %s %s\n", $3,$4); 36933965Sjdp break; 37033965Sjdp case 'c': 37133965Sjdp printf("if (ptr->%s%s == %s) { tabout(); printf(\"%s\\n\");}\n", name, names[rdepth],$4,$3); 37233965Sjdp } 37333965Sjdp } 37433965Sjdp 37533965Sjdp ; 37633965Sjdp 37733965Sjdp 37833965Sjdp 37933965Sjdp%% 38033965Sjdp/* four modes 38133965Sjdp 38233965Sjdp -d write structure defintions for sysroff in host format 38333965Sjdp -i write functions to swap into sysroff format in 38433965Sjdp -o write functions to swap into sysroff format out 38533965Sjdp -c write code to print info in human form */ 38633965Sjdp 38733965Sjdpint yydebug; 38833965Sjdpchar writecode; 38933965Sjdp 39033965Sjdpint 39133965Sjdpmain(ac,av) 39233965Sjdpint ac; 39333965Sjdpchar **av; 39433965Sjdp{ 39533965Sjdp yydebug=0; 39633965Sjdp if (ac > 1) 39733965Sjdp writecode = av[1][1]; 39833965Sjdpif (writecode == 'd') 39933965Sjdp { 40033965Sjdp printf("typedef struct { unsigned char *data; int len; } barray; \n"); 40133965Sjdp printf("typedef int INT;\n"); 40233965Sjdp printf("typedef char * CHARS;\n"); 40333965Sjdp 40433965Sjdp } 40533965Sjdp yyparse(); 40633965Sjdpreturn 0; 40733965Sjdp} 40833965Sjdp 40933965Sjdpint yyerror(s) 41033965Sjdp char *s; 41133965Sjdp{ 41233965Sjdp fprintf(stderr, "%s\n" , s); 41333965Sjdp} 414