blob: 516b8d78de5e4a14398f8397c8a6c81fd5a35281 [file] [log] [blame]
Bernhard Reutner-Fischerd9ed35c2006-05-19 12:38:30 +00001/* vi: set sw=4 ts=4: */
Rob Landley3b890392006-05-04 20:56:43 +00002/*
3 * Copyright (c) 2002 by David I. Bell
4 * Permission is granted to use, distribute, or modify this source,
5 * provided that this copyright notice remains intact.
6 *
7 * The "ed" built-in command (much simplified)
8 */
9
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Rob Landley3b890392006-05-04 20:56:43 +000011
Denis Vlasenkod9391b12007-09-25 11:55:57 +000012typedef struct LINE {
13 struct LINE *next;
14 struct LINE *prev;
15 int len;
16 char data[1];
17} LINE;
18
19
Denis Vlasenko74324c82007-06-04 10:16:52 +000020#define searchString bb_common_bufsiz1
21
Denis Vlasenko55f30b02007-03-24 22:42:29 +000022enum {
Denis Vlasenko74324c82007-06-04 10:16:52 +000023 USERSIZE = sizeof(searchString) > 1024 ? 1024
24 : sizeof(searchString) - 1, /* max line length typed in by user */
Denis Vlasenko55f30b02007-03-24 22:42:29 +000025 INITBUF_SIZE = 1024, /* initial buffer size */
26};
27
Denis Vlasenkod9391b12007-09-25 11:55:57 +000028struct globals {
29 int curNum;
30 int lastNum;
31 int bufUsed;
32 int bufSize;
33 LINE *curLine;
34 char *bufBase;
35 char *bufPtr;
36 char *fileName;
37 LINE lines;
38 smallint dirty;
39 int marks[26];
40};
41#define G (*ptr_to_globals)
42#define curLine (G.curLine )
43#define bufBase (G.bufBase )
44#define bufPtr (G.bufPtr )
45#define fileName (G.fileName )
46#define curNum (G.curNum )
47#define lastNum (G.lastNum )
48#define bufUsed (G.bufUsed )
49#define bufSize (G.bufSize )
50#define dirty (G.dirty )
51#define lines (G.lines )
52#define marks (G.marks )
53#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +000054 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkod9391b12007-09-25 11:55:57 +000055} while (0)
Rob Landley3b890392006-05-04 20:56:43 +000056
Rob Landley3b890392006-05-04 20:56:43 +000057
58static void doCommands(void);
59static void subCommand(const char *cmd, int num1, int num2);
Denis Vlasenkod9391b12007-09-25 11:55:57 +000060static int getNum(const char **retcp, smallint *retHaveNum, int *retNum);
Rob Landley3b890392006-05-04 20:56:43 +000061static int setCurNum(int num);
Rob Landley3b890392006-05-04 20:56:43 +000062static void addLines(int num);
63static int insertLine(int num, const char *data, int len);
Denis Vlasenkod9391b12007-09-25 11:55:57 +000064static void deleteLines(int num1, int num2);
Rob Landley3b890392006-05-04 20:56:43 +000065static int printLines(int num1, int num2, int expandFlag);
66static int writeLines(const char *file, int num1, int num2);
67static int readLines(const char *file, int num);
68static int searchLines(const char *str, int num1, int num2);
69static LINE *findLine(int num);
Rob Landley3b890392006-05-04 20:56:43 +000070static int findString(const LINE *lp, const char * str, int len, int offset);
71
Denis Vlasenkod9391b12007-09-25 11:55:57 +000072
73static int bad_nums(int num1, int num2, const char *for_what)
74{
75 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
76 bb_error_msg("bad line range for %s", for_what);
77 return 1;
78 }
79 return 0;
80}
81
82
83static char *skip_blank(const char *cp)
84{
Denis Vlasenkod9391b12007-09-25 11:55:57 +000085 while (isblank(*cp))
86 cp++;
87 return (char *)cp;
88}
89
90
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000091int ed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000092int ed_main(int argc UNUSED_PARAM, char **argv)
Rob Landley3b890392006-05-04 20:56:43 +000093{
Denis Vlasenkod9391b12007-09-25 11:55:57 +000094 INIT_G();
95
96 bufSize = INITBUF_SIZE;
97 bufBase = xmalloc(bufSize);
98 bufPtr = bufBase;
99 lines.next = &lines;
100 lines.prev = &lines;
Rob Landley3b890392006-05-04 20:56:43 +0000101
Denis Vlasenko1d426652008-03-17 09:09:09 +0000102 if (argv[1]) {
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000103 fileName = xstrdup(argv[1]);
Rob Landley3b890392006-05-04 20:56:43 +0000104 if (!readLines(fileName, 1)) {
Rob Landley3b890392006-05-04 20:56:43 +0000105 return EXIT_SUCCESS;
106 }
Rob Landley3b890392006-05-04 20:56:43 +0000107 if (lastNum)
108 setCurNum(1);
Rob Landley3b890392006-05-04 20:56:43 +0000109 dirty = FALSE;
110 }
111
112 doCommands();
Rob Landley3b890392006-05-04 20:56:43 +0000113 return EXIT_SUCCESS;
114}
115
116/*
117 * Read commands until we are told to stop.
118 */
119static void doCommands(void)
120{
121 const char *cp;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000122 char *endbuf, buf[USERSIZE];
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000123 int len, num1, num2;
124 smallint have1, have2;
Rob Landley3b890392006-05-04 20:56:43 +0000125
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000126 while (TRUE) {
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000127 /* Returns:
128 * -1 on read errors or EOF, or on bare Ctrl-D.
129 * 0 on ctrl-C,
130 * >0 length of input string, including terminating '\n'
131 */
132 len = read_line_input(": ", buf, sizeof(buf), NULL);
133 if (len <= 0)
Rob Landley3b890392006-05-04 20:56:43 +0000134 return;
Rob Landley3b890392006-05-04 20:56:43 +0000135 endbuf = &buf[len - 1];
Rob Landley3b890392006-05-04 20:56:43 +0000136 while ((endbuf > buf) && isblank(endbuf[-1]))
137 endbuf--;
Rob Landley3b890392006-05-04 20:56:43 +0000138 *endbuf = '\0';
139
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000140 cp = skip_blank(buf);
Rob Landley3b890392006-05-04 20:56:43 +0000141 have1 = FALSE;
142 have2 = FALSE;
143
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000144 if ((curNum == 0) && (lastNum > 0)) {
Rob Landley3b890392006-05-04 20:56:43 +0000145 curNum = 1;
146 curLine = lines.next;
147 }
148
149 if (!getNum(&cp, &have1, &num1))
150 continue;
151
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000152 cp = skip_blank(cp);
Rob Landley3b890392006-05-04 20:56:43 +0000153
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000154 if (*cp == ',') {
Rob Landley3b890392006-05-04 20:56:43 +0000155 cp++;
Rob Landley3b890392006-05-04 20:56:43 +0000156 if (!getNum(&cp, &have2, &num2))
157 continue;
Rob Landley3b890392006-05-04 20:56:43 +0000158 if (!have1)
159 num1 = 1;
Rob Landley3b890392006-05-04 20:56:43 +0000160 if (!have2)
161 num2 = lastNum;
Rob Landley3b890392006-05-04 20:56:43 +0000162 have1 = TRUE;
163 have2 = TRUE;
164 }
Rob Landley3b890392006-05-04 20:56:43 +0000165 if (!have1)
166 num1 = curNum;
Rob Landley3b890392006-05-04 20:56:43 +0000167 if (!have2)
168 num2 = num1;
169
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000170 switch (*cp++) {
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000171 case 'a':
172 addLines(num1 + 1);
173 break;
Rob Landley3b890392006-05-04 20:56:43 +0000174
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000175 case 'c':
176 deleteLines(num1, num2);
177 addLines(num1);
178 break;
Rob Landley3b890392006-05-04 20:56:43 +0000179
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000180 case 'd':
181 deleteLines(num1, num2);
182 break;
Rob Landley3b890392006-05-04 20:56:43 +0000183
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000184 case 'f':
185 if (*cp && !isblank(*cp)) {
186 bb_error_msg("bad file command");
Rob Landley3b890392006-05-04 20:56:43 +0000187 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000188 }
189 cp = skip_blank(cp);
190 if (*cp == '\0') {
191 if (fileName)
192 printf("\"%s\"\n", fileName);
193 else
194 printf("No file name\n");
Rob Landley3b890392006-05-04 20:56:43 +0000195 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000196 }
197 free(fileName);
198 fileName = xstrdup(cp);
199 break;
Rob Landley3b890392006-05-04 20:56:43 +0000200
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000201 case 'i':
202 addLines(num1);
203 break;
204
205 case 'k':
206 cp = skip_blank(cp);
207 if ((*cp < 'a') || (*cp > 'z') || cp[1]) {
208 bb_error_msg("bad mark name");
Rob Landley3b890392006-05-04 20:56:43 +0000209 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000210 }
211 marks[*cp - 'a'] = num2;
212 break;
Rob Landley3b890392006-05-04 20:56:43 +0000213
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000214 case 'l':
215 printLines(num1, num2, TRUE);
216 break;
217
218 case 'p':
219 printLines(num1, num2, FALSE);
220 break;
221
222 case 'q':
223 cp = skip_blank(cp);
224 if (have1 || *cp) {
225 bb_error_msg("bad quit command");
Rob Landley3b890392006-05-04 20:56:43 +0000226 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000227 }
228 if (!dirty)
229 return;
230 len = read_line_input("Really quit? ", buf, 16, NULL);
231 /* read error/EOF - no way to continue */
232 if (len < 0)
233 return;
234 cp = skip_blank(buf);
235 if ((*cp | 0x20) == 'y') /* Y or y */
236 return;
237 break;
Rob Landley3b890392006-05-04 20:56:43 +0000238
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000239 case 'r':
240 if (*cp && !isblank(*cp)) {
241 bb_error_msg("bad read command");
Rob Landley3b890392006-05-04 20:56:43 +0000242 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000243 }
244 cp = skip_blank(cp);
245 if (*cp == '\0') {
246 bb_error_msg("no file name");
Rob Landley3b890392006-05-04 20:56:43 +0000247 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000248 }
249 if (!have1)
250 num1 = lastNum;
251 if (readLines(cp, num1 + 1))
Rob Landley3b890392006-05-04 20:56:43 +0000252 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000253 if (fileName == NULL)
254 fileName = xstrdup(cp);
255 break;
Rob Landley3b890392006-05-04 20:56:43 +0000256
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000257 case 's':
258 subCommand(cp, num1, num2);
259 break;
260
261 case 'w':
262 if (*cp && !isblank(*cp)) {
263 bb_error_msg("bad write command");
Rob Landley3b890392006-05-04 20:56:43 +0000264 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000265 }
266 cp = skip_blank(cp);
267 if (!have1) {
268 num1 = 1;
269 num2 = lastNum;
270 }
271 if (*cp == '\0')
272 cp = fileName;
273 if (cp == NULL) {
274 bb_error_msg("no file name specified");
Rob Landley3b890392006-05-04 20:56:43 +0000275 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000276 }
277 writeLines(cp, num1, num2);
278 break;
Rob Landley3b890392006-05-04 20:56:43 +0000279
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000280 case 'z':
281 switch (*cp) {
Rob Landley3b890392006-05-04 20:56:43 +0000282 case '-':
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000283 printLines(curNum - 21, curNum, FALSE);
Rob Landley3b890392006-05-04 20:56:43 +0000284 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000285 case '.':
286 printLines(curNum - 11, curNum + 10, FALSE);
Rob Landley3b890392006-05-04 20:56:43 +0000287 break;
Rob Landley3b890392006-05-04 20:56:43 +0000288 default:
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000289 printLines(curNum, curNum + 21, FALSE);
Rob Landley3b890392006-05-04 20:56:43 +0000290 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000291 }
292 break;
293
294 case '.':
295 if (have1) {
296 bb_error_msg("no arguments allowed");
297 break;
298 }
299 printLines(curNum, curNum, FALSE);
300 break;
301
302 case '-':
303 if (setCurNum(curNum - 1))
304 printLines(curNum, curNum, FALSE);
305 break;
306
307 case '=':
308 printf("%d\n", num1);
309 break;
310 case '\0':
311 if (have1) {
312 printLines(num2, num2, FALSE);
313 break;
314 }
315 if (setCurNum(curNum + 1))
316 printLines(curNum, curNum, FALSE);
317 break;
318
319 default:
320 bb_error_msg("unimplemented command");
321 break;
Rob Landley3b890392006-05-04 20:56:43 +0000322 }
323 }
324}
325
326
327/*
328 * Do the substitute command.
329 * The current line is set to the last substitution done.
330 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000331static void subCommand(const char *cmd, int num1, int num2)
Rob Landley3b890392006-05-04 20:56:43 +0000332{
333 char *cp, *oldStr, *newStr, buf[USERSIZE];
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000334 int delim, oldLen, newLen, deltaLen, offset;
Rob Landley3b890392006-05-04 20:56:43 +0000335 LINE *lp, *nlp;
336 int globalFlag, printFlag, didSub, needPrint;
337
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000338 if (bad_nums(num1, num2, "substitute"))
Rob Landley3b890392006-05-04 20:56:43 +0000339 return;
Rob Landley3b890392006-05-04 20:56:43 +0000340
341 globalFlag = FALSE;
342 printFlag = FALSE;
343 didSub = FALSE;
344 needPrint = FALSE;
345
346 /*
347 * Copy the command so we can modify it.
348 */
349 strcpy(buf, cmd);
350 cp = buf;
351
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000352 if (isblank(*cp) || (*cp == '\0')) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000353 bb_error_msg("bad delimiter for substitute");
Rob Landley3b890392006-05-04 20:56:43 +0000354 return;
355 }
356
357 delim = *cp++;
358 oldStr = cp;
359
360 cp = strchr(cp, delim);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000361 if (cp == NULL) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000362 bb_error_msg("missing 2nd delimiter for substitute");
Rob Landley3b890392006-05-04 20:56:43 +0000363 return;
364 }
365
366 *cp++ = '\0';
367
368 newStr = cp;
369 cp = strchr(cp, delim);
370
371 if (cp)
372 *cp++ = '\0';
373 else
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000374 cp = (char*)"";
Rob Landley3b890392006-05-04 20:56:43 +0000375
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000376 while (*cp) switch (*cp++) {
Rob Landley3b890392006-05-04 20:56:43 +0000377 case 'g':
378 globalFlag = TRUE;
379 break;
Rob Landley3b890392006-05-04 20:56:43 +0000380 case 'p':
381 printFlag = TRUE;
382 break;
Rob Landley3b890392006-05-04 20:56:43 +0000383 default:
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000384 bb_error_msg("unknown option for substitute");
Rob Landley3b890392006-05-04 20:56:43 +0000385 return;
386 }
387
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000388 if (*oldStr == '\0') {
389 if (searchString[0] == '\0') {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000390 bb_error_msg("no previous search string");
Rob Landley3b890392006-05-04 20:56:43 +0000391 return;
392 }
Rob Landley3b890392006-05-04 20:56:43 +0000393 oldStr = searchString;
394 }
395
396 if (oldStr != searchString)
397 strcpy(searchString, oldStr);
398
399 lp = findLine(num1);
Rob Landley3b890392006-05-04 20:56:43 +0000400 if (lp == NULL)
401 return;
402
403 oldLen = strlen(oldStr);
404 newLen = strlen(newStr);
405 deltaLen = newLen - oldLen;
406 offset = 0;
407 nlp = NULL;
408
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000409 while (num1 <= num2) {
Rob Landley3b890392006-05-04 20:56:43 +0000410 offset = findString(lp, oldStr, oldLen, offset);
411
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000412 if (offset < 0) {
413 if (needPrint) {
Rob Landley3b890392006-05-04 20:56:43 +0000414 printLines(num1, num1, FALSE);
415 needPrint = FALSE;
416 }
Rob Landley3b890392006-05-04 20:56:43 +0000417 offset = 0;
418 lp = lp->next;
419 num1++;
Rob Landley3b890392006-05-04 20:56:43 +0000420 continue;
421 }
422
423 needPrint = printFlag;
424 didSub = TRUE;
425 dirty = TRUE;
426
427 /*
428 * If the replacement string is the same size or shorter
429 * than the old string, then the substitution is easy.
430 */
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000431 if (deltaLen <= 0) {
Rob Landley3b890392006-05-04 20:56:43 +0000432 memcpy(&lp->data[offset], newStr, newLen);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000433 if (deltaLen) {
Rob Landley3b890392006-05-04 20:56:43 +0000434 memcpy(&lp->data[offset + newLen],
435 &lp->data[offset + oldLen],
436 lp->len - offset - oldLen);
437
438 lp->len += deltaLen;
439 }
Rob Landley3b890392006-05-04 20:56:43 +0000440 offset += newLen;
Rob Landley3b890392006-05-04 20:56:43 +0000441 if (globalFlag)
442 continue;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000443 if (needPrint) {
Rob Landley3b890392006-05-04 20:56:43 +0000444 printLines(num1, num1, FALSE);
445 needPrint = FALSE;
446 }
Rob Landley3b890392006-05-04 20:56:43 +0000447 lp = lp->next;
448 num1++;
Rob Landley3b890392006-05-04 20:56:43 +0000449 continue;
450 }
451
452 /*
453 * The new string is larger, so allocate a new line
454 * structure and use that. Link it in in place of
455 * the old line structure.
456 */
Denys Vlasenko90377872010-01-08 09:07:50 +0100457 nlp = xmalloc(sizeof(LINE) + lp->len + deltaLen);
Rob Landley3b890392006-05-04 20:56:43 +0000458
459 nlp->len = lp->len + deltaLen;
460
461 memcpy(nlp->data, lp->data, offset);
Rob Landley3b890392006-05-04 20:56:43 +0000462 memcpy(&nlp->data[offset], newStr, newLen);
Rob Landley3b890392006-05-04 20:56:43 +0000463 memcpy(&nlp->data[offset + newLen],
464 &lp->data[offset + oldLen],
465 lp->len - offset - oldLen);
466
467 nlp->next = lp->next;
468 nlp->prev = lp->prev;
469 nlp->prev->next = nlp;
470 nlp->next->prev = nlp;
471
472 if (curLine == lp)
473 curLine = nlp;
474
475 free(lp);
476 lp = nlp;
477
478 offset += newLen;
479
480 if (globalFlag)
481 continue;
482
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000483 if (needPrint) {
Rob Landley3b890392006-05-04 20:56:43 +0000484 printLines(num1, num1, FALSE);
485 needPrint = FALSE;
486 }
487
488 lp = lp->next;
489 num1++;
490 }
491
492 if (!didSub)
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000493 bb_error_msg("no substitutions found for \"%s\"", oldStr);
Rob Landley3b890392006-05-04 20:56:43 +0000494}
495
496
497/*
498 * Search a line for the specified string starting at the specified
499 * offset in the line. Returns the offset of the found string, or -1.
500 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000501static int findString(const LINE *lp, const char *str, int len, int offset)
Rob Landley3b890392006-05-04 20:56:43 +0000502{
503 int left;
504 const char *cp, *ncp;
505
506 cp = &lp->data[offset];
507 left = lp->len - offset;
508
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000509 while (left >= len) {
Rob Landley3b890392006-05-04 20:56:43 +0000510 ncp = memchr(cp, *str, left);
Rob Landley3b890392006-05-04 20:56:43 +0000511 if (ncp == NULL)
512 return -1;
Rob Landley3b890392006-05-04 20:56:43 +0000513 left -= (ncp - cp);
Rob Landley3b890392006-05-04 20:56:43 +0000514 if (left < len)
515 return -1;
Rob Landley3b890392006-05-04 20:56:43 +0000516 cp = ncp;
Rob Landley3b890392006-05-04 20:56:43 +0000517 if (memcmp(cp, str, len) == 0)
518 return (cp - lp->data);
Rob Landley3b890392006-05-04 20:56:43 +0000519 cp++;
520 left--;
521 }
522
523 return -1;
524}
525
526
527/*
528 * Add lines which are typed in by the user.
529 * The lines are inserted just before the specified line number.
530 * The lines are terminated by a line containing a single dot (ugly!),
531 * or by an end of file.
532 */
533static void addLines(int num)
534{
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000535 int len;
536 char buf[USERSIZE + 1];
Rob Landley3b890392006-05-04 20:56:43 +0000537
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000538 while (1) {
539 /* Returns:
540 * -1 on read errors or EOF, or on bare Ctrl-D.
541 * 0 on ctrl-C,
542 * >0 length of input string, including terminating '\n'
543 */
544 len = read_line_input("", buf, sizeof(buf), NULL);
545 if (len <= 0) {
546 /* Previously, ctrl-C was exiting to shell.
547 * Now we exit to ed prompt. Is in important? */
Rob Landley3b890392006-05-04 20:56:43 +0000548 return;
549 }
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000550 if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0'))
551 return;
Rob Landley3b890392006-05-04 20:56:43 +0000552 if (!insertLine(num++, buf, len))
553 return;
554 }
555}
556
557
558/*
559 * Parse a line number argument if it is present. This is a sum
560 * or difference of numbers, '.', '$', 'x, or a search string.
Bernhard Reutner-Fischerd9ed35c2006-05-19 12:38:30 +0000561 * Returns TRUE if successful (whether or not there was a number).
Rob Landley3b890392006-05-04 20:56:43 +0000562 * Returns FALSE if there was a parsing error, with a message output.
563 * Whether there was a number is returned indirectly, as is the number.
564 * The character pointer which stopped the scan is also returned.
565 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000566static int getNum(const char **retcp, smallint *retHaveNum, int *retNum)
Rob Landley3b890392006-05-04 20:56:43 +0000567{
568 const char *cp;
569 char *endStr, str[USERSIZE];
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000570 int value, num;
571 smallint haveNum, minus;
Rob Landley3b890392006-05-04 20:56:43 +0000572
573 cp = *retcp;
Rob Landley3b890392006-05-04 20:56:43 +0000574 value = 0;
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000575 haveNum = FALSE;
576 minus = 0;
Rob Landley3b890392006-05-04 20:56:43 +0000577
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000578 while (TRUE) {
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000579 cp = skip_blank(cp);
Rob Landley3b890392006-05-04 20:56:43 +0000580
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000581 switch (*cp) {
Rob Landley3b890392006-05-04 20:56:43 +0000582 case '.':
583 haveNum = TRUE;
584 num = curNum;
585 cp++;
586 break;
587
588 case '$':
589 haveNum = TRUE;
590 num = lastNum;
591 cp++;
592 break;
593
594 case '\'':
595 cp++;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000596 if ((*cp < 'a') || (*cp > 'z')) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000597 bb_error_msg("bad mark name");
Rob Landley3b890392006-05-04 20:56:43 +0000598 return FALSE;
599 }
Rob Landley3b890392006-05-04 20:56:43 +0000600 haveNum = TRUE;
601 num = marks[*cp++ - 'a'];
602 break;
603
604 case '/':
605 strcpy(str, ++cp);
606 endStr = strchr(str, '/');
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000607 if (endStr) {
Rob Landley3b890392006-05-04 20:56:43 +0000608 *endStr++ = '\0';
609 cp += (endStr - str);
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000610 } else
Rob Landley3b890392006-05-04 20:56:43 +0000611 cp = "";
Rob Landley3b890392006-05-04 20:56:43 +0000612 num = searchLines(str, curNum, lastNum);
Rob Landley3b890392006-05-04 20:56:43 +0000613 if (num == 0)
614 return FALSE;
Rob Landley3b890392006-05-04 20:56:43 +0000615 haveNum = TRUE;
616 break;
617
618 default:
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000619 if (!isdigit(*cp)) {
Rob Landley3b890392006-05-04 20:56:43 +0000620 *retcp = cp;
621 *retHaveNum = haveNum;
622 *retNum = value;
Rob Landley3b890392006-05-04 20:56:43 +0000623 return TRUE;
624 }
Rob Landley3b890392006-05-04 20:56:43 +0000625 num = 0;
Rob Landley3b890392006-05-04 20:56:43 +0000626 while (isdigit(*cp))
627 num = num * 10 + *cp++ - '0';
Rob Landley3b890392006-05-04 20:56:43 +0000628 haveNum = TRUE;
629 break;
630 }
631
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000632 value += (minus ? -num : num);
Rob Landley3b890392006-05-04 20:56:43 +0000633
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000634 cp = skip_blank(cp);
Rob Landley3b890392006-05-04 20:56:43 +0000635
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000636 switch (*cp) {
Rob Landley3b890392006-05-04 20:56:43 +0000637 case '-':
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000638 minus = 1;
Rob Landley3b890392006-05-04 20:56:43 +0000639 cp++;
640 break;
641
642 case '+':
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000643 minus = 0;
Rob Landley3b890392006-05-04 20:56:43 +0000644 cp++;
645 break;
646
647 default:
648 *retcp = cp;
649 *retHaveNum = haveNum;
650 *retNum = value;
Rob Landley3b890392006-05-04 20:56:43 +0000651 return TRUE;
652 }
653 }
654}
655
656
657/*
Rob Landley3b890392006-05-04 20:56:43 +0000658 * Read lines from a file at the specified line number.
659 * Returns TRUE if the file was successfully read.
660 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000661static int readLines(const char *file, int num)
Rob Landley3b890392006-05-04 20:56:43 +0000662{
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000663 int fd, cc;
Rob Landley3b890392006-05-04 20:56:43 +0000664 int len, lineCount, charCount;
665 char *cp;
666
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000667 if ((num < 1) || (num > lastNum + 1)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000668 bb_error_msg("bad line for read");
Rob Landley3b890392006-05-04 20:56:43 +0000669 return FALSE;
670 }
671
672 fd = open(file, 0);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000673 if (fd < 0) {
Rob Landley3b890392006-05-04 20:56:43 +0000674 perror(file);
Rob Landley3b890392006-05-04 20:56:43 +0000675 return FALSE;
676 }
677
678 bufPtr = bufBase;
679 bufUsed = 0;
680 lineCount = 0;
681 charCount = 0;
682 cc = 0;
683
684 printf("\"%s\", ", file);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100685 fflush_all();
Rob Landley3b890392006-05-04 20:56:43 +0000686
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000687 do {
Rob Landley3b890392006-05-04 20:56:43 +0000688 cp = memchr(bufPtr, '\n', bufUsed);
689
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000690 if (cp) {
Rob Landley3b890392006-05-04 20:56:43 +0000691 len = (cp - bufPtr) + 1;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000692 if (!insertLine(num, bufPtr, len)) {
Rob Landley3b890392006-05-04 20:56:43 +0000693 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000694 return FALSE;
695 }
Rob Landley3b890392006-05-04 20:56:43 +0000696 bufPtr += len;
697 bufUsed -= len;
698 charCount += len;
699 lineCount++;
700 num++;
Rob Landley3b890392006-05-04 20:56:43 +0000701 continue;
702 }
703
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000704 if (bufPtr != bufBase) {
Rob Landley3b890392006-05-04 20:56:43 +0000705 memcpy(bufBase, bufPtr, bufUsed);
706 bufPtr = bufBase + bufUsed;
707 }
708
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000709 if (bufUsed >= bufSize) {
Rob Landley3b890392006-05-04 20:56:43 +0000710 len = (bufSize * 3) / 2;
Denys Vlasenko90377872010-01-08 09:07:50 +0100711 cp = xrealloc(bufBase, len);
Rob Landley3b890392006-05-04 20:56:43 +0000712 bufBase = cp;
713 bufPtr = bufBase + bufUsed;
714 bufSize = len;
715 }
716
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000717 cc = safe_read(fd, bufPtr, bufSize - bufUsed);
Rob Landley3b890392006-05-04 20:56:43 +0000718 bufUsed += cc;
719 bufPtr = bufBase;
720
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000721 } while (cc > 0);
Rob Landley3b890392006-05-04 20:56:43 +0000722
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000723 if (cc < 0) {
Rob Landley3b890392006-05-04 20:56:43 +0000724 perror(file);
725 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000726 return FALSE;
727 }
728
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000729 if (bufUsed) {
730 if (!insertLine(num, bufPtr, bufUsed)) {
Rob Landley3b890392006-05-04 20:56:43 +0000731 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000732 return -1;
733 }
Rob Landley3b890392006-05-04 20:56:43 +0000734 lineCount++;
735 charCount += bufUsed;
736 }
737
738 close(fd);
739
740 printf("%d lines%s, %d chars\n", lineCount,
741 (bufUsed ? " (incomplete)" : ""), charCount);
742
743 return TRUE;
744}
745
746
747/*
748 * Write the specified lines out to the specified file.
749 * Returns TRUE if successful, or FALSE on an error with a message output.
750 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000751static int writeLines(const char *file, int num1, int num2)
Rob Landley3b890392006-05-04 20:56:43 +0000752{
753 LINE *lp;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000754 int fd, lineCount, charCount;
Rob Landley3b890392006-05-04 20:56:43 +0000755
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000756 if (bad_nums(num1, num2, "write"))
Rob Landley3b890392006-05-04 20:56:43 +0000757 return FALSE;
Rob Landley3b890392006-05-04 20:56:43 +0000758
759 lineCount = 0;
760 charCount = 0;
761
762 fd = creat(file, 0666);
Rob Landley3b890392006-05-04 20:56:43 +0000763 if (fd < 0) {
764 perror(file);
Rob Landley3b890392006-05-04 20:56:43 +0000765 return FALSE;
766 }
767
768 printf("\"%s\", ", file);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100769 fflush_all();
Rob Landley3b890392006-05-04 20:56:43 +0000770
771 lp = findLine(num1);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000772 if (lp == NULL) {
Rob Landley3b890392006-05-04 20:56:43 +0000773 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000774 return FALSE;
775 }
776
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000777 while (num1++ <= num2) {
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000778 if (full_write(fd, lp->data, lp->len) != lp->len) {
Rob Landley3b890392006-05-04 20:56:43 +0000779 perror(file);
780 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000781 return FALSE;
782 }
Rob Landley3b890392006-05-04 20:56:43 +0000783 charCount += lp->len;
784 lineCount++;
785 lp = lp->next;
786 }
787
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000788 if (close(fd) < 0) {
Rob Landley3b890392006-05-04 20:56:43 +0000789 perror(file);
Rob Landley3b890392006-05-04 20:56:43 +0000790 return FALSE;
791 }
792
793 printf("%d lines, %d chars\n", lineCount, charCount);
Rob Landley3b890392006-05-04 20:56:43 +0000794 return TRUE;
795}
796
797
798/*
799 * Print lines in a specified range.
800 * The last line printed becomes the current line.
801 * If expandFlag is TRUE, then the line is printed specially to
802 * show magic characters.
803 */
804static int printLines(int num1, int num2, int expandFlag)
805{
806 const LINE *lp;
807 const char *cp;
808 int ch, count;
809
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000810 if (bad_nums(num1, num2, "print"))
Rob Landley3b890392006-05-04 20:56:43 +0000811 return FALSE;
Rob Landley3b890392006-05-04 20:56:43 +0000812
813 lp = findLine(num1);
Rob Landley3b890392006-05-04 20:56:43 +0000814 if (lp == NULL)
815 return FALSE;
816
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000817 while (num1 <= num2) {
818 if (!expandFlag) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000819 write(STDOUT_FILENO, lp->data, lp->len);
Rob Landley3b890392006-05-04 20:56:43 +0000820 setCurNum(num1++);
821 lp = lp->next;
Rob Landley3b890392006-05-04 20:56:43 +0000822 continue;
823 }
824
825 /*
826 * Show control characters and characters with the
827 * high bit set specially.
828 */
829 cp = lp->data;
830 count = lp->len;
831
832 if ((count > 0) && (cp[count - 1] == '\n'))
833 count--;
834
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000835 while (count-- > 0) {
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000836 ch = (unsigned char) *cp++;
837 fputc_printable(ch | PRINTABLE_META, stdout);
Rob Landley3b890392006-05-04 20:56:43 +0000838 }
839
840 fputs("$\n", stdout);
841
842 setCurNum(num1++);
843 lp = lp->next;
844 }
845
846 return TRUE;
847}
848
849
850/*
851 * Insert a new line with the specified text.
852 * The line is inserted so as to become the specified line,
853 * thus pushing any existing and further lines down one.
854 * The inserted line is also set to become the current line.
855 * Returns TRUE if successful.
856 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000857static int insertLine(int num, const char *data, int len)
Rob Landley3b890392006-05-04 20:56:43 +0000858{
859 LINE *newLp, *lp;
860
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000861 if ((num < 1) || (num > lastNum + 1)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000862 bb_error_msg("inserting at bad line number");
Rob Landley3b890392006-05-04 20:56:43 +0000863 return FALSE;
864 }
865
Denys Vlasenko90377872010-01-08 09:07:50 +0100866 newLp = xmalloc(sizeof(LINE) + len - 1);
Rob Landley3b890392006-05-04 20:56:43 +0000867
868 memcpy(newLp->data, data, len);
869 newLp->len = len;
870
871 if (num > lastNum)
872 lp = &lines;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000873 else {
Rob Landley3b890392006-05-04 20:56:43 +0000874 lp = findLine(num);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000875 if (lp == NULL) {
Rob Landley3b890392006-05-04 20:56:43 +0000876 free((char *) newLp);
Rob Landley3b890392006-05-04 20:56:43 +0000877 return FALSE;
878 }
879 }
880
881 newLp->next = lp;
882 newLp->prev = lp->prev;
883 lp->prev->next = newLp;
884 lp->prev = newLp;
885
886 lastNum++;
887 dirty = TRUE;
Rob Landley3b890392006-05-04 20:56:43 +0000888 return setCurNum(num);
889}
890
891
892/*
893 * Delete lines from the given range.
894 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000895static void deleteLines(int num1, int num2)
Rob Landley3b890392006-05-04 20:56:43 +0000896{
897 LINE *lp, *nlp, *plp;
898 int count;
899
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000900 if (bad_nums(num1, num2, "delete"))
901 return;
Rob Landley3b890392006-05-04 20:56:43 +0000902
903 lp = findLine(num1);
Rob Landley3b890392006-05-04 20:56:43 +0000904 if (lp == NULL)
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000905 return;
Rob Landley3b890392006-05-04 20:56:43 +0000906
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000907 if ((curNum >= num1) && (curNum <= num2)) {
Rob Landley3b890392006-05-04 20:56:43 +0000908 if (num2 < lastNum)
909 setCurNum(num2 + 1);
910 else if (num1 > 1)
911 setCurNum(num1 - 1);
912 else
913 curNum = 0;
914 }
915
916 count = num2 - num1 + 1;
Rob Landley3b890392006-05-04 20:56:43 +0000917 if (curNum > num2)
918 curNum -= count;
Rob Landley3b890392006-05-04 20:56:43 +0000919 lastNum -= count;
920
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000921 while (count-- > 0) {
Rob Landley3b890392006-05-04 20:56:43 +0000922 nlp = lp->next;
923 plp = lp->prev;
924 plp->next = nlp;
925 nlp->prev = plp;
Rob Landley3b890392006-05-04 20:56:43 +0000926 free(lp);
927 lp = nlp;
928 }
929
930 dirty = TRUE;
Rob Landley3b890392006-05-04 20:56:43 +0000931}
932
933
934/*
935 * Search for a line which contains the specified string.
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000936 * If the string is "", then the previously searched for string
Rob Landley3b890392006-05-04 20:56:43 +0000937 * is used. The currently searched for string is saved for future use.
938 * Returns the line number which matches, or 0 if there was no match
939 * with an error printed.
940 */
Denys Vlasenkoadf922e2009-10-08 14:35:37 +0200941static NOINLINE int searchLines(const char *str, int num1, int num2)
Rob Landley3b890392006-05-04 20:56:43 +0000942{
943 const LINE *lp;
944 int len;
945
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000946 if (bad_nums(num1, num2, "search"))
Rob Landley3b890392006-05-04 20:56:43 +0000947 return 0;
Rob Landley3b890392006-05-04 20:56:43 +0000948
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000949 if (*str == '\0') {
950 if (searchString[0] == '\0') {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000951 bb_error_msg("no previous search string");
Rob Landley3b890392006-05-04 20:56:43 +0000952 return 0;
953 }
Rob Landley3b890392006-05-04 20:56:43 +0000954 str = searchString;
955 }
956
957 if (str != searchString)
958 strcpy(searchString, str);
959
960 len = strlen(str);
961
962 lp = findLine(num1);
Rob Landley3b890392006-05-04 20:56:43 +0000963 if (lp == NULL)
964 return 0;
965
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000966 while (num1 <= num2) {
Rob Landley3b890392006-05-04 20:56:43 +0000967 if (findString(lp, str, len, 0) >= 0)
968 return num1;
Rob Landley3b890392006-05-04 20:56:43 +0000969 num1++;
970 lp = lp->next;
971 }
972
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100973 bb_error_msg("can't find string \"%s\"", str);
Rob Landley3b890392006-05-04 20:56:43 +0000974 return 0;
975}
976
977
978/*
979 * Return a pointer to the specified line number.
980 */
981static LINE *findLine(int num)
982{
983 LINE *lp;
984 int lnum;
985
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000986 if ((num < 1) || (num > lastNum)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000987 bb_error_msg("line number %d does not exist", num);
Rob Landley3b890392006-05-04 20:56:43 +0000988 return NULL;
989 }
990
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000991 if (curNum <= 0) {
Rob Landley3b890392006-05-04 20:56:43 +0000992 curNum = 1;
993 curLine = lines.next;
994 }
995
996 if (num == curNum)
997 return curLine;
998
999 lp = curLine;
1000 lnum = curNum;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +00001001 if (num < (curNum / 2)) {
Rob Landley3b890392006-05-04 20:56:43 +00001002 lp = lines.next;
1003 lnum = 1;
Denis Vlasenkod9391b12007-09-25 11:55:57 +00001004 } else if (num > ((curNum + lastNum) / 2)) {
Rob Landley3b890392006-05-04 20:56:43 +00001005 lp = lines.prev;
1006 lnum = lastNum;
1007 }
1008
Denis Vlasenko610c4aa2006-11-30 20:57:50 +00001009 while (lnum < num) {
Rob Landley3b890392006-05-04 20:56:43 +00001010 lp = lp->next;
1011 lnum++;
1012 }
1013
Denis Vlasenko610c4aa2006-11-30 20:57:50 +00001014 while (lnum > num) {
Rob Landley3b890392006-05-04 20:56:43 +00001015 lp = lp->prev;
1016 lnum--;
1017 }
Rob Landley3b890392006-05-04 20:56:43 +00001018 return lp;
1019}
1020
1021
1022/*
1023 * Set the current line number.
1024 * Returns TRUE if successful.
1025 */
1026static int setCurNum(int num)
1027{
1028 LINE *lp;
1029
1030 lp = findLine(num);
Rob Landley3b890392006-05-04 20:56:43 +00001031 if (lp == NULL)
1032 return FALSE;
Rob Landley3b890392006-05-04 20:56:43 +00001033 curNum = num;
1034 curLine = lp;
Rob Landley3b890392006-05-04 20:56:43 +00001035 return TRUE;
1036}