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