blob: 9ce8bead109565a08e2aed038235797a99598551 [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 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000457 nlp = malloc(sizeof(LINE) + lp->len + deltaLen);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000458 if (nlp == NULL) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100459 bb_error_msg("can't get memory for line");
Rob Landley3b890392006-05-04 20:56:43 +0000460 return;
461 }
462
463 nlp->len = lp->len + deltaLen;
464
465 memcpy(nlp->data, lp->data, offset);
Rob Landley3b890392006-05-04 20:56:43 +0000466 memcpy(&nlp->data[offset], newStr, newLen);
Rob Landley3b890392006-05-04 20:56:43 +0000467 memcpy(&nlp->data[offset + newLen],
468 &lp->data[offset + oldLen],
469 lp->len - offset - oldLen);
470
471 nlp->next = lp->next;
472 nlp->prev = lp->prev;
473 nlp->prev->next = nlp;
474 nlp->next->prev = nlp;
475
476 if (curLine == lp)
477 curLine = nlp;
478
479 free(lp);
480 lp = nlp;
481
482 offset += newLen;
483
484 if (globalFlag)
485 continue;
486
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000487 if (needPrint) {
Rob Landley3b890392006-05-04 20:56:43 +0000488 printLines(num1, num1, FALSE);
489 needPrint = FALSE;
490 }
491
492 lp = lp->next;
493 num1++;
494 }
495
496 if (!didSub)
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000497 bb_error_msg("no substitutions found for \"%s\"", oldStr);
Rob Landley3b890392006-05-04 20:56:43 +0000498}
499
500
501/*
502 * Search a line for the specified string starting at the specified
503 * offset in the line. Returns the offset of the found string, or -1.
504 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000505static int findString(const LINE *lp, const char *str, int len, int offset)
Rob Landley3b890392006-05-04 20:56:43 +0000506{
507 int left;
508 const char *cp, *ncp;
509
510 cp = &lp->data[offset];
511 left = lp->len - offset;
512
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000513 while (left >= len) {
Rob Landley3b890392006-05-04 20:56:43 +0000514 ncp = memchr(cp, *str, left);
Rob Landley3b890392006-05-04 20:56:43 +0000515 if (ncp == NULL)
516 return -1;
Rob Landley3b890392006-05-04 20:56:43 +0000517 left -= (ncp - cp);
Rob Landley3b890392006-05-04 20:56:43 +0000518 if (left < len)
519 return -1;
Rob Landley3b890392006-05-04 20:56:43 +0000520 cp = ncp;
Rob Landley3b890392006-05-04 20:56:43 +0000521 if (memcmp(cp, str, len) == 0)
522 return (cp - lp->data);
Rob Landley3b890392006-05-04 20:56:43 +0000523 cp++;
524 left--;
525 }
526
527 return -1;
528}
529
530
531/*
532 * Add lines which are typed in by the user.
533 * The lines are inserted just before the specified line number.
534 * The lines are terminated by a line containing a single dot (ugly!),
535 * or by an end of file.
536 */
537static void addLines(int num)
538{
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000539 int len;
540 char buf[USERSIZE + 1];
Rob Landley3b890392006-05-04 20:56:43 +0000541
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000542 while (1) {
543 /* Returns:
544 * -1 on read errors or EOF, or on bare Ctrl-D.
545 * 0 on ctrl-C,
546 * >0 length of input string, including terminating '\n'
547 */
548 len = read_line_input("", buf, sizeof(buf), NULL);
549 if (len <= 0) {
550 /* Previously, ctrl-C was exiting to shell.
551 * Now we exit to ed prompt. Is in important? */
Rob Landley3b890392006-05-04 20:56:43 +0000552 return;
553 }
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000554 if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0'))
555 return;
Rob Landley3b890392006-05-04 20:56:43 +0000556 if (!insertLine(num++, buf, len))
557 return;
558 }
559}
560
561
562/*
563 * Parse a line number argument if it is present. This is a sum
564 * or difference of numbers, '.', '$', 'x, or a search string.
Bernhard Reutner-Fischerd9ed35c2006-05-19 12:38:30 +0000565 * Returns TRUE if successful (whether or not there was a number).
Rob Landley3b890392006-05-04 20:56:43 +0000566 * Returns FALSE if there was a parsing error, with a message output.
567 * Whether there was a number is returned indirectly, as is the number.
568 * The character pointer which stopped the scan is also returned.
569 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000570static int getNum(const char **retcp, smallint *retHaveNum, int *retNum)
Rob Landley3b890392006-05-04 20:56:43 +0000571{
572 const char *cp;
573 char *endStr, str[USERSIZE];
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000574 int value, num;
575 smallint haveNum, minus;
Rob Landley3b890392006-05-04 20:56:43 +0000576
577 cp = *retcp;
Rob Landley3b890392006-05-04 20:56:43 +0000578 value = 0;
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000579 haveNum = FALSE;
580 minus = 0;
Rob Landley3b890392006-05-04 20:56:43 +0000581
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000582 while (TRUE) {
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000583 cp = skip_blank(cp);
Rob Landley3b890392006-05-04 20:56:43 +0000584
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000585 switch (*cp) {
Rob Landley3b890392006-05-04 20:56:43 +0000586 case '.':
587 haveNum = TRUE;
588 num = curNum;
589 cp++;
590 break;
591
592 case '$':
593 haveNum = TRUE;
594 num = lastNum;
595 cp++;
596 break;
597
598 case '\'':
599 cp++;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000600 if ((*cp < 'a') || (*cp > 'z')) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000601 bb_error_msg("bad mark name");
Rob Landley3b890392006-05-04 20:56:43 +0000602 return FALSE;
603 }
Rob Landley3b890392006-05-04 20:56:43 +0000604 haveNum = TRUE;
605 num = marks[*cp++ - 'a'];
606 break;
607
608 case '/':
609 strcpy(str, ++cp);
610 endStr = strchr(str, '/');
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000611 if (endStr) {
Rob Landley3b890392006-05-04 20:56:43 +0000612 *endStr++ = '\0';
613 cp += (endStr - str);
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000614 } else
Rob Landley3b890392006-05-04 20:56:43 +0000615 cp = "";
Rob Landley3b890392006-05-04 20:56:43 +0000616 num = searchLines(str, curNum, lastNum);
Rob Landley3b890392006-05-04 20:56:43 +0000617 if (num == 0)
618 return FALSE;
Rob Landley3b890392006-05-04 20:56:43 +0000619 haveNum = TRUE;
620 break;
621
622 default:
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000623 if (!isdigit(*cp)) {
Rob Landley3b890392006-05-04 20:56:43 +0000624 *retcp = cp;
625 *retHaveNum = haveNum;
626 *retNum = value;
Rob Landley3b890392006-05-04 20:56:43 +0000627 return TRUE;
628 }
Rob Landley3b890392006-05-04 20:56:43 +0000629 num = 0;
Rob Landley3b890392006-05-04 20:56:43 +0000630 while (isdigit(*cp))
631 num = num * 10 + *cp++ - '0';
Rob Landley3b890392006-05-04 20:56:43 +0000632 haveNum = TRUE;
633 break;
634 }
635
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000636 value += (minus ? -num : num);
Rob Landley3b890392006-05-04 20:56:43 +0000637
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000638 cp = skip_blank(cp);
Rob Landley3b890392006-05-04 20:56:43 +0000639
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000640 switch (*cp) {
Rob Landley3b890392006-05-04 20:56:43 +0000641 case '-':
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000642 minus = 1;
Rob Landley3b890392006-05-04 20:56:43 +0000643 cp++;
644 break;
645
646 case '+':
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000647 minus = 0;
Rob Landley3b890392006-05-04 20:56:43 +0000648 cp++;
649 break;
650
651 default:
652 *retcp = cp;
653 *retHaveNum = haveNum;
654 *retNum = value;
Rob Landley3b890392006-05-04 20:56:43 +0000655 return TRUE;
656 }
657 }
658}
659
660
661/*
Rob Landley3b890392006-05-04 20:56:43 +0000662 * Read lines from a file at the specified line number.
663 * Returns TRUE if the file was successfully read.
664 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000665static int readLines(const char *file, int num)
Rob Landley3b890392006-05-04 20:56:43 +0000666{
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000667 int fd, cc;
Rob Landley3b890392006-05-04 20:56:43 +0000668 int len, lineCount, charCount;
669 char *cp;
670
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000671 if ((num < 1) || (num > lastNum + 1)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000672 bb_error_msg("bad line for read");
Rob Landley3b890392006-05-04 20:56:43 +0000673 return FALSE;
674 }
675
676 fd = open(file, 0);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000677 if (fd < 0) {
Rob Landley3b890392006-05-04 20:56:43 +0000678 perror(file);
Rob Landley3b890392006-05-04 20:56:43 +0000679 return FALSE;
680 }
681
682 bufPtr = bufBase;
683 bufUsed = 0;
684 lineCount = 0;
685 charCount = 0;
686 cc = 0;
687
688 printf("\"%s\", ", file);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100689 fflush_all();
Rob Landley3b890392006-05-04 20:56:43 +0000690
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000691 do {
Rob Landley3b890392006-05-04 20:56:43 +0000692 cp = memchr(bufPtr, '\n', bufUsed);
693
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000694 if (cp) {
Rob Landley3b890392006-05-04 20:56:43 +0000695 len = (cp - bufPtr) + 1;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000696 if (!insertLine(num, bufPtr, len)) {
Rob Landley3b890392006-05-04 20:56:43 +0000697 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000698 return FALSE;
699 }
Rob Landley3b890392006-05-04 20:56:43 +0000700 bufPtr += len;
701 bufUsed -= len;
702 charCount += len;
703 lineCount++;
704 num++;
Rob Landley3b890392006-05-04 20:56:43 +0000705 continue;
706 }
707
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000708 if (bufPtr != bufBase) {
Rob Landley3b890392006-05-04 20:56:43 +0000709 memcpy(bufBase, bufPtr, bufUsed);
710 bufPtr = bufBase + bufUsed;
711 }
712
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000713 if (bufUsed >= bufSize) {
Rob Landley3b890392006-05-04 20:56:43 +0000714 len = (bufSize * 3) / 2;
715 cp = realloc(bufBase, len);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000716 if (cp == NULL) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000717 bb_error_msg("no memory for buffer");
Rob Landley3b890392006-05-04 20:56:43 +0000718 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000719 return FALSE;
720 }
Rob Landley3b890392006-05-04 20:56:43 +0000721 bufBase = cp;
722 bufPtr = bufBase + bufUsed;
723 bufSize = len;
724 }
725
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000726 cc = safe_read(fd, bufPtr, bufSize - bufUsed);
Rob Landley3b890392006-05-04 20:56:43 +0000727 bufUsed += cc;
728 bufPtr = bufBase;
729
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000730 } while (cc > 0);
Rob Landley3b890392006-05-04 20:56:43 +0000731
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000732 if (cc < 0) {
Rob Landley3b890392006-05-04 20:56:43 +0000733 perror(file);
734 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000735 return FALSE;
736 }
737
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000738 if (bufUsed) {
739 if (!insertLine(num, bufPtr, bufUsed)) {
Rob Landley3b890392006-05-04 20:56:43 +0000740 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000741 return -1;
742 }
Rob Landley3b890392006-05-04 20:56:43 +0000743 lineCount++;
744 charCount += bufUsed;
745 }
746
747 close(fd);
748
749 printf("%d lines%s, %d chars\n", lineCount,
750 (bufUsed ? " (incomplete)" : ""), charCount);
751
752 return TRUE;
753}
754
755
756/*
757 * Write the specified lines out to the specified file.
758 * Returns TRUE if successful, or FALSE on an error with a message output.
759 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000760static int writeLines(const char *file, int num1, int num2)
Rob Landley3b890392006-05-04 20:56:43 +0000761{
762 LINE *lp;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000763 int fd, lineCount, charCount;
Rob Landley3b890392006-05-04 20:56:43 +0000764
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000765 if (bad_nums(num1, num2, "write"))
Rob Landley3b890392006-05-04 20:56:43 +0000766 return FALSE;
Rob Landley3b890392006-05-04 20:56:43 +0000767
768 lineCount = 0;
769 charCount = 0;
770
771 fd = creat(file, 0666);
Rob Landley3b890392006-05-04 20:56:43 +0000772 if (fd < 0) {
773 perror(file);
Rob Landley3b890392006-05-04 20:56:43 +0000774 return FALSE;
775 }
776
777 printf("\"%s\", ", file);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100778 fflush_all();
Rob Landley3b890392006-05-04 20:56:43 +0000779
780 lp = findLine(num1);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000781 if (lp == NULL) {
Rob Landley3b890392006-05-04 20:56:43 +0000782 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000783 return FALSE;
784 }
785
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000786 while (num1++ <= num2) {
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000787 if (full_write(fd, lp->data, lp->len) != lp->len) {
Rob Landley3b890392006-05-04 20:56:43 +0000788 perror(file);
789 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000790 return FALSE;
791 }
Rob Landley3b890392006-05-04 20:56:43 +0000792 charCount += lp->len;
793 lineCount++;
794 lp = lp->next;
795 }
796
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000797 if (close(fd) < 0) {
Rob Landley3b890392006-05-04 20:56:43 +0000798 perror(file);
Rob Landley3b890392006-05-04 20:56:43 +0000799 return FALSE;
800 }
801
802 printf("%d lines, %d chars\n", lineCount, charCount);
Rob Landley3b890392006-05-04 20:56:43 +0000803 return TRUE;
804}
805
806
807/*
808 * Print lines in a specified range.
809 * The last line printed becomes the current line.
810 * If expandFlag is TRUE, then the line is printed specially to
811 * show magic characters.
812 */
813static int printLines(int num1, int num2, int expandFlag)
814{
815 const LINE *lp;
816 const char *cp;
817 int ch, count;
818
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000819 if (bad_nums(num1, num2, "print"))
Rob Landley3b890392006-05-04 20:56:43 +0000820 return FALSE;
Rob Landley3b890392006-05-04 20:56:43 +0000821
822 lp = findLine(num1);
Rob Landley3b890392006-05-04 20:56:43 +0000823 if (lp == NULL)
824 return FALSE;
825
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000826 while (num1 <= num2) {
827 if (!expandFlag) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000828 write(STDOUT_FILENO, lp->data, lp->len);
Rob Landley3b890392006-05-04 20:56:43 +0000829 setCurNum(num1++);
830 lp = lp->next;
Rob Landley3b890392006-05-04 20:56:43 +0000831 continue;
832 }
833
834 /*
835 * Show control characters and characters with the
836 * high bit set specially.
837 */
838 cp = lp->data;
839 count = lp->len;
840
841 if ((count > 0) && (cp[count - 1] == '\n'))
842 count--;
843
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000844 while (count-- > 0) {
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000845 ch = (unsigned char) *cp++;
846 fputc_printable(ch | PRINTABLE_META, stdout);
Rob Landley3b890392006-05-04 20:56:43 +0000847 }
848
849 fputs("$\n", stdout);
850
851 setCurNum(num1++);
852 lp = lp->next;
853 }
854
855 return TRUE;
856}
857
858
859/*
860 * Insert a new line with the specified text.
861 * The line is inserted so as to become the specified line,
862 * thus pushing any existing and further lines down one.
863 * The inserted line is also set to become the current line.
864 * Returns TRUE if successful.
865 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000866static int insertLine(int num, const char *data, int len)
Rob Landley3b890392006-05-04 20:56:43 +0000867{
868 LINE *newLp, *lp;
869
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000870 if ((num < 1) || (num > lastNum + 1)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000871 bb_error_msg("inserting at bad line number");
Rob Landley3b890392006-05-04 20:56:43 +0000872 return FALSE;
873 }
874
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000875 newLp = malloc(sizeof(LINE) + len - 1);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000876 if (newLp == NULL) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000877 bb_error_msg("failed to allocate memory for line");
Rob Landley3b890392006-05-04 20:56:43 +0000878 return FALSE;
879 }
880
881 memcpy(newLp->data, data, len);
882 newLp->len = len;
883
884 if (num > lastNum)
885 lp = &lines;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000886 else {
Rob Landley3b890392006-05-04 20:56:43 +0000887 lp = findLine(num);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000888 if (lp == NULL) {
Rob Landley3b890392006-05-04 20:56:43 +0000889 free((char *) newLp);
Rob Landley3b890392006-05-04 20:56:43 +0000890 return FALSE;
891 }
892 }
893
894 newLp->next = lp;
895 newLp->prev = lp->prev;
896 lp->prev->next = newLp;
897 lp->prev = newLp;
898
899 lastNum++;
900 dirty = TRUE;
Rob Landley3b890392006-05-04 20:56:43 +0000901 return setCurNum(num);
902}
903
904
905/*
906 * Delete lines from the given range.
907 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000908static void deleteLines(int num1, int num2)
Rob Landley3b890392006-05-04 20:56:43 +0000909{
910 LINE *lp, *nlp, *plp;
911 int count;
912
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000913 if (bad_nums(num1, num2, "delete"))
914 return;
Rob Landley3b890392006-05-04 20:56:43 +0000915
916 lp = findLine(num1);
Rob Landley3b890392006-05-04 20:56:43 +0000917 if (lp == NULL)
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000918 return;
Rob Landley3b890392006-05-04 20:56:43 +0000919
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000920 if ((curNum >= num1) && (curNum <= num2)) {
Rob Landley3b890392006-05-04 20:56:43 +0000921 if (num2 < lastNum)
922 setCurNum(num2 + 1);
923 else if (num1 > 1)
924 setCurNum(num1 - 1);
925 else
926 curNum = 0;
927 }
928
929 count = num2 - num1 + 1;
Rob Landley3b890392006-05-04 20:56:43 +0000930 if (curNum > num2)
931 curNum -= count;
Rob Landley3b890392006-05-04 20:56:43 +0000932 lastNum -= count;
933
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000934 while (count-- > 0) {
Rob Landley3b890392006-05-04 20:56:43 +0000935 nlp = lp->next;
936 plp = lp->prev;
937 plp->next = nlp;
938 nlp->prev = plp;
Rob Landley3b890392006-05-04 20:56:43 +0000939 free(lp);
940 lp = nlp;
941 }
942
943 dirty = TRUE;
Rob Landley3b890392006-05-04 20:56:43 +0000944}
945
946
947/*
948 * Search for a line which contains the specified string.
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000949 * If the string is "", then the previously searched for string
Rob Landley3b890392006-05-04 20:56:43 +0000950 * is used. The currently searched for string is saved for future use.
951 * Returns the line number which matches, or 0 if there was no match
952 * with an error printed.
953 */
Denys Vlasenkoadf922e2009-10-08 14:35:37 +0200954static NOINLINE int searchLines(const char *str, int num1, int num2)
Rob Landley3b890392006-05-04 20:56:43 +0000955{
956 const LINE *lp;
957 int len;
958
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000959 if (bad_nums(num1, num2, "search"))
Rob Landley3b890392006-05-04 20:56:43 +0000960 return 0;
Rob Landley3b890392006-05-04 20:56:43 +0000961
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000962 if (*str == '\0') {
963 if (searchString[0] == '\0') {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000964 bb_error_msg("no previous search string");
Rob Landley3b890392006-05-04 20:56:43 +0000965 return 0;
966 }
Rob Landley3b890392006-05-04 20:56:43 +0000967 str = searchString;
968 }
969
970 if (str != searchString)
971 strcpy(searchString, str);
972
973 len = strlen(str);
974
975 lp = findLine(num1);
Rob Landley3b890392006-05-04 20:56:43 +0000976 if (lp == NULL)
977 return 0;
978
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000979 while (num1 <= num2) {
Rob Landley3b890392006-05-04 20:56:43 +0000980 if (findString(lp, str, len, 0) >= 0)
981 return num1;
Rob Landley3b890392006-05-04 20:56:43 +0000982 num1++;
983 lp = lp->next;
984 }
985
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100986 bb_error_msg("can't find string \"%s\"", str);
Rob Landley3b890392006-05-04 20:56:43 +0000987 return 0;
988}
989
990
991/*
992 * Return a pointer to the specified line number.
993 */
994static LINE *findLine(int num)
995{
996 LINE *lp;
997 int lnum;
998
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000999 if ((num < 1) || (num > lastNum)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +00001000 bb_error_msg("line number %d does not exist", num);
Rob Landley3b890392006-05-04 20:56:43 +00001001 return NULL;
1002 }
1003
Denis Vlasenko610c4aa2006-11-30 20:57:50 +00001004 if (curNum <= 0) {
Rob Landley3b890392006-05-04 20:56:43 +00001005 curNum = 1;
1006 curLine = lines.next;
1007 }
1008
1009 if (num == curNum)
1010 return curLine;
1011
1012 lp = curLine;
1013 lnum = curNum;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +00001014 if (num < (curNum / 2)) {
Rob Landley3b890392006-05-04 20:56:43 +00001015 lp = lines.next;
1016 lnum = 1;
Denis Vlasenkod9391b12007-09-25 11:55:57 +00001017 } else if (num > ((curNum + lastNum) / 2)) {
Rob Landley3b890392006-05-04 20:56:43 +00001018 lp = lines.prev;
1019 lnum = lastNum;
1020 }
1021
Denis Vlasenko610c4aa2006-11-30 20:57:50 +00001022 while (lnum < num) {
Rob Landley3b890392006-05-04 20:56:43 +00001023 lp = lp->next;
1024 lnum++;
1025 }
1026
Denis Vlasenko610c4aa2006-11-30 20:57:50 +00001027 while (lnum > num) {
Rob Landley3b890392006-05-04 20:56:43 +00001028 lp = lp->prev;
1029 lnum--;
1030 }
Rob Landley3b890392006-05-04 20:56:43 +00001031 return lp;
1032}
1033
1034
1035/*
1036 * Set the current line number.
1037 * Returns TRUE if successful.
1038 */
1039static int setCurNum(int num)
1040{
1041 LINE *lp;
1042
1043 lp = findLine(num);
Rob Landley3b890392006-05-04 20:56:43 +00001044 if (lp == NULL)
1045 return FALSE;
Rob Landley3b890392006-05-04 20:56:43 +00001046 curNum = num;
1047 curLine = lp;
Rob Landley3b890392006-05-04 20:56:43 +00001048 return TRUE;
1049}