main.c revision 1.12 1 1.12 thorpej /* $NetBSD: main.c,v 1.12 2000/04/04 17:07:29 thorpej Exp $ */
2 1.3 cgd
3 1.1 alm /* main.c: This file contains the main control and user-interface routines
4 1.1 alm for the ed line editor. */
5 1.1 alm /*-
6 1.1 alm * Copyright (c) 1993 Andrew Moore, Talke Studio.
7 1.1 alm * All rights reserved.
8 1.1 alm *
9 1.1 alm * Redistribution and use in source and binary forms, with or without
10 1.1 alm * modification, are permitted provided that the following conditions
11 1.1 alm * are met:
12 1.1 alm * 1. Redistributions of source code must retain the above copyright
13 1.1 alm * notice, this list of conditions and the following disclaimer.
14 1.1 alm * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 alm * notice, this list of conditions and the following disclaimer in the
16 1.1 alm * documentation and/or other materials provided with the distribution.
17 1.1 alm *
18 1.1 alm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 1.1 alm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 1.1 alm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 1.1 alm * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 1.1 alm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 alm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 1.1 alm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 alm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 alm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 alm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 alm * SUCH DAMAGE.
29 1.1 alm */
30 1.1 alm
31 1.4 thorpej #include <sys/cdefs.h>
32 1.1 alm #ifndef lint
33 1.4 thorpej __COPYRIGHT(
34 1.1 alm "@(#) Copyright (c) 1993 Andrew Moore, Talke Studio. \n\
35 1.4 thorpej All rights reserved.\n");
36 1.1 alm #endif /* not lint */
37 1.1 alm
38 1.1 alm #ifndef lint
39 1.3 cgd #if 0
40 1.1 alm static char *rcsid = "@(#)main.c,v 1.1 1994/02/01 00:34:42 alm Exp";
41 1.3 cgd #else
42 1.12 thorpej __RCSID("$NetBSD: main.c,v 1.12 2000/04/04 17:07:29 thorpej Exp $");
43 1.3 cgd #endif
44 1.1 alm #endif /* not lint */
45 1.1 alm
46 1.1 alm /*
47 1.1 alm * CREDITS
48 1.1 alm *
49 1.1 alm * This program is based on the editor algorithm described in
50 1.1 alm * Brian W. Kernighan and P. J. Plauger's book "Software Tools
51 1.1 alm * in Pascal," Addison-Wesley, 1981.
52 1.1 alm *
53 1.1 alm * The buffering algorithm is attributed to Rodney Ruddock of
54 1.1 alm * the University of Guelph, Guelph, Ontario.
55 1.1 alm *
56 1.1 alm * The cbc.c encryption code is adapted from
57 1.1 alm * the bdes program by Matt Bishop of Dartmouth College,
58 1.1 alm * Hanover, NH.
59 1.1 alm *
60 1.1 alm */
61 1.1 alm
62 1.1 alm #include <sys/ioctl.h>
63 1.1 alm #include <sys/wait.h>
64 1.10 christos #include <termios.h>
65 1.1 alm #include <ctype.h>
66 1.1 alm #include <setjmp.h>
67 1.1 alm #include <pwd.h>
68 1.1 alm
69 1.1 alm #include "ed.h"
70 1.1 alm
71 1.1 alm
72 1.1 alm #ifdef _POSIX_SOURCE
73 1.1 alm sigjmp_buf env;
74 1.1 alm #else
75 1.1 alm jmp_buf env;
76 1.1 alm #endif
77 1.1 alm
78 1.1 alm /* static buffers */
79 1.1 alm char stdinbuf[1]; /* stdin buffer */
80 1.1 alm char *shcmd; /* shell command buffer */
81 1.1 alm int shcmdsz; /* shell command buffer size */
82 1.1 alm int shcmdi; /* shell command buffer index */
83 1.1 alm char *ibuf; /* ed command-line buffer */
84 1.1 alm int ibufsz; /* ed command-line buffer size */
85 1.1 alm char *ibufp; /* pointer to ed command-line buffer */
86 1.1 alm
87 1.1 alm /* global flags */
88 1.1 alm int des = 0; /* if set, use crypt(3) for i/o */
89 1.1 alm int garrulous = 0; /* if set, print all error messages */
90 1.1 alm int isbinary; /* if set, buffer contains ASCII NULs */
91 1.1 alm int isglobal; /* if set, doing a global command */
92 1.1 alm int modified; /* if set, buffer modified since last write */
93 1.1 alm int mutex = 0; /* if set, signals set "sigflags" */
94 1.1 alm int red = 0; /* if set, restrict shell/directory access */
95 1.1 alm int scripted = 0; /* if set, suppress diagnostics */
96 1.1 alm int sigflags = 0; /* if set, signals received while mutex set */
97 1.1 alm int sigactive = 0; /* if set, signal handlers are enabled */
98 1.1 alm
99 1.1 alm char old_filename[MAXPATHLEN + 1] = ""; /* default filename */
100 1.1 alm long current_addr; /* current address in editor buffer */
101 1.1 alm long addr_last; /* last address in editor buffer */
102 1.1 alm int lineno; /* script line number */
103 1.1 alm char *prompt; /* command-line prompt */
104 1.1 alm char *dps = "*"; /* default command-line prompt */
105 1.1 alm
106 1.1 alm char *usage = "usage: %s [-] [-sx] [-p string] [name]\n";
107 1.1 alm
108 1.4 thorpej int main __P((int, char *[]));
109 1.4 thorpej
110 1.1 alm /* ed: line editor */
111 1.1 alm int
112 1.1 alm main(argc, argv)
113 1.1 alm int argc;
114 1.4 thorpej char *argv[];
115 1.1 alm {
116 1.1 alm int c, n;
117 1.1 alm long status = 0;
118 1.5 christos #ifdef __GNUC__
119 1.5 christos (void) &argc;
120 1.5 christos (void) &argv;
121 1.5 christos #endif
122 1.1 alm
123 1.1 alm red = (n = strlen(argv[0])) > 2 && argv[0][n - 3] == 'r';
124 1.1 alm top:
125 1.7 lukem while ((c = getopt(argc, argv, "p:sx")) != -1)
126 1.1 alm switch(c) {
127 1.1 alm case 'p': /* set prompt */
128 1.1 alm prompt = optarg;
129 1.1 alm break;
130 1.1 alm case 's': /* run script */
131 1.1 alm scripted = 1;
132 1.1 alm break;
133 1.1 alm case 'x': /* use crypt */
134 1.1 alm #ifdef DES
135 1.1 alm des = get_keyword();
136 1.1 alm #else
137 1.1 alm fprintf(stderr, "crypt unavailable\n?\n");
138 1.1 alm #endif
139 1.1 alm break;
140 1.1 alm
141 1.1 alm default:
142 1.1 alm fprintf(stderr, usage, argv[0]);
143 1.1 alm exit(1);
144 1.8 mycroft /* NOTREACHED */
145 1.1 alm }
146 1.1 alm argv += optind;
147 1.1 alm argc -= optind;
148 1.1 alm if (argc && **argv == '-') {
149 1.1 alm scripted = 1;
150 1.1 alm if (argc > 1) {
151 1.1 alm optind = 1;
152 1.1 alm goto top;
153 1.1 alm }
154 1.1 alm argv++;
155 1.1 alm argc--;
156 1.1 alm }
157 1.1 alm /* assert: reliable signals! */
158 1.1 alm #ifdef SIGWINCH
159 1.1 alm handle_winch(SIGWINCH);
160 1.1 alm if (isatty(0)) signal(SIGWINCH, handle_winch);
161 1.1 alm #endif
162 1.1 alm signal(SIGHUP, signal_hup);
163 1.1 alm signal(SIGQUIT, SIG_IGN);
164 1.1 alm signal(SIGINT, signal_int);
165 1.1 alm #ifdef _POSIX_SOURCE
166 1.4 thorpej if ((status = sigsetjmp(env, 1)) != 0)
167 1.1 alm #else
168 1.4 thorpej if ((status = setjmp(env)) != 0)
169 1.1 alm #endif
170 1.1 alm {
171 1.1 alm fputs("\n?\n", stderr);
172 1.1 alm sprintf(errmsg, "interrupt");
173 1.1 alm } else {
174 1.1 alm init_buffers();
175 1.1 alm sigactive = 1; /* enable signal handlers */
176 1.1 alm if (argc && **argv && is_legal_filename(*argv)) {
177 1.1 alm if (read_file(*argv, 0) < 0 && !isatty(0))
178 1.1 alm quit(2);
179 1.1 alm else if (**argv != '!')
180 1.1 alm strcpy(old_filename, *argv);
181 1.1 alm } else if (argc) {
182 1.1 alm fputs("?\n", stderr);
183 1.1 alm if (**argv == '\0')
184 1.1 alm sprintf(errmsg, "invalid filename");
185 1.1 alm if (!isatty(0))
186 1.1 alm quit(2);
187 1.1 alm }
188 1.1 alm }
189 1.1 alm for (;;) {
190 1.1 alm if (status < 0 && garrulous)
191 1.1 alm fprintf(stderr, "%s\n", errmsg);
192 1.1 alm if (prompt) {
193 1.1 alm printf("%s", prompt);
194 1.1 alm fflush(stdout);
195 1.1 alm }
196 1.1 alm if ((n = get_tty_line()) < 0) {
197 1.1 alm status = ERR;
198 1.1 alm continue;
199 1.1 alm } else if (n == 0) {
200 1.1 alm if (modified && !scripted) {
201 1.1 alm fputs("?\n", stderr);
202 1.1 alm sprintf(errmsg, "warning: file modified");
203 1.1 alm if (!isatty(0)) {
204 1.1 alm fprintf(stderr, garrulous ?
205 1.1 alm "script, line %d: %s\n" :
206 1.1 alm "", lineno, errmsg);
207 1.1 alm quit(2);
208 1.1 alm }
209 1.1 alm clearerr(stdin);
210 1.1 alm modified = 0;
211 1.1 alm status = EMOD;
212 1.1 alm continue;
213 1.1 alm } else
214 1.1 alm quit(0);
215 1.1 alm } else if (ibuf[n - 1] != '\n') {
216 1.1 alm /* discard line */
217 1.1 alm sprintf(errmsg, "unexpected end-of-file");
218 1.1 alm clearerr(stdin);
219 1.1 alm status = ERR;
220 1.1 alm continue;
221 1.1 alm }
222 1.1 alm isglobal = 0;
223 1.1 alm if ((status = extract_addr_range()) >= 0 &&
224 1.1 alm (status = exec_command()) >= 0)
225 1.4 thorpej if (!status || (status &&
226 1.1 alm (status = display_lines(current_addr, current_addr,
227 1.4 thorpej status))) >= 0)
228 1.1 alm continue;
229 1.1 alm switch (status) {
230 1.1 alm case EOF:
231 1.1 alm quit(0);
232 1.1 alm case EMOD:
233 1.1 alm modified = 0;
234 1.1 alm fputs("?\n", stderr); /* give warning */
235 1.1 alm sprintf(errmsg, "warning: file modified");
236 1.1 alm if (!isatty(0)) {
237 1.1 alm fprintf(stderr, garrulous ?
238 1.1 alm "script, line %d: %s\n" :
239 1.1 alm "", lineno, errmsg);
240 1.1 alm quit(2);
241 1.1 alm }
242 1.1 alm break;
243 1.1 alm case FATAL:
244 1.1 alm if (!isatty(0))
245 1.1 alm fprintf(stderr, garrulous ?
246 1.1 alm "script, line %d: %s\n" : "",
247 1.1 alm lineno, errmsg);
248 1.1 alm else
249 1.1 alm fprintf(stderr, garrulous ? "%s\n" : "",
250 1.1 alm errmsg);
251 1.1 alm quit(3);
252 1.1 alm default:
253 1.1 alm fputs("?\n", stderr);
254 1.1 alm if (!isatty(0)) {
255 1.1 alm fprintf(stderr, garrulous ?
256 1.1 alm "script, line %d: %s\n" : "",
257 1.1 alm lineno, errmsg);
258 1.1 alm quit(2);
259 1.1 alm }
260 1.1 alm break;
261 1.1 alm }
262 1.1 alm }
263 1.9 mycroft /* NOTREACHED */
264 1.1 alm }
265 1.1 alm
266 1.1 alm long first_addr, second_addr, addr_cnt;
267 1.1 alm
268 1.1 alm /* extract_addr_range: get line addresses from the command buffer until an
269 1.1 alm illegal address is seen; return status */
270 1.1 alm int
271 1.1 alm extract_addr_range()
272 1.1 alm {
273 1.1 alm long addr;
274 1.1 alm
275 1.1 alm addr_cnt = 0;
276 1.1 alm first_addr = second_addr = current_addr;
277 1.1 alm while ((addr = next_addr()) >= 0) {
278 1.1 alm addr_cnt++;
279 1.1 alm first_addr = second_addr;
280 1.1 alm second_addr = addr;
281 1.1 alm if (*ibufp != ',' && *ibufp != ';')
282 1.1 alm break;
283 1.1 alm else if (*ibufp++ == ';')
284 1.1 alm current_addr = addr;
285 1.1 alm }
286 1.1 alm if ((addr_cnt = min(addr_cnt, 2)) == 1 || second_addr != addr)
287 1.1 alm first_addr = second_addr;
288 1.1 alm return (addr == ERR) ? ERR : 0;
289 1.1 alm }
290 1.1 alm
291 1.1 alm
292 1.10 christos #define SKIP_BLANKS() while (isspace((unsigned char)*ibufp) && *ibufp != '\n') \
293 1.10 christos ibufp++
294 1.1 alm
295 1.1 alm #define MUST_BE_FIRST() \
296 1.1 alm if (!first) { sprintf(errmsg, "invalid address"); return ERR; }
297 1.1 alm
298 1.1 alm /* next_addr: return the next line address in the command buffer */
299 1.1 alm long
300 1.1 alm next_addr()
301 1.1 alm {
302 1.1 alm char *hd;
303 1.1 alm long addr = current_addr;
304 1.1 alm long n;
305 1.1 alm int first = 1;
306 1.1 alm int c;
307 1.1 alm
308 1.1 alm SKIP_BLANKS();
309 1.1 alm for (hd = ibufp;; first = 0)
310 1.1 alm switch (c = *ibufp) {
311 1.1 alm case '+':
312 1.1 alm case '\t':
313 1.1 alm case ' ':
314 1.1 alm case '-':
315 1.1 alm case '^':
316 1.1 alm ibufp++;
317 1.1 alm SKIP_BLANKS();
318 1.10 christos if (isdigit((unsigned char)*ibufp)) {
319 1.1 alm STRTOL(n, ibufp);
320 1.1 alm addr += (c == '-' || c == '^') ? -n : n;
321 1.1 alm } else if (!isspace(c))
322 1.1 alm addr += (c == '-' || c == '^') ? -1 : 1;
323 1.1 alm break;
324 1.1 alm case '0': case '1': case '2':
325 1.1 alm case '3': case '4': case '5':
326 1.1 alm case '6': case '7': case '8': case '9':
327 1.1 alm MUST_BE_FIRST();
328 1.1 alm STRTOL(addr, ibufp);
329 1.1 alm break;
330 1.1 alm case '.':
331 1.1 alm case '$':
332 1.1 alm MUST_BE_FIRST();
333 1.1 alm ibufp++;
334 1.1 alm addr = (c == '.') ? current_addr : addr_last;
335 1.1 alm break;
336 1.1 alm case '/':
337 1.1 alm case '?':
338 1.1 alm MUST_BE_FIRST();
339 1.1 alm if ((addr = get_matching_node_addr(
340 1.1 alm get_compiled_pattern(), c == '/')) < 0)
341 1.1 alm return ERR;
342 1.1 alm else if (c == *ibufp)
343 1.1 alm ibufp++;
344 1.1 alm break;
345 1.1 alm case '\'':
346 1.1 alm MUST_BE_FIRST();
347 1.1 alm ibufp++;
348 1.1 alm if ((addr = get_marked_node_addr(*ibufp++)) < 0)
349 1.1 alm return ERR;
350 1.1 alm break;
351 1.1 alm case '%':
352 1.1 alm case ',':
353 1.1 alm case ';':
354 1.1 alm if (first) {
355 1.1 alm ibufp++;
356 1.1 alm addr_cnt++;
357 1.1 alm second_addr = (c == ';') ? current_addr : 1;
358 1.1 alm addr = addr_last;
359 1.1 alm break;
360 1.1 alm }
361 1.1 alm /* FALL THROUGH */
362 1.1 alm default:
363 1.1 alm if (ibufp == hd)
364 1.1 alm return EOF;
365 1.1 alm else if (addr < 0 || addr_last < addr) {
366 1.1 alm sprintf(errmsg, "invalid address");
367 1.1 alm return ERR;
368 1.1 alm } else
369 1.1 alm return addr;
370 1.1 alm }
371 1.1 alm /* NOTREACHED */
372 1.1 alm }
373 1.1 alm
374 1.1 alm
375 1.1 alm #ifdef BACKWARDS
376 1.1 alm /* GET_THIRD_ADDR: get a legal address from the command buffer */
377 1.1 alm #define GET_THIRD_ADDR(addr) \
378 1.1 alm { \
379 1.1 alm long ol1, ol2; \
380 1.1 alm \
381 1.1 alm ol1 = first_addr, ol2 = second_addr; \
382 1.1 alm if (extract_addr_range() < 0) \
383 1.1 alm return ERR; \
384 1.1 alm else if (addr_cnt == 0) { \
385 1.1 alm sprintf(errmsg, "destination expected"); \
386 1.1 alm return ERR; \
387 1.1 alm } else if (second_addr < 0 || addr_last < second_addr) { \
388 1.1 alm sprintf(errmsg, "invalid address"); \
389 1.1 alm return ERR; \
390 1.1 alm } \
391 1.1 alm addr = second_addr; \
392 1.1 alm first_addr = ol1, second_addr = ol2; \
393 1.1 alm }
394 1.1 alm #else /* BACKWARDS */
395 1.1 alm /* GET_THIRD_ADDR: get a legal address from the command buffer */
396 1.1 alm #define GET_THIRD_ADDR(addr) \
397 1.1 alm { \
398 1.1 alm long ol1, ol2; \
399 1.1 alm \
400 1.1 alm ol1 = first_addr, ol2 = second_addr; \
401 1.1 alm if (extract_addr_range() < 0) \
402 1.1 alm return ERR; \
403 1.1 alm if (second_addr < 0 || addr_last < second_addr) { \
404 1.1 alm sprintf(errmsg, "invalid address"); \
405 1.1 alm return ERR; \
406 1.1 alm } \
407 1.1 alm addr = second_addr; \
408 1.1 alm first_addr = ol1, second_addr = ol2; \
409 1.1 alm }
410 1.1 alm #endif
411 1.1 alm
412 1.1 alm
413 1.1 alm /* GET_COMMAND_SUFFIX: verify the command suffix in the command buffer */
414 1.1 alm #define GET_COMMAND_SUFFIX() { \
415 1.1 alm int done = 0; \
416 1.1 alm do { \
417 1.1 alm switch(*ibufp) { \
418 1.1 alm case 'p': \
419 1.1 alm gflag |= GPR, ibufp++; \
420 1.1 alm break; \
421 1.1 alm case 'l': \
422 1.1 alm gflag |= GLS, ibufp++; \
423 1.1 alm break; \
424 1.1 alm case 'n': \
425 1.1 alm gflag |= GNP, ibufp++; \
426 1.1 alm break; \
427 1.1 alm default: \
428 1.1 alm done++; \
429 1.1 alm } \
430 1.1 alm } while (!done); \
431 1.1 alm if (*ibufp++ != '\n') { \
432 1.1 alm sprintf(errmsg, "invalid command suffix"); \
433 1.1 alm return ERR; \
434 1.1 alm } \
435 1.1 alm }
436 1.1 alm
437 1.1 alm
438 1.1 alm /* sflags */
439 1.1 alm #define SGG 001 /* complement previous global substitute suffix */
440 1.1 alm #define SGP 002 /* complement previous print suffix */
441 1.1 alm #define SGR 004 /* use last regex instead of last pat */
442 1.1 alm #define SGF 010 /* repeat last substitution */
443 1.1 alm
444 1.1 alm int patlock = 0; /* if set, pattern not freed by get_compiled_pattern() */
445 1.1 alm
446 1.12 thorpej long rows = 22; /* scroll length: ws_row - 2 */
447 1.1 alm
448 1.1 alm /* exec_command: execute the next command in command buffer; return print
449 1.1 alm request, if any */
450 1.1 alm int
451 1.1 alm exec_command()
452 1.1 alm {
453 1.1 alm static pattern_t *pat = NULL;
454 1.1 alm static int sgflag = 0;
455 1.2 cgd static long sgnum = 0;
456 1.1 alm
457 1.1 alm pattern_t *tpat;
458 1.1 alm char *fnp;
459 1.1 alm int gflag = 0;
460 1.1 alm int sflags = 0;
461 1.1 alm long addr = 0;
462 1.1 alm int n = 0;
463 1.1 alm int c;
464 1.1 alm
465 1.1 alm SKIP_BLANKS();
466 1.1 alm switch(c = *ibufp++) {
467 1.1 alm case 'a':
468 1.1 alm GET_COMMAND_SUFFIX();
469 1.1 alm if (!isglobal) clear_undo_stack();
470 1.1 alm if (append_lines(second_addr) < 0)
471 1.1 alm return ERR;
472 1.1 alm break;
473 1.1 alm case 'c':
474 1.1 alm if (check_addr_range(current_addr, current_addr) < 0)
475 1.1 alm return ERR;
476 1.1 alm GET_COMMAND_SUFFIX();
477 1.1 alm if (!isglobal) clear_undo_stack();
478 1.1 alm if (delete_lines(first_addr, second_addr) < 0 ||
479 1.1 alm append_lines(current_addr) < 0)
480 1.1 alm return ERR;
481 1.1 alm break;
482 1.1 alm case 'd':
483 1.1 alm if (check_addr_range(current_addr, current_addr) < 0)
484 1.1 alm return ERR;
485 1.1 alm GET_COMMAND_SUFFIX();
486 1.1 alm if (!isglobal) clear_undo_stack();
487 1.1 alm if (delete_lines(first_addr, second_addr) < 0)
488 1.1 alm return ERR;
489 1.1 alm else if ((addr = INC_MOD(current_addr, addr_last)) != 0)
490 1.1 alm current_addr = addr;
491 1.1 alm break;
492 1.1 alm case 'e':
493 1.1 alm if (modified && !scripted)
494 1.1 alm return EMOD;
495 1.1 alm /* fall through */
496 1.1 alm case 'E':
497 1.1 alm if (addr_cnt > 0) {
498 1.1 alm sprintf(errmsg, "unexpected address");
499 1.1 alm return ERR;
500 1.10 christos } else if (!isspace((unsigned char)*ibufp)) {
501 1.1 alm sprintf(errmsg, "unexpected command suffix");
502 1.1 alm return ERR;
503 1.1 alm } else if ((fnp = get_filename()) == NULL)
504 1.1 alm return ERR;
505 1.1 alm GET_COMMAND_SUFFIX();
506 1.1 alm if (delete_lines(1, addr_last) < 0)
507 1.1 alm return ERR;
508 1.1 alm clear_undo_stack();
509 1.1 alm if (close_sbuf() < 0)
510 1.1 alm return ERR;
511 1.1 alm else if (open_sbuf() < 0)
512 1.1 alm return FATAL;
513 1.1 alm if (*fnp && *fnp != '!') strcpy(old_filename, fnp);
514 1.1 alm #ifdef BACKWARDS
515 1.1 alm if (*fnp == '\0' && *old_filename == '\0') {
516 1.1 alm sprintf(errmsg, "no current filename");
517 1.1 alm return ERR;
518 1.1 alm }
519 1.1 alm #endif
520 1.1 alm if (read_file(*fnp ? fnp : old_filename, 0) < 0)
521 1.1 alm return ERR;
522 1.1 alm clear_undo_stack();
523 1.1 alm modified = 0;
524 1.1 alm u_current_addr = u_addr_last = -1;
525 1.1 alm break;
526 1.1 alm case 'f':
527 1.1 alm if (addr_cnt > 0) {
528 1.1 alm sprintf(errmsg, "unexpected address");
529 1.1 alm return ERR;
530 1.10 christos } else if (!isspace((unsigned char)*ibufp)) {
531 1.1 alm sprintf(errmsg, "unexpected command suffix");
532 1.1 alm return ERR;
533 1.1 alm } else if ((fnp = get_filename()) == NULL)
534 1.1 alm return ERR;
535 1.1 alm else if (*fnp == '!') {
536 1.1 alm sprintf(errmsg, "invalid redirection");
537 1.1 alm return ERR;
538 1.1 alm }
539 1.1 alm GET_COMMAND_SUFFIX();
540 1.1 alm if (*fnp) strcpy(old_filename, fnp);
541 1.1 alm printf("%s\n", strip_escapes(old_filename));
542 1.1 alm break;
543 1.1 alm case 'g':
544 1.1 alm case 'v':
545 1.1 alm case 'G':
546 1.1 alm case 'V':
547 1.1 alm if (isglobal) {
548 1.1 alm sprintf(errmsg, "cannot nest global commands");
549 1.1 alm return ERR;
550 1.1 alm } else if (check_addr_range(1, addr_last) < 0)
551 1.1 alm return ERR;
552 1.1 alm else if (build_active_list(c == 'g' || c == 'G') < 0)
553 1.1 alm return ERR;
554 1.4 thorpej else if ((n = (c == 'G' || c == 'V')) != 0)
555 1.1 alm GET_COMMAND_SUFFIX();
556 1.1 alm isglobal++;
557 1.1 alm if (exec_global(n, gflag) < 0)
558 1.1 alm return ERR;
559 1.1 alm break;
560 1.1 alm case 'h':
561 1.1 alm if (addr_cnt > 0) {
562 1.1 alm sprintf(errmsg, "unexpected address");
563 1.1 alm return ERR;
564 1.1 alm }
565 1.1 alm GET_COMMAND_SUFFIX();
566 1.1 alm if (*errmsg) fprintf(stderr, "%s\n", errmsg);
567 1.1 alm break;
568 1.1 alm case 'H':
569 1.1 alm if (addr_cnt > 0) {
570 1.1 alm sprintf(errmsg, "unexpected address");
571 1.1 alm return ERR;
572 1.1 alm }
573 1.1 alm GET_COMMAND_SUFFIX();
574 1.1 alm if ((garrulous = 1 - garrulous) && *errmsg)
575 1.1 alm fprintf(stderr, "%s\n", errmsg);
576 1.1 alm break;
577 1.1 alm case 'i':
578 1.1 alm if (second_addr == 0) {
579 1.1 alm sprintf(errmsg, "invalid address");
580 1.1 alm return ERR;
581 1.1 alm }
582 1.1 alm GET_COMMAND_SUFFIX();
583 1.1 alm if (!isglobal) clear_undo_stack();
584 1.1 alm if (append_lines(second_addr - 1) < 0)
585 1.1 alm return ERR;
586 1.1 alm break;
587 1.1 alm case 'j':
588 1.1 alm if (check_addr_range(current_addr, current_addr + 1) < 0)
589 1.1 alm return ERR;
590 1.1 alm GET_COMMAND_SUFFIX();
591 1.1 alm if (!isglobal) clear_undo_stack();
592 1.1 alm if (first_addr != second_addr &&
593 1.1 alm join_lines(first_addr, second_addr) < 0)
594 1.1 alm return ERR;
595 1.1 alm break;
596 1.1 alm case 'k':
597 1.1 alm c = *ibufp++;
598 1.1 alm if (second_addr == 0) {
599 1.1 alm sprintf(errmsg, "invalid address");
600 1.1 alm return ERR;
601 1.1 alm }
602 1.1 alm GET_COMMAND_SUFFIX();
603 1.1 alm if (mark_line_node(get_addressed_line_node(second_addr), c) < 0)
604 1.1 alm return ERR;
605 1.1 alm break;
606 1.1 alm case 'l':
607 1.1 alm if (check_addr_range(current_addr, current_addr) < 0)
608 1.1 alm return ERR;
609 1.1 alm GET_COMMAND_SUFFIX();
610 1.1 alm if (display_lines(first_addr, second_addr, gflag | GLS) < 0)
611 1.1 alm return ERR;
612 1.1 alm gflag = 0;
613 1.1 alm break;
614 1.1 alm case 'm':
615 1.1 alm if (check_addr_range(current_addr, current_addr) < 0)
616 1.1 alm return ERR;
617 1.1 alm GET_THIRD_ADDR(addr);
618 1.1 alm if (first_addr <= addr && addr < second_addr) {
619 1.1 alm sprintf(errmsg, "invalid destination");
620 1.1 alm return ERR;
621 1.1 alm }
622 1.1 alm GET_COMMAND_SUFFIX();
623 1.1 alm if (!isglobal) clear_undo_stack();
624 1.1 alm if (move_lines(addr) < 0)
625 1.1 alm return ERR;
626 1.1 alm break;
627 1.1 alm case 'n':
628 1.1 alm if (check_addr_range(current_addr, current_addr) < 0)
629 1.1 alm return ERR;
630 1.1 alm GET_COMMAND_SUFFIX();
631 1.1 alm if (display_lines(first_addr, second_addr, gflag | GNP) < 0)
632 1.1 alm return ERR;
633 1.1 alm gflag = 0;
634 1.1 alm break;
635 1.1 alm case 'p':
636 1.1 alm if (check_addr_range(current_addr, current_addr) < 0)
637 1.1 alm return ERR;
638 1.1 alm GET_COMMAND_SUFFIX();
639 1.1 alm if (display_lines(first_addr, second_addr, gflag | GPR) < 0)
640 1.1 alm return ERR;
641 1.1 alm gflag = 0;
642 1.1 alm break;
643 1.1 alm case 'P':
644 1.1 alm if (addr_cnt > 0) {
645 1.1 alm sprintf(errmsg, "unexpected address");
646 1.1 alm return ERR;
647 1.1 alm }
648 1.1 alm GET_COMMAND_SUFFIX();
649 1.1 alm prompt = prompt ? NULL : optarg ? optarg : dps;
650 1.1 alm break;
651 1.1 alm case 'q':
652 1.1 alm case 'Q':
653 1.1 alm if (addr_cnt > 0) {
654 1.1 alm sprintf(errmsg, "unexpected address");
655 1.1 alm return ERR;
656 1.1 alm }
657 1.1 alm GET_COMMAND_SUFFIX();
658 1.1 alm gflag = (modified && !scripted && c == 'q') ? EMOD : EOF;
659 1.1 alm break;
660 1.1 alm case 'r':
661 1.10 christos if (!isspace((unsigned char)*ibufp)) {
662 1.1 alm sprintf(errmsg, "unexpected command suffix");
663 1.1 alm return ERR;
664 1.1 alm } else if (addr_cnt == 0)
665 1.1 alm second_addr = addr_last;
666 1.1 alm if ((fnp = get_filename()) == NULL)
667 1.1 alm return ERR;
668 1.1 alm GET_COMMAND_SUFFIX();
669 1.1 alm if (!isglobal) clear_undo_stack();
670 1.1 alm if (*old_filename == '\0' && *fnp != '!')
671 1.1 alm strcpy(old_filename, fnp);
672 1.1 alm #ifdef BACKWARDS
673 1.1 alm if (*fnp == '\0' && *old_filename == '\0') {
674 1.1 alm sprintf(errmsg, "no current filename");
675 1.1 alm return ERR;
676 1.1 alm }
677 1.1 alm #endif
678 1.1 alm if ((addr = read_file(*fnp ? fnp : old_filename, second_addr)) < 0)
679 1.1 alm return ERR;
680 1.1 alm else if (addr && addr != addr_last)
681 1.1 alm modified = 1;
682 1.1 alm break;
683 1.1 alm case 's':
684 1.1 alm do {
685 1.1 alm switch(*ibufp) {
686 1.1 alm case '\n':
687 1.1 alm sflags |=SGF;
688 1.1 alm break;
689 1.1 alm case 'g':
690 1.1 alm sflags |= SGG;
691 1.1 alm ibufp++;
692 1.1 alm break;
693 1.1 alm case 'p':
694 1.1 alm sflags |= SGP;
695 1.1 alm ibufp++;
696 1.1 alm break;
697 1.1 alm case 'r':
698 1.1 alm sflags |= SGR;
699 1.1 alm ibufp++;
700 1.1 alm break;
701 1.1 alm case '0': case '1': case '2': case '3': case '4':
702 1.1 alm case '5': case '6': case '7': case '8': case '9':
703 1.1 alm STRTOL(sgnum, ibufp);
704 1.1 alm sflags |= SGF;
705 1.1 alm sgflag &= ~GSG; /* override GSG */
706 1.1 alm break;
707 1.1 alm default:
708 1.1 alm if (sflags) {
709 1.1 alm sprintf(errmsg, "invalid command suffix");
710 1.1 alm return ERR;
711 1.1 alm }
712 1.1 alm }
713 1.1 alm } while (sflags && *ibufp != '\n');
714 1.1 alm if (sflags && !pat) {
715 1.1 alm sprintf(errmsg, "no previous substitution");
716 1.1 alm return ERR;
717 1.1 alm } else if (sflags & SGG)
718 1.1 alm sgnum = 0; /* override numeric arg */
719 1.1 alm if (*ibufp != '\n' && *(ibufp + 1) == '\n') {
720 1.1 alm sprintf(errmsg, "invalid pattern delimiter");
721 1.1 alm return ERR;
722 1.1 alm }
723 1.1 alm tpat = pat;
724 1.1 alm SPL1();
725 1.1 alm if ((!sflags || (sflags & SGR)) &&
726 1.1 alm (tpat = get_compiled_pattern()) == NULL) {
727 1.1 alm SPL0();
728 1.1 alm return ERR;
729 1.1 alm } else if (tpat != pat) {
730 1.1 alm if (pat) {
731 1.1 alm regfree(pat);
732 1.1 alm free(pat);
733 1.1 alm }
734 1.1 alm pat = tpat;
735 1.1 alm patlock = 1; /* reserve pattern */
736 1.1 alm }
737 1.1 alm SPL0();
738 1.1 alm if (!sflags && extract_subst_tail(&sgflag, &sgnum) < 0)
739 1.1 alm return ERR;
740 1.1 alm else if (isglobal)
741 1.1 alm sgflag |= GLB;
742 1.1 alm else
743 1.1 alm sgflag &= ~GLB;
744 1.1 alm if (sflags & SGG)
745 1.1 alm sgflag ^= GSG;
746 1.1 alm if (sflags & SGP)
747 1.1 alm sgflag ^= GPR, sgflag &= ~(GLS | GNP);
748 1.1 alm do {
749 1.1 alm switch(*ibufp) {
750 1.1 alm case 'p':
751 1.1 alm sgflag |= GPR, ibufp++;
752 1.1 alm break;
753 1.1 alm case 'l':
754 1.1 alm sgflag |= GLS, ibufp++;
755 1.1 alm break;
756 1.1 alm case 'n':
757 1.1 alm sgflag |= GNP, ibufp++;
758 1.1 alm break;
759 1.1 alm default:
760 1.1 alm n++;
761 1.1 alm }
762 1.1 alm } while (!n);
763 1.1 alm if (check_addr_range(current_addr, current_addr) < 0)
764 1.1 alm return ERR;
765 1.1 alm GET_COMMAND_SUFFIX();
766 1.1 alm if (!isglobal) clear_undo_stack();
767 1.1 alm if (search_and_replace(pat, sgflag, sgnum) < 0)
768 1.1 alm return ERR;
769 1.1 alm break;
770 1.1 alm case 't':
771 1.1 alm if (check_addr_range(current_addr, current_addr) < 0)
772 1.1 alm return ERR;
773 1.1 alm GET_THIRD_ADDR(addr);
774 1.1 alm GET_COMMAND_SUFFIX();
775 1.1 alm if (!isglobal) clear_undo_stack();
776 1.1 alm if (copy_lines(addr) < 0)
777 1.1 alm return ERR;
778 1.1 alm break;
779 1.1 alm case 'u':
780 1.1 alm if (addr_cnt > 0) {
781 1.1 alm sprintf(errmsg, "unexpected address");
782 1.1 alm return ERR;
783 1.1 alm }
784 1.1 alm GET_COMMAND_SUFFIX();
785 1.1 alm if (pop_undo_stack() < 0)
786 1.1 alm return ERR;
787 1.1 alm break;
788 1.1 alm case 'w':
789 1.1 alm case 'W':
790 1.1 alm if ((n = *ibufp) == 'q' || n == 'Q') {
791 1.1 alm gflag = EOF;
792 1.1 alm ibufp++;
793 1.1 alm }
794 1.10 christos if (!isspace((unsigned char)*ibufp)) {
795 1.1 alm sprintf(errmsg, "unexpected command suffix");
796 1.1 alm return ERR;
797 1.1 alm } else if ((fnp = get_filename()) == NULL)
798 1.1 alm return ERR;
799 1.1 alm if (addr_cnt == 0 && !addr_last)
800 1.1 alm first_addr = second_addr = 0;
801 1.1 alm else if (check_addr_range(1, addr_last) < 0)
802 1.1 alm return ERR;
803 1.1 alm GET_COMMAND_SUFFIX();
804 1.1 alm if (*old_filename == '\0' && *fnp != '!')
805 1.1 alm strcpy(old_filename, fnp);
806 1.1 alm #ifdef BACKWARDS
807 1.1 alm if (*fnp == '\0' && *old_filename == '\0') {
808 1.1 alm sprintf(errmsg, "no current filename");
809 1.1 alm return ERR;
810 1.1 alm }
811 1.1 alm #endif
812 1.1 alm if ((addr = write_file(*fnp ? fnp : old_filename,
813 1.1 alm (c == 'W') ? "a" : "w", first_addr, second_addr)) < 0)
814 1.1 alm return ERR;
815 1.1 alm else if (addr == addr_last)
816 1.1 alm modified = 0;
817 1.1 alm else if (modified && !scripted && n == 'q')
818 1.1 alm gflag = EMOD;
819 1.1 alm break;
820 1.1 alm case 'x':
821 1.1 alm if (addr_cnt > 0) {
822 1.1 alm sprintf(errmsg, "unexpected address");
823 1.1 alm return ERR;
824 1.1 alm }
825 1.1 alm GET_COMMAND_SUFFIX();
826 1.1 alm #ifdef DES
827 1.1 alm des = get_keyword();
828 1.1 alm #else
829 1.1 alm sprintf(errmsg, "crypt unavailable");
830 1.1 alm return ERR;
831 1.1 alm #endif
832 1.1 alm break;
833 1.1 alm case 'z':
834 1.1 alm #ifdef BACKWARDS
835 1.1 alm if (check_addr_range(first_addr = 1, current_addr + 1) < 0)
836 1.1 alm #else
837 1.1 alm if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0)
838 1.1 alm #endif
839 1.1 alm return ERR;
840 1.1 alm else if ('0' < *ibufp && *ibufp <= '9')
841 1.1 alm STRTOL(rows, ibufp);
842 1.1 alm GET_COMMAND_SUFFIX();
843 1.1 alm if (display_lines(second_addr, min(addr_last,
844 1.1 alm second_addr + rows), gflag) < 0)
845 1.1 alm return ERR;
846 1.1 alm gflag = 0;
847 1.1 alm break;
848 1.1 alm case '=':
849 1.1 alm GET_COMMAND_SUFFIX();
850 1.4 thorpej printf("%ld\n", addr_cnt ? second_addr : addr_last);
851 1.1 alm break;
852 1.1 alm case '!':
853 1.1 alm if (addr_cnt > 0) {
854 1.1 alm sprintf(errmsg, "unexpected address");
855 1.1 alm return ERR;
856 1.1 alm } else if ((sflags = get_shell_command()) < 0)
857 1.1 alm return ERR;
858 1.1 alm GET_COMMAND_SUFFIX();
859 1.1 alm if (sflags) printf("%s\n", shcmd + 1);
860 1.1 alm system(shcmd + 1);
861 1.1 alm if (!scripted) printf("!\n");
862 1.1 alm break;
863 1.1 alm case '\n':
864 1.1 alm #ifdef BACKWARDS
865 1.1 alm if (check_addr_range(first_addr = 1, current_addr + 1) < 0
866 1.1 alm #else
867 1.1 alm if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0
868 1.1 alm #endif
869 1.1 alm || display_lines(second_addr, second_addr, 0) < 0)
870 1.1 alm return ERR;
871 1.1 alm break;
872 1.1 alm default:
873 1.1 alm sprintf(errmsg, "unknown command");
874 1.1 alm return ERR;
875 1.1 alm }
876 1.1 alm return gflag;
877 1.1 alm }
878 1.1 alm
879 1.1 alm
880 1.1 alm /* check_addr_range: return status of address range check */
881 1.1 alm int
882 1.1 alm check_addr_range(n, m)
883 1.1 alm long n, m;
884 1.1 alm {
885 1.1 alm if (addr_cnt == 0) {
886 1.1 alm first_addr = n;
887 1.1 alm second_addr = m;
888 1.1 alm }
889 1.1 alm if (first_addr > second_addr || 1 > first_addr ||
890 1.1 alm second_addr > addr_last) {
891 1.1 alm sprintf(errmsg, "invalid address");
892 1.1 alm return ERR;
893 1.1 alm }
894 1.1 alm return 0;
895 1.1 alm }
896 1.1 alm
897 1.1 alm
898 1.1 alm /* get_matching_node_addr: return the address of the next line matching a
899 1.1 alm pattern in a given direction. wrap around begin/end of editor buffer if
900 1.1 alm necessary */
901 1.1 alm long
902 1.1 alm get_matching_node_addr(pat, dir)
903 1.1 alm pattern_t *pat;
904 1.1 alm int dir;
905 1.1 alm {
906 1.1 alm char *s;
907 1.1 alm long n = current_addr;
908 1.1 alm line_t *lp;
909 1.1 alm
910 1.1 alm if (!pat) return ERR;
911 1.1 alm do {
912 1.4 thorpej if ((n = dir ? INC_MOD(n, addr_last) :
913 1.4 thorpej DEC_MOD(n, addr_last)) != 0) {
914 1.1 alm lp = get_addressed_line_node(n);
915 1.1 alm if ((s = get_sbuf_line(lp)) == NULL)
916 1.1 alm return ERR;
917 1.1 alm if (isbinary)
918 1.1 alm NUL_TO_NEWLINE(s, lp->len);
919 1.1 alm if (!regexec(pat, s, 0, NULL, 0))
920 1.1 alm return n;
921 1.1 alm }
922 1.1 alm } while (n != current_addr);
923 1.1 alm sprintf(errmsg, "no match");
924 1.1 alm return ERR;
925 1.1 alm }
926 1.1 alm
927 1.1 alm
928 1.1 alm /* get_filename: return pointer to copy of filename in the command buffer */
929 1.1 alm char *
930 1.1 alm get_filename()
931 1.1 alm {
932 1.1 alm static char *file = NULL;
933 1.1 alm static int filesz = 0;
934 1.1 alm
935 1.1 alm int n;
936 1.1 alm
937 1.1 alm if (*ibufp != '\n') {
938 1.1 alm SKIP_BLANKS();
939 1.1 alm if (*ibufp == '\n') {
940 1.1 alm sprintf(errmsg, "invalid filename");
941 1.1 alm return NULL;
942 1.1 alm } else if ((ibufp = get_extended_line(&n, 1)) == NULL)
943 1.1 alm return NULL;
944 1.1 alm else if (*ibufp == '!') {
945 1.1 alm ibufp++;
946 1.1 alm if ((n = get_shell_command()) < 0)
947 1.1 alm return NULL;
948 1.1 alm if (n) printf("%s\n", shcmd + 1);
949 1.1 alm return shcmd;
950 1.1 alm } else if (n - 1 > MAXPATHLEN) {
951 1.1 alm sprintf(errmsg, "filename too long");
952 1.1 alm return NULL;
953 1.1 alm }
954 1.1 alm }
955 1.1 alm #ifndef BACKWARDS
956 1.1 alm else if (*old_filename == '\0') {
957 1.1 alm sprintf(errmsg, "no current filename");
958 1.1 alm return NULL;
959 1.1 alm }
960 1.1 alm #endif
961 1.1 alm REALLOC(file, filesz, MAXPATHLEN + 1, NULL);
962 1.1 alm for (n = 0; *ibufp != '\n';)
963 1.1 alm file[n++] = *ibufp++;
964 1.1 alm file[n] = '\0';
965 1.1 alm return is_legal_filename(file) ? file : NULL;
966 1.1 alm }
967 1.1 alm
968 1.1 alm
969 1.1 alm /* get_shell_command: read a shell command from stdin; return substitution
970 1.1 alm status */
971 1.1 alm int
972 1.1 alm get_shell_command()
973 1.1 alm {
974 1.1 alm static char *buf = NULL;
975 1.1 alm static int n = 0;
976 1.1 alm
977 1.1 alm char *s; /* substitution char pointer */
978 1.1 alm int i = 0;
979 1.1 alm int j = 0;
980 1.1 alm
981 1.1 alm if (red) {
982 1.1 alm sprintf(errmsg, "shell access restricted");
983 1.1 alm return ERR;
984 1.1 alm } else if ((s = ibufp = get_extended_line(&j, 1)) == NULL)
985 1.1 alm return ERR;
986 1.1 alm REALLOC(buf, n, j + 1, ERR);
987 1.1 alm buf[i++] = '!'; /* prefix command w/ bang */
988 1.1 alm while (*ibufp != '\n')
989 1.1 alm switch (*ibufp) {
990 1.1 alm default:
991 1.1 alm REALLOC(buf, n, i + 2, ERR);
992 1.1 alm buf[i++] = *ibufp;
993 1.1 alm if (*ibufp++ == '\\')
994 1.1 alm buf[i++] = *ibufp++;
995 1.1 alm break;
996 1.1 alm case '!':
997 1.1 alm if (s != ibufp) {
998 1.1 alm REALLOC(buf, n, i + 1, ERR);
999 1.1 alm buf[i++] = *ibufp++;
1000 1.1 alm }
1001 1.1 alm #ifdef BACKWARDS
1002 1.1 alm else if (shcmd == NULL || *(shcmd + 1) == '\0')
1003 1.1 alm #else
1004 1.1 alm else if (shcmd == NULL)
1005 1.1 alm #endif
1006 1.1 alm {
1007 1.1 alm sprintf(errmsg, "no previous command");
1008 1.1 alm return ERR;
1009 1.1 alm } else {
1010 1.1 alm REALLOC(buf, n, i + shcmdi, ERR);
1011 1.1 alm for (s = shcmd + 1; s < shcmd + shcmdi;)
1012 1.1 alm buf[i++] = *s++;
1013 1.1 alm s = ibufp++;
1014 1.1 alm }
1015 1.1 alm break;
1016 1.1 alm case '%':
1017 1.1 alm if (*old_filename == '\0') {
1018 1.1 alm sprintf(errmsg, "no current filename");
1019 1.1 alm return ERR;
1020 1.1 alm }
1021 1.1 alm j = strlen(s = strip_escapes(old_filename));
1022 1.1 alm REALLOC(buf, n, i + j, ERR);
1023 1.1 alm while (j--)
1024 1.1 alm buf[i++] = *s++;
1025 1.1 alm s = ibufp++;
1026 1.1 alm break;
1027 1.1 alm }
1028 1.1 alm REALLOC(shcmd, shcmdsz, i + 1, ERR);
1029 1.1 alm memcpy(shcmd, buf, i);
1030 1.1 alm shcmd[shcmdi = i] = '\0';
1031 1.1 alm return *s == '!' || *s == '%';
1032 1.1 alm }
1033 1.1 alm
1034 1.1 alm
1035 1.1 alm /* append_lines: insert text from stdin to after line n; stop when either a
1036 1.1 alm single period is read or EOF; return status */
1037 1.1 alm int
1038 1.1 alm append_lines(n)
1039 1.1 alm long n;
1040 1.1 alm {
1041 1.1 alm int l;
1042 1.1 alm char *lp = ibuf;
1043 1.1 alm char *eot;
1044 1.1 alm undo_t *up = NULL;
1045 1.1 alm
1046 1.1 alm for (current_addr = n;;) {
1047 1.1 alm if (!isglobal) {
1048 1.1 alm if ((l = get_tty_line()) < 0)
1049 1.1 alm return ERR;
1050 1.1 alm else if (l == 0 || ibuf[l - 1] != '\n') {
1051 1.1 alm clearerr(stdin);
1052 1.1 alm return l ? EOF : 0;
1053 1.1 alm }
1054 1.1 alm lp = ibuf;
1055 1.1 alm } else if (*(lp = ibufp) == '\0')
1056 1.1 alm return 0;
1057 1.1 alm else {
1058 1.1 alm while (*ibufp++ != '\n')
1059 1.1 alm ;
1060 1.1 alm l = ibufp - lp;
1061 1.1 alm }
1062 1.1 alm if (l == 2 && lp[0] == '.' && lp[1] == '\n') {
1063 1.1 alm return 0;
1064 1.1 alm }
1065 1.1 alm eot = lp + l;
1066 1.1 alm SPL1();
1067 1.1 alm do {
1068 1.1 alm if ((lp = put_sbuf_line(lp)) == NULL) {
1069 1.1 alm SPL0();
1070 1.1 alm return ERR;
1071 1.1 alm } else if (up)
1072 1.1 alm up->t = get_addressed_line_node(current_addr);
1073 1.1 alm else if ((up = push_undo_stack(UADD, current_addr,
1074 1.1 alm current_addr)) == NULL) {
1075 1.1 alm SPL0();
1076 1.1 alm return ERR;
1077 1.1 alm }
1078 1.1 alm } while (lp != eot);
1079 1.1 alm modified = 1;
1080 1.1 alm SPL0();
1081 1.1 alm }
1082 1.1 alm /* NOTREACHED */
1083 1.1 alm }
1084 1.1 alm
1085 1.1 alm
1086 1.1 alm /* join_lines: replace a range of lines with the joined text of those lines */
1087 1.1 alm int
1088 1.1 alm join_lines(from, to)
1089 1.1 alm long from;
1090 1.1 alm long to;
1091 1.1 alm {
1092 1.1 alm static char *buf = NULL;
1093 1.1 alm static int n;
1094 1.1 alm
1095 1.1 alm char *s;
1096 1.1 alm int size = 0;
1097 1.1 alm line_t *bp, *ep;
1098 1.1 alm
1099 1.1 alm ep = get_addressed_line_node(INC_MOD(to, addr_last));
1100 1.1 alm bp = get_addressed_line_node(from);
1101 1.1 alm for (; bp != ep; bp = bp->q_forw) {
1102 1.1 alm if ((s = get_sbuf_line(bp)) == NULL)
1103 1.1 alm return ERR;
1104 1.1 alm REALLOC(buf, n, size + bp->len, ERR);
1105 1.1 alm memcpy(buf + size, s, bp->len);
1106 1.1 alm size += bp->len;
1107 1.1 alm }
1108 1.1 alm REALLOC(buf, n, size + 2, ERR);
1109 1.1 alm memcpy(buf + size, "\n", 2);
1110 1.1 alm if (delete_lines(from, to) < 0)
1111 1.1 alm return ERR;
1112 1.1 alm current_addr = from - 1;
1113 1.1 alm SPL1();
1114 1.1 alm if (put_sbuf_line(buf) == NULL ||
1115 1.1 alm push_undo_stack(UADD, current_addr, current_addr) == NULL) {
1116 1.1 alm SPL0();
1117 1.1 alm return ERR;
1118 1.1 alm }
1119 1.1 alm modified = 1;
1120 1.1 alm SPL0();
1121 1.1 alm return 0;
1122 1.1 alm }
1123 1.1 alm
1124 1.1 alm
1125 1.1 alm /* move_lines: move a range of lines */
1126 1.1 alm int
1127 1.1 alm move_lines(addr)
1128 1.1 alm long addr;
1129 1.1 alm {
1130 1.1 alm line_t *b1, *a1, *b2, *a2;
1131 1.1 alm long n = INC_MOD(second_addr, addr_last);
1132 1.1 alm long p = first_addr - 1;
1133 1.1 alm int done = (addr == first_addr - 1 || addr == second_addr);
1134 1.1 alm
1135 1.1 alm SPL1();
1136 1.1 alm if (done) {
1137 1.1 alm a2 = get_addressed_line_node(n);
1138 1.1 alm b2 = get_addressed_line_node(p);
1139 1.1 alm current_addr = second_addr;
1140 1.1 alm } else if (push_undo_stack(UMOV, p, n) == NULL ||
1141 1.1 alm push_undo_stack(UMOV, addr, INC_MOD(addr, addr_last)) == NULL) {
1142 1.1 alm SPL0();
1143 1.1 alm return ERR;
1144 1.1 alm } else {
1145 1.1 alm a1 = get_addressed_line_node(n);
1146 1.1 alm if (addr < first_addr) {
1147 1.1 alm b1 = get_addressed_line_node(p);
1148 1.1 alm b2 = get_addressed_line_node(addr);
1149 1.1 alm /* this get_addressed_line_node last! */
1150 1.1 alm } else {
1151 1.1 alm b2 = get_addressed_line_node(addr);
1152 1.1 alm b1 = get_addressed_line_node(p);
1153 1.1 alm /* this get_addressed_line_node last! */
1154 1.1 alm }
1155 1.1 alm a2 = b2->q_forw;
1156 1.1 alm REQUE(b2, b1->q_forw);
1157 1.1 alm REQUE(a1->q_back, a2);
1158 1.1 alm REQUE(b1, a1);
1159 1.1 alm current_addr = addr + ((addr < first_addr) ?
1160 1.1 alm second_addr - first_addr + 1 : 0);
1161 1.1 alm }
1162 1.1 alm if (isglobal)
1163 1.1 alm unset_active_nodes(b2->q_forw, a2);
1164 1.1 alm modified = 1;
1165 1.1 alm SPL0();
1166 1.1 alm return 0;
1167 1.1 alm }
1168 1.1 alm
1169 1.1 alm
1170 1.1 alm /* copy_lines: copy a range of lines; return status */
1171 1.1 alm int
1172 1.1 alm copy_lines(addr)
1173 1.1 alm long addr;
1174 1.1 alm {
1175 1.1 alm line_t *lp, *np = get_addressed_line_node(first_addr);
1176 1.1 alm undo_t *up = NULL;
1177 1.1 alm long n = second_addr - first_addr + 1;
1178 1.1 alm long m = 0;
1179 1.1 alm
1180 1.1 alm current_addr = addr;
1181 1.1 alm if (first_addr <= addr && addr < second_addr) {
1182 1.1 alm n = addr - first_addr + 1;
1183 1.1 alm m = second_addr - addr;
1184 1.1 alm }
1185 1.1 alm for (; n > 0; n=m, m=0, np = get_addressed_line_node(current_addr + 1))
1186 1.1 alm for (; n-- > 0; np = np->q_forw) {
1187 1.1 alm SPL1();
1188 1.1 alm if ((lp = dup_line_node(np)) == NULL) {
1189 1.1 alm SPL0();
1190 1.1 alm return ERR;
1191 1.1 alm }
1192 1.1 alm add_line_node(lp);
1193 1.1 alm if (up)
1194 1.1 alm up->t = lp;
1195 1.1 alm else if ((up = push_undo_stack(UADD, current_addr,
1196 1.1 alm current_addr)) == NULL) {
1197 1.1 alm SPL0();
1198 1.1 alm return ERR;
1199 1.1 alm }
1200 1.1 alm modified = 1;
1201 1.1 alm SPL0();
1202 1.1 alm }
1203 1.1 alm return 0;
1204 1.1 alm }
1205 1.1 alm
1206 1.1 alm
1207 1.1 alm /* delete_lines: delete a range of lines */
1208 1.1 alm int
1209 1.1 alm delete_lines(from, to)
1210 1.1 alm long from, to;
1211 1.1 alm {
1212 1.1 alm line_t *n, *p;
1213 1.1 alm
1214 1.1 alm SPL1();
1215 1.1 alm if (push_undo_stack(UDEL, from, to) == NULL) {
1216 1.1 alm SPL0();
1217 1.1 alm return ERR;
1218 1.1 alm }
1219 1.1 alm n = get_addressed_line_node(INC_MOD(to, addr_last));
1220 1.1 alm p = get_addressed_line_node(from - 1);
1221 1.1 alm /* this get_addressed_line_node last! */
1222 1.1 alm if (isglobal)
1223 1.1 alm unset_active_nodes(p->q_forw, n);
1224 1.1 alm REQUE(p, n);
1225 1.1 alm addr_last -= to - from + 1;
1226 1.1 alm current_addr = from - 1;
1227 1.1 alm modified = 1;
1228 1.1 alm SPL0();
1229 1.1 alm return 0;
1230 1.1 alm }
1231 1.1 alm
1232 1.1 alm
1233 1.1 alm /* display_lines: print a range of lines to stdout */
1234 1.1 alm int
1235 1.1 alm display_lines(from, to, gflag)
1236 1.1 alm long from;
1237 1.1 alm long to;
1238 1.1 alm int gflag;
1239 1.1 alm {
1240 1.1 alm line_t *bp;
1241 1.1 alm line_t *ep;
1242 1.1 alm char *s;
1243 1.1 alm
1244 1.1 alm if (!from) {
1245 1.1 alm sprintf(errmsg, "invalid address");
1246 1.1 alm return ERR;
1247 1.1 alm }
1248 1.1 alm ep = get_addressed_line_node(INC_MOD(to, addr_last));
1249 1.1 alm bp = get_addressed_line_node(from);
1250 1.1 alm for (; bp != ep; bp = bp->q_forw) {
1251 1.1 alm if ((s = get_sbuf_line(bp)) == NULL)
1252 1.1 alm return ERR;
1253 1.1 alm if (put_tty_line(s, bp->len, current_addr = from++, gflag) < 0)
1254 1.1 alm return ERR;
1255 1.1 alm }
1256 1.1 alm return 0;
1257 1.1 alm }
1258 1.1 alm
1259 1.1 alm
1260 1.1 alm #define MAXMARK 26 /* max number of marks */
1261 1.1 alm
1262 1.1 alm line_t *mark[MAXMARK]; /* line markers */
1263 1.1 alm int markno; /* line marker count */
1264 1.1 alm
1265 1.1 alm /* mark_line_node: set a line node mark */
1266 1.1 alm int
1267 1.1 alm mark_line_node(lp, n)
1268 1.1 alm line_t *lp;
1269 1.1 alm int n;
1270 1.1 alm {
1271 1.1 alm if (!islower(n)) {
1272 1.1 alm sprintf(errmsg, "invalid mark character");
1273 1.1 alm return ERR;
1274 1.1 alm } else if (mark[n - 'a'] == NULL)
1275 1.1 alm markno++;
1276 1.1 alm mark[n - 'a'] = lp;
1277 1.1 alm return 0;
1278 1.1 alm }
1279 1.1 alm
1280 1.1 alm
1281 1.1 alm /* get_marked_node_addr: return address of a marked line */
1282 1.1 alm long
1283 1.1 alm get_marked_node_addr(n)
1284 1.1 alm int n;
1285 1.1 alm {
1286 1.1 alm if (!islower(n)) {
1287 1.1 alm sprintf(errmsg, "invalid mark character");
1288 1.1 alm return ERR;
1289 1.1 alm }
1290 1.1 alm return get_line_node_addr(mark[n - 'a']);
1291 1.1 alm }
1292 1.1 alm
1293 1.1 alm
1294 1.1 alm /* unmark_line_node: clear line node mark */
1295 1.1 alm void
1296 1.1 alm unmark_line_node(lp)
1297 1.1 alm line_t *lp;
1298 1.1 alm {
1299 1.1 alm int i;
1300 1.1 alm
1301 1.1 alm for (i = 0; markno && i < MAXMARK; i++)
1302 1.1 alm if (mark[i] == lp) {
1303 1.1 alm mark[i] = NULL;
1304 1.1 alm markno--;
1305 1.1 alm }
1306 1.1 alm }
1307 1.1 alm
1308 1.1 alm
1309 1.1 alm /* dup_line_node: return a pointer to a copy of a line node */
1310 1.1 alm line_t *
1311 1.1 alm dup_line_node(lp)
1312 1.1 alm line_t *lp;
1313 1.1 alm {
1314 1.1 alm line_t *np;
1315 1.1 alm
1316 1.1 alm if ((np = (line_t *) malloc(sizeof(line_t))) == NULL) {
1317 1.1 alm fprintf(stderr, "%s\n", strerror(errno));
1318 1.1 alm sprintf(errmsg, "out of memory");
1319 1.1 alm return NULL;
1320 1.1 alm }
1321 1.1 alm np->seek = lp->seek;
1322 1.1 alm np->len = lp->len;
1323 1.1 alm return np;
1324 1.1 alm }
1325 1.1 alm
1326 1.1 alm
1327 1.1 alm /* has_trailing_escape: return the parity of escapes preceding a character
1328 1.1 alm in a string */
1329 1.1 alm int
1330 1.1 alm has_trailing_escape(s, t)
1331 1.1 alm char *s;
1332 1.1 alm char *t;
1333 1.1 alm {
1334 1.1 alm return (s == t || *(t - 1) != '\\') ? 0 : !has_trailing_escape(s, t - 1);
1335 1.1 alm }
1336 1.1 alm
1337 1.1 alm
1338 1.1 alm /* strip_escapes: return copy of escaped string of at most length MAXPATHLEN */
1339 1.1 alm char *
1340 1.1 alm strip_escapes(s)
1341 1.1 alm char *s;
1342 1.1 alm {
1343 1.1 alm static char *file = NULL;
1344 1.1 alm static int filesz = 0;
1345 1.1 alm
1346 1.1 alm int i = 0;
1347 1.1 alm
1348 1.1 alm REALLOC(file, filesz, MAXPATHLEN + 1, NULL);
1349 1.1 alm /* assert: no trailing escape */
1350 1.6 augustss while ((file[i++] = (*s == '\\') != '\0' ? *++s : *s))
1351 1.1 alm s++;
1352 1.1 alm return file;
1353 1.1 alm }
1354 1.1 alm
1355 1.1 alm
1356 1.1 alm void
1357 1.1 alm signal_hup(signo)
1358 1.1 alm int signo;
1359 1.1 alm {
1360 1.1 alm if (mutex)
1361 1.1 alm sigflags |= (1 << (signo - 1));
1362 1.1 alm else handle_hup(signo);
1363 1.1 alm }
1364 1.1 alm
1365 1.1 alm
1366 1.1 alm void
1367 1.1 alm signal_int(signo)
1368 1.1 alm int signo;
1369 1.1 alm {
1370 1.1 alm if (mutex)
1371 1.1 alm sigflags |= (1 << (signo - 1));
1372 1.1 alm else handle_int(signo);
1373 1.1 alm }
1374 1.1 alm
1375 1.1 alm
1376 1.1 alm void
1377 1.1 alm handle_hup(signo)
1378 1.1 alm int signo;
1379 1.1 alm {
1380 1.1 alm char *hup = NULL; /* hup filename */
1381 1.1 alm char *s;
1382 1.1 alm int n;
1383 1.1 alm
1384 1.1 alm if (!sigactive)
1385 1.1 alm quit(1);
1386 1.1 alm sigflags &= ~(1 << (signo - 1));
1387 1.1 alm if (addr_last && write_file("ed.hup", "w", 1, addr_last) < 0 &&
1388 1.1 alm (s = getenv("HOME")) != NULL &&
1389 1.1 alm (n = strlen(s)) + 8 <= MAXPATHLEN && /* "ed.hup" + '/' */
1390 1.1 alm (hup = (char *) malloc(n + 10)) != NULL) {
1391 1.1 alm strcpy(hup, s);
1392 1.1 alm if (hup[n - 1] != '/')
1393 1.1 alm hup[n] = '/', hup[n+1] = '\0';
1394 1.1 alm strcat(hup, "ed.hup");
1395 1.1 alm write_file(hup, "w", 1, addr_last);
1396 1.1 alm }
1397 1.1 alm quit(2);
1398 1.1 alm }
1399 1.1 alm
1400 1.1 alm
1401 1.1 alm void
1402 1.1 alm handle_int(signo)
1403 1.1 alm int signo;
1404 1.1 alm {
1405 1.1 alm if (!sigactive)
1406 1.1 alm quit(1);
1407 1.1 alm sigflags &= ~(1 << (signo - 1));
1408 1.1 alm #ifdef _POSIX_SOURCE
1409 1.1 alm siglongjmp(env, -1);
1410 1.1 alm #else
1411 1.1 alm longjmp(env, -1);
1412 1.1 alm #endif
1413 1.1 alm }
1414 1.1 alm
1415 1.1 alm
1416 1.1 alm int cols = 72; /* wrap column */
1417 1.1 alm
1418 1.1 alm void
1419 1.1 alm handle_winch(signo)
1420 1.1 alm int signo;
1421 1.1 alm {
1422 1.1 alm struct winsize ws; /* window size structure */
1423 1.1 alm
1424 1.1 alm sigflags &= ~(1 << (signo - 1));
1425 1.1 alm if (ioctl(0, TIOCGWINSZ, (char *) &ws) >= 0) {
1426 1.1 alm if (ws.ws_row > 2) rows = ws.ws_row - 2;
1427 1.1 alm if (ws.ws_col > 8) cols = ws.ws_col - 8;
1428 1.1 alm }
1429 1.1 alm }
1430 1.1 alm
1431 1.1 alm
1432 1.1 alm /* is_legal_filename: return a legal filename */
1433 1.1 alm int
1434 1.1 alm is_legal_filename(s)
1435 1.1 alm char *s;
1436 1.1 alm {
1437 1.1 alm if (red && (*s == '!' || !strcmp(s, "..") || strchr(s, '/'))) {
1438 1.1 alm sprintf(errmsg, "shell access restricted");
1439 1.1 alm return 0;
1440 1.1 alm }
1441 1.1 alm return 1;
1442 1.1 alm }
1443