converted man page
[unix-history] / usr / src / usr.bin / more / command.c
index 6454dba..c91af04 100644 (file)
@@ -3,32 +3,18 @@
  * Copyright (c) 1988 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1988 Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by Mark Nudleman and the University of California, Berkeley.  The
- * name of Mark Nudleman or the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * %sccs.include.redist.c%
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)command.c  5.10 (Berkeley) %G%";
+static char sccsid[] = "@(#)command.c  5.20 (Berkeley) %G%";
 #endif /* not lint */
 
 #endif /* not lint */
 
-/*
- * User-level command processor.
- */
-
 #include <sys/param.h>
 #include <stdio.h>
 #include <ctype.h>
 #include <less.h>
 #include <sys/param.h>
 #include <stdio.h>
 #include <ctype.h>
 #include <less.h>
+#include "pathnames.h"
 
 #define        NO_MCA          0
 #define        MCA_DONE        1
 
 #define        NO_MCA          0
 #define        MCA_DONE        1
@@ -57,42 +43,32 @@ static int last_mca;                /* The previous mca */
 static int number;             /* The number typed by the user */
 static int wsearch;            /* Search for matches (1) or non-matches (0) */
 
 static int number;             /* The number typed by the user */
 static int wsearch;            /* Search for matches (1) or non-matches (0) */
 
-/*
- * Reset command buffer (to empty).
- */
-cmd_reset()
-{
-       cp = cmdbuf;
-}
+#define        CMD_RESET       cp = cmdbuf     /* reset command buffer to empty */
+#define        CMD_EXEC        lower_left(); flush()
 
 
-/*
- * Backspace in command buffer.
- */
+/* backspace in command buffer. */
 static
 cmd_erase()
 {
 static
 cmd_erase()
 {
+       /*
+        * backspace past beginning of the string: this usually means
+        * abort the command.
+        */
        if (cp == cmdbuf)
        if (cp == cmdbuf)
-               /*
-                * Backspace past beginning of the string:
-                * this usually means abort the command.
-                */
                return(1);
 
                return(1);
 
+       /* erase an extra character, for the carat. */
        if (CONTROL_CHAR(*--cp)) {
        if (CONTROL_CHAR(*--cp)) {
-               /*
-                * Erase an extra character, for the carat.
-                */
                backspace();
                --cmd_col;
        }
                backspace();
                --cmd_col;
        }
+
        backspace();
        --cmd_col;
        return(0);
 }
 
        backspace();
        --cmd_col;
        return(0);
 }
 
-/*
- * Set up the display to start a new multi-character command.
- */
+/* set up the display to start a new multi-character command. */
 start_mca(action, prompt)
        int action;
        char *prompt;
 start_mca(action, prompt)
        int action;
        char *prompt;
@@ -105,7 +81,7 @@ start_mca(action, prompt)
 }
 
 /*
 }
 
 /*
- * Process a single character of a multi-character command, such as
+ * process a single character of a multi-character command, such as
  * a number, or the pattern of a search command.
  */
 static
  * a number, or the pattern of a search command.
  */
 static
@@ -147,33 +123,10 @@ cmd_char(c)
        return(0);
 }
 
        return(0);
 }
 
-/*
- * Return the number currently in the command buffer.
- */
-       static int
-cmd_int()
-{
-       *cp = '\0';
-       cp = cmdbuf;
-       return (atoi(cmdbuf));
-}
-
-/*
- * Move the cursor to lower left before executing a command.
- * This looks nicer if the command takes a long time before
- * updating the screen.
- */
-static
-cmd_exec()
-{
-       lower_left();
-       flush();
-}
-
 prompt()
 {
 prompt()
 {
-       extern int terseprompt, linenums;
-       extern char *current_name, *firstsearch;
+       extern int linenums, short_file;
+       extern char *current_name, *firstsearch, *next_name;
        off_t len, pos, ch_length(), position(), forw_line();
        char pbuf[40];
 
        off_t len, pos, ch_length(), position(), forw_line();
        char pbuf[40];
 
@@ -191,7 +144,7 @@ prompt()
                repaint();
 
        /* if no -e flag and we've hit EOF on the last file, quit. */
                repaint();
 
        /* if no -e flag and we've hit EOF on the last file, quit. */
-       if (!quit_at_eof && hit_eof && curr_ac + 1 >= ac)
+       if ((!quit_at_eof || short_file) && hit_eof && curr_ac + 1 >= ac)
                quit();
 
        /* select the proper prompt and display it. */
                quit();
 
        /* select the proper prompt and display it. */
@@ -206,30 +159,36 @@ prompt()
                        putstr(pbuf);
                }
                if (linenums) {
                        putstr(pbuf);
                }
                if (linenums) {
-                       (void)sprintf(pbuf, " line %d", currline(TOP));
+                       (void)sprintf(pbuf, " line %d", currline(BOTTOM));
                        putstr(pbuf);
                }
                        putstr(pbuf);
                }
-               if ((pos = position(TOP)) != NULL_POSITION) {
+               if ((pos = position(BOTTOM)) != NULL_POSITION) {
                        (void)sprintf(pbuf, " byte %ld", pos);
                        putstr(pbuf);
                        if (!ispipe && (len = ch_length())) {
                        (void)sprintf(pbuf, " byte %ld", pos);
                        putstr(pbuf);
                        if (!ispipe && (len = ch_length())) {
-                               (void)sprintf(pbuf, " pct %ld%%",
-                                   ((100 * pos) / len));
+                               (void)sprintf(pbuf, "/%ld pct %ld%%",
+                                   len, ((100 * pos) / len));
                                putstr(pbuf);
                        }
                }
                so_exit();
                longprompt = 0;
        }
                                putstr(pbuf);
                        }
                }
                so_exit();
                longprompt = 0;
        }
-       else if (terseprompt)
-               putchr(':');
        else {
                so_enter();
                putstr(current_name);
                if (hit_eof)
        else {
                so_enter();
                putstr(current_name);
                if (hit_eof)
-                       putstr(": END");
-               else if (linenums) {
-                       (void)sprintf(pbuf, ": %d", currline(TOP));
+                       if (next_name) {
+                               putstr(": END (next file: ");
+                               putstr(next_name);
+                               putstr(")");
+                       }
+                       else
+                               putstr(": END");
+               else if (!ispipe &&
+                   (pos = position(BOTTOM)) != NULL_POSITION &&
+                   (len = ch_length())) {
+                       (void)sprintf(pbuf, " (%ld%%)", ((100 * pos) / len));
                        putstr(pbuf);
                }
                so_exit();
                        putstr(pbuf);
                }
                so_exit();
@@ -237,12 +196,19 @@ prompt()
        return(1);
 }
 
        return(1);
 }
 
-/*
- * Get command character.
- */
+/* get command character. */
 static
 getcc()
 {
 static
 getcc()
 {
+       extern int cmdstack;
+       int ch;
+
+       /* left over from error() routine. */
+       if (cmdstack) {
+               ch = cmdstack;
+               cmdstack = NULL;
+               return(ch);
+       }
        if (cp > cmdbuf && position(TOP) == NULL_POSITION) {
                /*
                 * Command is incomplete, so try to complete it.
        if (cp > cmdbuf && position(TOP) == NULL_POSITION) {
                /*
                 * Command is incomplete, so try to complete it.
@@ -261,9 +227,7 @@ getcc()
        return(getchr());
 }
 
        return(getchr());
 }
 
-/*
- * Execute a multicharacter command.
- */
+/* execute a multicharacter command. */
 static
 exec_mca()
 {
 static
 exec_mca()
 {
@@ -273,7 +237,7 @@ exec_mca()
        char *glob();
 
        *cp = '\0';
        char *glob();
 
        *cp = '\0';
-       cmd_exec();
+       CMD_EXEC;
        switch (mca) {
        case A_F_SEARCH:
                (void)search(1, cmdbuf, number, wsearch);
        switch (mca) {
        case A_F_SEARCH:
                (void)search(1, cmdbuf, number, wsearch);
@@ -296,9 +260,7 @@ exec_mca()
        }
 }
 
        }
 }
 
-/*
- * Add a character to a multi-character command.
- */
+/* add a character to a multi-character command. */
 static
 mca_char(c)
        int c;
 static
 mca_char(c)
        int c;
@@ -318,7 +280,9 @@ mca_char(c)
                         * Not part of the number.
                         * Treat as a normal command character.
                         */
                         * Not part of the number.
                         * Treat as a normal command character.
                         */
-                       number = cmd_int();
+                       *cp = '\0';
+                       number = atoi(cmdbuf);
+                       CMD_RESET;
                        mca = 0;
                        return(NO_MCA);
                }
                        mca = 0;
                        return(NO_MCA);
                }
@@ -330,23 +294,14 @@ mca_char(c)
         * is terminated by a newline.
         */
        if (c == '\n' || c == '\r') {
         * is terminated by a newline.
         */
        if (c == '\n' || c == '\r') {
-               /*
-                * Execute the command.
-                */
                exec_mca();
                return(MCA_DONE);
        }
                exec_mca();
                return(MCA_DONE);
        }
-       /*
-        * Append the char to the command buffer.
-        */
+
+       /* append the char to the command buffer. */
        if (cmd_char(c))
        if (cmd_char(c))
-               /*
-                * Abort the multi-char command.
-                */
                return(MCA_DONE);
                return(MCA_DONE);
-       /*
-        * Need another character.
-        */
+
        return(MCA_MORE);
 }
 
        return(MCA_MORE);
 }
 
@@ -377,7 +332,7 @@ commands()
                /*
                 * Display prompt and accept a character.
                 */
                /*
                 * Display prompt and accept a character.
                 */
-               cmd_reset();
+               CMD_RESET;
                if (!prompt()) {
                        next_file(1);
                        continue;
                if (!prompt()) {
                        next_file(1);
                        continue;
@@ -415,149 +370,81 @@ again:           if (sigs)
                                break;
                        }
 
                                break;
                        }
 
-               /*
-                * Decode the command character and decide what to do.
-                */
-               switch (action = cmd_decode(c))
-               {
-               case A_DIGIT:
-                       /*
-                        * First digit of a number.
-                        */
+               /* decode the command character and decide what to do. */
+               switch (action = cmd_decode(c)) {
+               case A_DIGIT:           /* first digit of a number */
                        start_mca(A_DIGIT, ":");
                        goto again;
                        start_mca(A_DIGIT, ":");
                        goto again;
-
-               case A_F_SCREEN:
-                       /*
-                        * Forward one screen.
-                        */
-                       if (number <= 0)
-                               number = sc_window;
-                       if (number <= 0)
+               case A_F_SCREEN:        /* forward one screen */
+                       CMD_EXEC;
+                       if (number <= 0 && (number = sc_window) <= 0)
                                number = sc_height - 1;
                                number = sc_height - 1;
-                       cmd_exec();
                        forward(number, 1);
                        break;
                        forward(number, 1);
                        break;
-
-               case A_B_SCREEN:
-                       /*
-                        * Backward one screen.
-                        */
-                       if (number <= 0)
-                               number = sc_window;
-                       if (number <= 0)
+               case A_B_SCREEN:        /* backward one screen */
+                       CMD_EXEC;
+                       if (number <= 0 && (number = sc_window) <= 0)
                                number = sc_height - 1;
                                number = sc_height - 1;
-                       cmd_exec();
                        backward(number, 1);
                        break;
                        backward(number, 1);
                        break;
-
-               case A_F_LINE:
-                       /*
-                        * Forward N (default 1) line.
-                        */
-                       if (number <= 0)
-                               number = 1;
-                       cmd_exec();
-                       forward(number, 0);
+               case A_F_LINE:          /* forward N (default 1) line */
+                       CMD_EXEC;
+                       forward(number <= 0 ? 1 : number, 0);
                        break;
                        break;
-
-               case A_B_LINE:
-                       /*
-                        * Backward N (default 1) line.
-                        */
-                       if (number <= 0)
-                               number = 1;
-                       cmd_exec();
-                       backward(number, 0);
+               case A_B_LINE:          /* backward N (default 1) line */
+                       CMD_EXEC;
+                       backward(number <= 0 ? 1 : number, 0);
                        break;
                        break;
-
-               case A_F_SCROLL:
-                       /*
-                        * Forward N lines 
-                        * (default same as last 'd' or 'u' command).
-                        */
+               case A_F_SCROLL:        /* forward N lines */
+                       CMD_EXEC;
                        if (number > 0)
                                scroll = number;
                        if (number > 0)
                                scroll = number;
-                       cmd_exec();
                        forward(scroll, 0);
                        break;
                        forward(scroll, 0);
                        break;
-
-               case A_B_SCROLL:
-                       /*
-                        * Forward N lines 
-                        * (default same as last 'd' or 'u' command).
-                        */
+               case A_B_SCROLL:        /* backward N lines */
+                       CMD_EXEC;
                        if (number > 0)
                                scroll = number;
                        if (number > 0)
                                scroll = number;
-                       cmd_exec();
                        backward(scroll, 0);
                        break;
                        backward(scroll, 0);
                        break;
-
-               case A_FREPAINT:
-                       /*
-                        * Flush buffers, then repaint screen.
-                        * Don't flush the buffers on a pipe!
-                        */
-                       if (!ispipe)
-                       {
+               case A_FREPAINT:        /* flush buffers and repaint */
+                       if (!ispipe) {
                                ch_init(0, 0);
                                clr_linenum();
                        }
                                ch_init(0, 0);
                                clr_linenum();
                        }
-                       /* FALLTHRU */
-               case A_REPAINT:
-                       /*
-                        * Repaint screen.
-                        */
-                       cmd_exec();
+                       /* FALLTHROUGH */
+               case A_REPAINT:         /* repaint the screen */
+                       CMD_EXEC;
                        repaint();
                        break;
                        repaint();
                        break;
-
-               case A_GOLINE:
-                       /*
-                        * Go to line N, default beginning of file.
-                        */
+               case A_GOLINE:          /* go to line N, default 1 */
+                       CMD_EXEC;
                        if (number <= 0)
                                number = 1;
                        if (number <= 0)
                                number = 1;
-                       cmd_exec();
                        jump_back(number);
                        break;
                        jump_back(number);
                        break;
-
-               case A_PERCENT:
-                       /*
-                        * Go to a specified percentage into the file.
-                        */
+               case A_PERCENT:         /* go to percent of file */
+                       CMD_EXEC;
                        if (number < 0)
                                number = 0;
                        if (number < 0)
                                number = 0;
-                       if (number > 100)
+                       else if (number > 100)
                                number = 100;
                                number = 100;
-                       cmd_exec();
                        jump_percent(number);
                        break;
                        jump_percent(number);
                        break;
-
-               case A_GOEND:
-                       /*
-                        * Go to line N, default end of file.
-                        */
-                       cmd_exec();
+               case A_GOEND:           /* go to line N, default end */
+                       CMD_EXEC;
                        if (number <= 0)
                                jump_forw();
                        else
                                jump_back(number);
                        break;
                        if (number <= 0)
                                jump_forw();
                        else
                                jump_back(number);
                        break;
-
                case A_STAT:            /* print file name, etc. */
                        longprompt = 1;
                        continue;
                case A_STAT:            /* print file name, etc. */
                        longprompt = 1;
                        continue;
-
                case A_QUIT:            /* exit */
                        quit();
                case A_QUIT:            /* exit */
                        quit();
-
-               case A_F_SEARCH:
+               case A_F_SEARCH:        /* search for a pattern */
                case A_B_SEARCH:
                case A_B_SEARCH:
-                       /*
-                        * Search for a pattern.
-                        * Accept chars of the pattern until \n.
-                        */
                        if (number <= 0)
                                number = 1;
                        start_mca(action, (action==A_F_SEARCH) ? "/" : "?");
                        if (number <= 0)
                                number = 1;
                        start_mca(action, (action==A_F_SEARCH) ? "/" : "?");
@@ -566,94 +453,72 @@ again:            if (sigs)
                        c = getcc();
                        if (c == '!') {
                                /*
                        c = getcc();
                        if (c == '!') {
                                /*
-                                * Invert the sense of the search.
-                                * Set wsearch to 0 and get a new
-                                * character for the start of the pattern.
+                                * Invert the sense of the search; set wsearch
+                                * to 0 and get a new character for the start
+                                * of the pattern.
                                 */
                                start_mca(action, 
                                 */
                                start_mca(action, 
-                                       (action==A_F_SEARCH) ? "!/" : "!?");
+                                   (action == A_F_SEARCH) ? "!/" : "!?");
                                wsearch = 0;
                                c = getcc();
                        }
                        goto again;
                                wsearch = 0;
                                c = getcc();
                        }
                        goto again;
-
-               case A_AGAIN_SEARCH:
-                       /*
-                        * Repeat previous search.
-                        */
+               case A_AGAIN_SEARCH:            /* repeat previous search */
                        if (number <= 0)
                                number = 1;
                        if (wsearch)
                                start_mca(last_mca, 
                        if (number <= 0)
                                number = 1;
                        if (wsearch)
                                start_mca(last_mca, 
-                                       (last_mca==A_F_SEARCH) ? "/" : "?");
+                                   (last_mca == A_F_SEARCH) ? "/" : "?");
                        else
                                start_mca(last_mca, 
                        else
                                start_mca(last_mca, 
-                                       (last_mca==A_F_SEARCH) ? "!/" : "!?");
-                       cmd_exec();
+                                   (last_mca == A_F_SEARCH) ? "!/" : "!?");
+                       CMD_EXEC;
                        (void)search(mca == A_F_SEARCH, (char *)NULL,
                            number, wsearch);
                        break;
                        (void)search(mca == A_F_SEARCH, (char *)NULL,
                            number, wsearch);
                        break;
-
-               case A_HELP:
-                       /*
-                        * Help.
-                        */
+               case A_HELP:                    /* help */
                        lower_left();
                        clear_eol();
                        putstr("help");
                        lower_left();
                        clear_eol();
                        putstr("help");
-                       cmd_exec();
+                       CMD_EXEC;
                        help();
                        break;
                        help();
                        break;
-
-               case A_TAGFILE:         /* tag a new file; get the file name */
-                       cmd_reset();
+               case A_TAGFILE:                 /* tag a new file */
+                       CMD_RESET;
                        start_mca(A_TAGFILE, "Tag: ");
                        c = getcc();
                        goto again;
                        start_mca(A_TAGFILE, "Tag: ");
                        c = getcc();
                        goto again;
-
-               case A_EXAMINE:         /* edit a new file; get the file name */
-                       cmd_reset();
+               case A_FILE_LIST:               /* show list of file names */
+                       CMD_EXEC;
+                       showlist();
+                       repaint();
+                       break;
+               case A_EXAMINE:                 /* edit a new file */
+                       CMD_RESET;
                        start_mca(A_EXAMINE, "Examine: ");
                        c = getcc();
                        goto again;
                        start_mca(A_EXAMINE, "Examine: ");
                        c = getcc();
                        goto again;
-
-               case A_VISUAL:
-                       /*
-                        * Invoke an editor on the input file.
-                        */
-                       if (ispipe)
-                       {
+               case A_VISUAL:                  /* invoke the editor */
+                       if (ispipe) {
                                error("Cannot edit standard input");
                                break;
                        }
                                error("Cannot edit standard input");
                                break;
                        }
-                       cmd_exec();
+                       CMD_EXEC;
                        editfile();
                        ch_init(0, 0);
                        clr_linenum();
                        break;
                        editfile();
                        ch_init(0, 0);
                        clr_linenum();
                        break;
-
-               case A_NEXT_FILE:
-                       /*
-                        * Examine next file.
-                        */
+               case A_NEXT_FILE:               /* examine next file */
                        if (number <= 0)
                                number = 1;
                        next_file(number);
                        break;
                        if (number <= 0)
                                number = 1;
                        next_file(number);
                        break;
-
-               case A_PREV_FILE:
-                       /*
-                        * Examine previous file.
-                        */
+               case A_PREV_FILE:               /* examine previous file */
                        if (number <= 0)
                                number = 1;
                        prev_file(number);
                        break;
                        if (number <= 0)
                                number = 1;
                        prev_file(number);
                        break;
-
-               case A_SETMARK:
-                       /*
-                        * Set a mark.
-                        */
+               case A_SETMARK:                 /* set a mark */
                        lower_left();
                        clear_eol();
                        start_mca(A_SETMARK, "mark: ");
                        lower_left();
                        clear_eol();
                        start_mca(A_SETMARK, "mark: ");
@@ -662,11 +527,7 @@ again:             if (sigs)
                                break;
                        setmark(c);
                        break;
                                break;
                        setmark(c);
                        break;
-
-               case A_GOMARK:
-                       /*
-                        * Go to a mark.
-                        */
+               case A_GOMARK:                  /* go to mark */
                        lower_left();
                        clear_eol();
                        start_mca(A_GOMARK, "goto mark: ");
                        lower_left();
                        clear_eol();
                        start_mca(A_GOMARK, "goto mark: ");
@@ -675,24 +536,21 @@ again:            if (sigs)
                                break;
                        gomark(c);
                        break;
                                break;
                        gomark(c);
                        break;
-
                case A_PREFIX:
                        /*
                         * The command is incomplete (more chars are needed).
                case A_PREFIX:
                        /*
                         * The command is incomplete (more chars are needed).
-                        * Display the current char so the user knows
-                        * what's going on and get another character.
+                        * Display the current char so the user knows what's
+                        * going on and get another character.
                         */
                        if (mca != A_PREFIX)
                         */
                        if (mca != A_PREFIX)
-                               start_mca(A_PREFIX, "& ");
-                       if (CONTROL_CHAR(c))
-                       {
+                               start_mca(A_PREFIX, "");
+                       if (CONTROL_CHAR(c)) {
                                putchr('^');
                                c = CARAT_CHAR(c);
                        }
                        putchr(c);
                        c = getcc();
                        goto again;
                                putchr('^');
                                c = CARAT_CHAR(c);
                        }
                        putchr(c);
                        c = getcc();
                        goto again;
-
                default:
                        bell();
                        break;
                default:
                        bell();
                        break;
@@ -713,8 +571,7 @@ editfile()
                editor = getenv("EDITOR");
                /* pass the line number to vi */
                if (editor == NULL || *editor == '\0') {
                editor = getenv("EDITOR");
                /* pass the line number to vi */
                if (editor == NULL || *editor == '\0') {
-#define        EDIT_PGM        "/usr/ucb/vi"
-                       editor = EDIT_PGM;
+                       editor = _PATH_VI;
                        dolinenumber = 1;
                }
                else
                        dolinenumber = 1;
                }
                else
@@ -726,3 +583,48 @@ editfile()
                (void)sprintf(buf, "%s %s", editor, current_file);
        lsystem(buf);
 }
                (void)sprintf(buf, "%s %s", editor, current_file);
        lsystem(buf);
 }
+
+static
+showlist()
+{
+       extern int sc_width;
+       extern char **av;
+       register int indx, width;
+       int len;
+       char *p;
+
+       if (ac <= 0) {
+               error("No files provided as arguments.");
+               return;
+       }
+       for (width = indx = 0; indx < ac;) {
+               p = strcmp(av[indx], "-") ? av[indx] : "stdin";
+               len = strlen(p) + 1;
+               if (curr_ac == indx)
+                       len += 2;
+               if (width + len + 1 >= sc_width) {
+                       if (!width) {
+                               if (curr_ac == indx)
+                                       putchr('[');
+                               putstr(p);
+                               if (curr_ac == indx)
+                                       putchr(']');
+                               ++indx;
+                       }
+                       width = 0;
+                       putchr('\n');
+                       continue;
+               }
+               if (width)
+                       putchr(' ');
+               if (curr_ac == indx)
+                       putchr('[');
+               putstr(p);
+               if (curr_ac == indx)
+                       putchr(']');
+               width += len;
+               ++indx;
+       }
+       putchr('\n');
+       error((char *)NULL);
+}