Deleted Added
full compact
main.c (81220) main.c (90109)
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[] =
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 81220 2001-08-06 22:01:31Z mike $";
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
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(argc, argv)
107 int argc;
108 char **argv;
106main(int argc, char *argv[])
109{
110 int c, n;
111 long status = 0;
112#if __GNUC__
113 /* Avoid longjmp clobbering */
114 (void) &argc;
115 (void) &argv;
116#endif

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

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

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

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

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

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

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

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

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

925 } while (n != current_addr);
926 errmsg = "no match";
927 return ERR;
928}
929
930
931/* get_filename: return pointer to copy of filename in the command buffer */
932char *
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 *
933get_filename()
928get_filename(void)
934{
935 static char *file = NULL;
936 static int filesz = 0;
937
938 int n;
939
940 if (*ibufp != '\n') {
941 SKIP_BLANKS();

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

968 file[n] = '\0';
969 return is_legal_filename(file) ? file : NULL;
970}
971
972
973/* get_shell_command: read a shell command from stdin; return substitution
974 status */
975int
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
976get_shell_command()
971get_shell_command(void)
977{
978 static char *buf = NULL;
979 static int n = 0;
980
981 char *s; /* substitution char pointer */
982 int i = 0;
983 int j = 0;
984

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

1034 shcmd[shcmdi = i] = '\0';
1035 return *s == '!' || *s == '%';
1036}
1037
1038
1039/* append_lines: insert text from stdin to after line n; stop when either a
1040 single period is read or EOF; return status */
1041int
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
1042append_lines(n)
1043 long n;
1037append_lines(long n)
1044{
1045 int l;
1046 const char *lp = ibuf;
1047 const char *eot;
1048 undo_t *up = NULL;
1049
1050 for (current_addr = n;;) {
1051 if (!isglobal) {

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

1084 SPL0();
1085 }
1086 /* NOTREACHED */
1087}
1088
1089
1090/* join_lines: replace a range of lines with the joined text of those lines */
1091int
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
1092join_lines(from, to)
1093 long from;
1094 long to;
1086join_lines(long from, long to)
1095{
1096 static char *buf = NULL;
1097 static int n;
1098
1099 char *s;
1100 int size = 0;
1101 line_t *bp, *ep;
1102

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

1123 modified = 1;
1124 SPL0();
1125 return 0;
1126}
1127
1128
1129/* move_lines: move a range of lines */
1130int
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
1131move_lines(addr)
1132 long addr;
1123move_lines(long addr)
1133{
1134 line_t *b1, *a1, *b2, *a2;
1135 long n = INC_MOD(second_addr, addr_last);
1136 long p = first_addr - 1;
1137 int done = (addr == first_addr - 1 || addr == second_addr);
1138
1139 SPL1();
1140 if (done) {

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

1168 modified = 1;
1169 SPL0();
1170 return 0;
1171}
1172
1173
1174/* copy_lines: copy a range of lines; return status */
1175int
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
1176copy_lines(addr)
1177 long addr;
1167copy_lines(long addr)
1178{
1179 line_t *lp, *np = get_addressed_line_node(first_addr);
1180 undo_t *up = NULL;
1181 long n = second_addr - first_addr + 1;
1182 long m = 0;
1183
1184 current_addr = addr;
1185 if (first_addr <= addr && addr < second_addr) {

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

1205 SPL0();
1206 }
1207 return 0;
1208}
1209
1210
1211/* delete_lines: delete a range of lines */
1212int
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
1213delete_lines(from, to)
1214 long from, to;
1203delete_lines(long from, long to)
1215{
1216 line_t *n, *p;
1217
1218 SPL1();
1219 if (push_undo_stack(UDEL, from, to) == NULL) {
1220 SPL0();
1221 return ERR;
1222 }

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

1231 modified = 1;
1232 SPL0();
1233 return 0;
1234}
1235
1236
1237/* display_lines: print a range of lines to stdout */
1238int
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
1239display_lines(from, to, gflag)
1240 long from;
1241 long to;
1242 int gflag;
1228display_lines(long from, long to, int gflag)
1243{
1244 line_t *bp;
1245 line_t *ep;
1246 char *s;
1247
1248 if (!from) {
1249 errmsg = "invalid address";
1250 return ERR;

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

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

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

1401 strcat(hup, "ed.hup");
1402 write_file(hup, "w", 1, addr_last);
1403 }
1404 quit(2);
1405}
1406
1407
1408void
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
1409handle_int(signo)
1410 int signo;
1384handle_int(int signo)
1411{
1412 if (!sigactive)
1413 quit(1);
1414 sigflags &= ~(1 << (signo - 1));
1415#ifdef _POSIX_SOURCE
1416 siglongjmp(env, -1);
1417#else
1418 longjmp(env, -1);
1419#endif
1420}
1421
1422
1423int cols = 72; /* wrap column */
1424
1425void
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
1426handle_winch(signo)
1427 int signo;
1400handle_winch(int signo)
1428{
1429 int save_errno = errno;
1430
1431 struct winsize ws; /* window size structure */
1432
1433 sigflags &= ~(1 << (signo - 1));
1434 if (ioctl(0, TIOCGWINSZ, (char *) &ws) >= 0) {
1435 if (ws.ws_row > 2) rows = ws.ws_row - 2;
1436 if (ws.ws_col > 8) cols = ws.ws_col - 8;
1437 }
1438 errno = save_errno;
1439}
1440
1441
1442/* is_legal_filename: return a legal filename */
1443int
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
1444is_legal_filename(s)
1445 char *s;
1417is_legal_filename(char *s)
1446{
1447 if (red && (*s == '!' || !strcmp(s, "..") || strchr(s, '/'))) {
1448 errmsg = "shell access restricted";
1449 return 0;
1450 }
1451 return 1;
1452}
1418{
1419 if (red && (*s == '!' || !strcmp(s, "..") || strchr(s, '/'))) {
1420 errmsg = "shell access restricted";
1421 return 0;
1422 }
1423 return 1;
1424}