poe.c (71644B)
1 /* This program is licensed under the terms of GNU GPL v3 or (at your option) 2 * any later version. Copyright (C) 2021-2026 Страхиња Радић. 3 * See the file LICENSE for exact copyright and license details. */ 4 5 #include <assert.h> 6 #include <errno.h> 7 #include <fcntl.h> 8 #include <locale.h> 9 #include <stdarg.h> 10 #include <stdint.h> 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <string.h> 14 #include <sys/stat.h> 15 #include <unistd.h> 16 17 #define COPYRIGHT \ 18 (" This program is licensed under the terms of GNU GPL v3" \ 19 " or (at your option)\n" \ 20 " any later version. Copyright (C) 2021-2026 Strahinya Radich.\n" \ 21 " See the file LICENSE for exact copyright and license " \ 22 "details.\n" \ 23 " File termbox.h is based on termbox2 TUI library, (c) 2021" \ 24 " termbox developers,\n" \ 25 " which is released under the terms of Expat license, see the file" \ 26 " LICENSE.termbox.\n") 27 #define POE_C_GLOBALS 28 #define TB_IMPL 29 30 /* Order is important! */ 31 #include "draw.h" 32 #include "poe.h" 33 #include "config.h" 34 #include "limit.h" 35 #include "po.h" 36 #include "strtonum.h" 37 #include "util.h" 38 #include "version.h" 39 40 #define MAXCOPYBUF 8192 41 42 extern const char* errors[]; 43 extern const char* actions[]; 44 extern const char* prompt_dirty; 45 extern const char* prompt_edit_dirty; 46 extern const char* prompt_overwrite; 47 48 static int 49 cancel_callback(struct DrawState* state, struct tb_event* ev) 50 { 51 assert((state != NULL) && (ev != NULL)); 52 if (strchr("Yy", ev->ch)) 53 { 54 state->reshow_edit = 0; 55 save_msgstr(state); 56 } 57 else /* not saving */ 58 { 59 state->edit_dirty = 0; 60 cancel_msgstr(state); 61 } 62 return 0; 63 } 64 65 static int 66 cancel_close(struct DrawState* state) 67 { 68 assert(state != NULL); 69 if (!state->error && state->in_prompt) 70 { 71 state->in_prompt = 0; 72 *state->prompt = 0; 73 } 74 else if (state->show_help) 75 state->show_help = 0; 76 else if (state->show_edit) 77 cancel_msgstr(state); 78 else if (state->show_search) 79 cancel_search(state); 80 else 81 state->error = ERR_EXIT_KEY; 82 return 0; 83 } 84 85 static int 86 cancel_msgstr(struct DrawState* state) 87 { 88 char* temp = NULL; 89 ssize_t i; 90 91 assert(state != NULL); 92 if (state->edit_dirty) 93 { 94 state->in_prompt = 1; 95 MEMCCPY(state->prompt, prompt_edit_dirty, LINE_DEFAULT, temp); 96 state->prompt_callback = cancel_callback; 97 return 0; 98 } 99 100 state->show_edit = 0; 101 state->reshow_edit = 0; 102 state->edit_info_focused = 0; 103 104 for (i = 0; i < state->input_rows_count; i++) 105 free_bufferline(state->input_buffer + i); 106 free(state->input_buffer); 107 state->input_buffer = NULL; 108 state->input_rows_count = 0; 109 if (state->info_buffer) 110 for (i = 0; i < state->info_rows_count; i++) 111 free_bufferline(&state->info_buffer[i]); 112 free(state->info_buffer); 113 state->info_buffer = NULL; 114 state->info_rows_count = 0; 115 state->fuzzy_count = state->fuzzy_before_edit; 116 switch (state->input_changed_fuzzy) 117 { 118 case SET: 119 state->entries[state->msgid_number - 1].flags &= ~FL_FUZZY; 120 break; 121 case CLEARED: 122 state->entries[state->msgid_number - 1].flags |= FL_FUZZY; 123 break; 124 default:; 125 } 126 state->input_changed_fuzzy = UNCHANGED; 127 tb_hide_cursor(); 128 return 0; 129 } 130 131 static int 132 cancel_search(struct DrawState* state) 133 { 134 assert(state != NULL); 135 state->show_search = 0; 136 *state->search = 0; 137 tb_hide_cursor(); 138 return 0; 139 } 140 141 static int 142 copy_msgid_to_input(struct DrawState* state) 143 { 144 struct PoEntry* current; 145 struct BufferLine* newbuf; 146 struct BufferLine* pbuffer; 147 const uint32_t* pmsgid; 148 const uint32_t* old_pmsgid; 149 uint32_t* newtext; 150 char* temp = NULL; 151 ssize_t msgid_num_lines, lines_to_init, lines_to_free, i, len, newlen; 152 153 assert(state != NULL); 154 155 if (state->edit_info_focused) 156 { 157 state->error = ERR_DLG_FOCUS_EDIT; 158 return 0; 159 } 160 161 if (!state->in_prompt && *state->input_buffer->text) 162 { 163 state->in_prompt = 1; 164 MEMCCPY(state->prompt, prompt_overwrite, LINE_DEFAULT, temp); 165 state->prompt_callback = overwrite_copy_msgid_callback; 166 return 0; 167 } 168 169 if (state->msgid_number == 1) 170 { 171 state->error = ERR_ILLEGAL_ON_FIRST; 172 return 0; 173 } 174 175 assert(state->entries != NULL); 176 current = &state->entries[state->msgid_number - 1]; 177 msgid_num_lines = u32_lines_in_string(current->msgid, 0, 0); 178 lines_to_init = state->input_rows_count < msgid_num_lines 179 ? msgid_num_lines - state->input_rows_count 180 : 0; 181 lines_to_free = state->input_rows_count > msgid_num_lines 182 ? state->input_rows_count - msgid_num_lines 183 : 0; 184 for (i = 0; i < lines_to_free; i++) 185 free_bufferline( 186 state->input_buffer + state->input_rows_count - i - 1); 187 newbuf = realloc(state->input_buffer, 188 msgid_num_lines * sizeof(struct BufferLine)); 189 pmsgid = current->msgid; 190 191 if (!newbuf) 192 return 0; 193 194 pbuffer = NULL; 195 for (i = 0; i < lines_to_init; i++) 196 { 197 pbuffer = newbuf + msgid_num_lines - 1 - i; 198 init_bufferline(pbuffer); 199 newtext = calloc(LINE_DEFAULT, sizeof(uint32_t)); 200 if (!newtext) 201 return 0; 202 pbuffer->text = newtext; 203 } 204 205 pbuffer = newbuf; 206 while (pmsgid && *pmsgid) 207 { 208 old_pmsgid = pmsgid; 209 len = u32_next_line(current->msgid, &pmsgid, 0, 0); 210 u32_strncpy(pbuffer->text, old_pmsgid, 211 MIN(len + 1, LINE_DEFAULT)); 212 newlen = u32_decode_tabs(state, pbuffer->text, LINE_DEFAULT); 213 pbuffer->length = newlen; 214 pbuffer++; 215 } 216 217 state->input_rows_count = msgid_num_lines; 218 state->input_buffer = newbuf; 219 state->edit_dirty = 1; 220 221 return 0; 222 } 223 224 int 225 cp(const char* from, const char* to) 226 { 227 struct stat st; 228 struct timespec times[2]; 229 char buf[MAXCOPYBUF]; 230 int fromfd, tofd; 231 int nread = 0; 232 int nwritten = 0; 233 int result; 234 int saved_errno; 235 236 assert((from != NULL) && (to != NULL)); 237 238 tofd = 0; 239 fromfd = open(from, O_RDONLY); 240 if (fromfd < 0) 241 return 1; 242 if (fstat(fromfd, &st) < 0) 243 { 244 result = 1; 245 goto cp_cleanup; 246 } 247 248 tofd = open(to, O_WRONLY | O_CREAT | O_TRUNC, st.st_mode); 249 if (tofd < 0) 250 return 1; 251 if (fchmod(tofd, st.st_mode) < 0) 252 { 253 result = 1; 254 goto cp_cleanup; 255 } 256 times[0] = st.st_atim; 257 times[1] = st.st_mtim; 258 259 result = 0; 260 while ((nread = read(fromfd, buf, MAXCOPYBUF)) > 0) 261 { 262 nwritten = write(tofd, buf, nread); 263 if (nwritten < nread) 264 { 265 result = 1; 266 goto cp_cleanup; 267 } 268 } 269 270 if (nread < 0) 271 { 272 result = 1; 273 goto cp_cleanup; 274 } 275 276 errno = 0; 277 if (futimens(tofd, times) < 0) 278 result = 1; 279 280 cp_cleanup: 281 saved_errno = errno; 282 if (tofd) 283 close(tofd); 284 close(fromfd); 285 errno = saved_errno; 286 return result; 287 } 288 289 static char* 290 dirname(char* dir, char* path, const ssize_t dir_size) 291 { 292 char* p = NULL; 293 294 assert((dir != NULL) && (path != NULL) && (dir_size > 0)); 295 if (!*path) 296 return NULL; 297 298 MEMCCPY(dir, path, dir_size, p); 299 300 if (p > dir + 2 && *(p - 2) == '/') 301 *p = 0; 302 if (p > dir + 1) 303 p--; 304 305 while (p != dir && *p != '/') 306 { 307 p--; 308 if (p != dir && *p == '/') 309 { 310 *p = 0; 311 return dir; 312 } 313 else if (p == dir && *p == '/') 314 return dir; 315 } 316 dir[0] = '.'; 317 dir[1] = 0; 318 return dir; 319 } 320 321 static int 322 edit_page_down(struct DrawState* state) 323 { 324 char* temp = NULL; 325 assert(state != NULL); 326 if (state->edit_info_focused) 327 { 328 if (state->info_first_shown.row + (EDIT_HEIGHT - 3) / 2 329 < state->info_rows_count - 1) 330 state->info_first_shown.row += (EDIT_HEIGHT - 3) / 2; 331 return 0; 332 } 333 if (state->edit_dirty) 334 { 335 state->in_prompt = 1; 336 MEMCCPY(state->prompt, prompt_edit_dirty, LINE_DEFAULT, temp); 337 state->prompt_callback = edit_page_down_callback; 338 return 0; 339 } 340 move_list_down(state); 341 state->msgstr_index = 0; 342 return show_edit(state); 343 } 344 345 static int 346 edit_page_down_callback(struct DrawState* state, struct tb_event* ev) 347 { 348 assert((state != NULL) && (ev != NULL)); 349 if (strchr("Yy", ev->ch)) 350 { 351 state->reshow_edit = 1; 352 save_msgstr(state); 353 } 354 else 355 state->edit_dirty = 0; 356 return edit_page_down(state); 357 } 358 359 static int 360 edit_page_up(struct DrawState* state) 361 { 362 char* temp = NULL; 363 assert(state != NULL); 364 if (state->edit_info_focused) 365 { 366 if (state->info_first_shown.row > (EDIT_HEIGHT - 3) / 2) 367 state->info_first_shown.row -= (EDIT_HEIGHT - 3) / 2; 368 else 369 move_start(state); 370 return 0; 371 } 372 if (state->edit_dirty) 373 { 374 state->in_prompt = 1; 375 MEMCCPY(state->prompt, prompt_edit_dirty, LINE_DEFAULT, temp); 376 state->prompt_callback = edit_page_up_callback; 377 return 0; 378 } 379 move_list_up(state); 380 state->msgstr_index = 0; 381 return show_edit(state); 382 } 383 384 static int 385 edit_page_up_callback(struct DrawState* state, struct tb_event* ev) 386 { 387 assert((state != NULL) && (ev != NULL)); 388 if (strchr("Yy", ev->ch)) 389 { 390 state->reshow_edit = 1; 391 save_msgstr(state); 392 } 393 else 394 state->edit_dirty = 0; 395 return edit_page_up(state); 396 } 397 398 static int 399 erase_backwards(struct DrawState* state) 400 { 401 struct BufferLine* buffer_line; 402 403 assert((state != NULL) && (state->input_buffer != NULL)); 404 buffer_line = &state->input_buffer[state->coord.row]; 405 if (state->edit_info_focused) 406 { 407 state->error = ERR_DLG_FOCUS_EDIT; 408 return 1; 409 } 410 411 if (state->coord.col > 0) 412 { 413 ssize_t i = state->coord.col; 414 assert(buffer_line->text != NULL); 415 while (i <= buffer_line->length) 416 { 417 buffer_line->text[i - 1] = buffer_line->text[i]; 418 i++; 419 } 420 buffer_line->text[i - 1] = 0; 421 buffer_line->length--; 422 position_to(state, state->coord.row, state->coord.col - 1); 423 state->edit_dirty = 1; 424 } 425 else if (state->coord.row > 0) 426 { 427 move_up(state); 428 move_end(state); 429 erase_forward(state); 430 } 431 return 0; 432 } 433 434 static int 435 erase_forward(struct DrawState* state) 436 { 437 struct BufferLine* buffer_line; 438 size_t i; 439 440 assert((state != NULL) && (state->input_buffer != NULL)); 441 buffer_line = &state->input_buffer[state->coord.row]; 442 if (state->edit_info_focused) 443 { 444 state->error = ERR_DLG_FOCUS_EDIT; 445 return 1; 446 } 447 448 assert(buffer_line->text != NULL); 449 if (buffer_line->text[state->coord.col]) 450 { 451 i = state->coord.col; 452 while (i < LINE_DEFAULT - 2 && buffer_line->text[i + 1]) 453 { 454 buffer_line->text[i] = buffer_line->text[i + 1]; 455 i++; 456 } 457 buffer_line->text[i] = 0; 458 buffer_line->length--; 459 state->edit_dirty = 1; 460 } 461 else if (state->coord.row < state->input_rows_count - 1) 462 { 463 if (join_lines(state)) 464 { 465 state->error = ERR_CANT_ALLOC; 466 state->running = 0; 467 return 1; 468 } 469 } 470 return 0; 471 } 472 473 static int 474 erase_prev_word(struct DrawState* state) 475 { 476 uint32_t* pline; 477 ssize_t len; 478 int i, j, d; 479 480 assert(state != NULL); 481 if (state->edit_info_focused) 482 { 483 state->error = ERR_DLG_FOCUS_EDIT; 484 return 1; 485 } 486 i = state->coord.col; 487 len = state->input_buffer[state->coord.row].length; 488 pline = state->input_buffer[state->coord.row].text; 489 490 if (i > 0 && u32_is_word_boundary(pline[i - 1], 0)) 491 i--; 492 while (i > 0 && u32_is_word_boundary(pline[i], 0)) 493 i--; 494 while (i > 0 && !u32_is_word_boundary(pline[i], 0) 495 && !u32_is_word_boundary(pline[i - 1], 0)) 496 i--; 497 498 d = state->coord.col - i; 499 500 for (j = i; j < len - d; j++) 501 pline[j] = pline[j + d]; 502 503 len -= d; 504 state->input_buffer[state->coord.row].length = len; 505 pline[len] = 0; 506 state->coord.col = i; 507 state->input_display_column = display_length(pline, state->coord.col); 508 if (state->input_display_column < state->input_first_shown.col) 509 state->input_first_shown.col = state->input_display_column; 510 state->edit_dirty = 1; 511 return 0; 512 } 513 514 static int 515 erase_search_backwards(struct DrawState* state) 516 { 517 size_t i, len; 518 assert(state != NULL); 519 len = u32_strlen(state->search); 520 if (state->search_column > 0) 521 { 522 i = state->search_column; 523 do 524 { 525 state->search[i - 1] = state->search[i]; 526 i++; 527 } while (i <= len); 528 state->search[i - 1] = 0; 529 search_position_to(state, state->search_column - 1); 530 return next_match(state); 531 } 532 return 0; 533 } 534 535 static int 536 erase_search_forward(struct DrawState* state) 537 { 538 size_t i; 539 assert(state != NULL); 540 if (state->search[state->search_column]) 541 { 542 i = state->search_column; 543 while (i < MAXSEARCH - 2 && state->search[i + 1]) 544 { 545 state->search[i] = state->search[i + 1]; 546 i++; 547 } 548 state->search[i] = 0; 549 550 return next_match(state); 551 } 552 return 0; 553 } 554 555 static int 556 erase_search_prev_word(struct DrawState* state) 557 { 558 uint32_t* pline; 559 ssize_t len; 560 int i, j, d; 561 562 assert(state != NULL); 563 pline = state->search; 564 len = u32_strlen(state->search); 565 i = state->search_column; 566 if (i > 0 && u32_is_word_boundary(pline[i - 1], 0)) 567 i--; 568 while (i > 0 && u32_is_word_boundary(pline[i], 0)) 569 i--; 570 while (i > 0 && !u32_is_word_boundary(pline[i], 0) 571 && !u32_is_word_boundary(pline[i - 1], 0)) 572 i--; 573 574 d = state->search_column - i; 575 576 for (j = i; j < len - d; j++) 577 pline[j] = pline[j + d]; 578 579 len -= d; 580 pline[len] = 0; 581 search_position_to(state, i); 582 return next_match(state); 583 } 584 585 static int 586 erase_search_to_end(struct DrawState* state) 587 { 588 assert(state != NULL); 589 state->search[state->search_column] = 0; 590 move_search_end(state); 591 return next_match(state); 592 } 593 594 static int 595 erase_search_to_start(struct DrawState* state) 596 { 597 size_t i, len; 598 599 assert((state != NULL) && (state->search != NULL)); 600 i = state->search_column; 601 len = u32_strlen(state->search); 602 do 603 { 604 assert(i < MAXSEARCH); 605 state->search[i - state->search_column] = state->search[i]; 606 i++; 607 } while (i <= len); 608 state->search[len - state->search_column] = 0; 609 move_search_start(state); 610 return next_match(state); 611 } 612 613 static int 614 erase_to_end(struct DrawState* state) 615 { 616 struct BufferLine* buffer_line = NULL; 617 618 assert((state != NULL) && (state->input_buffer != NULL)); 619 if (state->edit_info_focused) 620 { 621 state->error = ERR_DLG_FOCUS_EDIT; 622 return 1; 623 } 624 625 buffer_line = &state->input_buffer[state->coord.row]; 626 assert(buffer_line->text != NULL); 627 buffer_line->text[state->coord.col] = 0; 628 buffer_line->length = state->coord.col; 629 move_end(state); 630 state->edit_dirty = 1; 631 return 0; 632 } 633 634 static int 635 erase_to_start(struct DrawState* state) 636 { 637 struct BufferLine* buffer_line; 638 ssize_t i; 639 ssize_t len; 640 641 assert((state != NULL) && (state->input_buffer != NULL)); 642 i = state->coord.col; 643 buffer_line = &state->input_buffer[state->coord.row]; 644 len = buffer_line->length; 645 646 if (state->edit_info_focused) 647 { 648 state->error = ERR_DLG_FOCUS_EDIT; 649 return 1; 650 } 651 652 do 653 { 654 assert(i <= len); 655 buffer_line->text[i - state->coord.col] = buffer_line->text[i]; 656 i++; 657 } while (i <= len); 658 buffer_line->text[len - state->coord.col] = 0; 659 buffer_line->length = len - state->coord.col; 660 move_start(state); 661 state->edit_dirty = 1; 662 return 0; 663 } 664 665 static int 666 exit_program(struct DrawState* state) 667 { 668 char* temp = NULL; 669 670 assert(state != NULL); 671 if (state->edit_dirty) 672 { 673 state->in_prompt = 1; 674 MEMCCPY(state->prompt, prompt_edit_dirty, LINE_DEFAULT, temp); 675 state->prompt_callback = quit_callback; 676 } 677 else if (state->dirty) 678 { 679 state->in_prompt = 1; 680 MEMCCPY(state->prompt, prompt_dirty, LINE_DEFAULT, temp); 681 state->prompt_callback = quit_callback; 682 } 683 else 684 state->running = 0; 685 state->error = ERR_NONE; 686 return 0; 687 } 688 689 static void 690 goto_msgid(struct DrawState* state, const ssize_t msgid_number) 691 { 692 ssize_t last_non_obsolete; 693 694 assert(state != NULL); 695 last_non_obsolete 696 = state->real_msgid_count - state->obsolete_at_end_count; 697 if (msgid_number > state->real_msgid_count) 698 return; 699 700 state->msgid_number = msgid_number; 701 702 if (state->msgid_number < state->first_shown_msgid) 703 state->first_shown_msgid = state->msgid_number; 704 else if (state->msgid_number 705 > state->first_shown_msgid + state->maxy - 2) 706 state->first_shown_msgid 707 = state->msgid_number - (state->maxy - 2); 708 else if (last_non_obsolete < state->msgid_number + state->maxy - 2) 709 state->first_shown_msgid = (last_non_obsolete > state->maxy - 2 710 ? last_non_obsolete - (state->maxy - 2) 711 : 1); 712 } 713 714 static void 715 handle_key_event(struct tb_event* ev, struct DrawState* state) 716 { 717 const struct Key* current = NULL; 718 size_t key; 719 720 assert((state != NULL) && (ev != NULL)); 721 state->error = ERR_NONE; 722 errno = 0; 723 724 if (state->in_prompt) 725 { 726 if (ev->ch && strchr("YyNn", ev->ch)) 727 { 728 if (state->prompt_callback) 729 { 730 state->prompt_callback(state, ev); 731 state->prompt_callback = NULL; 732 } 733 state->in_prompt = 0; 734 *state->prompt = 0; 735 } 736 else if (ev->key == TB_KEY_ESC) 737 cancel_close(state); 738 return; 739 } 740 741 /* Remember to also edit the following condition if you 742 * reassign motion keys */ 743 if (ev->key != TB_KEY_ARROW_DOWN && ev->key != TB_KEY_ARROW_UP 744 && state->show_edit) 745 state->saved_column = -1; 746 747 for (key = 0; key < LEN(top_level_keys); key++) 748 { 749 current = top_level_keys + key; 750 if (((current->ev.mod == ev->mod) 751 && (current->ev.key == ev->key) && !ev->ch) 752 || (current->ev.ch && (current->ev.ch == ev->ch))) 753 if (current->callback) 754 { 755 current->callback(state); 756 return; 757 } 758 } 759 760 if (state->show_edit) 761 { 762 for (key = 0; key < LEN(edit_keys); key++) 763 { 764 current = &edit_keys[key]; 765 if (((current->ev.mod == ev->mod) 766 && (current->ev.key == ev->key) && !ev->ch) 767 || (current->ev.ch 768 && (current->ev.ch == ev->ch))) 769 if (current->callback) 770 { 771 current->callback(state); 772 return; 773 } 774 } 775 if (state->edit_info_focused) 776 { 777 state->error = ERR_DLG_FOCUS_EDIT; 778 return; 779 } 780 if (ev->ch) 781 { 782 insert_char(state, ev->ch); 783 return; 784 } 785 } 786 787 if (state->show_search) 788 { 789 for (key = 0; key < LEN(search_keys); key++) 790 { 791 current = &search_keys[key]; 792 if (((current->ev.mod == ev->mod) 793 && (current->ev.key == ev->key) && !ev->ch) 794 || (current->ev.ch 795 && (current->ev.ch == ev->ch))) 796 if (current->callback) 797 { 798 current->callback(state); 799 return; 800 } 801 } 802 if (ev->ch) 803 { 804 insert_search_char(state, ev->ch); 805 return; 806 } 807 } 808 809 if (state->show_help) 810 { 811 for (key = 0; key < LEN(help_keys); key++) 812 { 813 current = help_keys + key; 814 if (((current->ev.mod == ev->mod) 815 && (current->ev.key == ev->key) && !ev->ch) 816 || (current->ev.ch 817 && (current->ev.ch == ev->ch))) 818 if (current->callback) 819 { 820 current->callback(state); 821 return; 822 } 823 } 824 825 state->error = ERR_DLG_OPEN; 826 return; 827 } 828 829 if (state->show_search || state->show_edit) 830 { 831 state->error = ERR_DLG_OPEN_QUIT; 832 return; 833 } 834 835 for (key = 0; key < LEN(main_keys); key++) 836 { 837 current = &main_keys[key]; 838 if (((current->ev.mod == ev->mod) 839 && (current->ev.key == ev->key) && !ev->ch) 840 || (current->ev.ch && (current->ev.ch == ev->ch))) 841 if (current->callback) 842 { 843 current->callback(state); 844 return; 845 } 846 } 847 848 if (state->edit_info_focused) 849 state->error = ERR_DLG_FOCUS_EDIT; 850 else if (state->show_edit || state->show_search) 851 state->error = ERR_UNKNOWN_KEY; 852 else 853 state->error = ERR_UNKNOWN_KEY_HELP; 854 } 855 856 static int 857 hide_search(struct DrawState* state) 858 { 859 assert(state != NULL); 860 state->show_search = 0; 861 tb_hide_cursor(); 862 return 0; 863 } 864 865 static int 866 insert_char(struct DrawState* state, uint32_t ch) 867 { 868 struct BufferLine* buffer_line; 869 uint32_t* temp = NULL; 870 ssize_t i, len, size; 871 872 assert((state != NULL) && (state->input_buffer != NULL)); 873 if (state->edit_info_focused) 874 return 1; 875 876 ENSURE_SIZE(state->input_buffer[state->coord.row].text, temp, 877 state->input_buffer[state->coord.row].size, 878 state->input_buffer[state->coord.row].length + 2, 879 state->input_buffer[state->coord.row].size + LINE_ALLOC_DELTA, 880 insert_char_alloc_error, uint32_t); 881 882 buffer_line = &state->input_buffer[state->coord.row]; 883 len = buffer_line->length; 884 size = buffer_line->size; 885 i = len; 886 887 assert(buffer_line->text != NULL); 888 while (i + 1 < size && i > state->coord.col) 889 { 890 buffer_line->text[i] = buffer_line->text[i - 1]; 891 i--; 892 } 893 buffer_line->text[state->coord.col] = ch; 894 buffer_line->length++; 895 position_to(state, state->coord.row, state->coord.col + 1); 896 buffer_line->text[buffer_line->length] = 0; 897 898 state->edit_dirty = 1; 899 return 0; 900 901 insert_char_alloc_error: 902 state->error = ERR_CANT_ALLOC; 903 return 1; 904 } 905 906 static int 907 insert_line(struct DrawState* state) 908 { 909 struct BufferLine* newbuf = NULL; 910 struct BufferLine* line = NULL; 911 uint32_t* newtext = NULL; 912 uint32_t* ptext = NULL; 913 ssize_t i; 914 915 if (state->edit_info_focused) 916 { 917 state->error = ERR_DLG_FOCUS_EDIT; 918 return 1; 919 } 920 921 assert(state != NULL); 922 newbuf = realloc(state->input_buffer, 923 (state->input_rows_count + 1) * sizeof(struct BufferLine)); 924 if (!newbuf) 925 goto insert_line_alloc_error; 926 state->input_buffer = newbuf; 927 line = &state->input_buffer[state->input_rows_count]; 928 init_bufferline(line); 929 newtext = calloc(LINE_DEFAULT, sizeof(uint32_t)); 930 if (!newtext) 931 goto insert_line_alloc_error; 932 line->text = newtext; 933 line->size = LINE_DEFAULT; 934 state->input_rows_count++; 935 for (i = state->input_rows_count - 1; i > state->coord.row + 1; i--) 936 { 937 U32_SAFE_COPY(state->input_buffer[i].text, ptext, 938 state->input_buffer[i].size, 939 state->input_buffer[i - 1].text, 940 state->input_buffer[i].size, insert_line_alloc_error); 941 state->input_buffer[i].length 942 = state->input_buffer[i - 1].length; 943 } 944 ENSURE_SIZE(state->input_buffer[state->coord.row + 1].text, ptext, 945 state->input_buffer[state->coord.row + 1].size, LINE_DEFAULT, 946 LINE_DEFAULT, insert_line_alloc_error, uint32_t); 947 *state->input_buffer[state->coord.row + 1].text = 0; 948 if (state->coord.col < state->input_buffer[state->coord.row].length) 949 U32_SAFE_COPY(state->input_buffer[state->coord.row + 1].text, 950 ptext, state->input_buffer[state->coord.row + 1].size, 951 state->input_buffer[state->coord.row].text 952 + state->coord.col, 953 state->input_buffer[state->coord.row].size, 954 insert_line_alloc_error); 955 state->input_buffer[state->coord.row + 1].length 956 = state->input_buffer[state->coord.row].length 957 - state->coord.col; 958 state->input_buffer[state->coord.row].text[state->coord.col] = 0; 959 state->input_buffer[state->coord.row].length = state->coord.col; 960 961 state->saved_column = -1; 962 state->coord.row++; 963 if (state->coord.row 964 > state->input_first_shown.row + (EDIT_HEIGHT - 3) / 2) 965 state->input_first_shown.row 966 = state->coord.row - (EDIT_HEIGHT - 3) / 2; 967 move_start(state); 968 state->edit_dirty = 1; 969 return 0; 970 971 insert_line_alloc_error: 972 state->error = ERR_CANT_ALLOC; 973 return 1; 974 } 975 976 static int 977 insert_search_char(struct DrawState* state, uint32_t ch) 978 { 979 ssize_t len, i; 980 int add_null; 981 assert(state != NULL); 982 len = u32_strlen(state->search); 983 if (state->search_column + 1 < MAXSEARCH) 984 { 985 i = len + 1; 986 add_null = state->search[state->search_column] == 0; 987 while (i + 2 < MAXSEARCH && i > state->search_column) 988 { 989 state->search[i] = state->search[i - 1]; 990 i--; 991 } 992 state->search[state->search_column++] = ch; 993 if (state->search_column == MAXSEARCH) 994 state->search_column = MAXSEARCH - 1; 995 state->search_display_column 996 = display_length(state->search, state->search_column); 997 if (state->search_display_column 998 - state->search_first_shown_column 999 > state->search_width - 4) 1000 { 1001 state->search_first_shown_column 1002 = state->search_display_column 1003 - state->search_width + 4; 1004 state->search_display_column 1005 = display_length(state->search, 1006 state->search_column); 1007 } 1008 if (add_null) 1009 state->search[state->search_column] = 0; 1010 1011 next_match(state); 1012 } 1013 return 0; 1014 } 1015 1016 static int 1017 insert_search_space(struct DrawState* state) 1018 { 1019 return insert_search_char(state, SPACE); 1020 } 1021 1022 static int 1023 insert_search_tab(struct DrawState* state) 1024 { 1025 return insert_search_char(state, TAB); 1026 } 1027 1028 static int 1029 insert_space(struct DrawState* state) 1030 { 1031 return insert_char(state, SPACE); 1032 } 1033 1034 static int 1035 insert_tab(struct DrawState* state) 1036 { 1037 return insert_char(state, TAB); 1038 } 1039 1040 static int 1041 join_lines(struct DrawState* state) 1042 { 1043 struct BufferLine* first_line = NULL; 1044 struct BufferLine* next_line = NULL; 1045 uint32_t* ptext = NULL; 1046 ssize_t i; 1047 1048 assert((state != NULL) && (state->input_buffer != NULL)); 1049 if (state->coord.row + 1 == state->input_rows_count) 1050 return 1; 1051 1052 first_line = &state->input_buffer[state->coord.row]; 1053 next_line = &state->input_buffer[state->coord.row + 1]; 1054 ENSURE_SIZE(first_line->text, ptext, first_line->size, 1055 first_line->length + next_line->length + 1, 1056 first_line->length + next_line->length + LINE_ALLOC_DELTA, 1057 join_lines_alloc_error, uint32_t); 1058 U32_MEMCCPY(first_line->text + first_line->length, next_line->text, 1059 next_line->length + 1, ptext); 1060 first_line->length = u32_strlen(first_line->text); 1061 for (i = state->coord.row + 1; i + 1 < state->input_rows_count; i++) 1062 { 1063 U32_SAFE_COPY(state->input_buffer[i].text, ptext, 1064 state->input_buffer[i].size, 1065 state->input_buffer[i + 1].text, 1066 state->input_buffer[i + 1].size, 1067 join_lines_alloc_error); 1068 state->input_buffer[i].length 1069 = state->input_buffer[i + 1].length; 1070 } 1071 free_bufferline(&state->input_buffer[state->input_rows_count - 1]); 1072 state->input_rows_count--; 1073 state->edit_dirty = 1; 1074 return 0; 1075 1076 join_lines_alloc_error: 1077 state->error = ERR_CANT_ALLOC; 1078 return 1; 1079 } 1080 1081 static int 1082 load_info(struct DrawState* state) 1083 { 1084 struct PoEntry* first = NULL; 1085 struct PoEntry* current = NULL; 1086 struct BufferLine* newbuf = NULL; 1087 struct BufferLine* pnewbuf = NULL; 1088 uint32_t* newtext = NULL; 1089 ssize_t msgid_num_lines, msgid_plural_num_lines, num_lines, i; 1090 1091 assert((state != NULL) && (state->entries != NULL)); 1092 first = &state->entries[0]; 1093 current = &state->entries[state->msgid_number - 1]; 1094 msgid_num_lines = u32_lines_in_string(current->msgid, 0, 0); 1095 msgid_plural_num_lines 1096 = u32_lines_in_string(current->msgid_plural, 0, 0); 1097 num_lines = 0; 1098 1099 for (i = 0; i < state->info_rows_count; i++) 1100 free_bufferline(&state->info_buffer[i]); 1101 free(state->info_buffer); 1102 1103 state->info_maxlen = 0; 1104 1105 if (current->msgid) 1106 num_lines += msgid_num_lines + 1; 1107 if (current->msgid_plural) 1108 { 1109 num_lines += msgid_plural_num_lines + 1; 1110 if (first->plural_forms) 1111 num_lines += 2; /* title + plural_forms */ 1112 } 1113 if (current->comments) 1114 num_lines += current->comment_lines + 1; 1115 if (current->extr_comments) 1116 num_lines += current->extr_comment_lines + 1; 1117 if (current->ref_comments) 1118 num_lines += current->ref_comment_lines + 1; 1119 newbuf = calloc(num_lines, sizeof(struct BufferLine)); 1120 if (!newbuf) 1121 goto load_info_alloc_error; 1122 1123 pnewbuf = newbuf; 1124 for (i = 0; i < num_lines; i++) 1125 { 1126 init_bufferline(pnewbuf); 1127 newtext = calloc(LINE_DEFAULT, sizeof(uint32_t)); 1128 if (!newtext) 1129 goto load_info_alloc_error; 1130 pnewbuf->text = newtext; 1131 pnewbuf->size = LINE_DEFAULT; 1132 pnewbuf->size = 0; 1133 pnewbuf++; 1134 } 1135 1136 state->info_rows_count = num_lines; 1137 1138 pnewbuf = newbuf; 1139 1140 if (current->msgid) 1141 if (load_info_msgid(state, &pnewbuf)) 1142 return 1; 1143 1144 if (current->msgid_plural) 1145 if (load_info_msgid_plural(state, &pnewbuf)) 1146 return 1; 1147 1148 if (current->comments) 1149 if (load_info_comments(state, &pnewbuf)) 1150 return 1; 1151 1152 if (current->extr_comments) 1153 if (load_info_extr_comments(state, &pnewbuf)) 1154 return 1; 1155 1156 if (current->ref_comments) 1157 if (load_info_ref_comments(state, &pnewbuf)) 1158 return 1; 1159 1160 state->info_buffer = newbuf; 1161 return 0; 1162 1163 load_info_alloc_error: 1164 state->error = ERR_CANT_ALLOC; 1165 return 1; 1166 } 1167 1168 static int 1169 load_info_comments(struct DrawState* state, struct BufferLine** lines) 1170 { 1171 struct PoEntry* current = NULL; 1172 uint32_t* comment = NULL; 1173 uint32_t* ptext = NULL; 1174 ssize_t len, llen, size, i; 1175 1176 current = &state->entries[state->msgid_number - 1]; 1177 ptext = u32_u8_memccpy((*lines)->text, "Comments:", 0, LINE_DEFAULT); 1178 (*lines)->length = llen 1179 = ptext ? ptext - (*lines)->text : LINE_DEFAULT - 1; 1180 (*lines)->size = LINE_DEFAULT; 1181 if (llen > state->info_maxlen) 1182 state->info_maxlen = llen; 1183 (*lines)->fg = colors[C_EDIT_MSGID_HEADING].fg; 1184 (*lines)->bg = colors[C_EDIT_MSGID_HEADING].bg; 1185 (*lines)++; 1186 for (i = 0; i < current->comment_lines; i++) 1187 { 1188 comment = current->comments[i]; 1189 len = u32_strlen(comment); 1190 size = MAX(len + 2, LINE_DEFAULT); /* + NUL */ 1191 ENSURE_SIZE((*lines)->text, ptext, (*lines)->size, size, size, 1192 load_info_comments_alloc_error, uint32_t); 1193 U32_MEMCCPY((*lines)->text, comment, len + 1, ptext); 1194 llen = ptext ? ptext - (*lines)->text : (*lines)->size - 1; 1195 (*lines)->length = llen; 1196 if (llen > state->info_maxlen) 1197 state->info_maxlen = llen; 1198 (*lines)++; 1199 } 1200 return 0; 1201 1202 load_info_comments_alloc_error: 1203 state->error = ERR_CANT_ALLOC; 1204 return 1; 1205 } 1206 1207 static int 1208 load_info_extr_comments(struct DrawState* state, struct BufferLine** lines) 1209 { 1210 struct PoEntry* current = NULL; 1211 uint32_t* extr_comment = NULL; 1212 uint32_t* ptext = NULL; 1213 ssize_t len, llen, size, i; 1214 1215 current = &state->entries[state->msgid_number - 1]; 1216 ptext = u32_u8_memccpy((*lines)->text, "Translation comments:", 0, 1217 LINE_DEFAULT); 1218 (*lines)->length = llen = ptext ? ptext - (*lines)->text : LINE_DEFAULT; 1219 (*lines)->size = MAX(llen + 1, LINE_DEFAULT); 1220 if (llen > state->info_maxlen) 1221 state->info_maxlen = llen; 1222 (*lines)->fg = colors[C_EDIT_MSGID_HEADING].fg; 1223 (*lines)->bg = colors[C_EDIT_MSGID_HEADING].bg; 1224 (*lines)++; 1225 for (i = 0; i < current->extr_comment_lines; i++) 1226 { 1227 extr_comment = current->extr_comments[i]; 1228 len = u32_strlen(extr_comment); 1229 size = MAX(len + 2, LINE_DEFAULT); /* + NUL */ 1230 ENSURE_SIZE((*lines)->text, ptext, (*lines)->size, size, size, 1231 load_info_extr_comments_alloc_error, uint32_t); 1232 U32_MEMCCPY((*lines)->text, extr_comment, len + 1, ptext); 1233 /*len = ptext ? ptext - (*lines)->text : (*lines)->size - 1;*/ 1234 (*lines)->length = llen; 1235 if (llen > state->info_maxlen) 1236 state->info_maxlen = llen; 1237 (*lines)++; 1238 } 1239 return 0; 1240 1241 load_info_extr_comments_alloc_error: 1242 state->error = ERR_CANT_ALLOC; 1243 return 1; 1244 } 1245 1246 static int 1247 load_info_msgid(struct DrawState* state, struct BufferLine** lines) 1248 { 1249 struct PoEntry* current = NULL; 1250 const uint32_t* pumsgid = NULL; 1251 const uint32_t* old_pumsgid = NULL; 1252 uint32_t* ptext = NULL; 1253 ssize_t len, llen, size; 1254 1255 current = &state->entries[state->msgid_number - 1]; 1256 ptext = u32_u8_memccpy((*lines)->text, "msgid:", 0, LINE_DEFAULT); 1257 (*lines)->length = llen = ptext ? ptext - (*lines)->text : LINE_DEFAULT; 1258 if (llen > state->info_maxlen) 1259 state->info_maxlen = llen; 1260 (*lines)->fg = colors[C_EDIT_MSGID_HEADING].fg; 1261 (*lines)->bg = colors[C_EDIT_MSGID_HEADING].bg; 1262 (*lines)++; 1263 pumsgid = current->msgid; 1264 while (*pumsgid) 1265 { 1266 old_pumsgid = pumsgid; 1267 len = u32_next_line(current->msgid, &pumsgid, 0, 0); 1268 size = MAX(len + 2, LINE_DEFAULT); /* + NUL */ 1269 ENSURE_SIZE((*lines)->text, ptext, (*lines)->size, size, size, 1270 load_info_msgid_alloc_error, uint32_t); 1271 U32_MEMCCPY((*lines)->text, old_pumsgid, len + 1, ptext); 1272 llen = u32_decode_tabs(state, (*lines)->text, len + 1); 1273 (*lines)->length = llen; 1274 if (llen > state->info_maxlen) 1275 state->info_maxlen = llen; 1276 (*lines)++; 1277 } 1278 return 0; 1279 1280 load_info_msgid_alloc_error: 1281 state->error = ERR_CANT_ALLOC; 1282 return 1; 1283 } 1284 1285 static int 1286 load_info_msgid_plural(struct DrawState* state, struct BufferLine** lines) 1287 { 1288 struct PoEntry* current = NULL; 1289 struct PoEntry* first = NULL; 1290 const uint32_t* old_pumsgid = NULL; 1291 const uint32_t* pumsgid = NULL; 1292 uint32_t* ptext = NULL; 1293 ssize_t len, llen, size; 1294 1295 first = &state->entries[0]; 1296 current = &state->entries[state->msgid_number - 1]; 1297 ptext = u32_u8_memccpy((*lines)->text, "msgid_plural:", 0, LINE_DEFAULT); 1298 (*lines)->length = llen = ptext ? ptext - (*lines)->text : LINE_DEFAULT; 1299 if (llen > state->info_maxlen) 1300 state->info_maxlen = llen; 1301 (*lines)->fg = colors[C_EDIT_MSGID_HEADING].fg; 1302 (*lines)->bg = colors[C_EDIT_MSGID_HEADING].bg; 1303 (*lines)++; 1304 pumsgid = current->msgid_plural; 1305 while (*pumsgid) 1306 { 1307 old_pumsgid = pumsgid; 1308 len = u32_next_line(current->msgid_plural, &pumsgid, 0, 0); 1309 size = MAX(len + 2, LINE_DEFAULT); 1310 ENSURE_SIZE((*lines)->text, ptext, (*lines)->size, size, size, 1311 load_info_msgid_plural_alloc_error, uint32_t); 1312 U32_MEMCCPY((*lines)->text, old_pumsgid, len + 1, ptext); 1313 llen = u32_decode_tabs(state, (*lines)->text, size); 1314 (*lines)->length = llen; 1315 if (llen > state->info_maxlen) 1316 state->info_maxlen = llen; 1317 (*lines)++; 1318 } 1319 if (first->plural_forms) 1320 { 1321 ptext = u32_u8_memccpy((*lines)->text, "Plural forms:", 0, 1322 LINE_DEFAULT); 1323 (*lines)->length = llen 1324 = ptext ? ptext - (*lines)->text : LINE_DEFAULT - 1; 1325 (*lines)->size = LINE_DEFAULT; 1326 if (llen > state->info_maxlen) 1327 state->info_maxlen = llen; 1328 (*lines)->fg = colors[C_EDIT_MSGID_HEADING].fg; 1329 (*lines)->bg = colors[C_EDIT_MSGID_HEADING].bg; 1330 (*lines)++; 1331 U32_SAFE_COPY((*lines)->text, ptext, (*lines)->size, 1332 first->plural_forms, first->plural_forms_size, 1333 load_info_msgid_plural_alloc_error); 1334 (*lines)->length = llen 1335 = ptext ? ptext - (*lines)->text : (*lines)->size - 1; 1336 //(*lines)->size = MAX(llen + 1, LINE_DEFAULT); 1337 if (llen > state->info_maxlen) 1338 state->info_maxlen = llen; 1339 (*lines)++; 1340 } 1341 return 0; 1342 1343 load_info_msgid_plural_alloc_error: 1344 state->error = ERR_CANT_ALLOC; 1345 return 1; 1346 } 1347 1348 static int 1349 load_info_ref_comments(struct DrawState* state, struct BufferLine** lines) 1350 { 1351 struct PoEntry* current = NULL; 1352 uint32_t* ptext = NULL; 1353 char* ref_comment = NULL; 1354 ssize_t len, llen, size, i; 1355 1356 current = &state->entries[state->msgid_number - 1]; 1357 ptext = u32_u8_memccpy((*lines)->text, "References:", 0, LINE_DEFAULT); 1358 (*lines)->length = llen = ptext ? ptext - (*lines)->text : LINE_DEFAULT; 1359 (*lines)->size = MAX(llen + 1, LINE_DEFAULT); 1360 if (llen > state->info_maxlen) 1361 state->info_maxlen = llen; 1362 (*lines)->fg = colors[C_EDIT_MSGID_HEADING].fg; 1363 (*lines)->bg = colors[C_EDIT_MSGID_HEADING].bg; 1364 (*lines)++; 1365 for (i = 0; i < current->ref_comment_lines; i++) 1366 { 1367 ref_comment = current->ref_comments[i]; 1368 len = strlen(ref_comment); 1369 size = MAX(len + 2, LINE_DEFAULT); /* + NUL */ 1370 ENSURE_SIZE((*lines)->text, ptext, (*lines)->size, size, size, 1371 load_info_ref_comments_alloc_error, uint32_t); 1372 U32_U8_MEMCCPY((*lines)->text, ref_comment, len + 1, ptext); 1373 llen = ptext ? ptext - (*lines)->text : LINE_DEFAULT; 1374 (*lines)->length = llen; 1375 if (llen > state->info_maxlen) 1376 state->info_maxlen = llen; 1377 (*lines)++; 1378 } 1379 return 0; 1380 1381 load_info_ref_comments_alloc_error: 1382 state->error = ERR_CANT_ALLOC; 1383 return 1; 1384 } 1385 1386 static int 1387 load_msgstr(struct DrawState* state) 1388 { 1389 struct PoEntry* current; 1390 struct BufferLine* newbuf; 1391 struct BufferLine* pnewbuf; 1392 const uint32_t* old_pumsgstr = NULL; 1393 const uint32_t* pumsgstr = NULL; 1394 uint32_t* newtext = NULL; 1395 uint32_t* temp = NULL; 1396 ssize_t num_lines, i, len, newlen, size, row; 1397 int msgstr_index; 1398 1399 assert((state != NULL) && (state->entries != NULL)); 1400 current = &state->entries[state->msgid_number - 1]; 1401 newbuf = NULL; 1402 pnewbuf = NULL; 1403 msgstr_index = state->msgstr_index; 1404 num_lines = current->msgstr_count == 0 1405 ? 1 1406 : u32_lines_in_string(current->msgstr[msgstr_index], 0, 0); 1407 1408 for (row = 0; row < state->input_rows_count; row++) 1409 free_bufferline(&state->input_buffer[row]); 1410 free(state->input_buffer); 1411 1412 newbuf = calloc(num_lines, sizeof(struct BufferLine)); 1413 if (!newbuf) 1414 goto load_msgstr_alloc_error; 1415 1416 pnewbuf = newbuf; 1417 for (i = 0; i < num_lines; i++) 1418 { 1419 init_bufferline(pnewbuf); 1420 assert(current->msgstr_size != NULL); 1421 size = MAX(current->msgstr_size[msgstr_index], LINE_DEFAULT); 1422 newtext = calloc(size, sizeof(uint32_t)); 1423 if (!newtext) 1424 goto load_msgstr_alloc_error; 1425 pnewbuf->text = newtext; 1426 pnewbuf->size = size; 1427 pnewbuf->length = 0; 1428 pnewbuf++; 1429 } 1430 state->input_rows_count = num_lines; 1431 1432 if (current->msgstr_count > 0) 1433 { 1434 pumsgstr = current->msgstr[msgstr_index]; 1435 if (!pumsgstr) 1436 { 1437 newbuf[0].length = 0; 1438 goto load_msgstr_done; 1439 } 1440 pnewbuf = newbuf; 1441 while (*pumsgstr) 1442 { 1443 old_pumsgstr = pumsgstr; 1444 len = u32_next_line(current->msgstr[msgstr_index], 1445 &pumsgstr, 0, 0); 1446 size = MAX(len + 2, LINE_DEFAULT); /* + NUL */ 1447 ENSURE_SIZE(pnewbuf->text, temp, pnewbuf->size, size, 1448 size, load_msgstr_alloc_error, uint32_t); 1449 U32_MEMCCPY(pnewbuf->text, old_pumsgstr, len + 1, temp); 1450 newlen = u32_decode_tabs(state, pnewbuf->text, len + 1); 1451 pnewbuf->length = newlen; 1452 pnewbuf++; 1453 } 1454 } 1455 else 1456 newbuf[0].length = 0; 1457 1458 load_msgstr_done: 1459 state->input_buffer = newbuf; 1460 return 0; 1461 1462 load_msgstr_alloc_error: 1463 state->error = ERR_CANT_ALLOC; 1464 return 1; 1465 } 1466 1467 static int 1468 move_down(struct DrawState* state) 1469 { 1470 assert(state != NULL); 1471 if (state->edit_info_focused) 1472 { 1473 if (state->info_first_shown.row + (EDIT_HEIGHT - 3) / 2 1474 < state->info_rows_count) 1475 state->info_first_shown.row++; 1476 return 0; 1477 } 1478 position_to(state, state->coord.row + 1, state->coord.col); 1479 return 0; 1480 } 1481 1482 static int 1483 move_end(struct DrawState* state) 1484 { 1485 assert((state != NULL) && (state->input_buffer != NULL)); 1486 if (state->edit_info_focused) 1487 { 1488 if (state->info_maxlen < state->edit_width - 3) 1489 state->info_first_shown.col = 0; 1490 else 1491 state->info_first_shown.col 1492 = state->info_maxlen - state->edit_width + 3; 1493 return 0; 1494 } 1495 position_to(state, state->coord.row, 1496 state->input_buffer[state->coord.row].length); 1497 return 0; 1498 } 1499 1500 static int 1501 move_left(struct DrawState* state) 1502 { 1503 assert((state != NULL) && (state->input_buffer != NULL)); 1504 if (state->edit_info_focused) 1505 { 1506 if (state->info_first_shown.col > 0) 1507 state->info_first_shown.col--; 1508 return 0; 1509 } 1510 position_to(state, state->coord.row, state->coord.col - 1); 1511 return 0; 1512 } 1513 1514 static int 1515 move_list_down(struct DrawState* state) 1516 { 1517 assert(state != NULL); 1518 goto_msgid(state, next_msgid(state, 1)); 1519 return 0; 1520 } 1521 1522 static int 1523 move_list_end(struct DrawState* state) 1524 { 1525 assert(state != NULL); 1526 goto_msgid(state, 1527 next_msgid(state, 1528 state->real_msgid_count - state->msgid_number)); 1529 return 0; 1530 } 1531 1532 static int 1533 move_list_page_down(struct DrawState* state) 1534 { 1535 assert(state != NULL); 1536 state->error = ERR_NONE; 1537 goto_msgid(state, next_msgid(state, state->maxy - 2)); 1538 return 0; 1539 } 1540 1541 static int 1542 move_list_page_up(struct DrawState* state) 1543 { 1544 assert(state != NULL); 1545 state->error = ERR_NONE; 1546 goto_msgid(state, prev_msgid(state, state->maxy - 2)); 1547 return 0; 1548 } 1549 1550 static int 1551 move_list_start(struct DrawState* state) 1552 { 1553 assert(state != NULL); 1554 goto_msgid(state, 1); 1555 return 0; 1556 } 1557 1558 static int 1559 move_list_up(struct DrawState* state) 1560 { 1561 assert(state != NULL); 1562 goto_msgid(state, prev_msgid(state, 1)); 1563 return 0; 1564 } 1565 1566 static int 1567 move_next_word(struct DrawState* state) 1568 { 1569 uint32_t* pline; 1570 ssize_t len; 1571 int i; 1572 1573 assert(state != NULL); 1574 if (state->edit_info_focused) 1575 { 1576 state->error = ERR_DLG_FOCUS_EDIT; 1577 return 1; 1578 } 1579 1580 i = state->coord.col; 1581 pline = state->input_buffer[state->coord.row].text; 1582 len = state->input_buffer[state->coord.row].length; 1583 1584 while (i < len && !u32_is_word_boundary(pline[i], 0)) 1585 i++; 1586 while (i < len && u32_is_word_boundary(pline[i], 0)) 1587 i++; 1588 1589 position_to(state, state->coord.row, i); 1590 return 0; 1591 } 1592 1593 static int 1594 move_prev_word(struct DrawState* state) 1595 { 1596 uint32_t* pline; 1597 int i; 1598 1599 assert((state != NULL) && (state->input_buffer != NULL)); 1600 if (state->edit_info_focused) 1601 { 1602 state->error = ERR_DLG_FOCUS_EDIT; 1603 return 1; 1604 } 1605 1606 i = state->coord.col; 1607 pline = state->input_buffer[state->coord.row].text; 1608 1609 if (i > 0 && u32_is_word_boundary(pline[i - 1], 0)) 1610 i--; 1611 while (i > 0 && u32_is_word_boundary(pline[i], 0)) 1612 i--; 1613 while (i > 0 && !u32_is_word_boundary(pline[i], 0) 1614 && !u32_is_word_boundary(pline[i - 1], 0)) 1615 i--; 1616 1617 position_to(state, state->coord.row, i); 1618 return 0; 1619 } 1620 1621 static int 1622 move_right(struct DrawState* state) 1623 { 1624 assert(state != NULL); 1625 if (state->edit_info_focused) 1626 { 1627 if (state->info_first_shown.col + state->edit_width - 3 1628 < state->info_maxlen + 1) 1629 state->info_first_shown.col++; 1630 return 0; 1631 } 1632 position_to(state, state->coord.row, state->coord.col + 1); 1633 return 0; 1634 } 1635 1636 static int 1637 move_search_end(struct DrawState* state) 1638 { 1639 assert(state != NULL); 1640 search_position_to(state, u32_strlen(state->search)); 1641 return 0; 1642 } 1643 1644 static int 1645 move_search_left(struct DrawState* state) 1646 { 1647 assert((state != NULL) && (state->search != NULL)); 1648 if (state->search_column == 0) 1649 return 1; 1650 search_position_to(state, state->search_column - 1); 1651 return 0; 1652 } 1653 1654 static int 1655 move_search_next_word(struct DrawState* state) 1656 { 1657 uint32_t* pline = NULL; 1658 ssize_t len; 1659 int i; 1660 1661 assert((state != NULL) && (state->search != NULL)); 1662 i = state->search_column; 1663 pline = state->search; 1664 len = u32_strlen(state->search); 1665 while (i < len && !u32_is_word_boundary(pline[i], 0)) 1666 i++; 1667 while (i < len && u32_is_word_boundary(pline[i], 0)) 1668 i++; 1669 search_position_to(state, i); 1670 return 0; 1671 } 1672 1673 static int 1674 move_search_prev_word(struct DrawState* state) 1675 { 1676 uint32_t* pline = NULL; 1677 int i; 1678 1679 assert((state != NULL) && (state->search != NULL)); 1680 i = state->search_column; 1681 pline = state->search; 1682 if (i > 0 && u32_is_word_boundary(pline[i - 1], 0)) 1683 i--; 1684 while (i > 0 && u32_is_word_boundary(pline[i], 0)) 1685 i--; 1686 while (i > 0 && !u32_is_word_boundary(pline[i], 0) 1687 && !u32_is_word_boundary(pline[i - 1], 0)) 1688 i--; 1689 search_position_to(state, i); 1690 return 0; 1691 } 1692 1693 static int 1694 move_search_right(struct DrawState* state) 1695 { 1696 assert(state != NULL); 1697 search_position_to(state, state->search_display_column + 1); 1698 return 0; 1699 } 1700 1701 static int 1702 move_search_start(struct DrawState* state) 1703 { 1704 assert(state != NULL); 1705 search_position_to(state, 0); 1706 return 0; 1707 } 1708 1709 static int 1710 move_start(struct DrawState* state) 1711 { 1712 assert(state != NULL); 1713 if (state->edit_info_focused) 1714 { 1715 state->info_first_shown.row = 0; 1716 state->info_first_shown.col = 0; 1717 return 0; 1718 } 1719 position_to(state, state->coord.row, 0); 1720 return 0; 1721 } 1722 1723 static int 1724 move_up(struct DrawState* state) 1725 { 1726 assert(state != NULL); 1727 if (state->edit_info_focused) 1728 { 1729 if (state->info_first_shown.row > 0) 1730 state->info_first_shown.row--; 1731 return 0; 1732 } 1733 position_to(state, state->coord.row > 1 ? state->coord.row - 1 : 0, 1734 state->coord.col); 1735 return 0; 1736 } 1737 1738 static int 1739 next_fuzzy(struct DrawState* state) 1740 { 1741 struct PoEntry* current = NULL; 1742 char* temp = NULL; 1743 ssize_t e; 1744 1745 assert(state != NULL); 1746 if (state->msgid_count == 0) 1747 return 1; 1748 1749 if (state->edit_dirty) 1750 { 1751 state->in_prompt = 1; 1752 MEMCCPY(state->prompt, prompt_edit_dirty, LINE_DEFAULT, temp); 1753 state->prompt_callback = next_fuzzy_callback; 1754 return 0; 1755 } 1756 1757 for (e = state->msgid_number; e < state->msgid_count; e++) 1758 { 1759 current = &state->entries[e]; 1760 if (current->flags & FL_FUZZY) 1761 { 1762 goto_msgid(state, e + 1); 1763 if (state->show_edit) 1764 { 1765 state->msgstr_index = 0; 1766 show_edit(state); 1767 } 1768 return 0; 1769 } 1770 } 1771 return 0; 1772 } 1773 1774 static int 1775 next_fuzzy_callback(struct DrawState* state, struct tb_event* ev) 1776 { 1777 assert((state != NULL) && (ev != NULL)); 1778 if (strchr("Yy", ev->ch)) 1779 { 1780 state->reshow_edit = 1; 1781 save_msgstr(state); 1782 } 1783 else 1784 state->edit_dirty = 0; 1785 return next_fuzzy(state); 1786 } 1787 1788 static int 1789 next_match(struct DrawState* state) 1790 { 1791 struct PoEntry* current = NULL; 1792 ssize_t e; 1793 1794 assert(state != NULL); 1795 if (state->msgid_count == 0 || !state->search || !*state->search) 1796 return 1; 1797 1798 for (e = state->msgid_number; e < state->real_msgid_count; e++) 1799 { 1800 current = state->entries + e; 1801 if (current->obsolete) 1802 continue; 1803 if ((current->msgid && *current->msgid 1804 && u32_strstr(current->msgid, state->search)) 1805 || (current->msgstr && *current->msgstr 1806 && **current->msgstr 1807 && u32_strstr(*current->msgstr, state->search))) 1808 { 1809 goto_msgid(state, e + 1); 1810 return 0; 1811 } 1812 } 1813 return 0; 1814 } 1815 1816 static int 1817 next_plural(struct DrawState* state) 1818 { 1819 struct PoEntry* current = NULL; 1820 char* temp = NULL; 1821 assert((state != NULL) && (state->entries != NULL)); 1822 if (state->edit_info_focused) 1823 { 1824 state->error = ERR_DLG_FOCUS_EDIT; 1825 return 1; 1826 } 1827 if (state->edit_dirty) 1828 { 1829 state->in_prompt = 1; 1830 MEMCCPY(state->prompt, prompt_edit_dirty, LINE_DEFAULT, temp); 1831 state->prompt_callback = next_plural_callback; 1832 return 0; 1833 } 1834 current = &state->entries[state->msgid_number - 1]; 1835 if (state->msgstr_index + 1 < current->msgstr_count) 1836 state->msgstr_index++; 1837 show_edit(state); 1838 return 0; 1839 } 1840 1841 static int 1842 next_plural_callback(struct DrawState* state, struct tb_event* ev) 1843 { 1844 assert((state != NULL) && (ev != NULL)); 1845 if (strchr("Yy", ev->ch)) 1846 { 1847 state->reshow_edit = 1; 1848 save_msgstr(state); 1849 } 1850 else 1851 state->edit_dirty = 0; 1852 return next_plural(state); 1853 } 1854 1855 static int 1856 next_untranslated(struct DrawState* state) 1857 { 1858 struct PoEntry* current = NULL; 1859 char* temp = NULL; 1860 ssize_t e; 1861 1862 assert((state != NULL) && (state->entries != NULL)); 1863 if (state->msgid_count == 0) 1864 return 1; 1865 1866 if (state->edit_dirty) 1867 { 1868 state->in_prompt = 1; 1869 MEMCCPY(state->prompt, prompt_edit_dirty, LINE_DEFAULT, temp); 1870 state->prompt_callback = next_untranslated_callback; 1871 return 0; 1872 } 1873 1874 for (e = state->msgid_number; e < state->msgid_count; e++) 1875 { 1876 current = state->entries + e; 1877 if (!current->msgstr || !*current->msgstr 1878 || u32_strlen(*current->msgstr) == 0) 1879 { 1880 goto_msgid(state, e + 1); 1881 if (state->show_edit) 1882 { 1883 state->msgstr_index = 0; 1884 show_edit(state); 1885 } 1886 return 0; 1887 } 1888 } 1889 return 0; 1890 } 1891 1892 static int 1893 next_untranslated_callback(struct DrawState* state, struct tb_event* ev) 1894 { 1895 assert((state != NULL) && (ev != NULL)); 1896 if (strchr("Yy", ev->ch)) 1897 { 1898 state->reshow_edit = 1; 1899 save_msgstr(state); 1900 } 1901 else 1902 state->edit_dirty = 0; 1903 return next_untranslated(state); 1904 } 1905 1906 static size_t 1907 next_msgid(struct DrawState* state, const int delta) 1908 { 1909 size_t ret, start, last_non_obsolete; 1910 assert((state != NULL) && (state->entries != NULL)); 1911 ret = state->msgid_number; 1912 start = ret; 1913 last_non_obsolete 1914 = state->real_msgid_count - state->obsolete_at_end_count; 1915 while (ret < last_non_obsolete + 1) 1916 { 1917 ret++; 1918 if (!state->entries[ret - 1].obsolete && ret >= start + delta) 1919 break; 1920 } 1921 return ret == last_non_obsolete + 1 ? last_non_obsolete : ret; 1922 } 1923 1924 static int 1925 overwrite_copy_msgid_callback(struct DrawState* state, struct tb_event* ev) 1926 { 1927 assert((state != NULL) && (ev != NULL)); 1928 if (ev->ch == 'y') 1929 copy_msgid_to_input(state); 1930 state->prompt_callback = NULL; 1931 return 0; 1932 } 1933 1934 static int 1935 overwrite_paste_callback(struct DrawState* state, struct tb_event* ev) 1936 { 1937 assert((state != NULL) && (ev != NULL)); 1938 if (ev->ch == 'y') 1939 paste_paste_buffer_to_input(state); 1940 state->prompt_callback = NULL; 1941 return 0; 1942 } 1943 1944 static int 1945 paste_paste_buffer_to_input(struct DrawState* state) 1946 { 1947 struct BufferLine* pinput = NULL; 1948 struct BufferLine* newbuf = NULL; 1949 struct BufferLine* ppaste = NULL; 1950 uint32_t* newtext = NULL; 1951 uint32_t* utemp = NULL; 1952 char* temp = NULL; 1953 ssize_t lines_to_init = 0; 1954 ssize_t lines_to_free = 0; 1955 ssize_t i; 1956 1957 assert(state != NULL); 1958 if (state->edit_info_focused) 1959 { 1960 state->error = ERR_DLG_FOCUS_EDIT; 1961 return 1; 1962 } 1963 1964 assert(state->input_buffer != NULL); 1965 if (!state->in_prompt && *state->input_buffer->text) 1966 { 1967 state->in_prompt = 1; 1968 MEMCCPY(state->prompt, prompt_overwrite, LINE_DEFAULT, temp); 1969 state->prompt_callback = overwrite_paste_callback; 1970 return 0; 1971 } 1972 1973 if (state->msgid_number == 1) 1974 { 1975 state->error = ERR_ILLEGAL_ON_FIRST; 1976 return 1; 1977 } 1978 1979 if (!state->paste || !(*state->paste).text || !*(*state->paste).text) 1980 { 1981 /* leave one row */ 1982 for (i = 1; i < state->input_rows_count; i++) 1983 free_bufferline(state->input_buffer + i); 1984 state->input_buffer = realloc(state->input_buffer, 1985 sizeof(struct BufferLine)); 1986 if (!state->input_buffer) 1987 goto paste_paste_buffer_alloc_error; 1988 *(state->input_buffer->text) = 0; 1989 init_coord(&state->coord, 0, 0); 1990 state->input_display_column = 0; 1991 init_coord(&state->input_first_shown, 0, 0); 1992 state->input_rows_count = 1; 1993 state->edit_dirty = 1; 1994 return 0; 1995 } 1996 1997 lines_to_init = state->input_rows_count < state->paste_count 1998 ? state->paste_count - state->input_rows_count 1999 : 0; 2000 lines_to_free = state->input_rows_count > state->paste_count 2001 ? state->input_rows_count - state->paste_count 2002 : 0; 2003 2004 for (i = 0; i < lines_to_free; i++) 2005 free_bufferline( 2006 state->input_buffer + state->input_rows_count - i - 1); 2007 newbuf = realloc(state->input_buffer, 2008 state->paste_count * sizeof(struct BufferLine)); 2009 ppaste = state->paste; 2010 2011 if (!newbuf) 2012 goto paste_paste_buffer_alloc_error; 2013 2014 for (i = 0; i < lines_to_init; i++) 2015 { 2016 pinput = newbuf + state->paste_count - 1 - i; 2017 init_bufferline(pinput); 2018 newtext = calloc(LINE_DEFAULT, sizeof(uint32_t)); 2019 if (!newtext) 2020 goto paste_paste_buffer_alloc_error; 2021 pinput->text = newtext; 2022 } 2023 2024 if (ppaste) 2025 for (i = 0; i < state->paste_count; i++) 2026 { 2027 pinput = newbuf + i; 2028 U32_MEMCCPY(pinput->text, ppaste->text, 2029 MIN(ppaste->length + 1, LINE_DEFAULT), utemp); 2030 pinput->length = ppaste->length; 2031 ppaste++; 2032 } 2033 2034 state->input_rows_count = state->paste_count; 2035 state->input_buffer = newbuf; 2036 state->edit_dirty = 1; 2037 return 0; 2038 2039 paste_paste_buffer_alloc_error: 2040 state->error = ERR_CANT_ALLOC; 2041 return 1; 2042 } 2043 2044 /* 2045 * state->saved_column - display coordinate 2046 * row, column - buffer coordinates (not display) 2047 */ 2048 static void 2049 position_to(struct DrawState* state, const int row, const int column) 2050 { 2051 ssize_t dlen; 2052 2053 assert((state != NULL) && (state->input_buffer != NULL)); 2054 if (row != state->coord.row) 2055 { 2056 state->coord.row = row; 2057 if ((state->coord.row + 1) > state->input_rows_count) 2058 state->coord.row = state->input_rows_count - 1; 2059 if (state->coord.row < state->input_first_shown.row) 2060 state->input_first_shown.row = state->coord.row; 2061 if (state->input_first_shown.row + (EDIT_HEIGHT - 3) / 2 2062 < state->coord.row) 2063 state->input_first_shown.row 2064 = state->coord.row - (EDIT_HEIGHT - 3) / 2; 2065 if (state->saved_column == -1) 2066 { 2067 state->saved_column = state->input_display_column; 2068 state->coord.col = column; 2069 } 2070 else 2071 state->coord.col = buffer_length( 2072 state->input_buffer[state->coord.row].text, 2073 state->saved_column); 2074 if (state->coord.col < 0) 2075 state->coord.col = 0; 2076 else if (state->coord.col 2077 > (int)state->input_buffer[state->coord.row].length) 2078 state->coord.col 2079 = state->input_buffer[state->coord.row].length; 2080 dlen = display_length(state->input_buffer[state->coord.row].text, 2081 state->coord.col); 2082 if (dlen < state->saved_column) 2083 state->input_display_column = dlen; 2084 else 2085 state->input_display_column = state->saved_column; 2086 } 2087 else 2088 { 2089 state->coord.col = column; 2090 if (state->coord.col < 0) 2091 { 2092 if (state->coord.row > 0) 2093 { 2094 state->coord.row--; 2095 if (state->input_first_shown.row > 0 2096 && state->input_first_shown.row 2097 > state->coord.row) 2098 state->input_first_shown.row--; 2099 state->coord.col 2100 = state->input_buffer[state->coord.row] 2101 .length; 2102 } 2103 else 2104 state->coord.col = 0; 2105 } 2106 else if (state->coord.col 2107 > state->input_buffer[state->coord.row].length) 2108 { 2109 if ((state->coord.row + 1) < state->input_rows_count) 2110 { 2111 state->coord.row++; 2112 if (state->input_first_shown.row 2113 + (state->maxy - 2) 2114 < state->coord.row 2115 && state->input_first_shown.row + 1 2116 < state->input_rows_count) 2117 state->input_first_shown.row++; 2118 state->coord.col = 0; 2119 } 2120 else 2121 state->coord.col 2122 = state->input_buffer[state->coord.row] 2123 .length; 2124 } 2125 state->input_display_column 2126 = display_length(state->input_buffer[state->coord.row] 2127 .text, 2128 state->coord.col); 2129 state->saved_column = state->input_display_column; 2130 } 2131 if (state->input_first_shown.col > state->input_display_column) 2132 state->input_first_shown.col = state->input_display_column; 2133 if (state->input_first_shown.col + state->edit_width - 4 2134 < state->input_display_column) 2135 { 2136 int index_first; 2137 int fsc = state->input_first_shown.col; 2138 index_first 2139 = buffer_length(state->input_buffer[state->coord.row] 2140 .text, 2141 fsc); 2142 while (fsc + state->edit_width < state->input_display_column + 4) 2143 { 2144 index_first++; 2145 fsc = display_length(state->input_buffer[state->coord.row] 2146 .text, 2147 index_first); 2148 } 2149 state->input_first_shown.col = fsc; 2150 } 2151 } 2152 2153 static int 2154 prev_match(struct DrawState* state) 2155 { 2156 struct PoEntry* current = NULL; 2157 ssize_t e; 2158 2159 assert(state != NULL); 2160 if (state->msgid_count == 0 || !state->search || !*state->search) 2161 return 1; 2162 for (e = state->msgid_number - 1; e > 0; e--) 2163 { 2164 current = state->entries + e - 1; 2165 if ((current->msgid && *current->msgid 2166 && u32_strstr(current->msgid, state->search)) 2167 || (current->msgstr && *current->msgstr 2168 && **current->msgstr 2169 && u32_strstr(*current->msgstr, state->search))) 2170 { 2171 goto_msgid(state, e); 2172 return 0; 2173 } 2174 } 2175 return 0; 2176 } 2177 2178 static size_t 2179 prev_msgid(struct DrawState* state, const int delta) 2180 { 2181 ssize_t ret = state->msgid_number; 2182 ssize_t start = ret; 2183 2184 assert((state != NULL) && (state->entries != NULL)); 2185 if (start < delta) 2186 return state->real_msgid_count ? 1 : 0; 2187 while (ret > 1) 2188 { 2189 ret--; 2190 if (!state->entries[ret - 1].obsolete && ret <= start - delta) 2191 break; 2192 } 2193 return ret; 2194 } 2195 2196 static int 2197 prev_plural(struct DrawState* state) 2198 { 2199 char* temp = NULL; 2200 assert(state != NULL); 2201 if (state->edit_info_focused) 2202 { 2203 state->error = ERR_DLG_FOCUS_EDIT; 2204 return 1; 2205 } 2206 if (state->edit_dirty) 2207 { 2208 state->in_prompt = 1; 2209 MEMCCPY(state->prompt, prompt_edit_dirty, LINE_DEFAULT, temp); 2210 state->prompt_callback = prev_plural_callback; 2211 return 0; 2212 } 2213 if (state->msgstr_index > 0) 2214 state->msgstr_index--; 2215 show_edit(state); 2216 return 0; 2217 } 2218 2219 static int 2220 prev_plural_callback(struct DrawState* state, struct tb_event* ev) 2221 { 2222 assert((state != NULL) && (ev != NULL)); 2223 if (strchr("Yy", ev->ch)) 2224 { 2225 state->reshow_edit = 1; 2226 save_msgstr(state); 2227 } 2228 else 2229 state->edit_dirty = 0; 2230 return prev_plural(state); 2231 } 2232 2233 int 2234 print_error(const int code, const char* msg, ...) 2235 { 2236 char* buf = NULL; 2237 va_list args; 2238 long bufsize; 2239 assert((msg != NULL) && (limits != NULL)); 2240 bufsize = limits[L_LINE_MAX] ? limits[L_LINE_MAX] 2241 : PRINT_BUF_MAX_FALLBACK; 2242 buf = malloc(bufsize); 2243 if (!buf) 2244 return ERR_CANT_ALLOC; 2245 va_start(args, msg); 2246 if (vsnprintf(buf, bufsize, msg, args) >= bufsize) 2247 buf[bufsize - 1] = 0; 2248 va_end(args); 2249 fprintf(stderr, "%s: %s\n", PROGRAM_NAME, buf); 2250 free(buf); 2251 return code; 2252 } 2253 2254 static int 2255 quit_callback(struct DrawState* state, struct tb_event* ev) 2256 { 2257 int result = 0; 2258 assert((state != NULL) && (ev != NULL)); 2259 if (strchr("Yy", ev->ch)) 2260 { 2261 state->reshow_edit = 0; 2262 if (state->edit_dirty) 2263 save_msgstr(state); 2264 result = write_file(state); 2265 } 2266 state->prompt_callback = NULL; 2267 state->running = 0; 2268 return result; 2269 } 2270 2271 int 2272 redraw_status(struct DrawState* state) 2273 { 2274 int result = 0; 2275 assert(state != NULL); 2276 if (state->recalculate_size) 2277 { 2278 recalculate_size(state); 2279 state->recalculate_size = 0; 2280 } 2281 result = draw_status(state); 2282 tb_present(); 2283 return result; 2284 } 2285 2286 static int 2287 save_msgstr(struct DrawState* state) 2288 { 2289 struct BufferLine* pinput_buffer; 2290 struct PoEntry* current = NULL; 2291 uint32_t* to_save = NULL; 2292 uint32_t* temp = NULL; 2293 uint32_t* temp2 = NULL; 2294 size_t to_save_size = 0; 2295 ssize_t i; 2296 2297 assert(state != NULL); 2298 if (state->reshow_edit) 2299 state->reshow_edit = 0; 2300 else 2301 state->show_edit = 0; 2302 state->edit_info_focused = 0; 2303 2304 if (state->input_buffer) 2305 { 2306 to_save_size = state->input_rows_count * MAXMSGLINE; 2307 to_save = calloc(to_save_size, sizeof(uint32_t)); 2308 if (!to_save) 2309 { 2310 state->error = ERR_CANT_ALLOC; 2311 return 1; 2312 } 2313 pinput_buffer = NULL; 2314 for (i = 0; i < state->input_rows_count; i++) 2315 { 2316 pinput_buffer = &state->input_buffer[i]; 2317 if (i == 0) 2318 U32_MEMCCPY(to_save, pinput_buffer->text, 2319 to_save_size * sizeof(uint32_t), temp); 2320 else if (temp) 2321 { 2322 U32_U8_MEMCCPY((temp - 1), "\\n", 2323 to_save_size * sizeof(uint32_t), temp2); 2324 if (temp2) 2325 U32_MEMCCPY((temp2 - 1), 2326 pinput_buffer->text, 2327 to_save_size * sizeof(uint32_t), 2328 temp); 2329 } 2330 free_bufferline(pinput_buffer); 2331 } 2332 } 2333 free(state->input_buffer); 2334 state->input_buffer = NULL; 2335 state->input_rows_count = 0; 2336 assert(state->info_buffer != NULL); 2337 for (i = 0; i < state->info_rows_count; i++) 2338 free_bufferline(&state->info_buffer[i]); 2339 free(state->info_buffer); 2340 state->dirty = 1; 2341 state->edit_dirty = 0; 2342 state->info_rows_count = 0; 2343 state->info_buffer = NULL; 2344 if (u32_encode_tabs(state, to_save, to_save_size * sizeof(uint32_t)) < 0) 2345 goto save_msgstr_error; 2346 current = &state->entries[state->msgid_number - 1]; 2347 if (u32_match_msgid_ending(state, current->msgid, to_save, 2348 to_save_size * sizeof(uint32_t)) 2349 < 0) 2350 goto save_msgstr_error; 2351 if (*current->msgstr[state->msgstr_index] && (!to_save || !*to_save)) 2352 state->untranslated_count++; 2353 else if (!*current->msgstr[state->msgstr_index] && to_save && *to_save) 2354 state->untranslated_count--; 2355 2356 u32_set_msgstr(state, current, to_save, state->msgstr_index, 0); 2357 update_warning(current->msgid, to_save, ¤t->warning); 2358 state->input_changed_fuzzy = UNCHANGED; 2359 2360 save_msgstr_error: 2361 if (state->error == ERR_CANT_ALLOC) 2362 goto save_msgstr_alloc_error; 2363 2364 tb_hide_cursor(); 2365 free(to_save); 2366 return !!state->error; 2367 2368 save_msgstr_alloc_error: 2369 return 1; 2370 } 2371 2372 static int 2373 search_position_to(struct DrawState* state, const int column) 2374 { 2375 ssize_t len; 2376 2377 assert((state != NULL) && (state->search != NULL)); 2378 len = u32_strlen(state->search); 2379 state->search_column = column; 2380 2381 if (state->search_column > len) 2382 state->search_column = len; 2383 state->search_display_column 2384 = display_length(state->search, state->search_column); 2385 if (state->search_first_shown_column > state->search_display_column) 2386 state->search_first_shown_column = state->search_display_column; 2387 if (state->search_first_shown_column + (state->search_width - 4) 2388 < state->search_column) 2389 state->search_first_shown_column = state->search_display_column 2390 - (state->search_width - 4); 2391 return 0; 2392 } 2393 2394 static int 2395 show_edit(struct DrawState* state) 2396 { 2397 assert((state != NULL) && (state->entries != NULL)); 2398 if (state->msgstr_index + 1 2399 > state->entries[state->msgid_number - 1].msgstr_count) 2400 state->msgstr_index = 0; 2401 2402 if (load_msgstr(state) || load_info(state)) 2403 { 2404 state->running = 0; 2405 return 1; 2406 } 2407 2408 state->fuzzy_before_edit = state->fuzzy_count; 2409 state->input_changed_fuzzy = UNCHANGED; 2410 state->show_edit = 1; 2411 state->coord.row = 0; 2412 init_coord(&state->input_first_shown, 0, 0); 2413 init_coord(&state->info_first_shown, 0, 0); 2414 move_start(state); 2415 state->error = ERR_NONE; 2416 return 0; 2417 } 2418 2419 static int 2420 show_help(struct DrawState* state) 2421 { 2422 assert(state != NULL); 2423 state->show_help = 1; 2424 state->error = ERR_NONE; 2425 return 0; 2426 } 2427 2428 static int 2429 show_search(struct DrawState* state) 2430 { 2431 assert(state != NULL); 2432 state->show_search = 1; 2433 state->search_column = 0; 2434 state->search_display_column = 0; 2435 state->search_first_shown_column = 0; 2436 if (!state->search) 2437 state->search = calloc(MAXSEARCH, sizeof(uint32_t)); 2438 *state->search = 0; 2439 tb_set_cursor((state->maxx - state->search_width + 2) / 2, 2440 state->maxy - 1 - SEARCH_HEIGHT + 1); 2441 return 0; 2442 } 2443 2444 static int 2445 toggle_fuzzy(struct DrawState* state) 2446 { 2447 assert((state != NULL) && (state->entries != NULL)); 2448 if (state->msgid_number == 1) 2449 { 2450 state->error = ERR_ILLEGAL_ON_FIRST; 2451 return 0; 2452 } 2453 if (state->msgid_count > 0) 2454 { 2455 state->entries[state->msgid_number - 1].flags ^= FL_FUZZY; 2456 if (state->entries[state->msgid_number - 1].flags & FL_FUZZY) 2457 { 2458 state->fuzzy_count++; 2459 if (state->input_changed_fuzzy == CLEARED) 2460 state->input_changed_fuzzy = UNCHANGED; 2461 else 2462 state->input_changed_fuzzy = SET; 2463 } 2464 else 2465 { 2466 state->fuzzy_count--; 2467 if (state->input_changed_fuzzy == SET) 2468 state->input_changed_fuzzy = UNCHANGED; 2469 else 2470 state->input_changed_fuzzy = CLEARED; 2471 } 2472 state->edit_dirty = 1; 2473 } 2474 return 0; 2475 } 2476 2477 static int 2478 toggle_info_focused(struct DrawState* state) 2479 { 2480 assert(state != NULL); 2481 state->edit_info_focused = !state->edit_info_focused; 2482 return 0; 2483 } 2484 2485 static int 2486 yank_input_to_paste_buffer(struct DrawState* state) 2487 { 2488 struct BufferLine* ppaste = NULL; 2489 struct BufferLine* newbuf = NULL; 2490 struct BufferLine* pinput = NULL; 2491 uint32_t* newtext = NULL; 2492 uint32_t* temp = NULL; 2493 ssize_t lines_to_init = 0; 2494 ssize_t lines_to_free = 0; 2495 ssize_t i; 2496 2497 assert(state != NULL); 2498 if (state->edit_info_focused) 2499 return 1; 2500 2501 if (state->msgid_number == 1) 2502 { 2503 state->error = ERR_ILLEGAL_ON_FIRST; 2504 return 0; 2505 } 2506 2507 if (!state->input_buffer || !(*state->input_buffer).text 2508 || !*(*state->input_buffer).text) 2509 { 2510 for (i = 0; i < state->paste_count; i++) 2511 free_bufferline(state->paste + i); 2512 free(state->paste); 2513 state->paste = NULL; 2514 state->paste_count = 0; 2515 return 0; 2516 } 2517 2518 lines_to_init = state->paste_count < state->input_rows_count 2519 ? state->input_rows_count - state->paste_count 2520 : 0; 2521 lines_to_free = state->paste_count > state->input_rows_count 2522 ? state->paste_count - state->input_rows_count 2523 : 0; 2524 2525 for (i = 0; i < lines_to_free; i++) 2526 free_bufferline(state->paste + state->paste_count - i - 1); 2527 if (!state->paste) 2528 newbuf = calloc(state->input_rows_count, 2529 sizeof(struct BufferLine)); 2530 else 2531 newbuf = realloc(state->paste, 2532 state->input_rows_count * sizeof(struct BufferLine)); 2533 if (!newbuf) 2534 goto yank_input_alloc_error; 2535 2536 pinput = state->input_buffer; 2537 for (i = 0; i < lines_to_init; i++) 2538 { 2539 ppaste = newbuf + state->input_rows_count - 1 - i; 2540 init_bufferline(ppaste); 2541 newtext = calloc(LINE_DEFAULT, sizeof(uint32_t)); 2542 if (!newtext) 2543 goto yank_input_alloc_error; 2544 ppaste->text = newtext; 2545 ppaste->size = LINE_DEFAULT; 2546 } 2547 2548 if (pinput) 2549 for (i = 0; i < state->input_rows_count; i++) 2550 { 2551 ppaste = &newbuf[i]; 2552 U32_SAFE_COPY(ppaste->text, temp, ppaste->size, 2553 pinput->text, pinput->size, 2554 yank_input_alloc_error); 2555 ppaste->length = pinput->length; 2556 pinput++; 2557 } 2558 2559 state->paste_count = state->input_rows_count; 2560 state->paste = newbuf; 2561 return 0; 2562 2563 yank_input_alloc_error: 2564 state->error = ERR_CANT_ALLOC; 2565 return 1; 2566 } 2567 2568 static int 2569 write_file(struct DrawState* state) 2570 { 2571 int result; 2572 assert(state != NULL); 2573 result = save_file(state); 2574 if (result) 2575 { 2576 state->running = 0; 2577 return result; 2578 } 2579 state->error = ERR_NONE; 2580 state->edit_dirty = 0; 2581 state->dirty = 0; 2582 return 0; 2583 } 2584 2585 int 2586 main(int argc, char** argv) 2587 { 2588 struct DrawState state; 2589 struct tb_event ev; 2590 struct stat st; 2591 char prompt[LINE_DEFAULT]; 2592 char* dir = NULL; 2593 char* filename = NULL; 2594 char* pdir = NULL; 2595 char* rfn = NULL; 2596 const char* saved_tb_error = NULL; 2597 char* temp = NULL; 2598 char* wd = NULL; 2599 ssize_t filename_size = 0; 2600 long col = 1; 2601 long lineno = 1; 2602 int dir_access; 2603 int dir_access_errno; 2604 int errornum; 2605 int optind = 1; 2606 int result; 2607 int saved_errno = 0; 2608 int tbires; 2609 2610 *prompt = 0; 2611 2612 #ifdef __OpenBSD__ 2613 if (pledge("stdio tty unveil rpath wpath cpath fattr", NULL) < 0) 2614 { 2615 perror(PROGRAM_NAME ": pledge"); 2616 exit(errno); 2617 } 2618 #endif 2619 2620 /* Necessary in order for termbox to function properly 2621 * See: https://github.com/termbox/termbox2/issues/73 2622 */ 2623 setlocale(LC_ALL, ""); 2624 2625 main_options_loop: 2626 if (optind >= argc) 2627 return print_error(CUSTOM_ERROR_START + ERR_NO_CL_ARG, 2628 errors[ERR_NO_CL_ARG]); 2629 2630 if (!strcmp(argv[optind], "-h") || !strcmp(argv[optind], "--help")) 2631 { 2632 printf("Usage:\t%s -h | --help | -V | --full-version | -v |" 2633 " --version\n" 2634 "\t%s [-B | --no-backup] [-b | --backup]" 2635 " [-L | --dont-break-literals]" 2636 " [-W | --no-wrap] [-w <cols> | --wrap-width=<cols>]" 2637 " filename.po\n", 2638 PROGRAM_NAME, PROGRAM_NAME); 2639 return 0; 2640 } 2641 else if (!strcmp(argv[optind], "-v") 2642 || !strcmp(argv[optind], "--version")) 2643 { 2644 printf("%s %s, committed on %s\n", PROGRAM_NAME, VERSION, DATE); 2645 return 0; 2646 } 2647 else if (!strcmp(argv[optind], "-V") 2648 || !strcmp(argv[optind], "--full-version")) 2649 { 2650 printf("%s %s, committed on %s\n", PROGRAM_NAME, VERSION, DATE); 2651 puts(COPYRIGHT); 2652 printf("Features set in config.h:\n"); 2653 printf(" backup_fail:\t\t\t%s\n", 2654 backup_fail ? "set" : "unset"); 2655 if (backup_suffix) 2656 printf(" backup_suffix:\t\t\"%s\"\n", backup_suffix); 2657 else 2658 printf(" backup_suffix:\t\tunset\n"); 2659 printf(" break_literals:\t\t%s\n", 2660 break_literals ? "set" : "unset"); 2661 printf(" create_backups:\t\t%s\n", 2662 create_backups ? "set" : "unset"); 2663 printf(" literal_break_threshold:\t%d\n", 2664 literal_break_threshold); 2665 printf(" match_msgstr_ending:\t\t%s\n", 2666 match_msgstr_ending ? "set" : "unset"); 2667 if (plural_string) 2668 printf(" plural_string:\t\t\"%s\"\n", plural_string); 2669 else 2670 printf(" plural_string:\t\tunset\n"); 2671 printf(" warn_count_dots:\t\t%s\n", 2672 warn_count_dots ? "set" : "unset"); 2673 printf(" wrap_width:\t\t\t%d\n", wrap_width); 2674 return 0; 2675 } 2676 else if (!strcmp(argv[optind], "-B") 2677 || !strcmp(argv[optind], "--no-backup")) 2678 { 2679 create_backups = 0; 2680 optind++; 2681 goto main_options_loop; 2682 } 2683 else if (!strcmp(argv[optind], "-b") 2684 || !strcmp(argv[optind], "--backup")) 2685 { 2686 create_backups = 1; 2687 optind++; 2688 goto main_options_loop; 2689 } 2690 else if (!strcmp(argv[optind], "-L") 2691 || !strcmp(argv[optind], "--dont-break-literals")) 2692 { 2693 break_literals = 0; 2694 optind++; 2695 goto main_options_loop; 2696 } 2697 else if (!strcmp(argv[optind], "-W") 2698 || !strcmp(argv[optind], "--no-wrap")) 2699 { 2700 wrap_width = 0; 2701 optind++; 2702 goto main_options_loop; 2703 } 2704 else if (!strcmp(argv[optind], "-w") 2705 || !strcmp(argv[optind], "--wrap-width=")) 2706 { 2707 optind++; 2708 wrap_width = strtonumber(argv[optind], 0, INT_MAX, &errornum); 2709 if (errornum != STN_NONE) 2710 { 2711 if (*argv[optind] == '-') 2712 state.error = ERR_NO_CL_ARG_PARAM; 2713 else 2714 state.error = ERR_BAD_CL_ARG; 2715 goto main_done; 2716 } 2717 if (wrap_width < 0) 2718 wrap_width = 0; 2719 optind++; 2720 goto main_options_loop; 2721 } 2722 else if (starts_with(argv[optind], "--") 2723 || starts_with(argv[optind], "-")) 2724 { 2725 state.error = ERR_BAD_CL_ARG; 2726 goto main_done; 2727 } 2728 2729 /* 2730 * Chicken and egg problem: to get fpathconf(dirfd, _PC_PATH_MAX) 2731 * we need a dir, which needs to be allocated, and to allocate it we 2732 * need to know the value returned by fpathconf(dirfd, _PC_PATH_MAX)... 2733 */ 2734 /* argv[optind] is the filename; BTW, "filename" is a misnomer */ 2735 filename_size = strlen(argv[optind]) + 1; 2736 if (!(dir = malloc(filename_size))) 2737 { 2738 perror(PROGRAM_NAME ": malloc"); 2739 exit(errno); 2740 } 2741 2742 (void)dirname(dir, argv[optind], filename_size); 2743 if (init_limits(dir) < 0) 2744 { 2745 perror(PROGRAM_NAME ": init_limits"); 2746 free(dir); 2747 exit(errno); 2748 } 2749 2750 filename = calloc(limits[L_PATH_MAX], 1); 2751 if (!filename) 2752 { 2753 perror(PROGRAM_NAME ": calloc"); 2754 exit(errno); 2755 } 2756 2757 *filename = 0; 2758 MEMCCPY(filename, argv[optind], limits[L_PATH_MAX], temp); 2759 if (!*filename) 2760 { 2761 free(filename); 2762 free(dir); 2763 return print_error(CUSTOM_ERROR_START + ERR_NO_CL_ARG, 2764 errors[ERR_NO_CL_ARG]); 2765 } 2766 2767 init_drawstate(&state, filename, prompt); 2768 2769 #ifdef __OpenBSD__ 2770 if (unveil(state.filename, "rwc") < 0) 2771 { 2772 perror(PROGRAM_NAME ": unveil"); 2773 goto main_done; 2774 } 2775 if (unveil(dir, "rwc") < 0) 2776 { 2777 perror(PROGRAM_NAME ": unveil"); 2778 goto main_done; 2779 } 2780 #endif 2781 2782 if (!create_backups) 2783 goto main_skip_backups; 2784 2785 if (!(wd = malloc(limits[L_PATH_MAX]))) 2786 { 2787 perror(PROGRAM_NAME ": malloc"); 2788 goto main_done; 2789 } 2790 if (!getcwd(wd, limits[L_PATH_MAX])) 2791 { 2792 perror(PROGRAM_NAME ": getcwd"); 2793 goto main_done; 2794 } 2795 #ifdef __OpenBSD__ 2796 if (unveil(dir, "rwc") < 0) 2797 { 2798 perror(PROGRAM_NAME ": unveil"); 2799 goto main_done; 2800 } 2801 #endif 2802 2803 errno = 0; 2804 if (lstat(state.filename, &st) != -1) 2805 { 2806 if (S_ISLNK(st.st_mode)) 2807 { 2808 if (!(rfn = malloc(limits[L_PATH_MAX]))) 2809 { 2810 perror(PROGRAM_NAME ": malloc"); 2811 goto main_done; 2812 } 2813 2814 if (!(state.real_filename = malloc(limits[L_PATH_MAX]))) 2815 { 2816 perror(PROGRAM_NAME ": malloc"); 2817 goto main_done; 2818 } 2819 2820 state.rfn_len = readlink(state.filename, rfn, 2821 limits[L_PATH_MAX]); 2822 if (state.rfn_len == -1) 2823 goto main_done; 2824 2825 rfn[state.rfn_len] = 0; 2826 2827 if (*rfn == '/') /* rfn is absolute */ 2828 { 2829 MEMCCPY(state.real_filename, rfn, 2830 limits[L_PATH_MAX], temp); 2831 state.rfn_len = strlen(rfn); 2832 } 2833 else /* rfn is relative */ 2834 { 2835 size_t rfn_dir_len; 2836 2837 state.orig_real_filename 2838 = strndup(rfn, state.rfn_len); 2839 if (!state.orig_real_filename) 2840 { 2841 perror(PROGRAM_NAME ": strndup"); 2842 goto main_done; 2843 } 2844 2845 MEMCCPY(state.real_filename, dir, 2846 limits[L_PATH_MAX], temp); 2847 if (!temp) 2848 { 2849 errno = ENAMETOOLONG; 2850 goto main_done; 2851 } 2852 *(temp - 1) = '/'; /* overwrite NUL */ 2853 rfn_dir_len = temp - state.real_filename - 1; 2854 MEMCCPY_EXT(temp, state.real_filename, rfn, 2855 limits[L_PATH_MAX] - rfn_dir_len, temp); 2856 if (temp) 2857 state.rfn_len = temp 2858 - state.real_filename - 1; 2859 } 2860 free(rfn); 2861 rfn = NULL; 2862 #ifdef __OpenBSD__ 2863 if (unveil(state.real_filename, "rwc") < 0) 2864 { 2865 perror(PROGRAM_NAME ": unveil"); 2866 goto main_done; 2867 } 2868 #endif 2869 if (stat(state.real_filename, &st) == -1) 2870 goto main_done; 2871 } 2872 } 2873 else 2874 goto main_done; 2875 2876 if (!(state.backup_filename = malloc(limits[L_PATH_MAX]))) 2877 { 2878 perror(PROGRAM_NAME ": malloc"); 2879 goto main_done; 2880 } 2881 2882 /* Backup filename is the target file + suffix if the file to 2883 * write is a symlink, otherwise file + suffix */ 2884 MEMCCPY(state.backup_filename, 2885 state.rfn_len > 0 ? state.real_filename : state.filename, 2886 limits[L_PATH_MAX], temp); 2887 if (!temp) /* filename is full, turning off backups */ 2888 state.backup_possible = 0; 2889 else 2890 MEMCCPY_EXT((temp - 1), state.backup_filename, backup_suffix, 2891 limits[L_PATH_MAX] + temp - state.backup_filename, 2892 temp); 2893 #ifdef __OpenBSD__ 2894 if (unveil(state.backup_filename, "rwc") < 0) 2895 { 2896 perror(PROGRAM_NAME ": unveil"); 2897 goto main_done; 2898 } 2899 #endif 2900 2901 main_skip_backups: 2902 if (state.rfn_len > 0) 2903 { 2904 if (!(pdir = realloc(dir, limits[L_PATH_MAX]))) 2905 { 2906 perror(PROGRAM_NAME ": realloc"); 2907 goto main_done; 2908 } 2909 dir = pdir; 2910 *dir = 0; 2911 (void)dirname(dir, state.real_filename, limits[L_PATH_MAX]); 2912 #ifdef __OpenBSD__ 2913 if (unveil(dir, "rwc") < 0) 2914 { 2915 perror(PROGRAM_NAME ": unveil"); 2916 goto main_done; 2917 } 2918 #endif 2919 } 2920 2921 #ifdef __OpenBSD__ 2922 if (unveil_terminfo_path() < 0) 2923 { 2924 perror(PROGRAM_NAME ": unveil"); 2925 goto main_done; 2926 } 2927 /* Have to explicitly unveil /dev/tty before tb_init despite pledge(tty) 2928 * in OpenBSD 7.9+ 2929 */ 2930 if (unveil("/dev/tty", "rw")) 2931 { 2932 perror(PROGRAM_NAME ": unveil"); 2933 goto main_done; 2934 } 2935 if (unveil(NULL, NULL) < 0) 2936 { 2937 perror(PROGRAM_NAME ": unveil"); 2938 goto main_done; 2939 } 2940 #endif 2941 2942 dir_access = access(dir, W_OK | X_OK); 2943 dir_access_errno = errno; 2944 2945 if (!backup_fail && state.backup_possible) 2946 state.backup_possible = dir_access != -1; 2947 2948 result = stat(state.filename, &st); 2949 if (result == -1) 2950 { 2951 if (dir_access == -1) 2952 { 2953 errno = dir_access_errno; 2954 goto main_done; 2955 } 2956 goto init_termbox; 2957 } 2958 2959 errno = 0; 2960 2961 if (!S_ISREG(st.st_mode)) 2962 { 2963 state.error = ERR_NOTREG_FILE; 2964 goto main_done; 2965 } 2966 2967 if (access(state.filename, F_OK | R_OK | W_OK) == -1) 2968 goto main_done; 2969 2970 if (state.rfn_len > 0 2971 && access(state.real_filename, F_OK | R_OK | W_OK) == -1) 2972 goto main_done; 2973 2974 if (load_file(&state, &lineno, &col)) 2975 goto main_done; 2976 2977 state.msgid_number = state.msgid_count > 0 ? 1 : 0; 2978 state.first_shown_msgid = state.msgid_number; 2979 2980 init_termbox: 2981 if ((tbires = tb_init())) 2982 { 2983 saved_tb_error = tb_strerror(tbires); 2984 errno = ERR_TB_ERROR; 2985 goto main_done; 2986 } 2987 2988 errno = 0; 2989 state.maxx = 0; 2990 state.recalculate_size = 1; 2991 2992 while (state.running) 2993 { 2994 tb_set_clear_attrs(colors[C_CLEAR].fg, colors[C_CLEAR].bg); 2995 tb_clear(); 2996 2997 if (state.recalculate_size) 2998 { 2999 recalculate_size(&state); 3000 state.recalculate_size = 0; 3001 } 3002 if (draw(&state)) 3003 { 3004 state.running = 0; 3005 goto main_done; 3006 } 3007 tb_present(); 3008 result = tb_poll_event(&ev); 3009 if (result == TB_OK && ev.type == TB_EVENT_RESIZE) 3010 state.recalculate_size = 1; 3011 else if (result == TB_OK && ev.type == TB_EVENT_KEY) 3012 handle_key_event(&ev, &state); 3013 } 3014 3015 main_done: 3016 saved_errno = errno; 3017 3018 if (saved_tb_error) 3019 { 3020 tb_shutdown(1); 3021 print_error(1, saved_tb_error); 3022 exit(CUSTOM_ERROR_START + saved_errno); 3023 } 3024 3025 tb_shutdown(saved_errno != ENOMEM); 3026 3027 if (saved_errno) 3028 perror(PROGRAM_NAME); 3029 3030 switch (state.error) 3031 { 3032 case ERR_NOTREG_FILE: 3033 print_error(1, errors[state.error], state.filename); 3034 break; 3035 case ERR_NO_CL_ARG_PARAM: 3036 print_error(1, errors[state.error], argv[optind - 1]); 3037 break; 3038 case ERR_BAD_CL_ARG: 3039 print_error(1, errors[state.error], argv[optind]); 3040 break; 3041 case ERR_SYNTAX: 3042 print_error(1, errors[state.error], state.filename, lineno, col); 3043 break; 3044 case ERR_NONE: 3045 break; 3046 default: 3047 print_error(1, errors[state.error]); 3048 } 3049 3050 return saved_errno 3051 ? saved_errno 3052 : (state.error ? CUSTOM_ERROR_START + state.error : 0); 3053 }