1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
|
/* commands.c 10/05/1995
*/
/* Copyright (c) 1995,1996 Sascha Demetrio
* Copyright (c) 2009 Peter Pentchev
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* If you modify any part of HEXER and resitribute it, you must add
* a notice to the `README' file and the modified source files containing
* information about the changes you made. I do not want to take
* credit or be blamed for your modifications.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* If you modify any part of HEXER and resitribute it in binary form,
* you must supply a `README' file containing information about the
* changes you made.
* 3. The name of the developer may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* HEXER WAS DEVELOPED BY SASCHA DEMETRIO.
* THIS SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
* THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
* NOT MAKE USE OF THIS WORK.
*
* DISCLAIMER:
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "config.h"
#include <assert.h>
#include <pwd.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <stdlib.h>
#include "buffer.h"
#include "hexer.h"
#include "commands.h"
#include "edit.h"
#include "exh.h"
#include "helptext.h"
#include "regex.h"
#include "set.h"
#include "tio.h"
#include "util.h"
#define EXIT_EXEC_FAILED 27
#ifndef ERESTARTSYS
#define ERESTARTSYS EINTR
#endif
int he_map_special;
static void
press_any_key(struct he_s *hedit)
{
tio_printf("@Ar press any key @~@r");
tio_getch();
tio_clear_to_eol();
he_refresh_all(hedit);
}
/* press_any_key */
/* commands for the exh-interpreter. the commands may be separated by
* semicolons. if a command finds a semicolon at the end of its input,
* a pointer to the character immediately following the semicolon is
* returned. if a separating semicolon is not found in the input, a
* null-pointer is returned.
*/
static char *
exh_skipcmd(char *cmd, char *dest)
/* read `cmd' up to the next semicolon. `\;' escapes the semicolon.
* The return value is the character following the first occurence of an
* unmasked semicolon or 0 if no semicolon was found or the first unmasked
* semicolon was the last character in the string `cmd'. the string read is
* written to `dest'.
*/
{
char *p, *q;
if (!dest) dest = (char *)alloca(strlen(cmd) + 1);
for (p = cmd, q = dest; *p && *p != ';';)
if (*p == '\\' && p[1] == ';')
*q++ = *++p, ++p;
else
*q++ = *p++;
if (!*p) p = 0;
*q = 0;
return p;
}
/* exh_skipcmd */
static char *
exh_map(struct he_s *hedit, char *args, int map)
{
char *from, *to;
char *p, *skip;
skip = exh_skipcmd(args, p = (char *)alloca(strlen(args) + 1));
args = p;
if (!*args) {
char **list, **i;
i = list = tio_maplist(map);
if (*list) {
tio_message(list, 1);
press_any_key(hedit);
} else
he_message(0, "no mappings");
while (*i) free((char *)*i++);
free((char *)list);
return skip;
}
for (p = from = args; *p && *p != ' '; ++p);
if (*p) {
*p++ = 0;
while (*p == ' ') ++p;
for (to = p; *p; ++p);
*p = 0;
tio_map(map, from, to, he_map_special);
}
return skip;
}
/* exh_map */
static char *
exh_unmap(struct he_s *hedit, char *args, int map)
/* ARGSUSED */
{
char *from;
char *p, *skip;
skip = exh_skipcmd(args, p = (char *)alloca(strlen(args) + 1));
args = p;
if (!*args) {
he_message(0, "unmap what?");
return 0;
}
for (p = from = args; *p && *p != ' '; ++p);
*p = 0;
if (tio_unmap(map, from, he_map_special)) he_message(0, "no such mapping");
return skip;
}
/* exh_unmap */
static char *
exh_save_buffer(struct he_s *hedit, char *name, long begin, long end, int *error)
{
struct buffer_s *i;
char *p;
char *skip;
int force_f = 0;
FILE *fp;
long l;
int whole_f = 0;
char *file = 0;
if (end >= hedit->buffer->size || end < 0) end = hedit->buffer->size - 1;
if (begin == 0 && end == hedit->buffer->size - 1) whole_f = 1;
while (*name == ' ' || *name == '\t') ++name;
if (*name == '!') {
force_f = 1;
while (*name == ' ' || *name == '\t') ++name;
}
skip = exh_skipcmd(name, p = (char *)alloca(strlen(name) + 1));
name = p;
util_trunc(name);
if (!*name) {
i = current_buffer;
file = i->path;
} else {
for (i = buffer_list; i; i = i->next)
if (!strcmp(name, i->hedit->buffer_name)) {
file = i->path;
break;
}
if (!i) {
he_message(0, "@Abno buffer named@~ `%s'", name);
if (error) *error = 1;
return skip;
}
}
if (i->hedit->read_only && !force_f) {
he_message(0, "@Abread only buffer@~ - use @U:w!@~ to override");
if (error) *error = 1;
return skip;
}
if (!(fp = fopen(file, "w"))) {
he_message(0, "@Abcan't open@~ `%s'", file);
if (error) *error = 1;
return skip;
}
fclose(fp);
l = b_copy_to_file(i->hedit->buffer, file, begin, end - begin + 1);
he_message(0, "wrote `%s' - 0x%lx (%li) bytes", file, l, l);
if (whole_f) i->hedit->buffer->modified = 0;
if (error) *error = 0;
return skip;
}
/* exh_save_buffer */
static char *
exh_write(struct he_s *hedit, char *file, long begin, long end, int *error)
{
char *p;
char *skip;
int force_f = 0;
FILE *fp;
long l;
int whole_f = 0;
if (end >= hedit->buffer->size || end < 0) end = hedit->buffer->size - 1;
if (begin == 0 && end == hedit->buffer->size - 1) whole_f = 1;
while (*file == ' ' || *file == '\t') ++file;
if (*file == '!') {
force_f = 1, ++file;
while (*file == ' ' || *file == '\t') ++file;
}
skip = exh_skipcmd(file, p = (char *)alloca(strlen(file) + 1));
util_trunc(file = p);
if (!*file) {
if (!(file = current_buffer->path)) {
he_message(1, "@Abno filename@~");
return skip;
}
if (current_buffer->hedit->read_only && !force_f) {
he_message(0, "@Abread only buffer@~ - use @U:w!@~ to override");
if (error) *error = 1;
return skip;
}
}
if (!(fp = fopen(file, "w"))) {
he_message(0, "@Abcan't open@~ `%s'", file);
if (error) *error = 1;
return skip;
}
fclose(fp);
l = b_copy_to_file(hedit->buffer, file, begin, end - begin + 1);
he_message(0, "wrote `%s' - 0x%lx (%li) bytes", file, l, l);
if (whole_f) hedit->buffer->modified = 0;
if (error) *error = 0;
return skip;
}
/* exh_write */
static char *
exhcmd_substitute(struct he_s *hedit, char *pattern, long begin, long end)
{
char *p = pattern;
char separator = *pattern;
char *exp, *replace = 0, *options = 0;
char *skip = 0;
int confirm_f = 0;
long position, last_match = hedit->position;
char *replace_str;
long replace_len, match_len;
int subst = 0;
char *data;
static char prev_exp[4096] = "";
static char prev_replace[4096] = "";
static char prev_options[256] = "";
if (!*pattern) {
if (!*prev_exp) {
he_message(0, "@Abno previous substitute@~");
goto exit_exh_substitute;
} else {
exp = prev_exp;
replace = prev_replace;
options = prev_options;
}
} else {
exp = p + 1;
if (*exp) {
p = exh_skip_expression(exp, separator);
if (p) {
*p = 0;
replace = p + 1;
p = exh_skip_replace(replace, separator);
if (p) {
*p = 0;
options = p + 1;
while (*p && *p != ';') ++p;
if (*p) skip = p + 1; else skip = 0;
} else
options = "";
} else {
replace = "";
options = "";
}
} else {
if (!*prev_exp) {
he_message(0, "@Abno previous substitute@~");
goto exit_exh_substitute;
} else {
exp = prev_exp;
replace = prev_replace;
options = prev_options;
}
}
}
if (exp != prev_exp) strcpy(prev_exp, exp);
if (replace != prev_replace) strcpy(prev_replace, replace);
if (options != prev_options) strcpy(prev_options, options);
p = options;
while (*p) {
switch (*p++) {
case 'c': /* confirm all changes */
confirm_f = 1;
break;
case 'g': /* ignored */
break;
default:
he_message(0, "@Abinvalid option@~ `%c'", p[-1]);
goto exit_exh_substitute;
}
}
for (position = begin; position <= end;) {
hedit->position = position;
switch (position = he_search(hedit, exp, replace, 1, 0, 0, end,
&replace_str, &replace_len, &match_len)) {
case -2: /* invalid expression */
assert(rx_error);
he_message(0, "@Abinvalid expression:@~ %s", rx_error_msg[rx_error]);
goto exit_exh_substitute;
case -1: /* match not found */
if (rx_error)
he_message(0, "@Abregex: %s@~", rx_error_msg[rx_error]);
if (subst > 0)
he_message(0, "%i substitutions", subst);
else
he_message(0, "no match");
goto exit_exh_substitute;
default: /* found */
/* first we're gonna check if the match is in the selected area */
if (position + match_len > end + 1) {
position += 1;
break;
}
last_match = position;
if (!confirm_f)
goto substitute;
else {
/* We want to show the user the match found so we can query him/her
* if the match should be replaced. We'll do this by selecting the
* match.
* NOTE: `match_len' and/or `replace_len' might be zero. If both
* are zero, we won't query the user.
*/
int choice;
if (!match_len && !replace_len) goto substitute;
he_select(hedit, position, position + match_len - !!match_len);
/* we want to make sure that (if possible) the whole match is
* visible. */
hedit->position = position + match_len;
he_update_screen(hedit);
hedit->position = position;
he_update_screen(hedit);
choice =
he_query_yn(1,
"replace 0x%lx (%li) bytes at position 0x%lx (%li) (ynaq) ",
match_len, match_len, position, position);
tio_flush();
switch (choice) {
case -1: /* escape pressed */
if (subst > 0)
he_message(0, "%i substitutions / canceled", subst);
else
he_message(0, "canceled");
free((char *)replace_str);
he_cancel_selection(hedit);
he_update_screen(hedit);
goto exit_exh_substitute;
case 0:
free((char *)replace_str);
position += 1;
he_cancel_selection(hedit);
he_update_screen(hedit);
break;
case 1: /* substitute */
goto substitute;
case 2: /* always */
confirm_f = 0;
he_cancel_selection(hedit);
he_update_screen(hedit);
goto substitute;
}
}
break;
substitute:
end -= match_len - replace_len;
/* create an entry for the undo-list */
if (match_len) {
data = (char *)malloc(match_len);
b_read(hedit->buffer, data, position, match_len);
he_subcommand(hedit, 0, position, match_len, data);
}
if (replace_len)
he_subcommand(hedit, 1, position, replace_len, replace_str);
/* in any case the memory allocated for `replace_str' won't be
* `free((char *))'d before the final call to `he_subcommand()', so we
* can still use that pointer. */
if (match_len || replace_len) ++subst;
/* if nothing gets replaced by nothing, we won't count that
* as a substitution, so there won't be an entry in the undo-
* list if nothing got changed. */
if (match_len) b_delete(hedit->buffer, position, match_len);
if (replace_len) {
b_insert(hedit->buffer, position, replace_len);
b_write(hedit->buffer, replace_str, position, replace_len);
}
if (confirm_f && match_len) {
he_refresh_part(hedit, hedit->begin_selection, -1);
he_cancel_selection(hedit);
he_update_screen(hedit);
}
position += replace_len + !replace_len;
break;
}
exp = 0;
}
exit_exh_substitute:
if (subst) he_subcommand(hedit, -1, 0, 0, 0);
hedit->position = last_match;
if (!confirm_f) he_refresh_part(hedit, 0, -1);
return skip;
}
/* exhcmd_substitute */
static char *
exhcmd_write(struct he_s *hedit, char *file, long begin, long end)
{
return exh_write(hedit, file, begin, end, 0);
}
/* exhcmd_write */
static char *
exhcmd_skip(struct he_s *hedit, char *args)
/* ARGSUSED */
{
struct buffer_s *i;
char *ab = alternate_buffer;
char *skip;
skip = exh_skipcmd(args, 0);
for (i = buffer_list; i; i = i->next)
if (!i->visited_f) {
alternate_buffer = current_buffer->hedit->buffer_name;
if (he_select_buffer_(i)) { /* Oops. */
he_message(1, "@AbINTERNAL ERROR@~");
he_select_buffer_(current_buffer);
alternate_buffer = ab;
} else {
he_status_message(1);
he_refresh_part(current_buffer->hedit, 0, -1);
}
break;
}
if (!i) he_message(0, "no more unvisited buffers");
return skip;
}
/* exhcmd_skip */
static char *
exhcmd_next(struct he_s *hedit, char *args)
/* ARGSUSED */
{
struct buffer_s *i;
char *ab = alternate_buffer;
char *skip;
skip = exh_skipcmd(args, 0);
if ((i = current_buffer->next)) {
alternate_buffer = current_buffer->hedit->buffer_name;
if (he_select_buffer_(i)) { /* Oops. */
he_message(1, "@AbINTERNAL ERROR@~");
he_select_buffer_(current_buffer);
alternate_buffer = ab;
} else {
he_status_message(1);
he_refresh_part(current_buffer->hedit, 0, -1);
}
} else
he_message(0, "no next buffer");
return skip;
}
/* exhcmd_next */
static char *
exhcmd_previous(struct he_s *hedit, char *args)
/* ARGSUSED */
{
struct buffer_s *i;
char *ab = alternate_buffer;
char *skip;
skip = exh_skipcmd(args, 0);
for (i = buffer_list; i; i = i->next) if (i->next == current_buffer) break;
if (i) {
alternate_buffer = current_buffer->hedit->buffer_name;
if (he_select_buffer_(i)) { /* Oops. */
he_message(1, "@AbINTERNAL ERROR@~");
he_select_buffer_(current_buffer);
alternate_buffer = ab;
} else {
he_status_message(1);
he_refresh_part(current_buffer->hedit, 0, -1);
}
} else
he_message(0, "no previous buffer");
return skip;
}
/* exhcmd_previous */
static char *
exhcmd_rewind(struct he_s *hedit, char *args)
/* ARGSUSED */
{
char *ab = alternate_buffer;
char *skip;
skip = exh_skipcmd(args, 0);
alternate_buffer = current_buffer->hedit->buffer_name;
if (he_select_buffer_(buffer_list)) { /* Oops. */
he_message(1, "@AbINTERNAL ERROR@~");
he_select_buffer_(current_buffer);
alternate_buffer = ab;
} else {
he_status_message(1);
he_refresh_part(current_buffer->hedit, 0, -1);
}
return skip;
}
/* exhcmd_rewind */
static char *
exhcmd_wn(struct he_s *hedit, char *file, long begin, long end)
{
char *skip;
int error = 0;
skip = exh_write(hedit, file, begin, end, &error);
if (!error) exhcmd_next(hedit, "");
return skip;
}
/* exhcmd_wn */
static char *
exhcmd_read(struct he_s *hedit, char *file, long position)
{
FILE *fp;
long l;
char *data;
char *skip, *p;
skip = exh_skipcmd(file, p = (char *)alloca(strlen(file) + 1));
util_trunc(file = p);
while (*file == ' ' || *file == '\t') ++file;
if (!(fp = fopen(file, "r"))) {
he_message(0, "@Abcan't open@~ `%s'", file);
return skip;
}
fclose(fp);
l = b_paste_from_file(hedit->buffer, file, position);
if (l == -1)
return skip;
data = (char *)malloc(l);
fp = fopen(file, "r");
if (fread(data, 1, l, fp) != l) {
free(data);
fclose(fp);
return skip;
}
he_subcommand(hedit, 1, position, l, data);
he_subcommand(hedit, -1, 0, 0, 0);
he_message(0, "read 0x%lx (%li) bytes from `%s'", l, l, file);
if (l) he_refresh_part(hedit, position, -1);
return skip;
}
/* exhcmd_read */
static char *
exhcmd_edit(struct he_s *hedit, char *name)
/* ARGSUSED */
{
char *ab = alternate_buffer;
struct passwd *pe;
int new_buffer_f = 0;
FILE *fp;
char *skip, *p;
char *path = 0;
char *user = 0;
int uid = getuid();
skip = exh_skipcmd(name, p = (char *)alloca(strlen(name) + 1));
util_trunc(name = p);
while (*name == ' ' || *name == '\t') ++name;
if (!strcmp(name, "#")) { /* switch to alternate buffer. */
if (he_alternate_buffer() < 0)
he_message(0, "no alternate buffer");
else {
he_refresh_part(current_buffer->hedit, 0, -1);
tio_ungetch('g' & 0x1f); /* FIXME */
}
return skip;
} else
alternate_buffer = current_buffer->hedit->buffer_name;
if (he_select_buffer(name) < 0) {
new_buffer_f = 1;
if (*name == '~') {
user = strdup(name + 1);
for (p = user; *p && *p != '/'; ++p);
*p = 0;
if (*user) {
for (setpwent(); (pe = getpwent());)
if (!strcmp(user, pe->pw_name)) break;
} else
for (setpwent(); (pe = getpwent());)
if (uid == pe->pw_uid) break;
if (pe) {
path = (char *)malloc(strlen(name) + strlen(pe->pw_dir) + 4);
strcpy(path, pe->pw_dir);
strcat(path, name + strlen(user) + 1);
} else
path = name;
free(user);
} else
path = name;
if (!(fp = fopen(path, "r"))) {
he_message(0, "@Abcan't open@~ `%s'", name);
alternate_buffer = ab;
if (path && path != name) free(path);
return skip;
} else {
fclose(fp);
he_open_buffer(name, path);
he_select_buffer(name);
}
}
he_status_message(!new_buffer_f);
he_refresh_part(current_buffer->hedit, 0, -1);
if (path && path != name) free(path);
return skip;
}
/* exhcmd_edit */
static char *
exhcmd_buffer(struct he_s *hedit, char *name)
{
char *ab = alternate_buffer;
long j, k;
struct buffer_s *i;
char **list;
char *c;
char *skip, *p;
skip = exh_skipcmd(name, p = (char *)alloca(strlen(name) + 1));
util_trunc(name = p);
if (!*name) { /* list buffers */
int name_maxlen = 0;
for (i = buffer_list, k = 0; i; i = i->next, ++k)
if (strlen(i->hedit->buffer_name) > name_maxlen)
name_maxlen = strlen(i->hedit->buffer_name);
list = (char **)malloc((k + 1) * sizeof(char *));
for (i = buffer_list, k = 0; i; i = i->next, ++k) {
list[k] = (char *)malloc(strlen(i->hedit->buffer_name) + 512);
sprintf(list[k], " `%s' ", i->hedit->buffer_name);
c = list[k] + strlen(list[k]);
for (j = name_maxlen - strlen(i->hedit->buffer_name); j >= 0; --j)
*c++ = ' ';
*c = 0;
if (i->path) {
if (strcmp(i->hedit->buffer_name, i->path)) {
sprintf(c, "(%s) ", i->path);
c += strlen(c);
}
} else {
sprintf(c, "(no file) ");
c += strlen(c);
}
if (i->loaded_f) {
if (i->hedit->buffer->modified) {
sprintf(c, "[modified] ");
c += strlen(c);
}
if (i->hedit->read_only) {
sprintf(c, "[readonly] ");
c += strlen(c);
}
j = i->hedit->buffer->size;
sprintf(c, " 0x%lx (%li) bytes\n", j, j);
} else {
if (!i->visited_f)
sprintf(c, " (not visited)\n");
else
sprintf(c, " (not loaded)\n");
}
}
list[k] = 0;
tio_message(list, 0);
for (k = 0; list[k]; ++k) free((char *)list[k]);
free((char *)list);
press_any_key(hedit);
return skip;
} else { /* select buffer */
alternate_buffer = current_buffer->hedit->buffer_name;
if (he_select_buffer(name) < 0) {
he_message(0,
"@Abno buffer named@~ `%s' - use @U:edit@u to open a buffer",
name);
alternate_buffer = ab;
return skip;
} else {
he_status_message(1);
he_refresh_part(current_buffer->hedit, 0, -1);
}
}
return skip;
}
/* exhcmd_buffer */
static char *
exhcmd_wall(struct he_s *hedit, char *args)
/* ARGSUSED */
{
struct buffer_s *i;
char *skip;
skip = exh_skipcmd(args, 0);
for (i = buffer_list; i; i = i->next) {
if (i->loaded_f ? i->hedit->buffer->modified : 0) {
exh_save_buffer(hedit, i->hedit->buffer_name, 0, -1, 0);
#if 0
l = b_write_buffer_to_file(i->hedit->buffer, i->path);
if (l < 0)
he_message(1, "@Abcan't write `%s'", i->path);
else
he_message(0, "wrote `%s' - 0x%lx (%li) bytes", i->path, l, l);
#endif
}
}
return skip;
}
/* exhcmd_wall */
static char *
exhcmd_quit(struct he_s *hedit, char *args)
/* ARGSUSED */
{
struct buffer_s *i, *j;
char *skip, *p;
int n;
static int warn_more_files = 1;
skip = exh_skipcmd(args, p = (char *)alloca(strlen(args) + 1));
args = p;
while (*args == ' ' || *args == '\t') ++args;
if (*args == '!') goto close_all_buffers;
for (i = buffer_list, n = 0; i; i = i->next) {
if (i->loaded_f ? i->hedit->buffer->modified : 0) {
he_message(0,
"@Abno write since last change@~ - use @U:q!@u to override");
return skip;
}
n += !i->visited_f;
}
if (n && warn_more_files) {
/* one warning per session should be enough.
*/
warn_more_files = 0;
he_message(0, "@Ab%i more files to edit@~", n);
return skip;
}
close_all_buffers:
for (i = buffer_list, j = 0; i; j = i, i = i->next)
if (j) he_close_buffer(j->hedit->buffer_name);
if (j) he_close_buffer(j->hedit->buffer_name);
return skip;
}
/* exhcmd_quit */
static char *
exhcmd_close(struct he_s *hedit, char *args)
/* ARGSUSED */
{
int force_f = 0;
int nclosed = 0;
int i;
char *skip, *p;
skip = exh_skipcmd(args, p = (char *)alloca(strlen(args) + 1));
args = p;
while (*args == ' ' || *args == '\t') ++args;
if (*args == '!') ++args, ++force_f;
util_trunc(args);
do {
while (*args == ' ' || *args == '\t') ++args;
if (!*args && !nclosed) {
i = he_close_buffer(current_buffer->hedit->buffer_name);
assert(i <= 0);
++nclosed;
if (i < 0) break;
p = args;
} else {
for (p = args; *p && *p != ' ' && *p != '\t'; ++p);
if (*p) *p++ = 0;
if ((i = he_buffer_modified(args)) < 0)
he_message(0, "@Abno buffer named@~ `%s'", args);
else if (i && !force_f)
he_message(0,
"@Abno write since last change@~ - use @U:c!@u to override");
else {
i = he_close_buffer(args);
assert(i <= 0);
++nclosed;
if (i < 0) break;
}
}
args = p;
} while (*args);
return skip;
}
/* exhcmd_close */
static char *
exhcmd_map(struct he_s *hedit, char *args)
{
return exh_map(hedit, args, MAP_COMMAND);
}
/* exhcmd_map */
static char *
exhcmd_imap(struct he_s *hedit, char *args)
{
return exh_map(hedit, args, MAP_INSERT);
}
/* exhcmd_imap */
static char *
exhcmd_vmap(struct he_s *hedit, char *args)
{
return exh_map(hedit, args, MAP_VISUAL);
}
/* exhcmd_vmap */
static char *
exhcmd_unmap(struct he_s *hedit, char *args)
{
return exh_unmap(hedit, args, MAP_COMMAND);
}
/* exhcmd_unmap */
static char *
exhcmd_iunmap(struct he_s *hedit, char *args)
{
return exh_unmap(hedit, args, MAP_INSERT);
}
/* exhcmd_iunmap */
static char *
exhcmd_vunmap(struct he_s *hedit, char *args)
{
return exh_unmap(hedit, args, MAP_VISUAL);
}
/* exhcmd_vunmap */
static char *
exhcmd_set(struct he_s *hedit, char *args)
{
char *p, *q, *option;
enum s_option_e type;
int no_f;
char *skip;
skip = exh_skipcmd(args, p = (char *)alloca(strlen(args) + 1));
util_trunc(args = p);
if (!*args) {
char **list = s_option_value_list(), **i = list;
if (*list) {
tio_message(list, 1);
press_any_key(hedit);
} else
he_message(0, "no options set");
while (*i) free((char *)*i++);
free((char *)list);
return skip;
}
p = args;
for (; *p;) {
no_f = 0;
option = q = p;
while (*p && *p != ' ' && *p != '\t') ++p;
if (*p) *p++ = 0;
while (*p == ' ' || *p == '\t') ++p;
if (!strncmp(option, "no", 2)) {
option += 2;
no_f = 1;
} else {
while (*q && *q != '=' && *q != ' ' && *q != '\t') ++q;
if (*q == '=') {
if (no_f) {
he_message(0, "@Abinvalid argument:@~ %s", option);
return skip;
}
*q = 0;
} else
q = 0;
}
if (!(type = s_get_type(option))) {
he_message(0, "@Abinvalid option:@~ %s", option);
return skip;
}
if (no_f && type != S_BOOL) {
if (q) *q = '=';
he_message(0, "@Abinvalid argument:@~ %s", option - 2);
return skip;
}
if (q)
s_set_option(option, q + 1, 0);
else {
if (type == S_BOOL)
s_set_option(option, no_f ? "false" : "true", 0);
else {
char *value = s_get_option(option);
if (!*value) value = "<unset>";
he_message(0, "%s=%s@~", option, value);
}
}
}
return skip;
}
/* exhcmd_set */
static char *
exhcmd_delete(struct he_s *hedit, char *args, long begin, long end)
/* ARGSUSED */
{
long count = end - begin;
char *data;
char *skip, *p;
skip = exh_skipcmd(args, p = (char *)alloca(strlen(args) + 1));
util_trunc(args = p);
if (!count) return skip;
data = (char *)malloc(count);
b_read(hedit->buffer, data, begin, count);
if (!kill_buffer)
kill_buffer = new_buffer(0);
else
b_clear(kill_buffer);
b_insert(kill_buffer, 0, count);
b_write(kill_buffer, data, 0, count);
he_subcommand(hedit, 0, begin, count, data);
he_subcommand(hedit, -1, 0, 0, 0);
b_delete(hedit->buffer, begin, count);
if (hedit->position > begin) hedit->position -= count;
he_refresh_part(hedit, begin, -1);
if (count > 1)
he_message(0, "0x%lx (%li) bytes deleted (cut).", count, count);
return skip;
}
/* exhcmd_delete */
static char *
exhcmd_yank(struct he_s *hedit, char *args, long begin, long end)
/* ARGSUSED */
{
long count = end - begin;
char *skip, *p;
skip = exh_skipcmd(args, p = (char *)alloca(strlen(args) + 1));
util_trunc(args = p);
if (kill_buffer) b_clear(kill_buffer); else kill_buffer = new_buffer(0);
if (!count) return skip;
b_insert(kill_buffer, 0, count);
b_copy(kill_buffer, hedit->buffer, 0, begin, count);
if (count > 1)
he_message(0, "0x%lx (%li) bytes yanked.", count, count);
return skip;
}
/* exhcmd_yank */
static char *
exhcmd_version(struct he_s *hedit, char *args)
/* ARGSUSED */
{
char *skip = exh_skipcmd(args, 0);
hexer_version();
return skip;
}
/* exhcmd_version */
static char *
exhcmd_zz(struct he_s *hedit, char *args)
{
int k;
char *skip;
skip = exh_skipcmd(args, 0);
if ((k = HE_LINE(hedit->position - hedit->screen_offset)) < hx_lines / 2 - 1)
he_scroll_down(hedit, (hx_lines / 2 - 1) - k);
if ((k = HE_LINE(hedit->position - hedit->screen_offset)) > hx_lines / 2 - 1)
he_scroll_up(hedit, k - (hx_lines / 2 - 1));
he_update_screen(hedit);
return skip;
}
/* exhcmd_zz */
static char *
exhcmd_zt(struct he_s *hedit, char *args)
{
char *skip;
skip = exh_skipcmd(args, 0);
he_scroll_up(hedit, HE_LINE(hedit->position - hedit->screen_offset));
he_update_screen(hedit);
return skip;
}
/* exhcmd_zt */
static char *
exhcmd_zb(struct he_s *hedit, char *args)
{
char *skip;
skip = exh_skipcmd(args, 0);
he_scroll_down(hedit,
hx_lines - 2 - HE_LINE(hedit->position - hedit->screen_offset));
he_update_screen(hedit);
return skip;
}
/* exhcmd_zb */
static char *
exhcmd_help(struct he_s *hedit, char *args)
{
int pid1, pid2, status, x = 0;
int pipefd[2];
char *vp[64], *p;
char *skip;
int i;
skip = exh_skipcmd(args, 0);
tio_suspend();
tio_clear();
tio_flush();
if (pipe(pipefd)) {
he_message(1, "INTERNAL ERROR: `pipe()' failed.");
goto exit_exhcmd_help;
}
if ((pid1 = fork()) < 0) { /* `fork()' failed */
he_message(1, "INTERNAL ERROR: `fork()' failed.");
close(pipefd[0]);
close(pipefd[1]);
goto exit_exhcmd_help;
} else if (!pid1) { /* child */
close(pipefd[1]);
close(0);
if (dup(pipefd[0]) == -1)
exit(EXIT_EXEC_FAILED);
p = (char *)alloca(strlen(he_pagerprg) + 1);
strcpy(p, he_pagerprg);
for (vp[i = 0] = p; *p; ++p)
if (*p == ' ') {
for (*p++ = 0; *p == ' ' || *p == '\t'; ++p);
if (!*p) break;
vp[++i] = p;
}
vp[++i] = 0;
execvp(*vp, vp);
exit(EXIT_EXEC_FAILED);
}
if ((pid2 = fork()) < 0) {
he_message(1, "INTERNAL ERROR: `fork()' failed.");
close(pipefd[0]);
close(pipefd[1]);
goto exit_exhcmd_help;
} else if (!pid2) { /* child */
size_t len, n;
ssize_t res;
for (n = 0, len = strlen(helptext); n < len; n += res)
if ((res = write(pipefd[1], helptext + n, len - n)) == -1)
exit(1);
close(pipefd[1]);
exit(0);
}
close(pipefd[1]);
do if (waitpid(pid1, &status, 0) >= 0) break; while (errno == ERESTARTSYS);
kill(pid2, SIGKILL);
do if (waitpid(pid2, 0, 0) >= 0) break; while (errno == ERESTARTSYS);
close(pipefd[0]);
if ((x = WEXITSTATUS(status))) {
if (x == EXIT_EXEC_FAILED)
he_message(1, "couldn't start pager program `%s'", he_pagerprg);
else
he_message(1, "%s exited with code %i", he_pagerprg, x);
}
exit_exhcmd_help:
tio_restart();
he_refresh_all(hedit);
return skip;
}
/* exhcmd_help */
static char *
exhcmd_exit(struct he_s *hedit, char *args)
{
struct buffer_s *i;
int cant_write_f = 0;
long k;
char *skip, *p;
skip = exh_skipcmd(args, p = (char *)alloca(strlen(args) + 1));
util_trunc(args = p);
for (i = buffer_list; i; i = i->next)
if (i->loaded_f && i->path ? i->hedit->buffer->modified : 0) {
if (i->hedit->read_only) {
he_message(0, "@Abbuffer@~ `%s' @Abis readonly@~ - not written",
i->hedit->buffer_name);
cant_write_f = 1;
} else {
k = b_write_buffer_to_file(i->hedit->buffer, i->path);
if (k < 0) {
he_message(0, "@Aberror writing file@~ `%s': %s", i->path,
strerror(errno));
cant_write_f = 1;
} else {
he_message(0, "wrote `%s' - 0x%lx (%li) bytes", i->path, k, k);
hedit->buffer->modified = 0;
}
}
}
if (!cant_write_f) exhcmd_quit(hedit, "");
return skip;
}
/* exhcmd_exit */
const struct exh_cmd_s exh_commands[] = {
{ "substitute", 's', (exh_fn)exhcmd_substitute, 1, 0 },
{ "write", 'w', (exh_fn)exhcmd_write, 1, 1 },
{ "read", 'r', (exh_fn)exhcmd_read, 0, 1 },
{ "edit", 'e', (exh_fn)exhcmd_edit, 1, 3 },
{ "buffer", 'b', (exh_fn)exhcmd_buffer, 1, 2 },
{ "next", 'n', (exh_fn)exhcmd_next, 0, 0 },
{ "previous", 'N', (exh_fn)exhcmd_previous, 0, 0 },
{ "rewind", 0, (exh_fn)exhcmd_rewind, 0, 0 },
{ "skip", 'S', (exh_fn)exhcmd_skip, 0, 0 },
{ "wn", 0, (exh_fn)exhcmd_wn, 1, 1 },
{ "wall", 0, (exh_fn)exhcmd_wall, 0, 0 },
{ "quit", 'q', (exh_fn)exhcmd_quit, 0, 0 },
{ "close", 'c', (exh_fn)exhcmd_close, 0, 2 },
{ "map", 0, (exh_fn)exhcmd_map, 0, 0 },
{ "imap", 0, (exh_fn)exhcmd_imap, 0, 0 },
{ "vmap", 0, (exh_fn)exhcmd_vmap, 0, 0 },
{ "unmap", 0, (exh_fn)exhcmd_unmap, 0, 0 },
{ "iunmap", 0, (exh_fn)exhcmd_iunmap, 0, 0 },
{ "vunmap", 0, (exh_fn)exhcmd_vunmap, 0, 0 },
{ "set", 0, (exh_fn)exhcmd_set, 0, 4 },
{ "delete", 'd', (exh_fn)exhcmd_delete, 0, 0 },
{ "yank", 'y', (exh_fn)exhcmd_yank, 0, 0 },
{ "version", 0, (exh_fn)exhcmd_version, 0, 0 },
{ "zz", 0, (exh_fn)exhcmd_zz, 0, 0 },
{ "zt", 0, (exh_fn)exhcmd_zt, 0, 0 },
{ "zb", 0, (exh_fn)exhcmd_zb, 0, 0 },
{ "wq", 0, (exh_fn)exhcmd_exit, 0, 0 },
{ "help", 'h', (exh_fn)exhcmd_help, 0, 0 },
{ "exit", 'x', (exh_fn)exhcmd_exit, 0, 0 },
{ 0, 0, (exh_fn)0, 0, 0 }
};
/* end of commands.c */
/* VIM configuration: (do not delete this line)
*
* vim:bk:nodg:efm=%f\:%l\:%m:hid:icon:
* vim:sw=2:sm:textwidth=79:ul=1024:wrap:
*/
|