blob: 863a9936622e1c7c4ade4c7ee403bd14417bfb54 [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
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020011//config: bool "ed (25 kb)"
Denys Vlasenko73225b62013-11-13 12:45:33 +010012//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"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020026#include "common_bufsiz.h"
Rob Landley3b890392006-05-04 20:56:43 +000027
Denis Vlasenkod9391b12007-09-25 11:55:57 +000028typedef struct LINE {
29 struct LINE *next;
30 struct LINE *prev;
31 int len;
32 char data[1];
33} LINE;
34
35
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +020036#define searchString bb_common_bufsiz1
Denis Vlasenko74324c82007-06-04 10:16:52 +000037
Denis Vlasenko55f30b02007-03-24 22:42:29 +000038enum {
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +020039 USERSIZE = COMMON_BUFSIZE > 1024 ? 1024
40 : COMMON_BUFSIZE - 1, /* max line length typed in by user */
Denis Vlasenko55f30b02007-03-24 22:42:29 +000041 INITBUF_SIZE = 1024, /* initial buffer size */
42};
43
Denis Vlasenkod9391b12007-09-25 11:55:57 +000044struct globals {
45 int curNum;
46 int lastNum;
47 int bufUsed;
48 int bufSize;
49 LINE *curLine;
50 char *bufBase;
51 char *bufPtr;
52 char *fileName;
53 LINE lines;
54 smallint dirty;
55 int marks[26];
56};
57#define G (*ptr_to_globals)
58#define curLine (G.curLine )
59#define bufBase (G.bufBase )
60#define bufPtr (G.bufPtr )
61#define fileName (G.fileName )
62#define curNum (G.curNum )
63#define lastNum (G.lastNum )
64#define bufUsed (G.bufUsed )
65#define bufSize (G.bufSize )
66#define dirty (G.dirty )
67#define lines (G.lines )
68#define marks (G.marks )
69#define INIT_G() do { \
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +020070 setup_common_bufsiz(); \
Denis Vlasenko574f2f42008-02-27 18:41:59 +000071 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkod9391b12007-09-25 11:55:57 +000072} while (0)
Rob Landley3b890392006-05-04 20:56:43 +000073
Rob Landley3b890392006-05-04 20:56:43 +000074
75static void doCommands(void);
76static void subCommand(const char *cmd, int num1, int num2);
Denis Vlasenkod9391b12007-09-25 11:55:57 +000077static int getNum(const char **retcp, smallint *retHaveNum, int *retNum);
Rob Landley3b890392006-05-04 20:56:43 +000078static int setCurNum(int num);
Rob Landley3b890392006-05-04 20:56:43 +000079static void addLines(int num);
80static int insertLine(int num, const char *data, int len);
Denis Vlasenkod9391b12007-09-25 11:55:57 +000081static void deleteLines(int num1, int num2);
Rob Landley3b890392006-05-04 20:56:43 +000082static int printLines(int num1, int num2, int expandFlag);
83static int writeLines(const char *file, int num1, int num2);
84static int readLines(const char *file, int num);
85static int searchLines(const char *str, int num1, int num2);
86static LINE *findLine(int num);
Rob Landley3b890392006-05-04 20:56:43 +000087static int findString(const LINE *lp, const char * str, int len, int offset);
88
Denis Vlasenkod9391b12007-09-25 11:55:57 +000089
90static int bad_nums(int num1, int num2, const char *for_what)
91{
92 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
93 bb_error_msg("bad line range for %s", for_what);
94 return 1;
95 }
96 return 0;
97}
98
99
100static char *skip_blank(const char *cp)
101{
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000102 while (isblank(*cp))
103 cp++;
104 return (char *)cp;
105}
106
107
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000108int ed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000109int ed_main(int argc UNUSED_PARAM, char **argv)
Rob Landley3b890392006-05-04 20:56:43 +0000110{
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000111 INIT_G();
112
113 bufSize = INITBUF_SIZE;
114 bufBase = xmalloc(bufSize);
115 bufPtr = bufBase;
116 lines.next = &lines;
117 lines.prev = &lines;
Rob Landley3b890392006-05-04 20:56:43 +0000118
Denis Vlasenko1d426652008-03-17 09:09:09 +0000119 if (argv[1]) {
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000120 fileName = xstrdup(argv[1]);
Rob Landley3b890392006-05-04 20:56:43 +0000121 if (!readLines(fileName, 1)) {
Rob Landley3b890392006-05-04 20:56:43 +0000122 return EXIT_SUCCESS;
123 }
Rob Landley3b890392006-05-04 20:56:43 +0000124 if (lastNum)
125 setCurNum(1);
Rob Landley3b890392006-05-04 20:56:43 +0000126 dirty = FALSE;
127 }
128
129 doCommands();
Rob Landley3b890392006-05-04 20:56:43 +0000130 return EXIT_SUCCESS;
131}
132
133/*
134 * Read commands until we are told to stop.
135 */
136static void doCommands(void)
137{
138 const char *cp;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000139 char *endbuf, buf[USERSIZE];
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000140 int len, num1, num2;
141 smallint have1, have2;
Rob Landley3b890392006-05-04 20:56:43 +0000142
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000143 while (TRUE) {
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000144 /* Returns:
145 * -1 on read errors or EOF, or on bare Ctrl-D.
146 * 0 on ctrl-C,
147 * >0 length of input string, including terminating '\n'
148 */
Denys Vlasenko66c5b122011-02-08 05:07:02 +0100149 len = read_line_input(NULL, ": ", buf, sizeof(buf), /*timeout*/ -1);
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000150 if (len <= 0)
Rob Landley3b890392006-05-04 20:56:43 +0000151 return;
Rob Landley3b890392006-05-04 20:56:43 +0000152 endbuf = &buf[len - 1];
Rob Landley3b890392006-05-04 20:56:43 +0000153 while ((endbuf > buf) && isblank(endbuf[-1]))
154 endbuf--;
Rob Landley3b890392006-05-04 20:56:43 +0000155 *endbuf = '\0';
156
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000157 cp = skip_blank(buf);
Rob Landley3b890392006-05-04 20:56:43 +0000158 have1 = FALSE;
159 have2 = FALSE;
160
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000161 if ((curNum == 0) && (lastNum > 0)) {
Rob Landley3b890392006-05-04 20:56:43 +0000162 curNum = 1;
163 curLine = lines.next;
164 }
165
166 if (!getNum(&cp, &have1, &num1))
167 continue;
168
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000169 cp = skip_blank(cp);
Rob Landley3b890392006-05-04 20:56:43 +0000170
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000171 if (*cp == ',') {
Rob Landley3b890392006-05-04 20:56:43 +0000172 cp++;
Rob Landley3b890392006-05-04 20:56:43 +0000173 if (!getNum(&cp, &have2, &num2))
174 continue;
Rob Landley3b890392006-05-04 20:56:43 +0000175 if (!have1)
176 num1 = 1;
Rob Landley3b890392006-05-04 20:56:43 +0000177 if (!have2)
178 num2 = lastNum;
Rob Landley3b890392006-05-04 20:56:43 +0000179 have1 = TRUE;
180 have2 = TRUE;
181 }
Rob Landley3b890392006-05-04 20:56:43 +0000182 if (!have1)
183 num1 = curNum;
Rob Landley3b890392006-05-04 20:56:43 +0000184 if (!have2)
185 num2 = num1;
186
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000187 switch (*cp++) {
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000188 case 'a':
189 addLines(num1 + 1);
190 break;
Rob Landley3b890392006-05-04 20:56:43 +0000191
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000192 case 'c':
193 deleteLines(num1, num2);
194 addLines(num1);
195 break;
Rob Landley3b890392006-05-04 20:56:43 +0000196
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000197 case 'd':
198 deleteLines(num1, num2);
199 break;
Rob Landley3b890392006-05-04 20:56:43 +0000200
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000201 case 'f':
202 if (*cp && !isblank(*cp)) {
203 bb_error_msg("bad file command");
Rob Landley3b890392006-05-04 20:56:43 +0000204 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000205 }
206 cp = skip_blank(cp);
207 if (*cp == '\0') {
208 if (fileName)
209 printf("\"%s\"\n", fileName);
210 else
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200211 puts("No file name");
Rob Landley3b890392006-05-04 20:56:43 +0000212 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000213 }
214 free(fileName);
215 fileName = xstrdup(cp);
216 break;
Rob Landley3b890392006-05-04 20:56:43 +0000217
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000218 case 'i':
219 addLines(num1);
220 break;
221
222 case 'k':
223 cp = skip_blank(cp);
224 if ((*cp < 'a') || (*cp > 'z') || cp[1]) {
225 bb_error_msg("bad mark name");
Rob Landley3b890392006-05-04 20:56:43 +0000226 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000227 }
228 marks[*cp - 'a'] = num2;
229 break;
Rob Landley3b890392006-05-04 20:56:43 +0000230
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000231 case 'l':
232 printLines(num1, num2, TRUE);
233 break;
234
235 case 'p':
236 printLines(num1, num2, FALSE);
237 break;
238
239 case 'q':
240 cp = skip_blank(cp);
241 if (have1 || *cp) {
242 bb_error_msg("bad quit command");
Rob Landley3b890392006-05-04 20:56:43 +0000243 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000244 }
245 if (!dirty)
246 return;
Denys Vlasenko66c5b122011-02-08 05:07:02 +0100247 len = read_line_input(NULL, "Really quit? ", buf, 16, /*timeout*/ -1);
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000248 /* read error/EOF - no way to continue */
249 if (len < 0)
250 return;
251 cp = skip_blank(buf);
252 if ((*cp | 0x20) == 'y') /* Y or y */
253 return;
254 break;
Rob Landley3b890392006-05-04 20:56:43 +0000255
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000256 case 'r':
257 if (*cp && !isblank(*cp)) {
258 bb_error_msg("bad read command");
Rob Landley3b890392006-05-04 20:56:43 +0000259 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000260 }
261 cp = skip_blank(cp);
262 if (*cp == '\0') {
263 bb_error_msg("no file name");
Rob Landley3b890392006-05-04 20:56:43 +0000264 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000265 }
266 if (!have1)
267 num1 = lastNum;
268 if (readLines(cp, num1 + 1))
Rob Landley3b890392006-05-04 20:56:43 +0000269 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000270 if (fileName == NULL)
271 fileName = xstrdup(cp);
272 break;
Rob Landley3b890392006-05-04 20:56:43 +0000273
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000274 case 's':
275 subCommand(cp, num1, num2);
276 break;
277
278 case 'w':
279 if (*cp && !isblank(*cp)) {
280 bb_error_msg("bad write command");
Rob Landley3b890392006-05-04 20:56:43 +0000281 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000282 }
283 cp = skip_blank(cp);
284 if (!have1) {
285 num1 = 1;
286 num2 = lastNum;
287 }
288 if (*cp == '\0')
289 cp = fileName;
290 if (cp == NULL) {
291 bb_error_msg("no file name specified");
Rob Landley3b890392006-05-04 20:56:43 +0000292 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000293 }
294 writeLines(cp, num1, num2);
295 break;
Rob Landley3b890392006-05-04 20:56:43 +0000296
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000297 case 'z':
298 switch (*cp) {
Rob Landley3b890392006-05-04 20:56:43 +0000299 case '-':
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000300 printLines(curNum - 21, curNum, FALSE);
Rob Landley3b890392006-05-04 20:56:43 +0000301 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000302 case '.':
303 printLines(curNum - 11, curNum + 10, FALSE);
Rob Landley3b890392006-05-04 20:56:43 +0000304 break;
Rob Landley3b890392006-05-04 20:56:43 +0000305 default:
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000306 printLines(curNum, curNum + 21, FALSE);
Rob Landley3b890392006-05-04 20:56:43 +0000307 break;
Denis Vlasenkoc4523c22008-03-28 02:24:59 +0000308 }
309 break;
310
311 case '.':
312 if (have1) {
313 bb_error_msg("no arguments allowed");
314 break;
315 }
316 printLines(curNum, curNum, FALSE);
317 break;
318
319 case '-':
320 if (setCurNum(curNum - 1))
321 printLines(curNum, curNum, FALSE);
322 break;
323
324 case '=':
325 printf("%d\n", num1);
326 break;
327 case '\0':
328 if (have1) {
329 printLines(num2, num2, FALSE);
330 break;
331 }
332 if (setCurNum(curNum + 1))
333 printLines(curNum, curNum, FALSE);
334 break;
335
336 default:
337 bb_error_msg("unimplemented command");
338 break;
Rob Landley3b890392006-05-04 20:56:43 +0000339 }
340 }
341}
342
343
344/*
345 * Do the substitute command.
346 * The current line is set to the last substitution done.
347 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000348static void subCommand(const char *cmd, int num1, int num2)
Rob Landley3b890392006-05-04 20:56:43 +0000349{
350 char *cp, *oldStr, *newStr, buf[USERSIZE];
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000351 int delim, oldLen, newLen, deltaLen, offset;
Rob Landley3b890392006-05-04 20:56:43 +0000352 LINE *lp, *nlp;
353 int globalFlag, printFlag, didSub, needPrint;
354
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000355 if (bad_nums(num1, num2, "substitute"))
Rob Landley3b890392006-05-04 20:56:43 +0000356 return;
Rob Landley3b890392006-05-04 20:56:43 +0000357
358 globalFlag = FALSE;
359 printFlag = FALSE;
360 didSub = FALSE;
361 needPrint = FALSE;
362
363 /*
364 * Copy the command so we can modify it.
365 */
366 strcpy(buf, cmd);
367 cp = buf;
368
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000369 if (isblank(*cp) || (*cp == '\0')) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000370 bb_error_msg("bad delimiter for substitute");
Rob Landley3b890392006-05-04 20:56:43 +0000371 return;
372 }
373
374 delim = *cp++;
375 oldStr = cp;
376
377 cp = strchr(cp, delim);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000378 if (cp == NULL) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000379 bb_error_msg("missing 2nd delimiter for substitute");
Rob Landley3b890392006-05-04 20:56:43 +0000380 return;
381 }
382
383 *cp++ = '\0';
384
385 newStr = cp;
386 cp = strchr(cp, delim);
387
388 if (cp)
389 *cp++ = '\0';
390 else
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000391 cp = (char*)"";
Rob Landley3b890392006-05-04 20:56:43 +0000392
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000393 while (*cp) switch (*cp++) {
Rob Landley3b890392006-05-04 20:56:43 +0000394 case 'g':
395 globalFlag = TRUE;
396 break;
Rob Landley3b890392006-05-04 20:56:43 +0000397 case 'p':
398 printFlag = TRUE;
399 break;
Rob Landley3b890392006-05-04 20:56:43 +0000400 default:
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000401 bb_error_msg("unknown option for substitute");
Rob Landley3b890392006-05-04 20:56:43 +0000402 return;
403 }
404
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000405 if (*oldStr == '\0') {
406 if (searchString[0] == '\0') {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000407 bb_error_msg("no previous search string");
Rob Landley3b890392006-05-04 20:56:43 +0000408 return;
409 }
Rob Landley3b890392006-05-04 20:56:43 +0000410 oldStr = searchString;
411 }
412
413 if (oldStr != searchString)
414 strcpy(searchString, oldStr);
415
416 lp = findLine(num1);
Rob Landley3b890392006-05-04 20:56:43 +0000417 if (lp == NULL)
418 return;
419
420 oldLen = strlen(oldStr);
421 newLen = strlen(newStr);
422 deltaLen = newLen - oldLen;
423 offset = 0;
424 nlp = NULL;
425
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000426 while (num1 <= num2) {
Rob Landley3b890392006-05-04 20:56:43 +0000427 offset = findString(lp, oldStr, oldLen, offset);
428
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000429 if (offset < 0) {
430 if (needPrint) {
Rob Landley3b890392006-05-04 20:56:43 +0000431 printLines(num1, num1, FALSE);
432 needPrint = FALSE;
433 }
Rob Landley3b890392006-05-04 20:56:43 +0000434 offset = 0;
435 lp = lp->next;
436 num1++;
Rob Landley3b890392006-05-04 20:56:43 +0000437 continue;
438 }
439
440 needPrint = printFlag;
441 didSub = TRUE;
442 dirty = TRUE;
443
444 /*
445 * If the replacement string is the same size or shorter
446 * than the old string, then the substitution is easy.
447 */
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000448 if (deltaLen <= 0) {
Rob Landley3b890392006-05-04 20:56:43 +0000449 memcpy(&lp->data[offset], newStr, newLen);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000450 if (deltaLen) {
Rob Landley3b890392006-05-04 20:56:43 +0000451 memcpy(&lp->data[offset + newLen],
452 &lp->data[offset + oldLen],
453 lp->len - offset - oldLen);
454
455 lp->len += deltaLen;
456 }
Rob Landley3b890392006-05-04 20:56:43 +0000457 offset += newLen;
Rob Landley3b890392006-05-04 20:56:43 +0000458 if (globalFlag)
459 continue;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000460 if (needPrint) {
Rob Landley3b890392006-05-04 20:56:43 +0000461 printLines(num1, num1, FALSE);
462 needPrint = FALSE;
463 }
Rob Landley3b890392006-05-04 20:56:43 +0000464 lp = lp->next;
465 num1++;
Rob Landley3b890392006-05-04 20:56:43 +0000466 continue;
467 }
468
469 /*
470 * The new string is larger, so allocate a new line
Marek Polacekb0b88842011-04-16 17:33:43 +0200471 * structure and use that. Link it in place of
Rob Landley3b890392006-05-04 20:56:43 +0000472 * the old line structure.
473 */
Denys Vlasenko90377872010-01-08 09:07:50 +0100474 nlp = xmalloc(sizeof(LINE) + lp->len + deltaLen);
Rob Landley3b890392006-05-04 20:56:43 +0000475
476 nlp->len = lp->len + deltaLen;
477
478 memcpy(nlp->data, lp->data, offset);
Rob Landley3b890392006-05-04 20:56:43 +0000479 memcpy(&nlp->data[offset], newStr, newLen);
Rob Landley3b890392006-05-04 20:56:43 +0000480 memcpy(&nlp->data[offset + newLen],
481 &lp->data[offset + oldLen],
482 lp->len - offset - oldLen);
483
484 nlp->next = lp->next;
485 nlp->prev = lp->prev;
486 nlp->prev->next = nlp;
487 nlp->next->prev = nlp;
488
489 if (curLine == lp)
490 curLine = nlp;
491
492 free(lp);
493 lp = nlp;
494
495 offset += newLen;
496
497 if (globalFlag)
498 continue;
499
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000500 if (needPrint) {
Rob Landley3b890392006-05-04 20:56:43 +0000501 printLines(num1, num1, FALSE);
502 needPrint = FALSE;
503 }
504
505 lp = lp->next;
506 num1++;
507 }
508
509 if (!didSub)
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000510 bb_error_msg("no substitutions found for \"%s\"", oldStr);
Rob Landley3b890392006-05-04 20:56:43 +0000511}
512
513
514/*
515 * Search a line for the specified string starting at the specified
516 * offset in the line. Returns the offset of the found string, or -1.
517 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000518static int findString(const LINE *lp, const char *str, int len, int offset)
Rob Landley3b890392006-05-04 20:56:43 +0000519{
520 int left;
521 const char *cp, *ncp;
522
523 cp = &lp->data[offset];
524 left = lp->len - offset;
525
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000526 while (left >= len) {
Rob Landley3b890392006-05-04 20:56:43 +0000527 ncp = memchr(cp, *str, left);
Rob Landley3b890392006-05-04 20:56:43 +0000528 if (ncp == NULL)
529 return -1;
Rob Landley3b890392006-05-04 20:56:43 +0000530 left -= (ncp - cp);
Rob Landley3b890392006-05-04 20:56:43 +0000531 if (left < len)
532 return -1;
Rob Landley3b890392006-05-04 20:56:43 +0000533 cp = ncp;
Rob Landley3b890392006-05-04 20:56:43 +0000534 if (memcmp(cp, str, len) == 0)
535 return (cp - lp->data);
Rob Landley3b890392006-05-04 20:56:43 +0000536 cp++;
537 left--;
538 }
539
540 return -1;
541}
542
543
544/*
545 * Add lines which are typed in by the user.
546 * The lines are inserted just before the specified line number.
547 * The lines are terminated by a line containing a single dot (ugly!),
548 * or by an end of file.
549 */
550static void addLines(int num)
551{
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000552 int len;
553 char buf[USERSIZE + 1];
Rob Landley3b890392006-05-04 20:56:43 +0000554
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000555 while (1) {
556 /* Returns:
557 * -1 on read errors or EOF, or on bare Ctrl-D.
558 * 0 on ctrl-C,
559 * >0 length of input string, including terminating '\n'
560 */
Denys Vlasenko66c5b122011-02-08 05:07:02 +0100561 len = read_line_input(NULL, "", buf, sizeof(buf), /*timeout*/ -1);
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000562 if (len <= 0) {
563 /* Previously, ctrl-C was exiting to shell.
564 * Now we exit to ed prompt. Is in important? */
Rob Landley3b890392006-05-04 20:56:43 +0000565 return;
566 }
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000567 if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0'))
568 return;
Rob Landley3b890392006-05-04 20:56:43 +0000569 if (!insertLine(num++, buf, len))
570 return;
571 }
572}
573
574
575/*
576 * Parse a line number argument if it is present. This is a sum
577 * or difference of numbers, '.', '$', 'x, or a search string.
Bernhard Reutner-Fischerd9ed35c2006-05-19 12:38:30 +0000578 * Returns TRUE if successful (whether or not there was a number).
Rob Landley3b890392006-05-04 20:56:43 +0000579 * Returns FALSE if there was a parsing error, with a message output.
580 * Whether there was a number is returned indirectly, as is the number.
581 * The character pointer which stopped the scan is also returned.
582 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000583static int getNum(const char **retcp, smallint *retHaveNum, int *retNum)
Rob Landley3b890392006-05-04 20:56:43 +0000584{
585 const char *cp;
586 char *endStr, str[USERSIZE];
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000587 int value, num;
588 smallint haveNum, minus;
Rob Landley3b890392006-05-04 20:56:43 +0000589
590 cp = *retcp;
Rob Landley3b890392006-05-04 20:56:43 +0000591 value = 0;
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000592 haveNum = FALSE;
593 minus = 0;
Rob Landley3b890392006-05-04 20:56:43 +0000594
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000595 while (TRUE) {
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000596 cp = skip_blank(cp);
Rob Landley3b890392006-05-04 20:56:43 +0000597
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000598 switch (*cp) {
Rob Landley3b890392006-05-04 20:56:43 +0000599 case '.':
600 haveNum = TRUE;
601 num = curNum;
602 cp++;
603 break;
604
605 case '$':
606 haveNum = TRUE;
607 num = lastNum;
608 cp++;
609 break;
610
611 case '\'':
612 cp++;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000613 if ((*cp < 'a') || (*cp > 'z')) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000614 bb_error_msg("bad mark name");
Rob Landley3b890392006-05-04 20:56:43 +0000615 return FALSE;
616 }
Rob Landley3b890392006-05-04 20:56:43 +0000617 haveNum = TRUE;
618 num = marks[*cp++ - 'a'];
619 break;
620
621 case '/':
622 strcpy(str, ++cp);
623 endStr = strchr(str, '/');
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000624 if (endStr) {
Rob Landley3b890392006-05-04 20:56:43 +0000625 *endStr++ = '\0';
626 cp += (endStr - str);
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000627 } else
Rob Landley3b890392006-05-04 20:56:43 +0000628 cp = "";
Rob Landley3b890392006-05-04 20:56:43 +0000629 num = searchLines(str, curNum, lastNum);
Rob Landley3b890392006-05-04 20:56:43 +0000630 if (num == 0)
631 return FALSE;
Rob Landley3b890392006-05-04 20:56:43 +0000632 haveNum = TRUE;
633 break;
634
635 default:
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000636 if (!isdigit(*cp)) {
Rob Landley3b890392006-05-04 20:56:43 +0000637 *retcp = cp;
638 *retHaveNum = haveNum;
639 *retNum = value;
Rob Landley3b890392006-05-04 20:56:43 +0000640 return TRUE;
641 }
Rob Landley3b890392006-05-04 20:56:43 +0000642 num = 0;
Rob Landley3b890392006-05-04 20:56:43 +0000643 while (isdigit(*cp))
644 num = num * 10 + *cp++ - '0';
Rob Landley3b890392006-05-04 20:56:43 +0000645 haveNum = TRUE;
646 break;
647 }
648
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000649 value += (minus ? -num : num);
Rob Landley3b890392006-05-04 20:56:43 +0000650
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000651 cp = skip_blank(cp);
Rob Landley3b890392006-05-04 20:56:43 +0000652
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000653 switch (*cp) {
Rob Landley3b890392006-05-04 20:56:43 +0000654 case '-':
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000655 minus = 1;
Rob Landley3b890392006-05-04 20:56:43 +0000656 cp++;
657 break;
658
659 case '+':
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000660 minus = 0;
Rob Landley3b890392006-05-04 20:56:43 +0000661 cp++;
662 break;
663
664 default:
665 *retcp = cp;
666 *retHaveNum = haveNum;
667 *retNum = value;
Rob Landley3b890392006-05-04 20:56:43 +0000668 return TRUE;
669 }
670 }
671}
672
673
674/*
Rob Landley3b890392006-05-04 20:56:43 +0000675 * Read lines from a file at the specified line number.
676 * Returns TRUE if the file was successfully read.
677 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000678static int readLines(const char *file, int num)
Rob Landley3b890392006-05-04 20:56:43 +0000679{
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000680 int fd, cc;
Rob Landley3b890392006-05-04 20:56:43 +0000681 int len, lineCount, charCount;
682 char *cp;
683
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000684 if ((num < 1) || (num > lastNum + 1)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000685 bb_error_msg("bad line for read");
Rob Landley3b890392006-05-04 20:56:43 +0000686 return FALSE;
687 }
688
689 fd = open(file, 0);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000690 if (fd < 0) {
Marek Polacek7dd61e32010-09-12 17:06:43 +0200691 bb_simple_perror_msg(file);
Rob Landley3b890392006-05-04 20:56:43 +0000692 return FALSE;
693 }
694
695 bufPtr = bufBase;
696 bufUsed = 0;
697 lineCount = 0;
698 charCount = 0;
699 cc = 0;
700
701 printf("\"%s\", ", file);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100702 fflush_all();
Rob Landley3b890392006-05-04 20:56:43 +0000703
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000704 do {
Rob Landley3b890392006-05-04 20:56:43 +0000705 cp = memchr(bufPtr, '\n', bufUsed);
706
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000707 if (cp) {
Rob Landley3b890392006-05-04 20:56:43 +0000708 len = (cp - bufPtr) + 1;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000709 if (!insertLine(num, bufPtr, len)) {
Rob Landley3b890392006-05-04 20:56:43 +0000710 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000711 return FALSE;
712 }
Rob Landley3b890392006-05-04 20:56:43 +0000713 bufPtr += len;
714 bufUsed -= len;
715 charCount += len;
716 lineCount++;
717 num++;
Rob Landley3b890392006-05-04 20:56:43 +0000718 continue;
719 }
720
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000721 if (bufPtr != bufBase) {
Rob Landley3b890392006-05-04 20:56:43 +0000722 memcpy(bufBase, bufPtr, bufUsed);
723 bufPtr = bufBase + bufUsed;
724 }
725
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000726 if (bufUsed >= bufSize) {
Rob Landley3b890392006-05-04 20:56:43 +0000727 len = (bufSize * 3) / 2;
Denys Vlasenko90377872010-01-08 09:07:50 +0100728 cp = xrealloc(bufBase, len);
Rob Landley3b890392006-05-04 20:56:43 +0000729 bufBase = cp;
730 bufPtr = bufBase + bufUsed;
731 bufSize = len;
732 }
733
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000734 cc = safe_read(fd, bufPtr, bufSize - bufUsed);
Rob Landley3b890392006-05-04 20:56:43 +0000735 bufUsed += cc;
736 bufPtr = bufBase;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000737 } while (cc > 0);
Rob Landley3b890392006-05-04 20:56:43 +0000738
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000739 if (cc < 0) {
Marek Polacek7dd61e32010-09-12 17:06:43 +0200740 bb_simple_perror_msg(file);
Rob Landley3b890392006-05-04 20:56:43 +0000741 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000742 return FALSE;
743 }
744
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000745 if (bufUsed) {
746 if (!insertLine(num, bufPtr, bufUsed)) {
Rob Landley3b890392006-05-04 20:56:43 +0000747 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000748 return -1;
749 }
Rob Landley3b890392006-05-04 20:56:43 +0000750 lineCount++;
751 charCount += bufUsed;
752 }
753
754 close(fd);
755
756 printf("%d lines%s, %d chars\n", lineCount,
757 (bufUsed ? " (incomplete)" : ""), charCount);
758
759 return TRUE;
760}
761
762
763/*
764 * Write the specified lines out to the specified file.
765 * Returns TRUE if successful, or FALSE on an error with a message output.
766 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000767static int writeLines(const char *file, int num1, int num2)
Rob Landley3b890392006-05-04 20:56:43 +0000768{
769 LINE *lp;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000770 int fd, lineCount, charCount;
Rob Landley3b890392006-05-04 20:56:43 +0000771
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000772 if (bad_nums(num1, num2, "write"))
Rob Landley3b890392006-05-04 20:56:43 +0000773 return FALSE;
Rob Landley3b890392006-05-04 20:56:43 +0000774
775 lineCount = 0;
776 charCount = 0;
777
778 fd = creat(file, 0666);
Rob Landley3b890392006-05-04 20:56:43 +0000779 if (fd < 0) {
Marek Polacek7dd61e32010-09-12 17:06:43 +0200780 bb_simple_perror_msg(file);
Rob Landley3b890392006-05-04 20:56:43 +0000781 return FALSE;
782 }
783
784 printf("\"%s\", ", file);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100785 fflush_all();
Rob Landley3b890392006-05-04 20:56:43 +0000786
787 lp = findLine(num1);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000788 if (lp == NULL) {
Rob Landley3b890392006-05-04 20:56:43 +0000789 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000790 return FALSE;
791 }
792
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000793 while (num1++ <= num2) {
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000794 if (full_write(fd, lp->data, lp->len) != lp->len) {
Marek Polacek7dd61e32010-09-12 17:06:43 +0200795 bb_simple_perror_msg(file);
Rob Landley3b890392006-05-04 20:56:43 +0000796 close(fd);
Rob Landley3b890392006-05-04 20:56:43 +0000797 return FALSE;
798 }
Rob Landley3b890392006-05-04 20:56:43 +0000799 charCount += lp->len;
800 lineCount++;
801 lp = lp->next;
802 }
803
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000804 if (close(fd) < 0) {
Marek Polacek7dd61e32010-09-12 17:06:43 +0200805 bb_simple_perror_msg(file);
Rob Landley3b890392006-05-04 20:56:43 +0000806 return FALSE;
807 }
808
809 printf("%d lines, %d chars\n", lineCount, charCount);
Rob Landley3b890392006-05-04 20:56:43 +0000810 return TRUE;
811}
812
813
814/*
815 * Print lines in a specified range.
816 * The last line printed becomes the current line.
817 * If expandFlag is TRUE, then the line is printed specially to
818 * show magic characters.
819 */
820static int printLines(int num1, int num2, int expandFlag)
821{
822 const LINE *lp;
823 const char *cp;
824 int ch, count;
825
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000826 if (bad_nums(num1, num2, "print"))
Rob Landley3b890392006-05-04 20:56:43 +0000827 return FALSE;
Rob Landley3b890392006-05-04 20:56:43 +0000828
829 lp = findLine(num1);
Rob Landley3b890392006-05-04 20:56:43 +0000830 if (lp == NULL)
831 return FALSE;
832
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000833 while (num1 <= num2) {
834 if (!expandFlag) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000835 write(STDOUT_FILENO, lp->data, lp->len);
Rob Landley3b890392006-05-04 20:56:43 +0000836 setCurNum(num1++);
837 lp = lp->next;
Rob Landley3b890392006-05-04 20:56:43 +0000838 continue;
839 }
840
841 /*
842 * Show control characters and characters with the
843 * high bit set specially.
844 */
845 cp = lp->data;
846 count = lp->len;
847
848 if ((count > 0) && (cp[count - 1] == '\n'))
849 count--;
850
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000851 while (count-- > 0) {
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000852 ch = (unsigned char) *cp++;
853 fputc_printable(ch | PRINTABLE_META, stdout);
Rob Landley3b890392006-05-04 20:56:43 +0000854 }
855
856 fputs("$\n", stdout);
857
858 setCurNum(num1++);
859 lp = lp->next;
860 }
861
862 return TRUE;
863}
864
865
866/*
867 * Insert a new line with the specified text.
868 * The line is inserted so as to become the specified line,
869 * thus pushing any existing and further lines down one.
870 * The inserted line is also set to become the current line.
871 * Returns TRUE if successful.
872 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000873static int insertLine(int num, const char *data, int len)
Rob Landley3b890392006-05-04 20:56:43 +0000874{
875 LINE *newLp, *lp;
876
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000877 if ((num < 1) || (num > lastNum + 1)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000878 bb_error_msg("inserting at bad line number");
Rob Landley3b890392006-05-04 20:56:43 +0000879 return FALSE;
880 }
881
Denys Vlasenko90377872010-01-08 09:07:50 +0100882 newLp = xmalloc(sizeof(LINE) + len - 1);
Rob Landley3b890392006-05-04 20:56:43 +0000883
884 memcpy(newLp->data, data, len);
885 newLp->len = len;
886
887 if (num > lastNum)
888 lp = &lines;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000889 else {
Rob Landley3b890392006-05-04 20:56:43 +0000890 lp = findLine(num);
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000891 if (lp == NULL) {
Rob Landley3b890392006-05-04 20:56:43 +0000892 free((char *) newLp);
Rob Landley3b890392006-05-04 20:56:43 +0000893 return FALSE;
894 }
895 }
896
897 newLp->next = lp;
898 newLp->prev = lp->prev;
899 lp->prev->next = newLp;
900 lp->prev = newLp;
901
902 lastNum++;
903 dirty = TRUE;
Rob Landley3b890392006-05-04 20:56:43 +0000904 return setCurNum(num);
905}
906
907
908/*
909 * Delete lines from the given range.
910 */
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000911static void deleteLines(int num1, int num2)
Rob Landley3b890392006-05-04 20:56:43 +0000912{
913 LINE *lp, *nlp, *plp;
914 int count;
915
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000916 if (bad_nums(num1, num2, "delete"))
917 return;
Rob Landley3b890392006-05-04 20:56:43 +0000918
919 lp = findLine(num1);
Rob Landley3b890392006-05-04 20:56:43 +0000920 if (lp == NULL)
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000921 return;
Rob Landley3b890392006-05-04 20:56:43 +0000922
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000923 if ((curNum >= num1) && (curNum <= num2)) {
Rob Landley3b890392006-05-04 20:56:43 +0000924 if (num2 < lastNum)
925 setCurNum(num2 + 1);
926 else if (num1 > 1)
927 setCurNum(num1 - 1);
928 else
929 curNum = 0;
930 }
931
932 count = num2 - num1 + 1;
Rob Landley3b890392006-05-04 20:56:43 +0000933 if (curNum > num2)
934 curNum -= count;
Rob Landley3b890392006-05-04 20:56:43 +0000935 lastNum -= count;
936
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000937 while (count-- > 0) {
Rob Landley3b890392006-05-04 20:56:43 +0000938 nlp = lp->next;
939 plp = lp->prev;
940 plp->next = nlp;
941 nlp->prev = plp;
Rob Landley3b890392006-05-04 20:56:43 +0000942 free(lp);
943 lp = nlp;
944 }
945
946 dirty = TRUE;
Rob Landley3b890392006-05-04 20:56:43 +0000947}
948
949
950/*
951 * Search for a line which contains the specified string.
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000952 * If the string is "", then the previously searched for string
Rob Landley3b890392006-05-04 20:56:43 +0000953 * is used. The currently searched for string is saved for future use.
954 * Returns the line number which matches, or 0 if there was no match
955 * with an error printed.
956 */
Denys Vlasenkoadf922e2009-10-08 14:35:37 +0200957static NOINLINE int searchLines(const char *str, int num1, int num2)
Rob Landley3b890392006-05-04 20:56:43 +0000958{
959 const LINE *lp;
960 int len;
961
Denis Vlasenkod9391b12007-09-25 11:55:57 +0000962 if (bad_nums(num1, num2, "search"))
Rob Landley3b890392006-05-04 20:56:43 +0000963 return 0;
Rob Landley3b890392006-05-04 20:56:43 +0000964
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000965 if (*str == '\0') {
966 if (searchString[0] == '\0') {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000967 bb_error_msg("no previous search string");
Rob Landley3b890392006-05-04 20:56:43 +0000968 return 0;
969 }
Rob Landley3b890392006-05-04 20:56:43 +0000970 str = searchString;
971 }
972
973 if (str != searchString)
974 strcpy(searchString, str);
975
976 len = strlen(str);
977
978 lp = findLine(num1);
Rob Landley3b890392006-05-04 20:56:43 +0000979 if (lp == NULL)
980 return 0;
981
Denis Vlasenko610c4aa2006-11-30 20:57:50 +0000982 while (num1 <= num2) {
Rob Landley3b890392006-05-04 20:56:43 +0000983 if (findString(lp, str, len, 0) >= 0)
984 return num1;
Rob Landley3b890392006-05-04 20:56:43 +0000985 num1++;
986 lp = lp->next;
987 }
988
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100989 bb_error_msg("can't find string \"%s\"", str);
Rob Landley3b890392006-05-04 20:56:43 +0000990 return 0;
991}
992
993
994/*
995 * Return a pointer to the specified line number.
996 */
997static LINE *findLine(int num)
998{
999 LINE *lp;
1000 int lnum;
1001
Denis Vlasenko610c4aa2006-11-30 20:57:50 +00001002 if ((num < 1) || (num > lastNum)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +00001003 bb_error_msg("line number %d does not exist", num);
Rob Landley3b890392006-05-04 20:56:43 +00001004 return NULL;
1005 }
1006
Denis Vlasenko610c4aa2006-11-30 20:57:50 +00001007 if (curNum <= 0) {
Rob Landley3b890392006-05-04 20:56:43 +00001008 curNum = 1;
1009 curLine = lines.next;
1010 }
1011
1012 if (num == curNum)
1013 return curLine;
1014
1015 lp = curLine;
1016 lnum = curNum;
Denis Vlasenko610c4aa2006-11-30 20:57:50 +00001017 if (num < (curNum / 2)) {
Rob Landley3b890392006-05-04 20:56:43 +00001018 lp = lines.next;
1019 lnum = 1;
Denis Vlasenkod9391b12007-09-25 11:55:57 +00001020 } else if (num > ((curNum + lastNum) / 2)) {
Rob Landley3b890392006-05-04 20:56:43 +00001021 lp = lines.prev;
1022 lnum = lastNum;
1023 }
1024
Denis Vlasenko610c4aa2006-11-30 20:57:50 +00001025 while (lnum < num) {
Rob Landley3b890392006-05-04 20:56:43 +00001026 lp = lp->next;
1027 lnum++;
1028 }
1029
Denis Vlasenko610c4aa2006-11-30 20:57:50 +00001030 while (lnum > num) {
Rob Landley3b890392006-05-04 20:56:43 +00001031 lp = lp->prev;
1032 lnum--;
1033 }
Rob Landley3b890392006-05-04 20:56:43 +00001034 return lp;
1035}
1036
1037
1038/*
1039 * Set the current line number.
1040 * Returns TRUE if successful.
1041 */
1042static int setCurNum(int num)
1043{
1044 LINE *lp;
1045
1046 lp = findLine(num);
Rob Landley3b890392006-05-04 20:56:43 +00001047 if (lp == NULL)
1048 return FALSE;
Rob Landley3b890392006-05-04 20:56:43 +00001049 curNum = num;
1050 curLine = lp;
Rob Landley3b890392006-05-04 20:56:43 +00001051 return TRUE;
1052}