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