use busybox funcs to make smaller

This commit is contained in:
Mike Frysinger 2005-04-24 05:39:52 +00:00
parent cf131bb328
commit 560047ce86

View File

@ -60,8 +60,8 @@ Index: include/applets.h
APPLET_NOUSAGE("egrep", grep_main, _BB_DIR_BIN, _BB_SUID_NEVER) APPLET_NOUSAGE("egrep", grep_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif #endif
--- /dev/null 2005-04-24 01:00:01.350003056 -0400 --- /dev/null 2005-04-24 01:00:01.350003056 -0400
+++ editors/ed.c 2005-04-24 01:15:09.000000000 -0400 +++ ed.c 2005-04-24 01:38:51.000000000 -0400
@@ -0,0 +1,1447 @@ @@ -0,0 +1,1425 @@
+/* +/*
+ * Copyright (c) 2002 by David I. Bell + * Copyright (c) 2002 by David I. Bell
+ * Permission is granted to use, distribute, or modify this source, + * Permission is granted to use, distribute, or modify this source,
@ -80,49 +80,36 @@ Index: include/applets.h
+#include <ctype.h> +#include <ctype.h>
+#include <sys/param.h> +#include <sys/param.h>
+#include <malloc.h> +#include <malloc.h>
+//#include "sash.h" +#include "busybox.h"
+ +
+#define USERSIZE 1024 /* max line length typed in by user */ +#define USERSIZE 1024 /* max line length typed in by user */
+#define INITBUF_SIZE 1024 /* initial buffer size */ +#define INITBUF_SIZE 1024 /* initial buffer size */
+ +
+typedef int BOOL; +typedef int BOOL;
+
+#define FALSE ((BOOL) 0)
+#define TRUE ((BOOL) 1)
+
+#define isBlank(ch) (((ch) == ' ') || ((ch) == '\t'))
+#define isDecimal(ch) (((ch) >= '0') && ((ch) <= '9'))
+
+#define STDOUT 1
+
+typedef int NUM; +typedef int NUM;
+typedef int LEN; +typedef int LEN;
+ +
+typedef struct LINE LINE; +typedef struct LINE LINE;
+ +struct LINE {
+struct LINE + LINE *next;
+{ + LINE *prev;
+ LINE * next;
+ LINE * prev;
+ LEN len; + LEN len;
+ char data[1]; + char data[1];
+}; +};
+ +
+static LINE lines;
+static LINE *curLine;
+static NUM curNum;
+static NUM lastNum;
+static NUM marks[26];
+static BOOL dirty;
+static char *fileName;
+static char searchString[USERSIZE];
+ +
+static LINE lines; +static char *bufBase;
+static LINE * curLine; +static char *bufPtr;
+static NUM curNum; +static LEN bufUsed;
+static NUM lastNum; +static LEN bufSize;
+static NUM marks[26];
+static BOOL dirty;
+static char * fileName;
+static char searchString[USERSIZE];
+
+static char * bufBase;
+static char * bufPtr;
+static LEN bufUsed;
+static LEN bufSize;
+
+ +
+static void doCommands(void); +static void doCommands(void);
+static void subCommand(const char * cmd, NUM num1, NUM num2); +static void subCommand(const char * cmd, NUM num1, NUM num2);
@ -139,33 +126,25 @@ Index: include/applets.h
+static NUM searchLines(const char * str, NUM num1, NUM num2); +static NUM searchLines(const char * str, NUM num1, NUM num2);
+static LINE * findLine(NUM num); +static LINE * findLine(NUM num);
+ +
+static LEN findString +static LEN findString(const LINE * lp, const char * str, LEN len, LEN offset);
+ (const LINE * lp, const char * str, LEN len, LEN offset);
+ +
+ +int ed_main(int argc, char **argv)
+void
+ed_main(int argc, const char ** argv)
+{ +{
+ if (!initEdit()) + if (!initEdit())
+ return; + return EXIT_FAILURE;
+ +
+ if (argc > 1) + if (argc > 1) {
+ {
+ fileName = strdup(argv[1]); + fileName = strdup(argv[1]);
+ +
+ if (fileName == NULL) + if (fileName == NULL) {
+ { + bb_error_msg("No memory");
+ fprintf(stderr, "No memory\n");
+ termEdit(); + termEdit();
+ + return EXIT_SUCCESS;
+ return;
+ } + }
+ +
+ if (!readLines(fileName, 1)) + if (!readLines(fileName, 1)) {
+ {
+ termEdit(); + termEdit();
+ + return EXIT_SUCCESS;
+ return;
+ } + }
+ +
+ if (lastNum) + if (lastNum)
@ -177,14 +156,13 @@ Index: include/applets.h
+ doCommands(); + doCommands();
+ +
+ termEdit(); + termEdit();
+ return EXIT_SUCCESS;
+} +}
+ +
+
+/* +/*
+ * Read commands until we are told to stop. + * Read commands until we are told to stop.
+ */ + */
+static void +static void doCommands(void)
+doCommands(void)
+{ +{
+ const char * cp; + const char * cp;
+ char * endbuf; + char * endbuf;
@ -213,7 +191,7 @@ Index: include/applets.h
+ +
+ if (*endbuf != '\n') + if (*endbuf != '\n')
+ { + {
+ fprintf(stderr, "Command line too long\n"); + bb_error_msg("Command line too long");
+ +
+ do + do
+ { + {
@ -224,14 +202,14 @@ Index: include/applets.h
+ continue; + continue;
+ } + }
+ +
+ while ((endbuf > buf) && isBlank(endbuf[-1])) + while ((endbuf > buf) && isblank(endbuf[-1]))
+ endbuf--; + endbuf--;
+ +
+ *endbuf = '\0'; + *endbuf = '\0';
+ +
+ cp = buf; + cp = buf;
+ +
+ while (isBlank(*cp)) + while (isblank(*cp))
+ cp++; + cp++;
+ +
+ have1 = FALSE; + have1 = FALSE;
@ -246,7 +224,7 @@ Index: include/applets.h
+ if (!getNum(&cp, &have1, &num1)) + if (!getNum(&cp, &have1, &num1))
+ continue; + continue;
+ +
+ while (isBlank(*cp)) + while (isblank(*cp))
+ cp++; + cp++;
+ +
+ if (*cp == ',') + if (*cp == ',')
@ -288,13 +266,13 @@ Index: include/applets.h
+ break; + break;
+ +
+ case 'f': + case 'f':
+ if (*cp && !isBlank(*cp)) + if (*cp && !isblank(*cp))
+ { + {
+ fprintf(stderr, "Bad file command\n"); + bb_error_msg("Bad file command");
+ break; + break;
+ } + }
+ +
+ while (isBlank(*cp)) + while (isblank(*cp))
+ cp++; + cp++;
+ +
+ if (*cp == '\0') + if (*cp == '\0')
@ -311,7 +289,7 @@ Index: include/applets.h
+ +
+ if (newname == NULL) + if (newname == NULL)
+ { + {
+ fprintf(stderr, "No memory for file name\n"); + bb_error_msg("No memory for file name");
+ break; + break;
+ } + }
+ +
@ -326,12 +304,12 @@ Index: include/applets.h
+ break; + break;
+ +
+ case 'k': + case 'k':
+ while (isBlank(*cp)) + while (isblank(*cp))
+ cp++; + cp++;
+ +
+ if ((*cp < 'a') || (*cp > 'a') || cp[1]) + if ((*cp < 'a') || (*cp > 'a') || cp[1])
+ { + {
+ fprintf(stderr, "Bad mark name\n"); + bb_error_msg("Bad mark name");
+ break; + break;
+ } + }
+ +
@ -347,12 +325,12 @@ Index: include/applets.h
+ break; + break;
+ +
+ case 'q': + case 'q':
+ while (isBlank(*cp)) + while (isblank(*cp))
+ cp++; + cp++;
+ +
+ if (have1 || *cp) + if (have1 || *cp)
+ { + {
+ fprintf(stderr, "Bad quit command\n"); + bb_error_msg("Bad quit command");
+ break; + break;
+ } + }
+ +
@ -366,7 +344,7 @@ Index: include/applets.h
+ fgets(buf, sizeof(buf), stdin); + fgets(buf, sizeof(buf), stdin);
+ cp = buf; + cp = buf;
+ +
+ while (isBlank(*cp)) + while (isblank(*cp))
+ cp++; + cp++;
+ +
+ if ((*cp == 'y') || (*cp == 'Y')) + if ((*cp == 'y') || (*cp == 'Y'))
@ -375,18 +353,18 @@ Index: include/applets.h
+ break; + break;
+ +
+ case 'r': + case 'r':
+ if (*cp && !isBlank(*cp)) + if (*cp && !isblank(*cp))
+ { + {
+ fprintf(stderr, "Bad read command\n"); + bb_error_msg("Bad read command");
+ break; + break;
+ } + }
+ +
+ while (isBlank(*cp)) + while (isblank(*cp))
+ cp++; + cp++;
+ +
+ if (*cp == '\0') + if (*cp == '\0')
+ { + {
+ fprintf(stderr, "No file name\n"); + bb_error_msg("No file name");
+ break; + break;
+ } + }
+ +
@ -406,13 +384,13 @@ Index: include/applets.h
+ break; + break;
+ +
+ case 'w': + case 'w':
+ if (*cp && !isBlank(*cp)) + if (*cp && !isblank(*cp))
+ { + {
+ fprintf(stderr, "Bad write command\n"); + bb_error_msg("Bad write command");
+ break; + break;
+ } + }
+ +
+ while (isBlank(*cp)) + while (isblank(*cp))
+ cp++; + cp++;
+ +
+ if (!have1) { + if (!have1) {
@ -425,7 +403,7 @@ Index: include/applets.h
+ +
+ if (cp == NULL) + if (cp == NULL)
+ { + {
+ fprintf(stderr, "No file name specified\n"); + bb_error_msg("No file name specified");
+ break; + break;
+ } + }
+ +
@ -450,7 +428,7 @@ Index: include/applets.h
+ case '.': + case '.':
+ if (have1) + if (have1)
+ { + {
+ fprintf(stderr, "No arguments allowed\n"); + bb_error_msg("No arguments allowed");
+ break; + break;
+ } + }
+ +
@ -480,7 +458,7 @@ Index: include/applets.h
+ break; + break;
+ +
+ default: + default:
+ fprintf(stderr, "Unimplemented command\n"); + bb_error_msg("Unimplemented command");
+ break; + break;
+ } + }
+ } + }
@ -512,7 +490,7 @@ Index: include/applets.h
+ +
+ if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) + if ((num1 < 1) || (num2 > lastNum) || (num1 > num2))
+ { + {
+ fprintf(stderr, "Bad line range for substitute\n"); + bb_error_msg("Bad line range for substitute");
+ +
+ return; + return;
+ } + }
@ -528,9 +506,9 @@ Index: include/applets.h
+ strcpy(buf, cmd); + strcpy(buf, cmd);
+ cp = buf; + cp = buf;
+ +
+ if (isBlank(*cp) || (*cp == '\0')) + if (isblank(*cp) || (*cp == '\0'))
+ { + {
+ fprintf(stderr, "Bad delimiter for substitute\n"); + bb_error_msg("Bad delimiter for substitute");
+ +
+ return; + return;
+ } + }
@ -542,7 +520,7 @@ Index: include/applets.h
+ +
+ if (cp == NULL) + if (cp == NULL)
+ { + {
+ fprintf(stderr, "Missing 2nd delimiter for substitute\n"); + bb_error_msg("Missing 2nd delimiter for substitute");
+ +
+ return; + return;
+ } + }
@ -568,7 +546,7 @@ Index: include/applets.h
+ break; + break;
+ +
+ default: + default:
+ fprintf(stderr, "Unknown option for substitute\n"); + bb_error_msg("Unknown option for substitute");
+ +
+ return; + return;
+ } + }
@ -577,7 +555,7 @@ Index: include/applets.h
+ { + {
+ if (searchString[0] == '\0') + if (searchString[0] == '\0')
+ { + {
+ fprintf(stderr, "No previous search string\n"); + bb_error_msg("No previous search string");
+ +
+ return; + return;
+ } + }
@ -665,7 +643,7 @@ Index: include/applets.h
+ +
+ if (nlp == NULL) + if (nlp == NULL)
+ { + {
+ fprintf(stderr, "Cannot get memory for line\n"); + bb_error_msg("Cannot get memory for line");
+ +
+ return; + return;
+ } + }
@ -707,7 +685,7 @@ Index: include/applets.h
+ } + }
+ +
+ if (!didSub) + if (!didSub)
+ fprintf(stderr, "No substitutions found for \"%s\"\n", oldStr); + bb_error_msg("No substitutions found for \"%s\"", oldStr);
+} +}
+ +
+ +
@ -774,7 +752,7 @@ Index: include/applets.h
+ +
+ if (buf[len - 1] != '\n') + if (buf[len - 1] != '\n')
+ { + {
+ fprintf(stderr, "Line too long\n"); + bb_error_msg("Line too long");
+ +
+ do + do
+ { + {
@ -817,7 +795,7 @@ Index: include/applets.h
+ +
+ while (TRUE) + while (TRUE)
+ { + {
+ while (isBlank(*cp)) + while (isblank(*cp))
+ cp++; + cp++;
+ +
+ switch (*cp) + switch (*cp)
@ -839,7 +817,7 @@ Index: include/applets.h
+ +
+ if ((*cp < 'a') || (*cp > 'z')) + if ((*cp < 'a') || (*cp > 'z'))
+ { + {
+ fprintf(stderr, "Bad mark name\n"); + bb_error_msg("Bad mark name");
+ +
+ return FALSE; + return FALSE;
+ } + }
@ -869,7 +847,7 @@ Index: include/applets.h
+ break; + break;
+ +
+ default: + default:
+ if (!isDecimal(*cp)) + if (!isdigit(*cp))
+ { + {
+ *retcp = cp; + *retcp = cp;
+ *retHaveNum = haveNum; + *retHaveNum = haveNum;
@ -880,7 +858,7 @@ Index: include/applets.h
+ +
+ num = 0; + num = 0;
+ +
+ while (isDecimal(*cp)) + while (isdigit(*cp))
+ num = num * 10 + *cp++ - '0'; + num = num * 10 + *cp++ - '0';
+ +
+ haveNum = TRUE; + haveNum = TRUE;
@ -889,7 +867,7 @@ Index: include/applets.h
+ +
+ value += num * sign; + value += num * sign;
+ +
+ while (isBlank(*cp)) + while (isblank(*cp))
+ cp++; + cp++;
+ +
+ switch (*cp) + switch (*cp)
@ -928,7 +906,7 @@ Index: include/applets.h
+ +
+ if (bufBase == NULL) + if (bufBase == NULL)
+ { + {
+ fprintf(stderr, "No memory for buffer\n"); + bb_error_msg("No memory for buffer");
+ +
+ return FALSE; + return FALSE;
+ } + }
@ -999,7 +977,7 @@ Index: include/applets.h
+ +
+ if ((num < 1) || (num > lastNum + 1)) + if ((num < 1) || (num > lastNum + 1))
+ { + {
+ fprintf(stderr, "Bad line for read\n"); + bb_error_msg("Bad line for read");
+ +
+ return FALSE; + return FALSE;
+ } + }
@ -1059,7 +1037,7 @@ Index: include/applets.h
+ +
+ if (cp == NULL) + if (cp == NULL)
+ { + {
+ fprintf(stderr, "No memory for buffer\n"); + bb_error_msg("No memory for buffer");
+ close(fd); + close(fd);
+ +
+ return FALSE; + return FALSE;
@ -1121,7 +1099,7 @@ Index: include/applets.h
+ +
+ if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) + if ((num1 < 1) || (num2 > lastNum) || (num1 > num2))
+ { + {
+ fprintf(stderr, "Bad line range for write\n"); + bb_error_msg("Bad line range for write");
+ +
+ return FALSE; + return FALSE;
+ } + }
@ -1193,7 +1171,7 @@ Index: include/applets.h
+ +
+ if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) + if ((num1 < 1) || (num2 > lastNum) || (num1 > num2))
+ { + {
+ fprintf(stderr, "Bad line range for print\n"); + bb_error_msg("Bad line range for print");
+ +
+ return FALSE; + return FALSE;
+ } + }
@ -1207,7 +1185,7 @@ Index: include/applets.h
+ { + {
+ if (!expandFlag) + if (!expandFlag)
+ { + {
+ write(STDOUT, lp->data, lp->len); + write(1, lp->data, lp->len);
+ setCurNum(num1++); + setCurNum(num1++);
+ lp = lp->next; + lp = lp->next;
+ +
@ -1274,7 +1252,7 @@ Index: include/applets.h
+ +
+ if ((num < 1) || (num > lastNum + 1)) + if ((num < 1) || (num > lastNum + 1))
+ { + {
+ fprintf(stderr, "Inserting at bad line number\n"); + bb_error_msg("Inserting at bad line number");
+ +
+ return FALSE; + return FALSE;
+ } + }
@ -1283,7 +1261,7 @@ Index: include/applets.h
+ +
+ if (newLp == NULL) + if (newLp == NULL)
+ { + {
+ fprintf(stderr, "Failed to allocate memory for line\n"); + bb_error_msg("Failed to allocate memory for line");
+ +
+ return FALSE; + return FALSE;
+ } + }
@ -1330,7 +1308,7 @@ Index: include/applets.h
+ +
+ if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) + if ((num1 < 1) || (num2 > lastNum) || (num1 > num2))
+ { + {
+ fprintf(stderr, "Bad line numbers for delete\n"); + bb_error_msg("Bad line numbers for delete");
+ +
+ return FALSE; + return FALSE;
+ } + }
@ -1391,7 +1369,7 @@ Index: include/applets.h
+ +
+ if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) + if ((num1 < 1) || (num2 > lastNum) || (num1 > num2))
+ { + {
+ fprintf(stderr, "Bad line numbers for search\n"); + bb_error_msg("Bad line numbers for search");
+ +
+ return 0; + return 0;
+ } + }
@ -1400,7 +1378,7 @@ Index: include/applets.h
+ { + {
+ if (searchString[0] == '\0') + if (searchString[0] == '\0')
+ { + {
+ fprintf(stderr, "No previous search string\n"); + bb_error_msg("No previous search string");
+ +
+ return 0; + return 0;
+ } + }
@ -1427,7 +1405,7 @@ Index: include/applets.h
+ lp = lp->next; + lp = lp->next;
+ } + }
+ +
+ fprintf(stderr, "Cannot find string \"%s\"\n", str); + bb_error_msg("Cannot find string \"%s\"", str);
+ +
+ return 0; + return 0;
+} +}
@ -1444,7 +1422,7 @@ Index: include/applets.h
+ +
+ if ((num < 1) || (num > lastNum)) + if ((num < 1) || (num > lastNum))
+ { + {
+ fprintf(stderr, "Line number %d does not exist\n", num); + bb_error_msg("Line number %d does not exist", num);
+ +
+ return NULL; + return NULL;
+ } + }