command.c revision 1.1 1 1.1 cjs /*
2 1.1 cjs * Copyright (c) 1988 Mark Nudleman
3 1.1 cjs * Copyright (c) 1988, 1993
4 1.1 cjs * The Regents of the University of California. All rights reserved.
5 1.1 cjs *
6 1.1 cjs * Redistribution and use in source and binary forms, with or without
7 1.1 cjs * modification, are permitted provided that the following conditions
8 1.1 cjs * are met:
9 1.1 cjs * 1. Redistributions of source code must retain the above copyright
10 1.1 cjs * notice, this list of conditions and the following disclaimer.
11 1.1 cjs * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 cjs * notice, this list of conditions and the following disclaimer in the
13 1.1 cjs * documentation and/or other materials provided with the distribution.
14 1.1 cjs * 3. All advertising materials mentioning features or use of this software
15 1.1 cjs * must display the following acknowledgement:
16 1.1 cjs * This product includes software developed by the University of
17 1.1 cjs * California, Berkeley and its contributors.
18 1.1 cjs * 4. Neither the name of the University nor the names of its contributors
19 1.1 cjs * may be used to endorse or promote products derived from this software
20 1.1 cjs * without specific prior written permission.
21 1.1 cjs *
22 1.1 cjs * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cjs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cjs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cjs * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cjs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cjs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cjs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cjs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cjs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cjs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cjs * SUCH DAMAGE.
33 1.1 cjs */
34 1.1 cjs
35 1.1 cjs #ifndef lint
36 1.1 cjs static char sccsid[] = "@(#)command.c 8.1 (Berkeley) 6/6/93";
37 1.1 cjs #endif /* not lint */
38 1.1 cjs
39 1.1 cjs #include <sys/param.h>
40 1.1 cjs #include <stdio.h>
41 1.1 cjs #include <ctype.h>
42 1.1 cjs #include <less.h>
43 1.1 cjs #include "pathnames.h"
44 1.1 cjs
45 1.1 cjs #define NO_MCA 0
46 1.1 cjs #define MCA_DONE 1
47 1.1 cjs #define MCA_MORE 2
48 1.1 cjs
49 1.1 cjs extern int erase_char, kill_char, werase_char;
50 1.1 cjs extern int ispipe;
51 1.1 cjs extern int sigs;
52 1.1 cjs extern int quit_at_eof;
53 1.1 cjs extern int hit_eof;
54 1.1 cjs extern int sc_width;
55 1.1 cjs extern int sc_height;
56 1.1 cjs extern int sc_window;
57 1.1 cjs extern int curr_ac;
58 1.1 cjs extern int ac;
59 1.1 cjs extern int quitting;
60 1.1 cjs extern int scroll;
61 1.1 cjs extern int screen_trashed; /* The screen has been overwritten */
62 1.1 cjs
63 1.1 cjs static char cmdbuf[120]; /* Buffer for holding a multi-char command */
64 1.1 cjs static char *cp; /* Pointer into cmdbuf */
65 1.1 cjs static int cmd_col; /* Current column of the multi-char command */
66 1.1 cjs static int longprompt; /* if stat command instead of prompt */
67 1.1 cjs static int mca; /* The multicharacter command (action) */
68 1.1 cjs static int last_mca; /* The previous mca */
69 1.1 cjs static int number; /* The number typed by the user */
70 1.1 cjs static int wsearch; /* Search for matches (1) or non-matches (0) */
71 1.1 cjs
72 1.1 cjs #define CMD_RESET cp = cmdbuf /* reset command buffer to empty */
73 1.1 cjs #define CMD_EXEC lower_left(); flush()
74 1.1 cjs
75 1.1 cjs /* backspace in command buffer. */
76 1.1 cjs static
77 1.1 cjs cmd_erase()
78 1.1 cjs {
79 1.1 cjs /*
80 1.1 cjs * backspace past beginning of the string: this usually means
81 1.1 cjs * abort the command.
82 1.1 cjs */
83 1.1 cjs if (cp == cmdbuf)
84 1.1 cjs return(1);
85 1.1 cjs
86 1.1 cjs /* erase an extra character, for the carat. */
87 1.1 cjs if (CONTROL_CHAR(*--cp)) {
88 1.1 cjs backspace();
89 1.1 cjs --cmd_col;
90 1.1 cjs }
91 1.1 cjs
92 1.1 cjs backspace();
93 1.1 cjs --cmd_col;
94 1.1 cjs return(0);
95 1.1 cjs }
96 1.1 cjs
97 1.1 cjs /* set up the display to start a new multi-character command. */
98 1.1 cjs start_mca(action, prompt)
99 1.1 cjs int action;
100 1.1 cjs char *prompt;
101 1.1 cjs {
102 1.1 cjs lower_left();
103 1.1 cjs clear_eol();
104 1.1 cjs putstr(prompt);
105 1.1 cjs cmd_col = strlen(prompt);
106 1.1 cjs mca = action;
107 1.1 cjs }
108 1.1 cjs
109 1.1 cjs /*
110 1.1 cjs * process a single character of a multi-character command, such as
111 1.1 cjs * a number, or the pattern of a search command.
112 1.1 cjs */
113 1.1 cjs static
114 1.1 cjs cmd_char(c)
115 1.1 cjs int c;
116 1.1 cjs {
117 1.1 cjs if (c == erase_char)
118 1.1 cjs return(cmd_erase());
119 1.1 cjs /* in this order, in case werase == erase_char */
120 1.1 cjs if (c == werase_char) {
121 1.1 cjs if (cp > cmdbuf) {
122 1.1 cjs while (isspace(cp[-1]) && !cmd_erase());
123 1.1 cjs while (!isspace(cp[-1]) && !cmd_erase());
124 1.1 cjs while (isspace(cp[-1]) && !cmd_erase());
125 1.1 cjs }
126 1.1 cjs return(cp == cmdbuf);
127 1.1 cjs }
128 1.1 cjs if (c == kill_char) {
129 1.1 cjs while (!cmd_erase());
130 1.1 cjs return(1);
131 1.1 cjs }
132 1.1 cjs /*
133 1.1 cjs * No room in the command buffer, or no room on the screen;
134 1.1 cjs * {{ Could get fancy here; maybe shift the displayed line
135 1.1 cjs * and make room for more chars, like ksh. }}
136 1.1 cjs */
137 1.1 cjs if (cp >= &cmdbuf[sizeof(cmdbuf)-1] || cmd_col >= sc_width-3)
138 1.1 cjs bell();
139 1.1 cjs else {
140 1.1 cjs *cp++ = c;
141 1.1 cjs if (CONTROL_CHAR(c)) {
142 1.1 cjs putchr('^');
143 1.1 cjs cmd_col++;
144 1.1 cjs c = CARAT_CHAR(c);
145 1.1 cjs }
146 1.1 cjs putchr(c);
147 1.1 cjs cmd_col++;
148 1.1 cjs }
149 1.1 cjs return(0);
150 1.1 cjs }
151 1.1 cjs
152 1.1 cjs prompt()
153 1.1 cjs {
154 1.1 cjs extern int linenums, short_file;
155 1.1 cjs extern char *current_name, *firstsearch, *next_name;
156 1.1 cjs off_t len, pos, ch_length(), position(), forw_line();
157 1.1 cjs char pbuf[40];
158 1.1 cjs
159 1.1 cjs /*
160 1.1 cjs * if nothing is displayed yet, display starting from line 1;
161 1.1 cjs * if search string provided, go there instead.
162 1.1 cjs */
163 1.1 cjs if (position(TOP) == NULL_POSITION) {
164 1.1 cjs if (forw_line((off_t)0) == NULL_POSITION)
165 1.1 cjs return(0);
166 1.1 cjs if (!firstsearch || !search(1, firstsearch, 1, 1))
167 1.1 cjs jump_back(1);
168 1.1 cjs }
169 1.1 cjs else if (screen_trashed)
170 1.1 cjs repaint();
171 1.1 cjs
172 1.1 cjs /* if no -e flag and we've hit EOF on the last file, quit. */
173 1.1 cjs if ((!quit_at_eof || short_file) && hit_eof && curr_ac + 1 >= ac)
174 1.1 cjs quit();
175 1.1 cjs
176 1.1 cjs /* select the proper prompt and display it. */
177 1.1 cjs lower_left();
178 1.1 cjs clear_eol();
179 1.1 cjs if (longprompt) {
180 1.1 cjs so_enter();
181 1.1 cjs putstr(current_name);
182 1.1 cjs putstr(":");
183 1.1 cjs if (!ispipe) {
184 1.1 cjs (void)sprintf(pbuf, " file %d/%d", curr_ac + 1, ac);
185 1.1 cjs putstr(pbuf);
186 1.1 cjs }
187 1.1 cjs if (linenums) {
188 1.1 cjs (void)sprintf(pbuf, " line %d", currline(BOTTOM));
189 1.1 cjs putstr(pbuf);
190 1.1 cjs }
191 1.1 cjs if ((pos = position(BOTTOM)) != NULL_POSITION) {
192 1.1 cjs (void)sprintf(pbuf, " byte %qd", pos);
193 1.1 cjs putstr(pbuf);
194 1.1 cjs if (!ispipe && (len = ch_length())) {
195 1.1 cjs (void)sprintf(pbuf, "/%qd pct %qd%%",
196 1.1 cjs len, ((100 * pos) / len));
197 1.1 cjs putstr(pbuf);
198 1.1 cjs }
199 1.1 cjs }
200 1.1 cjs so_exit();
201 1.1 cjs longprompt = 0;
202 1.1 cjs }
203 1.1 cjs else {
204 1.1 cjs so_enter();
205 1.1 cjs putstr(current_name);
206 1.1 cjs if (hit_eof)
207 1.1 cjs if (next_name) {
208 1.1 cjs putstr(": END (next file: ");
209 1.1 cjs putstr(next_name);
210 1.1 cjs putstr(")");
211 1.1 cjs }
212 1.1 cjs else
213 1.1 cjs putstr(": END");
214 1.1 cjs else if (!ispipe &&
215 1.1 cjs (pos = position(BOTTOM)) != NULL_POSITION &&
216 1.1 cjs (len = ch_length())) {
217 1.1 cjs (void)sprintf(pbuf, " (%qd%%)", ((100 * pos) / len));
218 1.1 cjs putstr(pbuf);
219 1.1 cjs }
220 1.1 cjs so_exit();
221 1.1 cjs }
222 1.1 cjs return(1);
223 1.1 cjs }
224 1.1 cjs
225 1.1 cjs /* get command character. */
226 1.1 cjs static
227 1.1 cjs getcc()
228 1.1 cjs {
229 1.1 cjs extern int cmdstack;
230 1.1 cjs int ch;
231 1.1 cjs off_t position();
232 1.1 cjs
233 1.1 cjs /* left over from error() routine. */
234 1.1 cjs if (cmdstack) {
235 1.1 cjs ch = cmdstack;
236 1.1 cjs cmdstack = NULL;
237 1.1 cjs return(ch);
238 1.1 cjs }
239 1.1 cjs if (cp > cmdbuf && position(TOP) == NULL_POSITION) {
240 1.1 cjs /*
241 1.1 cjs * Command is incomplete, so try to complete it.
242 1.1 cjs * There are only two cases:
243 1.1 cjs * 1. We have "/string" but no newline. Add the \n.
244 1.1 cjs * 2. We have a number but no command. Treat as #g.
245 1.1 cjs * (This is all pretty hokey.)
246 1.1 cjs */
247 1.1 cjs if (mca != A_DIGIT)
248 1.1 cjs /* Not a number; must be search string */
249 1.1 cjs return('\n');
250 1.1 cjs else
251 1.1 cjs /* A number; append a 'g' */
252 1.1 cjs return('g');
253 1.1 cjs }
254 1.1 cjs return(getchr());
255 1.1 cjs }
256 1.1 cjs
257 1.1 cjs /* execute a multicharacter command. */
258 1.1 cjs static
259 1.1 cjs exec_mca()
260 1.1 cjs {
261 1.1 cjs extern int file;
262 1.1 cjs extern char *tagfile;
263 1.1 cjs register char *p;
264 1.1 cjs char *glob();
265 1.1 cjs
266 1.1 cjs *cp = '\0';
267 1.1 cjs CMD_EXEC;
268 1.1 cjs switch (mca) {
269 1.1 cjs case A_F_SEARCH:
270 1.1 cjs (void)search(1, cmdbuf, number, wsearch);
271 1.1 cjs break;
272 1.1 cjs case A_B_SEARCH:
273 1.1 cjs (void)search(0, cmdbuf, number, wsearch);
274 1.1 cjs break;
275 1.1 cjs case A_EXAMINE:
276 1.1 cjs for (p = cmdbuf; isspace(*p); ++p);
277 1.1 cjs (void)edit(glob(p));
278 1.1 cjs break;
279 1.1 cjs case A_TAGFILE:
280 1.1 cjs for (p = cmdbuf; isspace(*p); ++p);
281 1.1 cjs findtag(p);
282 1.1 cjs if (tagfile == NULL)
283 1.1 cjs break;
284 1.1 cjs if (edit(tagfile))
285 1.1 cjs (void)tagsearch();
286 1.1 cjs break;
287 1.1 cjs }
288 1.1 cjs }
289 1.1 cjs
290 1.1 cjs /* add a character to a multi-character command. */
291 1.1 cjs static
292 1.1 cjs mca_char(c)
293 1.1 cjs int c;
294 1.1 cjs {
295 1.1 cjs switch (mca) {
296 1.1 cjs case 0: /* not in a multicharacter command. */
297 1.1 cjs case A_PREFIX: /* in the prefix of a command. */
298 1.1 cjs return(NO_MCA);
299 1.1 cjs case A_DIGIT:
300 1.1 cjs /*
301 1.1 cjs * Entering digits of a number.
302 1.1 cjs * Terminated by a non-digit.
303 1.1 cjs */
304 1.1 cjs if (!isascii(c) || !isdigit(c) &&
305 1.1 cjs c != erase_char && c != kill_char && c != werase_char) {
306 1.1 cjs /*
307 1.1 cjs * Not part of the number.
308 1.1 cjs * Treat as a normal command character.
309 1.1 cjs */
310 1.1 cjs *cp = '\0';
311 1.1 cjs number = atoi(cmdbuf);
312 1.1 cjs CMD_RESET;
313 1.1 cjs mca = 0;
314 1.1 cjs return(NO_MCA);
315 1.1 cjs }
316 1.1 cjs break;
317 1.1 cjs }
318 1.1 cjs
319 1.1 cjs /*
320 1.1 cjs * Any other multicharacter command
321 1.1 cjs * is terminated by a newline.
322 1.1 cjs */
323 1.1 cjs if (c == '\n' || c == '\r') {
324 1.1 cjs exec_mca();
325 1.1 cjs return(MCA_DONE);
326 1.1 cjs }
327 1.1 cjs
328 1.1 cjs /* append the char to the command buffer. */
329 1.1 cjs if (cmd_char(c))
330 1.1 cjs return(MCA_DONE);
331 1.1 cjs
332 1.1 cjs return(MCA_MORE);
333 1.1 cjs }
334 1.1 cjs
335 1.1 cjs /*
336 1.1 cjs * Main command processor.
337 1.1 cjs * Accept and execute commands until a quit command, then return.
338 1.1 cjs */
339 1.1 cjs commands()
340 1.1 cjs {
341 1.1 cjs register int c;
342 1.1 cjs register int action;
343 1.1 cjs
344 1.1 cjs last_mca = 0;
345 1.1 cjs scroll = (sc_height + 1) / 2;
346 1.1 cjs
347 1.1 cjs for (;;) {
348 1.1 cjs mca = 0;
349 1.1 cjs number = 0;
350 1.1 cjs
351 1.1 cjs /*
352 1.1 cjs * See if any signals need processing.
353 1.1 cjs */
354 1.1 cjs if (sigs) {
355 1.1 cjs psignals();
356 1.1 cjs if (quitting)
357 1.1 cjs quit();
358 1.1 cjs }
359 1.1 cjs /*
360 1.1 cjs * Display prompt and accept a character.
361 1.1 cjs */
362 1.1 cjs CMD_RESET;
363 1.1 cjs if (!prompt()) {
364 1.1 cjs next_file(1);
365 1.1 cjs continue;
366 1.1 cjs }
367 1.1 cjs noprefix();
368 1.1 cjs c = getcc();
369 1.1 cjs
370 1.1 cjs again: if (sigs)
371 1.1 cjs continue;
372 1.1 cjs
373 1.1 cjs /*
374 1.1 cjs * If we are in a multicharacter command, call mca_char.
375 1.1 cjs * Otherwise we call cmd_decode to determine the
376 1.1 cjs * action to be performed.
377 1.1 cjs */
378 1.1 cjs if (mca)
379 1.1 cjs switch (mca_char(c)) {
380 1.1 cjs case MCA_MORE:
381 1.1 cjs /*
382 1.1 cjs * Need another character.
383 1.1 cjs */
384 1.1 cjs c = getcc();
385 1.1 cjs goto again;
386 1.1 cjs case MCA_DONE:
387 1.1 cjs /*
388 1.1 cjs * Command has been handled by mca_char.
389 1.1 cjs * Start clean with a prompt.
390 1.1 cjs */
391 1.1 cjs continue;
392 1.1 cjs case NO_MCA:
393 1.1 cjs /*
394 1.1 cjs * Not a multi-char command
395 1.1 cjs * (at least, not anymore).
396 1.1 cjs */
397 1.1 cjs break;
398 1.1 cjs }
399 1.1 cjs
400 1.1 cjs /* decode the command character and decide what to do. */
401 1.1 cjs switch (action = cmd_decode(c)) {
402 1.1 cjs case A_DIGIT: /* first digit of a number */
403 1.1 cjs start_mca(A_DIGIT, ":");
404 1.1 cjs goto again;
405 1.1 cjs case A_F_SCREEN: /* forward one screen */
406 1.1 cjs CMD_EXEC;
407 1.1 cjs if (number <= 0 && (number = sc_window) <= 0)
408 1.1 cjs number = sc_height - 1;
409 1.1 cjs forward(number, 1);
410 1.1 cjs break;
411 1.1 cjs case A_B_SCREEN: /* backward one screen */
412 1.1 cjs CMD_EXEC;
413 1.1 cjs if (number <= 0 && (number = sc_window) <= 0)
414 1.1 cjs number = sc_height - 1;
415 1.1 cjs backward(number, 1);
416 1.1 cjs break;
417 1.1 cjs case A_F_LINE: /* forward N (default 1) line */
418 1.1 cjs CMD_EXEC;
419 1.1 cjs forward(number <= 0 ? 1 : number, 0);
420 1.1 cjs break;
421 1.1 cjs case A_B_LINE: /* backward N (default 1) line */
422 1.1 cjs CMD_EXEC;
423 1.1 cjs backward(number <= 0 ? 1 : number, 0);
424 1.1 cjs break;
425 1.1 cjs case A_F_SCROLL: /* forward N lines */
426 1.1 cjs CMD_EXEC;
427 1.1 cjs if (number > 0)
428 1.1 cjs scroll = number;
429 1.1 cjs forward(scroll, 0);
430 1.1 cjs break;
431 1.1 cjs case A_B_SCROLL: /* backward N lines */
432 1.1 cjs CMD_EXEC;
433 1.1 cjs if (number > 0)
434 1.1 cjs scroll = number;
435 1.1 cjs backward(scroll, 0);
436 1.1 cjs break;
437 1.1 cjs case A_FREPAINT: /* flush buffers and repaint */
438 1.1 cjs if (!ispipe) {
439 1.1 cjs ch_init(0, 0);
440 1.1 cjs clr_linenum();
441 1.1 cjs }
442 1.1 cjs /* FALLTHROUGH */
443 1.1 cjs case A_REPAINT: /* repaint the screen */
444 1.1 cjs CMD_EXEC;
445 1.1 cjs repaint();
446 1.1 cjs break;
447 1.1 cjs case A_GOLINE: /* go to line N, default 1 */
448 1.1 cjs CMD_EXEC;
449 1.1 cjs if (number <= 0)
450 1.1 cjs number = 1;
451 1.1 cjs jump_back(number);
452 1.1 cjs break;
453 1.1 cjs case A_PERCENT: /* go to percent of file */
454 1.1 cjs CMD_EXEC;
455 1.1 cjs if (number < 0)
456 1.1 cjs number = 0;
457 1.1 cjs else if (number > 100)
458 1.1 cjs number = 100;
459 1.1 cjs jump_percent(number);
460 1.1 cjs break;
461 1.1 cjs case A_GOEND: /* go to line N, default end */
462 1.1 cjs CMD_EXEC;
463 1.1 cjs if (number <= 0)
464 1.1 cjs jump_forw();
465 1.1 cjs else
466 1.1 cjs jump_back(number);
467 1.1 cjs break;
468 1.1 cjs case A_STAT: /* print file name, etc. */
469 1.1 cjs longprompt = 1;
470 1.1 cjs continue;
471 1.1 cjs case A_QUIT: /* exit */
472 1.1 cjs quit();
473 1.1 cjs case A_F_SEARCH: /* search for a pattern */
474 1.1 cjs case A_B_SEARCH:
475 1.1 cjs if (number <= 0)
476 1.1 cjs number = 1;
477 1.1 cjs start_mca(action, (action==A_F_SEARCH) ? "/" : "?");
478 1.1 cjs last_mca = mca;
479 1.1 cjs wsearch = 1;
480 1.1 cjs c = getcc();
481 1.1 cjs if (c == '!') {
482 1.1 cjs /*
483 1.1 cjs * Invert the sense of the search; set wsearch
484 1.1 cjs * to 0 and get a new character for the start
485 1.1 cjs * of the pattern.
486 1.1 cjs */
487 1.1 cjs start_mca(action,
488 1.1 cjs (action == A_F_SEARCH) ? "!/" : "!?");
489 1.1 cjs wsearch = 0;
490 1.1 cjs c = getcc();
491 1.1 cjs }
492 1.1 cjs goto again;
493 1.1 cjs case A_AGAIN_SEARCH: /* repeat previous search */
494 1.1 cjs if (number <= 0)
495 1.1 cjs number = 1;
496 1.1 cjs if (wsearch)
497 1.1 cjs start_mca(last_mca,
498 1.1 cjs (last_mca == A_F_SEARCH) ? "/" : "?");
499 1.1 cjs else
500 1.1 cjs start_mca(last_mca,
501 1.1 cjs (last_mca == A_F_SEARCH) ? "!/" : "!?");
502 1.1 cjs CMD_EXEC;
503 1.1 cjs (void)search(mca == A_F_SEARCH, (char *)NULL,
504 1.1 cjs number, wsearch);
505 1.1 cjs break;
506 1.1 cjs case A_HELP: /* help */
507 1.1 cjs lower_left();
508 1.1 cjs clear_eol();
509 1.1 cjs putstr("help");
510 1.1 cjs CMD_EXEC;
511 1.1 cjs help();
512 1.1 cjs break;
513 1.1 cjs case A_TAGFILE: /* tag a new file */
514 1.1 cjs CMD_RESET;
515 1.1 cjs start_mca(A_TAGFILE, "Tag: ");
516 1.1 cjs c = getcc();
517 1.1 cjs goto again;
518 1.1 cjs case A_FILE_LIST: /* show list of file names */
519 1.1 cjs CMD_EXEC;
520 1.1 cjs showlist();
521 1.1 cjs repaint();
522 1.1 cjs break;
523 1.1 cjs case A_EXAMINE: /* edit a new file */
524 1.1 cjs CMD_RESET;
525 1.1 cjs start_mca(A_EXAMINE, "Examine: ");
526 1.1 cjs c = getcc();
527 1.1 cjs goto again;
528 1.1 cjs case A_VISUAL: /* invoke the editor */
529 1.1 cjs if (ispipe) {
530 1.1 cjs error("Cannot edit standard input");
531 1.1 cjs break;
532 1.1 cjs }
533 1.1 cjs CMD_EXEC;
534 1.1 cjs editfile();
535 1.1 cjs ch_init(0, 0);
536 1.1 cjs clr_linenum();
537 1.1 cjs break;
538 1.1 cjs case A_NEXT_FILE: /* examine next file */
539 1.1 cjs if (number <= 0)
540 1.1 cjs number = 1;
541 1.1 cjs next_file(number);
542 1.1 cjs break;
543 1.1 cjs case A_PREV_FILE: /* examine previous file */
544 1.1 cjs if (number <= 0)
545 1.1 cjs number = 1;
546 1.1 cjs prev_file(number);
547 1.1 cjs break;
548 1.1 cjs case A_SETMARK: /* set a mark */
549 1.1 cjs lower_left();
550 1.1 cjs clear_eol();
551 1.1 cjs start_mca(A_SETMARK, "mark: ");
552 1.1 cjs c = getcc();
553 1.1 cjs if (c == erase_char || c == kill_char)
554 1.1 cjs break;
555 1.1 cjs setmark(c);
556 1.1 cjs break;
557 1.1 cjs case A_GOMARK: /* go to mark */
558 1.1 cjs lower_left();
559 1.1 cjs clear_eol();
560 1.1 cjs start_mca(A_GOMARK, "goto mark: ");
561 1.1 cjs c = getcc();
562 1.1 cjs if (c == erase_char || c == kill_char)
563 1.1 cjs break;
564 1.1 cjs gomark(c);
565 1.1 cjs break;
566 1.1 cjs case A_PREFIX:
567 1.1 cjs /*
568 1.1 cjs * The command is incomplete (more chars are needed).
569 1.1 cjs * Display the current char so the user knows what's
570 1.1 cjs * going on and get another character.
571 1.1 cjs */
572 1.1 cjs if (mca != A_PREFIX)
573 1.1 cjs start_mca(A_PREFIX, "");
574 1.1 cjs if (CONTROL_CHAR(c)) {
575 1.1 cjs putchr('^');
576 1.1 cjs c = CARAT_CHAR(c);
577 1.1 cjs }
578 1.1 cjs putchr(c);
579 1.1 cjs c = getcc();
580 1.1 cjs goto again;
581 1.1 cjs default:
582 1.1 cjs bell();
583 1.1 cjs break;
584 1.1 cjs }
585 1.1 cjs }
586 1.1 cjs }
587 1.1 cjs
588 1.1 cjs editfile()
589 1.1 cjs {
590 1.1 cjs extern char *current_file;
591 1.1 cjs static int dolinenumber;
592 1.1 cjs static char *editor;
593 1.1 cjs int c;
594 1.1 cjs char buf[MAXPATHLEN * 2 + 20], *getenv();
595 1.1 cjs
596 1.1 cjs if (editor == NULL) {
597 1.1 cjs editor = getenv("EDITOR");
598 1.1 cjs /* pass the line number to vi */
599 1.1 cjs if (editor == NULL || *editor == '\0') {
600 1.1 cjs editor = _PATH_VI;
601 1.1 cjs dolinenumber = 1;
602 1.1 cjs }
603 1.1 cjs else
604 1.1 cjs dolinenumber = 0;
605 1.1 cjs }
606 1.1 cjs if (dolinenumber && (c = currline(MIDDLE)))
607 1.1 cjs (void)sprintf(buf, "%s +%d %s", editor, c, current_file);
608 1.1 cjs else
609 1.1 cjs (void)sprintf(buf, "%s %s", editor, current_file);
610 1.1 cjs lsystem(buf);
611 1.1 cjs }
612 1.1 cjs
613 1.1 cjs showlist()
614 1.1 cjs {
615 1.1 cjs extern int sc_width;
616 1.1 cjs extern char **av;
617 1.1 cjs register int indx, width;
618 1.1 cjs int len;
619 1.1 cjs char *p;
620 1.1 cjs
621 1.1 cjs if (ac <= 0) {
622 1.1 cjs error("No files provided as arguments.");
623 1.1 cjs return;
624 1.1 cjs }
625 1.1 cjs for (width = indx = 0; indx < ac;) {
626 1.1 cjs p = strcmp(av[indx], "-") ? av[indx] : "stdin";
627 1.1 cjs len = strlen(p) + 1;
628 1.1 cjs if (curr_ac == indx)
629 1.1 cjs len += 2;
630 1.1 cjs if (width + len + 1 >= sc_width) {
631 1.1 cjs if (!width) {
632 1.1 cjs if (curr_ac == indx)
633 1.1 cjs putchr('[');
634 1.1 cjs putstr(p);
635 1.1 cjs if (curr_ac == indx)
636 1.1 cjs putchr(']');
637 1.1 cjs ++indx;
638 1.1 cjs }
639 1.1 cjs width = 0;
640 1.1 cjs putchr('\n');
641 1.1 cjs continue;
642 1.1 cjs }
643 1.1 cjs if (width)
644 1.1 cjs putchr(' ');
645 1.1 cjs if (curr_ac == indx)
646 1.1 cjs putchr('[');
647 1.1 cjs putstr(p);
648 1.1 cjs if (curr_ac == indx)
649 1.1 cjs putchr(']');
650 1.1 cjs width += len;
651 1.1 cjs ++indx;
652 1.1 cjs }
653 1.1 cjs putchr('\n');
654 1.1 cjs error((char *)NULL);
655 1.1 cjs }
656