/********************************************************* * hdecode.c G. D'Agostini 11/3/91 * * * * Program to create starting from a definition file * * a structure relating the mnemonics and the constant. * * It is usefull to allow the use of the mnemonics * * also as calling parameters of the program. * * * * Warning : the conversion is done only to the * * definitions of numerical constant, assumed * * to be integers, performed inside the * * definition file. * * The aliases are also recognized. * * * * Check the resulting file before using it ! * *********************************************************/ #include #include #include #include #define YES 1 #define NO 0 main(argc, argv) int argc; char *argv[]; { int pin, ptemp, pout; int i, nwrite, linel, tlinel, accept, lcount; char *temp_file = "/dd/hdecode_temp"; char line[80], tline[80], cline[80]; char *def_key = "#define"; char *suffix = "_conv_tab"; char struct_name[32]; char key[32], code[32], dmy[32]; char out_file[64]; int key_in, key_l, code_in, code_l, dmy_in, dmy_l; char *blanks = " "; if (argc < 2 || (argc == 2 && *argv[1] == '-' )) { printf(" hdecode extract from a definition file a \n"); printf(" a structure containing a conversion table\n"); printf(" between mnemonics and constant \n\n"); printf(" usage : hdecode def_file_name \n"); printf(" ( the output file will be def_file_name.mn )\n"); exit(0); } printf("hdecode is going to extract mnemonic structure from %s\n", argv[1]); if ((pin = open(argv[1], 0x03)) == -1) exit(_errmsg(errno," hdecode: error opening %s\n",argv[1])); strcpy(out_file, argv[1]); strncat(out_file, "mn", 2); if((pout = create(out_file, 0x03, 0x03)) == -1) exit(_errmsg(errno," hdecode: error creating %s\n",out_file)); if((ptemp = create(temp_file, 0x03, 0x03)) == -1) exit(_errmsg(errno," hdecode: error creating %s\n",temp_file)); strcpy(cline, "\n"); write(ptemp, cline, strlen(cline)); write(ptemp, cline, strlen(cline)); strcpy(cline, "struct"); strncat(cline, " ",1); i = 0; for( i = 0; *(argv[1]+i) != '\0' && *(argv[1]+i) != '.' ; i++) *(struct_name+i) = *(argv[1]+i); *(struct_name+i) = '\0'; strncat(cline, struct_name, strlen(struct_name)); strncat(cline, suffix, strlen(suffix)); strncat(cline, " {\n", 3); write(ptemp, cline, strlen(cline)); strcpy(cline, " char *key;\n"); write(ptemp, cline, strlen(cline)); strcpy(cline, " int code;\n"); write(ptemp, cline, strlen(cline)); strcpy(cline, "} "); strncat(cline, struct_name, strlen(struct_name)); strncat(cline, "_conv[] = {\n", strlen("_conv[] = {\n")); write(ptemp, cline, strlen(cline)); nwrite = 0; while((linel = readln(pin, line, 80)) > 0) { if( (linel = comment_filter(line, linel))) { if (findnstr(1, line, def_key, linel) == 1){ if ((key_in = fword(line, linel, 7, key, &key_l)) > 0) { if ((code_in = fword(line, linel, key_in + key_l, code, &code_l)) > 0) { if (code[0] < '0' || code[0] > '9') { if( fword(line, linel, code_in +code_l, dmy, &dmy_l) < 0) { if (nwrite == 0) accept=NO; else if( nwrite > 0) { if (lseek(ptemp, 0, 0) == -1) exit(_errmsg(errno, " error rewinding %s\n",temp_file)); accept = NO; while((tlinel=(readln(ptemp, tline,80))) > 0) { if (findnstr(1, tline, code, tlinel) > 0) accept = YES; } if (lseek(ptemp, 0, 2) == -1) exit(_errmsg(errno, " error going to %s EOF\n",temp_file)); } } else accept = NO; } else accept = YES; if (accept == YES){ nwrite++; strcpy(cline, blanks, strlen(blanks)); strncat(cline, "\"", 1); strncat(cline, key, strlen(key)); strncat(cline, "\", ", 3); strncat(cline, key, strlen(key)); strncat(cline, ",\n",2); write(ptemp, cline, strlen(cline)); } } } } } } if (nwrite) { strcpy(cline, "};\n"); write(ptemp, cline, strlen(cline)); strcpy(cline, blanks); strncat(cline, "int ",strlen("int ")); strncat(cline, struct_name, strlen(struct_name)); strncat(cline, "_conv_tab_size = ",strlen("_conv_tab_size = ")); write(ptemp, cline, strlen(cline)); itoa(nwrite, cline); strncat(cline, ";\n", 2); write(ptemp, cline, strlen(cline)); lseek(pout, 0 , 0); lseek(ptemp, 0, 0); lcount = 0; while((linel = readln(ptemp, line, 80)) > 0){ lcount++; write(pout, line, linel); } } else printf(" decode structure not created \n"); strcpy(cline, "del "); strncat(cline, temp_file, strlen(temp_file)); close(pin); close(ptemp); close(pout); system(cline); if (lcount > 0){ printf("output file is %s ( %d lines, ", out_file, lcount); printf("%d mnemonics )\n", nwrite); } } int fword(line, linel, fchar, word, nchar) char *line, *word; int linel, fchar, *nchar; { int i, beg; int inword = NO; *nchar = 0; beg = -1; for( i = fchar; i < linel ; i++) { if ((line[i] == ' ' || line[i] == '\n' || line[i] == '\t' || line[i] == '\0') && inword) break; else if ( line[i] != ' ' && line[i] != '\n' && line[i] != '\t') { if (inword == NO) { inword = YES; beg = i; } word[(*nchar)++] = line[i]; } } word[*nchar] = '\0'; return( beg ); } int comment_filter(line, linel) char *line; int linel; { static int incom; int i, j, fchar; int com_beg = -1; int com_end = -1; if ( incom == NO ) if ((com_beg = findnstr( 1, line, "/*", linel) -1) >= 0) incom = YES; if ( incom == YES ){ fchar = 1; if ( com_beg >= 0) fchar = com_beg +2; com_end = findstr(fchar , line , "*/", linel) -1 ; if (com_end >= 0) { com_end += 1; incom = NO; } } if (com_beg < 0 && com_end < 0 && incom == NO) return(linel); /* accept full line */ else if (com_beg < 0 && com_end < 0 && incom == YES) return(0); /* reject full line */ else if(com_beg < 0) com_beg = 0; else if(com_end < 0) com_end = linel; j = 0; for (i = com_beg; i < linel ; i++) if (i > com_end) *(line + com_beg + j++) = *(line + i); /* *(line + com_beg + j) = '\n'; */ return ( linel - (com_end - com_beg +1)); }