blob: a4c41909937ba732e8d2a3508d8d7423c9a7f7d4 [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
Denys Vlasenko73225b62013-11-13 12:45:33 +010010//config:config ED
11//config: bool "ed"
12//config: default y
13//config: help
14//config: The original 1970's Unix text editor, from the days of teletypes.
15//config: Small, simple, evil. Part of SUSv3. If you're not already using
16//config: this, you don't need it.
17
18//kbuild:lib-$(CONFIG_ED) += ed.o
19
20//applet:IF_ED(APPLET(ed, BB_DIR_BIN, BB_SUID_DROP))
21
Pere Orga6a3e01d2011-04-01 22:56:30 +020022//usage:#define ed_trivial_usage ""
23//usage:#define ed_full_usage ""
24
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000025#include "libbb.h"
Rob Landley3b890392006-05-04 20:56:43 +000026
Denis Vlasenkod9391b12007-09-25 11:55:57 +000027typedef struct LINE {
28 struct LINE *next;
29 struct LINE *prev;
30 int len;
31 char data[1];
32} LINE;
33
34
Denis Vlasenko74324c82007-06-04 10:16:52 +000035#define searchString bb_common_bufsiz1
36
Denis Vlasenko55f30b02007-03-24 22:42:29 +000037enum {
Denis Vlasenko74324c82007-06-04 10:16:52 +000038 USERSIZE = sizeof(searchString) > 1024 ? 1024
39 : sizeof(searchString) - 1, /* max line length typed in by user */
Denis Vlasenko55f30b02007-03-24 22:42:29 +000040 INITBUF_SIZE = 1024, /* initial buffer size */
41};
42
Denis Vlasenkod9391b12007-09-25 11:55:57 +000043struct globals {
44 int curNum;
45 int lastNum;
46 int bufUsed;
47 int bufSize;
48 LINE *curLine;
49 char *bufBase;
50 char *bufPtr;
51 char *fileName;
52 LINE lines;
53 smallint dirty;
54 int marks[26];
55};
56#define G (*ptr_to_globals)
57#define curLine (G.curLine )
58#define bufBase (G.bufBase )
59#define bufPtr (G.bufPtr )
60#define fileName (G.fileName )
61#define curNum (G.curNum )
62#define lastNum (G.lastNum )
63#define bufUsed (G.bufUsed )
64#define bufSize (G.bufSize )
65#define dirty (G.dirty )
66#define lines (G.lines )
67#define marks (G.marks )
68#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +000069 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkod9391b12007-09-25 11:55:57 +000070} while (0)
Rob Landley3b890392006-05-04 20:56:43 +000071
Rob Landley3b890392006-05-04 20:56:43 +000072
73static void doCommands(void);
74static void subCommand(const char *cmd, int num1, int num2);
Denis Vlasenkod9391b12007-09-25 11:55:57 +000075static int getNum(const char **retcp, smallint *retHaveNum, int *retNum);
Rob Landley3b890392006-05-04 20:56:43 +000076static int setCurNum(int num);
Rob Landley3b890392006-05-04 20:56:43 +000077static void addLines(int num);
78static int insertLine(int num, const char *data, int len);
Denis Vlasenkod9391b12007-09-25 11:55:57 +000079static void deleteLines(int num1, int num2);
Rob Landley3b890392006-05-04 20:56:43 +000080static int printLines(int num1, int num2, int expandFlag);
81static int writeLines(const char *file, int num1, int num2);
82static int readLines(const char *file, int num);
83static int searchLines(const char *str, int num1, int num2);
84static LINE *findLine(int num);
Rob Landley3b890392006-05-04 20:56:43 +000085static int findString(const LINE *lp, const char * str, int len, int offset);
86
Denis Vlasenkod9391b12007-09-25 11:55:57 +000087
88static int bad_nums(int num1, int num2, const char *for_what)
89{
90 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
91 bb_error_msg("bad line range for %s", for_what);
92 return 1;
93 }
94 return 0;
95}
96
97
98static char *skip_blank(const char *cp)
99{
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000100 while (isblank(*cp))
101 cp++;
102 return (char *)cp;
103}
104
105
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000106int ed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000107int ed_main(int argc UNUSED_PARAM, char **argv)
Rob Landley3b890392006-05-04 20:56:43 +0000108{
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000109 INIT_G();
110
111 bufSize = INITBUF_SIZE;
112 bufBase = xmalloc(bufSize);
113 bufPtr = bufBase;
114 lines.next = &lines;
115 lines.prev = &lines;
Rob Landley3b890392006-05-04 20:56:43 +0000116
Denis Vlasenko1d426652008-03-17 09:09:09 +0000117 if (argv[1]) {
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000118 fileName = xstrdup(argv[1]);
Rob Landley3b890392006-05-04 20:56:43 +0000119 if (!readLines(fileName, 1)) {
Rob Landley3b890392006-05-04 20:56:43 +0000120 return EXIT_SUCCESS;
121 }
Rob Landley3b890392006-05-04 20:56:43 +0000122 if (lastNum)
123 setCurNum(1);
Rob Landley3b890392006-05-04 20:56:43 +0000124 dirty = FALSE;
125 }
126
127 doCommands();
Rob Landley3b890392006-05-04 20:56:43 +0000128 return EXIT_SUCCESS;
129}
130
131/*
132 * Read commands until we are told to stop.
133 */
134static void doCommands(void)
135{
136 const char *cp;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000137 char *endbuf, buf[USERSIZE];
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000138 int len, num1, num2;
139 smallint have1, have2;
Rob Landley3b890392006-05-04 20:56:43 +0000140
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000141 while (TRUE) {
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000142 /* Returns:
143 * -1 on read errors or EOF, or on bare Ctrl-D.
144 * 0 on ctrl-C,
145 * >0 length of input string, including terminating '\n'
146 */
Denys Vlasenko66c5b122011-02-08 05:07:02 +0100147 len = read_line_input(NULL, ": ", buf, sizeof(buf), /*timeout*/ -1);
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000148 if (len <= 0)
Rob Landley3b890392006-05-04 20:56:43 +0000149 return;
Rob Landley3b890392006-05-04 20:56:43 +0000150 endbuf = &buf[len - 1];
Rob Landley3b890392006-05-04 20:56:43 +0000151 while ((endbuf > buf) && isblank(endbuf[-1]))
152 endbuf--;
Rob Landley3b890392006-05-04 20:56:43 +0000153 *endbuf = '\0';
154
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000155 cp = skip_blank(buf);
Rob Landley3b890392006-05-04 20:56:43 +0000156 have1 = FALSE;
157 have2 = FALSE;
158
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000159 if ((curNum == 0) && (lastNum > 0)) {
Rob Landley3b890392006-05-04 20:56:43 +0000160 curNum = 1;
161 curLine = lines.next;
162 }
163
164 if (!getNum(&cp, &have1, &num1))
165 continue;
166
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000167 cp = skip_blank(cp);
Rob Landley3b890392006-05-04 20:56:43 +0000168
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000169 if (*cp == ',') {
Rob Landley3b890392006-05-04 20:56:43 +0000170 cp++;
Rob Landley3b890392006-05-04 20:56:43 +0000171 if (!getNum(&cp, &have2, &num2))
172 continue;
Rob Landley3b890392006-05-04 20:56:43 +0000173 if (!have1)
174 num1 = 1;
Rob Landley3b890392006-05-04 20:56:43 +0000175 if (!have2)
176 num2 = lastNum;
Rob Landley3b890392006-05-04 20:56:43 +0000177 have1 = TRUE;
178 have2 = TRUE;
179 }
Rob Landley3b890392006-05-04 20:56:43 +0000180 if (!have1)
181 num1 = curNum;
Rob Landley3b890392006-05-04 20:56:43 +0000182 if (!have2)
183 num2 = num1;
184
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000185 switch (*cp++) {
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000186 case 'a':
187 addLines(num1 + 1);
188 break;
Rob Landley3b890392006-05-04 20:56:43 +0000189
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000190 case 'c':
191 deleteLines(num1, num2);
192 addLines(num1);
193 break;
Rob Landley3b890392006-05-04 20:56:43 +0000194
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000195 case 'd':
196 deleteLines(num1, num2);
197 break;
Rob Landley3b890392006-05-04 20:56:43 +0000198
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000199 case 'f':
200 if (*cp && !isblank(*cp)) {
201 bb_error_msg("bad file command");
Rob Landley3b890392006-05-04 20:56:43 +0000202 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000203 }
204 cp = skip_blank(cp);
205 if (*cp == '\0') {
206 if (fileName)
207 printf("\"%s\"\n", fileName);
208 else
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200209 puts("No file name");
Rob Landley3b890392006-05-04 20:56:43 +0000210 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000211 }
212 free(fileName);
213 fileName = xstrdup(cp);
214 break;
Rob Landley3b890392006-05-04 20:56:43 +0000215
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000216 case 'i':
217 addLines(num1);
218 break;
219
220 case 'k':
221 cp = skip_blank(cp);
222 if ((*cp < 'a') || (*cp > 'z') || cp[1]) {
223 bb_error_msg("bad mark name");
Rob Landley3b890392006-05-04 20:56:43 +0000224 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000225 }
226 marks[*cp - 'a'] = num2;
227 break;
Rob Landley3b890392006-05-04 20:56:43 +0000228
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000229 case 'l':
230 printLines(num1, num2, TRUE);
231 break;
232
233 case 'p':
234 printLines(num1, num2, FALSE);
235 break;
236
237 case 'q':
238 cp = skip_blank(cp);
239 if (have1 || *cp) {
240 bb_error_msg("bad quit command");
Rob Landley3b890392006-05-04 20:56:43 +0000241 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000242 }
243 if (!dirty)
244 return;
Denys Vlasenko66c5b122011-02-08 05:07:02 +0100245 len = read_line_input(NULL, "Really quit? ", buf, 16, /*timeout*/ -1);
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000246 /* read error/EOF - no way to continue */
247 if (len < 0)
248 return;
249 cp = skip_blank(buf);
250 if ((*cp | 0x20) == 'y') /* Y or y */
251 return;
252 break;
Rob Landley3b890392006-05-04 20:56:43 +0000253
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000254 case 'r':
255 if (*cp && !isblank(*cp)) {
256 bb_error_msg("bad read command");
Rob Landley3b890392006-05-04 20:56:43 +0000257 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000258 }
259 cp = skip_blank(cp);
260 if (*cp == '\0') {
261 bb_error_msg("no file name");
Rob Landley3b890392006-05-04 20:56:43 +0000262 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000263 }
264 if (!have1)
265 num1 = lastNum;
266 if (readLines(cp, num1 + 1))
Rob Landley3b890392006-05-04 20:56:43 +0000267 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000268 if (fileName == NULL)
269 fileName = xstrdup(cp);
270 break;
Rob Landley3b890392006-05-04 20:56:43 +0000271
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000272 case 's':
273 subCommand(cp, num1, num2);
274 break;
275
276 case 'w':
277 if (*cp && !isblank(*cp)) {
278 bb_error_msg("bad write command");
Rob Landley3b890392006-05-04 20:56:43 +0000279 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000280 }
281 cp = skip_blank(cp);
282 if (!have1) {
283 num1 = 1;
284 num2 = lastNum;
285 }
286 if (*cp == '\0')
287 cp = fileName;
288 if (cp == NULL) {
289 bb_error_msg("no file name specified");
Rob Landley3b890392006-05-04 20:56:43 +0000290 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000291 }
292 writeLines(cp, num1, num2);
293 break;
Rob Landley3b890392006-05-04 20:56:43 +0000294
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000295 case 'z':
296 switch (*cp) {
Rob Landley3b890392006-05-04 20:56:43 +0000297 case '-':
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000298 printLines(curNum - 21, curNum, FALSE);
Rob Landley3b890392006-05-04 20:56:43 +0000299 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000300 case '.':
301 printLines(curNum - 11, curNum + 10, FALSE);
Rob Landley3b890392006-05-04 20:56:43 +0000302 break;
Rob Landley3b890392006-05-04 20:56:43 +0000303 default:
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000304 printLines(curNum, curNum + 21, FALSE);
Rob Landley3b890392006-05-04 20:56:43 +0000305 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000306 }
307 break;
308
309 case '.':
310 if (have1) {
311 bb_error_msg("no arguments allowed");
312 break;
313 }
314 printLines(curNum, curNum, FALSE);
315 break;
316
317 case '-':
318 if (setCurNum(curNum - 1))
319 printLines(curNum, curNum, FALSE);
320 break;
321
322 case '=':
323 printf("%d\n", num1);
324 break;
325 case '\0':
326 if (have1) {
327 printLines(num2, num2, FALSE);
328 break;
329 }
330 if (setCurNum(curNum + 1))
331 printLines(curNum, curNum, FALSE);
332 break;
333
334 default:
335 bb_error_msg("unimplemented command");
336 break;
Rob Landley3b890392006-05-04 20:56:43 +0000337 }
338 }
339}
340
341
342/*
343 * Do the substitute command.
344 * The current line is set to the last substitution done.
345 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000346static void subCommand(const char *cmd, int num1, int num2)
Rob Landley3b890392006-05-04 20:56:43 +0000347{
348 char *cp, *oldStr, *newStr, buf[USERSIZE];
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000349 int delim, oldLen, newLen, deltaLen, offset;
Rob Landley3b890392006-05-04 20:56:43 +0000350 LINE *lp, *nlp;
351 int globalFlag, printFlag, didSub, needPrint;
352
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000353 if (bad_nums(num1, num2, "substitute"))
Rob Landley3b890392006-05-04 20:56:43 +0000354 return;
Rob Landley3b890392006-05-04 20:56:43 +0000355
356 globalFlag = FALSE;
357 printFlag = FALSE;
358 didSub = FALSE;
359 needPrint = FALSE;
360
361 /*
362 * Copy the command so we can modify it.
363 */
364 strcpy(buf, cmd);
365 cp = buf;
366
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000367 if (isblank(*cp) || (*cp == '\0')) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000368 bb_error_msg("bad delimiter for substitute");
Rob Landley3b890392006-05-04 20:56:43 +0000369 return;
370 }
371
372 delim = *cp++;
373 oldStr = cp;
374
375 cp = strchr(cp, delim);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000376 if (cp == NULL) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000377 bb_error_msg("missing 2nd delimiter for substitute");
Rob Landley3b890392006-05-04 20:56:43 +0000378 return;
379 }
380
381 *cp++ = '\0';
382
383 newStr = cp;
384 cp = strchr(cp, delim);
385
386 if (cp)
387 *cp++ = '\0';
388 else
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000389 cp = (char*)"";
Rob Landley3b890392006-05-04 20:56:43 +0000390
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000391 while (*cp) switch (*cp++) {
Rob Landley3b890392006-05-04 20:56:43 +0000392 case 'g':
393 globalFlag = TRUE;
394 break;
Rob Landley3b890392006-05-04 20:56:43 +0000395 case 'p':
396 printFlag = TRUE;
397 break;
Rob Landley3b890392006-05-04 20:56:43 +0000398 default:
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000399 bb_error_msg("unknown option for substitute");
Rob Landley3b890392006-05-04 20:56:43 +0000400 return;
401 }
402
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000403 if (*oldStr == '\0') {
404 if (searchString[0] == '\0') {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000405 bb_error_msg("no previous search string");
Rob Landley3b890392006-05-04 20:56:43 +0000406 return;
407 }
Rob Landley3b890392006-05-04 20:56:43 +0000408 oldStr = searchString;
409 }
410
411 if (oldStr != searchString)
412 strcpy(searchString, oldStr);
413
414 lp = findLine(num1);
Rob Landley3b890392006-05-04 20:56:43 +0000415 if (lp == NULL)
416 return;
417
418 oldLen = strlen(oldStr);
419 newLen = strlen(newStr);
420 deltaLen = newLen - oldLen;
421 offset = 0;
422 nlp = NULL;
423
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000424 while (num1 <= num2) {
Rob Landley3b890392006-05-04 20:56:43 +0000425 offset = findString(lp, oldStr, oldLen, offset);
426
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000427 if (offset < 0) {
428 if (needPrint) {
Rob Landley3b890392006-05-04 20:56:43 +0000429 printLines(num1, num1, FALSE);
430 needPrint = FALSE;
431 }
Rob Landley3b890392006-05-04 20:56:43 +0000432 offset = 0;
433 lp = lp->next;
434 num1++;
Rob Landley3b890392006-05-04 20:56:43 +0000435 continue;
436 }
437
438 needPrint = printFlag;
439 didSub = TRUE;
440 dirty = TRUE;
441
442 /*
443 * If the replacement string is the same size or shorter
444 * than the old string, then the substitution is easy.
445 */
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000446 if (deltaLen <= 0) {
Rob Landley3b890392006-05-04 20:56:43 +0000447 memcpy(&lp->data[offset], newStr, newLen);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000448 if (deltaLen) {
Rob Landley3b890392006-05-04 20:56:43 +0000449 memcpy(&lp->data[offset + newLen],
450 &lp->data[offset + oldLen],
451 lp->len - offset - oldLen);
452
453 lp->len += deltaLen;
454 }
Rob Landley3b890392006-05-04 20:56:43 +0000455 offset += newLen;
Rob Landley3b890392006-05-04 20:56:43 +0000456 if (globalFlag)
457 continue;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000458 if (needPrint) {
Rob Landley3b890392006-05-04 20:56:43 +0000459 printLines(num1, num1, FALSE);
460 needPrint = FALSE;
461 }
Rob Landley3b890392006-05-04 20:56:43 +0000462 lp = lp->next;
463 num1++;
Rob Landley3b890392006-05-04 20:56:43 +0000464 continue;
465 }
466
467 /*
468 * The new string is larger, so allocate a new line
Marek Polacekb0b88842011-04-16 17:33:43 +0200469 * structure and use that. Link it in place of
Rob Landley3b890392006-05-04 20:56:43 +0000470 * the old line structure.
471 */
Denys Vlasenko90377872010-01-08 09:07:50 +0100472 nlp = xmalloc(sizeof(LINE) + lp->len + deltaLen);
Rob Landley3b890392006-05-04 20:56:43 +0000473
474 nlp->len = lp->len + deltaLen;
475
476 memcpy(nlp->data, lp->data, offset);
Rob Landley3b890392006-05-04 20:56:43 +0000477 memcpy(&nlp->data[offset], newStr, newLen);
Rob Landley3b890392006-05-04 20:56:43 +0000478 memcpy(&nlp->data[offset + newLen],
479 &lp->data[offset + oldLen],
480 lp->len - offset - oldLen);
481
482 nlp->next = lp->next;
483 nlp->prev = lp->prev;
484 nlp->prev->next = nlp;
485 nlp->next->prev = nlp;
486
487 if (curLine == lp)
488 curLine = nlp;
489
490 free(lp);
491 lp = nlp;
492
493 offset += newLen;
494
495 if (globalFlag)
496 continue;
497
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000498 if (needPrint) {
Rob Landley3b890392006-05-04 20:56:43 +0000499 printLines(num1, num1, FALSE);
500 needPrint = FALSE;
501 }
502
503 lp = lp->next;
504 num1++;
505 }
506
507 if (!didSub)
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000508 bb_error_msg("no substitutions found for \"%s\"", oldStr);
Rob Landley3b890392006-05-04 20:56:43 +0000509}
510
511
512/*
513 * Search a line for the specified string starting at the specified
514 * offset in the line. Returns the offset of the found string, or -1.
515 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000516static int findString(const LINE *lp, const char *str, int len, int offset)
Rob Landley3b890392006-05-04 20:56:43 +0000517{
518 int left;
519 const char *cp, *ncp;
520
521 cp = &lp->data[offset];
522 left = lp->len - offset;
523
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000524 while (left >= len) {
Rob Landley3b890392006-05-04 20:56:43 +0000525 ncp = memchr(cp, *str, left);
Rob Landley3b890392006-05-04 20:56:43 +0000526 if (ncp == NULL)
527 return -1;
Rob Landley3b890392006-05-04 20:56:43 +0000528 left -= (ncp - cp);
Rob Landley3b890392006-05-04 20:56:43 +0000529 if (left < len)
530 return -1;
Rob Landley3b890392006-05-04 20:56:43 +0000531 cp = ncp;
Rob Landley3b890392006-05-04 20:56:43 +0000532 if (memcmp(cp, str, len) == 0)
533 return (cp - lp->data);
Rob Landley3b890392006-05-04 20:56:43 +0000534 cp++;
535 left--;
536 }
537
538 return -1;
539}
540
541
542/*
543 * Add lines which are typed in by the user.
544 * The lines are inserted just before the specified line number.
545 * The lines are terminated by a line containing a single dot (ugly!),
546 * or by an end of file.
547 */
548static void addLines(int num)
549{
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000550 int len;
551 char buf[USERSIZE + 1];
Rob Landley3b890392006-05-04 20:56:43 +0000552
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000553 while (1) {
554 /* Returns:
555 * -1 on read errors or EOF, or on bare Ctrl-D.
556 * 0 on ctrl-C,
557 * >0 length of input string, including terminating '\n'
558 */
Denys Vlasenko66c5b122011-02-08 05:07:02 +0100559 len = read_line_input(NULL, "", buf, sizeof(buf), /*timeout*/ -1);
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000560 if (len <= 0) {
561 /* Previously, ctrl-C was exiting to shell.
562 * Now we exit to ed prompt. Is in important? */
Rob Landley3b890392006-05-04 20:56:43 +0000563 return;
564 }
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000565 if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0'))
566 return;
Rob Landley3b890392006-05-04 20:56:43 +0000567 if (!insertLine(num++, buf, len))
568 return;
569 }
570}
571
572
573/*
574 * Parse a line number argument if it is present. This is a sum
575 * or difference of numbers, '.', '$', 'x, or a search string.
Bernhard Reutner-Fischerd9ed35c2006-05-19 12:38:30 +0000576 * Returns TRUE if successful (whether or not there was a number).
Rob Landley3b890392006-05-04 20:56:43 +0000577 * Returns FALSE if there was a parsing error, with a message output.
578 * Whether there was a number is returned indirectly, as is the number.
579 * The character pointer which stopped the scan is also returned.
580 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000581static int getNum(const char **retcp, smallint *retHaveNum, int *retNum)
Rob Landley3b890392006-05-04 20:56:43 +0000582{
583 const char *cp;
584 char *endStr, str[USERSIZE];
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000585 int value, num;
586 smallint haveNum, minus;
Rob Landley3b890392006-05-04 20:56:43 +0000587
588 cp = *retcp;
Rob Landley3b890392006-05-04 20:56:43 +0000589 value = 0;
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000590 haveNum = FALSE;
591 minus = 0;
Rob Landley3b890392006-05-04 20:56:43 +0000592
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000593 while (TRUE) {
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000594 cp = skip_blank(cp);
Rob Landley3b890392006-05-04 20:56:43 +0000595
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000596 switch (*cp) {
Rob Landley3b890392006-05-04 20:56:43 +0000597 case '.':
598 haveNum = TRUE;
599 num = curNum;
600 cp++;
601 break;
602
603 case '$':
604 haveNum = TRUE;
605 num = lastNum;
606 cp++;
607 break;
608
609 case '\'':
610 cp++;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000611 if ((*cp < 'a') || (*cp > 'z')) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000612 bb_error_msg("bad mark name");
Rob Landley3b890392006-05-04 20:56:43 +0000613 return FALSE;
614 }
Rob Landley3b890392006-05-04 20:56:43 +0000615 haveNum = TRUE;
616 num = marks[*cp++ - 'a'];
617 break;
618
619 case '/':
620 strcpy(str, ++cp);
621 endStr = strchr(str, '/');
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000622 if (endStr) {
Rob Landley3b890392006-05-04 20:56:43 +0000623 *endStr++ = '\0';
624 cp += (endStr - str);
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000625 } else
Rob Landley3b890392006-05-04 20:56:43 +0000626 cp = "";
Rob Landley3b890392006-05-04 20:56:43 +0000627 num = searchLines(str, curNum, lastNum);
Rob Landley3b890392006-05-04 20:56:43 +0000628 if (num == 0)
629 return FALSE;
Rob Landley3b890392006-05-04 20:56:43 +0000630 haveNum = TRUE;
631 break;
632
633 default:
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000634 if (!isdigit(*cp)) {
Rob Landley3b890392006-05-04 20:56:43 +0000635 *retcp = cp;
636 *retHaveNum = haveNum;
637 *retNum = value;
Rob Landley3b890392006-05-04 20:56:43 +0000638 return TRUE;
639 }
Rob Landley3b890392006-05-04 20:56:43 +0000640 num = 0;
Rob Landley3b890392006-05-04 20:56:43 +0000641 while (isdigit(*cp))
642 num = num * 10 + *cp++ - '0';
Rob Landley3b890392006-05-04 20:56:43 +0000643 haveNum = TRUE;
644 break;
645 }
646
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000647 value += (minus ? -num : num);
Rob Landley3b890392006-05-04 20:56:43 +0000648
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000649 cp = skip_blank(cp);
Rob Landley3b890392006-05-04 20:56:43 +0000650
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000651 switch (*cp) {
Rob Landley3b890392006-05-04 20:56:43 +0000652 case '-':
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000653 minus = 1;
Rob Landley3b890392006-05-04 20:56:43 +0000654 cp++;
655 break;
656
657 case '+':
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000658 minus = 0;
Rob Landley3b890392006-05-04 20:56:43 +0000659 cp++;
660 break;
661
662 default:
663 *retcp = cp;
664 *retHaveNum = haveNum;
665 *retNum = value;
Rob Landley3b890392006-05-04 20:56:43 +0000666 return TRUE;
667 }
668 }
669}
670
671
672/*
Rob Landley3b890392006-05-04 20:56:43 +0000673 * Read lines from a file at the specified line number.
674 * Returns TRUE if the file was successfully read.
675 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000676static int readLines(const char *file, int num)
Rob Landley3b890392006-05-04 20:56:43 +0000677{
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000678 int fd, cc;
Rob Landley3b890392006-05-04 20:56:43 +0000679 int len, lineCount, charCount;
680 char *cp;
681
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000682 if ((num < 1) || (num > lastNum + 1)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000683 bb_error_msg("bad line for read");
Rob Landley3b890392006-05-04 20:56:43 +0000684 return FALSE;
685 }
686
687 fd = open(file, 0);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000688 if (fd < 0) {
Marek Polacek7dd61e32010-09-12 17:06:43 +0200689 bb_simple_perror_msg(file);
Rob Landley3b890392006-05-04 20:56:43 +0000690 return FALSE;
691 }
692
693 bufPtr = bufBase;
694 bufUsed = 0;
695 lineCount = 0;
696 charCount = 0;
697 cc = 0;
698
699 printf("\"%s\", ", file);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100700 fflush_all();
Rob Landley3b890392006-05-04 20:56:43 +0000701
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000702 do {
Rob Landley3b890392006-05-04 20:56:43 +0000703 cp = memchr(bufPtr, '\n', bufUsed);
704
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000705 if (cp) {
Rob Landley3b890392006-05-04 20:56:43 +0000706 len = (cp - bufPtr) + 1;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000707 if (!insertLine(num, bufPtr, len)) {
Rob Landley3b890392006-05-04 20:56:43 +0000708 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000709 return FALSE;
710 }
Rob Landley3b890392006-05-04 20:56:43 +0000711 bufPtr += len;
712 bufUsed -= len;
713 charCount += len;
714 lineCount++;
715 num++;
Rob Landley3b890392006-05-04 20:56:43 +0000716 continue;
717 }
718
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000719 if (bufPtr != bufBase) {
Rob Landley3b890392006-05-04 20:56:43 +0000720 memcpy(bufBase, bufPtr, bufUsed);
721 bufPtr = bufBase + bufUsed;
722 }
723
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000724 if (bufUsed >= bufSize) {
Rob Landley3b890392006-05-04 20:56:43 +0000725 len = (bufSize * 3) / 2;
Denys Vlasenko90377872010-01-08 09:07:50 +0100726 cp = xrealloc(bufBase, len);
Rob Landley3b890392006-05-04 20:56:43 +0000727 bufBase = cp;
728 bufPtr = bufBase + bufUsed;
729 bufSize = len;
730 }
731
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000732 cc = safe_read(fd, bufPtr, bufSize - bufUsed);
Rob Landley3b890392006-05-04 20:56:43 +0000733 bufUsed += cc;
734 bufPtr = bufBase;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000735 } while (cc > 0);
Rob Landley3b890392006-05-04 20:56:43 +0000736
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000737 if (cc < 0) {
Marek Polacek7dd61e32010-09-12 17:06:43 +0200738 bb_simple_perror_msg(file);
Rob Landley3b890392006-05-04 20:56:43 +0000739 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000740 return FALSE;
741 }
742
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000743 if (bufUsed) {
744 if (!insertLine(num, bufPtr, bufUsed)) {
Rob Landley3b890392006-05-04 20:56:43 +0000745 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000746 return -1;
747 }
Rob Landley3b890392006-05-04 20:56:43 +0000748 lineCount++;
749 charCount += bufUsed;
750 }
751
752 close(fd);
753
754 printf("%d lines%s, %d chars\n", lineCount,
755 (bufUsed ? " (incomplete)" : ""), charCount);
756
757 return TRUE;
758}
759
760
761/*
762 * Write the specified lines out to the specified file.
763 * Returns TRUE if successful, or FALSE on an error with a message output.
764 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000765static int writeLines(const char *file, int num1, int num2)
Rob Landley3b890392006-05-04 20:56:43 +0000766{
767 LINE *lp;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000768 int fd, lineCount, charCount;
Rob Landley3b890392006-05-04 20:56:43 +0000769
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000770 if (bad_nums(num1, num2, "write"))
Rob Landley3b890392006-05-04 20:56:43 +0000771 return FALSE;
Rob Landley3b890392006-05-04 20:56:43 +0000772
773 lineCount = 0;
774 charCount = 0;
775
776 fd = creat(file, 0666);
Rob Landley3b890392006-05-04 20:56:43 +0000777 if (fd < 0) {
Marek Polacek7dd61e32010-09-12 17:06:43 +0200778 bb_simple_perror_msg(file);
Rob Landley3b890392006-05-04 20:56:43 +0000779 return FALSE;
780 }
781
782 printf("\"%s\", ", file);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100783 fflush_all();
Rob Landley3b890392006-05-04 20:56:43 +0000784
785 lp = findLine(num1);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000786 if (lp == NULL) {
Rob Landley3b890392006-05-04 20:56:43 +0000787 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000788 return FALSE;
789 }
790
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000791 while (num1++ <= num2) {
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000792 if (full_write(fd, lp->data, lp->len) != lp->len) {
Marek Polacek7dd61e32010-09-12 17:06:43 +0200793 bb_simple_perror_msg(file);
Rob Landley3b890392006-05-04 20:56:43 +0000794 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000795 return FALSE;
796 }
Rob Landley3b890392006-05-04 20:56:43 +0000797 charCount += lp->len;
798 lineCount++;
799 lp = lp->next;
800 }
801
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000802 if (close(fd) < 0) {
Marek Polacek7dd61e32010-09-12 17:06:43 +0200803 bb_simple_perror_msg(file);
Rob Landley3b890392006-05-04 20:56:43 +0000804 return FALSE;
805 }
806
807 printf("%d lines, %d chars\n", lineCount, charCount);
Rob Landley3b890392006-05-04 20:56:43 +0000808 return TRUE;
809}
810
811
812/*
813 * Print lines in a specified range.
814 * The last line printed becomes the current line.
815 * If expandFlag is TRUE, then the line is printed specially to
816 * show magic characters.
817 */
818static int printLines(int num1, int num2, int expandFlag)
819{
820 const LINE *lp;
821 const char *cp;
822 int ch, count;
823
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000824 if (bad_nums(num1, num2, "print"))
Rob Landley3b890392006-05-04 20:56:43 +0000825 return FALSE;
Rob Landley3b890392006-05-04 20:56:43 +0000826
827 lp = findLine(num1);
Rob Landley3b890392006-05-04 20:56:43 +0000828 if (lp == NULL)
829 return FALSE;
830
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000831 while (num1 <= num2) {
832 if (!expandFlag) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000833 write(STDOUT_FILENO, lp->data, lp->len);
Rob Landley3b890392006-05-04 20:56:43 +0000834 setCurNum(num1++);
835 lp = lp->next;
Rob Landley3b890392006-05-04 20:56:43 +0000836 continue;
837 }
838
839 /*
840 * Show control characters and characters with the
841 * high bit set specially.
842 */
843 cp = lp->data;
844 count = lp->len;
845
846 if ((count > 0) && (cp[count - 1] == '\n'))
847 count--;
848
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000849 while (count-- > 0) {
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000850 ch = (unsigned char) *cp++;
851 fputc_printable(ch | PRINTABLE_META, stdout);
Rob Landley3b890392006-05-04 20:56:43 +0000852 }
853
854 fputs("$\n", stdout);
855
856 setCurNum(num1++);
857 lp = lp->next;
858 }
859
860 return TRUE;
861}
862
863
864/*
865 * Insert a new line with the specified text.
866 * The line is inserted so as to become the specified line,
867 * thus pushing any existing and further lines down one.
868 * The inserted line is also set to become the current line.
869 * Returns TRUE if successful.
870 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000871static int insertLine(int num, const char *data, int len)
Rob Landley3b890392006-05-04 20:56:43 +0000872{
873 LINE *newLp, *lp;
874
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000875 if ((num < 1) || (num > lastNum + 1)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000876 bb_error_msg("inserting at bad line number");
Rob Landley3b890392006-05-04 20:56:43 +0000877 return FALSE;
878 }
879
Denys Vlasenko90377872010-01-08 09:07:50 +0100880 newLp = xmalloc(sizeof(LINE) + len - 1);
Rob Landley3b890392006-05-04 20:56:43 +0000881
882 memcpy(newLp->data, data, len);
883 newLp->len = len;
884
885 if (num > lastNum)
886 lp = &lines;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000887 else {
Rob Landley3b890392006-05-04 20:56:43 +0000888 lp = findLine(num);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000889 if (lp == NULL) {
Rob Landley3b890392006-05-04 20:56:43 +0000890 free((char *) newLp);
Rob Landley3b890392006-05-04 20:56:43 +0000891 return FALSE;
892 }
893 }
894
895 newLp->next = lp;
896 newLp->prev = lp->prev;
897 lp->prev->next = newLp;
898 lp->prev = newLp;
899
900 lastNum++;
901 dirty = TRUE;
Rob Landley3b890392006-05-04 20:56:43 +0000902 return setCurNum(num);
903}
904
905
906/*
907 * Delete lines from the given range.
908 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000909static void deleteLines(int num1, int num2)
Rob Landley3b890392006-05-04 20:56:43 +0000910{
911 LINE *lp, *nlp, *plp;
912 int count;
913
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000914 if (bad_nums(num1, num2, "delete"))
915 return;
Rob Landley3b890392006-05-04 20:56:43 +0000916
917 lp = findLine(num1);
Rob Landley3b890392006-05-04 20:56:43 +0000918 if (lp == NULL)
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000919 return;
Rob Landley3b890392006-05-04 20:56:43 +0000920
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000921 if ((curNum >= num1) && (curNum <= num2)) {
Rob Landley3b890392006-05-04 20:56:43 +0000922 if (num2 < lastNum)
923 setCurNum(num2 + 1);
924 else if (num1 > 1)
925 setCurNum(num1 - 1);
926 else
927 curNum = 0;
928 }
929
930 count = num2 - num1 + 1;
Rob Landley3b890392006-05-04 20:56:43 +0000931 if (curNum > num2)
932 curNum -= count;
Rob Landley3b890392006-05-04 20:56:43 +0000933 lastNum -= count;
934
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000935 while (count-- > 0) {
Rob Landley3b890392006-05-04 20:56:43 +0000936 nlp = lp->next;
937 plp = lp->prev;
938 plp->next = nlp;
939 nlp->prev = plp;
Rob Landley3b890392006-05-04 20:56:43 +0000940 free(lp);
941 lp = nlp;
942 }
943
944 dirty = TRUE;
Rob Landley3b890392006-05-04 20:56:43 +0000945}
946
947
948/*
949 * Search for a line which contains the specified string.
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000950 * If the string is "", then the previously searched for string
Rob Landley3b890392006-05-04 20:56:43 +0000951 * is used. The currently searched for string is saved for future use.
952 * Returns the line number which matches, or 0 if there was no match
953 * with an error printed.
954 */
Denys Vlasenkoadf922e2009-10-08 14:35:37 +0200955static NOINLINE int searchLines(const char *str, int num1, int num2)
Rob Landley3b890392006-05-04 20:56:43 +0000956{
957 const LINE *lp;
958 int len;
959
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000960 if (bad_nums(num1, num2, "search"))
Rob Landley3b890392006-05-04 20:56:43 +0000961 return 0;
Rob Landley3b890392006-05-04 20:56:43 +0000962
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000963 if (*str == '\0') {
964 if (searchString[0] == '\0') {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000965 bb_error_msg("no previous search string");
Rob Landley3b890392006-05-04 20:56:43 +0000966 return 0;
967 }
Rob Landley3b890392006-05-04 20:56:43 +0000968 str = searchString;
969 }
970
971 if (str != searchString)
972 strcpy(searchString, str);
973
974 len = strlen(str);
975
976 lp = findLine(num1);
Rob Landley3b890392006-05-04 20:56:43 +0000977 if (lp == NULL)
978 return 0;
979
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000980 while (num1 <= num2) {
Rob Landley3b890392006-05-04 20:56:43 +0000981 if (findString(lp, str, len, 0) >= 0)
982 return num1;
Rob Landley3b890392006-05-04 20:56:43 +0000983 num1++;
984 lp = lp->next;
985 }
986
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100987 bb_error_msg("can't find string \"%s\"", str);
Rob Landley3b890392006-05-04 20:56:43 +0000988 return 0;
989}
990
991
992/*
993 * Return a pointer to the specified line number.
994 */
995static LINE *findLine(int num)
996{
997 LINE *lp;
998 int lnum;
999
Denis Vlasenko610c4aa2006-11-30 20:57:50 +00001000 if ((num < 1) || (num > lastNum)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +00001001 bb_error_msg("line number %d does not exist", num);
Rob Landley3b890392006-05-04 20:56:43 +00001002 return NULL;
1003 }
1004
Denis Vlasenko610c4aa2006-11-30 20:57:50 +00001005 if (curNum <= 0) {
Rob Landley3b890392006-05-04 20:56:43 +00001006 curNum = 1;
1007 curLine = lines.next;
1008 }
1009
1010 if (num == curNum)
1011 return curLine;
1012
1013 lp = curLine;
1014 lnum = curNum;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +00001015 if (num < (curNum / 2)) {
Rob Landley3b890392006-05-04 20:56:43 +00001016 lp = lines.next;
1017 lnum = 1;
Denis Vlasenkod9391b12007-09-25 11:55:57 +00001018 } else if (num > ((curNum + lastNum) / 2)) {
Rob Landley3b890392006-05-04 20:56:43 +00001019 lp = lines.prev;
1020 lnum = lastNum;
1021 }
1022
Denis Vlasenko610c4aa2006-11-30 20:57:50 +00001023 while (lnum < num) {
Rob Landley3b890392006-05-04 20:56:43 +00001024 lp = lp->next;
1025 lnum++;
1026 }
1027
Denis Vlasenko610c4aa2006-11-30 20:57:50 +00001028 while (lnum > num) {
Rob Landley3b890392006-05-04 20:56:43 +00001029 lp = lp->prev;
1030 lnum--;
1031 }
Rob Landley3b890392006-05-04 20:56:43 +00001032 return lp;
1033}
1034
1035
1036/*
1037 * Set the current line number.
1038 * Returns TRUE if successful.
1039 */
1040static int setCurNum(int num)
1041{
1042 LINE *lp;
1043
1044 lp = findLine(num);
Rob Landley3b890392006-05-04 20:56:43 +00001045 if (lp == NULL)
1046 return FALSE;
Rob Landley3b890392006-05-04 20:56:43 +00001047 curNum = num;
1048 curLine = lp;
Rob Landley3b890392006-05-04 20:56:43 +00001049 return TRUE;
1050}