void FiltersDialog::on_button_try() { // Iterators. GtkTextIter startiter, enditer; // Input text. GtkTextBuffer *inputbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview_input)); gtk_text_buffer_get_start_iter(inputbuffer, &startiter); gtk_text_buffer_get_end_iter(inputbuffer, &enditer); ustring inputfile = script_temporal_input_file(); gchar *txt = gtk_text_buffer_get_text(inputbuffer, &startiter, &enditer, false); g_file_set_contents(inputfile.c_str(), txt, -1, NULL); g_free(txt); // Postiff: plug memory leak // Filter. ustring scriptname = combobox_get_active_string(combobox_filters); bool straightthrough = scriptname == scripts_straight_through(); // Output file. ustring outputfile = script_temporal_output_file(); // Run filter. ustring error = script_filter(scriptname, straightthrough, inputfile, outputfile); // Show output in textview. gchar *outputtext; g_file_get_contents(outputfile.c_str(), &outputtext, NULL, NULL); GtkTextBuffer *outputbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview_output)); if (outputtext) { gtk_text_buffer_set_text(outputbuffer, outputtext, -1); g_free(outputtext); } else { gtk_text_buffer_set_text(outputbuffer, "", -1); } // If script failed, give the error for debugging purposes. if (!error.empty()) { gtk_text_buffer_set_text(outputbuffer, error.c_str(), -1); } // If there were compile errors before, show these. if (!compile_errors.empty()) { gtk_text_buffer_set_text(outputbuffer, "", -1); for (unsigned int i = 0; i < compile_errors.size(); i++) { gtk_text_buffer_insert_at_cursor(outputbuffer, compile_errors[i].c_str(), -1); gtk_text_buffer_insert_at_cursor(outputbuffer, "\n", -1); } } }
ustring script_filter(const ustring & scriptname, bool straightthrough, const ustring & inputfile, const ustring & outputfile) /* Runs the filter "scriptname". Input text in "inputfile", and the output text goes in "outputfile". If everything is okay, it returns nothing. If there were errors, it returns these. */ { // Remove any previous output. unlink(outputfile.c_str()); unlink(script_temporal_error_file().c_str()); // Handle straight through. if (straightthrough) { unix_cp(inputfile, outputfile); return ""; } // Get the filename and the type of the script. ScriptType scripttype; ustring scriptfile = script_get_path(scriptname, &scripttype, true); // If the rules file does not exist, or the script is of an unknown type, pass it straight through. if (!g_file_test(scriptfile.c_str(), G_FILE_TEST_IS_REGULAR) || (scripttype == stEnd)) { unix_cp(inputfile, outputfile); gw_warning(_("Error in script ") + scriptname); return ""; } // Encode the input usfm file. ustring encodedinputfile = script_temporal_input_file(); if (inputfile != encodedinputfile) { unix_cp(inputfile, encodedinputfile); } script_encode_usfm_file(encodedinputfile); // Run filter. ustring command; ustring error; switch (scripttype) { case stSed: { command.append(script_sed_binary()); command.append(" -f"); command.append(shell_quote_space(scriptfile)); command.append("<"); command.append(shell_quote_space(encodedinputfile)); command.append(">"); command.append(shell_quote_space(outputfile)); break; } case stTECkit: { command.append(script_teckit_txtconverter()); command.append(" -i"); command.append(shell_quote_space(encodedinputfile)); command.append(" -o"); command.append(shell_quote_space(outputfile)); command.append(" -t"); command.append(shell_quote_space(scriptfile)); command.append(" -nobom"); break; } case stFree: { // Text of the script. ustring scriptdata; { // Read script. gchar *contents; g_file_get_contents(scriptfile.c_str(), &contents, NULL, NULL); if (contents) { scriptdata = contents; g_free(contents); } else { error = _("Can't read script file"); gw_warning(error); return error; } } // Check for and insert the input filename. if (scriptdata.find(script_free_input_identifier()) == string::npos) { error = _("Can't find where to put input file"); gw_warning(error); return error; } replace_text(scriptdata, script_free_input_identifier(), shell_quote_space(encodedinputfile)); // Check for and insert the output filename. if (scriptdata.find(script_free_output_identifier()) == string::npos) { error = _("Can't find where to put output file"); gw_warning(error); return error; } replace_text(scriptdata, script_free_output_identifier(), shell_quote_space(outputfile)); // Write temporal script. g_file_set_contents(script_temporal_script_file().c_str(), scriptdata.c_str(), -1, NULL); // Assemble command to run. command.append("sh"); command.append(shell_quote_space(script_temporal_script_file())); break; } case stEnd: { break; } } // Add the error file to the command, and run it. command.append(" 2> "); command.append(script_temporal_error_file()); int result = system(command.c_str()); // This one is too unpredictable to be used with GwSpawn. // The filters are so much beyond any control that we never can be sure that // their output is in the UTF-8 encoding. // Sed would give UTF-8, but as TECkit can also give legacy encodings. // We can't know what free scripts will do, it could be anything. // So here check the UTF-8 encoding. // If UTF-8 validation fails, we copy the input straight to the output. { gchar *contents; g_file_get_contents(outputfile.c_str(), &contents, NULL, NULL); if (contents) { if (!g_utf8_validate(contents, -1, NULL)) { unix_cp(inputfile, outputfile); error = _("UTF-8 validation failure"); gw_warning(error); } g_free(contents); if (!error.empty()) return error; } } // Decode the output file. script_decode_usfm_file(outputfile); // Handle OK. if (result == 0) return ""; // Handle error. gchar *contents; g_file_get_contents(script_temporal_error_file().c_str(), &contents, NULL, NULL); if (contents) { error = contents; g_free(contents); gw_warning(error); } return error; }