blob: dbb51306cc35ca47aae622cd82ae2180bad92bf3 [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
Pere Orga6a3e01d2011-04-01 22:56:30 +020010//usage:#define ed_trivial_usage ""
11//usage:#define ed_full_usage ""
12
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000013#include "libbb.h"
Rob Landley3b890392006-05-04 20:56:43 +000014
Denis Vlasenkod9391b12007-09-25 11:55:57 +000015typedef struct LINE {
16 struct LINE *next;
17 struct LINE *prev;
18 int len;
19 char data[1];
20} LINE;
21
22
Denis Vlasenko74324c82007-06-04 10:16:52 +000023#define searchString bb_common_bufsiz1
24
Denis Vlasenko55f30b02007-03-24 22:42:29 +000025enum {
Denis Vlasenko74324c82007-06-04 10:16:52 +000026 USERSIZE = sizeof(searchString) > 1024 ? 1024
27 : sizeof(searchString) - 1, /* max line length typed in by user */
Denis Vlasenko55f30b02007-03-24 22:42:29 +000028 INITBUF_SIZE = 1024, /* initial buffer size */
29};
30
Denis Vlasenkod9391b12007-09-25 11:55:57 +000031struct globals {
32 int curNum;
33 int lastNum;
34 int bufUsed;
35 int bufSize;
36 LINE *curLine;
37 char *bufBase;
38 char *bufPtr;
39 char *fileName;
40 LINE lines;
41 smallint dirty;
42 int marks[26];
43};
44#define G (*ptr_to_globals)
45#define curLine (G.curLine )
46#define bufBase (G.bufBase )
47#define bufPtr (G.bufPtr )
48#define fileName (G.fileName )
49#define curNum (G.curNum )
50#define lastNum (G.lastNum )
51#define bufUsed (G.bufUsed )
52#define bufSize (G.bufSize )
53#define dirty (G.dirty )
54#define lines (G.lines )
55#define marks (G.marks )
56#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +000057 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkod9391b12007-09-25 11:55:57 +000058} while (0)
Rob Landley3b890392006-05-04 20:56:43 +000059
Rob Landley3b890392006-05-04 20:56:43 +000060
61static void doCommands(void);
62static void subCommand(const char *cmd, int num1, int num2);
Denis Vlasenkod9391b12007-09-25 11:55:57 +000063static int getNum(const char **retcp, smallint *retHaveNum, int *retNum);
Rob Landley3b890392006-05-04 20:56:43 +000064static int setCurNum(int num);
Rob Landley3b890392006-05-04 20:56:43 +000065static void addLines(int num);
66static int insertLine(int num, const char *data, int len);
Denis Vlasenkod9391b12007-09-25 11:55:57 +000067static void deleteLines(int num1, int num2);
Rob Landley3b890392006-05-04 20:56:43 +000068static int printLines(int num1, int num2, int expandFlag);
69static int writeLines(const char *file, int num1, int num2);
70static int readLines(const char *file, int num);
71static int searchLines(const char *str, int num1, int num2);
72static LINE *findLine(int num);
Rob Landley3b890392006-05-04 20:56:43 +000073static int findString(const LINE *lp, const char * str, int len, int offset);
74
Denis Vlasenkod9391b12007-09-25 11:55:57 +000075
76static int bad_nums(int num1, int num2, const char *for_what)
77{
78 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
79 bb_error_msg("bad line range for %s", for_what);
80 return 1;
81 }
82 return 0;
83}
84
85
86static char *skip_blank(const char *cp)
87{
Denis Vlasenkod9391b12007-09-25 11:55:57 +000088 while (isblank(*cp))
89 cp++;
90 return (char *)cp;
91}
92
93
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000094int ed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000095int ed_main(int argc UNUSED_PARAM, char **argv)
Rob Landley3b890392006-05-04 20:56:43 +000096{
Denis Vlasenkod9391b12007-09-25 11:55:57 +000097 INIT_G();
98
99 bufSize = INITBUF_SIZE;
100 bufBase = xmalloc(bufSize);
101 bufPtr = bufBase;
102 lines.next = &lines;
103 lines.prev = &lines;
Rob Landley3b890392006-05-04 20:56:43 +0000104
Denis Vlasenko1d426652008-03-17 09:09:09 +0000105 if (argv[1]) {
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000106 fileName = xstrdup(argv[1]);
Rob Landley3b890392006-05-04 20:56:43 +0000107 if (!readLines(fileName, 1)) {
Rob Landley3b890392006-05-04 20:56:43 +0000108 return EXIT_SUCCESS;
109 }
Rob Landley3b890392006-05-04 20:56:43 +0000110 if (lastNum)
111 setCurNum(1);
Rob Landley3b890392006-05-04 20:56:43 +0000112 dirty = FALSE;
113 }
114
115 doCommands();
Rob Landley3b890392006-05-04 20:56:43 +0000116 return EXIT_SUCCESS;
117}
118
119/*
120 * Read commands until we are told to stop.
121 */
122static void doCommands(void)
123{
124 const char *cp;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000125 char *endbuf, buf[USERSIZE];
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000126 int len, num1, num2;
127 smallint have1, have2;
Rob Landley3b890392006-05-04 20:56:43 +0000128
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000129 while (TRUE) {
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000130 /* Returns:
131 * -1 on read errors or EOF, or on bare Ctrl-D.
132 * 0 on ctrl-C,
133 * >0 length of input string, including terminating '\n'
134 */
Denys Vlasenko66c5b122011-02-08 05:07:02 +0100135 len = read_line_input(NULL, ": ", buf, sizeof(buf), /*timeout*/ -1);
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000136 if (len <= 0)
Rob Landley3b890392006-05-04 20:56:43 +0000137 return;
Rob Landley3b890392006-05-04 20:56:43 +0000138 endbuf = &buf[len - 1];
Rob Landley3b890392006-05-04 20:56:43 +0000139 while ((endbuf > buf) && isblank(endbuf[-1]))
140 endbuf--;
Rob Landley3b890392006-05-04 20:56:43 +0000141 *endbuf = '\0';
142
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000143 cp = skip_blank(buf);
Rob Landley3b890392006-05-04 20:56:43 +0000144 have1 = FALSE;
145 have2 = FALSE;
146
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000147 if ((curNum == 0) && (lastNum > 0)) {
Rob Landley3b890392006-05-04 20:56:43 +0000148 curNum = 1;
149 curLine = lines.next;
150 }
151
152 if (!getNum(&cp, &have1, &num1))
153 continue;
154
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000155 cp = skip_blank(cp);
Rob Landley3b890392006-05-04 20:56:43 +0000156
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000157 if (*cp == ',') {
Rob Landley3b890392006-05-04 20:56:43 +0000158 cp++;
Rob Landley3b890392006-05-04 20:56:43 +0000159 if (!getNum(&cp, &have2, &num2))
160 continue;
Rob Landley3b890392006-05-04 20:56:43 +0000161 if (!have1)
162 num1 = 1;
Rob Landley3b890392006-05-04 20:56:43 +0000163 if (!have2)
164 num2 = lastNum;
Rob Landley3b890392006-05-04 20:56:43 +0000165 have1 = TRUE;
166 have2 = TRUE;
167 }
Rob Landley3b890392006-05-04 20:56:43 +0000168 if (!have1)
169 num1 = curNum;
Rob Landley3b890392006-05-04 20:56:43 +0000170 if (!have2)
171 num2 = num1;
172
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000173 switch (*cp++) {
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000174 case 'a':
175 addLines(num1 + 1);
176 break;
Rob Landley3b890392006-05-04 20:56:43 +0000177
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000178 case 'c':
179 deleteLines(num1, num2);
180 addLines(num1);
181 break;
Rob Landley3b890392006-05-04 20:56:43 +0000182
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000183 case 'd':
184 deleteLines(num1, num2);
185 break;
Rob Landley3b890392006-05-04 20:56:43 +0000186
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000187 case 'f':
188 if (*cp && !isblank(*cp)) {
189 bb_error_msg("bad file command");
Rob Landley3b890392006-05-04 20:56:43 +0000190 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000191 }
192 cp = skip_blank(cp);
193 if (*cp == '\0') {
194 if (fileName)
195 printf("\"%s\"\n", fileName);
196 else
197 printf("No file name\n");
Rob Landley3b890392006-05-04 20:56:43 +0000198 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000199 }
200 free(fileName);
201 fileName = xstrdup(cp);
202 break;
Rob Landley3b890392006-05-04 20:56:43 +0000203
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000204 case 'i':
205 addLines(num1);
206 break;
207
208 case 'k':
209 cp = skip_blank(cp);
210 if ((*cp < 'a') || (*cp > 'z') || cp[1]) {
211 bb_error_msg("bad mark name");
Rob Landley3b890392006-05-04 20:56:43 +0000212 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000213 }
214 marks[*cp - 'a'] = num2;
215 break;
Rob Landley3b890392006-05-04 20:56:43 +0000216
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000217 case 'l':
218 printLines(num1, num2, TRUE);
219 break;
220
221 case 'p':
222 printLines(num1, num2, FALSE);
223 break;
224
225 case 'q':
226 cp = skip_blank(cp);
227 if (have1 || *cp) {
228 bb_error_msg("bad quit command");
Rob Landley3b890392006-05-04 20:56:43 +0000229 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000230 }
231 if (!dirty)
232 return;
Denys Vlasenko66c5b122011-02-08 05:07:02 +0100233 len = read_line_input(NULL, "Really quit? ", buf, 16, /*timeout*/ -1);
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000234 /* read error/EOF - no way to continue */
235 if (len < 0)
236 return;
237 cp = skip_blank(buf);
238 if ((*cp | 0x20) == 'y') /* Y or y */
239 return;
240 break;
Rob Landley3b890392006-05-04 20:56:43 +0000241
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000242 case 'r':
243 if (*cp && !isblank(*cp)) {
244 bb_error_msg("bad read command");
Rob Landley3b890392006-05-04 20:56:43 +0000245 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000246 }
247 cp = skip_blank(cp);
248 if (*cp == '\0') {
249 bb_error_msg("no file name");
Rob Landley3b890392006-05-04 20:56:43 +0000250 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000251 }
252 if (!have1)
253 num1 = lastNum;
254 if (readLines(cp, num1 + 1))
Rob Landley3b890392006-05-04 20:56:43 +0000255 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000256 if (fileName == NULL)
257 fileName = xstrdup(cp);
258 break;
Rob Landley3b890392006-05-04 20:56:43 +0000259
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000260 case 's':
261 subCommand(cp, num1, num2);
262 break;
263
264 case 'w':
265 if (*cp && !isblank(*cp)) {
266 bb_error_msg("bad write command");
Rob Landley3b890392006-05-04 20:56:43 +0000267 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000268 }
269 cp = skip_blank(cp);
270 if (!have1) {
271 num1 = 1;
272 num2 = lastNum;
273 }
274 if (*cp == '\0')
275 cp = fileName;
276 if (cp == NULL) {
277 bb_error_msg("no file name specified");
Rob Landley3b890392006-05-04 20:56:43 +0000278 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000279 }
280 writeLines(cp, num1, num2);
281 break;
Rob Landley3b890392006-05-04 20:56:43 +0000282
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000283 case 'z':
284 switch (*cp) {
Rob Landley3b890392006-05-04 20:56:43 +0000285 case '-':
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000286 printLines(curNum - 21, curNum, FALSE);
Rob Landley3b890392006-05-04 20:56:43 +0000287 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000288 case '.':
289 printLines(curNum - 11, curNum + 10, FALSE);
Rob Landley3b890392006-05-04 20:56:43 +0000290 break;
Rob Landley3b890392006-05-04 20:56:43 +0000291 default:
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000292 printLines(curNum, curNum + 21, FALSE);
Rob Landley3b890392006-05-04 20:56:43 +0000293 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000294 }
295 break;
296
297 case '.':
298 if (have1) {
299 bb_error_msg("no arguments allowed");
300 break;
301 }
302 printLines(curNum, curNum, FALSE);
303 break;
304
305 case '-':
306 if (setCurNum(curNum - 1))
307 printLines(curNum, curNum, FALSE);
308 break;
309
310 case '=':
311 printf("%d\n", num1);
312 break;
313 case '\0':
314 if (have1) {
315 printLines(num2, num2, FALSE);
316 break;
317 }
318 if (setCurNum(curNum + 1))
319 printLines(curNum, curNum, FALSE);
320 break;
321
322 default:
323 bb_error_msg("unimplemented command");
324 break;
Rob Landley3b890392006-05-04 20:56:43 +0000325 }
326 }
327}
328
329
330/*
331 * Do the substitute command.
332 * The current line is set to the last substitution done.
333 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000334static void subCommand(const char *cmd, int num1, int num2)
Rob Landley3b890392006-05-04 20:56:43 +0000335{
336 char *cp, *oldStr, *newStr, buf[USERSIZE];
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000337 int delim, oldLen, newLen, deltaLen, offset;
Rob Landley3b890392006-05-04 20:56:43 +0000338 LINE *lp, *nlp;
339 int globalFlag, printFlag, didSub, needPrint;
340
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000341 if (bad_nums(num1, num2, "substitute"))
Rob Landley3b890392006-05-04 20:56:43 +0000342 return;
Rob Landley3b890392006-05-04 20:56:43 +0000343
344 globalFlag = FALSE;
345 printFlag = FALSE;
346 didSub = FALSE;
347 needPrint = FALSE;
348
349 /*
350 * Copy the command so we can modify it.
351 */
352 strcpy(buf, cmd);
353 cp = buf;
354
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000355 if (isblank(*cp) || (*cp == '\0')) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000356 bb_error_msg("bad delimiter for substitute");
Rob Landley3b890392006-05-04 20:56:43 +0000357 return;
358 }
359
360 delim = *cp++;
361 oldStr = cp;
362
363 cp = strchr(cp, delim);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000364 if (cp == NULL) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000365 bb_error_msg("missing 2nd delimiter for substitute");
Rob Landley3b890392006-05-04 20:56:43 +0000366 return;
367 }
368
369 *cp++ = '\0';
370
371 newStr = cp;
372 cp = strchr(cp, delim);
373
374 if (cp)
375 *cp++ = '\0';
376 else
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000377 cp = (char*)"";
Rob Landley3b890392006-05-04 20:56:43 +0000378
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000379 while (*cp) switch (*cp++) {
Rob Landley3b890392006-05-04 20:56:43 +0000380 case 'g':
381 globalFlag = TRUE;
382 break;
Rob Landley3b890392006-05-04 20:56:43 +0000383 case 'p':
384 printFlag = TRUE;
385 break;
Rob Landley3b890392006-05-04 20:56:43 +0000386 default:
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000387 bb_error_msg("unknown option for substitute");
Rob Landley3b890392006-05-04 20:56:43 +0000388 return;
389 }
390
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000391 if (*oldStr == '\0') {
392 if (searchString[0] == '\0') {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000393 bb_error_msg("no previous search string");
Rob Landley3b890392006-05-04 20:56:43 +0000394 return;
395 }
Rob Landley3b890392006-05-04 20:56:43 +0000396 oldStr = searchString;
397 }
398
399 if (oldStr != searchString)
400 strcpy(searchString, oldStr);
401
402 lp = findLine(num1);
Rob Landley3b890392006-05-04 20:56:43 +0000403 if (lp == NULL)
404 return;
405
406 oldLen = strlen(oldStr);
407 newLen = strlen(newStr);
408 deltaLen = newLen - oldLen;
409 offset = 0;
410 nlp = NULL;
411
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000412 while (num1 <= num2) {
Rob Landley3b890392006-05-04 20:56:43 +0000413 offset = findString(lp, oldStr, oldLen, offset);
414
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000415 if (offset < 0) {
416 if (needPrint) {
Rob Landley3b890392006-05-04 20:56:43 +0000417 printLines(num1, num1, FALSE);
418 needPrint = FALSE;
419 }
Rob Landley3b890392006-05-04 20:56:43 +0000420 offset = 0;
421 lp = lp->next;
422 num1++;
Rob Landley3b890392006-05-04 20:56:43 +0000423 continue;
424 }
425
426 needPrint = printFlag;
427 didSub = TRUE;
428 dirty = TRUE;
429
430 /*
431 * If the replacement string is the same size or shorter
432 * than the old string, then the substitution is easy.
433 */
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000434 if (deltaLen <= 0) {
Rob Landley3b890392006-05-04 20:56:43 +0000435 memcpy(&lp->data[offset], newStr, newLen);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000436 if (deltaLen) {
Rob Landley3b890392006-05-04 20:56:43 +0000437 memcpy(&lp->data[offset + newLen],
438 &lp->data[offset + oldLen],
439 lp->len - offset - oldLen);
440
441 lp->len += deltaLen;
442 }
Rob Landley3b890392006-05-04 20:56:43 +0000443 offset += newLen;
Rob Landley3b890392006-05-04 20:56:43 +0000444 if (globalFlag)
445 continue;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000446 if (needPrint) {
Rob Landley3b890392006-05-04 20:56:43 +0000447 printLines(num1, num1, FALSE);
448 needPrint = FALSE;
449 }
Rob Landley3b890392006-05-04 20:56:43 +0000450 lp = lp->next;
451 num1++;
Rob Landley3b890392006-05-04 20:56:43 +0000452 continue;
453 }
454
455 /*
456 * The new string is larger, so allocate a new line
Marek Polacekb0b88842011-04-16 17:33:43 +0200457 * structure and use that. Link it in place of
Rob Landley3b890392006-05-04 20:56:43 +0000458 * the old line structure.
459 */
Denys Vlasenko90377872010-01-08 09:07:50 +0100460 nlp = xmalloc(sizeof(LINE) + lp->len + deltaLen);
Rob Landley3b890392006-05-04 20:56:43 +0000461
462 nlp->len = lp->len + deltaLen;
463
464 memcpy(nlp->data, lp->data, offset);
Rob Landley3b890392006-05-04 20:56:43 +0000465 memcpy(&nlp->data[offset], newStr, newLen);
Rob Landley3b890392006-05-04 20:56:43 +0000466 memcpy(&nlp->data[offset + newLen],
467 &lp->data[offset + oldLen],
468 lp->len - offset - oldLen);
469
470 nlp->next = lp->next;
471 nlp->prev = lp->prev;
472 nlp->prev->next = nlp;
473 nlp->next->prev = nlp;
474
475 if (curLine == lp)
476 curLine = nlp;
477
478 free(lp);
479 lp = nlp;
480
481 offset += newLen;
482
483 if (globalFlag)
484 continue;
485
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000486 if (needPrint) {
Rob Landley3b890392006-05-04 20:56:43 +0000487 printLines(num1, num1, FALSE);
488 needPrint = FALSE;
489 }
490
491 lp = lp->next;
492 num1++;
493 }
494
495 if (!didSub)
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000496 bb_error_msg("no substitutions found for \"%s\"", oldStr);
Rob Landley3b890392006-05-04 20:56:43 +0000497}
498
499
500/*
501 * Search a line for the specified string starting at the specified
502 * offset in the line. Returns the offset of the found string, or -1.
503 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000504static int findString(const LINE *lp, const char *str, int len, int offset)
Rob Landley3b890392006-05-04 20:56:43 +0000505{
506 int left;
507 const char *cp, *ncp;
508
509 cp = &lp->data[offset];
510 left = lp->len - offset;
511
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000512 while (left >= len) {
Rob Landley3b890392006-05-04 20:56:43 +0000513 ncp = memchr(cp, *str, left);
Rob Landley3b890392006-05-04 20:56:43 +0000514 if (ncp == NULL)
515 return -1;
Rob Landley3b890392006-05-04 20:56:43 +0000516 left -= (ncp - cp);
Rob Landley3b890392006-05-04 20:56:43 +0000517 if (left < len)
518 return -1;
Rob Landley3b890392006-05-04 20:56:43 +0000519 cp = ncp;
Rob Landley3b890392006-05-04 20:56:43 +0000520 if (memcmp(cp, str, len) == 0)
521 return (cp - lp->data);
Rob Landley3b890392006-05-04 20:56:43 +0000522 cp++;
523 left--;
524 }
525
526 return -1;
527}
528
529
530/*
531 * Add lines which are typed in by the user.
532 * The lines are inserted just before the specified line number.
533 * The lines are terminated by a line containing a single dot (ugly!),
534 * or by an end of file.
535 */
536static void addLines(int num)
537{
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000538 int len;
539 char buf[USERSIZE + 1];
Rob Landley3b890392006-05-04 20:56:43 +0000540
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000541 while (1) {
542 /* Returns:
543 * -1 on read errors or EOF, or on bare Ctrl-D.
544 * 0 on ctrl-C,
545 * >0 length of input string, including terminating '\n'
546 */
Denys Vlasenko66c5b122011-02-08 05:07:02 +0100547 len = read_line_input(NULL, "", buf, sizeof(buf), /*timeout*/ -1);
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000548 if (len <= 0) {
549 /* Previously, ctrl-C was exiting to shell.
550 * Now we exit to ed prompt. Is in important? */
Rob Landley3b890392006-05-04 20:56:43 +0000551 return;
552 }
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000553 if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0'))
554 return;
Rob Landley3b890392006-05-04 20:56:43 +0000555 if (!insertLine(num++, buf, len))
556 return;
557 }
558}
559
560
561/*
562 * Parse a line number argument if it is present. This is a sum
563 * or difference of numbers, '.', '$', 'x, or a search string.
Bernhard Reutner-Fischerd9ed35c2006-05-19 12:38:30 +0000564 * Returns TRUE if successful (whether or not there was a number).
Rob Landley3b890392006-05-04 20:56:43 +0000565 * Returns FALSE if there was a parsing error, with a message output.
566 * Whether there was a number is returned indirectly, as is the number.
567 * The character pointer which stopped the scan is also returned.
568 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000569static int getNum(const char **retcp, smallint *retHaveNum, int *retNum)
Rob Landley3b890392006-05-04 20:56:43 +0000570{
571 const char *cp;
572 char *endStr, str[USERSIZE];
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000573 int value, num;
574 smallint haveNum, minus;
Rob Landley3b890392006-05-04 20:56:43 +0000575
576 cp = *retcp;
Rob Landley3b890392006-05-04 20:56:43 +0000577 value = 0;
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000578 haveNum = FALSE;
579 minus = 0;
Rob Landley3b890392006-05-04 20:56:43 +0000580
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000581 while (TRUE) {
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000582 cp = skip_blank(cp);
Rob Landley3b890392006-05-04 20:56:43 +0000583
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000584 switch (*cp) {
Rob Landley3b890392006-05-04 20:56:43 +0000585 case '.':
586 haveNum = TRUE;
587 num = curNum;
588 cp++;
589 break;
590
591 case '$':
592 haveNum = TRUE;
593 num = lastNum;
594 cp++;
595 break;
596
597 case '\'':
598 cp++;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000599 if ((*cp < 'a') || (*cp > 'z')) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000600 bb_error_msg("bad mark name");
Rob Landley3b890392006-05-04 20:56:43 +0000601 return FALSE;
602 }
Rob Landley3b890392006-05-04 20:56:43 +0000603 haveNum = TRUE;
604 num = marks[*cp++ - 'a'];
605 break;
606
607 case '/':
608 strcpy(str, ++cp);
609 endStr = strchr(str, '/');
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000610 if (endStr) {
Rob Landley3b890392006-05-04 20:56:43 +0000611 *endStr++ = '\0';
612 cp += (endStr - str);
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000613 } else
Rob Landley3b890392006-05-04 20:56:43 +0000614 cp = "";
Rob Landley3b890392006-05-04 20:56:43 +0000615 num = searchLines(str, curNum, lastNum);
Rob Landley3b890392006-05-04 20:56:43 +0000616 if (num == 0)
617 return FALSE;
Rob Landley3b890392006-05-04 20:56:43 +0000618 haveNum = TRUE;
619 break;
620
621 default:
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000622 if (!isdigit(*cp)) {
Rob Landley3b890392006-05-04 20:56:43 +0000623 *retcp = cp;
624 *retHaveNum = haveNum;
625 *retNum = value;
Rob Landley3b890392006-05-04 20:56:43 +0000626 return TRUE;
627 }
Rob Landley3b890392006-05-04 20:56:43 +0000628 num = 0;
Rob Landley3b890392006-05-04 20:56:43 +0000629 while (isdigit(*cp))
630 num = num * 10 + *cp++ - '0';
Rob Landley3b890392006-05-04 20:56:43 +0000631 haveNum = TRUE;
632 break;
633 }
634
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000635 value += (minus ? -num : num);
Rob Landley3b890392006-05-04 20:56:43 +0000636
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000637 cp = skip_blank(cp);
Rob Landley3b890392006-05-04 20:56:43 +0000638
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000639 switch (*cp) {
Rob Landley3b890392006-05-04 20:56:43 +0000640 case '-':
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000641 minus = 1;
Rob Landley3b890392006-05-04 20:56:43 +0000642 cp++;
643 break;
644
645 case '+':
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000646 minus = 0;
Rob Landley3b890392006-05-04 20:56:43 +0000647 cp++;
648 break;
649
650 default:
651 *retcp = cp;
652 *retHaveNum = haveNum;
653 *retNum = value;
Rob Landley3b890392006-05-04 20:56:43 +0000654 return TRUE;
655 }
656 }
657}
658
659
660/*
Rob Landley3b890392006-05-04 20:56:43 +0000661 * Read lines from a file at the specified line number.
662 * Returns TRUE if the file was successfully read.
663 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000664static int readLines(const char *file, int num)
Rob Landley3b890392006-05-04 20:56:43 +0000665{
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000666 int fd, cc;
Rob Landley3b890392006-05-04 20:56:43 +0000667 int len, lineCount, charCount;
668 char *cp;
669
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000670 if ((num < 1) || (num > lastNum + 1)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000671 bb_error_msg("bad line for read");
Rob Landley3b890392006-05-04 20:56:43 +0000672 return FALSE;
673 }
674
675 fd = open(file, 0);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000676 if (fd < 0) {
Marek Polacek7dd61e32010-09-12 17:06:43 +0200677 bb_simple_perror_msg(file);
Rob Landley3b890392006-05-04 20:56:43 +0000678 return FALSE;
679 }
680
681 bufPtr = bufBase;
682 bufUsed = 0;
683 lineCount = 0;
684 charCount = 0;
685 cc = 0;
686
687 printf("\"%s\", ", file);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100688 fflush_all();
Rob Landley3b890392006-05-04 20:56:43 +0000689
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000690 do {
Rob Landley3b890392006-05-04 20:56:43 +0000691 cp = memchr(bufPtr, '\n', bufUsed);
692
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000693 if (cp) {
Rob Landley3b890392006-05-04 20:56:43 +0000694 len = (cp - bufPtr) + 1;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000695 if (!insertLine(num, bufPtr, len)) {
Rob Landley3b890392006-05-04 20:56:43 +0000696 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000697 return FALSE;
698 }
Rob Landley3b890392006-05-04 20:56:43 +0000699 bufPtr += len;
700 bufUsed -= len;
701 charCount += len;
702 lineCount++;
703 num++;
Rob Landley3b890392006-05-04 20:56:43 +0000704 continue;
705 }
706
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000707 if (bufPtr != bufBase) {
Rob Landley3b890392006-05-04 20:56:43 +0000708 memcpy(bufBase, bufPtr, bufUsed);
709 bufPtr = bufBase + bufUsed;
710 }
711
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000712 if (bufUsed >= bufSize) {
Rob Landley3b890392006-05-04 20:56:43 +0000713 len = (bufSize * 3) / 2;
Denys Vlasenko90377872010-01-08 09:07:50 +0100714 cp = xrealloc(bufBase, len);
Rob Landley3b890392006-05-04 20:56:43 +0000715 bufBase = cp;
716 bufPtr = bufBase + bufUsed;
717 bufSize = len;
718 }
719
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000720 cc = safe_read(fd, bufPtr, bufSize - bufUsed);
Rob Landley3b890392006-05-04 20:56:43 +0000721 bufUsed += cc;
722 bufPtr = bufBase;
723
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000724 } while (cc > 0);
Rob Landley3b890392006-05-04 20:56:43 +0000725
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000726 if (cc < 0) {
Marek Polacek7dd61e32010-09-12 17:06:43 +0200727 bb_simple_perror_msg(file);
Rob Landley3b890392006-05-04 20:56:43 +0000728 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000729 return FALSE;
730 }
731
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000732 if (bufUsed) {
733 if (!insertLine(num, bufPtr, bufUsed)) {
Rob Landley3b890392006-05-04 20:56:43 +0000734 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000735 return -1;
736 }
Rob Landley3b890392006-05-04 20:56:43 +0000737 lineCount++;
738 charCount += bufUsed;
739 }
740
741 close(fd);
742
743 printf("%d lines%s, %d chars\n", lineCount,
744 (bufUsed ? " (incomplete)" : ""), charCount);
745
746 return TRUE;
747}
748
749
750/*
751 * Write the specified lines out to the specified file.
752 * Returns TRUE if successful, or FALSE on an error with a message output.
753 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000754static int writeLines(const char *file, int num1, int num2)
Rob Landley3b890392006-05-04 20:56:43 +0000755{
756 LINE *lp;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000757 int fd, lineCount, charCount;
Rob Landley3b890392006-05-04 20:56:43 +0000758
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000759 if (bad_nums(num1, num2, "write"))
Rob Landley3b890392006-05-04 20:56:43 +0000760 return FALSE;
Rob Landley3b890392006-05-04 20:56:43 +0000761
762 lineCount = 0;
763 charCount = 0;
764
765 fd = creat(file, 0666);
Rob Landley3b890392006-05-04 20:56:43 +0000766 if (fd < 0) {
Marek Polacek7dd61e32010-09-12 17:06:43 +0200767 bb_simple_perror_msg(file);
Rob Landley3b890392006-05-04 20:56:43 +0000768 return FALSE;
769 }
770
771 printf("\"%s\", ", file);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100772 fflush_all();
Rob Landley3b890392006-05-04 20:56:43 +0000773
774 lp = findLine(num1);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000775 if (lp == NULL) {
Rob Landley3b890392006-05-04 20:56:43 +0000776 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000777 return FALSE;
778 }
779
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000780 while (num1++ <= num2) {
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000781 if (full_write(fd, lp->data, lp->len) != lp->len) {
Marek Polacek7dd61e32010-09-12 17:06:43 +0200782 bb_simple_perror_msg(file);
Rob Landley3b890392006-05-04 20:56:43 +0000783 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000784 return FALSE;
785 }
Rob Landley3b890392006-05-04 20:56:43 +0000786 charCount += lp->len;
787 lineCount++;
788 lp = lp->next;
789 }
790
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000791 if (close(fd) < 0) {
Marek Polacek7dd61e32010-09-12 17:06:43 +0200792 bb_simple_perror_msg(file);
Rob Landley3b890392006-05-04 20:56:43 +0000793 return FALSE;
794 }
795
796 printf("%d lines, %d chars\n", lineCount, charCount);
Rob Landley3b890392006-05-04 20:56:43 +0000797 return TRUE;
798}
799
800
801/*
802 * Print lines in a specified range.
803 * The last line printed becomes the current line.
804 * If expandFlag is TRUE, then the line is printed specially to
805 * show magic characters.
806 */
807static int printLines(int num1, int num2, int expandFlag)
808{
809 const LINE *lp;
810 const char *cp;
811 int ch, count;
812
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000813 if (bad_nums(num1, num2, "print"))
Rob Landley3b890392006-05-04 20:56:43 +0000814 return FALSE;
Rob Landley3b890392006-05-04 20:56:43 +0000815
816 lp = findLine(num1);
Rob Landley3b890392006-05-04 20:56:43 +0000817 if (lp == NULL)
818 return FALSE;
819
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000820 while (num1 <= num2) {
821 if (!expandFlag) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000822 write(STDOUT_FILENO, lp->data, lp->len);
Rob Landley3b890392006-05-04 20:56:43 +0000823 setCurNum(num1++);
824 lp = lp->next;
Rob Landley3b890392006-05-04 20:56:43 +0000825 continue;
826 }
827
828 /*
829 * Show control characters and characters with the
830 * high bit set specially.
831 */
832 cp = lp->data;
833 count = lp->len;
834
835 if ((count > 0) && (cp[count - 1] == '\n'))
836 count--;
837
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000838 while (count-- > 0) {
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000839 ch = (unsigned char) *cp++;
840 fputc_printable(ch | PRINTABLE_META, stdout);
Rob Landley3b890392006-05-04 20:56:43 +0000841 }
842
843 fputs("$\n", stdout);
844
845 setCurNum(num1++);
846 lp = lp->next;
847 }
848
849 return TRUE;
850}
851
852
853/*
854 * Insert a new line with the specified text.
855 * The line is inserted so as to become the specified line,
856 * thus pushing any existing and further lines down one.
857 * The inserted line is also set to become the current line.
858 * Returns TRUE if successful.
859 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000860static int insertLine(int num, const char *data, int len)
Rob Landley3b890392006-05-04 20:56:43 +0000861{
862 LINE *newLp, *lp;
863
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000864 if ((num < 1) || (num > lastNum + 1)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000865 bb_error_msg("inserting at bad line number");
Rob Landley3b890392006-05-04 20:56:43 +0000866 return FALSE;
867 }
868
Denys Vlasenko90377872010-01-08 09:07:50 +0100869 newLp = xmalloc(sizeof(LINE) + len - 1);
Rob Landley3b890392006-05-04 20:56:43 +0000870
871 memcpy(newLp->data, data, len);
872 newLp->len = len;
873
874 if (num > lastNum)
875 lp = &lines;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000876 else {
Rob Landley3b890392006-05-04 20:56:43 +0000877 lp = findLine(num);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000878 if (lp == NULL) {
Rob Landley3b890392006-05-04 20:56:43 +0000879 free((char *) newLp);
Rob Landley3b890392006-05-04 20:56:43 +0000880 return FALSE;
881 }
882 }
883
884 newLp->next = lp;
885 newLp->prev = lp->prev;
886 lp->prev->next = newLp;
887 lp->prev = newLp;
888
889 lastNum++;
890 dirty = TRUE;
Rob Landley3b890392006-05-04 20:56:43 +0000891 return setCurNum(num);
892}
893
894
895/*
896 * Delete lines from the given range.
897 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000898static void deleteLines(int num1, int num2)
Rob Landley3b890392006-05-04 20:56:43 +0000899{
900 LINE *lp, *nlp, *plp;
901 int count;
902
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000903 if (bad_nums(num1, num2, "delete"))
904 return;
Rob Landley3b890392006-05-04 20:56:43 +0000905
906 lp = findLine(num1);
Rob Landley3b890392006-05-04 20:56:43 +0000907 if (lp == NULL)
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000908 return;
Rob Landley3b890392006-05-04 20:56:43 +0000909
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000910 if ((curNum >= num1) && (curNum <= num2)) {
Rob Landley3b890392006-05-04 20:56:43 +0000911 if (num2 < lastNum)
912 setCurNum(num2 + 1);
913 else if (num1 > 1)
914 setCurNum(num1 - 1);
915 else
916 curNum = 0;
917 }
918
919 count = num2 - num1 + 1;
Rob Landley3b890392006-05-04 20:56:43 +0000920 if (curNum > num2)
921 curNum -= count;
Rob Landley3b890392006-05-04 20:56:43 +0000922 lastNum -= count;
923
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000924 while (count-- > 0) {
Rob Landley3b890392006-05-04 20:56:43 +0000925 nlp = lp->next;
926 plp = lp->prev;
927 plp->next = nlp;
928 nlp->prev = plp;
Rob Landley3b890392006-05-04 20:56:43 +0000929 free(lp);
930 lp = nlp;
931 }
932
933 dirty = TRUE;
Rob Landley3b890392006-05-04 20:56:43 +0000934}
935
936
937/*
938 * Search for a line which contains the specified string.
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000939 * If the string is "", then the previously searched for string
Rob Landley3b890392006-05-04 20:56:43 +0000940 * is used. The currently searched for string is saved for future use.
941 * Returns the line number which matches, or 0 if there was no match
942 * with an error printed.
943 */
Denys Vlasenkoadf922e2009-10-08 14:35:37 +0200944static NOINLINE int searchLines(const char *str, int num1, int num2)
Rob Landley3b890392006-05-04 20:56:43 +0000945{
946 const LINE *lp;
947 int len;
948
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000949 if (bad_nums(num1, num2, "search"))
Rob Landley3b890392006-05-04 20:56:43 +0000950 return 0;
Rob Landley3b890392006-05-04 20:56:43 +0000951
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000952 if (*str == '\0') {
953 if (searchString[0] == '\0') {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000954 bb_error_msg("no previous search string");
Rob Landley3b890392006-05-04 20:56:43 +0000955 return 0;
956 }
Rob Landley3b890392006-05-04 20:56:43 +0000957 str = searchString;
958 }
959
960 if (str != searchString)
961 strcpy(searchString, str);
962
963 len = strlen(str);
964
965 lp = findLine(num1);
Rob Landley3b890392006-05-04 20:56:43 +0000966 if (lp == NULL)
967 return 0;
968
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000969 while (num1 <= num2) {
Rob Landley3b890392006-05-04 20:56:43 +0000970 if (findString(lp, str, len, 0) >= 0)
971 return num1;
Rob Landley3b890392006-05-04 20:56:43 +0000972 num1++;
973 lp = lp->next;
974 }
975
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100976 bb_error_msg("can't find string \"%s\"", str);
Rob Landley3b890392006-05-04 20:56:43 +0000977 return 0;
978}
979
980
981/*
982 * Return a pointer to the specified line number.
983 */
984static LINE *findLine(int num)
985{
986 LINE *lp;
987 int lnum;
988
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000989 if ((num < 1) || (num > lastNum)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000990 bb_error_msg("line number %d does not exist", num);
Rob Landley3b890392006-05-04 20:56:43 +0000991 return NULL;
992 }
993
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000994 if (curNum <= 0) {
Rob Landley3b890392006-05-04 20:56:43 +0000995 curNum = 1;
996 curLine = lines.next;
997 }
998
999 if (num == curNum)
1000 return curLine;
1001
1002 lp = curLine;
1003 lnum = curNum;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +00001004 if (num < (curNum / 2)) {
Rob Landley3b890392006-05-04 20:56:43 +00001005 lp = lines.next;
1006 lnum = 1;
Denis Vlasenkod9391b12007-09-25 11:55:57 +00001007 } else if (num > ((curNum + lastNum) / 2)) {
Rob Landley3b890392006-05-04 20:56:43 +00001008 lp = lines.prev;
1009 lnum = lastNum;
1010 }
1011
Denis Vlasenko610c4aa2006-11-30 20:57:50 +00001012 while (lnum < num) {
Rob Landley3b890392006-05-04 20:56:43 +00001013 lp = lp->next;
1014 lnum++;
1015 }
1016
Denis Vlasenko610c4aa2006-11-30 20:57:50 +00001017 while (lnum > num) {
Rob Landley3b890392006-05-04 20:56:43 +00001018 lp = lp->prev;
1019 lnum--;
1020 }
Rob Landley3b890392006-05-04 20:56:43 +00001021 return lp;
1022}
1023
1024
1025/*
1026 * Set the current line number.
1027 * Returns TRUE if successful.
1028 */
1029static int setCurNum(int num)
1030{
1031 LINE *lp;
1032
1033 lp = findLine(num);
Rob Landley3b890392006-05-04 20:56:43 +00001034 if (lp == NULL)
1035 return FALSE;
Rob Landley3b890392006-05-04 20:56:43 +00001036 curNum = num;
1037 curLine = lp;
Rob Landley3b890392006-05-04 20:56:43 +00001038 return TRUE;
1039}