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
|
#include "tweak.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
static void act_exit (void);
static void act_save (void);
static void act_exitsave (void);
static void act_top (void);
static void act_pgup (void);
static void act_up (void);
static void act_home (void);
static void act_left (void);
static void act_right (void);
static void act_end (void);
static void act_down (void);
static void act_pgdn (void);
static void act_bottom (void);
static void act_togins (void);
static void act_chmode (void);
extern void act_self_ins (void); /* this one must be external */
static void act_delete (void);
static void act_delch (void);
static void act_mark (void);
static void act_cut (void);
static void act_copy (void);
static void act_paste (void);
static void act_susp (void);
static void act_goto (void);
static void act_togstat (void);
static void act_search (void);
static void act_search_backwards (void);
static void act_recentre (void);
static void act_width (void);
static void act_offset (void);
#ifdef TEST_BUFFER
static void act_diagnostics (void);
#endif
static Search *last_search = NULL;
keyact parse_action (char *name) {
char *names[] = {
"exit", "top-of-file", "page-up", "move-up",
"begin-line", "move-left", "move-right", "end-line",
"move-down", "page-down", "bottom-of-file", "toggle-insert",
"change-mode", "delete-left", "delete-right", "mark-place",
"cut", "copy", "paste", "suspend", "goto-position",
"toggle-status", "search", "search-back", "save-file",
"exit-and-save", "screen-recentre", "new-width", "new-offset"
#ifdef TEST_BUFFER
, "diagnostics"
#endif
};
keyact actions[] = {
act_exit, act_top, act_pgup, act_up, act_home, act_left,
act_right, act_end, act_down, act_pgdn, act_bottom,
act_togins, act_chmode, act_delete, act_delch, act_mark,
act_cut, act_copy, act_paste, act_susp, act_goto,
act_togstat, act_search, act_search_backwards, act_save,
act_exitsave, act_recentre, act_width, act_offset
#ifdef TEST_BUFFER
, act_diagnostics
#endif
};
int i;
for (i=0; i<sizeof(names)/sizeof(*names); i++)
if (!strcmp(name, names[i]))
return actions[i];
return NULL;
}
static fileoffset_t begline(fileoffset_t x) {
fileoffset_t y = x + width-offset;
y -= (y % width);
y -= width-offset;
if (y < 0)
y = 0;
return y;
}
static fileoffset_t endline(fileoffset_t x) {
fileoffset_t y = x + width-offset;
y -= (y % width);
y += width-1;
y -= width-offset;
if (y < 0)
y = 0;
return y;
}
static void act_exit(void) {
static char question[] = "File is modified. Save before quitting? [yn] ";
if (modified) {
int c;
display_moveto (display_rows-1, 0);
display_clear_to_eol ();
display_set_colour (COL_MINIBUF);
display_write_str (question);
display_refresh();
do {
#if defined(unix) && !defined(GO32)
if (update_required) {
update();
display_moveto (display_rows-1, 0);
display_clear_to_eol ();
display_set_colour (COL_MINIBUF);
display_write_str (question);
display_refresh();
}
safe_update = TRUE;
#endif
c = display_getkey();
#if defined(unix) && !defined(GO32)
safe_update = FALSE;
#endif
if (c >= 'a' && c <= 'z')
c += 'A'-'a';
} while (c != 'Y' && c != 'N' && c != '\007');
if (c == 'Y') {
act_save();
if (modified)
return; /* couldn't save, so don't quit */
draw_scr(); /* update the ** on status line! */
} else if (c == '\007') {
return; /* don't even quit */
}
}
finished = TRUE;
}
static void act_save(void) {
static int backed_up = FALSE;
if (!backed_up) {
if (!backup_file()) {
display_beep();
strncpy (message, "Unable to back up file!", sizeof(message) - 1);
return;
}
backed_up = TRUE;
}
if (!save_file()) {
display_beep();
strncpy (message, "Unable to save file!", sizeof(message) - 1);
return;
}
modified = FALSE;
}
static void act_exitsave(void) {
act_save();
draw_scr(); /* update ** on status line */
act_exit();
}
static void act_top (void) {
cur_pos = top_pos = 0;
edit_type = !!edit_type;
}
static void act_pgup(void) {
cur_pos -= (scrlines-1)*width;
if (cur_pos < 0) {
cur_pos = 0;
edit_type = !!edit_type;
}
if (top_pos > cur_pos)
top_pos = begline(cur_pos);
}
static void act_up(void) {
cur_pos -= width;
if (cur_pos < 0) {
cur_pos = 0;
edit_type = !!edit_type;
}
if (top_pos > cur_pos)
top_pos = begline(cur_pos);
}
static void act_home(void) {
cur_pos = begline(cur_pos);
if (cur_pos < 0)
cur_pos = 0;
if (top_pos > cur_pos)
top_pos = begline(cur_pos);
edit_type = !!edit_type;
}
static void act_left(void) {
if (edit_type == 2) {
edit_type = 1;
return;
} else {
cur_pos--;
edit_type = 2*!!edit_type;
if (cur_pos < 0) {
cur_pos = 0;
edit_type = !!edit_type;
}
if (top_pos > cur_pos)
top_pos = begline(cur_pos);
}
}
static void act_right(void) {
fileoffset_t new_top;
if (edit_type == 1) {
if (cur_pos < file_size)
edit_type = 2;
return;
} else {
cur_pos++;
if (cur_pos > file_size)
cur_pos = file_size;
new_top = cur_pos - (scrlines-1) * width;
if (new_top < 0)
new_top = 0;
new_top = begline(new_top);
if (top_pos < new_top)
top_pos = new_top;
edit_type = !!edit_type;
}
}
static void act_end(void) {
fileoffset_t new_top;
cur_pos = endline(cur_pos);
edit_type = !!edit_type;
if (cur_pos >= file_size)
cur_pos = file_size;
new_top = cur_pos - (scrlines-1) * width;
if (new_top < 0)
new_top = 0;
new_top = begline(new_top);
if (top_pos < new_top)
top_pos = new_top;
}
static void act_down(void) {
fileoffset_t new_top;
cur_pos += width;
if (cur_pos >= file_size) {
cur_pos = file_size;
edit_type = !!edit_type;
}
new_top = cur_pos - (scrlines-1) * width;
if (new_top < 0)
new_top = 0;
new_top = begline(new_top);
if (top_pos < new_top)
top_pos = new_top;
}
static void act_pgdn(void) {
fileoffset_t new_top;
cur_pos += (scrlines-1) * width;
if (cur_pos >= file_size) {
cur_pos = file_size;
edit_type = !!edit_type;
}
new_top = cur_pos - (scrlines-1) * width;
if (new_top < 0)
new_top = 0;
new_top = begline(new_top);
if (top_pos < new_top)
top_pos = new_top;
}
static void act_bottom (void) {
fileoffset_t new_top;
cur_pos = file_size;
edit_type = !!edit_type;
new_top = cur_pos - (scrlines-1) * width;
if (new_top < 0)
new_top = 0;
new_top = begline(new_top);
if (top_pos < new_top)
top_pos = new_top;
}
static void act_togins(void) {
if (look_mode || fix_mode) {
display_beep();
snprintf(message, sizeof(message), "Can't engage Insert mode when in %s mode",
(look_mode ? "LOOK" : "FIX"));
insert_mode = FALSE; /* safety! */
} else
insert_mode = !insert_mode;
}
static void act_chmode(void) {
if (ascii_enabled)
edit_type = !edit_type; /* 0 -> 1, [12] -> 0 */
else if (edit_type == 0) /* just in case */
edit_type = 1;
}
void act_self_ins(void) {
int insert = insert_mode;
unsigned char c;
if (look_mode) {
display_beep();
strncpy (message, "Can't modify file in LOOK mode", sizeof(message - 1));
return;
}
if (edit_type) {
if (last_char >= '0' && last_char <= '9')
last_char -= '0';
else if (last_char >= 'A' && last_char <= 'F')
last_char -= 'A'-10;
else if (last_char >= 'a' && last_char <= 'f')
last_char -= 'a'-10;
else {
display_beep();
strncpy(message, "Not a valid character when in hex editing mode", sizeof(message) - 1);
return;
}
}
if ( (!insert || edit_type == 2) && cur_pos == file_size) {
display_beep();
strncpy(message, "End of file reached", sizeof(message) - 1);
return;
}
switch (edit_type) {
case 0: /* ascii mode */
c = last_char;
break;
case 1: /* hex, first digit */
if (insert)
c = 0;
else
buf_fetch_data(filedata, &c, 1, cur_pos);
c &= 0xF;
c |= 16 * last_char;
break;
case 2: /* hex, second digit */
buf_fetch_data(filedata, &c, 1, cur_pos);
c &= 0xF0;
c |= last_char;
insert = FALSE;
break;
}
if (insert) {
buf_insert_data(filedata, &c, 1, cur_pos);
file_size++;
modified = TRUE;
} else if (cur_pos < file_size) {
buf_overwrite_data(filedata, &c, 1, cur_pos);
modified = TRUE;
} else {
display_beep();
strncpy(message, "End of file reached", sizeof(message) - 1);
}
act_right();
}
static void act_delete(void) {
if (!insert_mode || (edit_type!=2 && cur_pos==0)) {
display_beep();
strncpy (message, "Can't delete while not in Insert mode", sizeof(message) - 1);
} else if (cur_pos > 0 || edit_type == 2) {
act_left();
buf_delete (filedata, 1, cur_pos);
file_size--;
edit_type = !!edit_type;
modified = TRUE;
}
}
static void act_delch(void) {
if (!insert_mode) {
display_beep();
strncpy (message, "Can't delete while not in Insert mode", sizeof(message) - 1);
} else if (cur_pos < file_size) {
buf_delete (filedata, 1, cur_pos);
file_size--;
edit_type = !!edit_type;
modified = TRUE;
}
}
static void act_mark (void) {
if (look_mode) {
display_beep();
strncpy (message, "Can't cut or paste in LOOK mode", sizeof(message) - 1);
marking = FALSE; /* safety */
return;
}
marking = !marking;
mark_point = cur_pos;
}
static void act_cut (void) {
fileoffset_t marktop, marksize;
if (!marking || mark_point==cur_pos) {
display_beep();
strncpy (message, "Set mark first", sizeof(message) - 1);
return;
}
if (!insert_mode) {
display_beep();
strncpy (message, "Can't cut while not in Insert mode", sizeof(message) - 1);
return;
}
marktop = cur_pos;
marksize = mark_point - cur_pos;
if (marksize < 0) {
marktop += marksize;
marksize = -marksize;
}
if (cutbuffer)
buf_free (cutbuffer);
cutbuffer = buf_cut (filedata, marksize, marktop);
file_size -= marksize;
cur_pos = marktop;
if (cur_pos < 0)
cur_pos = 0;
if (top_pos > cur_pos)
top_pos = begline(cur_pos);
edit_type = !!edit_type;
modified = TRUE;
marking = FALSE;
}
static void act_copy (void) {
fileoffset_t marktop, marksize;
if (!marking) {
display_beep();
strncpy (message, "Set mark first", sizeof(message) - 1);
return;
}
marktop = cur_pos;
marksize = mark_point - cur_pos;
if (marksize < 0) {
marktop += marksize;
marksize = -marksize;
}
if (cutbuffer)
buf_free (cutbuffer);
cutbuffer = buf_copy (filedata, marksize, marktop);
marking = FALSE;
}
static void act_paste (void) {
fileoffset_t cutsize, new_top;
if (!cutbuffer)
return;
cutsize = buf_length (cutbuffer);
if (!insert_mode) {
if (cur_pos + cutsize > file_size) {
display_beep();
strncpy (message, "Too close to end of file to paste", sizeof(message) - 1);
return;
}
buf_delete (filedata, cutsize, cur_pos);
file_size -= cutsize;
}
buf_paste (filedata, cutbuffer, cur_pos);
modified = TRUE;
cur_pos += cutsize;
file_size += cutsize;
edit_type = !!edit_type;
new_top = cur_pos - (scrlines-1) * width;
if (new_top < 0)
new_top = 0;
new_top = begline(new_top);
if (top_pos < new_top)
top_pos = new_top;
}
static void act_susp (void) {
suspend();
}
static void act_goto (void) {
char buffer[80];
fileoffset_t position, new_top;
int error;
if (!get_str("Enter position to go to: ", buffer, FALSE))
return; /* user break */
position = parse_num (buffer, &error);
if (error) {
display_beep();
strncpy (message, "Unable to parse position value", sizeof(message) - 1);
return;
}
if (position < 0 || position > file_size) {
display_beep();
strncpy (message, "Position is outside bounds of file", sizeof(message) - 1);
return;
}
cur_pos = position;
edit_type = !!edit_type;
new_top = cur_pos - (scrlines-1) * width;
if (new_top < 0)
new_top = 0;
new_top = begline(new_top);
if (top_pos > cur_pos)
top_pos = begline(cur_pos);
if (top_pos < new_top)
top_pos = new_top;
}
static void act_togstat (void) {
if (statfmt == decstatus)
statfmt = hexstatus;
else
statfmt = decstatus;
}
static int search_prompt(char *withdef, char *withoutdef)
{
char buffer[80];
int len;
if (!get_str(last_search ? withdef : withoutdef, buffer, TRUE))
return 0; /* user break */
if (!last_search && !*buffer) {
strncpy (message, "Search aborted.", sizeof(message) - 1);
return 0;
}
if (!*buffer) {
len = last_search->len;
} else {
len = parse_quoted (buffer);
if (len == -1) {
display_beep();
strncpy (message, "Invalid escape sequence in search string", sizeof(message) - 1);
return 0;
}
if (last_search)
free_search(last_search);
last_search = build_search (buffer, len);
}
return 1;
}
static void act_search (void) {
int len;
fileoffset_t posn, dfapos;
DFA dfa;
static unsigned char sblk[SEARCH_BLK];
static char withdef[] = "Search forward (default=last): ";
static char withoutdef[] = "Search forward: ";
if (!search_prompt(withdef, withoutdef))
return;
dfa = last_search->forward;
len = last_search->len;
dfapos = 0;
for (posn = cur_pos+1; posn < file_size; posn++) {
unsigned char *q;
int size = SEARCH_BLK;
if (size > file_size-posn)
size = file_size-posn;
buf_fetch_data (filedata, sblk, size, posn);
q = sblk;
while (size--) {
posn++;
dfapos = dfa[dfapos][*q++];
if (dfapos == len) {
fileoffset_t new_top;
cur_pos = posn - len;
edit_type = !!edit_type;
new_top = cur_pos - (scrlines-1) * width;
new_top = begline(new_top);
if (top_pos < new_top)
top_pos = new_top;
return;
}
}
}
strncpy (message, "Not found.", sizeof(message) - 1);
}
static void act_search_backwards (void) {
int len;
fileoffset_t posn, dfapos;
DFA dfa;
static unsigned char sblk[SEARCH_BLK];
static char withdef[] = "Search backward (default=last): ";
static char withoutdef[] = "Search backward: ";
if (!search_prompt(withdef, withoutdef))
return;
dfa = last_search->reverse;
len = last_search->len;
dfapos = 0;
posn = cur_pos + len - 1;
if (posn >= file_size)
posn = file_size;
for (; posn >= 0; posn--) {
unsigned char *q;
int size = SEARCH_BLK;
if (size > posn)
size = posn;
buf_fetch_data (filedata, sblk, size, posn-size);
q = sblk + size;
while (size--) {
posn--;
dfapos = dfa[dfapos][*--q];
if (dfapos == len) {
fileoffset_t new_top;
cur_pos = posn;
edit_type = !!edit_type;
new_top = cur_pos - (scrlines-1) * width;
new_top = begline(new_top);
if (top_pos > new_top)
top_pos = new_top;
return;
}
}
}
strncpy (message, "Not found.", sizeof(message) - 1);
}
static void act_recentre (void) {
top_pos = cur_pos - (display_rows-2)/2 * width;
if (top_pos < 0)
top_pos = 0;
top_pos = begline(top_pos);
}
static void act_width (void) {
char buffer[80];
char prompt[80];
fileoffset_t w;
fileoffset_t new_top;
int error;
snprintf (prompt, sizeof(prompt), "Enter screen width in bytes (now %"OFF"d): ", width);
if (!get_str (prompt, buffer, FALSE))
return;
w = parse_num (buffer, &error);
if (error) {
display_beep();
strncpy (message, "Unable to parse width value", sizeof(message) - 1);
return;
}
if (w > 0) {
width = w;
fix_offset();
new_top = cur_pos - (scrlines-1) * width;
new_top = begline(new_top);
if (top_pos < new_top)
top_pos = new_top;
}
}
static void act_offset (void) {
char buffer[80];
char prompt[80];
fileoffset_t o;
fileoffset_t new_top;
int error;
snprintf (prompt, sizeof(prompt), "Enter start-of-file offset in bytes (now %"OFF"d): ",
realoffset);
if (!get_str (prompt, buffer, FALSE))
return;
o = parse_num (buffer, &error);
if (error) {
display_beep();
strncpy (message, "Unable to parse offset value", sizeof(message) - 1);
return;
}
if (o >= 0) {
realoffset = o;
fix_offset();
new_top = cur_pos - (scrlines-1) * width;
new_top = begline(new_top);
if (top_pos < new_top)
top_pos = new_top;
}
}
#ifdef TEST_BUFFER
static void act_diagnostics(void)
{
extern void buffer_diagnostic(buffer *buf, char *title);
buffer_diagnostic(filedata, "filedata");
buffer_diagnostic(cutbuffer, "cutbuffer");
}
#endif
|