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