commands.c 34.7 KB
Newer Older
1 2 3 4
/* commands.c	10/05/1995
 */

/* Copyright (c) 1995,1996 Sascha Demetrio
5
 * Copyright (c) 2009, 2010, 2015, 2016, 2018 Peter Pentchev
6 7 8 9 10 11 12
 * 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.
13
 *    If you modify any part of HEXER and redistribute it, you must add
14 15 16 17 18 19
 *    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.
20
 *    If you modify any part of HEXER and redistribute it in binary form,
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
 *    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>
57
#include <stdlib.h>
58

59
#include "buffer.h"
60
#include "hexer.h"
61 62 63 64 65
#include "commands.h"
#include "edit.h"
#include "exh.h"
#include "helptext.h"
#include "regex.h"
66
#include "set.h"
67 68
#include "tio.h"
#include "util.h"
69 70 71 72 73 74 75 76 77 78

#define EXIT_EXEC_FAILED 27

#ifndef ERESTARTSYS
#define ERESTARTSYS EINTR
#endif

int he_map_special;

  static void
79
press_any_key(struct he_s *hedit)
80 81 82 83 84 85 86 87 88 89 90 91 92
{
  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
93
 * null pointer is returned.
94 95
 */

96 97
  static const char *
exh_skipcmd(const char *cmd, char *dest)
98
  /* read `cmd' up to the next semicolon.  `\;' escapes the semicolon.
99
   * The return value is the character following the first occurrence of an
100 101 102 103 104
   * 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'.
   */
{
105 106
  const char *p;
  char *q;
107 108 109 110 111 112 113 114 115 116 117 118 119

  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 */

120
  static const char *
121
exh_map(struct he_s *hedit, const char *args, int map)
122 123
{
  char *from, *to;
124 125
  char *p;
  const char *skip;
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

  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;
  }
141
  for (from = p; *p && *p != ' '; ++p);
142 143 144 145 146 147 148 149 150 151 152
  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 */

153
  static const char *
154
exh_unmap(struct he_s *hedit __unused, const char *args, int map)
155 156 157
  /* ARGSUSED */
{
  char *from;
158 159
  char *p;
  const char *skip;
160 161 162 163 164 165 166

  skip = exh_skipcmd(args, p = (char *)alloca(strlen(args) + 1));
  args = p;
  if (!*args) {
    he_message(0, "unmap what?");
    return 0;
  }
167
  for (from = p; *p && *p != ' '; ++p);
168 169 170 171 172 173
  *p = 0;
  if (tio_unmap(map, from, he_map_special)) he_message(0, "no such mapping");
  return skip;
}
/* exh_unmap */

174
  static const char *
175
exh_save_buffer(struct he_s *hedit, char *name)
176 177 178
{
  struct buffer_s *i;
  char *p;
179
  const char *skip;
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
  int force_f = 0;
  FILE *fp;
  long l;
  char *file = 0;

  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);
      return skip;
    }
  }
  if (i->hedit->read_only && !force_f) {
    he_message(0, "@Abread only buffer@~ - use @U:w!@~ to override");
    return skip;
  }
  if (!(fp = fopen(file, "w"))) {
    he_message(0, "@Abcan't open@~ `%s'", file);
    return skip;
  }
  fclose(fp);
216
  l = b_copy_to_file(i->hedit->buffer, file, 0, hedit->buffer->size);
217 218
  if (l > 0) {
    he_message(0, "wrote `%s' - 0x%lx (%li) bytes", file, l, l);
219
    i->hedit->buffer->modified = 0;
220 221 222
  } else {
    he_message(1, "could not write `%s'", file);
  }
223 224 225 226
  return skip;
}
/* exh_save_buffer */

227
  static const char *
228
exh_write(struct he_s *hedit, const char *file, long s_begin, long s_end, int *error)
229 230
{
  char *p;
231
  const char *skip;
232 233 234 235 236
  int force_f = 0;
  FILE *fp;
  long l;
  int whole_f = 0;

237 238 239 240
  unsigned long begin = (unsigned long)s_begin;
  unsigned long end = (s_end >= 0 && (unsigned long)s_end < hedit->buffer->size)?
    (unsigned long)s_end:
    hedit->buffer->size;
241 242 243 244 245 246 247
  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));
248 249
  util_trunc(p);
  file = p;
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
  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);
268 269 270 271 272 273 274 275
  if (l > 0) {
    he_message(0, "wrote `%s' - 0x%lx (%li) bytes", file, l, l);
    if (whole_f) hedit->buffer->modified = 0;
    if (error) *error = 0;
  } else {
    he_message(1, "could not write `%s'", file);
    if (error) *error = 1;
  }
276 277 278 279 280
  return skip;
}
/* exh_write */


281
  static const char *
282
exhcmd_substitute(struct he_s *hedit, const char *pattern, long begin, long end)
283
{
284
  const char *p = pattern;
285
  char separator = *pattern;
286 287 288
  const char *exp;
  const char *replace = 0, *options = 0;
  const char *skip = 0;
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
  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) {
313 314 315
	char *expbuf = alloca(p - exp + 1);
	snprintf(expbuf, p - exp + 1, "%s", exp);
	exp = expbuf;
316 317 318
        replace = p + 1;
        p = exh_skip_replace(replace, separator);
        if (p) {
319 320 321
	  char *replbuf = alloca(p - replace + 1);
	  snprintf(replbuf, p - replace + 1, "%s", replace);
	  replace = replbuf;
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
          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 */
375
        /* first we're going to check if the match is in the selected area */
376 377 378 379 380 381 382 383
        if (position + match_len > end + 1) {
          position += 1;
          break;
        }
        last_match = position;
	if (!confirm_f)
	  goto substitute;
	else {
384
	  /* We want to show the user the match found so we can ask him/her
385 386 387
	   * 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
388
           *   are zero, we won't ask the user.
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
	   */
	  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) {
434
          data = (char *)malloc_fatal(match_len);
435 436 437 438 439 440 441 442
	  if (b_read(hedit->buffer, data, position, match_len) < 0) {
	    he_message(1, "could not read data from the internal buffer");
	    free((char *)data);
	    free((char *)replace_str);
	    he_cancel_selection(hedit);
	    he_update_screen(hedit);
	    goto exit_exh_substitute;
	  }
443 444 445 446 447 448 449 450 451 452 453
          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. */
454 455 456 457 458 459 460 461
	if (match_len)
	  if (b_delete(hedit->buffer, position, match_len) < 0) {
	    he_message(1, "could not remove data from the internal buffer");
	    free((char *)replace_str);
	    he_cancel_selection(hedit);
	    he_update_screen(hedit);
	    goto exit_exh_substitute;
	  }
462
	if (replace_len) {
463 464 465 466 467 468 469
          if (b_insert(hedit->buffer, position, replace_len) < 0) {
	    he_message(1, "could not insert data into the internal buffer");
	    free((char *)replace_str);
	    he_cancel_selection(hedit);
	    he_update_screen(hedit);
	    goto exit_exh_substitute;
	  }
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
          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 */

491
  static const char *
492
exhcmd_write(struct he_s *hedit, const char *file, long begin, long end)
493 494 495 496 497
{
  return exh_write(hedit, file, begin, end, 0);
}
/* exhcmd_write */

498
  static const char *
499
exhcmd_skip(struct he_s *hedit __unused, const char *args, long begin __unused, long end __unused)
500 501 502 503
  /* ARGSUSED */
{
  struct buffer_s *i;
  char *ab = alternate_buffer;
504
  const char *skip;
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524

  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 */

525
  static const char *
526
exhcmd_next(struct he_s *hedit __unused, const char *args, long begin __unused, long end __unused)
527 528 529 530
  /* ARGSUSED */
{
  struct buffer_s *i;
  char *ab = alternate_buffer;
531
  const char *skip;
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549

  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 */

550
  static const char *
551
exhcmd_previous(struct he_s *hedit __unused, const char *args, long begin __unused, long end __unused)
552 553 554 555
  /* ARGSUSED */
{
  struct buffer_s *i;
  char *ab = alternate_buffer;
556
  const char *skip;
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575

  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 */

576
  static const char *
577
exhcmd_rewind(struct he_s *hedit __unused, const char *args, long begin __unused, long end __unused)
578 579 580
  /* ARGSUSED */
{
  char *ab = alternate_buffer;
581
  const char *skip;
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596

  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 */

597
  static const char *
598
exhcmd_wn(struct he_s *hedit, const char *file, long begin, long end)
599
{
600
  const char *skip;
601 602 603
  int error = 0;

  skip = exh_write(hedit, file, begin, end, &error);
604
  if (!error) exhcmd_next(hedit, "", 0, 0);
605 606 607 608
  return skip;
}
/* exhcmd_wn */

609
  static const char *
610
exhcmd_read(struct he_s *hedit, const char *file, long position, long end __unused)
611 612 613 614
{
  FILE *fp;
  long l;
  char *data;
615 616
  const char *skip;
  char *p;
617 618

  skip = exh_skipcmd(file, p = (char *)alloca(strlen(file) + 1));
619 620
  util_trunc(p);
  file = p;
621 622 623 624 625 626 627
  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);
628 629
  if (l == -1)
    return skip;
630
  data = (char *)malloc_fatal(l);
631
  fp = fopen(file, "r");
632
  if (fread(data, 1, l, fp) != (unsigned long)l) {
633 634 635 636
    free(data);
    fclose(fp);
    return skip;
  }
637 638 639 640
  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);
641
  fclose(fp);
642 643 644 645
  return skip;
}
/* exhcmd_read */

646
  static const char *
647
exhcmd_edit(struct he_s *hedit __unused, const char *name, long begin __unused, long end __unused)
648 649 650 651 652 653
  /* ARGSUSED */
{
  char *ab = alternate_buffer;
  struct passwd *pe;
  int new_buffer_f = 0;
  FILE *fp;
654 655
  const char *skip;
  char *p;
656
  const char *path = 0;
657
  char *user = 0;
658
  uid_t uid = getuid();
659
  char *pathbuf = 0;
660 661

  skip = exh_skipcmd(name, p = (char *)alloca(strlen(name) + 1));
662 663
  util_trunc(p);
  name = p;
664 665 666 667 668 669 670 671 672 673 674 675 676 677
  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 == '~') {
678
      user = strdup_fatal(name + 1);
679 680 681 682 683 684 685 686 687
      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) {
688
        pathbuf = (char *)malloc_fatal(strlen(name) + strlen(pe->pw_dir) + 4);
689 690 691
        strcpy(pathbuf, pe->pw_dir);
        strcat(pathbuf, name + strlen(user) + 1);
	path = pathbuf;
692 693 694 695 696 697 698 699
      } else
        path = name;
      free(user);
    } else
      path = name;
    if (!(fp = fopen(path, "r"))) {
      he_message(0, "@Abcan't open@~ `%s'", name);
      alternate_buffer = ab;
700
      if (pathbuf != NULL) free(pathbuf);
701 702 703 704 705 706 707 708 709
      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);
710
  if (pathbuf != NULL) free(pathbuf);
711 712 713 714
  return skip;
}
/* exhcmd_edit */

715
  static const char *
716
exhcmd_buffer(struct he_s *hedit, const char *name, long begin __unused, long end __unused)
717 718 719 720 721 722
{
  char *ab = alternate_buffer;
  long j, k;
  struct buffer_s *i;
  char **list;
  char *c;
723 724
  const char *skip;
  char *p;
725 726

  skip = exh_skipcmd(name, p = (char *)alloca(strlen(name) + 1));
727 728
  util_trunc(p);
  name = p;
729
  if (!*name) { /* list buffers */
730
    size_t name_maxlen = 0;
731 732 733
    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);
734
    list = (char **)malloc_fatal((k + 1) * sizeof(char *));
735
    for (i = buffer_list, k = 0; i; i = i->next, ++k) {
736
      list[k] = (char *)malloc_fatal(strlen(i->hedit->buffer_name) + 512);
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
      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 */

792
  static const char *
793
exhcmd_wall(struct he_s *hedit, const char *args, long begin __unused, long end __unused)
794 795 796
  /* ARGSUSED */
{
  struct buffer_s *i;
797
  const char *skip;
798 799 800 801

  skip = exh_skipcmd(args, 0);
  for (i = buffer_list; i; i = i->next) {
    if (i->loaded_f ? i->hedit->buffer->modified : 0) {
802
      exh_save_buffer(hedit, i->hedit->buffer_name);
803 804 805 806 807 808 809 810 811 812 813 814 815
#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 */

816
  static const char *
817
exhcmd_quit(struct he_s *hedit __unused, const char *args, long begin __unused, long end __unused)
818 819 820
  /* ARGSUSED */
{
  struct buffer_s *i, *j;
821 822
  const char *skip;
  char *p;
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
  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 */

854
  static const char *
855
exhcmd_close(struct he_s *hedit __unused, const char *aargs, long begin __unused, long end __unused)
856 857 858 859 860
  /* ARGSUSED */
{
  int force_f = 0;
  int nclosed = 0;
  int i;
861
  const char *skip;
862
  char *p, *args;
863

864
  skip = exh_skipcmd(aargs, p = (char *)alloca(strlen(aargs) + 1));
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
  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 */

898
  static const char *
899
exhcmd_map(struct he_s *hedit, const char *args, long begin __unused, long end __unused)
900 901 902 903 904
{
  return exh_map(hedit, args, MAP_COMMAND);
}
/* exhcmd_map */

905
  static const char *
906
exhcmd_imap(struct he_s *hedit, const char *args, long begin __unused, long end __unused)
907 908 909 910 911
{
  return exh_map(hedit, args, MAP_INSERT);
}
/* exhcmd_imap */

912
  static const char *
913
exhcmd_vmap(struct he_s *hedit, const char *args, long begin __unused, long end __unused)
914 915 916 917 918
{
  return exh_map(hedit, args, MAP_VISUAL);
}
/* exhcmd_vmap */

919
  static const char *
920
exhcmd_unmap(struct he_s *hedit, const char *args, long begin __unused, long end __unused)
921 922 923 924 925
{
  return exh_unmap(hedit, args, MAP_COMMAND);
}
/* exhcmd_unmap */

926
  static const char *
927
exhcmd_iunmap(struct he_s *hedit, const char *args, long begin __unused, long end __unused)
928 929 930 931 932
{
  return exh_unmap(hedit, args, MAP_INSERT);
}
/* exhcmd_iunmap */

933
  static const char *
934
exhcmd_vunmap(struct he_s *hedit, const char *args, long begin __unused, long end __unused)
935 936 937 938 939
{
  return exh_unmap(hedit, args, MAP_VISUAL);
}
/* exhcmd_vunmap */

940
  static const char *
941
exhcmd_set(struct he_s *hedit, const char *args, long begin __unused, long end __unused)
942 943 944 945
{
  char *p, *q, *option;
  enum s_option_e type;
  int no_f;
946
  const char *skip;
947 948

  skip = exh_skipcmd(args, p = (char *)alloca(strlen(args) + 1));
949 950
  util_trunc(p);
  args = p;
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
  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;
  }
  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 {
997
        const char *value = s_get_option(option);
998 999 1000 1001 1002 1003 1004 1005 1006
        if (!*value) value = "<unset>";
        he_message(0, "%s=%s@~", option, value);
      }
    }
  }
  return skip;
}
/* exhcmd_set */

1007
  static const char *
1008
exhcmd_delete(struct he_s *hedit, const char *args, long begin, long end)
1009 1010 1011 1012
  /* ARGSUSED */
{
  long count = end - begin;
  char *data;
1013 1014
  const char *skip;
  char *p;
1015 1016

  skip = exh_skipcmd(args, p = (char *)alloca(strlen(args) + 1));
1017 1018
  util_trunc(p);
  args = p;
1019
  if (!count) return skip;
1020
  data = (char *)malloc_fatal(count);
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
  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 */

1040
  static const char *
1041
exhcmd_yank(struct he_s *hedit, const char *args, long begin, long end)
1042 1043 1044
  /* ARGSUSED */
{
  long count = end - begin;
1045 1046
  const char *skip;
  char *p;
1047 1048

  skip = exh_skipcmd(args, p = (char *)alloca(strlen(args) + 1));
1049 1050
  util_trunc(p);
  args = p;
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
  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 */

1061
  static const char *
1062
exhcmd_version(struct he_s *hedit __unused, const char *args, long begin __unused, long end __unused)
1063 1064
  /* ARGSUSED */
{
1065
  const char *skip = exh_skipcmd(args, 0);
1066 1067 1068 1069 1070
  hexer_version();
  return skip;
}
/* exhcmd_version */

1071
  static const char *
1072
exhcmd_zz(struct he_s *hedit, const char *args, long begin __unused, long end __unused)
1073 1074
{
  int k;
1075
  const char *skip;
1076 1077

  skip = exh_skipcmd(args, 0);
1078 1079 1080 1081
  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));
1082 1083 1084 1085 1086
  he_update_screen(hedit);
  return skip;
}
/* exhcmd_zz */

1087
  static const char *
1088
exhcmd_zt(struct he_s *hedit, const char *args, long begin __unused, long end __unused)
1089
{
1090
  const char *skip;
1091 1092 1093 1094 1095 1096 1097 1098

  skip = exh_skipcmd(args, 0);
  he_scroll_up(hedit, HE_LINE(hedit->position - hedit->screen_offset));
  he_update_screen(hedit);
  return skip;
}
/* exhcmd_zt */

1099
  static const char *
1100
exhcmd_zb(struct he_s *hedit, const char *args, long begin __unused, long end __unused)
1101
{
1102
  const char *skip;
1103 1104 1105

  skip = exh_skipcmd(args, 0);
  he_scroll_down(hedit,
1106
                 hx_lines - 2 - HE_LINE(hedit->position - hedit->screen_offset));
1107 1108 1109 1110 1111
  he_update_screen(hedit);
  return skip;
}
/* exhcmd_zb */

1112
  static const char *
1113
exhcmd_help(struct he_s *hedit, const char *args, long begin __unused, long end __unused)
1114 1115 1116 1117
{
  int pid1, pid2, status, x = 0;
  int pipefd[2];
  char *vp[64], *p;
1118
  const char *skip;
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
  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);
1137 1138
    if (dup(pipefd[0]) == -1)
      exit(EXIT_EXEC_FAILED);
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
    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 */
1157 1158
    if (write_buf(pipefd[1], helptext, strlen(helptext)) < 0)
      exit(1);
1159 1160 1161 1162 1163 1164 1165 1166
    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]);
1167
  if ((x = WEXITSTATUS(status))) {
1168 1169 1170 1171
    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);
1172
  }
1173 1174 1175 1176 1177 1178 1179
exit_exhcmd_help:
  tio_restart();
  he_refresh_all(hedit);
  return skip;
}
/* exhcmd_help */

1180
  static const char *
1181
exhcmd_exit(struct he_s *hedit, const char *args, long begin __unused, long end __unused)
1182 1183 1184 1185
{
  struct buffer_s *i;
  int cant_write_f = 0;
  long k;
1186 1187
  const char *skip;
  char *p;
1188 1189

  skip = exh_skipcmd(args, p = (char *)alloca(strlen(args) + 1));
1190 1191
  util_trunc(p);
  args = p;
1192 1193 1194 1195 1196 1197 1198 1199 1200
  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) {
1201 1202
          he_message(0, "@Aberror writing file@~ `%s': %s", i->path,
              strerror(errno));
1203 1204 1205 1206 1207 1208 1209
          cant_write_f = 1;
        } else {
          he_message(0, "wrote `%s' - 0x%lx (%li) bytes", i->path, k, k);
          hedit->buffer->modified = 0;
        }
      }
    }
1210
  if (!cant_write_f) exhcmd_quit(hedit, "", 0, 0);
1211 1212 1213 1214 1215
  return skip;
}
/* exhcmd_exit */

const struct exh_cmd_s exh_commands[] = {
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
  { "substitute", 's', exhcmd_substitute, 1, 0 },
  { "write", 'w', exhcmd_write, 1, 1 },
  { "read", 'r', exhcmd_read, 0, 1 },
  { "edit", 'e', exhcmd_edit, 1, 3 },
  { "buffer", 'b', exhcmd_buffer, 1, 2 },
  { "next", 'n', exhcmd_next, 0, 0 },
  { "previous", 'N', exhcmd_previous, 0, 0 },
  { "rewind", 0, exhcmd_rewind, 0, 0 },
  { "skip", 'S', exhcmd_skip, 0, 0 },
  { "wn", 0, exhcmd_wn, 1, 1 },
  { "wall", 0, exhcmd_wall, 0, 0 },
  { "quit", 'q', exhcmd_quit, 0, 0 },
  { "close", 'c', exhcmd_close, 0, 2 },
  { "map", 0, exhcmd_map, 0, 0 },
  { "imap", 0, exhcmd_imap, 0, 0 },
  { "vmap", 0, exhcmd_vmap, 0, 0 },
  { "unmap", 0, exhcmd_unmap, 0, 0 },
  { "iunmap", 0, exhcmd_iunmap, 0, 0 },
  { "vunmap", 0, exhcmd_vunmap, 0, 0 },
  { "set", 0, exhcmd_set, 0, 4 },
  { "delete", 'd', exhcmd_delete, 0, 0 },
  { "yank", 'y', exhcmd_yank, 0, 0 },
  { "version", 0, exhcmd_version, 0, 0 },
  { "zz", 0, exhcmd_zz, 0, 0 },
  { "zt", 0, exhcmd_zt, 0, 0 },
  { "zb", 0, exhcmd_zb, 0, 0 },
  { "wq", 0, exhcmd_exit, 0, 0 },
  { "help", 'h', exhcmd_help, 0, 0 },
  { "exit", 'x', exhcmd_exit, 0, 0 },
1245 1246 1247 1248 1249 1250 1251 1252
  { 0, 0, (exh_fn)0, 0, 0 }
};

/* end of commands.c */


/* VIM configuration: (do not delete this line)
 *
1253 1254
 * vim:bk:nodg:efm=%f\:%l\:%m:hid:icon:
 * vim:sw=2:sm:textwidth=79:ul=1024:wrap:
1255
 */