static enumError Generate ( control_t * ctrl ) { ASSERT(ctrl); ASSERT(ctrl->info); FILE * cf = ctrl->cf; FILE * hf = ctrl->hf; ASSERT(cf); ASSERT(hf); FREE(ctrl->opt_allow_grp); FREE(ctrl->opt_allow_cmd); ctrl->opt_allow_grp = ctrl->opt_allow_cmd = 0; const info_t *info; //----- ui header fprintf(cf,text_ui_head); fprintf(hf,text_ui_head); //----- setup guard char guard[100]; snprintf(guard,sizeof(guard),"WIT_UI_%s_h",ctrl->info->c_name); char * ptr; for ( ptr = guard; *ptr; ptr++ ) *ptr = *ptr == '-' ? '_' : toupper((int)*ptr); fprintf(hf,"\n#ifndef %s\n#define %s\n",guard,guard); //----- header ASSERT( ctrl->info->type & T_DEF_TOOL ); ccp tool_name = ctrl->info->c_name; fprintf(cf,"#include <getopt.h>\n"); fprintf(cf,"#include \"ui-%s.h\"\n",tool_name); fprintf(hf,"#include \"lib-std.h\"\n"); fprintf(hf,"#include \"ui.h\"\n"); //----- print enum enumOptions & OptionInfo[] print_section(cf,sep1,"OptionInfo[]"); print_section(hf,sep1,"enum enumOptions"); char * var_ptr = var_buf; char * var_end = var_buf + sizeof(var_buf); var_ptr += snprintf(var_ptr,var_end-var_ptr, "extern const InfoOption_t OptionInfo[OPT__N_TOTAL+1];\n"); fprintf(cf, "const InfoOption_t OptionInfo[OPT__N_TOTAL+1] =\n" "{\n" " {0,0,0,0,0}, // OPT_NONE,\n" "\n" ); fprintf(hf, "typedef enum enumOptions\n" "{\n" "\tOPT_NONE,\n" "\n" ); ctrl->n_opt = 1; ctrl->n_opt_specific = 0; if ( ctrl->n_cmd ) { fprintf(cf," //----- command specific options -----\n\n"); fprintf(hf,"\t//----- command specific options -----\n\n"); for ( info = ctrl->info; info < ctrl->end; info++ ) if ( info->type & F_OPT_COMMAND ) print_opt(ctrl,info); ctrl->n_opt_specific = ctrl->n_opt; fprintf(cf, " {0,0,0,0,0}, // OPT__N_SPECIFIC == %d\n\n" " //----- global options -----\n\n", ctrl->n_opt_specific ); fprintf(hf, "\n\tOPT__N_SPECIFIC, // == %d \n\n" "\t//----- global options -----\n\n", ctrl->n_opt_specific ); } for ( info = ctrl->info; info < ctrl->end; info++ ) if ( info->type & F_OPT_GLOBAL ) print_opt(ctrl,info); fprintf(cf, " {0,0,0,0,0} // OPT__N_TOTAL == %d\n\n" "};\n" ,ctrl->n_opt ); fprintf(hf, "\n\tOPT__N_TOTAL // == %d\n\n" "} enumOptions;\n" ,ctrl->n_opt ); if (ctrl->n_opt_specific) { noTRACE("opt_allowed = ( %2u + %2u ) * %2u\n", ctrl->n_grp, ctrl->n_cmd, ctrl->n_opt_specific ); if (ctrl->n_grp) ctrl->opt_allow_grp = CALLOC(ctrl->n_grp,ctrl->n_opt_specific); ctrl->opt_allow_cmd = CALLOC(ctrl->n_cmd,ctrl->n_opt_specific); } //----- print alternate option infos bool done = false; const info_t * last_cmd = ctrl->info; ctrl->opt_prefix = "def"; for ( info = ctrl->info; info < ctrl->end; info++ ) { if ( info->type & T_CMD_BEG ) { ctrl->opt_prefix = "cmd"; last_cmd = info; } else if ( info->type & T_GRP_BEG ) { ctrl->opt_prefix = "grp"; last_cmd = info; } else if ( info->type & T_CMD_OPT && info->help && *info->help ) { if (!done) { print_section(cf,sep1,"alternate option infos"); done = true; } const info_t * info0; for ( info0 = ctrl->info; info0 < info; info0++ ) if ( info0->type & T_DEF_OPT && !strcmp(info->c_name,info0->c_name) ) { print_info_opt(ctrl,info,info0,last_cmd->c_name); break; } } } //----- print enum enumOptionsBit if ( ctrl->n_cmd ) { print_section(hf,sep1,"enum enumOptionsBit"); fprintf(hf, "//\t***** only for verification *****\n" "\n" "//typedef enum enumOptionsBit\n" "//{\n" "//\t//----- command specific options -----\n" "//\n" ); for ( info = ctrl->info; info < ctrl->end; info++ ) if ( info->type & F_OPT_COMMAND ) fprintf(hf,"//\tOB_%s%.*s= 1llu << OPT_%s,\n", info->c_name, ( 28 - (int)strlen(info->c_name) ) / 8, tabs, info->c_name ); fprintf(hf,"//\n//\t//----- group & command options -----\n"); for ( info = ctrl->info; info < ctrl->end; ) { ccp cmd_name; u8 * opt_allow = 0; if ( info->type & T_CMD_BEG ) { cmd_name = info->c_name; fprintf(hf,"//\n//\tOB_CMD_%s%.*s=", info->c_name, ( 24 - (int)strlen(info->c_name) ) / 8, tabs ); if (ctrl->opt_allow_cmd) { //PRINT("SELECT ALLOW CMD %u/%s\n",info->index,info->c_name); opt_allow = ctrl->opt_allow_cmd + info->index * ctrl->n_opt_specific; } } else if ( info->type & T_GRP_BEG ) { cmd_name = info->c_name; fprintf(hf,"//\n//\tOB_GRP_%s%.*s=", info->c_name, ( 24 - (int)strlen(info->c_name) ) / 8, tabs ); if (ctrl->opt_allow_grp) { //PRINT("SELECT ALLOW GRP %u/%s\n",info->index,info->c_name); opt_allow = ctrl->opt_allow_grp + info->index * ctrl->n_opt_specific; } } else { info++; continue; } info++; char * dest = iobuf; while ( info < ctrl->end ) { if ( info->type & T_ALL_OPT ) { dest += sprintf(dest,"\n//\t\t\t\t| ~(u64)0"); if (opt_allow) { //PRINT("ALLOW ALL\n"); memset(opt_allow,1,ctrl->n_opt_specific); } } else if ( info->type & T_COPY_CMD ) { dest += sprintf(dest,"\n//\t\t\t\t| OB_CMD_%s",info->c_name); if (opt_allow) { //PRINT("OR CMD %u/%s\n",info->index,info->c_name); DASSERT(ctrl->opt_allow_cmd); u8 * src = ctrl->opt_allow_cmd + info->index * ctrl->n_opt_specific; u8 * dest = opt_allow; int count = ctrl->n_opt_specific; while ( count-- > 0 ) *dest++ |= *src++; } } else if ( info->type & T_COPY_GRP ) { dest += sprintf(dest,"\n//\t\t\t\t| OB_GRP_%s",info->c_name); if ( opt_allow && ctrl->opt_allow_grp ) { //PRINT("OR GRP %u/%s\n",info->index,info->c_name); u8 * src = ctrl->opt_allow_grp + info->index * ctrl->n_opt_specific; u8 * dest = opt_allow; int count = ctrl->n_opt_specific; while ( count-- > 0 ) *dest++ |= *src++; } } else if ( info->type & T_CMD_OPT ) { if (FindStringField(&ctrl->copt,info->c_name)) { dest += sprintf(dest,"\n//\t\t\t\t| OB_%s",info->c_name); if ( opt_allow && info->index ) { //PRINT("ALLOW OPT %u/%s\n",info->index,info->c_name); opt_allow[info->index] = 1; } } else if (!FindStringField(&ctrl->gopt,info->c_name)) ERROR0(ERR_SEMANTIC,"Option not defined: %s %s --%s", tool_name, cmd_name, info->c_name ); } else if ( info->type & (T_CMD_BEG|T_GRP_BEG) ) break; ASSERT( dest < iobuf + sizeof(iobuf) ); info++; } if ( dest == iobuf ) fprintf(hf," 0,\n"); else fprintf(hf,"%s,\n",iobuf+8); } fprintf(hf,"//\n//} enumOptionsBit;\n"); } //----- print enum enumCommands & CommandTab[] print_section(hf,sep1,"enum enumCommands"); fprintf(hf, "typedef enum enumCommands\n" "{\n" "\tCMD__NONE," ); if ( ctrl->n_cmd ) { print_section(cf,sep1,"CommandTab[]"); fputs("\n\n",hf); var_ptr += snprintf(var_ptr,var_end-var_ptr, "extern const CommandTab_t CommandTab[];\n"); fprintf(cf, "const CommandTab_t CommandTab[] =\n" "{\n" ); for ( info = ctrl->info; info < ctrl->end; info++ ) if ( info->type & T_DEF_CMD ) { fprintf( hf, "\tCMD_%s,\n",info->c_name); ccp ptr = info->namelist; while (*ptr) { ccp n1 = ptr; while ( *ptr && *ptr != '|' ) ptr++; const int l1 = ptr - n1; while ( *ptr == '|' ) ptr++; if (*ptr) { ccp n2 = ptr; while ( *ptr && *ptr != '|' ) ptr++; const int l2 = ptr - n2; fprintf(cf, " { CMD_%s,%.*s\"%.*s\",%.*s\"%.*s\",%.*s0 },\n", info->c_name, (20-(int)strlen(info->c_name))/8, tabs, l1, n1, (20-l1)/8, tabs, l2, n2, (20-l2)/8, tabs ); while ( *ptr == '|' ) ptr++; } else fprintf(cf, " { CMD_%s,%.*s\"%.*s\",%.*s0,\t\t0 },\n", info->c_name, (20-(int)strlen(info->c_name))/8, tabs, l1, n1, (20-l1)/8, tabs ); } } else if ( info->type == T_SEP_CMD ) fprintf(hf,"\n"); fprintf(cf, "\n { CMD__N,0,0,0 }\n" "};\n" ); } fprintf(hf, "\n\tCMD__N // == %u\n\n" "} enumCommands;\n" , ctrl->n_cmd ); //----- print options print_section(cf,sep1,"OptionShort & OptionLong"); char * dest = iobuf; for ( info = ctrl->info; info < ctrl->end; info++ ) if ( info->type & T_DEF_OPT && info->namelist[1] == '|' ) { *dest++ = info->namelist[0]; if ( info->type & F_OPT_OPTPARAM ) *dest++ = ':'; if ( info->type & (F_OPT_OPTPARAM|F_OPT_PARAM) ) *dest++ = ':'; } *dest = 0; fprintf(cf,"const char OptionShort[] = \"%s\";\n\n",iobuf); var_ptr += snprintf(var_ptr,var_end-var_ptr, "extern const char OptionShort[];\n"); ccp opt_buf[OPT_INDEX_SIZE]; memset(opt_buf,0,sizeof(opt_buf)); int getopt_idx = OPT_LONG_BASE; fprintf(cf,"const struct option OptionLong[] =\n{\n"); var_ptr += snprintf(var_ptr,var_end-var_ptr, "extern const struct option OptionLong[];\n"); for ( info = ctrl->info; info < ctrl->end; info++ ) if ( info->type & T_DEF_OPT ) { ccp ptr = info->namelist; const int pmode = (info->type & F_OPT_OPTPARAM) ? 2 : (info->type & F_OPT_PARAM) ? 1 : 0; if ( info->namelist[1] == '|' ) { snprintf(iobuf,sizeof(iobuf),"%d, 0, '%c'", pmode, info->namelist[0] ); ptr += 2; opt_buf[(u8)(info->namelist[0])] = info->c_name; } else { snprintf(iobuf,sizeof(iobuf),"%d, 0, GO_%s", pmode, info->c_name ); ASSERT_MSG( getopt_idx < OPT_INDEX_SIZE, "getopt_idx[%x] >= OPT_INDEX_SIZE[%x]\n", getopt_idx, OPT_INDEX_SIZE ); opt_buf[(u8)(getopt_idx++)] = info->c_name; } int indent = 0; while (*ptr) { ccp start = ptr; while ( *ptr && *ptr != '|' ) ptr++; const int len = ptr - start; fprintf(cf,"\t%s{ \"%.*s\",%.*s%s },\n", indent ? " " : "", len, start, (26-len-indent)/8, tabs, iobuf ); if (*ptr) ptr++; indent = 1; } } fprintf(cf,"\n\t{0,0,0,0}\n};\n"); //----- print enumGetOpt print_section(hf,sep1,"enumGetOpt"); fprintf(hf,"typedef enum enumGetOpt\n{"); // add '?' temporary; ASSERT(!opt_buf['?']); opt_buf['?'] = "_ERR"; static const int septab[] = { 0, '0', '9'+1, '?', '?'+1, 'A', 'Z'+1, 'a', 'z'+1, OPT_LONG_BASE, OPT_INDEX_SIZE }; const int * sepptr = septab; int i; for ( i = 0; i < OPT_INDEX_SIZE; i++ ) if ( opt_buf[i] ) { if ( i >= *sepptr ) { fputc('\n',hf); while ( i >= *sepptr ) sepptr++; } if ( i < OPT_LONG_BASE ) fprintf(hf,"\tGO_%s%.*s= '%c',\n", opt_buf[i], (28-(int)strlen(opt_buf[i]))/8, tabs, i ); else if ( i == OPT_LONG_BASE ) fprintf(hf,"\tGO_%s%.*s= 0x%02x,\n", opt_buf[i], (28-(int)strlen(opt_buf[i]))/8, tabs, i ); else fprintf(hf,"\tGO_%s,\n",opt_buf[i]); } fprintf(hf,"\n} enumGetOpt;\n"); opt_buf['?'] = 0; //----- print option index print_section(cf,sep1,"OptionUsed & OptionIndex"); fprintf(cf,"u8 OptionUsed[OPT__N_TOTAL+1] = {0};\n\n"); var_ptr += snprintf(var_ptr,var_end-var_ptr, "extern u8 OptionUsed[OPT__N_TOTAL+1];\n"); fprintf(cf,"const u8 OptionIndex[OPT_INDEX_SIZE] = \n{\n"); var_ptr += snprintf(var_ptr,var_end-var_ptr, "extern const u8 OptionIndex[OPT_INDEX_SIZE];\n"); for ( i = 0; i < OPT_INDEX_SIZE; ) { int start = i; while ( i < OPT_INDEX_SIZE && !opt_buf[i] ) i++; int len = i - start; while ( len > 0 ) { const int now_len = len < 16 ? len : 16 - start % 16; fprintf(cf,"\t/* 0x%02x */\t %.*s\n", start, 2*now_len + now_len/4, "0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0," ); start += now_len; len -= now_len; } while ( i < OPT_INDEX_SIZE && opt_buf[i] ) { fprintf(cf,"\t/* 0x%02x %c */\tOPT_%s,\n", i, i > ' ' && i < 0x7f ? i : ' ', opt_buf[i]); i++; } } fprintf(cf,"};\n"); //----- option allowed if (ctrl->opt_allow_cmd) { print_section(cf,sep1,"opt_allowed_cmd_*"); for ( info = ctrl->info; info < ctrl->end; info++ ) { if ( !( info->type & T_DEF_CMD ) ) continue; fprintf(cf,"static u8 option_allowed_cmd_%s[%u] = // cmd #%u\n{", info->c_name, ctrl->n_opt_specific, info->index ); int i; u8 * ptr = ctrl->opt_allow_cmd + info->index * ctrl->n_opt_specific; for ( i = 0; i < ctrl->n_opt_specific; i++ ) fprintf(cf, "%s%u%s", !(i%30) ? "\n " : !(i%10) ? " " : !(i%5) ? " " : "", ptr[i], i < ctrl->n_opt_specific-1 ? "," : "" ); fprintf(cf,"\n};\n\n"); } } //----- InfoCommand print_links(ctrl); var_ptr += snprintf(var_ptr,var_end-var_ptr, "extern const InfoCommand_t CommandInfo[CMD__N+1];\n"); //----- InfoUI print_section(cf,sep1,"InfoUI"); var_ptr += snprintf(var_ptr,var_end-var_ptr, "extern const InfoUI_t InfoUI;\n"); fprintf(cf, "const InfoUI_t InfoUI =\n" "{\n" "\t\"%s\",\n" // tool_name "\t%s\n" // n_cmd "\t%s\n" // cmd_tab "\tCommandInfo,\n" // cmd_info "\t%s\n" // n_opt_specific "\tOPT__N_TOTAL,\n" // n_opt_total "\tOptionInfo,\n" // opt_info "\tOptionUsed,\n" // opt_used "\tOptionIndex,\n" // opt_index "\tOptionShort,\n" // opt_short "\tOptionLong\n" // opt_long "};\n", ctrl->info->c_name, ctrl->n_cmd ? "CMD__N," : "0, // n_cmd", ctrl->n_cmd ? "CommandTab," : "0, // cmd_tab", ctrl->n_cmd ? "OPT__N_SPECIFIC," : "0, // n_opt_specific" ); //----- external vars print_section(hf,sep1,"external vars"); fputs(var_buf,hf); //----- terminate print_section(cf,sep1,"END"); print_section(hf,sep1,"END"); fprintf(hf,"#endif // %s\n\n",guard); return ERR_OK; };
enumError ReadWDF ( SuperFile_t * sf, off_t off, void * buf, size_t count ) { ASSERT(sf); ASSERT(sf->wc); ASSERT(sf->wc_used); TRACE("#W# -----\n"); TRACE(TRACE_RDWR_FORMAT, "#W# ReadWDF()", GetFD(&sf->f), GetFP(&sf->f), (u64)off, (u64)off+count, count, "" ); if ( off + count > sf->wh.file_size ) { if (!sf->f.read_behind_eof) { if ( !sf->f.disable_errors ) ERROR0( ERR_READ_FAILED, "Read behind eof [%c,%llx+%zx]: %s\n", sf->f.fp ? 'S' : sf->f.fd != -1 ? 'F' : '-', (u64)off, count, sf->f.fname ); return ERR_READ_FAILED; } const off_t max_read = sf->wh.file_size > off ? sf->wh.file_size - off : 0; ASSERT( count > max_read ); if ( sf->f.read_behind_eof == 1 ) { sf->f.read_behind_eof = 2; if ( !sf->f.disable_errors ) ERROR0( ERR_WARNING, "Read behind eof -> zero filled [%c,%llx+%zx]: %s\n", sf->f.fp ? 'S' : sf->f.fd != -1 ? 'F' : '-', (u64)off, count, sf->f.fname ); } size_t fill_count = count - (size_t)max_read; count = (size_t)max_read; memset((char*)buf+count,0,fill_count); if (!count) return ERR_OK; } // find chunk header WDF_Chunk_t * wc = sf->wc; const int used_m1 = sf->wc_used - 1; int beg = 0, end = used_m1; ASSERT( beg <= end ); while ( beg < end ) { int idx = (beg+end)/2; wc = sf->wc + idx; if ( off < wc->file_pos ) end = idx-1; else if ( idx < used_m1 && off >= wc[1].file_pos ) beg = idx + 1; else beg = end = idx; } wc = sf->wc + beg; noTRACE("#W# - FOUND #%03d: off=%09llx ds=%llx, off=%09llx\n", beg, wc->file_pos, wc->data_size, (u64)off ); ASSERT( off >= wc->file_pos ); ASSERT( beg == used_m1 || off < wc[1].file_pos ); char * dest = buf; while ( count > 0 ) { noTRACE("#W# %d/%d count=%zd off=%llx,%llx \n", beg, sf->wc_used, count, (u64)off, (u64)wc->file_pos ); if ( off < wc->file_pos ) { const u64 max_size = wc->file_pos - off; const u32 fill_size = max_size < count ? (u32)max_size : count; TRACE("#W# >FILL %p +%8zx = %p .. %x\n", buf, dest-(ccp)buf, dest, fill_size ); memset(dest,0,fill_size); count -= fill_size; off += fill_size; dest += fill_size; if (!count) break; } if ( off >= wc->file_pos && off < wc->file_pos + wc->data_size ) { // we want a part of this const u64 delta = off - wc->file_pos; const u64 max_size = wc->data_size - delta; const u32 read_size = max_size < count ? (u32)max_size : count; TRACE("#W# >READ %p +%8zx = %p .. %x <- %10llx\n", buf, dest-(ccp)buf, dest, read_size, wc->data_off+delta ); int stat = ReadAtF(&sf->f,wc->data_off+delta,dest,read_size); if (stat) return stat; count -= read_size; off += read_size; dest += read_size; if (!count) break; } wc++; if ( ++beg >= sf->wc_used ) { TRACE("ERR_WDF_INVALID\n"); return ERR_WDF_INVALID; } } TRACE("#W# - done, dest = %p\n",dest); return ERR_OK; }