Changeset 1770 in MondoRescue for branches/stable/mindi-busybox/editors/ed.c
- Timestamp:
- Nov 6, 2007, 11:01:53 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mindi-busybox/editors/ed.c
r821 r1770 8 8 */ 9 9 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 14 enum { 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 22 20 typedef struct LINE { 23 21 struct LINE *next; … … 29 27 static LINE lines, *curLine; 30 28 static int curNum, lastNum, marks[26], dirty; 31 static char *bufBase, *bufPtr, *fileName , searchString[USERSIZE];29 static char *bufBase, *bufPtr, *fileName; 32 30 static int bufUsed, bufSize; 33 31 … … 49 47 static int findString(const LINE *lp, const char * str, int len, int offset); 50 48 49 int ed_main(int argc, char **argv); 51 50 int ed_main(int argc, char **argv) 52 51 { … … 58 57 59 58 if (fileName == NULL) { 60 bb_error_msg(" No memory");59 bb_error_msg("no memory"); 61 60 termEdit(); 62 61 return EXIT_SUCCESS; … … 89 88 int len, num1, num2, have1, have2; 90 89 91 while (TRUE) 92 { 90 while (TRUE) { 93 91 printf(": "); 94 92 fflush(stdout); … … 104 102 endbuf = &buf[len - 1]; 105 103 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 { 112 108 len = fgetc(stdin); 113 } 114 while ((len != EOF) && (len != '\n')); 109 } while ((len != EOF) && (len != '\n')); 115 110 116 111 continue; … … 130 125 have2 = FALSE; 131 126 132 if ((curNum == 0) && (lastNum > 0)) 133 { 127 if ((curNum == 0) && (lastNum > 0)) { 134 128 curNum = 1; 135 129 curLine = lines.next; … … 142 136 cp++; 143 137 144 if (*cp == ',') 145 { 138 if (*cp == ',') { 146 139 cp++; 147 140 … … 165 158 num2 = num1; 166 159 167 switch (*cp++) 168 { 160 switch (*cp++) { 169 161 case 'a': 170 162 addLines(num1 + 1); … … 181 173 182 174 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"); 186 177 break; 187 178 } … … 190 181 cp++; 191 182 192 if (*cp == '\0') 193 { 183 if (*cp == '\0') { 194 184 if (fileName) 195 185 printf("\"%s\"\n", fileName); 196 186 else 197 187 printf("No file name\n"); 198 199 188 break; 200 189 } … … 202 191 newname = strdup(cp); 203 192 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"); 207 195 break; 208 196 } … … 222 210 cp++; 223 211 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"); 227 214 break; 228 215 } … … 243 230 cp++; 244 231 245 if (have1 || *cp) 246 { 247 bb_error_msg("Bad quit command"); 232 if (have1 || *cp) { 233 bb_error_msg("bad quit command"); 248 234 break; 249 235 } … … 268 254 269 255 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"); 273 258 break; 274 259 } … … 277 262 cp++; 278 263 279 if (*cp == '\0') 280 { 281 bb_error_msg("No file name"); 264 if (*cp == '\0') { 265 bb_error_msg("no file name"); 282 266 break; 283 267 } … … 299 283 300 284 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"); 304 287 break; 305 288 } … … 316 299 cp = fileName; 317 300 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"); 321 303 break; 322 304 } … … 326 308 327 309 case 'z': 328 switch (*cp) 329 { 310 switch (*cp) { 330 311 case '-': 331 312 printLines(curNum-21, curNum, FALSE); … … 341 322 342 323 case '.': 343 if (have1) 344 { 345 bb_error_msg("No arguments allowed"); 324 if (have1) { 325 bb_error_msg("no arguments allowed"); 346 326 break; 347 327 } … … 361 341 362 342 case '\0': 363 if (have1) 364 { 343 if (have1) { 365 344 printLines(num2, num2, FALSE); 366 345 break; … … 373 352 374 353 default: 375 bb_error_msg(" Unimplemented command");354 bb_error_msg("unimplemented command"); 376 355 break; 377 356 } … … 387 366 { 388 367 char *cp, *oldStr, *newStr, buf[USERSIZE]; 389 int 368 int delim, oldLen, newLen, deltaLen, offset; 390 369 LINE *lp, *nlp; 391 370 int globalFlag, printFlag, didSub, needPrint; 392 371 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"); 397 374 return; 398 375 } … … 409 386 cp = buf; 410 387 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"); 415 390 return; 416 391 } … … 421 396 cp = strchr(cp, delim); 422 397 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"); 427 400 return; 428 401 } … … 436 409 *cp++ = '\0'; 437 410 else 438 cp = ""; 439 440 while (*cp) switch (*cp++) 441 { 411 cp = (char*)""; 412 413 while (*cp) switch (*cp++) { 442 414 case 'g': 443 415 globalFlag = TRUE; … … 449 421 450 422 default: 451 bb_error_msg("Unknown option for substitute"); 452 423 bb_error_msg("unknown option for substitute"); 453 424 return; 454 425 } 455 426 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"); 462 430 return; 463 431 } … … 480 448 nlp = NULL; 481 449 482 while (num1 <= num2) 483 { 450 while (num1 <= num2) { 484 451 offset = findString(lp, oldStr, oldLen, offset); 485 452 486 if (offset < 0) 487 { 488 if (needPrint) 489 { 453 if (offset < 0) { 454 if (needPrint) { 490 455 printLines(num1, num1, FALSE); 491 456 needPrint = FALSE; … … 507 472 * than the old string, then the substitution is easy. 508 473 */ 509 if (deltaLen <= 0) 510 { 474 if (deltaLen <= 0) { 511 475 memcpy(&lp->data[offset], newStr, newLen); 512 476 513 if (deltaLen) 514 { 477 if (deltaLen) { 515 478 memcpy(&lp->data[offset + newLen], 516 479 &lp->data[offset + oldLen], … … 525 488 continue; 526 489 527 if (needPrint) 528 { 490 if (needPrint) { 529 491 printLines(num1, num1, FALSE); 530 492 needPrint = FALSE; … … 544 506 nlp = (LINE *) malloc(sizeof(LINE) + lp->len + deltaLen); 545 507 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"); 550 510 return; 551 511 } … … 577 537 continue; 578 538 579 if (needPrint) 580 { 539 if (needPrint) { 581 540 printLines(num1, num1, FALSE); 582 541 needPrint = FALSE; … … 588 547 589 548 if (!didSub) 590 bb_error_msg(" No substitutions found for \"%s\"", oldStr);549 bb_error_msg("no substitutions found for \"%s\"", oldStr); 591 550 } 592 551 … … 604 563 left = lp->len - offset; 605 564 606 while (left >= len) 607 { 565 while (left >= len) { 608 566 ncp = memchr(cp, *str, left); 609 567 … … 637 595 static void addLines(int num) 638 596 { 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)) { 644 601 if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0')) 645 602 return; … … 650 607 return; 651 608 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 { 658 612 len = fgetc(stdin); 659 } 660 while ((len != EOF) && (len != '\n')); 661 613 } while ((len != EOF) && (len != '\n')); 662 614 return; 663 615 } … … 688 640 sign = 1; 689 641 690 while (TRUE) 691 { 642 while (TRUE) { 692 643 while (isblank(*cp)) 693 644 cp++; 694 645 695 switch (*cp) 696 { 646 switch (*cp) { 697 647 case '.': 698 648 haveNum = TRUE; … … 710 660 cp++; 711 661 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"); 716 664 return FALSE; 717 665 } … … 725 673 endStr = strchr(str, '/'); 726 674 727 if (endStr) 728 { 675 if (endStr) { 729 676 *endStr++ = '\0'; 730 677 cp += (endStr - str); … … 742 689 743 690 default: 744 if (!isdigit(*cp)) 745 { 691 if (!isdigit(*cp)) { 746 692 *retcp = cp; 747 693 *retHaveNum = haveNum; 748 694 *retNum = value; 749 750 695 return TRUE; 751 696 } … … 765 710 cp++; 766 711 767 switch (*cp) 768 { 712 switch (*cp) { 769 713 case '-': 770 714 sign = -1; … … 781 725 *retHaveNum = haveNum; 782 726 *retNum = value; 783 784 727 return TRUE; 785 728 } … … 793 736 static int initEdit(void) 794 737 { 795 int 738 int i; 796 739 797 740 bufSize = INITBUF_SIZE; 798 741 bufBase = malloc(bufSize); 799 742 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"); 804 745 return FALSE; 805 746 } … … 860 801 static int readLines(const char * file, int num) 861 802 { 862 int 803 int fd, cc; 863 804 int len, lineCount, charCount; 864 805 char *cp; 865 806 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"); 870 809 return FALSE; 871 810 } … … 873 812 fd = open(file, 0); 874 813 875 if (fd < 0) 876 { 814 if (fd < 0) { 877 815 perror(file); 878 879 816 return FALSE; 880 817 } … … 889 826 fflush(stdout); 890 827 891 do 892 { 828 do { 893 829 cp = memchr(bufPtr, '\n', bufUsed); 894 830 895 if (cp) 896 { 831 if (cp) { 897 832 len = (cp - bufPtr) + 1; 898 833 899 if (!insertLine(num, bufPtr, len)) 900 { 834 if (!insertLine(num, bufPtr, len)) { 901 835 close(fd); 902 903 836 return FALSE; 904 837 } … … 913 846 } 914 847 915 if (bufPtr != bufBase) 916 { 848 if (bufPtr != bufBase) { 917 849 memcpy(bufBase, bufPtr, bufUsed); 918 850 bufPtr = bufBase + bufUsed; 919 851 } 920 852 921 if (bufUsed >= bufSize) 922 { 853 if (bufUsed >= bufSize) { 923 854 len = (bufSize * 3) / 2; 924 855 cp = realloc(bufBase, len); 925 856 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"); 929 859 close(fd); 930 931 860 return FALSE; 932 861 } … … 941 870 bufPtr = bufBase; 942 871 943 } 944 while (cc > 0); 945 946 if (cc < 0) 947 { 872 } while (cc > 0); 873 874 if (cc < 0) { 948 875 perror(file); 949 876 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)) { 958 882 close(fd); 959 960 883 return -1; 961 884 } … … 981 904 { 982 905 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"); 989 910 return FALSE; 990 911 } … … 997 918 if (fd < 0) { 998 919 perror(file); 999 1000 920 return FALSE; 1001 921 } … … 1006 926 lp = findLine(num1); 1007 927 1008 if (lp == NULL) 1009 { 928 if (lp == NULL) { 1010 929 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) { 1019 935 perror(file); 1020 936 close(fd); 1021 1022 937 return FALSE; 1023 938 } … … 1028 943 } 1029 944 1030 if (close(fd) < 0) 1031 { 945 if (close(fd) < 0) { 1032 946 perror(file); 1033 1034 947 return FALSE; 1035 948 } 1036 949 1037 950 printf("%d lines, %d chars\n", lineCount, charCount); 1038 1039 951 return TRUE; 1040 952 } … … 1053 965 int ch, count; 1054 966 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"); 1059 969 return FALSE; 1060 970 } … … 1065 975 return FALSE; 1066 976 1067 while (num1 <= num2) 1068 { 1069 if (!expandFlag) 1070 { 977 while (num1 <= num2) { 978 if (!expandFlag) { 1071 979 write(1, lp->data, lp->len); 1072 980 setCurNum(num1++); … … 1086 994 count--; 1087 995 1088 while (count-- > 0) 1089 { 996 while (count-- > 0) { 1090 997 ch = *cp++; 1091 998 1092 if (ch & 0x80) 1093 { 999 if (ch & 0x80) { 1094 1000 fputs("M-", stdout); 1095 1001 ch &= 0x7f; 1096 1002 } 1097 1003 1098 if (ch < ' ') 1099 { 1004 if (ch < ' ') { 1100 1005 fputc('^', stdout); 1101 1006 ch += '@'; 1102 1007 } 1103 1008 1104 if (ch == 0x7f) 1105 { 1009 if (ch == 0x7f) { 1106 1010 fputc('^', stdout); 1107 1011 ch = '?'; … … 1132 1036 LINE *newLp, *lp; 1133 1037 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"); 1147 1047 return FALSE; 1148 1048 } … … 1153 1053 if (num > lastNum) 1154 1054 lp = &lines; 1155 else 1156 { 1055 else { 1157 1056 lp = findLine(num); 1158 1057 1159 if (lp == NULL) 1160 { 1058 if (lp == NULL) { 1161 1059 free((char *) newLp); 1162 1163 1060 return FALSE; 1164 1061 } … … 1172 1069 lastNum++; 1173 1070 dirty = TRUE; 1174 1175 1071 return setCurNum(num); 1176 1072 } … … 1185 1081 int count; 1186 1082 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"); 1191 1085 return FALSE; 1192 1086 } … … 1197 1091 return FALSE; 1198 1092 1199 if ((curNum >= num1) && (curNum <= num2)) 1200 { 1093 if ((curNum >= num1) && (curNum <= num2)) { 1201 1094 if (num2 < lastNum) 1202 1095 setCurNum(num2 + 1); … … 1214 1107 lastNum -= count; 1215 1108 1216 while (count-- > 0) 1217 { 1109 while (count-- > 0) { 1218 1110 nlp = lp->next; 1219 1111 plp = lp->prev; … … 1245 1137 int len; 1246 1138 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"); 1251 1141 return 0; 1252 1142 } 1253 1143 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"); 1260 1147 return 0; 1261 1148 } … … 1274 1161 return 0; 1275 1162 1276 while (num1 <= num2) 1277 { 1163 while (num1 <= num2) { 1278 1164 if (findString(lp, str, len, 0) >= 0) 1279 1165 return num1; … … 1283 1169 } 1284 1170 1285 bb_error_msg("Cannot find string \"%s\"", str); 1286 1171 bb_error_msg("cannot find string \"%s\"", str); 1287 1172 return 0; 1288 1173 } … … 1297 1182 int lnum; 1298 1183 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); 1303 1186 return NULL; 1304 1187 } 1305 1188 1306 if (curNum <= 0) 1307 { 1189 if (curNum <= 0) { 1308 1190 curNum = 1; 1309 1191 curLine = lines.next; … … 1316 1198 lnum = curNum; 1317 1199 1318 if (num < (curNum / 2)) 1319 { 1200 if (num < (curNum / 2)) { 1320 1201 lp = lines.next; 1321 1202 lnum = 1; 1322 1203 } 1323 else if (num > ((curNum + lastNum) / 2)) 1324 { 1204 else if (num > ((curNum + lastNum) / 2)) { 1325 1205 lp = lines.prev; 1326 1206 lnum = lastNum; 1327 1207 } 1328 1208 1329 while (lnum < num) 1330 { 1209 while (lnum < num) { 1331 1210 lp = lp->next; 1332 1211 lnum++; 1333 1212 } 1334 1213 1335 while (lnum > num) 1336 { 1214 while (lnum > num) { 1337 1215 lp = lp->prev; 1338 1216 lnum--; 1339 1217 } 1340 1341 1218 return lp; 1342 1219 } … … 1358 1235 curNum = num; 1359 1236 curLine = lp; 1360 1361 1237 return TRUE; 1362 1238 }
Note:
See TracChangeset
for help on using the changeset viewer.