Deleted Added
sdiff udiff text old ( 81220 ) new ( 90109 )
full compact
1/* main.c: This file contains the main control and user-interface routines
2 for the ed line editor. */
3/*-
4 * Copyright (c) 1993 Andrew Moore, Talke Studio.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 20 unchanged lines hidden (view full) ---

29#ifndef lint
30static const char copyright[] =
31"@(#) Copyright (c) 1993 Andrew Moore, Talke Studio. \n\
32 All rights reserved.\n";
33#endif /* not lint */
34
35#ifndef lint
36static const char rcsid[] =
37 "$FreeBSD: head/bin/ed/main.c 90109 2002-02-02 06:36:49Z imp $";
38#endif /* not lint */
39
40/*
41 * CREDITS
42 *
43 * This program is based on the editor algorithm described in
44 * Brian W. Kernighan and P. J. Plauger's book "Software Tools
45 * in Pascal," Addison-Wesley, 1981.

--- 52 unchanged lines hidden (view full) ---

98int lineno; /* script line number */
99const char *prompt; /* command-line prompt */
100const char *dps = "*"; /* default command-line prompt */
101
102const char usage[] = "usage: %s [-] [-sx] [-p string] [name]\n";
103
104/* ed: line editor */
105int
106main(int argc, char *argv[])
107{
108 int c, n;
109 long status = 0;
110#if __GNUC__
111 /* Avoid longjmp clobbering */
112 (void) &argc;
113 (void) &argv;
114#endif

--- 144 unchanged lines hidden (view full) ---

259 /*NOTREACHED*/
260}
261
262long first_addr, second_addr, addr_cnt;
263
264/* extract_addr_range: get line addresses from the command buffer until an
265 illegal address is seen; return status */
266int
267extract_addr_range(void)
268{
269 long addr;
270
271 addr_cnt = 0;
272 first_addr = second_addr = current_addr;
273 while ((addr = next_addr()) >= 0) {
274 addr_cnt++;
275 first_addr = second_addr;

--- 15 unchanged lines hidden (view full) ---

291 if (!first) { \
292 errmsg = "invalid address"; \
293 return ERR; \
294 } \
295} while (0);
296
297/* next_addr: return the next line address in the command buffer */
298long
299next_addr(void)
300{
301 const char *hd;
302 long addr = current_addr;
303 long n;
304 int first = 1;
305 int c;
306
307 SKIP_BLANKS();

--- 134 unchanged lines hidden (view full) ---

442
443int patlock = 0; /* if set, pattern not freed by get_compiled_pattern() */
444
445long rows = 22; /* scroll length: ws_row - 2 */
446
447/* exec_command: execute the next command in command buffer; return print
448 request, if any */
449int
450exec_command(void)
451{
452 extern long u_current_addr;
453 extern long u_addr_last;
454
455 static pattern_t *pat = NULL;
456 static int sgflag = 0;
457 static long sgnum = 0;
458

--- 417 unchanged lines hidden (view full) ---

876 return ERR;
877 }
878 return gflag;
879}
880
881
882/* check_addr_range: return status of address range check */
883int
884check_addr_range(long n, long m)
885{
886 if (addr_cnt == 0) {
887 first_addr = n;
888 second_addr = m;
889 }
890 if (first_addr > second_addr || 1 > first_addr ||
891 second_addr > addr_last) {
892 errmsg = "invalid address";
893 return ERR;
894 }
895 return 0;
896}
897
898
899/* get_matching_node_addr: return the address of the next line matching a
900 pattern in a given direction. wrap around begin/end of editor buffer if
901 necessary */
902long
903get_matching_node_addr(pattern_t *pat, int dir)
904{
905 char *s;
906 long n = current_addr;
907 line_t *lp;
908
909 if (!pat) return ERR;
910 do {
911 if ((n = dir ? INC_MOD(n, addr_last) : DEC_MOD(n, addr_last))) {

--- 8 unchanged lines hidden (view full) ---

920 } while (n != current_addr);
921 errmsg = "no match";
922 return ERR;
923}
924
925
926/* get_filename: return pointer to copy of filename in the command buffer */
927char *
928get_filename(void)
929{
930 static char *file = NULL;
931 static int filesz = 0;
932
933 int n;
934
935 if (*ibufp != '\n') {
936 SKIP_BLANKS();

--- 26 unchanged lines hidden (view full) ---

963 file[n] = '\0';
964 return is_legal_filename(file) ? file : NULL;
965}
966
967
968/* get_shell_command: read a shell command from stdin; return substitution
969 status */
970int
971get_shell_command(void)
972{
973 static char *buf = NULL;
974 static int n = 0;
975
976 char *s; /* substitution char pointer */
977 int i = 0;
978 int j = 0;
979

--- 49 unchanged lines hidden (view full) ---

1029 shcmd[shcmdi = i] = '\0';
1030 return *s == '!' || *s == '%';
1031}
1032
1033
1034/* append_lines: insert text from stdin to after line n; stop when either a
1035 single period is read or EOF; return status */
1036int
1037append_lines(long n)
1038{
1039 int l;
1040 const char *lp = ibuf;
1041 const char *eot;
1042 undo_t *up = NULL;
1043
1044 for (current_addr = n;;) {
1045 if (!isglobal) {

--- 32 unchanged lines hidden (view full) ---

1078 SPL0();
1079 }
1080 /* NOTREACHED */
1081}
1082
1083
1084/* join_lines: replace a range of lines with the joined text of those lines */
1085int
1086join_lines(long from, long to)
1087{
1088 static char *buf = NULL;
1089 static int n;
1090
1091 char *s;
1092 int size = 0;
1093 line_t *bp, *ep;
1094

--- 20 unchanged lines hidden (view full) ---

1115 modified = 1;
1116 SPL0();
1117 return 0;
1118}
1119
1120
1121/* move_lines: move a range of lines */
1122int
1123move_lines(long addr)
1124{
1125 line_t *b1, *a1, *b2, *a2;
1126 long n = INC_MOD(second_addr, addr_last);
1127 long p = first_addr - 1;
1128 int done = (addr == first_addr - 1 || addr == second_addr);
1129
1130 SPL1();
1131 if (done) {

--- 27 unchanged lines hidden (view full) ---

1159 modified = 1;
1160 SPL0();
1161 return 0;
1162}
1163
1164
1165/* copy_lines: copy a range of lines; return status */
1166int
1167copy_lines(long addr)
1168{
1169 line_t *lp, *np = get_addressed_line_node(first_addr);
1170 undo_t *up = NULL;
1171 long n = second_addr - first_addr + 1;
1172 long m = 0;
1173
1174 current_addr = addr;
1175 if (first_addr <= addr && addr < second_addr) {

--- 19 unchanged lines hidden (view full) ---

1195 SPL0();
1196 }
1197 return 0;
1198}
1199
1200
1201/* delete_lines: delete a range of lines */
1202int
1203delete_lines(long from, long to)
1204{
1205 line_t *n, *p;
1206
1207 SPL1();
1208 if (push_undo_stack(UDEL, from, to) == NULL) {
1209 SPL0();
1210 return ERR;
1211 }

--- 8 unchanged lines hidden (view full) ---

1220 modified = 1;
1221 SPL0();
1222 return 0;
1223}
1224
1225
1226/* display_lines: print a range of lines to stdout */
1227int
1228display_lines(long from, long to, int gflag)
1229{
1230 line_t *bp;
1231 line_t *ep;
1232 char *s;
1233
1234 if (!from) {
1235 errmsg = "invalid address";
1236 return ERR;

--- 12 unchanged lines hidden (view full) ---

1249
1250#define MAXMARK 26 /* max number of marks */
1251
1252line_t *mark[MAXMARK]; /* line markers */
1253int markno; /* line marker count */
1254
1255/* mark_line_node: set a line node mark */
1256int
1257mark_line_node(line_t *lp, int n)
1258{
1259 if (!islower((unsigned char)n)) {
1260 errmsg = "invalid mark character";
1261 return ERR;
1262 } else if (mark[n - 'a'] == NULL)
1263 markno++;
1264 mark[n - 'a'] = lp;
1265 return 0;
1266}
1267
1268
1269/* get_marked_node_addr: return address of a marked line */
1270long
1271get_marked_node_addr(int n)
1272{
1273 if (!islower((unsigned char)n)) {
1274 errmsg = "invalid mark character";
1275 return ERR;
1276 }
1277 return get_line_node_addr(mark[n - 'a']);
1278}
1279
1280
1281/* unmark_line_node: clear line node mark */
1282void
1283unmark_line_node(line_t *lp)
1284{
1285 int i;
1286
1287 for (i = 0; markno && i < MAXMARK; i++)
1288 if (mark[i] == lp) {
1289 mark[i] = NULL;
1290 markno--;
1291 }
1292}
1293
1294
1295/* dup_line_node: return a pointer to a copy of a line node */
1296line_t *
1297dup_line_node(line_t *lp)
1298{
1299 line_t *np;
1300
1301 if ((np = (line_t *) malloc(sizeof(line_t))) == NULL) {
1302 fprintf(stderr, "%s\n", strerror(errno));
1303 errmsg = "out of memory";
1304 return NULL;
1305 }
1306 np->seek = lp->seek;
1307 np->len = lp->len;
1308 return np;
1309}
1310
1311
1312/* has_trailing_escape: return the parity of escapes preceding a character
1313 in a string */
1314int
1315has_trailing_escape(char *s, char *t)
1316{
1317 return (s == t || *(t - 1) != '\\') ? 0 : !has_trailing_escape(s, t - 1);
1318}
1319
1320
1321/* strip_escapes: return copy of escaped string of at most length PATH_MAX */
1322char *
1323strip_escapes(char *s)
1324{
1325 static char *file = NULL;
1326 static int filesz = 0;
1327
1328 int i = 0;
1329
1330 REALLOC(file, filesz, PATH_MAX, NULL);
1331 while (i < filesz - 1 /* Worry about a possible trailing escape */
1332 && (file[i++] = (*s == '\\') ? *++s : *s))
1333 s++;
1334 return file;
1335}
1336
1337
1338void
1339signal_hup(int signo)
1340{
1341 if (mutex)
1342 sigflags |= (1 << (signo - 1));
1343 else
1344 handle_hup(signo);
1345}
1346
1347
1348void
1349signal_int(int signo)
1350{
1351 if (mutex)
1352 sigflags |= (1 << (signo - 1));
1353 else
1354 handle_int(signo);
1355}
1356
1357
1358void
1359handle_hup(int signo)
1360{
1361 char *hup = NULL; /* hup filename */
1362 char *s;
1363 char ed_hup[] = "ed.hup";
1364 int n;
1365
1366 if (!sigactive)
1367 quit(1);

--- 8 unchanged lines hidden (view full) ---

1376 strcat(hup, "ed.hup");
1377 write_file(hup, "w", 1, addr_last);
1378 }
1379 quit(2);
1380}
1381
1382
1383void
1384handle_int(int signo)
1385{
1386 if (!sigactive)
1387 quit(1);
1388 sigflags &= ~(1 << (signo - 1));
1389#ifdef _POSIX_SOURCE
1390 siglongjmp(env, -1);
1391#else
1392 longjmp(env, -1);
1393#endif
1394}
1395
1396
1397int cols = 72; /* wrap column */
1398
1399void
1400handle_winch(int signo)
1401{
1402 int save_errno = errno;
1403
1404 struct winsize ws; /* window size structure */
1405
1406 sigflags &= ~(1 << (signo - 1));
1407 if (ioctl(0, TIOCGWINSZ, (char *) &ws) >= 0) {
1408 if (ws.ws_row > 2) rows = ws.ws_row - 2;
1409 if (ws.ws_col > 8) cols = ws.ws_col - 8;
1410 }
1411 errno = save_errno;
1412}
1413
1414
1415/* is_legal_filename: return a legal filename */
1416int
1417is_legal_filename(char *s)
1418{
1419 if (red && (*s == '!' || !strcmp(s, "..") || strchr(s, '/'))) {
1420 errmsg = "shell access restricted";
1421 return 0;
1422 }
1423 return 1;
1424}