[821] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 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 |
|
---|
[3232] | 10 | //usage:#define ed_trivial_usage ""
|
---|
| 11 | //usage:#define ed_full_usage ""
|
---|
| 12 |
|
---|
[1765] | 13 | #include "libbb.h"
|
---|
[821] | 14 |
|
---|
[2725] | 15 | typedef struct LINE {
|
---|
| 16 | struct LINE *next;
|
---|
| 17 | struct LINE *prev;
|
---|
| 18 | int len;
|
---|
| 19 | char data[1];
|
---|
| 20 | } LINE;
|
---|
| 21 |
|
---|
| 22 |
|
---|
[1765] | 23 | #define searchString bb_common_bufsiz1
|
---|
| 24 |
|
---|
| 25 | enum {
|
---|
| 26 | USERSIZE = sizeof(searchString) > 1024 ? 1024
|
---|
| 27 | : sizeof(searchString) - 1, /* max line length typed in by user */
|
---|
| 28 | INITBUF_SIZE = 1024, /* initial buffer size */
|
---|
| 29 | };
|
---|
| 30 |
|
---|
[2725] | 31 | struct 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 { \
|
---|
| 57 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
|
---|
| 58 | } while (0)
|
---|
[821] | 59 |
|
---|
| 60 |
|
---|
| 61 | static void doCommands(void);
|
---|
| 62 | static void subCommand(const char *cmd, int num1, int num2);
|
---|
[2725] | 63 | static int getNum(const char **retcp, smallint *retHaveNum, int *retNum);
|
---|
[821] | 64 | static int setCurNum(int num);
|
---|
| 65 | static void addLines(int num);
|
---|
| 66 | static int insertLine(int num, const char *data, int len);
|
---|
[2725] | 67 | static void deleteLines(int num1, int num2);
|
---|
[821] | 68 | static int printLines(int num1, int num2, int expandFlag);
|
---|
| 69 | static int writeLines(const char *file, int num1, int num2);
|
---|
| 70 | static int readLines(const char *file, int num);
|
---|
| 71 | static int searchLines(const char *str, int num1, int num2);
|
---|
| 72 | static LINE *findLine(int num);
|
---|
| 73 | static int findString(const LINE *lp, const char * str, int len, int offset);
|
---|
| 74 |
|
---|
[2725] | 75 |
|
---|
| 76 | static int bad_nums(int num1, int num2, const char *for_what)
|
---|
[821] | 77 | {
|
---|
[2725] | 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 | }
|
---|
[821] | 84 |
|
---|
| 85 |
|
---|
[2725] | 86 | static char *skip_blank(const char *cp)
|
---|
| 87 | {
|
---|
| 88 | while (isblank(*cp))
|
---|
| 89 | cp++;
|
---|
| 90 | return (char *)cp;
|
---|
| 91 | }
|
---|
[821] | 92 |
|
---|
[2725] | 93 |
|
---|
| 94 | int ed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 95 | int ed_main(int argc UNUSED_PARAM, char **argv)
|
---|
| 96 | {
|
---|
| 97 | INIT_G();
|
---|
| 98 |
|
---|
| 99 | bufSize = INITBUF_SIZE;
|
---|
| 100 | bufBase = xmalloc(bufSize);
|
---|
| 101 | bufPtr = bufBase;
|
---|
| 102 | lines.next = &lines;
|
---|
| 103 | lines.prev = &lines;
|
---|
| 104 |
|
---|
| 105 | if (argv[1]) {
|
---|
| 106 | fileName = xstrdup(argv[1]);
|
---|
[821] | 107 | if (!readLines(fileName, 1)) {
|
---|
| 108 | return EXIT_SUCCESS;
|
---|
| 109 | }
|
---|
| 110 | if (lastNum)
|
---|
| 111 | setCurNum(1);
|
---|
| 112 | dirty = FALSE;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | doCommands();
|
---|
| 116 | return EXIT_SUCCESS;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | /*
|
---|
| 120 | * Read commands until we are told to stop.
|
---|
| 121 | */
|
---|
| 122 | static void doCommands(void)
|
---|
| 123 | {
|
---|
| 124 | const char *cp;
|
---|
[2725] | 125 | char *endbuf, buf[USERSIZE];
|
---|
| 126 | int len, num1, num2;
|
---|
| 127 | smallint have1, have2;
|
---|
[821] | 128 |
|
---|
[1765] | 129 | while (TRUE) {
|
---|
[2725] | 130 | /* 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 | */
|
---|
[3232] | 135 | len = read_line_input(NULL, ": ", buf, sizeof(buf), /*timeout*/ -1);
|
---|
[2725] | 136 | if (len <= 0)
|
---|
[821] | 137 | return;
|
---|
| 138 | endbuf = &buf[len - 1];
|
---|
| 139 | while ((endbuf > buf) && isblank(endbuf[-1]))
|
---|
| 140 | endbuf--;
|
---|
| 141 | *endbuf = '\0';
|
---|
| 142 |
|
---|
[2725] | 143 | cp = skip_blank(buf);
|
---|
[821] | 144 | have1 = FALSE;
|
---|
| 145 | have2 = FALSE;
|
---|
| 146 |
|
---|
[1765] | 147 | if ((curNum == 0) && (lastNum > 0)) {
|
---|
[821] | 148 | curNum = 1;
|
---|
| 149 | curLine = lines.next;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | if (!getNum(&cp, &have1, &num1))
|
---|
| 153 | continue;
|
---|
| 154 |
|
---|
[2725] | 155 | cp = skip_blank(cp);
|
---|
[821] | 156 |
|
---|
[1765] | 157 | if (*cp == ',') {
|
---|
[821] | 158 | cp++;
|
---|
| 159 | if (!getNum(&cp, &have2, &num2))
|
---|
| 160 | continue;
|
---|
| 161 | if (!have1)
|
---|
| 162 | num1 = 1;
|
---|
| 163 | if (!have2)
|
---|
| 164 | num2 = lastNum;
|
---|
| 165 | have1 = TRUE;
|
---|
| 166 | have2 = TRUE;
|
---|
| 167 | }
|
---|
| 168 | if (!have1)
|
---|
| 169 | num1 = curNum;
|
---|
| 170 | if (!have2)
|
---|
| 171 | num2 = num1;
|
---|
| 172 |
|
---|
[1765] | 173 | switch (*cp++) {
|
---|
[2725] | 174 | case 'a':
|
---|
| 175 | addLines(num1 + 1);
|
---|
| 176 | break;
|
---|
[821] | 177 |
|
---|
[2725] | 178 | case 'c':
|
---|
| 179 | deleteLines(num1, num2);
|
---|
| 180 | addLines(num1);
|
---|
| 181 | break;
|
---|
[821] | 182 |
|
---|
[2725] | 183 | case 'd':
|
---|
| 184 | deleteLines(num1, num2);
|
---|
| 185 | break;
|
---|
| 186 |
|
---|
| 187 | case 'f':
|
---|
| 188 | if (*cp && !isblank(*cp)) {
|
---|
| 189 | bb_error_msg("bad file command");
|
---|
[821] | 190 | break;
|
---|
[2725] | 191 | }
|
---|
| 192 | cp = skip_blank(cp);
|
---|
| 193 | if (*cp == '\0') {
|
---|
[821] | 194 | if (fileName)
|
---|
[2725] | 195 | printf("\"%s\"\n", fileName);
|
---|
| 196 | else
|
---|
| 197 | printf("No file name\n");
|
---|
[821] | 198 | break;
|
---|
[2725] | 199 | }
|
---|
| 200 | free(fileName);
|
---|
| 201 | fileName = xstrdup(cp);
|
---|
| 202 | break;
|
---|
[821] | 203 |
|
---|
[2725] | 204 | 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");
|
---|
[821] | 212 | break;
|
---|
[2725] | 213 | }
|
---|
| 214 | marks[*cp - 'a'] = num2;
|
---|
| 215 | break;
|
---|
[821] | 216 |
|
---|
[2725] | 217 | case 'l':
|
---|
| 218 | printLines(num1, num2, TRUE);
|
---|
| 219 | break;
|
---|
[821] | 220 |
|
---|
[2725] | 221 | case 'p':
|
---|
| 222 | printLines(num1, num2, FALSE);
|
---|
| 223 | break;
|
---|
[821] | 224 |
|
---|
[2725] | 225 | case 'q':
|
---|
| 226 | cp = skip_blank(cp);
|
---|
| 227 | if (have1 || *cp) {
|
---|
| 228 | bb_error_msg("bad quit command");
|
---|
[821] | 229 | break;
|
---|
[2725] | 230 | }
|
---|
| 231 | if (!dirty)
|
---|
| 232 | return;
|
---|
[3232] | 233 | len = read_line_input(NULL, "Really quit? ", buf, 16, /*timeout*/ -1);
|
---|
[2725] | 234 | /* 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;
|
---|
[821] | 241 |
|
---|
[2725] | 242 | case 'r':
|
---|
| 243 | if (*cp && !isblank(*cp)) {
|
---|
| 244 | bb_error_msg("bad read command");
|
---|
[821] | 245 | break;
|
---|
[2725] | 246 | }
|
---|
| 247 | cp = skip_blank(cp);
|
---|
| 248 | if (*cp == '\0') {
|
---|
| 249 | bb_error_msg("no file name");
|
---|
[821] | 250 | break;
|
---|
[2725] | 251 | }
|
---|
| 252 | if (!have1)
|
---|
| 253 | num1 = lastNum;
|
---|
| 254 | if (readLines(cp, num1 + 1))
|
---|
[821] | 255 | break;
|
---|
[2725] | 256 | if (fileName == NULL)
|
---|
| 257 | fileName = xstrdup(cp);
|
---|
| 258 | break;
|
---|
[821] | 259 |
|
---|
[2725] | 260 | case 's':
|
---|
| 261 | subCommand(cp, num1, num2);
|
---|
| 262 | break;
|
---|
[821] | 263 |
|
---|
[2725] | 264 | case 'w':
|
---|
| 265 | if (*cp && !isblank(*cp)) {
|
---|
| 266 | bb_error_msg("bad write command");
|
---|
[821] | 267 | break;
|
---|
[2725] | 268 | }
|
---|
| 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");
|
---|
[821] | 278 | break;
|
---|
[2725] | 279 | }
|
---|
| 280 | writeLines(cp, num1, num2);
|
---|
| 281 | break;
|
---|
[821] | 282 |
|
---|
[2725] | 283 | case 'z':
|
---|
| 284 | switch (*cp) {
|
---|
| 285 | case '-':
|
---|
| 286 | printLines(curNum - 21, curNum, FALSE);
|
---|
[821] | 287 | break;
|
---|
| 288 | case '.':
|
---|
[2725] | 289 | printLines(curNum - 11, curNum + 10, FALSE);
|
---|
[821] | 290 | break;
|
---|
[2725] | 291 | default:
|
---|
| 292 | printLines(curNum, curNum + 21, FALSE);
|
---|
[821] | 293 | break;
|
---|
[2725] | 294 | }
|
---|
| 295 | break;
|
---|
[821] | 296 |
|
---|
[2725] | 297 | case '.':
|
---|
| 298 | if (have1) {
|
---|
| 299 | bb_error_msg("no arguments allowed");
|
---|
[821] | 300 | break;
|
---|
[2725] | 301 | }
|
---|
| 302 | printLines(curNum, curNum, FALSE);
|
---|
| 303 | break;
|
---|
[821] | 304 |
|
---|
[2725] | 305 | case '-':
|
---|
| 306 | if (setCurNum(curNum - 1))
|
---|
| 307 | printLines(curNum, curNum, FALSE);
|
---|
| 308 | break;
|
---|
[821] | 309 |
|
---|
[2725] | 310 | case '=':
|
---|
| 311 | printf("%d\n", num1);
|
---|
| 312 | break;
|
---|
| 313 | case '\0':
|
---|
| 314 | if (have1) {
|
---|
| 315 | printLines(num2, num2, FALSE);
|
---|
[821] | 316 | break;
|
---|
[2725] | 317 | }
|
---|
| 318 | if (setCurNum(curNum + 1))
|
---|
| 319 | printLines(curNum, curNum, FALSE);
|
---|
| 320 | break;
|
---|
[821] | 321 |
|
---|
[2725] | 322 | default:
|
---|
| 323 | bb_error_msg("unimplemented command");
|
---|
| 324 | break;
|
---|
[821] | 325 | }
|
---|
| 326 | }
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 |
|
---|
| 330 | /*
|
---|
| 331 | * Do the substitute command.
|
---|
| 332 | * The current line is set to the last substitution done.
|
---|
| 333 | */
|
---|
[2725] | 334 | static void subCommand(const char *cmd, int num1, int num2)
|
---|
[821] | 335 | {
|
---|
| 336 | char *cp, *oldStr, *newStr, buf[USERSIZE];
|
---|
[1765] | 337 | int delim, oldLen, newLen, deltaLen, offset;
|
---|
[821] | 338 | LINE *lp, *nlp;
|
---|
| 339 | int globalFlag, printFlag, didSub, needPrint;
|
---|
| 340 |
|
---|
[2725] | 341 | if (bad_nums(num1, num2, "substitute"))
|
---|
[821] | 342 | return;
|
---|
| 343 |
|
---|
| 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 |
|
---|
[1765] | 355 | if (isblank(*cp) || (*cp == '\0')) {
|
---|
| 356 | bb_error_msg("bad delimiter for substitute");
|
---|
[821] | 357 | return;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | delim = *cp++;
|
---|
| 361 | oldStr = cp;
|
---|
| 362 |
|
---|
| 363 | cp = strchr(cp, delim);
|
---|
[1765] | 364 | if (cp == NULL) {
|
---|
| 365 | bb_error_msg("missing 2nd delimiter for substitute");
|
---|
[821] | 366 | return;
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | *cp++ = '\0';
|
---|
| 370 |
|
---|
| 371 | newStr = cp;
|
---|
| 372 | cp = strchr(cp, delim);
|
---|
| 373 |
|
---|
| 374 | if (cp)
|
---|
| 375 | *cp++ = '\0';
|
---|
| 376 | else
|
---|
[1765] | 377 | cp = (char*)"";
|
---|
[821] | 378 |
|
---|
[1765] | 379 | while (*cp) switch (*cp++) {
|
---|
[821] | 380 | case 'g':
|
---|
| 381 | globalFlag = TRUE;
|
---|
| 382 | break;
|
---|
| 383 | case 'p':
|
---|
| 384 | printFlag = TRUE;
|
---|
| 385 | break;
|
---|
| 386 | default:
|
---|
[1765] | 387 | bb_error_msg("unknown option for substitute");
|
---|
[821] | 388 | return;
|
---|
| 389 | }
|
---|
| 390 |
|
---|
[1765] | 391 | if (*oldStr == '\0') {
|
---|
| 392 | if (searchString[0] == '\0') {
|
---|
| 393 | bb_error_msg("no previous search string");
|
---|
[821] | 394 | return;
|
---|
| 395 | }
|
---|
| 396 | oldStr = searchString;
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | if (oldStr != searchString)
|
---|
| 400 | strcpy(searchString, oldStr);
|
---|
| 401 |
|
---|
| 402 | lp = findLine(num1);
|
---|
| 403 | 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 |
|
---|
[1765] | 412 | while (num1 <= num2) {
|
---|
[821] | 413 | offset = findString(lp, oldStr, oldLen, offset);
|
---|
| 414 |
|
---|
[1765] | 415 | if (offset < 0) {
|
---|
| 416 | if (needPrint) {
|
---|
[821] | 417 | printLines(num1, num1, FALSE);
|
---|
| 418 | needPrint = FALSE;
|
---|
| 419 | }
|
---|
| 420 | offset = 0;
|
---|
| 421 | lp = lp->next;
|
---|
| 422 | num1++;
|
---|
| 423 | 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 | */
|
---|
[1765] | 434 | if (deltaLen <= 0) {
|
---|
[821] | 435 | memcpy(&lp->data[offset], newStr, newLen);
|
---|
[1765] | 436 | if (deltaLen) {
|
---|
[821] | 437 | memcpy(&lp->data[offset + newLen],
|
---|
| 438 | &lp->data[offset + oldLen],
|
---|
| 439 | lp->len - offset - oldLen);
|
---|
| 440 |
|
---|
| 441 | lp->len += deltaLen;
|
---|
| 442 | }
|
---|
| 443 | offset += newLen;
|
---|
| 444 | if (globalFlag)
|
---|
| 445 | continue;
|
---|
[1765] | 446 | if (needPrint) {
|
---|
[821] | 447 | printLines(num1, num1, FALSE);
|
---|
| 448 | needPrint = FALSE;
|
---|
| 449 | }
|
---|
| 450 | lp = lp->next;
|
---|
| 451 | num1++;
|
---|
| 452 | continue;
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | /*
|
---|
| 456 | * The new string is larger, so allocate a new line
|
---|
[3232] | 457 | * structure and use that. Link it in place of
|
---|
[821] | 458 | * the old line structure.
|
---|
| 459 | */
|
---|
[2725] | 460 | nlp = xmalloc(sizeof(LINE) + lp->len + deltaLen);
|
---|
[821] | 461 |
|
---|
| 462 | nlp->len = lp->len + deltaLen;
|
---|
| 463 |
|
---|
| 464 | memcpy(nlp->data, lp->data, offset);
|
---|
| 465 | memcpy(&nlp->data[offset], newStr, newLen);
|
---|
| 466 | 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 |
|
---|
[1765] | 486 | if (needPrint) {
|
---|
[821] | 487 | printLines(num1, num1, FALSE);
|
---|
| 488 | needPrint = FALSE;
|
---|
| 489 | }
|
---|
| 490 |
|
---|
| 491 | lp = lp->next;
|
---|
| 492 | num1++;
|
---|
| 493 | }
|
---|
| 494 |
|
---|
| 495 | if (!didSub)
|
---|
[1765] | 496 | bb_error_msg("no substitutions found for \"%s\"", oldStr);
|
---|
[821] | 497 | }
|
---|
| 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 | */
|
---|
[2725] | 504 | static int findString(const LINE *lp, const char *str, int len, int offset)
|
---|
[821] | 505 | {
|
---|
| 506 | int left;
|
---|
| 507 | const char *cp, *ncp;
|
---|
| 508 |
|
---|
| 509 | cp = &lp->data[offset];
|
---|
| 510 | left = lp->len - offset;
|
---|
| 511 |
|
---|
[1765] | 512 | while (left >= len) {
|
---|
[821] | 513 | ncp = memchr(cp, *str, left);
|
---|
| 514 | if (ncp == NULL)
|
---|
| 515 | return -1;
|
---|
| 516 | left -= (ncp - cp);
|
---|
| 517 | if (left < len)
|
---|
| 518 | return -1;
|
---|
| 519 | cp = ncp;
|
---|
| 520 | if (memcmp(cp, str, len) == 0)
|
---|
| 521 | return (cp - lp->data);
|
---|
| 522 | 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 | */
|
---|
| 536 | static void addLines(int num)
|
---|
| 537 | {
|
---|
[1765] | 538 | int len;
|
---|
| 539 | char buf[USERSIZE + 1];
|
---|
[821] | 540 |
|
---|
[2725] | 541 | 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 | */
|
---|
[3232] | 547 | len = read_line_input(NULL, "", buf, sizeof(buf), /*timeout*/ -1);
|
---|
[2725] | 548 | if (len <= 0) {
|
---|
| 549 | /* Previously, ctrl-C was exiting to shell.
|
---|
| 550 | * Now we exit to ed prompt. Is in important? */
|
---|
| 551 | return;
|
---|
| 552 | }
|
---|
[821] | 553 | if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0'))
|
---|
| 554 | return;
|
---|
| 555 | 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.
|
---|
| 564 | * Returns TRUE if successful (whether or not there was a number).
|
---|
| 565 | * 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 | */
|
---|
[2725] | 569 | static int getNum(const char **retcp, smallint *retHaveNum, int *retNum)
|
---|
[821] | 570 | {
|
---|
| 571 | const char *cp;
|
---|
| 572 | char *endStr, str[USERSIZE];
|
---|
[2725] | 573 | int value, num;
|
---|
| 574 | smallint haveNum, minus;
|
---|
[821] | 575 |
|
---|
| 576 | cp = *retcp;
|
---|
[2725] | 577 | value = 0;
|
---|
[821] | 578 | haveNum = FALSE;
|
---|
[2725] | 579 | minus = 0;
|
---|
[821] | 580 |
|
---|
[1765] | 581 | while (TRUE) {
|
---|
[2725] | 582 | cp = skip_blank(cp);
|
---|
[821] | 583 |
|
---|
[1765] | 584 | switch (*cp) {
|
---|
[821] | 585 | 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++;
|
---|
[1765] | 599 | if ((*cp < 'a') || (*cp > 'z')) {
|
---|
| 600 | bb_error_msg("bad mark name");
|
---|
[821] | 601 | return FALSE;
|
---|
| 602 | }
|
---|
| 603 | haveNum = TRUE;
|
---|
| 604 | num = marks[*cp++ - 'a'];
|
---|
| 605 | break;
|
---|
| 606 |
|
---|
| 607 | case '/':
|
---|
| 608 | strcpy(str, ++cp);
|
---|
| 609 | endStr = strchr(str, '/');
|
---|
[1765] | 610 | if (endStr) {
|
---|
[821] | 611 | *endStr++ = '\0';
|
---|
| 612 | cp += (endStr - str);
|
---|
[2725] | 613 | } else
|
---|
[821] | 614 | cp = "";
|
---|
| 615 | num = searchLines(str, curNum, lastNum);
|
---|
| 616 | if (num == 0)
|
---|
| 617 | return FALSE;
|
---|
| 618 | haveNum = TRUE;
|
---|
| 619 | break;
|
---|
| 620 |
|
---|
| 621 | default:
|
---|
[1765] | 622 | if (!isdigit(*cp)) {
|
---|
[821] | 623 | *retcp = cp;
|
---|
| 624 | *retHaveNum = haveNum;
|
---|
| 625 | *retNum = value;
|
---|
| 626 | return TRUE;
|
---|
| 627 | }
|
---|
| 628 | num = 0;
|
---|
| 629 | while (isdigit(*cp))
|
---|
| 630 | num = num * 10 + *cp++ - '0';
|
---|
| 631 | haveNum = TRUE;
|
---|
| 632 | break;
|
---|
| 633 | }
|
---|
| 634 |
|
---|
[2725] | 635 | value += (minus ? -num : num);
|
---|
[821] | 636 |
|
---|
[2725] | 637 | cp = skip_blank(cp);
|
---|
[821] | 638 |
|
---|
[1765] | 639 | switch (*cp) {
|
---|
[821] | 640 | case '-':
|
---|
[2725] | 641 | minus = 1;
|
---|
[821] | 642 | cp++;
|
---|
| 643 | break;
|
---|
| 644 |
|
---|
| 645 | case '+':
|
---|
[2725] | 646 | minus = 0;
|
---|
[821] | 647 | cp++;
|
---|
| 648 | break;
|
---|
| 649 |
|
---|
| 650 | default:
|
---|
| 651 | *retcp = cp;
|
---|
| 652 | *retHaveNum = haveNum;
|
---|
| 653 | *retNum = value;
|
---|
| 654 | return TRUE;
|
---|
| 655 | }
|
---|
| 656 | }
|
---|
| 657 | }
|
---|
| 658 |
|
---|
| 659 |
|
---|
| 660 | /*
|
---|
| 661 | * Read lines from a file at the specified line number.
|
---|
| 662 | * Returns TRUE if the file was successfully read.
|
---|
| 663 | */
|
---|
[2725] | 664 | static int readLines(const char *file, int num)
|
---|
[821] | 665 | {
|
---|
[1765] | 666 | int fd, cc;
|
---|
[821] | 667 | int len, lineCount, charCount;
|
---|
| 668 | char *cp;
|
---|
| 669 |
|
---|
[1765] | 670 | if ((num < 1) || (num > lastNum + 1)) {
|
---|
| 671 | bb_error_msg("bad line for read");
|
---|
[821] | 672 | return FALSE;
|
---|
| 673 | }
|
---|
| 674 |
|
---|
| 675 | fd = open(file, 0);
|
---|
[1765] | 676 | if (fd < 0) {
|
---|
[2725] | 677 | bb_simple_perror_msg(file);
|
---|
[821] | 678 | return FALSE;
|
---|
| 679 | }
|
---|
| 680 |
|
---|
| 681 | bufPtr = bufBase;
|
---|
| 682 | bufUsed = 0;
|
---|
| 683 | lineCount = 0;
|
---|
| 684 | charCount = 0;
|
---|
| 685 | cc = 0;
|
---|
| 686 |
|
---|
| 687 | printf("\"%s\", ", file);
|
---|
[2725] | 688 | fflush_all();
|
---|
[821] | 689 |
|
---|
[1765] | 690 | do {
|
---|
[821] | 691 | cp = memchr(bufPtr, '\n', bufUsed);
|
---|
| 692 |
|
---|
[1765] | 693 | if (cp) {
|
---|
[821] | 694 | len = (cp - bufPtr) + 1;
|
---|
[1765] | 695 | if (!insertLine(num, bufPtr, len)) {
|
---|
[821] | 696 | close(fd);
|
---|
| 697 | return FALSE;
|
---|
| 698 | }
|
---|
| 699 | bufPtr += len;
|
---|
| 700 | bufUsed -= len;
|
---|
| 701 | charCount += len;
|
---|
| 702 | lineCount++;
|
---|
| 703 | num++;
|
---|
| 704 | continue;
|
---|
| 705 | }
|
---|
| 706 |
|
---|
[1765] | 707 | if (bufPtr != bufBase) {
|
---|
[821] | 708 | memcpy(bufBase, bufPtr, bufUsed);
|
---|
| 709 | bufPtr = bufBase + bufUsed;
|
---|
| 710 | }
|
---|
| 711 |
|
---|
[1765] | 712 | if (bufUsed >= bufSize) {
|
---|
[821] | 713 | len = (bufSize * 3) / 2;
|
---|
[2725] | 714 | cp = xrealloc(bufBase, len);
|
---|
[821] | 715 | bufBase = cp;
|
---|
| 716 | bufPtr = bufBase + bufUsed;
|
---|
| 717 | bufSize = len;
|
---|
| 718 | }
|
---|
| 719 |
|
---|
[2725] | 720 | cc = safe_read(fd, bufPtr, bufSize - bufUsed);
|
---|
[821] | 721 | bufUsed += cc;
|
---|
| 722 | bufPtr = bufBase;
|
---|
| 723 |
|
---|
[1765] | 724 | } while (cc > 0);
|
---|
[821] | 725 |
|
---|
[1765] | 726 | if (cc < 0) {
|
---|
[2725] | 727 | bb_simple_perror_msg(file);
|
---|
[821] | 728 | close(fd);
|
---|
| 729 | return FALSE;
|
---|
| 730 | }
|
---|
| 731 |
|
---|
[1765] | 732 | if (bufUsed) {
|
---|
| 733 | if (!insertLine(num, bufPtr, bufUsed)) {
|
---|
[821] | 734 | close(fd);
|
---|
| 735 | return -1;
|
---|
| 736 | }
|
---|
| 737 | 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 | */
|
---|
[2725] | 754 | static int writeLines(const char *file, int num1, int num2)
|
---|
[821] | 755 | {
|
---|
| 756 | LINE *lp;
|
---|
[1765] | 757 | int fd, lineCount, charCount;
|
---|
[821] | 758 |
|
---|
[2725] | 759 | if (bad_nums(num1, num2, "write"))
|
---|
[821] | 760 | return FALSE;
|
---|
| 761 |
|
---|
| 762 | lineCount = 0;
|
---|
| 763 | charCount = 0;
|
---|
| 764 |
|
---|
| 765 | fd = creat(file, 0666);
|
---|
| 766 | if (fd < 0) {
|
---|
[2725] | 767 | bb_simple_perror_msg(file);
|
---|
[821] | 768 | return FALSE;
|
---|
| 769 | }
|
---|
| 770 |
|
---|
| 771 | printf("\"%s\", ", file);
|
---|
[2725] | 772 | fflush_all();
|
---|
[821] | 773 |
|
---|
| 774 | lp = findLine(num1);
|
---|
[1765] | 775 | if (lp == NULL) {
|
---|
[821] | 776 | close(fd);
|
---|
| 777 | return FALSE;
|
---|
| 778 | }
|
---|
| 779 |
|
---|
[1765] | 780 | while (num1++ <= num2) {
|
---|
[2725] | 781 | if (full_write(fd, lp->data, lp->len) != lp->len) {
|
---|
| 782 | bb_simple_perror_msg(file);
|
---|
[821] | 783 | close(fd);
|
---|
| 784 | return FALSE;
|
---|
| 785 | }
|
---|
| 786 | charCount += lp->len;
|
---|
| 787 | lineCount++;
|
---|
| 788 | lp = lp->next;
|
---|
| 789 | }
|
---|
| 790 |
|
---|
[1765] | 791 | if (close(fd) < 0) {
|
---|
[2725] | 792 | bb_simple_perror_msg(file);
|
---|
[821] | 793 | return FALSE;
|
---|
| 794 | }
|
---|
| 795 |
|
---|
| 796 | printf("%d lines, %d chars\n", lineCount, charCount);
|
---|
| 797 | 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 | */
|
---|
| 807 | static int printLines(int num1, int num2, int expandFlag)
|
---|
| 808 | {
|
---|
| 809 | const LINE *lp;
|
---|
| 810 | const char *cp;
|
---|
| 811 | int ch, count;
|
---|
| 812 |
|
---|
[2725] | 813 | if (bad_nums(num1, num2, "print"))
|
---|
[821] | 814 | return FALSE;
|
---|
| 815 |
|
---|
| 816 | lp = findLine(num1);
|
---|
| 817 | if (lp == NULL)
|
---|
| 818 | return FALSE;
|
---|
| 819 |
|
---|
[1765] | 820 | while (num1 <= num2) {
|
---|
| 821 | if (!expandFlag) {
|
---|
[2725] | 822 | write(STDOUT_FILENO, lp->data, lp->len);
|
---|
[821] | 823 | setCurNum(num1++);
|
---|
| 824 | lp = lp->next;
|
---|
| 825 | 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 |
|
---|
[1765] | 838 | while (count-- > 0) {
|
---|
[2725] | 839 | ch = (unsigned char) *cp++;
|
---|
| 840 | fputc_printable(ch | PRINTABLE_META, stdout);
|
---|
[821] | 841 | }
|
---|
| 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 | */
|
---|
[2725] | 860 | static int insertLine(int num, const char *data, int len)
|
---|
[821] | 861 | {
|
---|
| 862 | LINE *newLp, *lp;
|
---|
| 863 |
|
---|
[1765] | 864 | if ((num < 1) || (num > lastNum + 1)) {
|
---|
| 865 | bb_error_msg("inserting at bad line number");
|
---|
[821] | 866 | return FALSE;
|
---|
| 867 | }
|
---|
| 868 |
|
---|
[2725] | 869 | newLp = xmalloc(sizeof(LINE) + len - 1);
|
---|
[821] | 870 |
|
---|
| 871 | memcpy(newLp->data, data, len);
|
---|
| 872 | newLp->len = len;
|
---|
| 873 |
|
---|
| 874 | if (num > lastNum)
|
---|
| 875 | lp = &lines;
|
---|
[1765] | 876 | else {
|
---|
[821] | 877 | lp = findLine(num);
|
---|
[1765] | 878 | if (lp == NULL) {
|
---|
[821] | 879 | free((char *) newLp);
|
---|
| 880 | 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;
|
---|
| 891 | return setCurNum(num);
|
---|
| 892 | }
|
---|
| 893 |
|
---|
| 894 |
|
---|
| 895 | /*
|
---|
| 896 | * Delete lines from the given range.
|
---|
| 897 | */
|
---|
[2725] | 898 | static void deleteLines(int num1, int num2)
|
---|
[821] | 899 | {
|
---|
| 900 | LINE *lp, *nlp, *plp;
|
---|
| 901 | int count;
|
---|
| 902 |
|
---|
[2725] | 903 | if (bad_nums(num1, num2, "delete"))
|
---|
| 904 | return;
|
---|
[821] | 905 |
|
---|
| 906 | lp = findLine(num1);
|
---|
| 907 | if (lp == NULL)
|
---|
[2725] | 908 | return;
|
---|
[821] | 909 |
|
---|
[1765] | 910 | if ((curNum >= num1) && (curNum <= num2)) {
|
---|
[821] | 911 | 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;
|
---|
| 920 | if (curNum > num2)
|
---|
| 921 | curNum -= count;
|
---|
| 922 | lastNum -= count;
|
---|
| 923 |
|
---|
[1765] | 924 | while (count-- > 0) {
|
---|
[821] | 925 | nlp = lp->next;
|
---|
| 926 | plp = lp->prev;
|
---|
| 927 | plp->next = nlp;
|
---|
| 928 | nlp->prev = plp;
|
---|
| 929 | free(lp);
|
---|
| 930 | lp = nlp;
|
---|
| 931 | }
|
---|
| 932 |
|
---|
| 933 | dirty = TRUE;
|
---|
| 934 | }
|
---|
| 935 |
|
---|
| 936 |
|
---|
| 937 | /*
|
---|
| 938 | * Search for a line which contains the specified string.
|
---|
[2725] | 939 | * If the string is "", then the previously searched for string
|
---|
[821] | 940 | * 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 | */
|
---|
[2725] | 944 | static NOINLINE int searchLines(const char *str, int num1, int num2)
|
---|
[821] | 945 | {
|
---|
| 946 | const LINE *lp;
|
---|
| 947 | int len;
|
---|
| 948 |
|
---|
[2725] | 949 | if (bad_nums(num1, num2, "search"))
|
---|
[821] | 950 | return 0;
|
---|
| 951 |
|
---|
[1765] | 952 | if (*str == '\0') {
|
---|
| 953 | if (searchString[0] == '\0') {
|
---|
| 954 | bb_error_msg("no previous search string");
|
---|
[821] | 955 | return 0;
|
---|
| 956 | }
|
---|
| 957 | str = searchString;
|
---|
| 958 | }
|
---|
| 959 |
|
---|
| 960 | if (str != searchString)
|
---|
| 961 | strcpy(searchString, str);
|
---|
| 962 |
|
---|
| 963 | len = strlen(str);
|
---|
| 964 |
|
---|
| 965 | lp = findLine(num1);
|
---|
| 966 | if (lp == NULL)
|
---|
| 967 | return 0;
|
---|
| 968 |
|
---|
[1765] | 969 | while (num1 <= num2) {
|
---|
[821] | 970 | if (findString(lp, str, len, 0) >= 0)
|
---|
| 971 | return num1;
|
---|
| 972 | num1++;
|
---|
| 973 | lp = lp->next;
|
---|
| 974 | }
|
---|
| 975 |
|
---|
[2725] | 976 | bb_error_msg("can't find string \"%s\"", str);
|
---|
[821] | 977 | return 0;
|
---|
| 978 | }
|
---|
| 979 |
|
---|
| 980 |
|
---|
| 981 | /*
|
---|
| 982 | * Return a pointer to the specified line number.
|
---|
| 983 | */
|
---|
| 984 | static LINE *findLine(int num)
|
---|
| 985 | {
|
---|
| 986 | LINE *lp;
|
---|
| 987 | int lnum;
|
---|
| 988 |
|
---|
[1765] | 989 | if ((num < 1) || (num > lastNum)) {
|
---|
| 990 | bb_error_msg("line number %d does not exist", num);
|
---|
[821] | 991 | return NULL;
|
---|
| 992 | }
|
---|
| 993 |
|
---|
[1765] | 994 | if (curNum <= 0) {
|
---|
[821] | 995 | curNum = 1;
|
---|
| 996 | curLine = lines.next;
|
---|
| 997 | }
|
---|
| 998 |
|
---|
| 999 | if (num == curNum)
|
---|
| 1000 | return curLine;
|
---|
| 1001 |
|
---|
| 1002 | lp = curLine;
|
---|
| 1003 | lnum = curNum;
|
---|
[1765] | 1004 | if (num < (curNum / 2)) {
|
---|
[821] | 1005 | lp = lines.next;
|
---|
| 1006 | lnum = 1;
|
---|
[2725] | 1007 | } else if (num > ((curNum + lastNum) / 2)) {
|
---|
[821] | 1008 | lp = lines.prev;
|
---|
| 1009 | lnum = lastNum;
|
---|
| 1010 | }
|
---|
| 1011 |
|
---|
[1765] | 1012 | while (lnum < num) {
|
---|
[821] | 1013 | lp = lp->next;
|
---|
| 1014 | lnum++;
|
---|
| 1015 | }
|
---|
| 1016 |
|
---|
[1765] | 1017 | while (lnum > num) {
|
---|
[821] | 1018 | lp = lp->prev;
|
---|
| 1019 | lnum--;
|
---|
| 1020 | }
|
---|
| 1021 | return lp;
|
---|
| 1022 | }
|
---|
| 1023 |
|
---|
| 1024 |
|
---|
| 1025 | /*
|
---|
| 1026 | * Set the current line number.
|
---|
| 1027 | * Returns TRUE if successful.
|
---|
| 1028 | */
|
---|
| 1029 | static int setCurNum(int num)
|
---|
| 1030 | {
|
---|
| 1031 | LINE *lp;
|
---|
| 1032 |
|
---|
| 1033 | lp = findLine(num);
|
---|
| 1034 | if (lp == NULL)
|
---|
| 1035 | return FALSE;
|
---|
| 1036 | curNum = num;
|
---|
| 1037 | curLine = lp;
|
---|
| 1038 | return TRUE;
|
---|
| 1039 | }
|
---|