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
|
/*
* File: cmd-file.c
* Purpose: Functions that implement file commands.
* Author: Lars Wirzenius
* Version: "@(#)SeX:$Id: cmd-file.c,v 1.52 1999/08/07 21:28:49 liw Exp $"
*/
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <publib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include "anchor.h"
#include "config.h"
#include "cmd.h"
#include "buflist.h"
#include "filewin.h"
#include "selections.h"
#include "win.h"
#include "error.h"
#include "x.h"
/*
* How much to read/write at a time.
*/
#define READ_BYTES 1024
#define WRITE_BYTES 1024
/*
* Structure for passing around information about the command for piping
* selection through.
*/
struct cmd {
char *cmdline;
int pid;
Sbufmark *sel;
};
/*
* Prototypes
*/
static void pipe_through_cmd(char *, long, void *);
static int start_cmd(struct cmd *, int *, int *);
static int kill_cmd(struct cmd *);
static int check_read_write(int, int);
static int read_some(int, struct dynarr *);
static int write_some(int, char *, long, long *);
/*
* Function: cmd_new
* Purpose: Create a new buffer and make it visible in window.
*/
int cmd_new(struct win *win) {
Sbuf *buf;
anchor_up(win);
cmd_prev_was_cut = 0;
buf = create_buffer();
if (buf == NULL)
return -1;
if (win_set_buf(win, buf) == -1) {
destroy_buffer(buf);
return -1;
}
return 0;
}
/*
* Function: cmd_next_file
* Purpose: Change visible buffer to the next one.
*/
int cmd_next_file(struct win *win) {
Sbuf *buf;
anchor_up(win);
cmd_prev_was_cut = 0;
buf = buflist_next(win_buf(win));
if (win_set_buf(win, buf) == -1)
return -1;
if (!sbuf_has_flags(buf, SBUF_LOADED_FLAG)) {
if (sbuf_load(buf) == -1) {
error(win, "Error loading file into memory: %s",
sbuf_get_name(buf));
return -1;
}
sbuf_remark(win_selection(win), 0, 0);
sbuf_mark_set_columnar(win_selection(win), 0);
}
return 0;
}
/*
* Function: cmd_prev_file
* Purpose: Change visible buffer to previous one.
*/
int cmd_prev_file(struct win *win) {
Sbuf *buf;
anchor_up(win);
cmd_prev_was_cut = 0;
buf = buflist_prev(win_buf(win));
if (win_set_buf(win, buf) == -1)
return -1;
if (!sbuf_has_flags(buf, SBUF_LOADED_FLAG)) {
if (sbuf_load(buf) == -1) {
error(win, "Error loading file into memory: %s",
sbuf_get_name(buf));
return -1;
}
sbuf_remark(win_selection(win), 0, 0);
sbuf_mark_set_columnar(win_selection(win), 0);
}
return 0;
}
/*
* Function: cmd_query_load_file
* Purpose: Popup dialog to ask for filename to be loaded.
*/
int cmd_query_load_file(struct win *win) {
struct filewin *fw;
Sbuf *buf;
char *s;
anchor_up(win);
cmd_prev_was_cut = 0;
fw = win_filewin(win, WIN_LOAD);
buf = win_buf(win);
s = sbuf_get_name(buf);
if (s == NULL)
s = "";
filewin_set_filename(fw, s);
filewin_popup(fw);
return 0;
}
/*
* Function: cmd_set_name_and_load_file
* Purpose: Set current filename from dialog and load it into buffer.
*/
int cmd_set_name_and_load_file(struct win *win) {
struct filewin *fw;
Sbuf *buf, *this;
char *name, *p;
char fullname[1024]; /* xxx boo hiss */
size_t n;
anchor_up(win);
cmd_prev_was_cut = 0;
fw = win_filewin(win, WIN_LOAD);
name = filewin_filename(fw);
n = fnqualify(fullname, name, sizeof(fullname));
if (n > sizeof(fullname)) {
error(win, "Filename too long");
return -1;
}
buf = this = win_buf(win);
if (buf != NULL) {
do {
p = sbuf_get_name(buf);
if (p != NULL && strcmp(p, fullname) == 0)
return win_set_buf(win, buf);
buf = buflist_by_name(fullname);
} while (buf != NULL && buf != this);
}
buf = create_buffer();
if (buf == NULL)
return -1;
if (sbuf_set_name(buf, fullname) == -1 || win_set_buf(win, buf) == -1){
destroy_buffer(buf);
return -1;
}
return cmd_load_file(win);
}
/*
* Function: cmd_load_file
* Purpose: Load file into buffer.
*/
int cmd_load_file(struct win *win) {
Sbuf *buf;
anchor_up(win);
cmd_prev_was_cut = 0;
buf = win_buf(win);
if (sbuf_load(buf) == -1) {
error(win, "Error loading file into memory: %s",
sbuf_get_name(buf));
return -1;
}
sbuf_remark(win_selection(win), 0, 0);
sbuf_mark_set_columnar(win_selection(win), 0);
win_show(win, 0);
return 0;
}
/*
* Function: cmd_reload_file
* Purpose: Load file into buffer again.
*/
int cmd_reload_file(struct win *win) {
Sbuf *buf;
anchor_up(win);
cmd_prev_was_cut = 0;
buf = win_buf(win);
if (sbuf_is_dirty(buf)) {
error(win, "Can't reload dirty buffer: mark unmodifed first");
return -1;
}
if (sbuf_load(buf) == -1) {
error(win, "Error loading file: %100s", strerror(errno));
return -1;
}
sbuf_remark(win_selection(win), 0, 0);
sbuf_mark_set_columnar(win_selection(win), 0);
win_show(win, 0);
return 0;
}
/*
* Function: cmd_load_selection
* Purpose: Load file given in selection.
*/
static void do_load(char *str, long len, void *win) {
filewin_set_filename(win_filewin(win, WIN_LOAD), str);
(void) cmd_set_name_and_load_file(win);
}
int cmd_load_selection(struct win *win) {
anchor_up(win);
cmd_prev_was_cut = 0;
if (sel_string(win, do_load, win) == -1)
return -1;
return 0;
}
/*
* Function: cmd_save_as
* Purpose: Popup dialog to ask for new filename.
*/
int cmd_save_as(struct win *win) {
struct filewin *fw;
Sbuf *buf;
char *s;
cmd_prev_was_cut = 0;
fw = win_filewin(win, WIN_SAVE);
buf = win_buf(win);
s = sbuf_get_name(buf);
if (s == NULL)
s = "";
filewin_set_filename(fw, s);
filewin_popup(fw);
return 0;
}
/*
* Function: cmd_set_name_and_save_file
* Purpose: Set current filename from dialog and save it.
*/
int cmd_set_name_and_save_file(struct win *win) {
struct filewin *fw;
int ret;
char fullname[1024]; /* xxx boo hiss */
size_t n;
cmd_prev_was_cut = 0;
fw = win_filewin(win, WIN_SAVE);
n = fnqualify(fullname, filewin_filename(fw), sizeof(fullname));
if (n > sizeof(fullname)) {
error(win, "Filename too long");
return -1;
}
ret = win_set_filename(win, fullname);
if (ret != -1)
ret = cmd_save_file(win);
return ret;
}
/*
* Function: cmd_save_file
* Purpose: Save buffer to file.
*/
int cmd_save_file(struct win *win) {
Sbuf *buf;
int bak;
cmd_prev_was_cut = 0;
buf = win_buf(win);
bak = config_get_boolean(CONFIG_SAVE_BACKUP);
if (sbuf_get_name(buf) == NULL) {
error(win, "Error: can't save (buffer has no name)");
return -1;
}
if (sbuf_save(buf, bak) == -1) {
error(win, "Error: couldn't save (%s)", strerror(errno));
sbuf_set_flags(buf, SBUF_DIRTY_FLAG); /* not saved! */
return -1;
}
return 0;
}
/*
* Function: cmd_fake_save
* Purpose: Save buffer to file.
*/
int cmd_fake_save(struct win *win) {
cmd_prev_was_cut = 0;
sbuf_set_dirty(win_buf(win), 0);
return 0;
}
/*
* Function: cmd_save_all
* Purpose: Save all buffers to file.
*/
int cmd_save_all(struct win *win) {
Sbuf *buf, *first;
int backups;
char *p;
cmd_prev_was_cut = 0;
backups = config_get_boolean(CONFIG_SAVE_BACKUP);
buf = first = buflist_first();
if (buf != NULL) {
do {
if (sbuf_is_dirty(buf) && sbuf_save(buf, backups) == -1) {
p = sbuf_get_name(buf);
if (p == NULL)
p = "(untitled)";
error(win, "Error: saving file %s", p);
return -1;
}
buf = buflist_next(buf);
} while (buf != first);
}
return 0;
}
/*
* Function: cmd_query_insert_file
* Purpose: Popup dialog to let user enter name of file to be inserted.
*/
int cmd_query_insert_file(struct win *win) {
struct filewin *fw;
anchor_up(win);
cmd_prev_was_cut = 0;
fw = win_filewin(win, WIN_INSERT);
filewin_popup(fw);
return 0;
}
/*
* Function: cmd_insert_file
* Purpose: Replace selection with the contents of the requested file.
*/
int cmd_insert_file(struct win *win) {
FILE *f;
int ret;
struct filewin *fw;
char fullname[1024]; /* xxx boo hiss */
size_t n;
anchor_up(win);
cmd_prev_was_cut = 0;
fw = win_filewin(win, WIN_INSERT);
n = fnqualify(fullname, filewin_filename(fw), sizeof(fullname));
if (n > sizeof(fullname)) {
error(win, "Filename too long");
return -1;
}
f = fopen(fullname, "r");
if (f == NULL) {
error(win, "error opening file: %.512s", strerror(errno));
return -1;
}
ret = sbuf_insert_file(win_selection(win), f);
if (ret == -1)
error(win, "error inserting file: %.512s", strerror(errno));
if (fclose(f) == EOF) {
if (ret == 0)
error(win, "error closing file: %.512s",
strerror(errno));
ret = -1;
}
return ret;
}
/*
* Function: cmd_query_write_to
* Purpose: Popup dialog to let user enter file selection is to saved to.
*/
int cmd_query_write_to(struct win *win) {
cmd_prev_was_cut = 0;
filewin_popup(win_filewin(win, WIN_WRITE_TO));
return 0;
}
/*
* Function: cmd_write_to
* Purpose: Write selection to the requested file.
*/
int cmd_write_to(struct win *win) {
Sbuf *buf;
struct filewin *fw;
char fullname[1024]; /* xxx boo hiss */
size_t n;
cmd_prev_was_cut = 0;
fw = win_filewin(win, WIN_WRITE_TO);
n = fnqualify(fullname, filewin_filename(fw), sizeof(fullname));
if (n > sizeof(fullname)) {
error(win, "Filename too long");
return -1;
}
buf = win_buf(win);
if (sbuf_write_to(win_selection(win), fullname) == -1) {
error(win, "Error writing selection: %s", strerror(errno));
return -1;
}
return 0;
}
/*
* Function: cmd_query_pipe
* Purpose: Popup dialog to let user enter name of command to pipe
* selection through.
*/
int cmd_query_pipe(struct win *win) {
struct filewin *fw;
anchor_up(win);
cmd_prev_was_cut = 0;
fw = win_filewin(win, WIN_PIPE);
filewin_popup(fw);
return 0;
}
/*
* Function: cmd_pipe
* Purpose: Pipe selection through command.
*/
int cmd_pipe(struct win *win) {
struct cmd *cmd;
anchor_up(win);
cmd_prev_was_cut = 0;
cmd = malloc(sizeof(struct cmd));
if (cmd == NULL) {
error(win, "Error: out of memory before piping");
return -1;
}
cmd->cmdline = filewin_filename(win_filewin(win, WIN_PIPE));
cmd->sel = win_selection(win);
return sel_string(win, pipe_through_cmd, cmd);
}
/*
* Function: cmd_format_selection
* Purpose: Pipe selection through "fmt". Select para, if no selection.
*/
int cmd_format_selection(struct win *win) {
struct cmd *cmd;
anchor_up(win);
cmd_prev_was_cut = 0;
cmd = malloc(sizeof(struct cmd));
if (cmd == NULL) {
error(win, "Error: out of memory before piping");
return -1;
}
cmd->cmdline = config_get_string(CONFIG_FMT_PROGRAM);
cmd->sel = win_selection(win);
if (sbuf_mark_length(cmd->sel) == 0) {
if (cmd_select_para(win) == -1 || sel_own(win, -1) == -1) {
free(cmd);
return -1;
}
}
if (cmd_copy(win) == -1) {
free(cmd);
return -1;
}
return sel_string(win, pipe_through_cmd, cmd);
}
/*
* Function. cmd_kill_buffer
* Purpose: Kill current buffer, exit if it was the last one.
*/
int cmd_kill_buffer(struct win *win) {
Sbuf *buf;
anchor_up(win);
cmd_prev_was_cut = 0;
buf = win_buf(win);
if (sbuf_is_dirty(buf)) {
error(win, "Can't kill modified buffer");
return -1;
}
if (buflist_next(buf) == buf)
return cmd_quit(win);
if (win_change_buf_all(buf, buflist_next(buf)) == -1)
return -1;
destroy_buffer(buf);
return 0;
}
/***********************************************************************
* Local functions.
*/
/*
* Function: pipe_through_cmd
*/
static void pipe_through_cmd(char *sel, long len, void *arg) {
struct cmd *cmd;
int x, ret, fromcmd, tocmd;
long n;
struct dynarr output;
cmd = arg;
if (start_cmd(cmd, &tocmd, &fromcmd) == -1) {
error(0, "Error: Can't start command to pipe through");
return;
}
n = 0;
ret = 1;
dynarr_init(&output, 1);
while (tocmd != -1 || fromcmd != -1) {
assert(n <= len);
x = check_read_write(tocmd, fromcmd);
if (x == -1)
ret = -1;
else if (x == tocmd) {
ret = write_some(tocmd, sel, len, &n);
if (ret == -1 || n == len) {
(void) close(tocmd);
tocmd = -1;
}
} else {
ret = read_some(fromcmd, &output);
if (ret <= 0) {
(void) close(fromcmd);
fromcmd = -1;
}
}
}
(void) kill_cmd(cmd);
if (output.used == 0)
ret = sbuf_strchange(cmd->sel, "", 0);
else
ret = sbuf_strchange(cmd->sel, output.data, output.used);
if (ret == -1)
error(0, "Error: Out of memory processing piped data");
dynarr_free(&output);
free(cmd);
}
/*
* Function: start_cmd
* Purpose: Start the command through which text is piped through.
*/
#define CLOSE_PIPE(p) ((void) close(p[0]), (void) close(p[1]))
static int start_cmd(struct cmd *cmd, int *tocmd, int *fromcmd) {
int p[2], q[2];
if (pipe(p) == -1)
return -1;
if (pipe(q) == -1) {
CLOSE_PIPE(p);
return -1;
}
cmd->pid = fork();
switch (cmd->pid) {
case -1:
CLOSE_PIPE(p);
CLOSE_PIPE(q);
return -1;
case 0:
if (dup2(p[0], 0) == -1 || dup2(q[1], 1) == -1)
exit(1);
(void) close(p[1]);
(void) close(q[0]);
execl("/bin/sh", "/bin/sh", "-c", cmd->cmdline, (char *) 0);
exit(1);
default:
*tocmd = p[1];
*fromcmd = q[0];
(void) close(p[0]);
(void) close(q[1]);
return 0;
}
/*NOTREACHED*/
}
/*
* Function: kill_cmd
* Purpose: Make sure the child process is dead.
*/
static int kill_cmd(struct cmd *cmd) {
int n, status;
do {
n = waitpid(cmd->pid, &status, 0);
} while (n == -1 && errno == EAGAIN); /* XXX man says ERESTARTSYS */
if (n == -1) {
error(0, "Error: %d: %.512s", errno, strerror(errno));
return -1;
}
if (WIFSIGNALED(status)) {
error(0, "Error: Child exited by signal %d", WTERMSIG(status));
return -1;
}
if (!WIFEXITED(status)) {
error(0, "Error: Child did not exit");
return -1;
}
if (WEXITSTATUS(status) != 0) {
error(0, "Error: child did not exit correctly: %d",
WEXITSTATUS(status));
return -1;
}
return 0;
}
/*
* Function: check_read_write
* Purpose: Check if pipes can be read from or written to.
* Return: -1 for error, else the fd that is OK.
* Note: Wait reading or writing is OK.
*/
static int check_read_write(int tocmd, int fromcmd) {
fd_set r, w;
int n, num;
if (tocmd < fromcmd)
num = fromcmd + 1;
else
num = tocmd + 1;
do {
FD_ZERO(&r);
FD_ZERO(&w);
if (fromcmd != -1)
FD_SET(fromcmd, &r);
if (tocmd != -1)
FD_SET(tocmd, &w);
n = select(num, &r, &w, NULL, NULL);
} while (n == -1 && errno == EINTR);
if (n == -1)
return -1;
if (fromcmd >= 0 && FD_ISSET(fromcmd, &r))
return fromcmd;
if (tocmd >= 0 && FD_ISSET(tocmd, &w))
return tocmd;
return -1;
}
/*
* Function: Read some data from pipe.
* Return: -1 for error, 0 for EOF, 1 for got data.
*/
static int read_some(int fd, struct dynarr *output) {
int n;
if (dynarr_resize(output, output->used + READ_BYTES) == -1) {
error(0, "Error: out of memory");
return -1;
}
n = read(fd, (char *) output->data + output->used, READ_BYTES);
if (n == -1 || n == 0)
return n;
output->used += n;
return 1;
}
/*
* Function: Write some data to pipe.
* Return: -1 for error, 1 for wrote data
*/
static int write_some(int fd, char *data, long max, long *written) {
int n, nn;
nn = WRITE_BYTES;
if (nn > max - *written)
nn = max - *written;
n = write(fd, data + *written, nn);
if (n == -1)
return -1;
*written += n;
return 1;
}
|