commit a3cf75374e2fa9825a81044af69bcf2ec919e6cb
parent 254edfc0712db1bda2a4cc0ab42c346a1b1c97d5
Author: shrub <maybeshrub@gmail.com>
Date: Wed, 1 Jul 2026 20:11:56 +0000
add extra/yap
Diffstat:
34 files changed, 4316 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile
@@ -282,6 +282,7 @@ PSEUDO_BIN_ALL =\
cmd/extra/lsblk\
cmd/extra/fdisk\
cmd/extra/sync\
+ cmd/extra/yap/yap\
cmd/dev/ar/ar\
cmd/dev/as/as\
cmd/dev/ld/ld\
@@ -466,6 +467,7 @@ BIN_blkid_1 = cmd/extra/blkid
BIN_lsblk_1 = cmd/extra/lsblk
BIN_fdisk_1 = cmd/extra/fdisk
BIN_sync_1 = cmd/extra/sync
+BIN_yap_1 = cmd/extra/yap/yap
BIN_diff3_1 = cmd/posix/diff3
BIN_ar_1 = cmd/dev/ar/ar
BIN_as_1 = cmd/dev/as/as
@@ -811,6 +813,23 @@ SHOBJ =\
cmd/posix/sh/nodes.o\
cmd/posix/sh/syntax.o
+YAPOBJ =\
+ cmd/extra/yap/ansi.o\
+ cmd/extra/yap/commands.o\
+ cmd/extra/yap/display.o\
+ cmd/extra/yap/getcomm.o\
+ cmd/extra/yap/getline.o\
+ cmd/extra/yap/help.o\
+ cmd/extra/yap/keys.o\
+ cmd/extra/yap/machine.o\
+ cmd/extra/yap/main.o\
+ cmd/extra/yap/options.o\
+ cmd/extra/yap/output.o\
+ cmd/extra/yap/pattern.o\
+ cmd/extra/yap/process.o\
+ cmd/extra/yap/prompt.o\
+ cmd/extra/yap/term.o
+
cmd/posix/awk/awkgram.tab.c cmd/posix/awk/awkgram.tab.h: cmd/posix/awk/awkgram.y
$(YACC) -d -o cmd/posix/awk/awkgram.tab.c cmd/posix/awk/awkgram.y
@if [ ! -f cmd/posix/awk/awkgram.tab.h ]; then \
@@ -858,6 +877,12 @@ cmd/posix/sh/%.o: cmd/posix/sh/%.c
cmd/posix/sh/sh: $(SHOBJ) $(LIB)
$(CC) $(LDFLAGS) -o $@ $(SHOBJ) $(LIB) $(LDLIBS)
+cmd/extra/yap/%.o: cmd/extra/yap/%.c
+ $(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $<
+
+cmd/extra/yap/yap: $(YAPOBJ) $(LIB)
+ $(CC) $(LDFLAGS) -o $@ $(YAPOBJ) $(LIB) $(LDLIBS) -ltinfo
+
cmd/extra/diff3/diff3: cmd/extra/diff3/diff3.o cmd/posix/diff/xmalloc.o $(LIB)
$(CC) $(LDFLAGS) -o $@ cmd/extra/diff3/diff3.o cmd/posix/diff/xmalloc.o $(LIB) $(LDLIBS)
diff --git a/cmd/extra/yap/ansi.c b/cmd/extra/yap/ansi.c
@@ -0,0 +1,75 @@
+#include "in_all.h"
+#include "ansi.h"
+
+static int
+consume_csi(const char *s, const char **next, enum ansi_kind *kind)
+{
+ const unsigned char *p = (const unsigned char *)s + 2;
+
+ while (*p >= 0x30 && *p <= 0x3f) {
+ p++;
+ }
+ while (*p >= 0x20 && *p <= 0x2f) {
+ p++;
+ }
+ if (*p >= 0x40 && *p <= 0x7e) {
+ *kind = (*p == 'm') ? ANSI_SGR : ANSI_DISCARD;
+ *next = (const char *)p + 1;
+ return 1;
+ }
+ return 0;
+}
+
+static int
+consume_string(const char *s, const char **next)
+{
+ const unsigned char *p = (const unsigned char *)s + 2;
+
+ while (*p) {
+ if (*p == '\a') {
+ *next = (const char *)p + 1;
+ return 1;
+ }
+ if (*p == '\x1b' && p[1] == '\\') {
+ *next = (const char *)p + 2;
+ return 1;
+ }
+ p++;
+ }
+ return 0;
+}
+
+static int
+consume_esc(const char *s, const char **next)
+{
+ const unsigned char *p = (const unsigned char *)s + 1;
+
+ while (*p >= 0x20 && *p <= 0x2f) {
+ p++;
+ }
+ if (*p >= 0x30 && *p <= 0x7e) {
+ *next = (const char *)p + 1;
+ return 1;
+ }
+ return 0;
+}
+
+int
+ansi_escape(const char *s, const char **next, enum ansi_kind *kind)
+{
+ *kind = ANSI_NONE;
+ *next = s;
+ if ((unsigned char)s[0] != 0x1b || s[1] == '\0') {
+ return 0;
+ }
+ if (s[1] == '[') {
+ return consume_csi(s, next, kind);
+ }
+ if (s[1] == ']' || s[1] == 'P' || s[1] == 'X' || s[1] == '^' ||
+ s[1] == '_') {
+ *kind = ANSI_DISCARD;
+ return consume_string(s, next);
+ }
+ *kind = ANSI_DISCARD;
+ return consume_esc(s, next);
+}
diff --git a/cmd/extra/yap/ansi.h b/cmd/extra/yap/ansi.h
@@ -0,0 +1,12 @@
+#ifndef ANSI_H
+#define ANSI_H
+
+enum ansi_kind {
+ ANSI_NONE = 0,
+ ANSI_SGR,
+ ANSI_DISCARD,
+};
+
+int ansi_escape(const char *s, const char **next, enum ansi_kind *kind);
+
+#endif
diff --git a/cmd/extra/yap/assert.h b/cmd/extra/yap/assert.h
@@ -0,0 +1,6 @@
+#ifndef ASSERT_H
+#define ASSERT_H
+
+#define assert(x) ((void)0)
+
+#endif
diff --git a/cmd/extra/yap/commands.c b/cmd/extra/yap/commands.c
@@ -0,0 +1,785 @@
+/* Copyright (c) 1985 Ceriel J.H. Jacobs */
+
+#include "in_all.h"
+#include "commands.h"
+#include "output.h"
+#include "process.h"
+#include "help.h"
+#include "term.h"
+#include "prompt.h"
+#include "getline.h"
+#include "getcomm.h"
+#include "pattern.h"
+#include "display.h"
+#include "options.h"
+#include "machine.h"
+#include "keys.h"
+#include "main.h"
+#include "assert.h"
+
+static long lastcount; /* Save last count for '.' command */
+static int lastcomm; /* Save last command for '.' command */
+static int searchdir; /* Direction of last search */
+
+static int do_nocomm(long cnt);
+static void do_search(char *str, long cnt, int dir);
+static int do_fsearch(long cnt);
+static int do_bsearch(long cnt);
+static int n_or_rn_search(long cnt, int dir);
+static int do_nsearch(long cnt);
+static int do_rnsearch(long cnt);
+static int shell_command(int esc_ch, long cnt);
+static int do_shell(long cnt);
+static int do_pipe(long cnt);
+static int do_writefile(long cnt);
+static int do_absolute(long cnt);
+static int do_visit(long cnt);
+static int do_error(long cnt);
+static int prev_screen(int sz, int really);
+static int next_screen(int sz, int really);
+static int do_redraw(long cnt);
+static int page_size(unsigned cnt);
+static int do_forward(long cnt);
+static int do_backward(long cnt);
+static int do_firstline(long cnt);
+static int do_lline(long cnt);
+static int do_lf(long cnt);
+static int do_upline(long cnt);
+static int do_skiplines(long cnt);
+static int do_bskiplines(long cnt);
+static int do_fscreens(long cnt);
+static int do_bscreens(long cnt);
+static int scro_size(unsigned cnt);
+static int do_f_scroll(long cnt);
+static int do_b_scroll(long cnt);
+static int do_previousfile(long cnt);
+static int do_nextfile(long cnt);
+static int do_lcomm(long cnt);
+static int do_quit(long cnt);
+
+static int
+do_nocomm(long cnt)
+{ /* Do nothing */
+ (void)cnt;
+ return 0;
+}
+
+int
+do_chkm(long cnt)
+{ /* Change key map */
+ struct keymap *p;
+ (void)cnt;
+
+ if (!(p = othermap)) {
+ error("No other keymap");
+ return 0;
+ }
+ othermap = currmap;
+ currmap = p;
+ return 0;
+}
+
+/*
+ * Perform searches
+ */
+
+static void
+do_search(char *str, long cnt, int dir)
+{
+ char *p;
+ long lineno;
+
+ if (str) {
+ /*
+ * We have to get a pattern, which we have to prompt for
+ * with the string "str".
+ */
+ if ((p = readline(str)) == 0) {
+ /*
+ * User cancelled command
+ */
+ return;
+ }
+ if ((p = re_comp(p))) {
+ /*
+ * There was an error in the pattern
+ */
+ error(p);
+ return;
+ }
+ searchdir = dir;
+ }
+ if (dir < 0)
+ lineno = scr_info.firstline;
+ else
+ lineno = scr_info.lastline;
+ for (;;) {
+ p = 0;
+ if ((lineno += dir) > 0)
+ p = getline(lineno, 0);
+ if (interrupt)
+ return;
+ if (!p) { /* End of file reached */
+ error("pattern not found");
+ return;
+ }
+ if (re_exec(p) && --cnt <= 0) {
+ /*
+ * We found the pattern, and we found it often enough.
+ * Pity that we still don't know where the match is.
+ * We only know the linenumber. So, we just hope the
+ * following will at least bring it on the screen ...
+ */
+ (void)display(lineno, 0, pagesize, 0);
+ (void)scrollb(2, 0);
+ redraw(0);
+ return;
+ }
+ }
+ /* NOTREACHED */
+}
+
+static int
+do_fsearch(long cnt)
+{ /* Forward search */
+ do_search("/", cnt, 1);
+ return 0;
+}
+
+static int
+do_bsearch(long cnt)
+{ /* Backward search */
+ do_search("?", cnt, -1);
+ return 0;
+}
+
+/*
+ * Repeat last search in direction "dir"
+ */
+
+static int
+n_or_rn_search(long cnt, int dir)
+{
+ char *p;
+
+ if (dir == 1) {
+ p = "/\r";
+ } else if (dir == -1) {
+ p = "?\r";
+ } else {
+ error("No previous pattern");
+ return 0;
+ }
+ if (!stupid)
+ clrbline();
+ putline(p);
+ flush();
+ do_search((char *)0, cnt, dir);
+ return 0;
+}
+
+static int
+do_nsearch(long cnt)
+{ /* Repeat search in same direction */
+ n_or_rn_search(cnt, searchdir);
+ return 0;
+}
+
+static int
+do_rnsearch(long cnt)
+{ /* Repeat search in opposite direction */
+ n_or_rn_search(cnt, -searchdir);
+ return 0;
+}
+
+static int
+shell_command(int esc_ch, long cnt)
+{
+ char *p;
+ static char buf[2];
+
+ buf[0] = esc_ch;
+ buf[1] = '\0';
+ if ((p = readline(buf)) != 0) {
+ shellescape(p, esc_ch);
+ if (cnt >= 0 && !hardcopy) {
+ p = startcomm;
+ startcomm = 0;
+ ret_to_continue();
+ putline(TI);
+ if (!p) {
+ /*
+ * Avoid double redraw.
+ * After a "startcomm", a redraw will
+ * take place anyway.
+ */
+ redraw(1);
+ }
+ }
+ }
+ return 0;
+}
+
+static int
+do_shell(long cnt)
+{ /* Execute a shell escape */
+ return shell_command('!', cnt);
+}
+
+static int
+do_pipe(long cnt)
+{ /* Execute a shell escape */
+ return shell_command('|', cnt);
+}
+
+static int
+do_writefile(long cnt)
+{ /* Write input to a file */
+ char *p;
+ int fd;
+ (void)cnt;
+
+ if ((p = readline("Filename: ")) == 0 || !*p) {
+ /*
+ * No file name given
+ */
+ return 0;
+ }
+ if ((fd = open(p, O_CREAT | O_EXCL | O_WRONLY, 0644)) < 0) {
+ if (errno == EEXIST) {
+ error("File exists");
+ return 0;
+ }
+ error("Could not open file");
+ return 0;
+ }
+ wrt_fd(fd);
+ (void)close(fd);
+ return 0;
+}
+
+void
+wrt_fd(int fd)
+{
+ long l = 1;
+ char *p = getline(l, 0), *pbuf;
+ char buf[1024];
+
+ while (p) {
+ pbuf = buf;
+ while (p && pbuf < &buf[1024]) {
+ if (!*p) {
+ *pbuf++ = '\n';
+ p = getline(++l, 0);
+ } else
+ *pbuf++ = *p++;
+ }
+ if (write(fd, buf, pbuf - buf) < 0) {
+ error("Write failed");
+ break;
+ }
+ }
+}
+
+static int
+do_absolute(long cnt)
+{ /* Go to linenumber "cnt" */
+
+ if (!getline(cnt, 0)) { /* Not there or interrupt */
+ if (!interrupt) {
+ /*
+ * User did'nt give an interrupt, so the line number
+ * was too high. Go to the last line.
+ */
+ do_lline(cnt);
+ }
+ return 0;
+ }
+ (void)display(cnt, 0, pagesize, 1);
+ return 0;
+}
+
+static int
+do_visit(long cnt)
+{ /* Visit a file */
+ char *p;
+ static char fn[128]; /* Keep file name */
+ (void)cnt;
+
+ if ((p = readline("Filename: ")) == 0) {
+ return 0;
+ }
+ if (*p) {
+ (void)strcpy(fn, p);
+ visitfile(fn);
+ } else {
+ /*
+ * User typed a return. Visit the current file
+ */
+ if (!(p = filenames[filecount])) {
+ error("No current file");
+ return 0;
+ }
+ visitfile(p);
+ }
+ (void)display(1L, 0, pagesize, 1);
+ return 0;
+}
+
+static int
+do_error(long cnt)
+{ /* Called when user types wrong key sequence */
+ (void)cnt;
+ error(currmap->k_help);
+ return 0;
+}
+
+/*
+ * Interface routine for displaying previous screen,
+ * depending on cflag.
+ */
+
+static int
+prev_screen(int sz, int really)
+{
+ int retval;
+
+ retval = scrollb(sz - 1, really && cflag);
+ if (really && !cflag) {
+ /*
+ * The previous call did not display anything, but at least we
+ * know where to start
+ */
+ return display(scr_info.firstline, scr_info.nf, sz, 1);
+ }
+ return retval;
+}
+
+/*
+ * Interface routine for displaying the next screen,
+ * dependent on cflag.
+ */
+
+static int
+next_screen(int sz, int really)
+{
+ int t;
+ struct scr_info *p = &scr_info;
+
+ if (cflag) {
+ return scrollf(sz - 1, really);
+ }
+ t = p->tail->cnt - 1;
+ if (p->lastline == p->firstline) {
+ t += p->nf;
+ }
+ return display(p->lastline, t, sz, really);
+}
+
+static int
+do_redraw(long cnt)
+{
+ (void)cnt;
+ redraw(1);
+ return 0;
+}
+
+static int
+page_size(unsigned cnt)
+{
+
+ if (cnt) {
+ if (cnt > (unsigned)maxpagesize)
+ return maxpagesize;
+ if (cnt < MINPAGESIZE)
+ return MINPAGESIZE;
+ return (int)cnt;
+ }
+ return pagesize;
+}
+
+static int
+do_forward(long cnt)
+{ /* Display next page */
+ int i;
+
+ i = page_size((unsigned)cnt);
+ if (status & EOFILE) {
+ /*
+ * May seem strange, but actually a visit to the next file
+ * has already been done here
+ */
+ (void)display(1L, 0, i, 1);
+ return 0;
+ }
+ (void)next_screen(i, 1);
+ return 0;
+}
+
+static int
+do_backward(long cnt)
+{
+ int i, temp;
+
+ i = page_size((unsigned)cnt);
+ if (!(status & START)) {
+ (void)prev_screen(i, 1);
+ return 0;
+ }
+ if (stdf < 0) {
+ (void)display(1L, 0, i, 1);
+ return 0;
+ }
+ /*
+ * The next part is a bit clumsy.
+ * We want to display the last page of the previous file (for which
+ * a visit has already been done), but the pagesize may temporarily
+ * be different because the command had a count
+ */
+ temp = pagesize;
+ pagesize = i;
+ do_lline(cnt);
+ pagesize = temp;
+ return 0;
+}
+
+static int
+do_firstline(long cnt)
+{ /* Go to start of input */
+ (void)cnt;
+ do_absolute(1L);
+ return 0;
+}
+
+static int
+do_lline(long cnt)
+{ /* Go to end of input */
+ int i = 0;
+ int j = pagesize - 1;
+
+ if ((cnt = to_lastline()) < 0) {
+ /*
+ * Interrupted by the user
+ */
+ return 0;
+ }
+ /*
+ * Display the page such that only the last line of the page is
+ * a "~", independant of the pagesize
+ */
+ while (!(display(cnt, i, j, 0) & EOFILE)) {
+ /*
+ * The last line could of course be very long ...
+ */
+ i += j;
+ }
+ (void)scrollb(j - scr_info.tail->cnt, 0);
+ redraw(0);
+ return 0;
+}
+
+static int
+do_lf(long cnt)
+{ /* Display next line, or go to line */
+
+ if (cnt) { /* Go to line */
+ do_absolute(cnt);
+ return 0;
+ }
+ (void)scrollf(1, 1);
+ return 0;
+}
+
+static int
+do_upline(long cnt)
+{ /* Display previous line, or go to line */
+
+ if (cnt) { /* Go to line */
+ do_absolute(cnt);
+ return 0;
+ }
+ (void)scrollb(1, 1);
+ return 0;
+}
+
+static int
+do_skiplines(long cnt)
+{ /* Skip lines forwards */
+
+ /* Should be interruptable ... */
+ (void)scrollf((int)(cnt + maxpagesize - 1), 0);
+ redraw(0);
+ return 0;
+}
+
+static int
+do_bskiplines(long cnt)
+{ /* Skip lines backwards */
+
+ /* Should be interruptable ... */
+ (void)scrollb((int)(cnt + pagesize - 1), 0);
+ redraw(0);
+ return 0;
+}
+
+static int
+do_fscreens(long cnt)
+{ /* Skip screens forwards */
+
+ do {
+ if ((next_screen(pagesize, 0) & EOFILE) || interrupt)
+ break;
+ } while (--cnt >= 0);
+ redraw(0);
+ return 0;
+}
+
+static int
+do_bscreens(long cnt)
+{ /* Skip screens backwards */
+
+ do {
+ if ((prev_screen(pagesize, 0) & START) || interrupt)
+ break;
+ } while (--cnt >= 0);
+ redraw(0);
+ return 0;
+}
+
+static int
+scro_size(unsigned cnt)
+{
+
+ if (cnt >= (unsigned)maxpagesize)
+ return maxpagesize;
+ if (cnt)
+ return (int)cnt;
+ return scrollsize;
+}
+
+static int
+do_f_scroll(long cnt)
+{ /* Scroll forwards */
+
+ (void)scrollf(scro_size((unsigned)cnt), 1);
+ return 0;
+}
+
+static int
+do_b_scroll(long cnt)
+{ /* Scroll backwards */
+
+ (void)scrollb(scro_size((unsigned)cnt), 1);
+ return 0;
+}
+
+static int
+do_previousfile(long cnt)
+{ /* Visit previous file */
+
+ if (nextfile(-(int)cnt)) {
+ error("No (Nth) previous file");
+ return 0;
+ }
+ redraw(0);
+ return 0;
+}
+
+static int
+do_nextfile(long cnt)
+{ /* Visit next file */
+
+ if (nextfile((int)cnt)) {
+ error("No (Nth) next file");
+ return 0;
+ }
+ redraw(0);
+ return 0;
+}
+
+static int
+do_quit(long cnt)
+{
+ (void)cnt;
+ return quit();
+}
+
+/*
+ * The next array is initialized, sorted on the first element of the structs,
+ * so that we can perform binary search
+ */
+struct commands commands[] = {
+ {"", 0, do_error, ""},
+ {"", 0, do_nocomm, ""},
+ {"bf", STICKY | NEEDS_COUNT,
+ do_previousfile, "Visit previous file"},
+ {"bl", NEEDS_SCREEN | STICKY,
+ do_upline, "Scroll one line up, or go to line"},
+ {"bot", STICKY,
+ do_lline, "Go to last line of the input"},
+ {"bp", BACK | NEEDS_SCREEN | TOPREVFILE | STICKY,
+ do_backward, "display previous page"},
+ {"bps", SCREENSIZE_ADAPT | BACK | NEEDS_SCREEN | TOPREVFILE | STICKY,
+ do_backward, "Display previous page, set pagesize"},
+ {"bs", BACK | NEEDS_SCREEN | STICKY,
+ do_b_scroll, "Scroll backwards"},
+ {"bse", 0, do_bsearch, "Search backwards for pattern"},
+ {"bsl", BACK | NEEDS_SCREEN | STICKY | NEEDS_COUNT,
+ do_bskiplines, "Skip lines backwards"},
+ {"bsp", BACK | NEEDS_SCREEN | STICKY | NEEDS_COUNT,
+ do_bscreens, "Skip screens backwards"},
+ {"bss", SCROLLSIZE_ADAPT | BACK | NEEDS_SCREEN | STICKY,
+ do_b_scroll, "Scroll backwards, set scrollsize"},
+ {"chm", 0, do_chkm, "Switch to other keymap"},
+ {"exg", STICKY, exgmark, "Exchange current page with mark"},
+ {"ff", STICKY | NEEDS_COUNT,
+ do_nextfile, "Visit next file"},
+ {"fl", NEEDS_SCREEN | STICKY,
+ do_lf, "Scroll one line down, or go to line"},
+ {"fp", TONEXTFILE | AHEAD | STICKY,
+ do_forward, "Display next page"},
+ {"fps", SCREENSIZE_ADAPT | TONEXTFILE | AHEAD | STICKY,
+ do_forward, "Display next page, set pagesize"},
+ {"fs", AHEAD | NEEDS_SCREEN | STICKY,
+ do_f_scroll, "Scroll forwards"},
+ {"fse", 0, do_fsearch, "Search forwards for pattern"},
+ {"fsl", AHEAD | NEEDS_SCREEN | STICKY | NEEDS_COUNT,
+ do_skiplines, "Skip lines forwards"},
+ {"fsp", AHEAD | NEEDS_SCREEN | STICKY | NEEDS_COUNT,
+ do_fscreens, "Skip screens forwards"},
+ {"fss", SCROLLSIZE_ADAPT | AHEAD | NEEDS_SCREEN | STICKY,
+ do_f_scroll, "Scroll forwards, set scrollsize"},
+ {"hlp", 0, do_help, "Give description of all commands"},
+ {"mar", 0, setmark, "Set a mark on the current page"},
+ {"nse", STICKY, do_nsearch, "Repeat the last search"},
+ {"nsr", STICKY, do_rnsearch, "Repeat last search in other direction"},
+ {"pip", ESC, do_pipe, "pipe input into shell command"},
+ {"qui", 0, do_quit, "Exit from yap"},
+ {"red", 0, do_redraw, "Redraw screen"},
+ {"rep", 0, do_lcomm, "Repeat last command"},
+ {"shl", ESC, do_shell, "Execute a shell escape"},
+ {"tom", 0, tomark, "Go to mark"},
+ {"top", STICKY, do_firstline, "Go to the first line of the input"},
+ {"vis", 0, do_visit, "Visit a file"},
+ {"wrf", 0, do_writefile, "Write input to a file"},
+};
+
+/*
+ * Lookup string "s" in the commands array, and return index.
+ * return 0 if not found.
+ */
+
+int
+lookup(char *s)
+{
+ struct commands *l, *u, *m;
+
+ l = &commands[2];
+ u = &commands[sizeof(commands) / sizeof(*u) - 1];
+ do {
+ /*
+ * Perform binary search
+ */
+ m = l + (u - l) / 2;
+ if (strcmp(s, m->c_cmd) > 0)
+ l = m + 1;
+ else
+ u = m;
+ } while (l < u);
+ if (!strcmp(s, u->c_cmd))
+ return u - commands;
+ return 0;
+}
+
+/*ARGSUSED*/
+static int
+do_lcomm(long cnt)
+{ /* Repeat last command */
+ (void)cnt;
+
+ if (!lastcomm) {
+ error("No previous command");
+ return 0;
+ }
+ do_comm(lastcomm, lastcount);
+ return 0;
+}
+
+/*
+ * Execute a command, with optional count "count".
+ */
+
+void
+do_comm(int comm, long count)
+{
+ struct commands *pcomm;
+ int temp;
+ int flags;
+
+ pcomm = &commands[comm];
+ flags = pcomm->c_flags;
+
+ /*
+ * Check the command.
+ * If the last line of the file is displayed and the command goes
+ * forwards and does'nt have the ability to go to the next file, it
+ * is an error.
+ * If the first line of the file is displayed and the command goes
+ * backwards and does'nt have the ability to go to the previous file,
+ * it is an error.
+ * Also check wether we need the next or previous file. If so, get it.
+ */
+ if ((status & EOFILE) && (flags & AHEAD)) {
+ if (qflag || !(flags & TONEXTFILE))
+ return;
+ if (nextfile(1))
+ quit();
+ }
+ if ((status & START) && (flags & BACK)) {
+ if (qflag || !(flags & TOPREVFILE))
+ return;
+ if (nextfile(-1))
+ quit();
+ }
+ /*
+ * Does the command stick around for LASTCOMM?
+ */
+ if (flags & STICKY) {
+ lastcomm = comm;
+ lastcount = count;
+ }
+ if (!count) {
+ if (flags & NEEDS_COUNT)
+ count = 1;
+ } else {
+ /*
+ * Does the command adapt the screensize?
+ */
+ if (flags & SCREENSIZE_ADAPT) {
+ temp = maxpagesize;
+ if ((unsigned)count < (unsigned)temp) {
+ temp = (int)count;
+ }
+ if (temp < MINPAGESIZE) {
+ temp = MINPAGESIZE;
+ }
+ count = 0;
+ pagesize = temp;
+ }
+ /*
+ * Does the command adapt the scrollsize?
+ */
+ if (flags & SCROLLSIZE_ADAPT) {
+ temp = maxpagesize - 1;
+ if ((unsigned)count < (unsigned)temp) {
+ temp = (int)count;
+ }
+ scrollsize = temp;
+ count = 0;
+ }
+ }
+ /*
+ * Now execute the command.
+ */
+ (*(pcomm->c_func))(count);
+}
diff --git a/cmd/extra/yap/commands.h b/cmd/extra/yap/commands.h
@@ -0,0 +1,27 @@
+#ifndef COMMANDS_H
+#define COMMANDS_H
+
+#define SCREENSIZE_ADAPT 01
+#define SCROLLSIZE_ADAPT 02
+#define TONEXTFILE 04
+#define AHEAD 010
+#define BACK 020
+#define NEEDS_SCREEN 040
+#define TOPREVFILE 0100
+#define STICKY 0200
+#define NEEDS_COUNT 0400
+#define ESC 01000
+
+extern struct commands {
+ char *c_cmd;
+ int c_flags;
+ int (*c_func)(long);
+ char *c_descr;
+} commands[];
+
+int do_chkm(long cnt);
+void do_comm(int command, long count);
+int lookup(char *str);
+void wrt_fd(int fd);
+
+#endif
diff --git a/cmd/extra/yap/display.c b/cmd/extra/yap/display.c
@@ -0,0 +1,692 @@
+/* Copyright (c) 1985 Ceriel J.H. Jacobs */
+
+#include "in_all.h"
+#include "display.h"
+#include "assert.h"
+#include "machine.h"
+#include "term.h"
+#include "output.h"
+#include "options.h"
+#include "process.h"
+#include "getline.h"
+#include "main.h"
+#define getline yap_getline
+#include "utf.h"
+#undef getline
+#include "ansi.h"
+
+int pagesize;
+int maxpagesize;
+int scrollsize;
+struct scr_info scr_info;
+int status;
+
+static char *do_line(char *str, int reallydispl);
+static void flush_display_buffer(char *buf, char **p);
+static int rune_width(Rune r);
+static int decode_cell(const char *s, const char **next, int *width,
+ int *is_underscore);
+int wcwidth(wchar_t wc);
+
+/*
+ * Fill n lines of the screen, each with "str".
+ */
+
+static void
+fillscr(char *str, int n)
+{
+
+ while (n-- > 0) {
+ putline(str);
+ }
+}
+
+/*
+ * Skip "n" screenlines of line "p", and return what's left of it.
+ */
+
+static char *
+skiplines(char *p, int n)
+{
+
+ while (n-- > 0) {
+ p = do_line(p, 0);
+ scr_info.currentpos--;
+ }
+ return p;
+}
+
+/*
+ * Redraw screen.
+ * "n" = 1 if it is a real redraw, 0 if one page must be displayed.
+ * It is also called when yap receives a stop signal.
+ */
+
+void
+redraw(int n)
+{
+ struct scr_info *p = &scr_info;
+ int i;
+
+ i = pagesize;
+ if (n && p->currentpos) {
+ i = p->currentpos;
+ }
+ (void)display(p->firstline, p->nf, i, 1);
+}
+
+/*
+ * Compute return value for the routines "display" and "scrollf".
+ * This return value indicates wether we are at the end of file
+ * or at the start, or both.
+ * "s" contains that part of the last line that was not displayed.
+ */
+
+static int
+compretval(const char *s)
+{
+ int i;
+ struct scr_info *p = &scr_info;
+
+ i = 0;
+ if (!s || (!*s && !getline(p->lastline + 1, 1))) {
+ i = EOFILE;
+ }
+ if (p->firstline == 1 && !p->nf) {
+ i |= START;
+ }
+ status = i;
+ return i;
+}
+
+/*
+ * Display nlines, starting at line n, not displaying the first
+ * nd screenlines of n.
+ * If reallydispl = 0, the actual displaying is not performed,
+ * only the computing associated with it is done.
+ */
+
+int
+display(long n, int nd, int nlines, int reallydispl)
+{
+
+ struct scr_info *s = &scr_info;
+ char *p; /* pointer to line to be displayed */
+
+ if (startcomm) { /* No displaying on a command from the
+ * yap command line. In this case, displaying
+ * will be done after executing the command,
+ * by a redraw.
+ */
+ reallydispl = 0;
+ }
+ if (!n) {
+ n = 1L;
+ nd = 0;
+ }
+ if (reallydispl) { /* move cursor to starting point */
+ if (stupid) {
+ putline(currentfile);
+ putline(", line ");
+ prnum(n);
+ nlines--;
+ }
+ if (cflag) {
+ putline("\r\n");
+ } else {
+ home();
+ clrscreen();
+ }
+ }
+ /*
+ * Now, do computations and display
+ */
+ s->currentpos = 0;
+ s->nf = nd;
+ s->head = s->tail;
+ s->tail->cnt = 0;
+ s->tail->line = n;
+ p = skiplines(getline(n, 1), nd);
+ while (nlines && p) {
+ /*
+ * While there is room,
+ * and there is something left to display ...
+ */
+ (s->tail->cnt)++;
+ nlines--;
+ if (*(p = do_line(p, reallydispl)) == '\0') {
+ /*
+ * File-line finished, get next one ...
+ */
+ p = getline(++n, 1);
+ if (nlines && p) {
+ s->tail = s->tail->next;
+ s->tail->cnt = 0;
+ s->tail->line = n;
+ }
+ }
+ }
+ if (!stupid) {
+ s->currentpos += nlines;
+ if (reallydispl) {
+ fillscr("~\r\n", nlines);
+ fillscr("\r\n", maxpagesize - s->currentpos);
+ }
+ }
+ return compretval(p);
+}
+
+/*
+ * Scroll forwards n lines.
+ */
+
+int
+scrollf(int n, int reallydispl)
+{
+
+ struct scr_info *s = &scr_info;
+ char *p;
+ long ll;
+ int i;
+
+ /*
+ * First, find out how many screenlines of the last line were already
+ * on the screen, and possibly above it.
+ */
+
+ if (n <= 0 || (status & EOFILE))
+ return status;
+ if (startcomm)
+ reallydispl = 0;
+ /*
+ * Find out where to begin displaying
+ */
+ i = s->tail->cnt;
+ if ((ll = s->lastline) == s->firstline)
+ i += s->nf;
+ p = skiplines(getline(ll, 1), i);
+ /*
+ * Now, place the cursor at the first free line
+ */
+ if (reallydispl && !stupid) {
+ clrbline();
+ mgoto(s->currentpos);
+ }
+ /*
+ * Now display lines, keeping track of which lines are on the screen.
+ */
+ while (n-- > 0) { /* There are still rows to be displayed */
+ if (!*p) { /* End of line, get next one */
+ if (!(p = getline(++ll, 1))) {
+ /*
+ * No lines left. At end of file
+ */
+ break;
+ }
+ s->tail = s->tail->next;
+ s->tail->cnt = 0;
+ s->tail->line = ll;
+ }
+ if (s->currentpos >= maxpagesize) {
+ /*
+ * No room, delete first screen-line
+ */
+ s->currentpos--;
+ s->nf++;
+ if (--(s->head->cnt) == 0) {
+ /*
+ * The first file-line on the screen is wiped
+ * out completely; update administration
+ * accordingly.
+ */
+ s->nf = 0;
+ s->head = s->head->next;
+ assert(s->head->cnt > 0);
+ }
+ }
+ s->tail->cnt++;
+ p = do_line(p, reallydispl);
+ }
+ return compretval(p);
+}
+
+/*
+ * Scroll back n lines
+ */
+
+int
+scrollb(int n, int reallydispl)
+{
+
+ struct scr_info *s = &scr_info;
+ char *p; /* Holds string to be displayed */
+ int i;
+ int count;
+ long ln; /* a line number */
+ int nodispl;
+ int cannotscroll; /* stupid or no insert-line */
+
+ /*
+ * First, find out where to start
+ */
+ if ((count = n) <= 0 || (status & START))
+ return status;
+ if (startcomm)
+ reallydispl = 0;
+ cannotscroll = stupid || (!*AL && !*SR);
+ ln = s->firstline;
+ nodispl = s->nf;
+ while (count) { /* While scrolling back ... */
+ i = nodispl;
+ if (i) {
+ /*
+ * There were screen-lines of s->firstline that were not
+ * displayed.
+ * We can use them now, but only "count" of them.
+ */
+ if (i > count)
+ i = count;
+ s->currentpos += i;
+ nodispl -= i;
+ count -= i;
+ } else { /* Get previous line */
+ if (ln == 1)
+ break; /* isn't there ... */
+ p = getline(--ln, 1);
+ /*
+ * Make it the first line of the screen and compute
+ * how many screenlines it takes. These lines are not
+ * displayed, but nodispl is set to this count, so
+ * that it will be nonzero next time around
+ */
+ nodispl = 0;
+ do { /* Find out how many screenlines */
+ nodispl++;
+ p = skiplines(p, 1);
+ } while (*p);
+ }
+ }
+ n -= count;
+ if ((i = s->currentpos) > maxpagesize)
+ i = maxpagesize;
+ if (reallydispl && hardcopy)
+ i = n;
+ /*
+ * Now that we know where to start, we can use "display" to do the
+ * rest of the computing for us, and maybe even the displaying ...
+ */
+ i = display(ln,
+ nodispl,
+ i,
+ reallydispl && cannotscroll);
+ if (cannotscroll || !reallydispl) {
+ /*
+ * Yes, "display" did the displaying, or we did'nt have to
+ * display at all.
+ * I like it, but the user obviously does not.
+ * Let him buy another (smarter) terminal ...
+ */
+ return i;
+ }
+ /*
+ * Now, all we have to do is the displaying. And we are dealing with
+ * a smart terminal (it can insert lines or scroll back).
+ */
+ home();
+ /*
+ * Insert lines all at once
+ */
+ for (i = n; i; i--) {
+ if (DB && *CE) {
+ /*
+ * Grumble..., terminal retains lines below, so we have
+ * to clear the lines that we push off the screen
+ */
+ clrbline();
+ home();
+ }
+ if (*SR) {
+ scrollreverse();
+ } else {
+ insert_line(0);
+ }
+ }
+ p = skiplines(getline(ln = s->firstline, 1), s->nf);
+ for (i = 0; i < n; i++) {
+ p = do_line(p, 1);
+ s->currentpos--;
+ if (!*p) {
+ p = getline(++ln, 1);
+ }
+ }
+ return count;
+}
+
+/*
+ * Process a line.
+ * If reallydispl > 0 then display it.
+ */
+
+static char *
+do_line(char *str, int reallydispl)
+{
+ char buf[1024];
+ char *p = buf;
+ int pos = COLS;
+ int do_ul = 0, do_hl = 0;
+ int lastmode = 0, lasthlmode = 0;
+ int c2;
+ int cell_width;
+ int tab_width;
+ int is_underscore;
+ int cell_len;
+ int next_len;
+ int next_is_underscore;
+ unsigned char uc;
+ const char *cell_start;
+ const char *cursor;
+ const char *next;
+ enum ansi_kind ansi_kind;
+
+ while (*str && pos > 0) {
+ uc = (unsigned char)*str;
+ if (uc == 0x1b && ansi_escape(str, &next, &ansi_kind)) {
+ if (ansi_kind == ANSI_SGR) {
+ cell_len = next - str;
+ if (reallydispl &&
+ p + cell_len >= &buf[sizeof(buf) - 1]) {
+ flush_display_buffer(buf, &p);
+ }
+ if (reallydispl) {
+ (void)memcpy(p, str, (size_t)cell_len);
+ p += cell_len;
+ }
+ }
+ str = (char *)next;
+ continue;
+ }
+ if (uc < ' ' && (cell_len = match(str, &c2, sppat)) > 0) {
+ /*
+ * We found a string that matches, and thus must be
+ * echoed literally
+ */
+ if ((pos - c2) <= 0) {
+ /*
+ * It did not fit
+ */
+ break;
+ }
+ pos -= c2;
+ if (reallydispl) {
+ flush_display_buffer(buf, &p);
+ cell_start = str;
+ str += cell_len;
+ uc = (unsigned char)*str;
+ *str = '\0';
+ putline((char *)cell_start);
+ *str = (char)uc;
+ } else {
+ str += cell_len;
+ }
+ continue;
+ }
+
+ cell_start = str;
+ cell_len = decode_cell(str, &next, &cell_width, &is_underscore);
+ cursor = next;
+ do_hl = 0;
+ if (*cursor == '\b' && *(cursor + 1) != '\0') {
+ next_len = decode_cell(cursor + 1, &next, &tab_width,
+ &next_is_underscore);
+ if (!(is_underscore && *(cursor + 1 + next_len) != '\b')) {
+ while (*cursor == '\b' && *(cursor + 1) != '\0') {
+ cell_start = cursor + 1;
+ cell_len = decode_cell(cell_start, &next, &cell_width,
+ &is_underscore);
+ cursor = next;
+ do_hl = 1;
+ }
+ }
+ }
+ do_ul = 1;
+ /*
+ * Find underline sequences ...
+ */
+ if (is_underscore && *cursor == '\b' && *(cursor + 1) != '\0') {
+ cell_start = cursor + 1;
+ cell_len = decode_cell(cell_start, &next, &cell_width,
+ &is_underscore);
+ cursor = next;
+ } else {
+ if (*cursor == '\b' && *(cursor + 1) == '_') {
+ cursor += 2;
+ } else {
+ do_ul = 0;
+ }
+ }
+ if (cell_width == -1) {
+ tab_width = 8 - ((COLS - pos) & 0x07);
+ if (pos - tab_width < 0) {
+ break;
+ }
+ } else if (cell_width == -2) {
+ if (pos <= 1) {
+ break;
+ }
+ } else if (cell_width > pos) {
+ break;
+ }
+ if (reallydispl && do_hl != lasthlmode) {
+ flush_display_buffer(buf, &p);
+ if (do_hl) {
+ bold();
+ } else {
+ end_bold();
+ }
+ }
+ lasthlmode = do_hl;
+ if (reallydispl && do_ul != lastmode) {
+ flush_display_buffer(buf, &p);
+ if (do_ul) {
+ underline();
+ } else {
+ end_underline();
+ }
+ }
+ lastmode = do_ul;
+ if (cell_width == -1) {
+ pos -= tab_width;
+ if (reallydispl) {
+ if (expandtabs) {
+ while (tab_width-- > 0) {
+ *p++ = ' ';
+ }
+ } else {
+ flush_display_buffer(buf, &p);
+ givetab();
+ }
+ }
+ str = (char *)cursor;
+ continue;
+ }
+ if (reallydispl && p + cell_len >= &buf[sizeof(buf) - 1]) {
+ flush_display_buffer(buf, &p);
+ }
+ if (cell_width == -2) {
+ if (reallydispl) {
+ if (p + 2 >= &buf[sizeof(buf) - 1]) {
+ flush_display_buffer(buf, &p);
+ }
+ *p++ = '^';
+ *p++ = *cell_start ^ 0x40;
+ }
+ pos -= 2;
+ str = (char *)cursor;
+ continue;
+ }
+ if (reallydispl) {
+ (void)memcpy(p, cell_start, (size_t)cell_len);
+ p += cell_len;
+ }
+ if (cell_width >= 0) {
+ pos -= cell_width;
+ if (reallydispl && do_ul && *UC && cell_width == 1 && pos > 0) {
+ /*
+ * Underlining apparently is done one
+ * character at a time.
+ */
+ flush_display_buffer(buf, &p);
+ backspace();
+ underchar();
+ }
+ str = (char *)cursor;
+ continue;
+ }
+ str = (char *)cursor;
+ }
+ if (reallydispl) {
+ flush_display_buffer(buf, &p);
+ if (pos > 0 || (pos <= 0 && (!AM || XN))) {
+ putline("\r\n");
+ }
+ /*
+ * The next should be here! I.e. it may not be before printing
+ * the newline. This has to do with XN. We don't know exactly
+ * WHEN the terminal will stop ignoring the newline.
+ * I have for example a terminal (Ampex a230) that will
+ * continue to ignore the newline after a clear to end of line
+ * sequence, but not after an end_underline sequence.
+ */
+ if (lastmode) {
+ end_underline();
+ }
+ if (lasthlmode) {
+ end_bold();
+ }
+ }
+ scr_info.currentpos++;
+ return str;
+}
+
+static void
+flush_display_buffer(char *buf, char **p)
+{
+ if (*p == buf) {
+ return;
+ }
+ **p = '\0';
+ putline(buf);
+ *p = buf;
+}
+
+static int
+rune_width(Rune r)
+{
+ int width;
+
+ width = wcwidth((wchar_t)r);
+ if (width < 0) {
+ return 1;
+ }
+ return width;
+}
+
+static int
+decode_cell(const char *s, const char **next, int *width, int *is_underscore)
+{
+ const char *p;
+ Rune r;
+ Rune mark;
+ int len;
+ int mark_len;
+ int mark_width;
+
+ len = chartorune(&r, s);
+ if (len <= 0) {
+ len = 1;
+ r = Runeerror;
+ }
+ *is_underscore = (len == 1 && *s == '_');
+ if (*s == '\t') {
+ *width = -1;
+ *next = s + 1;
+ return 1;
+ }
+ if ((unsigned char)*s < ' ' || (unsigned char)*s == 0x7f) {
+ *width = -2;
+ *next = s + 1;
+ return 1;
+ }
+ *width = rune_width(r);
+ p = s + len;
+ while (*p) {
+ if (*p == '\b' || *p == '\t' || (unsigned char)*p < ' ' ||
+ (unsigned char)*p == 0x7f) {
+ break;
+ }
+ mark_len = chartorune(&mark, p);
+ if (mark_len <= 0) {
+ break;
+ }
+ mark_width = rune_width(mark);
+ if (mark_width != 0) {
+ break;
+ }
+ p += mark_len;
+ len += mark_len;
+ }
+ *next = p;
+ return len;
+}
+
+/* ARGSUSED */
+int
+setmark(long cnt)
+{ /* Set a mark on the current page */
+ struct scr_info *p = &scr_info;
+ (void)cnt;
+
+ p->savfirst = p->firstline;
+ p->savnf = p->nf;
+ return 0;
+}
+
+/* ARGSUSED */
+int
+tomark(long cnt)
+{ /* Go to the mark */
+ struct scr_info *p = &scr_info;
+ (void)cnt;
+
+ (void)display(p->savfirst, p->savnf, pagesize, 1);
+ return 0;
+}
+
+/* ARGSUSED */
+int
+exgmark(long cnt)
+{ /* Exchange mark and current page */
+ struct scr_info *p = &scr_info;
+ long svfirst;
+ int svnf;
+ (void)cnt;
+
+ svfirst = p->firstline;
+ svnf = p->nf;
+ tomark(0L);
+ p->savfirst = svfirst;
+ p->savnf = svnf;
+ return 0;
+}
+
+void
+d_clean()
+{ /* Clean up */
+ struct scr_info *p = &scr_info;
+
+ p->savnf = 0;
+ p->savfirst = 0;
+ p->head = p->tail;
+ p->head->line = 0;
+ p->currentpos = 0;
+}
diff --git a/cmd/extra/yap/display.h b/cmd/extra/yap/display.h
@@ -0,0 +1,41 @@
+#ifndef DISPLAY_H
+#define DISPLAY_H
+
+#define MINPAGESIZE 5
+
+extern int pagesize;
+extern int maxpagesize;
+extern int scrollsize;
+
+struct scr_info {
+ struct linelist {
+ int cnt;
+ long line;
+#define firstline head->line
+#define lastline tail->line
+ struct linelist *next;
+ struct linelist *prev;
+ } *tail, *head;
+ int nf;
+ int currentpos;
+ struct linelist ssaavv;
+#define savfirst ssaavv.line
+#define savnf ssaavv.cnt
+};
+
+extern struct scr_info scr_info;
+extern int status;
+
+#define EOFILE 01
+#define START 02
+
+void redraw(int flag);
+int display(long first_line, int nodispl, int nlines, int really);
+int scrollf(int nlines, int really);
+int scrollb(int nlines, int really);
+int tomark(long cnt);
+int setmark(long cnt);
+int exgmark(long cnt);
+void d_clean(void);
+
+#endif
diff --git a/cmd/extra/yap/getcomm.c b/cmd/extra/yap/getcomm.c
@@ -0,0 +1,303 @@
+/* Copyright (c) 1985 Ceriel J.H. Jacobs */
+
+/*
+ * Command reader, also executes shell escapes
+ */
+
+#include <ctype.h>
+#include "in_all.h"
+#include "term.h"
+#include "process.h"
+#include "getcomm.h"
+#include "commands.h"
+#include "prompt.h"
+#include "main.h"
+#include "output.h"
+#include "getline.h"
+#include "machine.h"
+#include "keys.h"
+#include "display.h"
+#include "assert.h"
+
+static int killchar(int c);
+static void sigquit(int signo);
+
+/*
+ * Read a line from the terminal, doing line editing.
+ * The parameter s contains the prompt for the line.
+ */
+
+char *
+readline(char *s)
+{
+
+ static char buf[80];
+ char *p = buf;
+ int ch;
+ int pos;
+
+ clrbline();
+ putline(s);
+ pos = strlen(s);
+ while ((ch = getch()) != '\n' && ch != '\r') {
+ if (ch == -1) {
+ /*
+ * Can only occur because of an interrupted read.
+ */
+ ch = erasech;
+ interrupt = 0;
+ }
+ if (ch == erasech) {
+ /*
+ * Erase last char
+ */
+ if (p == buf) {
+ /*
+ * There was none, so return
+ */
+ return (char *)0;
+ }
+ pos -= killchar(*--p);
+ if (*p != '\\')
+ continue;
+ }
+ if (ch == killch) {
+ /*
+ * Erase the whole line
+ */
+ if (!(p > buf && *(p - 1) == '\\')) {
+ while (p > buf) {
+ pos -= killchar(*--p);
+ }
+ continue;
+ }
+ pos -= killchar(*--p);
+ }
+ if (p > &buf[78] || pos >= COLS - 2) {
+ /*
+ * Line does not fit.
+ * Simply refuse to make it any longer
+ */
+ pos -= killchar(*--p);
+ }
+ *p++ = ch;
+ if (ch < ' ' || ch >= 0177) {
+ fputch('^');
+ pos++;
+ ch ^= 0100;
+ }
+ fputch(ch);
+ pos++;
+ }
+ fputch('\r');
+ *p++ = '\0';
+ flush();
+ return buf;
+}
+
+/*
+ * Erase a character from the command line.
+ */
+
+static int
+killchar(int c)
+{
+
+ backspace();
+ putch(' ');
+ backspace();
+ if (c < ' ' || c >= 0177) {
+ (void)killchar(' ');
+ return 2;
+ }
+ return 1;
+}
+
+/*
+ * Do a shell escape, after expanding '%' and '!'.
+ */
+
+void
+shellescape(char *p, int esc_char)
+{
+ char *p2; /* walks through command */
+ int id; /* procid of child */
+ int cnt; /* prevent array bound errors */
+ int lastc = 0; /* will contain the previous char */
+#ifdef SIGTSTP
+ void (*savetstp)(int);
+#endif
+ static char previous[256]; /* previous command */
+ char comm[256]; /* space for command */
+ int piped[2];
+
+ p2 = comm;
+ *p2++ = esc_char;
+ cnt = 253;
+ while (*p) {
+ /*
+ * expand command
+ */
+ switch (*p++) {
+ case '!':
+ /*
+ * An unescaped ! expands to the previous
+ * command, but disappears if there is none
+ */
+ if (lastc != '\\') {
+ if (*previous) {
+ id = strlen(previous);
+ if ((cnt -= id) <= 0)
+ break;
+ (void)strcpy(p2, previous);
+ p2 += id;
+ }
+ } else {
+ *(p2 - 1) = '!';
+ }
+ continue;
+ case '%':
+ /*
+ * An unescaped % will expand to the current
+ * filename, but disappears is there is none
+ */
+ if (lastc != '\\') {
+ if (nopipe) {
+ id = strlen(currentfile);
+ if ((cnt -= id) <= 0)
+ break;
+ (void)strcpy(p2, currentfile);
+ p2 += id;
+ }
+ } else {
+ *(p2 - 1) = '%';
+ }
+ continue;
+ default:
+ lastc = *(p - 1);
+ if (cnt-- <= 0)
+ break;
+ *p2++ = lastc;
+ continue;
+ }
+ break;
+ }
+ clrbline();
+ *p2 = '\0';
+ if (!stupid) {
+ /*
+ * Display expanded command
+ */
+ cputline(comm);
+ putline("\r\n");
+ }
+ flush();
+ (void)strcpy(previous, comm + 1);
+ resettty();
+ if (esc_char == '|' && pipe(piped) < 0) {
+ error("Cannot create pipe");
+ return;
+ }
+ if ((id = fork()) < 0) {
+ error("Cannot fork");
+ return;
+ }
+ if (id == 0) {
+ if (esc_char == '|') {
+ close(piped[1]);
+ close(0);
+ fcntl(piped[0], F_DUPFD, 0);
+ close(piped[0]);
+ }
+ execl("/bin/sh", "sh", "-c", comm + 1, (char *)0);
+ exit(1);
+ }
+ (void)signal(SIGINT, SIG_IGN);
+ (void)signal(SIGQUIT, SIG_IGN);
+#ifdef SIGTSTP
+ if ((savetstp = signal(SIGTSTP, SIG_IGN)) != SIG_IGN) {
+ (void)signal(SIGTSTP, SIG_DFL);
+ }
+#endif
+ if (esc_char == '|') {
+ (void)close(piped[0]);
+ (void)signal(SIGPIPE, SIG_IGN);
+ wrt_fd(piped[1]);
+ (void)close(piped[1]);
+ }
+ while ((lastc = wait((int *)0)) != id && lastc >= 0) {
+ /*
+ * Wait for child, making sure it is the one we expected ...
+ */
+ }
+ (void)signal(SIGINT, catchdel);
+ (void)signal(SIGQUIT, sigquit);
+#ifdef SIGTSTP
+ (void)signal(SIGTSTP, savetstp);
+#endif
+ inittty();
+}
+
+static void
+sigquit(int signo)
+{
+ (void)signo;
+ quit();
+}
+
+/*
+ * Get all those commands ...
+ */
+
+int
+getcomm(long *plong)
+{
+ int c;
+ long count;
+ char *p;
+ int i;
+ int j;
+ char buf[10];
+
+ for (;;) {
+ count = 0;
+ give_prompt();
+ while (isdigit((c = getch()))) {
+ count = count * 10 + (c - '0');
+ }
+ *plong = count;
+ p = buf;
+ for (;;) {
+ if (c == -1) {
+ /*
+ * This should never happen, but it does,
+ * when the user gives a TSTP signal (^Z) or
+ * an interrupt while the program is trying
+ * to read a character from the terminal.
+ * In this case, the read is interrupted, so
+ * we end up here.
+ * Right, we will have to read again.
+ */
+ if (interrupt)
+ return 1;
+ break;
+ }
+ *p++ = c;
+ *p = 0;
+ if ((i = match(buf, &j, currmap->k_mach)) > 0) {
+ /*
+ * The key sequence matched. We have a command
+ */
+ return j;
+ }
+ if (i == 0)
+ return 0;
+ /*
+ * We have a prefix of a command.
+ */
+ assert(i == FSM_ISPREFIX);
+ c = getch();
+ }
+ }
+ /* NOTREACHED */
+}
diff --git a/cmd/extra/yap/getcomm.h b/cmd/extra/yap/getcomm.h
@@ -0,0 +1,8 @@
+#ifndef GETCOMM_H
+#define GETCOMM_H
+
+int getcomm(long *arg);
+void shellescape(char *command, int esc_char);
+char *readline(char *prompt);
+
+#endif
diff --git a/cmd/extra/yap/getline.c b/cmd/extra/yap/getline.c
@@ -0,0 +1,475 @@
+/* Copyright (c) 1985 Ceriel J.H. Jacobs */
+
+#include "in_all.h"
+#include "getline.h"
+#include "options.h"
+#include "process.h"
+#include "term.h"
+#include "main.h"
+#include "display.h"
+#include "output.h"
+#include "prompt.h"
+#include "assert.h"
+
+#define BLOCKSIZE 2048 /* size of blocks */
+#define CHUNK 50 /* # of blockheaders allocated at a time */
+
+/*
+ * Blocks are kept in an array in line-number order. Each block stores the raw
+ * text and the offsets of the lines parsed from it.
+ */
+
+struct block {
+ int b_flags; /* Contains the following flags: */
+#define PARTLY 02 /* block not filled completely (eof) */
+ long b_end; /* line number of last line in block */
+ char *b_info; /* the block */
+ int *b_offs; /* line offsets within the block */
+};
+
+static struct block *blocklist, /* beginning of the list of blocks */
+ *maxblocklist, /* first free entry in the list */
+ *topblocklist; /* end of allocated core for the list */
+static long lastreadline; /* lineno of last line read */
+static int ENDseen;
+
+static void nextblock(struct block *pblock);
+static char *re_alloc(char *ptr, unsigned newsize);
+static struct block *getblock(long n, int disable_interrupt);
+
+static struct block *
+new_block()
+{
+ struct block *pblock = maxblocklist - 1;
+
+ if (!maxblocklist || !(pblock->b_flags & PARTLY)) {
+ /*
+ * There is no last block, or it was filled completely,
+ * so allocate a new blockheader.
+ */
+ int siz;
+
+ pblock = blocklist;
+ if (maxblocklist == topblocklist) {
+ /*
+ * No blockheaders left. Allocate new ones
+ */
+ siz = topblocklist - pblock;
+ blocklist = pblock = (struct block *)
+ re_alloc((char *)pblock,
+ (unsigned)((siz + CHUNK) * sizeof(*pblock)));
+ pblock += siz;
+ topblocklist = pblock + CHUNK;
+ maxblocklist = pblock;
+ for (; pblock < topblocklist; pblock++) {
+ pblock->b_end = 0;
+ pblock->b_info = 0;
+ pblock->b_flags = 0;
+ }
+ if (!siz) {
+ /*
+ * Create dummy header cell.
+ */
+ maxblocklist++;
+ }
+ }
+ pblock = maxblocklist++;
+ }
+ nextblock(pblock);
+ return pblock;
+}
+
+/*
+ * Return the block in which line 'n' of the current file can be found.
+ * If "disable_interrupt" = 0, the call may be interrupted, in which
+ * case it returns 0.
+ */
+
+static struct block *
+getblock(long n, int disable_interrupt)
+{
+ struct block *pblock;
+
+ if (stdf < 0) {
+ /*
+ * Not file descriptor, so return end of file
+ */
+ return 0;
+ }
+ pblock = maxblocklist - 1;
+ if (n < lastreadline ||
+ (n == lastreadline && !(pblock->b_flags & PARTLY))) {
+ /*
+ * The line asked for has been read already.
+ * Perform binary search in the blocklist to find the block
+ * where it's in.
+ */
+ struct block *min, *mid;
+
+ min = blocklist + 1;
+ do {
+ mid = min + (pblock - min) / 2;
+ if (n > mid->b_end) {
+ min = mid + 1;
+ } else
+ pblock = mid;
+ } while (min < pblock);
+ /* Found, pblock is now a reference to the block wanted */
+ return pblock;
+ }
+
+ /*
+ * The line was'nt read yet, so read blocks until found
+ */
+ for (;;) {
+ if (interrupt && !disable_interrupt)
+ return 0;
+ pblock = new_block();
+ if (pblock->b_end >= n) {
+ return pblock;
+ }
+ if (pblock->b_flags & PARTLY) {
+ /*
+ * We did not find it, and the last block could not be
+ * read completely, so return 0;
+ */
+ return 0;
+ }
+ }
+ /* NOTREACHED */
+}
+
+char *
+getline(long n, int disable_interrupt)
+{
+ struct block *pblock;
+
+ if (!(pblock = getblock(n, disable_interrupt))) {
+ return (char *)0;
+ }
+ return pblock->b_info + pblock->b_offs[n - ((pblock - 1)->b_end + 1)];
+}
+
+/*
+ * Find the last line of the input, and return its number
+ */
+
+long
+to_lastline()
+{
+
+ for (;;) {
+ if (!getline(lastreadline + 1, 0)) {
+ /*
+ * "lastreadline" always contains the linenumber of
+ * the last line read. So, if the call to getline
+ * succeeds, "lastreadline" is affected
+ */
+ if (interrupt)
+ return -1L;
+ return lastreadline;
+ }
+ }
+ /* NOTREACHED */
+}
+
+char *
+alloc(unsigned size)
+{
+ char *pmem;
+
+ pmem = malloc(size);
+ if (!pmem && size != 0) {
+ panic("No core");
+ }
+ return pmem;
+}
+
+/*
+ * Re-allocate the memorychunk pointed to by ptr, to let it
+ * grow or shrink.
+ */
+
+static char *
+re_alloc(char *ptr, unsigned newsize)
+{
+ char *pmem;
+
+ pmem = realloc(ptr, newsize);
+ if (!pmem && newsize != 0) {
+ panic("No core");
+ }
+ return pmem;
+}
+
+static char *saved;
+static long filldegree;
+
+/*
+ * Try to read the block indicated by pblock
+ */
+
+static void
+nextblock(struct block *pblock)
+{
+ char *c, /* Run through pblock->b_info */
+ *c1; /* indicate end of pblock->b_info */
+ int *poff; /* pointer in line-offset list */
+ int cnt; /* # of characters read */
+ unsigned siz; /* Size of allocated line-offset list */
+ static unsigned savedsiz; /* saved "siz" */
+ static int *savedpoff; /* saved "poff" */
+ static char *savedc1; /* saved "c1" */
+
+ if (pblock->b_flags & PARTLY) {
+ /*
+ * The block was already partly filled. Initialize locals
+ * accordingly
+ */
+ poff = savedpoff;
+ siz = savedsiz;
+ pblock->b_flags = 0;
+ c1 = savedc1;
+ if (c1 == pblock->b_info || *(c1 - 1)) {
+ /*
+ * We had incremented "lastreadline" temporarily,
+ * because the last line could not be completely read
+ * last time we tried. Undo this increment
+ */
+ poff--;
+ --lastreadline;
+ }
+ } else {
+ if (saved) {
+ /*
+ * There were leftovers from the previous block
+ */
+ pblock->b_info = saved;
+ c1 = savedc1;
+ saved = 0;
+ } else { /* Allocate new block */
+ pblock->b_info = c1 = alloc(BLOCKSIZE + 1);
+ }
+ /*
+ * Allocate some space for line-offsets
+ */
+ pblock->b_offs = poff = (int *)
+ alloc((unsigned)(100 * sizeof(int)));
+ siz = 99;
+ *poff++ = 0;
+ }
+ c = c1;
+ for (;;) {
+ /*
+ * Read loop
+ */
+ cnt = read(stdf, c1, BLOCKSIZE - (c1 - pblock->b_info));
+ if (cnt < 0) {
+ /*
+ * Interrupted read
+ */
+ if (errno == EINTR)
+ continue;
+ error("Could not read input file");
+ cnt = 0;
+ }
+ c1 += cnt;
+ if (c1 != pblock->b_info + BLOCKSIZE) {
+ ENDseen = 1;
+ pblock->b_flags |= PARTLY;
+ }
+ break;
+ }
+ assert(c <= c1);
+ while (c < c1) {
+ /*
+ * Now process the block
+ */
+ if (*c == '\n') {
+ /*
+ * Newlines are replaced by '\0', so that "getline"
+ * can deliver one line at a time
+ */
+ *c = 0;
+ lastreadline++;
+ /*
+ * Remember the line-offset
+ */
+ if (poff == pblock->b_offs + siz) {
+ /*
+ * No space for it, allocate some more
+ */
+ pblock->b_offs = (int *)
+ re_alloc((char *)pblock->b_offs,
+ (unsigned)((siz + 51) * sizeof(int)));
+ poff = pblock->b_offs + siz;
+ siz += 50;
+ }
+ *poff++ = c - pblock->b_info + 1;
+ }
+ c++;
+ }
+ assert(c == c1);
+ *c = 0;
+ if (c != pblock->b_info && *(c - 1) != 0) {
+ /*
+ * The last line read does not end with a newline, so add one
+ */
+ lastreadline++;
+ *poff++ = c - pblock->b_info + 1;
+ if (!(pblock->b_flags & PARTLY) && *(poff - 2) != 0) {
+ /*
+ * Save the started line; it will be in the next block.
+ * Remove the newline we added just now.
+ */
+ saved = c1 = alloc(BLOCKSIZE + 1);
+ c = pblock->b_info + *(--poff - 1);
+ while (*c)
+ *c1++ = *c++;
+ c = pblock->b_info + *(poff - 1);
+ savedc1 = c1;
+ --lastreadline;
+ }
+ }
+ pblock->b_end = lastreadline;
+ if (pblock->b_flags & PARTLY) {
+ /*
+ * Take care, that we can call "nextblock" again, to fill in
+ * the rest of this block
+ */
+ savedsiz = siz;
+ savedpoff = poff;
+ savedc1 = c;
+ if (c == pblock->b_info) {
+ lastreadline++;
+ pblock->b_end = 0;
+ }
+ } else {
+ cnt = pblock - blocklist;
+ filldegree = ((c - pblock->b_info) + (cnt - 1) * filldegree) / cnt;
+ }
+ assert(pblock->b_end - (pblock - 1)->b_end <= poff - pblock->b_offs);
+}
+
+/*
+ * Called after processing a file.
+ * Free all core.
+ */
+
+void
+do_clean()
+{
+
+ struct block *pblock;
+ char *p;
+
+ for (pblock = blocklist; pblock < maxblocklist; pblock++) {
+ if ((p = pblock->b_info) != 0) {
+ free(p);
+ free((char *)pblock->b_offs);
+ }
+ }
+ if ((p = (char *)blocklist) != 0) {
+ free(p);
+ }
+ blocklist = 0;
+ maxblocklist = 0;
+ topblocklist = 0;
+ lastreadline = 0;
+ filldegree = 0;
+ ENDseen = 0;
+ if ((p = saved) != 0)
+ free(p);
+ saved = 0;
+}
+
+/*
+ * Get a character. If possible, do some workahead.
+ */
+
+int
+getch()
+{
+ int flags, bytes_ready, bytes_read;
+ struct stat buf;
+ char c;
+
+ flush();
+ if (startcomm) {
+ /*
+ * Command line option command
+ */
+ if (*startcomm)
+ return *startcomm++;
+ return '\n';
+ }
+ if (stdf >= 0) {
+ /*
+ * Make reads from the terminal non-blocking, so that
+ * we can see if the user typed something
+ */
+ flags = fcntl(0, F_GETFL, 0);
+ if (flags != -1 && fcntl(0, F_SETFL, flags | O_NONBLOCK) != -1) {
+ bytes_read = 0;
+ while (!ENDseen &&
+ ((bytes_read = read(0, &c, 1)) == 0
+#ifdef EWOULDBLOCK
+ || (bytes_read < 0 && errno == EWOULDBLOCK)
+#endif
+#ifdef EAGAIN
+ || (bytes_read < 0 && errno == EAGAIN)
+#endif
+ ) &&
+ (nopipe ||
+ (fstat(stdf, &buf) >= 0 && buf.st_size > 0))) {
+ /*
+ * Do some read ahead, after making sure there
+ * is input and the user did not type a command
+ */
+ new_block();
+ }
+ (void)fcntl(0, F_SETFL, flags);
+ if (bytes_read < 0) {
+ /*
+ * Could this have happened?
+ * I'm not sure, because the read is
+ * nonblocking. Can it be interrupted then?
+ */
+ return -1;
+ }
+ if (bytes_read > 0)
+ return c & 0x7f;
+ }
+ }
+ if (ioctl(0, FIONREAD, (char *)&bytes_ready) >= 0 && stdf >= 0) {
+ while (!ENDseen &&
+ bytes_ready == 0 &&
+ (nopipe || (fstat(stdf, &buf) >= 0 && buf.st_size > 0))) {
+ if (interrupt)
+ return -1;
+ new_block();
+ if (ioctl(0, FIONREAD, (char *)&bytes_ready) < 0) {
+ break;
+ }
+ }
+ }
+ if (read(0, &c, 1) <= 0)
+ return -1;
+ return c & 0x7f;
+}
+
+/*
+ * Get the position of line "ln" in the file.
+ */
+
+long
+getpos(long ln)
+{
+ struct block *pblock;
+ long i;
+
+ pblock = getblock(ln, 1);
+ assert(pblock != 0);
+ i = filldegree * (pblock - blocklist);
+ return i - (filldegree - pblock->b_offs[ln - (pblock - 1)->b_end]);
+}
diff --git a/cmd/extra/yap/getline.h b/cmd/extra/yap/getline.h
@@ -0,0 +1,11 @@
+#ifndef GETLINE_H
+#define GETLINE_H
+
+char *getline(long ln, int disable_interrupt);
+char *alloc(unsigned size);
+void do_clean(void);
+int getch(void);
+long to_lastline(void);
+long getpos(long line);
+
+#endif
diff --git a/cmd/extra/yap/help.c b/cmd/extra/yap/help.c
@@ -0,0 +1,112 @@
+/* Copyright (c) 1985 Ceriel J.H. Jacobs */
+
+#include "in_all.h"
+#include "help.h"
+#include "machine.h"
+#include "commands.h"
+#include "keys.h"
+#include "output.h"
+#include "prompt.h"
+#include "main.h"
+#include "display.h"
+#include "term.h"
+#include "options.h"
+
+static int h_cnt; /* Count # of lines */
+static struct state *origin; /* Keep track of startstate */
+
+/*
+ * Print a key sequence.
+ * We arrived at an endstate. The s_next link in the state structure now
+ * leads us from "origin" to the current state, so that we can print the key
+ * sequence easily.
+ */
+
+static void
+pr_comm()
+{
+ struct state *p = origin;
+ char *pb;
+ int c;
+ char buf[30];
+ int i = 0; /* How many characters printed? */
+
+ pb = buf;
+ for (;;) {
+ c = p->s_char & 0177;
+ if (c < ' ' || c == 0177) {
+ /*
+ * Will take an extra position
+ */
+ i++;
+ }
+ *pb++ = c;
+ i++;
+ if (!p->s_match)
+ break;
+ p = p->s_next;
+ }
+ do {
+ *pb++ = ' ';
+ } while (++i < 12);
+ *pb = 0;
+ cputline(buf);
+}
+
+/*
+ * Print out a description of the keymap. This is done, by temporarily using
+ * the s_next field in the state structure indicate the state matching the
+ * next character, so that we can walk from "origin" to an endstate.
+ */
+
+static void
+pr_mach(struct state *currstate, struct state *back)
+{
+ struct state *save;
+
+ while (currstate) {
+ if (interrupt)
+ break;
+ if (back) {
+ save = back->s_next; /* Save original link */
+ back->s_next = currstate;
+ }
+ if (!currstate->s_match) {
+ /*
+ * End state, print command
+ */
+ pr_comm();
+ putline(commands[currstate->s_cnt].c_descr);
+ putline("\r\n");
+ if (++h_cnt >= maxpagesize) {
+ ret_to_continue();
+ h_cnt = 0;
+ }
+ } else
+ pr_mach(currstate->s_match, currstate);
+ currstate = currstate->s_next;
+ if (back)
+ back->s_next = save; /* restore */
+ else
+ origin = currstate;
+ }
+}
+
+/*ARGSUSED*/
+int
+do_help(long i)
+{ /* The help command */
+ (void)i;
+
+ startcomm = 0;
+ h_cnt = 2;
+ putline("\r\nSummary of yap commands:\r\n");
+ origin = currmap->k_mach;
+ pr_mach(currmap->k_mach, (struct state *)0);
+ if (h_cnt) {
+ ret_to_continue();
+ }
+ if (!hardcopy && scr_info.currentpos)
+ redraw(1);
+ return 0;
+}
diff --git a/cmd/extra/yap/help.h b/cmd/extra/yap/help.h
@@ -0,0 +1,6 @@
+#ifndef HELP_H
+#define HELP_H
+
+int do_help(long cnt);
+
+#endif
diff --git a/cmd/extra/yap/in_all.h b/cmd/extra/yap/in_all.h
@@ -0,0 +1,25 @@
+#ifndef IN_ALL_H
+#define IN_ALL_H
+
+#ifndef _DEFAULT_SOURCE
+#define _DEFAULT_SOURCE
+#endif
+
+#include <errno.h>
+#include <fcntl.h>
+#include <libgen.h>
+#include <locale.h>
+#include <regex.h>
+#include <signal.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <termios.h>
+#include <unistd.h>
+#include <wchar.h>
+
+#endif
diff --git a/cmd/extra/yap/keys.c b/cmd/extra/yap/keys.c
@@ -0,0 +1,187 @@
+/* Copyright (c) 1985 Ceriel J.H. Jacobs */
+
+#include <ctype.h>
+#include "in_all.h"
+#include "machine.h"
+#include "keys.h"
+#include "commands.h"
+#include "prompt.h"
+#include "assert.h"
+
+struct keymap *currmap, *othermap;
+
+char defaultmap[] = "\
+bf=P:bl=k:bl=^K:bl=^[[A:bot=l:bot=$:bp=-:bs=^B:bse=?:bsl=S:bsp=F:chm=X:exg=x:\
+ff=N:fl=^J:fl=^M:fl=j:fl=^[[B:fp= :fs=^D:fse=/:fsl=s:fsp=f:hlp=h:nse=n:nsr=r:\
+red=^L:rep=.:bps=Z:bss=b:fps=z:fss=d:shl=!:tom=':top=\\^:vis=e:\
+wrf=w:qui=q:qui=Q:mar=m:pip=|";
+
+/*
+ * Construct an error message and return it
+ */
+
+static char *
+kerror(char *key, char *emess)
+{
+ static char ebuf[80]; /* Room for the error message */
+
+ (void)strcpy(ebuf, key);
+ (void)strcat(ebuf, emess);
+ return ebuf;
+}
+
+/*
+ * Compile a keymap into commtable. Returns an error message if there
+ * is one
+ */
+
+static char *
+compile(char *map, struct keymap *commtable)
+{
+ char *mark; /* Indicates start of mnemonic */
+ char *c; /* Runs through buf */
+ int temp;
+ char *escapes = commtable->k_esc;
+ char buf[10]; /* Will hold key sequence */
+
+ (void)strcpy(commtable->k_help, "Illegal command");
+ while (*map) {
+ c = buf;
+ mark = map; /* Start of mnemonic */
+ while (*map && *map != '=') {
+ map++;
+ }
+ if (!*map) {
+ /*
+ * Mnemonic should end with '='
+ */
+ return kerror(mark, ": Syntax error");
+ }
+ *map++ = 0;
+ while (*map) {
+ /*
+ * Get key sequence
+ */
+ if (*map == ':') {
+ /*
+ * end of key sequence
+ */
+ map++;
+ break;
+ }
+ *c = *map++ & 0x7f;
+ if (*c == '^' || *c == '\\') {
+ if (!(temp = *map++)) {
+ /*
+ * Escape not followed by a character
+ */
+ return kerror(mark, ": Syntax error");
+ }
+ if (*c == '^') {
+ if (temp == '?')
+ *c = 0x7f;
+ else
+ *c = temp & 0x1f;
+ } else
+ *c = temp & 0x7f;
+ }
+ setused(*c);
+ c++;
+ if (c >= &buf[9]) {
+ return kerror(mark, ": Key sequence too long");
+ }
+ }
+ *c = 0;
+ if (!(temp = lookup(mark))) {
+ return kerror(mark, ": Nonexistent function");
+ }
+ if (c == &buf[1] && (commands[temp].c_flags & ESC) &&
+ escapes < &(commtable->k_esc[sizeof(commtable->k_esc) - 1])) {
+ *escapes++ = buf[0] & 0x7f;
+ }
+ temp = addstring(buf, temp, &(commtable->k_mach));
+ if (temp == FSM_ISPREFIX) {
+ return kerror(mark, ": Prefix of other key sequence");
+ }
+ if (temp == FSM_HASPREFIX) {
+ return kerror(mark, ": Other key sequence is prefix");
+ }
+ assert(temp == FSM_OKE);
+ if (!strcmp(mark, "hlp")) {
+ /*
+ * Create an error message to be given when the user
+ * types an illegal command
+ */
+ (void)strcpy(commtable->k_help, "Type ");
+ (void)strcat(commtable->k_help, buf);
+ (void)strcat(commtable->k_help, " for help");
+ }
+ }
+ *escapes = 0;
+ return (char *)0;
+}
+
+/*
+ * Initialize the keymaps
+ */
+
+void
+initkeys()
+{
+ char *p;
+ static struct keymap xx[2];
+
+ currmap = &xx[0];
+ othermap = &xx[1];
+ p = compile(defaultmap, currmap); /* Compile default map */
+ assert(p == (char *)0);
+ p = getenv("YAPKEYS");
+ if (p) {
+ if (!(p = compile(p, othermap))) {
+ /*
+ * No errors in user defined keymap. So, use it
+ */
+ do_chkm(0L);
+ return;
+ }
+ error(p);
+ }
+ othermap = 0; /* No other keymap */
+}
+
+int
+is_escape(int c)
+{
+ char *p = currmap->k_esc;
+
+ while (*p) {
+ if (c == *p++)
+ return 1;
+ }
+ return 0;
+}
+
+static char keyset[16]; /* bitset indicating which keys are
+ * used
+ */
+/*
+ * Mark key "key" as used
+ */
+
+void
+setused(int key)
+{
+
+ keyset[(key & 0x7f) >> 3] |= (1 << (key & 0x07));
+}
+
+/*
+ * return non-zero if key "key" is used in a keymap
+ */
+
+int
+isused(int key)
+{
+
+ return keyset[(key & 0x7f) >> 3] & (1 << (key & 0x07));
+}
diff --git a/cmd/extra/yap/keys.h b/cmd/extra/yap/keys.h
@@ -0,0 +1,15 @@
+#ifndef KEYS_H
+#define KEYS_H
+
+extern struct keymap {
+ char k_help[80];
+ struct state *k_mach;
+ char k_esc[10];
+} *currmap, *othermap;
+
+void initkeys(void);
+void setused(int key);
+int isused(int key);
+int is_escape(int c);
+
+#endif
diff --git a/cmd/extra/yap/machine.c b/cmd/extra/yap/machine.c
@@ -0,0 +1,134 @@
+/* Copyright (c) 1985 Ceriel J.H. Jacobs */
+
+#include <ctype.h>
+#include "in_all.h"
+#include "machine.h"
+#include "getline.h"
+#include "assert.h"
+
+/*
+ * Add part of finite state machine to recognize the string s.
+ */
+
+static int
+addtomach(char *s, int cnt, struct state **list)
+{
+
+ struct state *l;
+ int i = FSM_OKE; /* Return value */
+ int j;
+
+ for (;;) {
+ l = *list;
+ if (!l) {
+ /*
+ * Create new list element
+ */
+ *list = l = (struct state *)alloc(sizeof(*l));
+ l->s_char = *s;
+ l->s_endstate = 0;
+ l->s_match = 0;
+ l->s_next = 0;
+ }
+ if (l->s_char == *s) {
+ /*
+ * Continue with next character
+ */
+ if (!*++s) {
+ /*
+ * No next character
+ */
+ j = l->s_endstate;
+ l->s_endstate = 1;
+ if (l->s_match || j) {
+ /*
+ * If the state already was an endstate,
+ * or has a successor, the currently
+ * added string is a prefix of an
+ * already recognized string
+ */
+ return FSM_ISPREFIX;
+ }
+ l->s_cnt = cnt;
+ return i;
+ }
+ if (l->s_endstate) {
+ /*
+ * In this case, the currently added string has
+ * a prefix that is an already recognized
+ * string.
+ */
+ i = FSM_HASPREFIX;
+ }
+ list = &(l->s_match);
+ continue;
+ }
+ list = &(l->s_next);
+ }
+ /* NOTREACHED */
+}
+
+/*
+ * Add a string to the FSM.
+ */
+
+int
+addstring(char *s, int cnt, struct state **machine)
+{
+
+ if (!s || !*s) {
+ return FSM_ISPREFIX;
+ }
+ return addtomach(s, cnt, machine);
+}
+
+/*
+ * Match string s with the finite state machine.
+ * If it matches, the number of characters actually matched is returned,
+ * and the count is put in the word pointed to by i.
+ * If the string is a prefix of a string that could be matched,
+ * FSM_ISPREFIX is returned. Otherwise, 0 is returned.
+ */
+
+int
+match(char *s, int *i, struct state *mach)
+{
+
+ char *s1 = s; /* Walk through string */
+ struct state *mach1 = 0;
+ /* Keep track of previous state */
+
+ while (mach && *s1) {
+ if (mach->s_char == *s1) {
+ /*
+ * Current character matches. Carry on with next
+ * character and next state
+ */
+ mach1 = mach;
+ mach = mach->s_match;
+ s1++;
+ continue;
+ }
+ mach = mach->s_next;
+ }
+ if (!mach1) {
+ /*
+ * No characters matched
+ */
+ return 0;
+ }
+ if (mach1->s_endstate) {
+ /*
+ * The string matched
+ */
+ *i = mach1->s_cnt;
+ return s1 - s;
+ }
+ if (!*s1) {
+ /*
+ * The string matched a prefix
+ */
+ return FSM_ISPREFIX;
+ }
+ return 0;
+}
diff --git a/cmd/extra/yap/machine.h b/cmd/extra/yap/machine.h
@@ -0,0 +1,19 @@
+#ifndef MACHINE_H
+#define MACHINE_H
+
+struct state {
+ char s_char;
+ char s_endstate;
+ struct state *s_match;
+ struct state *s_next;
+ short s_cnt;
+};
+
+#define FSM_OKE 0
+#define FSM_ISPREFIX -1
+#define FSM_HASPREFIX 1
+
+int addstring(char *str, int cnt, struct state **mach);
+int match(char *str, int *p_int, struct state *mach);
+
+#endif
diff --git a/cmd/extra/yap/main.c b/cmd/extra/yap/main.c
@@ -0,0 +1,195 @@
+/* Copyright (c) 1985 Ceriel J.H. Jacobs */
+
+#include "in_all.h"
+#include "main.h"
+#include "term.h"
+#include "options.h"
+#include "output.h"
+#include "process.h"
+#include "commands.h"
+#include "display.h"
+#include "prompt.h"
+
+int nopipe;
+char *progname;
+int interrupt;
+int no_tty;
+
+static int initialize(int x);
+static void sigquit(int signo);
+#ifdef SIGTSTP
+static void suspsig(int signo);
+#endif
+
+int
+main(int argc, char **argv)
+{
+ char **av;
+
+ (void)setlocale(LC_CTYPE, "");
+ if (!isatty(1)) {
+ no_tty = 1;
+ }
+ argv[argc] = 0;
+ progname = argv[0];
+ if ((av = readoptions(argv)) == (char **)0 ||
+ initialize(*av ? 1 : 0)) {
+ if (no_tty) {
+ close(1);
+ (void)dup(2);
+ }
+ putline("Usage: ");
+ putline(argv[0]);
+ putline(
+ " [-c] [-u] [-n] [-q] [-number] [+command] [file ... ]\n");
+ flush();
+ exit(1);
+ }
+ if (no_tty) {
+ *--av = "cat";
+ execve("/bin/cat", av, (char *const[]){NULL});
+ } else
+ processfiles(argc - (av - argv), av);
+ (void)quit();
+ /* NOTREACHED */
+}
+
+/*
+ * Open temporary file for reading and writing.
+ * Panic if it fails
+ */
+
+/*
+ * Collect initializing stuff here.
+ */
+
+static int
+initialize(int x)
+{
+
+ if (!(nopipe = x)) {
+ /*
+ * Reading from pipe
+ */
+ if (isatty(0)) {
+ return 1;
+ }
+ stdf = dup(0); /* Duplicate file descriptor of input */
+ if (no_tty)
+ return 0;
+ /*
+ * Make sure standard input is from the terminal.
+ */
+ (void)close(0);
+ if (open("/dev/tty", O_RDONLY, 0) != 0) {
+ putline("Couldn't open terminal\n");
+ flush();
+ exit(1);
+ }
+ }
+ if (no_tty)
+ return 0;
+ /*
+ * Handle signals.
+ * Catch QUIT, DELETE and ^Z
+ */
+ (void)signal(SIGQUIT, SIG_IGN);
+ (void)signal(SIGINT, catchdel);
+ ini_terminal();
+#ifdef SIGTSTP
+ if (signal(SIGTSTP, SIG_IGN) == SIG_DFL) {
+ (void)signal(SIGTSTP, suspsig);
+ }
+#endif
+ (void)signal(SIGQUIT, sigquit);
+ return 0;
+}
+
+void
+catchdel(int signo)
+{
+ (void)signo;
+ (void)signal(SIGINT, catchdel);
+ interrupt = 1;
+}
+
+static void
+sigquit(int signo)
+{
+ (void)signo;
+ quit();
+}
+
+#ifdef SIGTSTP
+
+/*
+ * We had a SIGTSTP signal.
+ * Suspend, by a call to this routine.
+ */
+
+void
+suspend()
+{
+
+ nflush();
+ resettty();
+ (void)signal(SIGTSTP, SIG_DFL);
+ (void)kill(0, SIGTSTP);
+ /*
+ * We are not here anymore ...
+ *
+
+ *
+ * But we arive here ...
+ */
+ inittty();
+ putline(TI);
+ flush();
+ (void)signal(SIGTSTP, suspsig);
+}
+
+/*
+ * SIGTSTP signal handler.
+ * Just indicate that we had one, ignore further ones and return.
+ */
+
+static void
+suspsig(int signo)
+{
+ (void)signo;
+
+ suspend();
+ if (DoneSetJmp)
+ longjmp(SetJmpBuf, 1);
+}
+#endif
+
+/*
+ * quit : called on exit.
+ * I bet you guessed that much.
+ */
+
+int
+quit()
+{
+
+ clrbline();
+ resettty();
+ flush();
+ exit(0);
+}
+
+/*
+ * Exit, but nonvoluntarily.
+ * At least tell the user why.
+ */
+
+void
+panic(char *s)
+{
+
+ putline("\a\a\a\r\n");
+ putline(s);
+ putline("\r\n");
+ quit();
+}
diff --git a/cmd/extra/yap/main.h b/cmd/extra/yap/main.h
@@ -0,0 +1,18 @@
+#ifndef MAIN_H
+#define MAIN_H
+
+extern int nopipe;
+extern char *progname;
+extern int interrupt;
+extern int no_tty;
+
+int main(int argc, char **argv);
+void catchdel(int signo);
+int quit(void);
+void panic(char *s);
+
+#ifdef SIGTSTP
+void suspend(void);
+#endif
+
+#endif
diff --git a/cmd/extra/yap/options.c b/cmd/extra/yap/options.c
@@ -0,0 +1,91 @@
+/* Copyright (c) 1985 Ceriel J.H. Jacobs */
+
+#include "in_all.h"
+#include "options.h"
+#include "output.h"
+#include "display.h"
+#include <ctype.h>
+
+int cflag;
+int uflag;
+int nflag;
+int qflag;
+char *startcomm;
+
+static int parsopt(char *s);
+
+/*
+ * Read the options. Return the argv pointer following them if there were
+ * no errors, otherwise return 0.
+ */
+
+char **
+readoptions(char **argv)
+{
+
+ char **av = argv + 1;
+ char *p;
+
+ if ((p = getenv("YAP")) != 0) {
+ (void)parsopt(p);
+ }
+ while (*av && **av == '-') {
+ if (parsopt(*av)) {
+ /*
+ * Error in option
+ */
+ putline(*av);
+ putline(": illegal option\n");
+ return (char **)0;
+ }
+ av++;
+ }
+ if (*av && **av == '+') {
+ /*
+ * Command in command line
+ */
+ startcomm = *av + 1;
+ av++;
+ }
+ return av;
+}
+
+static int
+parsopt(char *s)
+{
+ int i;
+
+ if (*s == '-')
+ s++;
+ if (isdigit(*s)) {
+ /*
+ * pagesize option
+ */
+ i = 0;
+ do {
+ i = i * 10 + *s++ - '0';
+ } while (isdigit(*s));
+ if (i < MINPAGESIZE)
+ i = MINPAGESIZE;
+ pagesize = i;
+ }
+ while (*s) {
+ switch (*s++) {
+ case 'c':
+ cflag++;
+ break;
+ case 'n':
+ nflag++;
+ break;
+ case 'u':
+ uflag++;
+ break;
+ case 'q':
+ qflag++;
+ break;
+ default:
+ return 1;
+ }
+ }
+ return 0;
+}
diff --git a/cmd/extra/yap/options.h b/cmd/extra/yap/options.h
@@ -0,0 +1,12 @@
+#ifndef OPTIONS_H
+#define OPTIONS_H
+
+extern int cflag;
+extern int uflag;
+extern int nflag;
+extern int qflag;
+extern char *startcomm;
+
+char **readoptions(char **argv);
+
+#endif
diff --git a/cmd/extra/yap/output.c b/cmd/extra/yap/output.c
@@ -0,0 +1,102 @@
+/* Copyright (c) 1985 Ceriel J.H. Jacobs */
+
+/*
+ * Handle output to screen
+ */
+
+#include "in_all.h"
+#include "output.h"
+#include "main.h"
+
+int _ocnt;
+char *_optr;
+
+#define OBUFSIZ 64 * 128
+
+static char _outbuf[OBUFSIZ];
+
+void
+flush()
+{ /* Flush output buffer, by writing it */
+ char *p = _outbuf;
+
+ _ocnt = OBUFSIZ;
+ if (_optr)
+ (void)write(1, p, _optr - p);
+ _optr = p;
+}
+
+void
+nflush()
+{ /* Flush output buffer, ignoring it */
+
+ _ocnt = OBUFSIZ;
+ _optr = _outbuf;
+}
+
+int
+fputch(int ch)
+{ /* print a character */
+ putch(ch);
+ return ch;
+}
+
+void
+putline(char *s)
+{ /* Print string s */
+
+ if (!s)
+ return;
+ while (*s) {
+ putch(*s++);
+ }
+}
+
+/*
+ * A safe version of putline. All control characters are echoed as ^X
+ */
+
+void
+cputline(char *s)
+{
+ int c;
+
+ while ((c = *s++) != 0) {
+ if ((unsigned char)c < ' ' || (unsigned char)c == 0x7f) {
+ putch('^');
+ c ^= 0x40;
+ }
+ putch(c);
+ }
+}
+
+/*
+ * Simple minded routine to print a number
+ */
+
+void
+prnum(long n)
+{
+
+ putline(getnum(n));
+}
+
+static char *
+fillnum(long n, char *p)
+{
+ if (n >= 10) {
+ p = fillnum(n / 10, p);
+ }
+ *p++ = (int)(n % 10) + '0';
+ *p = '\0';
+ return p;
+}
+
+char *
+getnum(long n)
+{
+ static char buf[20];
+
+ fillnum(n, buf);
+ return buf;
+}
diff --git a/cmd/extra/yap/output.h b/cmd/extra/yap/output.h
@@ -0,0 +1,22 @@
+#ifndef OUTPUT_H
+#define OUTPUT_H
+
+extern int _ocnt;
+extern char *_optr;
+
+#define putch(ch) \
+ do { \
+ if (--_ocnt <= 0) \
+ flush(); \
+ *_optr++ = (ch); \
+ } while (0)
+
+void flush(void);
+void nflush(void);
+int fputch(int c);
+void putline(char *s);
+void cputline(char *s);
+void prnum(long n);
+char *getnum(long n);
+
+#endif
diff --git a/cmd/extra/yap/pattern.c b/cmd/extra/yap/pattern.c
@@ -0,0 +1,34 @@
+#include "in_all.h"
+#include "pattern.h"
+#include <regex.h>
+
+/*
+ * Interface to POSIX regular expression routines.
+ */
+
+static regex_t pattern;
+static int pattern_valid;
+
+char *
+re_comp(char *s)
+{
+
+ if (!*s) {
+ return (char *)0;
+ }
+ if (pattern_valid) {
+ regfree(&pattern);
+ pattern_valid = 0;
+ }
+ if (regcomp(&pattern, s, 0) == 0) {
+ pattern_valid = 1;
+ return (char *)0;
+ }
+ return "Error in pattern";
+}
+
+int
+re_exec(char *s)
+{
+ return pattern_valid && regexec(&pattern, s, 0, (regmatch_t *)0, 0) == 0;
+}
diff --git a/cmd/extra/yap/pattern.h b/cmd/extra/yap/pattern.h
@@ -0,0 +1,7 @@
+#ifndef PATTERN_H
+#define PATTERN_H
+
+char *re_comp(char *s);
+int re_exec(char *s);
+
+#endif
diff --git a/cmd/extra/yap/process.c b/cmd/extra/yap/process.c
@@ -0,0 +1,115 @@
+/* Copyright (c) 1985 Ceriel J.H. Jacobs */
+
+#include "in_all.h"
+#include "process.h"
+#include "commands.h"
+#include "display.h"
+#include "prompt.h"
+#include "getline.h"
+#include "getcomm.h"
+#include "main.h"
+#include "options.h"
+#include "output.h"
+
+jmp_buf SetJmpBuf;
+int DoneSetJmp;
+int stdf;
+int filecount;
+char **filenames;
+char *currentfile;
+long maxpos;
+
+static int nfiles; /* Number of filenames on command line */
+
+/*
+ * Visit a file, file name is "fn".
+ */
+
+void
+visitfile(char *fn)
+{
+ struct stat statbuf;
+
+ if (stdf > 0) {
+ /*
+ * Close old input file
+ */
+ (void)close(stdf);
+ }
+ currentfile = fn;
+ if ((stdf = open(fn, O_RDONLY, 0)) < 0) {
+ error(": could not open");
+ maxpos = 0;
+ } else { /* Get size for percentage in prompt */
+ (void)fstat(stdf, &statbuf);
+ maxpos = statbuf.st_size;
+ }
+ do_clean();
+ d_clean();
+}
+
+/*
+ * process the input files, one by one.
+ * If there is none, input is from a pipe.
+ */
+
+void
+processfiles(int n, char **argv)
+{
+
+ static char *dummies[3];
+ long arg;
+
+ if (!(nfiles = n)) {
+ /*
+ * Input from pipe
+ */
+ currentfile = "standard-input";
+ /*
+ * Take care that *(filenames - 1) and *(filenames + 1) are 0
+ */
+ filenames = &dummies[1];
+ d_clean();
+ do_clean();
+ } else {
+ filenames = argv;
+ (void)nextfile(0);
+ }
+ *--argv = 0;
+ if (startcomm) {
+ n = getcomm(&arg);
+ if (commands[n].c_flags & NEEDS_SCREEN) {
+ redraw(0);
+ }
+ do_comm(n, arg);
+ startcomm = 0;
+ }
+ redraw(1);
+ if (setjmp(SetJmpBuf)) {
+ nflush();
+ redraw(1);
+ }
+ DoneSetJmp = 1;
+ for (;;) {
+ interrupt = 0;
+ n = getcomm(&arg);
+ do_comm(n, arg);
+ }
+}
+
+/*
+ * Get the next file the user asks for.
+ */
+
+int
+nextfile(int n)
+{
+ int i;
+
+ if ((i = filecount + n) >= nfiles || i < 0) {
+ return 1;
+ }
+ filecount = i;
+ visitfile(filenames[i]);
+ return 0;
+}
diff --git a/cmd/extra/yap/process.h b/cmd/extra/yap/process.h
@@ -0,0 +1,19 @@
+#ifndef PROCESS_H
+#define PROCESS_H
+
+#include <setjmp.h>
+
+extern jmp_buf SetJmpBuf;
+extern int DoneSetJmp;
+
+extern int stdf;
+extern int filecount;
+extern char **filenames;
+extern char *currentfile;
+extern long maxpos;
+
+void visitfile(char *fn);
+void processfiles(int n, char **argv);
+int nextfile(int n);
+
+#endif
diff --git a/cmd/extra/yap/prompt.c b/cmd/extra/yap/prompt.c
@@ -0,0 +1,174 @@
+/* Copyright (c) 1985 Ceriel J.H. Jacobs */
+
+#include "in_all.h"
+#include "prompt.h"
+#include "term.h"
+#include "output.h"
+#include "options.h"
+#include "display.h"
+#include "process.h"
+#include "getline.h"
+#include "main.h"
+#include "getcomm.h"
+#include "machine.h"
+#include "keys.h"
+#include "assert.h"
+#include "commands.h"
+
+static char *errorgiven; /* Set to error message, if there is one */
+
+static char *display_basename(char *path, char *scratch, size_t scratch_size);
+
+char *
+copy(char *p, const char *ep, char *s)
+{
+ while (p < ep && *s) {
+ *p++ = *s++;
+ }
+ return p;
+}
+
+/*
+ * display the prompt and refresh the screen.
+ */
+
+void
+give_prompt()
+{
+
+ char **name;
+ struct scr_info *p = &scr_info;
+ char buf[256];
+ char filebuf[256];
+ char *pb = buf;
+
+ if (startcomm)
+ return;
+ flush();
+ if (window()) {
+ redraw(0);
+ flush();
+ }
+ if (!stupid) {
+ /*
+ * fancy prompt
+ */
+ clrbline();
+ standout();
+ pb = copy(pb, &buf[255],
+ display_basename(currentfile, filebuf, sizeof(filebuf)));
+ if (stdf >= 0) {
+ pb = copy(pb, &buf[255], ", ");
+ pb = copy(pb, &buf[255], getnum(p->firstline));
+ pb = copy(pb, &buf[255], "-");
+ pb = copy(pb, &buf[255], getnum(p->lastline));
+ }
+ } else {
+ *pb++ = '\007'; /* Stupid terminal, stupid prompt */
+ }
+ if (errorgiven) {
+ /*
+ * display error message
+ */
+ pb = copy(pb, &buf[255], " ");
+ pb = copy(pb, &buf[255], errorgiven);
+ if (stupid) {
+ pb = copy(pb, &buf[255], "\r\n");
+ }
+ errorgiven = 0;
+ } else if (!stupid && (status || maxpos)) {
+ pb = copy(pb, &buf[255], " (");
+ name = &filenames[filecount];
+ if (status) {
+ /*
+ * indicate top and/or bottom
+ */
+ if (status & START) {
+ if (!*(name - 1)) {
+ pb = copy(pb, &buf[255], "Top");
+ } else {
+ pb = copy(pb, &buf[255], "Previous: ");
+ pb = copy(pb, &buf[255], display_basename(*(name - 1),
+ filebuf,
+ sizeof(filebuf)));
+ }
+ if (status & EOFILE) {
+ pb = copy(pb, &buf[255], ", ");
+ }
+ }
+ if (status & EOFILE) {
+ if (!*(name + 1)) {
+ pb = copy(pb, &buf[255], "Bottom");
+ } else {
+ pb = copy(pb, &buf[255], "Next: ");
+ pb = copy(pb, &buf[255], display_basename(*(name + 1),
+ filebuf,
+ sizeof(filebuf)));
+ }
+ }
+ } else { /* display percentage */
+ pb = copy(pb, &buf[255], getnum((100 * getpos(p->lastline)) / maxpos));
+ pb = copy(pb, &buf[255], "%");
+ }
+ pb = copy(pb, &buf[255], ")");
+ }
+ *pb = '\0';
+ if (!stupid) {
+ buf[COLS - 1] = 0;
+ putline(buf);
+ standend();
+ } else
+ putline(buf);
+}
+
+static char *
+display_basename(char *path, char *scratch, size_t scratch_size)
+{
+ char *base;
+
+ if (!path || scratch_size == 0) {
+ return "";
+ }
+ (void)strncpy(scratch, path, scratch_size - 1);
+ scratch[scratch_size - 1] = '\0';
+ base = basename(scratch);
+ return base ? base : scratch;
+}
+
+/*
+ * Remember error message
+ */
+
+void
+error(char *str)
+{
+
+ errorgiven = str;
+}
+
+void
+ret_to_continue()
+{ /* Obvious */
+ int c;
+ static char buf[2];
+
+ for (;;) {
+ clrbline();
+ standout();
+ if (errorgiven) {
+ putline(errorgiven);
+ putline(" ");
+ errorgiven = 0;
+ }
+ putline("[Type anything to continue]");
+ standend();
+ if (is_escape(c = getch())) {
+ buf[0] = c;
+ (void)match(buf, &c, currmap->k_mach);
+ assert(c > 0);
+ do_comm(c, -1L);
+ } else
+ break;
+ }
+ clrbline();
+}
diff --git a/cmd/extra/yap/prompt.h b/cmd/extra/yap/prompt.h
@@ -0,0 +1,8 @@
+#ifndef PROMPT_H
+#define PROMPT_H
+
+void give_prompt(void);
+void error(char *s);
+void ret_to_continue(void);
+
+#endif
diff --git a/cmd/extra/yap/term.c b/cmd/extra/yap/term.c
@@ -0,0 +1,459 @@
+/* Copyright (c) 1985 Ceriel J.H. Jacobs */
+
+/*
+ * Terminal handling routines, mostly initializing.
+ */
+
+#include "in_all.h"
+#include "term.h"
+#include "machine.h"
+#include "output.h"
+#include "display.h"
+#include "options.h"
+#include "getline.h"
+#include "keys.h"
+#include "main.h"
+#define getline yap_getline
+#define getch yap_getch
+#include <term.h>
+#undef getch
+#undef getline
+#undef insert_line
+
+int expandtabs;
+int stupid;
+int hardcopy;
+char *CE, *CL, *SO, *SE, *US, *UE, *UC, *MD, *ME, *TI, *TE, *CM, *TA, *SR, *AL;
+char *UP, *HO, *BO;
+int LINES, COLS, AM, XN, DB;
+int erasech, killch;
+struct state *sppat;
+char *BC;
+
+#ifdef TIOCGWINSZ
+static struct winsize w;
+#endif
+
+#ifdef TIOCSPGRP
+static int proc_id, saved_pgrpid;
+#endif
+
+static char *ll;
+
+struct linelist _X[100]; /* 100 is enough ? */
+
+static struct termios _tty, _svtty;
+
+static void
+handle(cc_t *c)
+{ /* if character *c is used, set it to undefined */
+
+ if (isused(*c))
+ *c = 0xff;
+}
+
+/*
+ * Set terminal in cbreak mode.
+ * Also check if tabs need expanding.
+ */
+
+void
+inittty()
+{
+ struct termios *p = &_tty;
+
+ tcgetattr(0, p);
+ _svtty = *p;
+ p->c_oflag &= ~OPOST;
+ p->c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | ICANON);
+ if (isused('S' & 0x1f) || isused('Q' & 0x1f))
+ p->c_iflag &= ~IXON;
+ handle(&p->c_cc[VINTR]);
+ handle(&p->c_cc[VQUIT]);
+ erasech = p->c_cc[VERASE];
+ killch = p->c_cc[VKILL];
+ p->c_cc[VMIN] = 1; /* Just wait for one character */
+ p->c_cc[VTIME] = 0;
+ tcsetattr(0, TCSANOW, p);
+}
+
+void
+backspace()
+{
+ putline(BC);
+}
+
+void
+clrscreen()
+{
+ tputs(CL, LINES, fputch);
+}
+
+void
+clrtoeol()
+{
+ tputs(CE, 1, fputch);
+}
+
+void
+scrollreverse()
+{
+ tputs(SR, LINES, fputch);
+}
+
+void
+standout()
+{
+ tputs(SO, 1, fputch);
+}
+
+void
+standend()
+{
+ tputs(SE, 1, fputch);
+}
+
+void
+underline()
+{
+ tputs(US, 1, fputch);
+}
+
+void
+end_underline()
+{
+ tputs(UE, 1, fputch);
+}
+
+void
+bold()
+{
+ tputs(MD, 1, fputch);
+}
+
+void
+end_bold()
+{
+ tputs(ME, 1, fputch);
+}
+
+void
+underchar()
+{
+ tputs(UC, 1, fputch);
+}
+
+void
+givetab()
+{
+ tputs(TA, 1, fputch);
+}
+
+/*
+ * Reset the terminal to its original state
+ */
+
+void
+resettty()
+{
+ tcsetattr(0, TCSANOW, &_svtty);
+ putline(TE);
+ flush();
+}
+
+/*
+ * Get string terminal capability "cap".
+ * If not present, return an empty string.
+ */
+
+static char *
+getcap(char *cap)
+{
+ char *s;
+
+ s = tigetstr(cap);
+ if (!s || s == (char *)-1)
+ return "";
+ return s;
+}
+
+static int
+getflag(char *cap)
+{
+ int value;
+
+ value = tigetflag(cap);
+ return value < 0 ? 0 : value;
+}
+
+static int
+getnumcap(char *cap)
+{
+ int value;
+
+ value = tigetnum(cap);
+ return value < 0 ? -1 : value;
+}
+
+/*
+ * Initialize some terminal-dependent stuff.
+ */
+
+void
+ini_terminal()
+{
+
+ char *s;
+ struct linelist *lp, *lp1;
+ int i;
+ int UG, SG;
+ char tempbuf[20];
+ char *mb, *mh, *mr; /* attributes */
+ int err;
+
+ initkeys();
+#ifdef TIOCSPGRP
+ proc_id = getpid();
+ ioctl(0, TIOCGPGRP, (char *)&saved_pgrpid);
+#endif
+ inittty();
+ stupid = 1;
+ BC = "\b";
+ TA = "\t";
+ if (!(s = getenv("TERM")))
+ s = "dumb";
+ if (setupterm(s, 1, &err) != 0) {
+ panic("No terminfo entry");
+ }
+ stupid = 0;
+ hardcopy = getflag("hc"); /* Hard copy terminal?*/
+ if (*(s = getcap("cub1"))) {
+ /*
+ * Backspace if not ^H
+ */
+ BC = s;
+ }
+ UP = getcap("cuu1"); /* move up a line */
+ CE = getcap("el"); /* clear to end of line */
+ CL = getcap("clear"); /* clear screen */
+ if (!*CL)
+ cflag = 1;
+ TI = getcap("smcup"); /* Initialization for CM */
+ TE = getcap("rmcup"); /* end for CM */
+ CM = getcap("cup"); /* cursor addressing */
+ SR = getcap("ri"); /* scroll reverse */
+ AL = getcap("il"); /* Insert line */
+ SO = getcap("smso"); /* standout */
+ SE = getcap("rmso"); /* standend */
+ SG = getnumcap("xmc"); /* blanks left by attributes */
+ if (SG < 0)
+ SG = 0;
+ US = getcap("smul"); /* underline */
+ UE = getcap("rmul"); /* end underline */
+ UG = getnumcap("xmc"); /* blanks left by attributes */
+ if (UG < 0)
+ UG = 0;
+ UC = getcap("uc"); /* underline a character */
+ mb = getcap("blink"); /* blinking attribute */
+ MD = getcap("bold"); /* bold attribute */
+ ME = getcap("sgr0"); /* turn off attributes */
+ mh = getcap("dim"); /* half bright attribute */
+ mr = getcap("rev"); /* reversed video attribute */
+ if (!nflag) {
+ /*
+ * Recognize special strings
+ */
+ (void)addstring(SO, SG, &sppat);
+ (void)addstring(SE, SG, &sppat);
+ (void)addstring(US, UG, &sppat);
+ (void)addstring(UE, UG, &sppat);
+ (void)addstring(mb, 0, &sppat);
+ (void)addstring(MD, 0, &sppat);
+ (void)addstring(ME, 0, &sppat);
+ (void)addstring(mh, 0, &sppat);
+ (void)addstring(mr, 0, &sppat);
+ if (*UC) {
+ (void)strcpy(tempbuf, BC);
+ (void)strcat(tempbuf, UC);
+ (void)addstring(tempbuf, 0, &sppat);
+ }
+ }
+ if (UG > 0 || uflag) {
+ US = "";
+ UE = "";
+ }
+ if (*US || uflag)
+ UC = "";
+ COLS = getnumcap("cols"); /* columns on page */
+ i = getnumcap("lines"); /* Lines on page */
+ AM = getflag("am"); /* terminal wraps automatically? */
+ XN = getflag("xenl"); /* and then ignores next newline? */
+ DB = getflag("db"); /* terminal retains lines below */
+ HO = getcap("home");
+ if (!*HO && *CM) {
+ HO = tiparm(CM, 0, 0); /* Another way of getting home */
+ }
+ if ((!*CE && !*AL) || !*HO || hardcopy) {
+ cflag = stupid = 1;
+ }
+ if (*(s = getcap("ht"))) {
+ /*
+ * Tab (other than ^I or padding)
+ */
+ TA = s;
+ }
+ if (!*(ll = getcap("ll")) && *CM && i > 0) {
+ /*
+ * Lower left hand corner
+ */
+ BO = tiparm(CM, i - 1, 0);
+ } else
+ BO = ll;
+ if (COLS <= 0 || COLS > 256) {
+ if ((unsigned)COLS >= 65409) {
+ /* SUN bug */
+ COLS &= 0xffff;
+ COLS -= (65409 - 128);
+ }
+ if (COLS <= 0 || COLS > 256)
+ COLS = 80;
+ }
+ if (i <= 0) {
+ i = 24;
+ cflag = stupid = 1;
+ }
+ LINES = i;
+ maxpagesize = i - 1;
+ scrollsize = maxpagesize / 2;
+ if (scrollsize <= 0)
+ scrollsize = 1;
+ if (!pagesize || pagesize >= i) {
+ pagesize = maxpagesize;
+ }
+
+ /*
+ * The next part does not really belong here, but there it is ...
+ * Initialize a circular list for the screenlines.
+ */
+
+ scr_info.tail = lp = _X;
+ lp1 = lp + (100 - 1);
+ for (; lp <= lp1; lp++) {
+ /*
+ * Circular doubly linked list
+ */
+ lp->next = lp + 1;
+ lp->prev = lp - 1;
+ }
+ lp1->next = scr_info.tail;
+ lp1->next->prev = lp1;
+ if (stupid) {
+ BO = "\r\n";
+ }
+ putline(TI);
+ window();
+}
+
+/*
+ * Place cursor at start of line n.
+ */
+
+void
+mgoto(int n)
+{
+
+ if (n == 0)
+ home();
+ else if (n == maxpagesize && *BO)
+ bottom();
+ else if (*CM) {
+ /*
+ * Cursor addressing
+ */
+ tputs(tiparm(CM, n, 0), 1, fputch);
+ } else if (*BO && *UP && n >= (maxpagesize >> 1)) {
+ /*
+ * Bottom and then up
+ */
+ bottom();
+ while (n++ < maxpagesize)
+ putline(UP);
+ } else { /* Home, and then down */
+ home();
+ while (n--)
+ putline("\r\n");
+ }
+}
+
+/*
+ * Clear bottom line
+ */
+
+void
+clrbline()
+{
+
+ if (stupid) {
+ putline("\r\n");
+ return;
+ }
+ bottom();
+ if (*CE) {
+ /*
+ * We can clear to end of line
+ */
+ clrtoeol();
+ return;
+ }
+ ins_line(maxpagesize);
+}
+
+void
+ins_line(int l)
+{
+ tputs(tiparm(AL, l), maxpagesize - l, fputch);
+}
+
+void
+home()
+{
+
+ tputs(HO, 1, fputch);
+}
+
+void
+bottom()
+{
+
+ tputs(BO, 1, fputch);
+ if (!*BO)
+ mgoto(maxpagesize);
+}
+
+int
+window()
+{
+#ifdef TIOCGWINSZ
+ if (ioctl(1, TIOCGWINSZ, &w) < 0)
+ return 0;
+
+ if (w.ws_col == 0)
+ w.ws_col = COLS;
+ if (w.ws_row == 0)
+ w.ws_row = LINES;
+ if (w.ws_col != COLS || w.ws_row != LINES) {
+ COLS = w.ws_col;
+ LINES = w.ws_row;
+ maxpagesize = LINES - 1;
+ pagesize = maxpagesize;
+ if (!*ll)
+ BO = tiparm(CM, maxpagesize, 0);
+ scr_info.currentpos = 0;
+ scrollsize = maxpagesize / 2;
+ if (scrollsize <= 0)
+ scrollsize = 1;
+ return 1;
+ }
+#endif
+ return 0;
+}
diff --git a/cmd/extra/yap/term.h b/cmd/extra/yap/term.h
@@ -0,0 +1,61 @@
+#ifndef TERM_H
+#define TERM_H
+
+extern int expandtabs;
+extern int stupid;
+extern int hardcopy;
+
+extern char *CE;
+extern char *CL;
+extern char *SO;
+extern char *SE;
+extern char *US;
+extern char *UE;
+extern char *UC;
+extern char *MD;
+extern char *ME;
+extern char *TI;
+extern char *TE;
+extern char *CM;
+extern char *TA;
+extern char *SR;
+extern char *AL;
+extern char *UP;
+extern char *HO;
+extern char *BO;
+
+extern int LINES;
+extern int COLS;
+extern int AM;
+extern int XN;
+extern int DB;
+
+extern int erasech;
+extern int killch;
+extern struct state *sppat;
+extern char *BC;
+
+void inittty(void);
+void resettty(void);
+void ini_terminal(void);
+void backspace(void);
+void clrscreen(void);
+void clrtoeol(void);
+void scrollreverse(void);
+void standout(void);
+void standend(void);
+void underline(void);
+void end_underline(void);
+void bold(void);
+void end_bold(void);
+void underchar(void);
+void givetab(void);
+void mgoto(int n);
+void clrbline(void);
+void home(void);
+void bottom(void);
+int window(void);
+void ins_line(int l);
+#define insert_line(l) ins_line(l)
+
+#endif
diff --git a/scripts/mkbox b/scripts/mkbox
@@ -116,6 +116,7 @@ compile_make() {
compile_diff() {
ddir="${ROOT}/cmd/posix/diff"
+ [ -d "${ddir}" ] || return 0
symf="${BDIR}/diff.syms"
diff_objs=""
@@ -146,6 +147,7 @@ compile_diff() {
compile_patch() {
pdir="${ROOT}/cmd/posix/patch"
+ [ -d "${pdir}" ] || return 0
symf="${BDIR}/patch.syms"
patch_objs=""
@@ -175,6 +177,7 @@ compile_patch() {
compile_diff3() {
ddir="${ROOT}/cmd/extra/diff3"
+ [ -d "${ddir}" ] || return 0
pdir="${ROOT}/cmd/posix/diff"
symf="${BDIR}/diff3.syms"
diff3_objs=""
@@ -207,6 +210,40 @@ compile_diff3() {
printf '\t{ "diff3", diff3_main, "extra" },\n' >> "${BDIR}/entries_extra.inc"
}
+compile_yap() {
+ ydir="${ROOT}/cmd/extra/yap"
+ symf="${BDIR}/yap.syms"
+ yap_objs=""
+
+ [ -d "${ydir}" ] || return 0
+ printf 'mkbox: cc yap (multi)\n'
+ for src in "${ydir}"/*.c; do
+ [ -f "${src}" ] || continue
+ s=${src##*/}
+ s=${s%.c}
+ out="${BDIR}/yap_${s}.o"
+ eval "${CC} ${CPPFLAGS} ${CFLAGS} -c \"\$src\" -o \"\$out\"" ||
+ die "compile failed: yap/${s}.c"
+ yap_objs="${yap_objs} ${out}"
+ done
+
+ combined="${BDIR}/yap_combined.o"
+ ${LD:-ld} -r -o "${combined}" ${yap_objs} ||
+ die "ld -r failed: yap"
+
+ printf 'main yap_main\n' > "${symf}"
+ ${OBJCOPY} --redefine-syms="${symf}" "${combined}" ||
+ die "objcopy failed: yap"
+ ${OBJCOPY} --keep-global-symbol=yap_main "${combined}" ||
+ die "objcopy localize failed: yap"
+ rm -f "${symf}" ${yap_objs}
+
+ OBJS="${OBJS} ${combined}"
+ LDLIBS="${LDLIBS} -ltinfo"
+ printf 'int yap_main(int, char **);\n' >> "${DECL_F}"
+ printf '\t{ "yap", yap_main, "extra" },\n' >> "${BDIR}/entries_extra.inc"
+}
+
compile_awk() {
adir="${ROOT}/cmd/posix/awk"
symf="${BDIR}/awk.syms"
@@ -283,7 +320,7 @@ compile_sh() {
(cd "${sdir}" && sh mktokens) ||
die "mktokens failed"
- for s in alias arith_yacc arith_yylex cd echo error eval exec \
+ for s in alias arith_yacc arith_yylex cd easter echo error eval exec \
expand lineedit input jobs kill mail main memalloc \
miscbltin mystring options output parser printf redir \
show test trap var builtins nodes syntax; do
@@ -328,6 +365,8 @@ process_dir() {
done
}
+[ -f "${ROOT}/cmd/posix/diff.c" ] && LDLIBS="${LDLIBS} -lm"
+
process_dir "${ROOT}/cmd/posix" "posix"
compile_diff
compile_patch
@@ -340,6 +379,7 @@ process_dir "${ROOT}/cmd/pseudo" "pseudo"
process_dir "${ROOT}/cmd/xsi" "xsi"
process_dir "${ROOT}/cmd/extra" "extra"
compile_diff3
+compile_yap
DISP="${BDIR}/aruu-box.c"
printf 'mkbox: generating dispatch\n'