aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-11-30 20:57:50 +0000
committervda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-11-30 20:57:50 +0000
commitadb2351deb037c5568c94d759dc2b5dc36ae9a1a (patch)
treeef0ade8ee097d7a9331f129072f27af3230cf7d3
parent8a5004a066106277023245975da281b2b4050ee1 (diff)
downloadbusybox-w32-adb2351deb037c5568c94d759dc2b5dc36ae9a1a.tar.gz
busybox-w32-adb2351deb037c5568c94d759dc2b5dc36ae9a1a.tar.bz2
busybox-w32-adb2351deb037c5568c94d759dc2b5dc36ae9a1a.zip
ed: convert style to bbox's. No code changes.
git-svn-id: svn://busybox.net/trunk/busybox@16741 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r--editors/ed.c317
1 files changed, 93 insertions, 224 deletions
diff --git a/editors/ed.c b/editors/ed.c
index 190c547f4..3aca75912 100644
--- a/editors/ed.c
+++ b/editors/ed.c
@@ -7,14 +7,6 @@
7 * The "ed" built-in command (much simplified) 7 * The "ed" built-in command (much simplified)
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" 10#include "busybox.h"
19 11
20#define USERSIZE 1024 /* max line length typed in by user */ 12#define USERSIZE 1024 /* max line length typed in by user */
@@ -88,8 +80,7 @@ static void doCommands(void)
88 char *endbuf, *newname, buf[USERSIZE]; 80 char *endbuf, *newname, buf[USERSIZE];
89 int len, num1, num2, have1, have2; 81 int len, num1, num2, have1, have2;
90 82
91 while (TRUE) 83 while (TRUE) {
92 {
93 printf(": "); 84 printf(": ");
94 fflush(stdout); 85 fflush(stdout);
95 86
@@ -103,15 +94,12 @@ static void doCommands(void)
103 94
104 endbuf = &buf[len - 1]; 95 endbuf = &buf[len - 1];
105 96
106 if (*endbuf != '\n') 97 if (*endbuf != '\n') {
107 {
108 bb_error_msg("command line too long"); 98 bb_error_msg("command line too long");
109 99
110 do 100 do {
111 {
112 len = fgetc(stdin); 101 len = fgetc(stdin);
113 } 102 } while ((len != EOF) && (len != '\n'));
114 while ((len != EOF) && (len != '\n'));
115 103
116 continue; 104 continue;
117 } 105 }
@@ -129,8 +117,7 @@ static void doCommands(void)
129 have1 = FALSE; 117 have1 = FALSE;
130 have2 = FALSE; 118 have2 = FALSE;
131 119
132 if ((curNum == 0) && (lastNum > 0)) 120 if ((curNum == 0) && (lastNum > 0)) {
133 {
134 curNum = 1; 121 curNum = 1;
135 curLine = lines.next; 122 curLine = lines.next;
136 } 123 }
@@ -141,8 +128,7 @@ static void doCommands(void)
141 while (isblank(*cp)) 128 while (isblank(*cp))
142 cp++; 129 cp++;
143 130
144 if (*cp == ',') 131 if (*cp == ',') {
145 {
146 cp++; 132 cp++;
147 133
148 if (!getNum(&cp, &have2, &num2)) 134 if (!getNum(&cp, &have2, &num2))
@@ -164,8 +150,7 @@ static void doCommands(void)
164 if (!have2) 150 if (!have2)
165 num2 = num1; 151 num2 = num1;
166 152
167 switch (*cp++) 153 switch (*cp++) {
168 {
169 case 'a': 154 case 'a':
170 addLines(num1 + 1); 155 addLines(num1 + 1);
171 break; 156 break;
@@ -180,8 +165,7 @@ static void doCommands(void)
180 break; 165 break;
181 166
182 case 'f': 167 case 'f':
183 if (*cp && !isblank(*cp)) 168 if (*cp && !isblank(*cp)) {
184 {
185 bb_error_msg("bad file command"); 169 bb_error_msg("bad file command");
186 break; 170 break;
187 } 171 }
@@ -189,20 +173,17 @@ static void doCommands(void)
189 while (isblank(*cp)) 173 while (isblank(*cp))
190 cp++; 174 cp++;
191 175
192 if (*cp == '\0') 176 if (*cp == '\0') {
193 {
194 if (fileName) 177 if (fileName)
195 printf("\"%s\"\n", fileName); 178 printf("\"%s\"\n", fileName);
196 else 179 else
197 printf("No file name\n"); 180 printf("No file name\n");
198
199 break; 181 break;
200 } 182 }
201 183
202 newname = strdup(cp); 184 newname = strdup(cp);
203 185
204 if (newname == NULL) 186 if (newname == NULL) {
205 {
206 bb_error_msg("no memory for file name"); 187 bb_error_msg("no memory for file name");
207 break; 188 break;
208 } 189 }
@@ -221,8 +202,7 @@ static void doCommands(void)
221 while (isblank(*cp)) 202 while (isblank(*cp))
222 cp++; 203 cp++;
223 204
224 if ((*cp < 'a') || (*cp > 'a') || cp[1]) 205 if ((*cp < 'a') || (*cp > 'a') || cp[1]) {
225 {
226 bb_error_msg("bad mark name"); 206 bb_error_msg("bad mark name");
227 break; 207 break;
228 } 208 }
@@ -242,8 +222,7 @@ static void doCommands(void)
242 while (isblank(*cp)) 222 while (isblank(*cp))
243 cp++; 223 cp++;
244 224
245 if (have1 || *cp) 225 if (have1 || *cp) {
246 {
247 bb_error_msg("bad quit command"); 226 bb_error_msg("bad quit command");
248 break; 227 break;
249 } 228 }
@@ -267,8 +246,7 @@ static void doCommands(void)
267 break; 246 break;
268 247
269 case 'r': 248 case 'r':
270 if (*cp && !isblank(*cp)) 249 if (*cp && !isblank(*cp)) {
271 {
272 bb_error_msg("bad read command"); 250 bb_error_msg("bad read command");
273 break; 251 break;
274 } 252 }
@@ -276,8 +254,7 @@ static void doCommands(void)
276 while (isblank(*cp)) 254 while (isblank(*cp))
277 cp++; 255 cp++;
278 256
279 if (*cp == '\0') 257 if (*cp == '\0') {
280 {
281 bb_error_msg("no file name"); 258 bb_error_msg("no file name");
282 break; 259 break;
283 } 260 }
@@ -298,8 +275,7 @@ static void doCommands(void)
298 break; 275 break;
299 276
300 case 'w': 277 case 'w':
301 if (*cp && !isblank(*cp)) 278 if (*cp && !isblank(*cp)) {
302 {
303 bb_error_msg("bad write command"); 279 bb_error_msg("bad write command");
304 break; 280 break;
305 } 281 }
@@ -315,8 +291,7 @@ static void doCommands(void)
315 if (*cp == '\0') 291 if (*cp == '\0')
316 cp = fileName; 292 cp = fileName;
317 293
318 if (cp == NULL) 294 if (cp == NULL) {
319 {
320 bb_error_msg("no file name specified"); 295 bb_error_msg("no file name specified");
321 break; 296 break;
322 } 297 }
@@ -325,8 +300,7 @@ static void doCommands(void)
325 break; 300 break;
326 301
327 case 'z': 302 case 'z':
328 switch (*cp) 303 switch (*cp) {
329 {
330 case '-': 304 case '-':
331 printLines(curNum-21, curNum, FALSE); 305 printLines(curNum-21, curNum, FALSE);
332 break; 306 break;
@@ -340,8 +314,7 @@ static void doCommands(void)
340 break; 314 break;
341 315
342 case '.': 316 case '.':
343 if (have1) 317 if (have1) {
344 {
345 bb_error_msg("no arguments allowed"); 318 bb_error_msg("no arguments allowed");
346 break; 319 break;
347 } 320 }
@@ -360,8 +333,7 @@ static void doCommands(void)
360 break; 333 break;
361 334
362 case '\0': 335 case '\0':
363 if (have1) 336 if (have1) {
364 {
365 printLines(num2, num2, FALSE); 337 printLines(num2, num2, FALSE);
366 break; 338 break;
367 } 339 }
@@ -386,14 +358,12 @@ static void doCommands(void)
386static void subCommand(const char * cmd, int num1, int num2) 358static void subCommand(const char * cmd, int num1, int num2)
387{ 359{
388 char *cp, *oldStr, *newStr, buf[USERSIZE]; 360 char *cp, *oldStr, *newStr, buf[USERSIZE];
389 int delim, oldLen, newLen, deltaLen, offset; 361 int delim, oldLen, newLen, deltaLen, offset;
390 LINE *lp, *nlp; 362 LINE *lp, *nlp;
391 int globalFlag, printFlag, didSub, needPrint; 363 int globalFlag, printFlag, didSub, needPrint;
392 364
393 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) 365 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
394 {
395 bb_error_msg("bad line range for substitute"); 366 bb_error_msg("bad line range for substitute");
396
397 return; 367 return;
398 } 368 }
399 369
@@ -408,10 +378,8 @@ static void subCommand(const char * cmd, int num1, int num2)
408 strcpy(buf, cmd); 378 strcpy(buf, cmd);
409 cp = buf; 379 cp = buf;
410 380
411 if (isblank(*cp) || (*cp == '\0')) 381 if (isblank(*cp) || (*cp == '\0')) {
412 {
413 bb_error_msg("bad delimiter for substitute"); 382 bb_error_msg("bad delimiter for substitute");
414
415 return; 383 return;
416 } 384 }
417 385
@@ -420,10 +388,8 @@ static void subCommand(const char * cmd, int num1, int num2)
420 388
421 cp = strchr(cp, delim); 389 cp = strchr(cp, delim);
422 390
423 if (cp == NULL) 391 if (cp == NULL) {
424 {
425 bb_error_msg("missing 2nd delimiter for substitute"); 392 bb_error_msg("missing 2nd delimiter for substitute");
426
427 return; 393 return;
428 } 394 }
429 395
@@ -437,8 +403,7 @@ static void subCommand(const char * cmd, int num1, int num2)
437 else 403 else
438 cp = ""; 404 cp = "";
439 405
440 while (*cp) switch (*cp++) 406 while (*cp) switch (*cp++) {
441 {
442 case 'g': 407 case 'g':
443 globalFlag = TRUE; 408 globalFlag = TRUE;
444 break; 409 break;
@@ -449,16 +414,12 @@ static void subCommand(const char * cmd, int num1, int num2)
449 414
450 default: 415 default:
451 bb_error_msg("unknown option for substitute"); 416 bb_error_msg("unknown option for substitute");
452
453 return; 417 return;
454 } 418 }
455 419
456 if (*oldStr == '\0') 420 if (*oldStr == '\0') {
457 { 421 if (searchString[0] == '\0') {
458 if (searchString[0] == '\0')
459 {
460 bb_error_msg("no previous search string"); 422 bb_error_msg("no previous search string");
461
462 return; 423 return;
463 } 424 }
464 425
@@ -479,14 +440,11 @@ static void subCommand(const char * cmd, int num1, int num2)
479 offset = 0; 440 offset = 0;
480 nlp = NULL; 441 nlp = NULL;
481 442
482 while (num1 <= num2) 443 while (num1 <= num2) {
483 {
484 offset = findString(lp, oldStr, oldLen, offset); 444 offset = findString(lp, oldStr, oldLen, offset);
485 445
486 if (offset < 0) 446 if (offset < 0) {
487 { 447 if (needPrint) {
488 if (needPrint)
489 {
490 printLines(num1, num1, FALSE); 448 printLines(num1, num1, FALSE);
491 needPrint = FALSE; 449 needPrint = FALSE;
492 } 450 }
@@ -506,12 +464,10 @@ static void subCommand(const char * cmd, int num1, int num2)
506 * If the replacement string is the same size or shorter 464 * If the replacement string is the same size or shorter
507 * than the old string, then the substitution is easy. 465 * than the old string, then the substitution is easy.
508 */ 466 */
509 if (deltaLen <= 0) 467 if (deltaLen <= 0) {
510 {
511 memcpy(&lp->data[offset], newStr, newLen); 468 memcpy(&lp->data[offset], newStr, newLen);
512 469
513 if (deltaLen) 470 if (deltaLen) {
514 {
515 memcpy(&lp->data[offset + newLen], 471 memcpy(&lp->data[offset + newLen],
516 &lp->data[offset + oldLen], 472 &lp->data[offset + oldLen],
517 lp->len - offset - oldLen); 473 lp->len - offset - oldLen);
@@ -524,8 +480,7 @@ static void subCommand(const char * cmd, int num1, int num2)
524 if (globalFlag) 480 if (globalFlag)
525 continue; 481 continue;
526 482
527 if (needPrint) 483 if (needPrint) {
528 {
529 printLines(num1, num1, FALSE); 484 printLines(num1, num1, FALSE);
530 needPrint = FALSE; 485 needPrint = FALSE;
531 } 486 }
@@ -543,10 +498,8 @@ static void subCommand(const char * cmd, int num1, int num2)
543 */ 498 */
544 nlp = (LINE *) malloc(sizeof(LINE) + lp->len + deltaLen); 499 nlp = (LINE *) malloc(sizeof(LINE) + lp->len + deltaLen);
545 500
546 if (nlp == NULL) 501 if (nlp == NULL) {
547 {
548 bb_error_msg("cannot get memory for line"); 502 bb_error_msg("cannot get memory for line");
549
550 return; 503 return;
551 } 504 }
552 505
@@ -576,8 +529,7 @@ static void subCommand(const char * cmd, int num1, int num2)
576 if (globalFlag) 529 if (globalFlag)
577 continue; 530 continue;
578 531
579 if (needPrint) 532 if (needPrint) {
580 {
581 printLines(num1, num1, FALSE); 533 printLines(num1, num1, FALSE);
582 needPrint = FALSE; 534 needPrint = FALSE;
583 } 535 }
@@ -603,8 +555,7 @@ static int findString( const LINE * lp, const char * str, int len, int offset)
603 cp = &lp->data[offset]; 555 cp = &lp->data[offset];
604 left = lp->len - offset; 556 left = lp->len - offset;
605 557
606 while (left >= len) 558 while (left >= len) {
607 {
608 ncp = memchr(cp, *str, left); 559 ncp = memchr(cp, *str, left);
609 560
610 if (ncp == NULL) 561 if (ncp == NULL)
@@ -636,11 +587,10 @@ static int findString( const LINE * lp, const char * str, int len, int offset)
636 */ 587 */
637static void addLines(int num) 588static void addLines(int num)
638{ 589{
639 int len; 590 int len;
640 char buf[USERSIZE + 1]; 591 char buf[USERSIZE + 1];
641 592
642 while (fgets(buf, sizeof(buf), stdin)) 593 while (fgets(buf, sizeof(buf), stdin)) {
643 {
644 if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0')) 594 if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0'))
645 return; 595 return;
646 596
@@ -649,16 +599,11 @@ static void addLines(int num)
649 if (len == 0) 599 if (len == 0)
650 return; 600 return;
651 601
652 if (buf[len - 1] != '\n') 602 if (buf[len - 1] != '\n') {
653 {
654 bb_error_msg("line too long"); 603 bb_error_msg("line too long");
655 604 do {
656 do
657 {
658 len = fgetc(stdin); 605 len = fgetc(stdin);
659 } 606 } while ((len != EOF) && (len != '\n'));
660 while ((len != EOF) && (len != '\n'));
661
662 return; 607 return;
663 } 608 }
664 609
@@ -687,13 +632,11 @@ static int getNum(const char **retcp, int *retHaveNum, int *retNum)
687 value = 0; 632 value = 0;
688 sign = 1; 633 sign = 1;
689 634
690 while (TRUE) 635 while (TRUE) {
691 {
692 while (isblank(*cp)) 636 while (isblank(*cp))
693 cp++; 637 cp++;
694 638
695 switch (*cp) 639 switch (*cp) {
696 {
697 case '.': 640 case '.':
698 haveNum = TRUE; 641 haveNum = TRUE;
699 num = curNum; 642 num = curNum;
@@ -709,10 +652,8 @@ static int getNum(const char **retcp, int *retHaveNum, int *retNum)
709 case '\'': 652 case '\'':
710 cp++; 653 cp++;
711 654
712 if ((*cp < 'a') || (*cp > 'z')) 655 if ((*cp < 'a') || (*cp > 'z')) {
713 {
714 bb_error_msg("bad mark name"); 656 bb_error_msg("bad mark name");
715
716 return FALSE; 657 return FALSE;
717 } 658 }
718 659
@@ -724,8 +665,7 @@ static int getNum(const char **retcp, int *retHaveNum, int *retNum)
724 strcpy(str, ++cp); 665 strcpy(str, ++cp);
725 endStr = strchr(str, '/'); 666 endStr = strchr(str, '/');
726 667
727 if (endStr) 668 if (endStr) {
728 {
729 *endStr++ = '\0'; 669 *endStr++ = '\0';
730 cp += (endStr - str); 670 cp += (endStr - str);
731 } 671 }
@@ -741,12 +681,10 @@ static int getNum(const char **retcp, int *retHaveNum, int *retNum)
741 break; 681 break;
742 682
743 default: 683 default:
744 if (!isdigit(*cp)) 684 if (!isdigit(*cp)) {
745 {
746 *retcp = cp; 685 *retcp = cp;
747 *retHaveNum = haveNum; 686 *retHaveNum = haveNum;
748 *retNum = value; 687 *retNum = value;
749
750 return TRUE; 688 return TRUE;
751 } 689 }
752 690
@@ -764,8 +702,7 @@ static int getNum(const char **retcp, int *retHaveNum, int *retNum)
764 while (isblank(*cp)) 702 while (isblank(*cp))
765 cp++; 703 cp++;
766 704
767 switch (*cp) 705 switch (*cp) {
768 {
769 case '-': 706 case '-':
770 sign = -1; 707 sign = -1;
771 cp++; 708 cp++;
@@ -780,7 +717,6 @@ static int getNum(const char **retcp, int *retHaveNum, int *retNum)
780 *retcp = cp; 717 *retcp = cp;
781 *retHaveNum = haveNum; 718 *retHaveNum = haveNum;
782 *retNum = value; 719 *retNum = value;
783
784 return TRUE; 720 return TRUE;
785 } 721 }
786 } 722 }
@@ -792,15 +728,13 @@ static int getNum(const char **retcp, int *retHaveNum, int *retNum)
792 */ 728 */
793static int initEdit(void) 729static int initEdit(void)
794{ 730{
795 int i; 731 int i;
796 732
797 bufSize = INITBUF_SIZE; 733 bufSize = INITBUF_SIZE;
798 bufBase = malloc(bufSize); 734 bufBase = malloc(bufSize);
799 735
800 if (bufBase == NULL) 736 if (bufBase == NULL) {
801 {
802 bb_error_msg("no memory for buffer"); 737 bb_error_msg("no memory for buffer");
803
804 return FALSE; 738 return FALSE;
805 } 739 }
806 740
@@ -859,23 +793,19 @@ static void termEdit(void)
859 */ 793 */
860static int readLines(const char * file, int num) 794static int readLines(const char * file, int num)
861{ 795{
862 int fd, cc; 796 int fd, cc;
863 int len, lineCount, charCount; 797 int len, lineCount, charCount;
864 char *cp; 798 char *cp;
865 799
866 if ((num < 1) || (num > lastNum + 1)) 800 if ((num < 1) || (num > lastNum + 1)) {
867 {
868 bb_error_msg("bad line for read"); 801 bb_error_msg("bad line for read");
869
870 return FALSE; 802 return FALSE;
871 } 803 }
872 804
873 fd = open(file, 0); 805 fd = open(file, 0);
874 806
875 if (fd < 0) 807 if (fd < 0) {
876 {
877 perror(file); 808 perror(file);
878
879 return FALSE; 809 return FALSE;
880 } 810 }
881 811
@@ -888,18 +818,14 @@ static int readLines(const char * file, int num)
888 printf("\"%s\", ", file); 818 printf("\"%s\", ", file);
889 fflush(stdout); 819 fflush(stdout);
890 820
891 do 821 do {
892 {
893 cp = memchr(bufPtr, '\n', bufUsed); 822 cp = memchr(bufPtr, '\n', bufUsed);
894 823
895 if (cp) 824 if (cp) {
896 {
897 len = (cp - bufPtr) + 1; 825 len = (cp - bufPtr) + 1;
898 826
899 if (!insertLine(num, bufPtr, len)) 827 if (!insertLine(num, bufPtr, len)) {
900 {
901 close(fd); 828 close(fd);
902
903 return FALSE; 829 return FALSE;
904 } 830 }
905 831
@@ -912,22 +838,18 @@ static int readLines(const char * file, int num)
912 continue; 838 continue;
913 } 839 }
914 840
915 if (bufPtr != bufBase) 841 if (bufPtr != bufBase) {
916 {
917 memcpy(bufBase, bufPtr, bufUsed); 842 memcpy(bufBase, bufPtr, bufUsed);
918 bufPtr = bufBase + bufUsed; 843 bufPtr = bufBase + bufUsed;
919 } 844 }
920 845
921 if (bufUsed >= bufSize) 846 if (bufUsed >= bufSize) {
922 {
923 len = (bufSize * 3) / 2; 847 len = (bufSize * 3) / 2;
924 cp = realloc(bufBase, len); 848 cp = realloc(bufBase, len);
925 849
926 if (cp == NULL) 850 if (cp == NULL) {
927 {
928 bb_error_msg("no memory for buffer"); 851 bb_error_msg("no memory for buffer");
929 close(fd); 852 close(fd);
930
931 return FALSE; 853 return FALSE;
932 } 854 }
933 855
@@ -940,23 +862,17 @@ static int readLines(const char * file, int num)
940 bufUsed += cc; 862 bufUsed += cc;
941 bufPtr = bufBase; 863 bufPtr = bufBase;
942 864
943 } 865 } while (cc > 0);
944 while (cc > 0);
945 866
946 if (cc < 0) 867 if (cc < 0) {
947 {
948 perror(file); 868 perror(file);
949 close(fd); 869 close(fd);
950
951 return FALSE; 870 return FALSE;
952 } 871 }
953 872
954 if (bufUsed) 873 if (bufUsed) {
955 { 874 if (!insertLine(num, bufPtr, bufUsed)) {
956 if (!insertLine(num, bufPtr, bufUsed))
957 {
958 close(fd); 875 close(fd);
959
960 return -1; 876 return -1;
961 } 877 }
962 878
@@ -980,12 +896,10 @@ static int readLines(const char * file, int num)
980static int writeLines(const char * file, int num1, int num2) 896static int writeLines(const char * file, int num1, int num2)
981{ 897{
982 LINE *lp; 898 LINE *lp;
983 int fd, lineCount, charCount; 899 int fd, lineCount, charCount;
984 900
985 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) 901 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
986 {
987 bb_error_msg("bad line range for write"); 902 bb_error_msg("bad line range for write");
988
989 return FALSE; 903 return FALSE;
990 } 904 }
991 905
@@ -996,7 +910,6 @@ static int writeLines(const char * file, int num1, int num2)
996 910
997 if (fd < 0) { 911 if (fd < 0) {
998 perror(file); 912 perror(file);
999
1000 return FALSE; 913 return FALSE;
1001 } 914 }
1002 915
@@ -1005,20 +918,15 @@ static int writeLines(const char * file, int num1, int num2)
1005 918
1006 lp = findLine(num1); 919 lp = findLine(num1);
1007 920
1008 if (lp == NULL) 921 if (lp == NULL) {
1009 {
1010 close(fd); 922 close(fd);
1011
1012 return FALSE; 923 return FALSE;
1013 } 924 }
1014 925
1015 while (num1++ <= num2) 926 while (num1++ <= num2) {
1016 { 927 if (write(fd, lp->data, lp->len) != lp->len) {
1017 if (write(fd, lp->data, lp->len) != lp->len)
1018 {
1019 perror(file); 928 perror(file);
1020 close(fd); 929 close(fd);
1021
1022 return FALSE; 930 return FALSE;
1023 } 931 }
1024 932
@@ -1027,15 +935,12 @@ static int writeLines(const char * file, int num1, int num2)
1027 lp = lp->next; 935 lp = lp->next;
1028 } 936 }
1029 937
1030 if (close(fd) < 0) 938 if (close(fd) < 0) {
1031 {
1032 perror(file); 939 perror(file);
1033
1034 return FALSE; 940 return FALSE;
1035 } 941 }
1036 942
1037 printf("%d lines, %d chars\n", lineCount, charCount); 943 printf("%d lines, %d chars\n", lineCount, charCount);
1038
1039 return TRUE; 944 return TRUE;
1040} 945}
1041 946
@@ -1052,10 +957,8 @@ static int printLines(int num1, int num2, int expandFlag)
1052 const char *cp; 957 const char *cp;
1053 int ch, count; 958 int ch, count;
1054 959
1055 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) 960 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
1056 {
1057 bb_error_msg("bad line range for print"); 961 bb_error_msg("bad line range for print");
1058
1059 return FALSE; 962 return FALSE;
1060 } 963 }
1061 964
@@ -1064,10 +967,8 @@ static int printLines(int num1, int num2, int expandFlag)
1064 if (lp == NULL) 967 if (lp == NULL)
1065 return FALSE; 968 return FALSE;
1066 969
1067 while (num1 <= num2) 970 while (num1 <= num2) {
1068 { 971 if (!expandFlag) {
1069 if (!expandFlag)
1070 {
1071 write(1, lp->data, lp->len); 972 write(1, lp->data, lp->len);
1072 setCurNum(num1++); 973 setCurNum(num1++);
1073 lp = lp->next; 974 lp = lp->next;
@@ -1085,24 +986,20 @@ static int printLines(int num1, int num2, int expandFlag)
1085 if ((count > 0) && (cp[count - 1] == '\n')) 986 if ((count > 0) && (cp[count - 1] == '\n'))
1086 count--; 987 count--;
1087 988
1088 while (count-- > 0) 989 while (count-- > 0) {
1089 {
1090 ch = *cp++; 990 ch = *cp++;
1091 991
1092 if (ch & 0x80) 992 if (ch & 0x80) {
1093 {
1094 fputs("M-", stdout); 993 fputs("M-", stdout);
1095 ch &= 0x7f; 994 ch &= 0x7f;
1096 } 995 }
1097 996
1098 if (ch < ' ') 997 if (ch < ' ') {
1099 {
1100 fputc('^', stdout); 998 fputc('^', stdout);
1101 ch += '@'; 999 ch += '@';
1102 } 1000 }
1103 1001
1104 if (ch == 0x7f) 1002 if (ch == 0x7f) {
1105 {
1106 fputc('^', stdout); 1003 fputc('^', stdout);
1107 ch = '?'; 1004 ch = '?';
1108 } 1005 }
@@ -1131,19 +1028,15 @@ static int insertLine(int num, const char * data, int len)
1131{ 1028{
1132 LINE *newLp, *lp; 1029 LINE *newLp, *lp;
1133 1030
1134 if ((num < 1) || (num > lastNum + 1)) 1031 if ((num < 1) || (num > lastNum + 1)) {
1135 {
1136 bb_error_msg("inserting at bad line number"); 1032 bb_error_msg("inserting at bad line number");
1137
1138 return FALSE; 1033 return FALSE;
1139 } 1034 }
1140 1035
1141 newLp = (LINE *) malloc(sizeof(LINE) + len - 1); 1036 newLp = malloc(sizeof(LINE) + len - 1);
1142 1037
1143 if (newLp == NULL) 1038 if (newLp == NULL) {
1144 {
1145 bb_error_msg("failed to allocate memory for line"); 1039 bb_error_msg("failed to allocate memory for line");
1146
1147 return FALSE; 1040 return FALSE;
1148 } 1041 }
1149 1042
@@ -1152,14 +1045,11 @@ static int insertLine(int num, const char * data, int len)
1152 1045
1153 if (num > lastNum) 1046 if (num > lastNum)
1154 lp = &lines; 1047 lp = &lines;
1155 else 1048 else {
1156 {
1157 lp = findLine(num); 1049 lp = findLine(num);
1158 1050
1159 if (lp == NULL) 1051 if (lp == NULL) {
1160 {
1161 free((char *) newLp); 1052 free((char *) newLp);
1162
1163 return FALSE; 1053 return FALSE;
1164 } 1054 }
1165 } 1055 }
@@ -1171,7 +1061,6 @@ static int insertLine(int num, const char * data, int len)
1171 1061
1172 lastNum++; 1062 lastNum++;
1173 dirty = TRUE; 1063 dirty = TRUE;
1174
1175 return setCurNum(num); 1064 return setCurNum(num);
1176} 1065}
1177 1066
@@ -1184,10 +1073,8 @@ static int deleteLines(int num1, int num2)
1184 LINE *lp, *nlp, *plp; 1073 LINE *lp, *nlp, *plp;
1185 int count; 1074 int count;
1186 1075
1187 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) 1076 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
1188 {
1189 bb_error_msg("bad line numbers for delete"); 1077 bb_error_msg("bad line numbers for delete");
1190
1191 return FALSE; 1078 return FALSE;
1192 } 1079 }
1193 1080
@@ -1196,8 +1083,7 @@ static int deleteLines(int num1, int num2)
1196 if (lp == NULL) 1083 if (lp == NULL)
1197 return FALSE; 1084 return FALSE;
1198 1085
1199 if ((curNum >= num1) && (curNum <= num2)) 1086 if ((curNum >= num1) && (curNum <= num2)) {
1200 {
1201 if (num2 < lastNum) 1087 if (num2 < lastNum)
1202 setCurNum(num2 + 1); 1088 setCurNum(num2 + 1);
1203 else if (num1 > 1) 1089 else if (num1 > 1)
@@ -1213,8 +1099,7 @@ static int deleteLines(int num1, int num2)
1213 1099
1214 lastNum -= count; 1100 lastNum -= count;
1215 1101
1216 while (count-- > 0) 1102 while (count-- > 0) {
1217 {
1218 nlp = lp->next; 1103 nlp = lp->next;
1219 plp = lp->prev; 1104 plp = lp->prev;
1220 plp->next = nlp; 1105 plp->next = nlp;
@@ -1244,19 +1129,14 @@ static int searchLines(const char *str, int num1, int num2)
1244 const LINE *lp; 1129 const LINE *lp;
1245 int len; 1130 int len;
1246 1131
1247 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) 1132 if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
1248 {
1249 bb_error_msg("bad line numbers for search"); 1133 bb_error_msg("bad line numbers for search");
1250
1251 return 0; 1134 return 0;
1252 } 1135 }
1253 1136
1254 if (*str == '\0') 1137 if (*str == '\0') {
1255 { 1138 if (searchString[0] == '\0') {
1256 if (searchString[0] == '\0')
1257 {
1258 bb_error_msg("no previous search string"); 1139 bb_error_msg("no previous search string");
1259
1260 return 0; 1140 return 0;
1261 } 1141 }
1262 1142
@@ -1273,8 +1153,7 @@ static int searchLines(const char *str, int num1, int num2)
1273 if (lp == NULL) 1153 if (lp == NULL)
1274 return 0; 1154 return 0;
1275 1155
1276 while (num1 <= num2) 1156 while (num1 <= num2) {
1277 {
1278 if (findString(lp, str, len, 0) >= 0) 1157 if (findString(lp, str, len, 0) >= 0)
1279 return num1; 1158 return num1;
1280 1159
@@ -1283,7 +1162,6 @@ static int searchLines(const char *str, int num1, int num2)
1283 } 1162 }
1284 1163
1285 bb_error_msg("cannot find string \"%s\"", str); 1164 bb_error_msg("cannot find string \"%s\"", str);
1286
1287 return 0; 1165 return 0;
1288} 1166}
1289 1167
@@ -1296,15 +1174,12 @@ static LINE *findLine(int num)
1296 LINE *lp; 1174 LINE *lp;
1297 int lnum; 1175 int lnum;
1298 1176
1299 if ((num < 1) || (num > lastNum)) 1177 if ((num < 1) || (num > lastNum)) {
1300 {
1301 bb_error_msg("line number %d does not exist", num); 1178 bb_error_msg("line number %d does not exist", num);
1302
1303 return NULL; 1179 return NULL;
1304 } 1180 }
1305 1181
1306 if (curNum <= 0) 1182 if (curNum <= 0) {
1307 {
1308 curNum = 1; 1183 curNum = 1;
1309 curLine = lines.next; 1184 curLine = lines.next;
1310 } 1185 }
@@ -1315,29 +1190,24 @@ static LINE *findLine(int num)
1315 lp = curLine; 1190 lp = curLine;
1316 lnum = curNum; 1191 lnum = curNum;
1317 1192
1318 if (num < (curNum / 2)) 1193 if (num < (curNum / 2)) {
1319 {
1320 lp = lines.next; 1194 lp = lines.next;
1321 lnum = 1; 1195 lnum = 1;
1322 } 1196 }
1323 else if (num > ((curNum + lastNum) / 2)) 1197 else if (num > ((curNum + lastNum) / 2)) {
1324 {
1325 lp = lines.prev; 1198 lp = lines.prev;
1326 lnum = lastNum; 1199 lnum = lastNum;
1327 } 1200 }
1328 1201
1329 while (lnum < num) 1202 while (lnum < num) {
1330 {
1331 lp = lp->next; 1203 lp = lp->next;
1332 lnum++; 1204 lnum++;
1333 } 1205 }
1334 1206
1335 while (lnum > num) 1207 while (lnum > num) {
1336 {
1337 lp = lp->prev; 1208 lp = lp->prev;
1338 lnum--; 1209 lnum--;
1339 } 1210 }
1340
1341 return lp; 1211 return lp;
1342} 1212}
1343 1213
@@ -1357,6 +1227,5 @@ static int setCurNum(int num)
1357 1227
1358 curNum = num; 1228 curNum = num;
1359 curLine = lp; 1229 curLine = lp;
1360
1361 return TRUE; 1230 return TRUE;
1362} 1231}