el_source(): * Source a file */
| 534 | * Source a file |
| 535 | */ |
| 536 | public int |
| 537 | el_source(EditLine *el, const char *fname) |
| 538 | { |
| 539 | FILE *fp; |
| 540 | size_t len; |
| 541 | char *ptr; |
| 542 | char *path = NULL; |
| 543 | const Char *dptr; |
| 544 | int error = 0; |
| 545 | |
| 546 | fp = NULL; |
| 547 | if (fname == NULL) { |
| 548 | static const char elpath[] = "/.editrc"; |
| 549 | size_t plen = sizeof(elpath); |
| 550 | |
| 551 | if ((ptr = secure_getenv("HOME")) == NULL) |
| 552 | return -1; |
| 553 | plen += strlen(ptr); |
| 554 | if ((path = el_malloc(plen * sizeof(*path))) == NULL) |
| 555 | return -1; |
| 556 | (void)snprintf(path, plen, "%s%s", ptr, elpath); |
| 557 | fname = path; |
| 558 | } |
| 559 | if (fp == NULL) |
| 560 | fp = fopen(fname, "r"); |
| 561 | if (fp == NULL) { |
| 562 | el_free(path); |
| 563 | return -1; |
| 564 | } |
| 565 | |
| 566 | while ((ptr = fgetln(fp, &len)) != NULL) { |
| 567 | if (*ptr == '\n') |
| 568 | continue; /* Empty line. */ |
| 569 | dptr = ct_decode_string(ptr, &el->el_scratch); |
| 570 | if (!dptr) |
| 571 | continue; |
| 572 | if (len > 0 && dptr[len - 1] == '\n') |
| 573 | --len; |
| 574 | |
| 575 | /* loop until first non-space char or EOL */ |
| 576 | while (*dptr != '\0' && Isspace(*dptr)) |
| 577 | dptr++; |
| 578 | if (*dptr == '#') |
| 579 | continue; /* ignore, this is a comment line */ |
| 580 | if ((error = parse_line(el, dptr)) == -1) |
| 581 | break; |
| 582 | } |
| 583 | |
| 584 | el_free(path); |
| 585 | (void) fclose(fp); |
| 586 | return error; |
| 587 | } |
| 588 | |
| 589 | |
| 590 | /* el_resize(): |
no test coverage detected