Changeset 1765 in MondoRescue for branches/2.2.5/mindi-busybox/editors/ed.c


Ignore:
Timestamp:
Nov 4, 2007, 3:16:40 AM (17 years ago)
Author:
Bruno Cornec
Message:

Update to busybox 1.7.2

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.5/mindi-busybox/editors/ed.c

    r821 r1765  
    88 */
    99
    10 #include <stdio.h>
    11 #include <stdlib.h>
    12 #include <unistd.h>
    13 #include <fcntl.h>
    14 #include <string.h>
    15 #include <time.h>
    16 #include <ctype.h>
    17 #include <sys/param.h>
    18 #include "busybox.h"
    19 
    20 #define USERSIZE    1024    /* max line length typed in by user */
    21 #define INITBUF_SIZE    1024    /* initial buffer size */
     10#include "libbb.h"
     11
     12#define searchString bb_common_bufsiz1
     13
     14enum {
     15    USERSIZE = sizeof(searchString) > 1024 ? 1024
     16             : sizeof(searchString) - 1, /* max line length typed in by user */
     17    INITBUF_SIZE = 1024, /* initial buffer size */
     18};
     19
    2220typedef struct LINE {
    2321    struct LINE *next;
     
    2927static LINE lines, *curLine;
    3028static int curNum, lastNum, marks[26], dirty;
    31 static char *bufBase, *bufPtr, *fileName, searchString[USERSIZE];
     29static char *bufBase, *bufPtr, *fileName;
    3230static int bufUsed, bufSize;
    3331
     
    4947static int findString(const LINE *lp, const char * str, int len, int offset);
    5048
     49int ed_main(int argc, char **argv);
    5150int ed_main(int argc, char **argv)
    5251{
     
    5857
    5958        if (fileName == NULL) {
    60             bb_error_msg("No memory");
     59            bb_error_msg("no memory");
    6160            termEdit();
    6261            return EXIT_SUCCESS;
     
    8988    int len, num1, num2, have1, have2;
    9089
    91     while (TRUE)
    92     {
     90    while (TRUE) {
    9391        printf(": ");
    9492        fflush(stdout);
     
    104102        endbuf = &buf[len - 1];
    105103
    106         if (*endbuf != '\n')
    107         {
    108             bb_error_msg("Command line too long");
    109 
    110             do
    111             {
     104        if (*endbuf != '\n') {
     105            bb_error_msg("command line too long");
     106
     107            do {
    112108                len = fgetc(stdin);
    113             }
    114             while ((len != EOF) && (len != '\n'));
     109            } while ((len != EOF) && (len != '\n'));
    115110
    116111            continue;
     
    130125        have2 = FALSE;
    131126
    132         if ((curNum == 0) && (lastNum > 0))
    133         {
     127        if ((curNum == 0) && (lastNum > 0)) {
    134128            curNum = 1;
    135129            curLine = lines.next;
     
    142136            cp++;
    143137
    144         if (*cp == ',')
    145         {
     138        if (*cp == ',') {
    146139            cp++;
    147140
     
    165158            num2 = num1;
    166159
    167         switch (*cp++)
    168         {
     160        switch (*cp++) {
    169161            case 'a':
    170162                addLines(num1 + 1);
     
    181173
    182174            case 'f':
    183                 if (*cp && !isblank(*cp))
    184                 {
    185                     bb_error_msg("Bad file command");
     175                if (*cp && !isblank(*cp)) {
     176                    bb_error_msg("bad file command");
    186177                    break;
    187178                }
     
    190181                    cp++;
    191182
    192                 if (*cp == '\0')
    193                 {
     183                if (*cp == '\0') {
    194184                    if (fileName)
    195185                        printf("\"%s\"\n", fileName);
    196186                    else
    197187                        printf("No file name\n");
    198 
    199188                    break;
    200189                }
     
    202191                newname = strdup(cp);
    203192
    204                 if (newname == NULL)
    205                 {
    206                     bb_error_msg("No memory for file name");
     193                if (newname == NULL) {
     194                    bb_error_msg("no memory for file name");
    207195                    break;
    208196                }
     
    222210                    cp++;
    223211
    224                 if ((*cp < 'a') || (*cp > 'a') || cp[1])
    225                 {
    226                     bb_error_msg("Bad mark name");
     212                if ((*cp < 'a') || (*cp > 'a') || cp[1]) {
     213                    bb_error_msg("bad mark name");
    227214                    break;
    228215                }
     
    243230                    cp++;
    244231
    245                 if (have1 || *cp)
    246                 {
    247                     bb_error_msg("Bad quit command");
     232                if (have1 || *cp) {
     233                    bb_error_msg("bad quit command");
    248234                    break;
    249235                }
     
    268254
    269255            case 'r':
    270                 if (*cp && !isblank(*cp))
    271                 {
    272                     bb_error_msg("Bad read command");
     256                if (*cp && !isblank(*cp)) {
     257                    bb_error_msg("bad read command");
    273258                    break;
    274259                }
     
    277262                    cp++;
    278263
    279                 if (*cp == '\0')
    280                 {
    281                     bb_error_msg("No file name");
     264                if (*cp == '\0') {
     265                    bb_error_msg("no file name");
    282266                    break;
    283267                }
     
    299283
    300284            case 'w':
    301                 if (*cp && !isblank(*cp))
    302                 {
    303                     bb_error_msg("Bad write command");
     285                if (*cp && !isblank(*cp)) {
     286                    bb_error_msg("bad write command");
    304287                    break;
    305288                }
     
    316299                    cp = fileName;
    317300
    318                 if (cp == NULL)
    319                 {
    320                     bb_error_msg("No file name specified");
     301                if (cp == NULL) {
     302                    bb_error_msg("no file name specified");
    321303                    break;
    322304                }
     
    326308
    327309            case 'z':
    328                 switch (*cp)
    329                 {
     310                switch (*cp) {
    330311                case '-':
    331312                    printLines(curNum-21, curNum, FALSE);
     
    341322
    342323            case '.':
    343                 if (have1)
    344                 {
    345                     bb_error_msg("No arguments allowed");
     324                if (have1) {
     325                    bb_error_msg("no arguments allowed");
    346326                    break;
    347327                }
     
    361341
    362342            case '\0':
    363                 if (have1)
    364                 {
     343                if (have1) {
    365344                    printLines(num2, num2, FALSE);
    366345                    break;
     
    373352
    374353            default:
    375                 bb_error_msg("Unimplemented command");
     354                bb_error_msg("unimplemented command");
    376355                break;
    377356        }
     
    387366{
    388367    char *cp, *oldStr, *newStr, buf[USERSIZE];
    389     int delim, oldLen, newLen, deltaLen, offset;
     368    int delim, oldLen, newLen, deltaLen, offset;
    390369    LINE *lp, *nlp;
    391370    int globalFlag, printFlag, didSub, needPrint;
    392371
    393     if ((num1 < 1) || (num2 > lastNum) || (num1 > num2))
    394     {
    395         bb_error_msg("Bad line range for substitute");
    396 
     372    if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
     373        bb_error_msg("bad line range for substitute");
    397374        return;
    398375    }
     
    409386    cp = buf;
    410387
    411     if (isblank(*cp) || (*cp == '\0'))
    412     {
    413         bb_error_msg("Bad delimiter for substitute");
    414 
     388    if (isblank(*cp) || (*cp == '\0')) {
     389        bb_error_msg("bad delimiter for substitute");
    415390        return;
    416391    }
     
    421396    cp = strchr(cp, delim);
    422397
    423     if (cp == NULL)
    424     {
    425         bb_error_msg("Missing 2nd delimiter for substitute");
    426 
     398    if (cp == NULL) {
     399        bb_error_msg("missing 2nd delimiter for substitute");
    427400        return;
    428401    }
     
    436409        *cp++ = '\0';
    437410    else
    438         cp = "";
    439 
    440     while (*cp) switch (*cp++)
    441     {
     411        cp = (char*)"";
     412
     413    while (*cp) switch (*cp++) {
    442414        case 'g':
    443415            globalFlag = TRUE;
     
    449421
    450422        default:
    451             bb_error_msg("Unknown option for substitute");
    452 
     423            bb_error_msg("unknown option for substitute");
    453424            return;
    454425    }
    455426
    456     if (*oldStr == '\0')
    457     {
    458         if (searchString[0] == '\0')
    459         {
    460             bb_error_msg("No previous search string");
    461 
     427    if (*oldStr == '\0') {
     428        if (searchString[0] == '\0') {
     429            bb_error_msg("no previous search string");
    462430            return;
    463431        }
     
    480448    nlp = NULL;
    481449
    482     while (num1 <= num2)
    483     {
     450    while (num1 <= num2) {
    484451        offset = findString(lp, oldStr, oldLen, offset);
    485452
    486         if (offset < 0)
    487         {
    488             if (needPrint)
    489             {
     453        if (offset < 0) {
     454            if (needPrint) {
    490455                printLines(num1, num1, FALSE);
    491456                needPrint = FALSE;
     
    507472         * than the old string, then the substitution is easy.
    508473         */
    509         if (deltaLen <= 0)
    510         {
     474        if (deltaLen <= 0) {
    511475            memcpy(&lp->data[offset], newStr, newLen);
    512476
    513             if (deltaLen)
    514             {
     477            if (deltaLen) {
    515478                memcpy(&lp->data[offset + newLen],
    516479                    &lp->data[offset + oldLen],
     
    525488                continue;
    526489
    527             if (needPrint)
    528             {
     490            if (needPrint) {
    529491                printLines(num1, num1, FALSE);
    530492                needPrint = FALSE;
     
    544506        nlp = (LINE *) malloc(sizeof(LINE) + lp->len + deltaLen);
    545507
    546         if (nlp == NULL)
    547         {
    548             bb_error_msg("Cannot get memory for line");
    549 
     508        if (nlp == NULL) {
     509            bb_error_msg("cannot get memory for line");
    550510            return;
    551511        }
     
    577537            continue;
    578538
    579         if (needPrint)
    580         {
     539        if (needPrint) {
    581540            printLines(num1, num1, FALSE);
    582541            needPrint = FALSE;
     
    588547
    589548    if (!didSub)
    590         bb_error_msg("No substitutions found for \"%s\"", oldStr);
     549        bb_error_msg("no substitutions found for \"%s\"", oldStr);
    591550}
    592551
     
    604563    left = lp->len - offset;
    605564
    606     while (left >= len)
    607     {
     565    while (left >= len) {
    608566        ncp = memchr(cp, *str, left);
    609567
     
    637595static void addLines(int num)
    638596{
    639     int len;
    640     char    buf[USERSIZE + 1];
    641 
    642     while (fgets(buf, sizeof(buf), stdin))
    643     {
     597    int len;
     598    char buf[USERSIZE + 1];
     599
     600    while (fgets(buf, sizeof(buf), stdin)) {
    644601        if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0'))
    645602            return;
     
    650607            return;
    651608
    652         if (buf[len - 1] != '\n')
    653         {
    654             bb_error_msg("Line too long");
    655 
    656             do
    657             {
     609        if (buf[len - 1] != '\n') {
     610            bb_error_msg("line too long");
     611            do {
    658612                len = fgetc(stdin);
    659             }
    660             while ((len != EOF) && (len != '\n'));
    661 
     613            } while ((len != EOF) && (len != '\n'));
    662614            return;
    663615        }
     
    688640    sign = 1;
    689641
    690     while (TRUE)
    691     {
     642    while (TRUE) {
    692643        while (isblank(*cp))
    693644            cp++;
    694645
    695         switch (*cp)
    696         {
     646        switch (*cp) {
    697647            case '.':
    698648                haveNum = TRUE;
     
    710660                cp++;
    711661
    712                 if ((*cp < 'a') || (*cp > 'z'))
    713                 {
    714                     bb_error_msg("Bad mark name");
    715 
     662                if ((*cp < 'a') || (*cp > 'z')) {
     663                    bb_error_msg("bad mark name");
    716664                    return FALSE;
    717665                }
     
    725673                endStr = strchr(str, '/');
    726674
    727                 if (endStr)
    728                 {
     675                if (endStr) {
    729676                    *endStr++ = '\0';
    730677                    cp += (endStr - str);
     
    742689
    743690            default:
    744                 if (!isdigit(*cp))
    745                 {
     691                if (!isdigit(*cp)) {
    746692                    *retcp = cp;
    747693                    *retHaveNum = haveNum;
    748694                    *retNum = value;
    749 
    750695                    return TRUE;
    751696                }
     
    765710            cp++;
    766711
    767         switch (*cp)
    768         {
     712        switch (*cp) {
    769713            case '-':
    770714                sign = -1;
     
    781725                *retHaveNum = haveNum;
    782726                *retNum = value;
    783 
    784727                return TRUE;
    785728        }
     
    793736static int initEdit(void)
    794737{
    795     int i;
     738    int i;
    796739
    797740    bufSize = INITBUF_SIZE;
    798741    bufBase = malloc(bufSize);
    799742
    800     if (bufBase == NULL)
    801     {
    802         bb_error_msg("No memory for buffer");
    803 
     743    if (bufBase == NULL) {
     744        bb_error_msg("no memory for buffer");
    804745        return FALSE;
    805746    }
     
    860801static int readLines(const char * file, int num)
    861802{
    862     int fd, cc;
     803    int fd, cc;
    863804    int len, lineCount, charCount;
    864805    char *cp;
    865806
    866     if ((num < 1) || (num > lastNum + 1))
    867     {
    868         bb_error_msg("Bad line for read");
    869 
     807    if ((num < 1) || (num > lastNum + 1)) {
     808        bb_error_msg("bad line for read");
    870809        return FALSE;
    871810    }
     
    873812    fd = open(file, 0);
    874813
    875     if (fd < 0)
    876     {
     814    if (fd < 0) {
    877815        perror(file);
    878 
    879816        return FALSE;
    880817    }
     
    889826    fflush(stdout);
    890827
    891     do
    892     {
     828    do {
    893829        cp = memchr(bufPtr, '\n', bufUsed);
    894830
    895         if (cp)
    896         {
     831        if (cp) {
    897832            len = (cp - bufPtr) + 1;
    898833
    899             if (!insertLine(num, bufPtr, len))
    900             {
     834            if (!insertLine(num, bufPtr, len)) {
    901835                close(fd);
    902 
    903836                return FALSE;
    904837            }
     
    913846        }
    914847
    915         if (bufPtr != bufBase)
    916         {
     848        if (bufPtr != bufBase) {
    917849            memcpy(bufBase, bufPtr, bufUsed);
    918850            bufPtr = bufBase + bufUsed;
    919851        }
    920852
    921         if (bufUsed >= bufSize)
    922         {
     853        if (bufUsed >= bufSize) {
    923854            len = (bufSize * 3) / 2;
    924855            cp = realloc(bufBase, len);
    925856
    926             if (cp == NULL)
    927             {
    928                 bb_error_msg("No memory for buffer");
     857            if (cp == NULL) {
     858                bb_error_msg("no memory for buffer");
    929859                close(fd);
    930 
    931860                return FALSE;
    932861            }
     
    941870        bufPtr = bufBase;
    942871
    943     }
    944     while (cc > 0);
    945 
    946     if (cc < 0)
    947     {
     872    } while (cc > 0);
     873
     874    if (cc < 0) {
    948875        perror(file);
    949876        close(fd);
    950 
    951         return FALSE;
    952     }
    953 
    954     if (bufUsed)
    955     {
    956         if (!insertLine(num, bufPtr, bufUsed))
    957         {
     877        return FALSE;
     878    }
     879
     880    if (bufUsed) {
     881        if (!insertLine(num, bufPtr, bufUsed)) {
    958882            close(fd);
    959 
    960883            return -1;
    961884        }
     
    981904{
    982905    LINE *lp;
    983     int fd, lineCount, charCount;
    984 
    985     if ((num1 < 1) || (num2 > lastNum) || (num1 > num2))
    986     {
    987         bb_error_msg("Bad line range for write");
    988 
     906    int fd, lineCount, charCount;
     907
     908    if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
     909        bb_error_msg("bad line range for write");
    989910        return FALSE;
    990911    }
     
    997918    if (fd < 0) {
    998919        perror(file);
    999 
    1000920        return FALSE;
    1001921    }
     
    1006926    lp = findLine(num1);
    1007927
    1008     if (lp == NULL)
    1009     {
     928    if (lp == NULL) {
    1010929        close(fd);
    1011 
    1012         return FALSE;
    1013     }
    1014 
    1015     while (num1++ <= num2)
    1016     {
    1017         if (write(fd, lp->data, lp->len) != lp->len)
    1018         {
     930        return FALSE;
     931    }
     932
     933    while (num1++ <= num2) {
     934        if (write(fd, lp->data, lp->len) != lp->len) {
    1019935            perror(file);
    1020936            close(fd);
    1021 
    1022937            return FALSE;
    1023938        }
     
    1028943    }
    1029944
    1030     if (close(fd) < 0)
    1031     {
     945    if (close(fd) < 0) {
    1032946        perror(file);
    1033 
    1034947        return FALSE;
    1035948    }
    1036949
    1037950    printf("%d lines, %d chars\n", lineCount, charCount);
    1038 
    1039951    return TRUE;
    1040952}
     
    1053965    int ch, count;
    1054966
    1055     if ((num1 < 1) || (num2 > lastNum) || (num1 > num2))
    1056     {
    1057         bb_error_msg("Bad line range for print");
    1058 
     967    if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
     968        bb_error_msg("bad line range for print");
    1059969        return FALSE;
    1060970    }
     
    1065975        return FALSE;
    1066976
    1067     while (num1 <= num2)
    1068     {
    1069         if (!expandFlag)
    1070         {
     977    while (num1 <= num2) {
     978        if (!expandFlag) {
    1071979            write(1, lp->data, lp->len);
    1072980            setCurNum(num1++);
     
    1086994            count--;
    1087995
    1088         while (count-- > 0)
    1089         {
     996        while (count-- > 0) {
    1090997            ch = *cp++;
    1091998
    1092             if (ch & 0x80)
    1093             {
     999            if (ch & 0x80) {
    10941000                fputs("M-", stdout);
    10951001                ch &= 0x7f;
    10961002            }
    10971003
    1098             if (ch < ' ')
    1099             {
     1004            if (ch < ' ') {
    11001005                fputc('^', stdout);
    11011006                ch += '@';
    11021007            }
    11031008
    1104             if (ch == 0x7f)
    1105             {
     1009            if (ch == 0x7f) {
    11061010                fputc('^', stdout);
    11071011                ch = '?';
     
    11321036    LINE *newLp, *lp;
    11331037
    1134     if ((num < 1) || (num > lastNum + 1))
    1135     {
    1136         bb_error_msg("Inserting at bad line number");
    1137 
    1138         return FALSE;
    1139     }
    1140 
    1141     newLp = (LINE *) malloc(sizeof(LINE) + len - 1);
    1142 
    1143     if (newLp == NULL)
    1144     {
    1145         bb_error_msg("Failed to allocate memory for line");
    1146 
     1038    if ((num < 1) || (num > lastNum + 1)) {
     1039        bb_error_msg("inserting at bad line number");
     1040        return FALSE;
     1041    }
     1042
     1043    newLp = malloc(sizeof(LINE) + len - 1);
     1044
     1045    if (newLp == NULL) {
     1046        bb_error_msg("failed to allocate memory for line");
    11471047        return FALSE;
    11481048    }
     
    11531053    if (num > lastNum)
    11541054        lp = &lines;
    1155     else
    1156     {
     1055    else {
    11571056        lp = findLine(num);
    11581057
    1159         if (lp == NULL)
    1160         {
     1058        if (lp == NULL) {
    11611059            free((char *) newLp);
    1162 
    11631060            return FALSE;
    11641061        }
     
    11721069    lastNum++;
    11731070    dirty = TRUE;
    1174 
    11751071    return setCurNum(num);
    11761072}
     
    11851081    int count;
    11861082
    1187     if ((num1 < 1) || (num2 > lastNum) || (num1 > num2))
    1188     {
    1189         bb_error_msg("Bad line numbers for delete");
    1190 
     1083    if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
     1084        bb_error_msg("bad line numbers for delete");
    11911085        return FALSE;
    11921086    }
     
    11971091        return FALSE;
    11981092
    1199     if ((curNum >= num1) && (curNum <= num2))
    1200     {
     1093    if ((curNum >= num1) && (curNum <= num2)) {
    12011094        if (num2 < lastNum)
    12021095            setCurNum(num2 + 1);
     
    12141107    lastNum -= count;
    12151108
    1216     while (count-- > 0)
    1217     {
     1109    while (count-- > 0) {
    12181110        nlp = lp->next;
    12191111        plp = lp->prev;
     
    12451137    int len;
    12461138
    1247     if ((num1 < 1) || (num2 > lastNum) || (num1 > num2))
    1248     {
    1249         bb_error_msg("Bad line numbers for search");
    1250 
     1139    if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
     1140        bb_error_msg("bad line numbers for search");
    12511141        return 0;
    12521142    }
    12531143
    1254     if (*str == '\0')
    1255     {
    1256         if (searchString[0] == '\0')
    1257         {
    1258             bb_error_msg("No previous search string");
    1259 
     1144    if (*str == '\0') {
     1145        if (searchString[0] == '\0') {
     1146            bb_error_msg("no previous search string");
    12601147            return 0;
    12611148        }
     
    12741161        return 0;
    12751162
    1276     while (num1 <= num2)
    1277     {
     1163    while (num1 <= num2) {
    12781164        if (findString(lp, str, len, 0) >= 0)
    12791165            return num1;
     
    12831169    }
    12841170
    1285     bb_error_msg("Cannot find string \"%s\"", str);
    1286 
     1171    bb_error_msg("cannot find string \"%s\"", str);
    12871172    return 0;
    12881173}
     
    12971182    int lnum;
    12981183
    1299     if ((num < 1) || (num > lastNum))
    1300     {
    1301         bb_error_msg("Line number %d does not exist", num);
    1302 
     1184    if ((num < 1) || (num > lastNum)) {
     1185        bb_error_msg("line number %d does not exist", num);
    13031186        return NULL;
    13041187    }
    13051188
    1306     if (curNum <= 0)
    1307     {
     1189    if (curNum <= 0) {
    13081190        curNum = 1;
    13091191        curLine = lines.next;
     
    13161198    lnum = curNum;
    13171199
    1318     if (num < (curNum / 2))
    1319     {
     1200    if (num < (curNum / 2)) {
    13201201        lp = lines.next;
    13211202        lnum = 1;
    13221203    }
    1323     else if (num > ((curNum + lastNum) / 2))
    1324     {
     1204    else if (num > ((curNum + lastNum) / 2)) {
    13251205        lp = lines.prev;
    13261206        lnum = lastNum;
    13271207    }
    13281208
    1329     while (lnum < num)
    1330     {
     1209    while (lnum < num) {
    13311210        lp = lp->next;
    13321211        lnum++;
    13331212    }
    13341213
    1335     while (lnum > num)
    1336     {
     1214    while (lnum > num) {
    13371215        lp = lp->prev;
    13381216        lnum--;
    13391217    }
    1340 
    13411218    return lp;
    13421219}
     
    13581235    curNum = num;
    13591236    curLine = lp;
    1360 
    13611237    return TRUE;
    13621238}
Note: See TracChangeset for help on using the changeset viewer.