| 1 | /* |
| 2 | Editor unescape reencode and insert string |
| 3 | |
| 4 | Copyright (C) 2013 |
| 5 | The Free Software Foundation, Inc. |
| 6 | |
| 7 | Written by: |
| 8 | Gergely Szasz <szaszg@hu.inter.net> 2013 |
| 9 | |
| 10 | This file is part of the Midnight Commander. |
| 11 | |
| 12 | The Midnight Commander is free software: you can redistribute it |
| 13 | and/or modify it under the terms of the GNU General Public License as |
| 14 | published by the Free Software Foundation, either version 3 of the License, |
| 15 | or (at your option) any later version. |
| 16 | |
| 17 | The Midnight Commander is distributed in the hope that it will be useful, |
| 18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | GNU General Public License for more details. |
| 21 | |
| 22 | You should have received a copy of the GNU General Public License |
| 23 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | /** \file |
| 27 | * \brief Source: editor unescape reencode and insert string |
| 28 | * \author Gergely Szasz |
| 29 | * \date 2013 |
| 30 | */ |
| 31 | |
| 32 | |
| 33 | #include <config.h> |
| 34 | |
| 35 | #ifdef HAVE_ASSERT_H |
| 36 | #include <assert.h> |
| 37 | #endif |
| 38 | #include <ctype.h> |
| 39 | |
| 40 | #include <stdio.h> |
| 41 | #include <stdarg.h> |
| 42 | #include <sys/types.h> |
| 43 | #include <unistd.h> |
| 44 | #include <string.h> |
| 45 | #include <errno.h> |
| 46 | #include <sys/stat.h> |
| 47 | #include <stdlib.h> |
| 48 | #include <fcntl.h> |
| 49 | |
| 50 | #include "lib/global.h" |
| 51 | #include "lib/tty/tty.h" |
| 52 | #include "lib/tty/key.h" /* XCTRL */ |
| 53 | #include "lib/mcconfig.h" |
| 54 | #include "lib/skin.h" |
| 55 | #include "lib/strutil.h" /* utf string functions */ |
| 56 | #include "lib/lock.h" |
| 57 | #include "lib/util.h" /* tilde_expand() */ |
| 58 | #include "lib/vfs/vfs.h" |
| 59 | #include "lib/widget.h" |
| 60 | #include "lib/event.h" /* mc_event_raise() */ |
| 61 | #ifdef HAVE_CHARSET |
| 62 | #include "lib/charsets.h" |
| 63 | #endif |
| 64 | |
| 65 | #include "src/history.h" |
| 66 | #include "src/setup.h" /* option_tab_spacing */ |
| 67 | #ifdef HAVE_CHARSET |
| 68 | #include "src/selcodepage.h" |
| 69 | #endif |
| 70 | #include "src/keybind-defaults.h" |
| 71 | #include "src/util.h" /* check_for_default() */ |
| 72 | #include "src/filemanager/layout.h" /* mc_refresh() */ |
| 73 | |
| 74 | #include "edit-impl.h" |
| 75 | #include "editwidget.h" |
| 76 | #include "editcmd_dialogs.h" |
| 77 | #ifdef HAVE_ASPELL |
| 78 | #include "spell.h" |
| 79 | #include "spell_dialogs.h" |
| 80 | #endif |
| 81 | #include "etags.h" |
| 82 | #include "etags.h" |
| 83 | |
| 84 | /*** global variables ****************************************************************************/ |
| 85 | |
| 86 | |
| 87 | /*** file scope macro definitions ****************************************************************/ |
| 88 | |
| 89 | #define UNUSED_ARG(x) {(void)(x);} |
| 90 | |
| 91 | #define RECODE_LABEL 6 |
| 92 | |
| 93 | #define TEMP_BUF_LEN 1024 |
| 94 | |
| 95 | #define B_FILE (B_USER + 1) |
| 96 | #define B_MARK (B_USER + 2) |
| 97 | |
| 98 | /*** file scope type declarations ****************************************************************/ |
| 99 | |
| 100 | typedef struct |
| 101 | { |
| 102 | const char *name; |
| 103 | gint32 u; |
| 104 | } html_enc_t; |
| 105 | |
| 106 | /*** file scope variables ************************************************************************/ |
| 107 | |
| 108 | /* include html entities table for edit_insert_estring () */ |
| 109 | #include "html_enc_table.h" |
| 110 | |
| 111 | /* --------------------------------------------------------------------------------------------- */ |
| 112 | /*** file scope functions ************************************************************************/ |
| 113 | /* --------------------------------------------------------------------------------------------- */ |
| 114 | |
| 115 | static void |
| 116 | edit_utf16recode (GIConv cnv, guint32 u, GString **out, int *show_warn, int curr_index) |
| 117 | { |
| 118 | char str[4]; |
| 119 | #ifdef HAVE_CHARSET |
| 120 | gchar *ostr; |
| 121 | gsize olen; |
| 122 | GError *err = NULL; |
| 123 | |
| 124 | str[0] = u & 0xff; |
| 125 | str[1] = (u >> 8) & 0xff; |
| 126 | str[2] = (u >> 16) & 0xff; |
| 127 | str[3] = (u >> 24) & 0xff; |
| 128 | ostr = g_convert_with_iconv (str, 4, cnv, NULL, &olen, &err); |
| 129 | if (*show_warn && (err != NULL || ostr == NULL)) |
| 130 | { |
| 131 | char *tmp; |
| 132 | tmp = g_strdup_printf (_("Error when reencoding string at %u:\n%s"), curr_index, |
| 133 | err == NULL ? "No result from conversion!" : err->message); |
| 134 | if (query_dialog (_("Warning"), tmp, D_ERROR, 2, _("&Dismiss"), _("Dismiss &All")) == 1) |
| 135 | *show_warn = 0; |
| 136 | g_free (tmp); |
| 137 | } |
| 138 | if (ostr != NULL) |
| 139 | { |
| 140 | *out = g_string_append (*out, ostr); |
| 141 | g_free (ostr); |
| 142 | } |
| 143 | #else |
| 144 | UNUSED_ARG (cnv); |
| 145 | UNUSED_ARG (show_warn); |
| 146 | /* convert UTF-16 to UTF-8 */ |
| 147 | if (u <= 0x7f) |
| 148 | { |
| 149 | str[0] = u; |
| 150 | str[1] = '\0'; |
| 151 | } |
| 152 | else if (u > 0x07FF) |
| 153 | { |
| 154 | str[0] = 0xe0 | ((u >> 12) & 0x0f); |
| 155 | str[1] = 0x80 | ((u >> 6) & 0x3f); |
| 156 | str[2] = 0x80 | (u & 0x3f); |
| 157 | str[3] = '\0'; |
| 158 | } else { |
| 159 | str[0] = 0xc0 | ((u >> 6) & 0x1f); |
| 160 | str[1] = 0x80 | ((u >> 0) & 0x3f); |
| 161 | str[2] = '\0'; |
| 162 | } |
| 163 | *out = g_string_append (*out, str); |
| 164 | #endif /* HAVE_CHARSET */ |
| 165 | } |
| 166 | |
| 167 | /* --------------------------------------------------------------------------------------------- */ |
| 168 | /*** public functions ****************************************************************************/ |
| 169 | /* --------------------------------------------------------------------------------------------- */ |
| 170 | |
| 171 | void |
| 172 | edit_insert_estring_cmd (WEdit * edit) |
| 173 | { |
| 174 | char *string; |
| 175 | static int esc_style = ESC_C; |
| 176 | static int show_warn = 1; |
| 177 | static int accept_u = 1; |
| 178 | static int accept_i = 1; |
| 179 | const char *esc_names[ESC_NAMES] = { |
| 180 | N_("&C-style"), |
| 181 | N_("&HTML-style"), |
| 182 | N_("&URL-style"), |
| 183 | N_("He&x-string"), |
| 184 | N_("&Numpad-style") |
| 185 | }; |
| 186 | int ret; |
| 187 | int mark = 0; |
| 188 | #ifdef HAVE_CHARSET |
| 189 | GString *str_recode = NULL; |
| 190 | const char *str_recode_tpl = N_("Recode: %s%s%s"); |
| 191 | static int cp_id = -1; |
| 192 | int cp_to, recode; |
| 193 | #endif /* HAVE_CHARSET */ |
| 194 | |
| 195 | quick_widget_t quick_widgets[] = { |
| 196 | /* *INDENT-OFF* */ |
| 197 | QUICK_LABELED_INPUT (N_("Enter string:"), input_label_above, INPUT_LAST_TEXT, |
| 198 | "insert-escaped-string", &string, NULL, FALSE, FALSE, |
| 199 | INPUT_COMPLETE_NONE), |
| 200 | // QUICK_BUTTON (N_("Insert from &File..."), B_FILE, NULL, NULL), |
| 201 | QUICK_SEPARATOR (TRUE), |
| 202 | QUICK_START_COLUMNS, |
| 203 | #ifdef HAVE_CHARSET |
| 204 | QUICK_BUTTON (N_("&Encoding..."), B_USER, NULL, NULL), |
| 205 | #else |
| 206 | QUICK_SEPARATOR (FALSE), |
| 207 | #endif /* HAVE_CHARSET */ |
| 208 | QUICK_RADIO (ESC_NAMES, esc_names, (int *) &esc_style, NULL), |
| 209 | QUICK_NEXT_COLUMN, |
| 210 | QUICK_LABEL(NULL, NULL), |
| 211 | QUICK_SEPARATOR (FALSE), |
| 212 | QUICK_CHECKBOX (N_("&Accept unicode"), &accept_u, NULL), |
| 213 | QUICK_CHECKBOX (N_("&Insert invalid chars"), &accept_i, NULL), |
| 214 | QUICK_CHECKBOX (N_("&Show warnings"), &show_warn, NULL), |
| 215 | QUICK_STOP_COLUMNS, |
| 216 | QUICK_START_BUTTONS (TRUE, TRUE), \ |
| 217 | QUICK_BUTTON (N_("&OK"), B_ENTER, NULL, NULL), \ |
| 218 | QUICK_BUTTON (N_("&From File"), B_FILE, NULL, NULL), |
| 219 | QUICK_BUTTON (N_("&Marked"), B_MARK, NULL, NULL), |
| 220 | QUICK_BUTTON (N_("&Cancel"), B_CANCEL, NULL, NULL), |
| 221 | QUICK_END |
| 222 | /* *INDENT-ON* */ |
| 223 | }; |
| 224 | |
| 225 | quick_dialog_t qdlg = { |
| 226 | -1, -1, 64, |
| 227 | N_("Unescape, reencode and insert string"), "[Internal File Editor / estring]", |
| 228 | quick_widgets, NULL, NULL |
| 229 | }; |
| 230 | |
| 231 | #ifdef HAVE_CHARSET |
| 232 | cp_to = mc_global.source_codepage >= |
| 233 | 0 ? mc_global.source_codepage : mc_global.display_codepage; |
| 234 | cp_id = cp_id == -1 ? cp_to : cp_id; |
| 235 | recode = (cp_id != cp_to); |
| 236 | str_recode = g_string_new (""); |
| 237 | g_string_printf (str_recode, str_recode_tpl, |
| 238 | recode ? get_codepage_id (cp_id) : "", |
| 239 | recode ? " to " : "-", |
| 240 | recode ? get_codepage_id (cp_to) : ""); |
| 241 | quick_widgets[RECODE_LABEL].u.label.text = str_recode->str; |
| 242 | #endif |
| 243 | while ((ret = quick_dialog (&qdlg)) != B_CANCEL) |
| 244 | { |
| 245 | FILE *finp = NULL; |
| 246 | int fdinp = -1, fdout = -1; |
| 247 | int insert_from_file = 0; |
| 248 | vfs_path_t *temp_vpath; |
| 249 | char *file_buff = NULL; |
| 250 | gsize len = 0; |
| 251 | GIConv cnvu = INVALID_CONV; |
| 252 | #ifdef HAVE_CHARSET |
| 253 | int lcp_id = -1; |
| 254 | #endif |
| 255 | /* feed from marked */ |
| 256 | /* feed from a file */ |
| 257 | if (ret == B_FILE || ret == B_MARK) |
| 258 | { |
| 259 | |
| 260 | if (insert_from_file == 0) /* Initialize file input */ |
| 261 | { |
| 262 | char *p; |
| 263 | char *tmp; |
| 264 | char *exp = NULL; |
| 265 | vfs_path_t *file_vpath; |
| 266 | |
| 267 | g_free (string); |
| 268 | |
| 269 | if (ret != B_MARK) |
| 270 | { |
| 271 | tmp = mc_config_get_full_path (EDIT_CLIP_FILE); |
| 272 | exp = input_expand_dialog (_("Insert from file"), _("Enter file name:"), |
| 273 | MC_HISTORY_EDIT_INSERT_FILE, tmp, INPUT_COMPLETE_FILENAMES); |
| 274 | g_free (tmp); |
| 275 | if (exp == NULL || *exp == '\0') |
| 276 | break; |
| 277 | } |
| 278 | |
| 279 | fdout = mc_mkstemps (&temp_vpath, "estring", NULL); |
| 280 | if (fdout <= 0) |
| 281 | { |
| 282 | edit_error_dialog (_("Error"), _("Cannot create temporary file!")); |
| 283 | g_free (exp); |
| 284 | break; |
| 285 | } |
| 286 | if (ret == B_MARK) |
| 287 | { |
| 288 | int fdclip; |
| 289 | off_t start_mark, end_mark; |
| 290 | |
| 291 | if (!eval_marks (edit, &start_mark, &end_mark)) |
| 292 | { |
| 293 | int rv; |
| 294 | |
| 295 | rv = edit_query_dialog3 (_("Warning"), |
| 296 | _("There is no marked block.\nProcess whole file or current line?"), |
| 297 | _("&Whole file"), _("Current &Line"), _("&Cancel")); |
| 298 | switch (rv) |
| 299 | { |
| 300 | case 0: |
| 301 | edit_set_markers (edit, 0, edit->buffer.size, 0, 0); |
| 302 | eval_marks (edit, &start_mark, &end_mark); |
| 303 | mark = 2; /* later we have to reset highlite ?*/ |
| 304 | break; |
| 305 | case 1: |
| 306 | edit->mark1 = edit_buffer_get_current_bol (&edit->buffer); |
| 307 | edit->mark2 = edit_buffer_get_current_eol (&edit->buffer); |
| 308 | eval_marks (edit, &start_mark, &end_mark); |
| 309 | break; |
| 310 | default: |
| 311 | mark = -1; |
| 312 | break; |
| 313 | } |
| 314 | if (mark == -1) |
| 315 | break; |
| 316 | } |
| 317 | fdclip = mc_mkstemps (&file_vpath, "estring", NULL); |
| 318 | if (fdclip < 0) |
| 319 | { |
| 320 | edit_error_dialog (_("Save block"), get_sys_error (_("Cannot create temporary file"))); |
| 321 | break; |
| 322 | } |
| 323 | close (fdclip); |
| 324 | // exp = vfs_path_to_str (file_vpath); |
| 325 | exp = vfs_path_to_str_flags (file_vpath, 0, VPF_STRIP_PASSWORD); |
| 326 | if (!edit_save_block (edit, exp, start_mark, end_mark)) |
| 327 | { |
| 328 | edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file"))); |
| 329 | break; |
| 330 | } |
| 331 | mark = 1; |
| 332 | } |
| 333 | else |
| 334 | { |
| 335 | file_vpath = vfs_path_from_str (exp); |
| 336 | } |
| 337 | p = edit_get_filter (file_vpath); |
| 338 | if (p != NULL) |
| 339 | { |
| 340 | if (mark) |
| 341 | mc_unlink (file_vpath); |
| 342 | vfs_path_free (file_vpath); |
| 343 | finp = (FILE *) popen (p, "r"); |
| 344 | if (finp == NULL) |
| 345 | { |
| 346 | close (fdout); |
| 347 | edit_error_dialog (_("Open filter"), get_sys_error (_("Cannot open file filter"))); |
| 348 | g_free (exp); |
| 349 | break; |
| 350 | } |
| 351 | } |
| 352 | else |
| 353 | { |
| 354 | fdinp = mc_open (file_vpath, O_RDONLY | O_BINARY); |
| 355 | if (mark) |
| 356 | mc_unlink (file_vpath); |
| 357 | vfs_path_free (file_vpath); |
| 358 | if (fdinp == -1) |
| 359 | { |
| 360 | char *errmsg; |
| 361 | |
| 362 | close (fdout); |
| 363 | errmsg = g_strdup_printf (_("Cannot open file for reading: %s"), exp); |
| 364 | edit_error_dialog (_("Error"), errmsg); |
| 365 | g_free (errmsg); |
| 366 | g_free (exp); |
| 367 | break; |
| 368 | } |
| 369 | } |
| 370 | g_free (exp); |
| 371 | file_buff = g_malloc (TEMP_BUF_LEN); |
| 372 | string = file_buff; |
| 373 | ret = B_ENTER; /* convert string */ |
| 374 | insert_from_file = 1; |
| 375 | } |
| 376 | continue_file: |
| 377 | /* read next string */ |
| 378 | if (insert_from_file == 1) |
| 379 | { |
| 380 | size_t blocklen; |
| 381 | if (finp != NULL) |
| 382 | { |
| 383 | blocklen = fread (file_buff, 1, TEMP_BUF_LEN, finp); |
| 384 | if (blocklen == 0 && feof (finp)) |
| 385 | { |
| 386 | insert_from_file = 2; |
| 387 | if (pclose (finp)) |
| 388 | { |
| 389 | edit_error_dialog (_("Error"), get_sys_error (_("File filter error"))); |
| 390 | break; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | else |
| 395 | { |
| 396 | blocklen = mc_read (fdinp, file_buff, TEMP_BUF_LEN); |
| 397 | if (blocklen <= 0) |
| 398 | { |
| 399 | insert_from_file = 2; |
| 400 | mc_close (fdinp); |
| 401 | } |
| 402 | } |
| 403 | if (insert_from_file == 2) |
| 404 | { |
| 405 | close (fdout); |
| 406 | g_free (file_buff); |
| 407 | #ifdef HAVE_CHARSET |
| 408 | recode = 0; |
| 409 | #endif |
| 410 | len = 0; /* avoid recoding and string processing */ |
| 411 | if (mark && edit_block_delete_cmd (edit)) |
| 412 | { |
| 413 | edit_error_dialog (_("Error"), _("Cannot delete marked block")); |
| 414 | mc_unlink (temp_vpath); |
| 415 | vfs_path_free (temp_vpath); |
| 416 | break; |
| 417 | } |
| 418 | if (!edit_insert_file (edit, temp_vpath)) |
| 419 | { |
| 420 | edit_error_dialog (_("Error"), get_sys_error (mark ? |
| 421 | _("Cannot replace marked block") : _("Cannot insert file"))); |
| 422 | mc_unlink (temp_vpath); |
| 423 | vfs_path_free (temp_vpath); |
| 424 | break; |
| 425 | } |
| 426 | mc_unlink (temp_vpath); |
| 427 | vfs_path_free (temp_vpath); |
| 428 | } |
| 429 | else if (blocklen > 0) |
| 430 | { |
| 431 | len = blocklen; |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | if (ret == B_USER) |
| 437 | { |
| 438 | #ifdef HAVE_CHARSET |
| 439 | int r; |
| 440 | r = select_charset (-1, -1, cp_id, FALSE); |
| 441 | if (r != SELECT_CHARSET_CANCEL) |
| 442 | { |
| 443 | cp_id = r == -1 ? cp_to : r; |
| 444 | recode = (cp_id != cp_to); |
| 445 | g_string_printf (str_recode, str_recode_tpl, |
| 446 | recode ? get_codepage_id (cp_id) : "", |
| 447 | recode ? " to " : "-", |
| 448 | recode ? get_codepage_id (cp_to) : ""); |
| 449 | quick_widgets[RECODE_LABEL].u.label.text = str_recode->str; |
| 450 | } |
| 451 | #endif /* HAVE_CHARSET */ |
| 452 | } |
| 453 | else if (ret == B_ENTER) |
| 454 | { |
| 455 | GString *out = g_string_new(""); |
| 456 | gsize curr_index; |
| 457 | int accept_u_and_conv = accept_u; |
| 458 | |
| 459 | #ifdef HAVE_CHARSET |
| 460 | /* converter for unicode escapes and HTML */ |
| 461 | if (accept_u || esc_style == ESC_NUMP || esc_style == ESC_HTML) |
| 462 | { |
| 463 | if (lcp_id != cp_id) |
| 464 | { |
| 465 | if (cnvu != INVALID_CONV) |
| 466 | g_iconv_close (cnvu); |
| 467 | cnvu = g_iconv_open (get_codepage_id (cp_id), "UTF-16"); |
| 468 | lcp_id = cp_id; |
| 469 | } |
| 470 | |
| 471 | if (accept_u_and_conv && cnvu == INVALID_CONV) |
| 472 | { |
| 473 | char *tmp; |
| 474 | tmp = g_strdup_printf (_("Cannot initialise conversion from:\n" |
| 475 | "UTF-16 to %s\nUTF-16 cannot accepted."), |
| 476 | get_codepage_id (cp_to)); |
| 477 | edit_error_dialog (_("Warning"), tmp); |
| 478 | g_free (tmp); |
| 479 | accept_u_and_conv = 0; |
| 480 | } |
| 481 | } |
| 482 | #endif |
| 483 | if (insert_from_file == 0) |
| 484 | len = strlen (string); |
| 485 | |
| 486 | for (curr_index = 0; curr_index < len; curr_index++) |
| 487 | { |
| 488 | switch (esc_style) |
| 489 | { |
| 490 | case ESC_C: |
| 491 | if (string[curr_index] != '\\') |
| 492 | { |
| 493 | out = g_string_append_c (out, string[curr_index]); |
| 494 | continue; |
| 495 | } |
| 496 | curr_index++; |
| 497 | if (curr_index == len) |
| 498 | continue; |
| 499 | switch (string[curr_index]) |
| 500 | { |
| 501 | case '\\': |
| 502 | out = g_string_append_c (out, '\\'); |
| 503 | continue; |
| 504 | case 'a': |
| 505 | out = g_string_append_c (out, '\a'); |
| 506 | continue; |
| 507 | case 'b': |
| 508 | out = g_string_append_c (out, '\b'); |
| 509 | continue; |
| 510 | case 't': |
| 511 | out = g_string_append_c (out, '\t'); |
| 512 | continue; |
| 513 | case 'n': |
| 514 | out = g_string_append_c (out, '\n'); |
| 515 | continue; |
| 516 | case 'v': |
| 517 | out = g_string_append_c (out, '\v'); |
| 518 | continue; |
| 519 | case 'f': |
| 520 | out = g_string_append_c (out, '\f'); |
| 521 | continue; |
| 522 | case 'r': |
| 523 | out = g_string_append_c (out, '\r'); |
| 524 | continue; |
| 525 | case 'e': |
| 526 | out = g_string_append_c (out, '\027'); |
| 527 | continue; |
| 528 | case 'x': |
| 529 | { |
| 530 | int i, c = -1; |
| 531 | for (i = 1; i <= 2; i++) |
| 532 | { |
| 533 | int x; |
| 534 | if (curr_index + 1 >= len) |
| 535 | { |
| 536 | if (c < 0) |
| 537 | out = g_string_append_c (out, 'x'); |
| 538 | continue; |
| 539 | } |
| 540 | x = string[++curr_index]; |
| 541 | if (x >= 'a' && x <= 'f') |
| 542 | x -= 'a' - 10; |
| 543 | else if (x >= 'A' && x <= 'F') |
| 544 | x -= 'A' - 10; |
| 545 | else if (x >= '0' && x <= '9') |
| 546 | x -= '0'; |
| 547 | else |
| 548 | { |
| 549 | if (c >= 0) |
| 550 | out = g_string_append_c (out, c); |
| 551 | else |
| 552 | out = g_string_append_c (out, 'x'); |
| 553 | c = -1; |
| 554 | curr_index--; |
| 555 | break; |
| 556 | } |
| 557 | c = c == -1 ? 0 : c << 4; |
| 558 | c |= x & 0x0f; |
| 559 | } |
| 560 | if (c >= 0) |
| 561 | out = g_string_append_c (out, c); |
| 562 | } |
| 563 | continue; |
| 564 | case '0': |
| 565 | case '1': |
| 566 | case '2': |
| 567 | case '3': |
| 568 | case '4': |
| 569 | case '5': |
| 570 | case '6': |
| 571 | case '7': |
| 572 | { |
| 573 | int i, c = -1; |
| 574 | |
| 575 | curr_index = curr_index - 1; |
| 576 | for (i = 1; i <= 3; i++) |
| 577 | { |
| 578 | int o; |
| 579 | if (curr_index + 1 >= len) |
| 580 | break; |
| 581 | o = string[++curr_index]; |
| 582 | if (o >= '0' && o <= '7') |
| 583 | o -= '0'; |
| 584 | else |
| 585 | { |
| 586 | out = g_string_append_c (out, c&0xff); |
| 587 | out = g_string_append_c (out, o); |
| 588 | c = -1; |
| 589 | break; |
| 590 | } |
| 591 | c = c == -1 ? 0 : c << 3; |
| 592 | c |= o & 0x0f; |
| 593 | } |
| 594 | if (c > 0xff && show_warn) |
| 595 | { |
| 596 | char *tmp; |
| 597 | |
| 598 | tmp = g_strdup_printf (_("Octal value out " |
| 599 | "of range (000-377) at %u: %03o\n" |
| 600 | "Value converted to: %03o"), |
| 601 | curr_index, c, c&0xff); |
| 602 | if (query_dialog (_("Warning"), tmp, D_ERROR, 2, _("&Dismiss"), _("Dismiss &All")) == 1) |
| 603 | show_warn = 0; |
| 604 | |
| 605 | g_free (tmp); |
| 606 | } |
| 607 | if (c >= 0) |
| 608 | out = g_string_append_c (out, c&0xff); |
| 609 | continue; |
| 610 | } |
| 611 | case 'u': |
| 612 | if (accept_u_and_conv) |
| 613 | { |
| 614 | gint32 u = -1; |
| 615 | int i; |
| 616 | for (i = 1; i <= 4; i++) |
| 617 | { |
| 618 | int x; |
| 619 | if (curr_index + 1 >= len) |
| 620 | { |
| 621 | if (u < 0) |
| 622 | out = g_string_append_c (out, 'u'); |
| 623 | break; |
| 624 | } |
| 625 | x = string[++curr_index]; |
| 626 | if (x >= 'a' && x <= 'f') |
| 627 | x -= 'a' - 10; |
| 628 | else if (x >= 'A' && x <= 'F') |
| 629 | x -= 'A' - 10; |
| 630 | else if (x >= '0' && x <= '9') |
| 631 | x -= '0'; |
| 632 | else |
| 633 | { |
| 634 | if (u < 0) |
| 635 | { |
| 636 | out = g_string_append_c (out, 'x'); |
| 637 | } |
| 638 | curr_index--; |
| 639 | break; |
| 640 | } |
| 641 | u = u == -1 ? 0 : u << 4; |
| 642 | u |= x & 0x0f; |
| 643 | } |
| 644 | if (u >= 0) |
| 645 | edit_utf16recode (cnvu, u, &out, &show_warn, curr_index); |
| 646 | } |
| 647 | continue; |
| 648 | default: |
| 649 | out = g_string_append_c (out, string[curr_index]); |
| 650 | continue; |
| 651 | } |
| 652 | break; |
| 653 | case ESC_HTML: |
| 654 | if (string[curr_index] != '&' || cnvu == INVALID_CONV) |
| 655 | { |
| 656 | out = g_string_append_c (out, string[curr_index]); |
| 657 | continue; |
| 658 | } |
| 659 | curr_index++; |
| 660 | if (curr_index == len) |
| 661 | { |
| 662 | out = g_string_append_c (out, '&'); |
| 663 | continue; |
| 664 | } |
| 665 | else if (string[curr_index] != '#') |
| 666 | { |
| 667 | gsize i; |
| 668 | int hlen; |
| 669 | |
| 670 | for (i = curr_index; i < len; i++) |
| 671 | if (string[i] == ';') |
| 672 | break; |
| 673 | if (string[i] != ';') |
| 674 | { |
| 675 | out = g_string_append_c (out, '&'); |
| 676 | curr_index--; |
| 677 | continue; |
| 678 | } |
| 679 | hlen = i - curr_index; |
| 680 | for (i = 0; html_enc_table[i].name != NULL; i++) |
| 681 | { |
| 682 | if (strncmp (html_enc_table[i].name, |
| 683 | &string[curr_index], hlen) == 0) |
| 684 | break; |
| 685 | } |
| 686 | if (html_enc_table[i].name == NULL) |
| 687 | { |
| 688 | out = g_string_append_c (out, '&'); |
| 689 | curr_index--; |
| 690 | continue; |
| 691 | } |
| 692 | edit_utf16recode (cnvu, html_enc_table[i].u, &out, &show_warn, curr_index); |
| 693 | curr_index += hlen; |
| 694 | continue; |
| 695 | } |
| 696 | else if (curr_index + 1 < len && |
| 697 | string[curr_index + 1] >= '0' && string[curr_index + 1] <= '9') |
| 698 | { |
| 699 | gint32 u = -1; |
| 700 | int i, x = 0; |
| 701 | |
| 702 | for (i = 1; i <= 6; i++) |
| 703 | { |
| 704 | if (curr_index + 1 >= len) |
| 705 | { |
| 706 | out = g_string_append (out, "&#"); |
| 707 | u = -1; |
| 708 | curr_index -= i; |
| 709 | break; |
| 710 | } |
| 711 | x = string[++curr_index]; |
| 712 | if (x >= '0' && x <= '9') |
| 713 | x -= '0'; |
| 714 | else |
| 715 | break; |
| 716 | u = u == -1 ? 0 : u * 10; |
| 717 | u += x; |
| 718 | } |
| 719 | if (x != ';') |
| 720 | { |
| 721 | out = g_string_append (out, "&#"); |
| 722 | u = -1; |
| 723 | curr_index -= i; |
| 724 | continue; |
| 725 | } |
| 726 | if (u >= 0) |
| 727 | edit_utf16recode (cnvu, u, &out, &show_warn, curr_index); |
| 728 | } |
| 729 | else if (curr_index + 2 < len && string[curr_index + 1] == 'x') |
| 730 | { |
| 731 | gint32 u = -1; |
| 732 | int i, x = 0; |
| 733 | curr_index++; |
| 734 | for (i = 1; i <= 5; i++) |
| 735 | { |
| 736 | if (curr_index + 1 >= len) |
| 737 | { |
| 738 | out = g_string_append (out, "&#"); |
| 739 | u = -1; |
| 740 | curr_index -= i; |
| 741 | break; |
| 742 | } |
| 743 | x = string[++curr_index]; |
| 744 | if (x >= 'a' && x <= 'f') |
| 745 | x -= 'a' - 10; |
| 746 | else if (x >= 'A' && x <= 'F') |
| 747 | x -= 'A' - 10; |
| 748 | else if (x >= '0' && x <= '9') |
| 749 | x -= '0'; |
| 750 | else |
| 751 | break; |
| 752 | u = u == -1 ? 0 : u << 4; |
| 753 | u += x; |
| 754 | } |
| 755 | if (x != ';') |
| 756 | { |
| 757 | out = g_string_append (out, "&#"); |
| 758 | u = -1; |
| 759 | curr_index -= i; |
| 760 | continue; |
| 761 | } |
| 762 | if (u >= 0) |
| 763 | edit_utf16recode (cnvu, u, &out, &show_warn, curr_index); |
| 764 | } |
| 765 | else |
| 766 | { |
| 767 | out = g_string_append (out, "&#"); |
| 768 | continue; |
| 769 | } |
| 770 | break; |
| 771 | case ESC_URL: |
| 772 | if (string[curr_index] != '%') |
| 773 | { |
| 774 | out = g_string_append_c (out, string[curr_index]); |
| 775 | continue; |
| 776 | } |
| 777 | curr_index++; |
| 778 | if (curr_index == len) |
| 779 | { |
| 780 | out = g_string_append_c (out, '%'); |
| 781 | continue; |
| 782 | } |
| 783 | else if (accept_u_and_conv && string[curr_index] == 'u') |
| 784 | { |
| 785 | gint32 u = -1; |
| 786 | int i; |
| 787 | for (i = 1; i <= 4; i++) |
| 788 | { |
| 789 | int x; |
| 790 | if (curr_index + 1 >= len) |
| 791 | { |
| 792 | if (u < 0) |
| 793 | out = g_string_append_c (out, 'u'); |
| 794 | break; |
| 795 | } |
| 796 | x = string[++curr_index]; |
| 797 | if (x >= 'a' && x <= 'f') |
| 798 | x -= 'a' - 10; |
| 799 | else if (x >= 'A' && x <= 'F') |
| 800 | x -= 'A' - 10; |
| 801 | else if (x >= '0' && x <= '9') |
| 802 | x -= '0'; |
| 803 | else |
| 804 | { |
| 805 | if (u < 0) |
| 806 | { |
| 807 | out = g_string_append_c (out, 'u'); |
| 808 | } |
| 809 | curr_index--; |
| 810 | break; |
| 811 | } |
| 812 | u = u == -1 ? 0 : u << 4; |
| 813 | u |= x & 0x0f; |
| 814 | } |
| 815 | if (u >= 0) |
| 816 | edit_utf16recode (cnvu, u, &out, &show_warn, curr_index); |
| 817 | continue; |
| 818 | } |
| 819 | else |
| 820 | { |
| 821 | int i, c = -1; |
| 822 | curr_index--; |
| 823 | for (i = 0; i < 2; i++) |
| 824 | { |
| 825 | int x; |
| 826 | if (curr_index + 1 >= len) |
| 827 | { |
| 828 | if (c < 0) |
| 829 | out = g_string_append_c (out, '%'); |
| 830 | break; |
| 831 | } |
| 832 | x = string[++curr_index]; |
| 833 | if (x >= 'a' && x <= 'f') |
| 834 | x -= 'a' - 10; |
| 835 | else if (x >= 'A' && x <= 'F') |
| 836 | x -= 'A' - 10; |
| 837 | else if (x >= '0' && x <= '9') |
| 838 | x -= '0'; |
| 839 | else |
| 840 | { |
| 841 | if (c >= 0) |
| 842 | out = g_string_append_c (out, c); |
| 843 | else |
| 844 | out = g_string_append_c (out, '%'); |
| 845 | c = -1; |
| 846 | curr_index--; |
| 847 | break; |
| 848 | } |
| 849 | c = c == -1 ? 0 : c << 4; |
| 850 | c |= x & 0x0f; |
| 851 | } |
| 852 | if (c >= 0) |
| 853 | out = g_string_append_c (out, c); |
| 854 | continue; |
| 855 | } |
| 856 | break; |
| 857 | case ESC_HEX: |
| 858 | if (string[curr_index] < '0' || |
| 859 | string[curr_index] > '9' && string[curr_index] < 'A' || |
| 860 | string[curr_index] > 'F' && string[curr_index] < 'a' || |
| 861 | string[curr_index] > 'f') |
| 862 | { |
| 863 | if (accept_i) |
| 864 | out = g_string_append_c (out, string[curr_index]); |
| 865 | continue; |
| 866 | } |
| 867 | else |
| 868 | { |
| 869 | int i, c = -1; |
| 870 | curr_index--; |
| 871 | for (i=0; i<2; i++) |
| 872 | { |
| 873 | int x; |
| 874 | if (curr_index + 1 == len) |
| 875 | { |
| 876 | break; |
| 877 | } |
| 878 | x = string[++curr_index]; |
| 879 | if (x >= 'a' && x <= 'f') |
| 880 | x -= 'a' - 10; |
| 881 | else if (x >= 'A' && x <= 'F') |
| 882 | x -= 'A' - 10; |
| 883 | else if (x >= '0' && x <= '9') |
| 884 | x -= '0'; |
| 885 | else |
| 886 | { |
| 887 | if (c >= 0) |
| 888 | out = g_string_append_c (out, c); |
| 889 | out = g_string_append_c (out, x); |
| 890 | c = -1; |
| 891 | break; |
| 892 | } |
| 893 | c = c == -1 ? 0 : c << 4; |
| 894 | c |= x & 0x0f; |
| 895 | } |
| 896 | if (c >= 0) |
| 897 | out = g_string_append_c (out, c); |
| 898 | continue; |
| 899 | } |
| 900 | case ESC_NUMP: |
| 901 | if (string[curr_index] != '^') |
| 902 | { |
| 903 | out = g_string_append_c (out, string[curr_index]); |
| 904 | continue; |
| 905 | } |
| 906 | curr_index++; |
| 907 | if (curr_index == len) |
| 908 | { |
| 909 | out = g_string_append_c (out, '^'); |
| 910 | continue; |
| 911 | } |
| 912 | else if (accept_u_and_conv && string[curr_index] == '+') |
| 913 | { |
| 914 | gint32 u = -1; |
| 915 | int i; |
| 916 | for (i = 1; i <= 4; i++) |
| 917 | { |
| 918 | int x; |
| 919 | if (curr_index + 1 >= len) |
| 920 | { |
| 921 | if (u < 0) |
| 922 | out = g_string_append_c (out, '+'); |
| 923 | break; |
| 924 | } |
| 925 | x = string[++curr_index]; |
| 926 | if (x >= 'a' && x <= 'f') |
| 927 | x -= 'a' - 10; |
| 928 | else if (x >= 'A' && x <= 'F') |
| 929 | x -= 'A' - 10; |
| 930 | else if (x >= '0' && x <= '9') |
| 931 | x -= '0'; |
| 932 | else |
| 933 | { |
| 934 | if (u < 0) |
| 935 | { |
| 936 | out = g_string_append_c (out, '+'); |
| 937 | } |
| 938 | curr_index--; |
| 939 | break; |
| 940 | } |
| 941 | u = u == -1 ? 0 : u << 4; |
| 942 | u |= x & 0x0f; |
| 943 | } |
| 944 | if (u >= 0) |
| 945 | edit_utf16recode (cnvu, u, &out, &show_warn, curr_index); |
| 946 | continue; |
| 947 | } |
| 948 | else |
| 949 | { |
| 950 | int i, c = -1; |
| 951 | curr_index--; |
| 952 | for (i = 0; i < 3; i++) |
| 953 | { |
| 954 | int x; |
| 955 | if (curr_index + 1 >= len) |
| 956 | { |
| 957 | if (c < 0) |
| 958 | out = g_string_append_c (out, '^'); |
| 959 | break; |
| 960 | } |
| 961 | x = string[++curr_index]; |
| 962 | if (x >= '0' && x <= '9') |
| 963 | x -= '0'; |
| 964 | else |
| 965 | { |
| 966 | if (c >= 0) |
| 967 | out = g_string_append_c (out, c); |
| 968 | else |
| 969 | out = g_string_append_c (out, '^'); |
| 970 | c = -1; |
| 971 | curr_index--; |
| 972 | break; |
| 973 | } |
| 974 | c = c == -1 ? 0 : c * 10; |
| 975 | c += x; |
| 976 | } |
| 977 | if (c > 0xff && show_warn) |
| 978 | { |
| 979 | char *tmp; |
| 980 | |
| 981 | tmp = g_strdup_printf (_("Decimal value out " |
| 982 | "of range (0-255) at %u: %3d\n" |
| 983 | "Value converted to: %3d"), |
| 984 | curr_index, c, c&0xff); |
| 985 | if (query_dialog (_("Warning"), tmp, D_ERROR, 2, _("&Dismiss"), _("Dismiss &All")) == 1) |
| 986 | show_warn = 0; |
| 987 | g_free (tmp); |
| 988 | } |
| 989 | if (c >= 0) |
| 990 | out = g_string_append_c (out, c&0xff); |
| 991 | continue; |
| 992 | } |
| 993 | break; |
| 994 | } |
| 995 | } |
| 996 | #ifdef HAVE_CHARSET |
| 997 | if (recode) |
| 998 | { |
| 999 | GIConv cnv; |
| 1000 | |
| 1001 | cnv = g_iconv_open (get_codepage_id (cp_to), |
| 1002 | get_codepage_id (cp_id)); |
| 1003 | |
| 1004 | if (cnv != INVALID_CONV) |
| 1005 | { |
| 1006 | GError *err = NULL; |
| 1007 | gchar *ostr; |
| 1008 | |
| 1009 | ostr = g_convert_with_iconv (out->str, out->len, cnv, NULL, NULL, &err); |
| 1010 | if (show_warn && (err != NULL || out == NULL)) |
| 1011 | { |
| 1012 | char *tmp; |
| 1013 | tmp = g_strdup_printf (_("Error(s) when reencoding string at %u:\n%s"), out->len, |
| 1014 | err == NULL ? "No result from conversion!" : err->message); |
| 1015 | edit_error_dialog (_("Warning"), tmp); |
| 1016 | g_free (tmp); |
| 1017 | } |
| 1018 | if (ostr != NULL) |
| 1019 | { |
| 1020 | g_string_truncate (out, 0); |
| 1021 | g_string_append (out, ostr); |
| 1022 | g_free (ostr); |
| 1023 | } |
| 1024 | g_iconv_close (cnv); |
| 1025 | } |
| 1026 | else |
| 1027 | { |
| 1028 | char *tmp; |
| 1029 | tmp = g_strdup_printf (_("Cannot initialise conversion from: %s to %s"), |
| 1030 | get_codepage_id (cp_to), |
| 1031 | get_codepage_id (cp_id)); |
| 1032 | edit_error_dialog (_("Warning"), tmp); |
| 1033 | g_free (tmp); |
| 1034 | } |
| 1035 | } |
| 1036 | #endif /* HAVE_CHARSET */ |
| 1037 | if (insert_from_file) |
| 1038 | { |
| 1039 | if (insert_from_file != 2) |
| 1040 | { |
| 1041 | |
| 1042 | if (write (fdout, out->str, out->len) != (ssize_t)out->len) |
| 1043 | { |
| 1044 | edit_error_dialog (_("Error"), get_sys_error (_("Cannot write temporary file"))); |
| 1045 | break; |
| 1046 | } |
| 1047 | goto continue_file; |
| 1048 | } |
| 1049 | } |
| 1050 | else |
| 1051 | { |
| 1052 | for (len = 0; len < out->len; len++) |
| 1053 | edit_insert (edit, out->str[len]); |
| 1054 | g_string_free (out, TRUE); |
| 1055 | } |
| 1056 | #ifdef HAVE_CHARSET |
| 1057 | if (cnvu != INVALID_CONV) |
| 1058 | g_iconv_close (cnvu); |
| 1059 | #endif /* HAVE_CHARSET */ |
| 1060 | if (insert_from_file == 0) |
| 1061 | g_free (string); |
| 1062 | break; |
| 1063 | } |
| 1064 | if (insert_from_file == 0) |
| 1065 | g_free (string); |
| 1066 | } |
| 1067 | #ifdef HAVE_CHARSET |
| 1068 | g_string_free (str_recode, TRUE); |
| 1069 | #endif /* HAVE_CHARSET */ |
| 1070 | } |