SCCS-vsn: usr.bin/more/command.c 5.15
-static char sccsid[] = "@(#)command.c 5.14 (Berkeley) %G%";
+static char sccsid[] = "@(#)command.c 5.15 (Berkeley) %G%";
-/*
- * User-level command processor.
- */
-
#include <sys/param.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/param.h>
#include <stdio.h>
#include <ctype.h>
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. */
+ /*
+ * backspace past beginning of the string: this usually means
+ * abort the command.
+ */
- /*
- * Backspace past beginning of the string:
- * this usually means abort the command.
- */
+ /* 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;
- * 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
-/*
- * 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();
-}
-
- extern int terseprompt, linenums;
extern char *current_name, *firstsearch, *next_name;
off_t len, pos, ch_length(), position(), forw_line();
char pbuf[40];
extern char *current_name, *firstsearch, *next_name;
off_t len, pos, ch_length(), position(), forw_line();
char pbuf[40];
so_exit();
longprompt = 0;
}
so_exit();
longprompt = 0;
}
- else if (terseprompt)
- putchr(':');
else {
so_enter();
putstr(current_name);
if (hit_eof)
if (next_name) {
else {
so_enter();
putstr(current_name);
if (hit_eof)
if (next_name) {
- (void)sprintf(pbuf, ": END (%s)", next_name);
+ (void)sprintf(pbuf, ": END (next file: %s)",
+ next_name);
-/*
- * Get command character.
- */
+/* get command character. */
-/*
- * Execute a multicharacter command.
- */
+/* execute a multicharacter command. */
char *glob();
*cp = '\0';
char *glob();
*cp = '\0';
switch (mca) {
case A_F_SEARCH:
(void)search(1, cmdbuf, number, wsearch);
switch (mca) {
case A_F_SEARCH:
(void)search(1, cmdbuf, number, wsearch);
-/*
- * 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;
* Not part of the number.
* Treat as a normal command character.
*/
* Not part of the number.
* Treat as a normal command character.
*/
+ *cp = '\0';
+ number = atoi(cmdbuf);
+ CMD_RESET;
mca = 0;
return(NO_MCA);
}
mca = 0;
return(NO_MCA);
}
* 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. */
- /*
- * Abort the multi-char command.
- */
- /*
- * Need another character.
- */
/*
* Display prompt and accept a character.
*/
/*
* Display prompt and accept a character.
*/
if (!prompt()) {
next_file(1);
continue;
if (!prompt()) {
next_file(1);
continue;
- /*
- * 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)
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)
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);
-
- 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);
-
- 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;
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;
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();
}
case A_REPAINT: /* repaint the screen */
case A_REPAINT: /* repaint the screen */
-
- 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;
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;
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: /* search for a pattern */
- /*
- * 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) ? "/" : "?");
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.
- (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) ? "/" : "?");
- (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");
-
- 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_FILE_LIST: /* show list of file names */
- cmd_exec();
+ case A_FILE_LIST: /* show list of file names */
+ CMD_EXEC;
showlist();
repaint();
break;
showlist();
repaint();
break;
-
- case A_EXAMINE: /* edit a new file; get the file name */
- cmd_reset();
+ 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;
}
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: ");
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: ");
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.
- 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;