/* parse into a temporary file */ int wpp_parse_temp( const char *input, const char *output_base, char **output_name ) { FILE *output; int ret, fd; char *temp_name; if (!output_base || !output_base[0]) output_base = "wpptmp"; temp_name = pp_xmalloc( strlen(output_base) + 8 ); strcpy( temp_name, output_base ); strcat( temp_name, ".XXXXXX" ); if((fd = mkstemps( temp_name, 0 )) == -1) { fprintf(stderr, "Could not generate a temp name from %s\n", temp_name); exit(2); } if (!(output = fdopen(fd, "wt"))) { fprintf(stderr,"Could not open fd %s for writing\n", temp_name); exit(2); } *output_name = temp_name; ret = wpp_parse( input, output ); fclose( output ); return ret; }
int main(int argc,char *argv[]) { extern char* optarg; extern int optind; int optc; int opti = 0; int stdinc = 1; int lose = 0; int ret; int i; int cmdlen; signal(SIGSEGV, segvhandler); signal( SIGTERM, exit_on_signal ); signal( SIGINT, exit_on_signal ); #ifdef SIGHUP signal( SIGHUP, exit_on_signal ); #endif now = time(NULL); /* Set the default defined stuff */ set_version_defines(); wpp_add_cmdline_define("RC_INVOKED=1"); wpp_add_cmdline_define("__WIN32__=1"); wpp_add_cmdline_define("__FLAT__=1"); /* Microsoft RC always searches current directory */ wpp_add_include_path("."); /* First rebuild the commandline to put in destination */ /* Could be done through env[], but not all OS-es support it */ cmdlen = 4; /* for "wrc " */ for(i = 1; i < argc; i++) cmdlen += strlen(argv[i]) + 1; cmdline = (char *)xmalloc(cmdlen); strcpy(cmdline, "wrc "); for(i = 1; i < argc; i++) { strcat(cmdline, argv[i]); if(i < argc-1) strcat(cmdline, " "); } while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF) { switch(optc) { case 1: stdinc = 0; break; case 2: if (debuglevel) warning("--use-temp-file option not yet supported, ignored.\n"); break; case 3: if (debuglevel) warning("--no-use-temp-file option not yet supported, ignored.\n"); break; case 4: if (strcmp(optarg, "cat") == 0) no_preprocess = 1; else fprintf(stderr, "-P option not yet supported, ignored.\n"); break; case 5: printf(version_string); exit(0); break; case 6: debuglevel = strtol(optarg, NULL, 0); break; case 7: switch(optarg[0]) { case 'n': case 'N': byteorder = WRC_BO_NATIVE; break; case 'l': case 'L': byteorder = WRC_BO_LITTLE; break; case 'b': case 'B': byteorder = WRC_BO_BIG; break; default: fprintf(stderr, "Byte ordering must be n[ative], l[ittle] or b[ig]\n"); lose++; } break; case 8: pedantic = 1; wpp_set_pedantic(1); break; case 9: verify_translations_mode = 1; break; case 'D': wpp_add_cmdline_define(optarg); break; case 'E': preprocess_only = 1; break; case 'F': /* ignored for compatibility with windres */ break; case 'h': printf(usage); exit(0); case 'i': if (!input_name) input_name = strdup(optarg); else error("Too many input files.\n"); break; case 'I': wpp_add_include_path(optarg); break; case 'J': if (strcmp(optarg, "rc16") == 0) extensions = 0; else if (strcmp(optarg, "rc")) error("Output format %s not supported.\n", optarg); break; case 'l': { int lan; lan = strtol(optarg, NULL, 0); if (get_language_codepage(PRIMARYLANGID(lan), SUBLANGID(lan)) == -1) error("Language %04x is not supported\n", lan); currentlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan)); } break; case 'f': if (*optarg != 'o') error("Unknown option: -f%s\n", optarg); optarg++; /* fall through */ case 'o': if (!output_name) output_name = strdup(optarg); else error("Too many output files.\n"); break; case 'O': if (strcmp(optarg, "res16") == 0) { win32 = 0; wpp_del_define("__WIN32__"); wpp_del_define("__FLAT__"); } else if (strcmp(optarg, "res")) warning("Output format %s not supported.\n", optarg); break; case 'r': /* ignored for compatibility with rc */ break; case 'U': wpp_del_define(optarg); break; case 'v': debuglevel = DEBUGLEVEL_CHAT; break; default: lose++; break; } } if(lose) { fprintf(stderr, usage); return 1; } /* If we do need to search standard includes, add them to the path */ if (stdinc) { wpp_add_include_path(INCLUDEDIR"/msvcrt"); wpp_add_include_path(INCLUDEDIR"/windows"); } /* Check for input file on command-line */ if(optind < argc) { if (!input_name) input_name = argv[optind++]; else error("Too many input files.\n"); } /* Check for output file on command-line */ if(optind < argc) { if (!output_name) output_name = argv[optind++]; else error("Too many output files.\n"); } /* Kill io buffering when some kind of debuglevel is enabled */ if(debuglevel) { setbuf(stdout,0); setbuf(stderr,0); } yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0; yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0; wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0, (debuglevel & DEBUGLEVEL_PPTRACE) != 0, (debuglevel & DEBUGLEVEL_PPMSG) != 0 ); /* Check if the user set a language, else set default */ if(!currentlanguage) currentlanguage = new_language(0, 0); /* Generate appropriate outfile names */ if(!output_name && !preprocess_only) { output_name = dup_basename(input_name, ".rc"); strcat(output_name, ".res"); } atexit(cleanup_files); /* Run the preprocessor on the input */ if(!no_preprocess) { /* * Preprocess the input to a temp-file, or stdout if * no output was given. */ chat("Starting preprocess"); if (!preprocess_only) { ret = wpp_parse_temp( input_name, output_name, &temp_name ); } else if (output_name) { FILE *output; if (!(output = fopen( output_name, "w" ))) error( "Could not open %s for writing\n", output_name ); ret = wpp_parse( input_name, output ); fclose( output ); } else { ret = wpp_parse( input_name, stdout ); } if(ret) exit(1); /* Error during preprocess */ if(preprocess_only) { output_name = NULL; exit(0); } input_name = temp_name; } /* Go from .rc to .res */ chat("Starting parse"); if(!(yyin = fopen(input_name, "rb"))) error("Could not open %s for input\n", input_name); ret = yyparse(); if(input_name) fclose(yyin); if(ret) exit(1); /* Error during parse */ if(debuglevel & DEBUGLEVEL_DUMP) dump_resources(resource_top); if(verify_translations_mode) { verify_translations(resource_top); exit(0); } /* Convert the internal lists to binary data */ resources2res(resource_top); chat("Writing .res-file"); write_resfile(output_name, resource_top); output_name = NULL; return 0; }
/* load a single input file */ static int load_file( const char *input_name, const char *output_name ) { int ret; /* Run the preprocessor on the input */ if(!no_preprocess) { FILE *output; int ret, fd; char *name; /* * Preprocess the input to a temp-file, or stdout if * no output was given. */ if (preprocess_only) { if (output_name) { if (!(output = fopen( output_name, "w" ))) fatal_perror( "Could not open %s for writing", output_name ); ret = wpp_parse( input_name, output ); fclose( output ); } else ret = wpp_parse( input_name, stdout ); if (ret) return ret; output_name = NULL; exit(0); } if (output_name && output_name[0]) name = strmake( "%s.XXXXXX", output_name ); else name = xstrdup( "wrc.XXXXXX" ); if ((fd = mkstemps( name, 0 )) == -1) error("Could not generate a temp name from %s\n", name); temp_name = name; if (!(output = fdopen(fd, "wt"))) error("Could not open fd %s for writing\n", name); ret = wpp_parse( input_name, output ); fclose( output ); if (ret) return ret; input_name = name; } /* Reset the language */ currentlanguage = dup_language( defaultlanguage ); check_utf8 = 1; /* Go from .rc to .res */ chat("Starting parse\n"); if(!(parser_in = fopen(input_name, "rb"))) fatal_perror("Could not open %s for input", input_name); ret = parser_parse(); fclose(parser_in); parser_lex_destroy(); if (temp_name) { unlink( temp_name ); temp_name = NULL; } free( currentlanguage ); return ret; }
static HRESULT preprocess_shader(const void *data, SIZE_T data_size, const char *filename, const D3D_SHADER_MACRO *defines, ID3DInclude *include, ID3DBlob **error_messages) { int ret; HRESULT hr = S_OK; const D3D_SHADER_MACRO *def = defines; static const struct wpp_callbacks wpp_callbacks = { wpp_lookup_mem, wpp_open_mem, wpp_close_mem, wpp_read_mem, wpp_write_mem, wpp_error, wpp_warning, }; if (def != NULL) { while (def->Name != NULL) { wpp_add_define(def->Name, def->Definition); def++; } } current_include = include; includes_size = 0; wpp_output_size = wpp_output_capacity = 0; wpp_output = NULL; wpp_set_callbacks(&wpp_callbacks); wpp_messages_size = wpp_messages_capacity = 0; wpp_messages = NULL; current_shader.buffer = data; current_shader.size = data_size; initial_filename = filename ? filename : ""; ret = wpp_parse(initial_filename, NULL); if (!wpp_close_output()) ret = 1; if (ret) { TRACE("Error during shader preprocessing\n"); if (wpp_messages) { int size; ID3DBlob *buffer; TRACE("Preprocessor messages:\n%s\n", debugstr_a(wpp_messages)); if (error_messages) { size = strlen(wpp_messages) + 1; hr = D3DCreateBlob(size, &buffer); if (FAILED(hr)) goto cleanup; CopyMemory(ID3D10Blob_GetBufferPointer(buffer), wpp_messages, size); *error_messages = buffer; } } if (data) TRACE("Shader source:\n%s\n", debugstr_an(data, data_size)); hr = E_FAIL; } cleanup: /* Remove the previously added defines */ if (defines != NULL) { while (defines->Name != NULL) { wpp_del_define(defines->Name); defines++; } } HeapFree(GetProcessHeap(), 0, wpp_messages); return hr; }