From 55f30b05ac6902ac17bad04f38fd4f381a8d75f7 Mon Sep 17 00:00:00 2001
From: Denis Vlasenko <vda.linux@googlemail.com>
Date: Sat, 24 Mar 2007 22:42:29 +0000
Subject: inetd,ed,msh: data/bss reduction (in mss, more than 9k of it)

---
 editors/ed.c       |  12 +++++--
 networking/inetd.c |  24 +++++++------
 shell/msh.c        | 104 +++++++++++++++++++++++++++++++----------------------
 3 files changed, 83 insertions(+), 57 deletions(-)

diff --git a/editors/ed.c b/editors/ed.c
index 42adca409..1706e4271 100644
--- a/editors/ed.c
+++ b/editors/ed.c
@@ -9,8 +9,12 @@
 
 #include "busybox.h"
 
-#define	USERSIZE	1024	/* max line length typed in by user */
-#define	INITBUF_SIZE	1024	/* initial buffer size */
+enum {
+	USERSIZE = sizeof(bb_common_bufsiz1) > 1024 ? 1024
+	         : sizeof(bb_common_bufsiz1) - 1, /* max line length typed in by user */
+	INITBUF_SIZE = 1024, /* initial buffer size */
+};
+
 typedef struct LINE {
 	struct LINE *next;
 	struct LINE *prev;
@@ -18,9 +22,11 @@ typedef struct LINE {
 	char data[1];
 } LINE;
 
+#define searchString bb_common_bufsiz1
+
 static LINE lines, *curLine;
 static int curNum, lastNum, marks[26], dirty;
-static char *bufBase, *bufPtr, *fileName, searchString[USERSIZE];
+static char *bufBase, *bufPtr, *fileName;
 static int bufUsed, bufSize;
 
 static void doCommands(void);
diff --git a/networking/inetd.c b/networking/inetd.c
index 48e23db2e..83123463f 100644
--- a/networking/inetd.c
+++ b/networking/inetd.c
@@ -172,7 +172,6 @@
 #include <rpc/pmap_clnt.h>
 #endif
 
-#define _PATH_INETDCONF "/etc/inetd.conf"
 #define _PATH_INETDPID  "/var/run/inetd.pid"
 
 
@@ -327,10 +326,9 @@ static int timingout;
 static struct servent *sp;
 static uid_t uid;
 
-static const char *CONFIG = _PATH_INETDCONF;
+static const char *config_filename = "/etc/inetd.conf";
 
 static FILE *fconfig;
-static char line[1024];
 static char *defhost;
 
 /* xstrdup(NULL) returns NULL, but this one
@@ -350,7 +348,7 @@ static int setconfig(void)
 		fseek(fconfig, 0L, SEEK_SET);
 		return 1;
 	}
-	fconfig = fopen(CONFIG, "r");
+	fconfig = fopen(config_filename, "r");
 	return (fconfig != NULL);
 }
 
@@ -511,6 +509,8 @@ static void setup(servtab_t *sep)
 
 static char *nextline(void)
 {
+#define line bb_common_bufsiz1
+
 	char *cp;
 	FILE *fd = fconfig;
 
@@ -541,10 +541,12 @@ static char *skip(char **cpp) /* int report; */
 		int c;
 
 		c = getc(fconfig);
-		(void) ungetc(c, fconfig);
-		if (c == ' ' || c == '\t')
-			if ((cp = nextline()))
+		ungetc(c, fconfig);
+		if (c == ' ' || c == '\t') {
+			cp = nextline();
+			if (cp)
 				goto again;
+		}
 		*cpp = NULL;
 		/* goto erp; */
 		return NULL;
@@ -924,7 +926,7 @@ static void config(int sig ATTRIBUTE_UNUSED)
 	char protoname[10];
 
 	if (!setconfig()) {
-		bb_perror_msg("%s", CONFIG);
+		bb_perror_msg("%s", config_filename);
 		return;
 	}
 	for (sep = servtab; sep; sep = sep->se_next)
@@ -1281,10 +1283,10 @@ int inetd_main(int argc, char *argv[])
 
 	uid = getuid();
 	if (uid != 0)
-		CONFIG = NULL;
+		config_filename = NULL;
 	if (argc > 0)
-		CONFIG = argv[0];
-	if (CONFIG == NULL)
+		config_filename = argv[0];
+	if (config_filename == NULL)
 		bb_error_msg_and_die("non-root must specify a config file");
 
 #ifdef BB_NOMMU
diff --git a/shell/msh.c b/shell/msh.c
index d9dd3efb2..66b10f346 100644
--- a/shell/msh.c
+++ b/shell/msh.c
@@ -13,12 +13,13 @@
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
+# include <sys/times.h>
+# include <setjmp.h>
+
 #ifdef STANDALONE
 # ifndef _GNU_SOURCE
 #  define _GNU_SOURCE
 # endif
-# include <setjmp.h>
-# include <sys/times.h>
 # include <sys/types.h>
 # include <sys/stat.h>
 # include <sys/wait.h>
@@ -82,8 +83,6 @@ static char *itoa(int n)
 	return local_buf;
 }
 #else
-# include <setjmp.h>
-# include <sys/times.h>
 # include "busybox.h"
 extern char **environ;
 #endif
@@ -162,12 +161,10 @@ int mshdbg_rc = 0;
  */
 typedef void xint;				/* base type of jmp_buf, for not broken compilers */
 
+
 /*
  * shell components
  */
-
-#define	QUOTE	0200
-
 #define	NOBLOCK	((struct op *)NULL)
 #define	NOWORD	((char *)NULL)
 #define	NOWORDS	((char **)NULL)
@@ -297,24 +294,21 @@ static char *flag = flags - 'a';
 static char *null;				/* null value for variable */
 static int intr;				/* interrupt pending */
 
-static char *trap[_NSIG + 1];
-static char ourtrap[_NSIG + 1];
+/* moved to G: static char *trap[_NSIG + 1]; */
+/* moved to G: static char ourtrap[_NSIG + 1]; */
 static int trapset;				/* trap pending */
 
 static int heedint;				/* heed interrupt signals */
 
 static int yynerrs;				/* yacc */
 
-static char line[LINELIM];
-static char *elinep;
+/* moved to G: static char line[LINELIM]; */
 
 #if ENABLE_FEATURE_EDITING
 static char *current_prompt;
 static line_input_t *line_input_state;
 #endif
 
-static int areanum;				/* current allocation area */
-
 
 /*
  * other functions
@@ -483,29 +477,15 @@ struct io {
 #define	INSUB()	(e.iop->task == XGRAVE || e.iop->task == XDOLL)
 
 static struct ioarg temparg = { 0, 0, 0, AFID_NOBUF, 0 };	/* temporary for PUSHIO */
-static struct ioarg ioargstack[NPUSH];
+/* moved to G: static struct ioarg ioargstack[NPUSH]; */
 static struct io iostack[NPUSH];
-static struct iobuf sharedbuf = { AFID_NOBUF };
-static struct iobuf mainbuf = { AFID_NOBUF };
+/* moved to G: static struct iobuf sharedbuf = { AFID_NOBUF }; */
+/* moved to G: static struct iobuf mainbuf = { AFID_NOBUF }; */
 static unsigned bufid = AFID_ID;	/* buffer id counter */
 
-#define	PUSHIO(what,arg,gen) ((temparg.what = (arg)), pushio(&temparg,(gen)))
 #define	RUN(what,arg,gen) ((temparg.what = (arg)), run(&temparg,(gen)))
 
 
-/*
- * parsing & execution environment
- */
-static struct env {
-	char *linep;
-	struct io *iobase;
-	struct io *iop;
-	xint *errpt;				/* void * */
-	int iofd;
-	struct env *oenv;
-} e;
-
-
 /*
  * input generators for IO structure
  */
@@ -537,6 +517,7 @@ static void ioecho(char c);
  * IO control
  */
 static void pushio(struct ioarg *argp, int (*f) (struct ioarg *));
+#define PUSHIO(what,arg,gen) ((temparg.what = (arg)), pushio(&temparg,(gen)))
 static int remap(int fd);
 static int openpipe(int *pv);
 static void closepipe(int *pv);
@@ -599,7 +580,6 @@ static int xstrcmp(char *p1, char *p2);
 static void glob0(char *a0, unsigned a1, int a2,
 				  int (*a3) (char *, char *));
 static void readhere(char **name, char *s, int ec);
-static void pushio(struct ioarg *argp, int (*f) (struct ioarg *));
 static int xxchar(struct ioarg *ap);
 
 struct here {
@@ -705,9 +685,6 @@ static struct brkcon *brklist;
 static int isbreak;
 static struct wdblock *wdlist;
 static struct wdblock *iolist;
-static char *trap[_NSIG + 1];
-static char ourtrap[_NSIG + 1];
-static int trapset;				/* trap pending */
 
 #ifdef MSHDEBUG
 static struct var *mshdbg_var;
@@ -731,8 +708,7 @@ static int peeksym;
 static int nlseen;
 static int iounit = IODEFAULT;
 static YYSTYPE yylval;
-static char *elinep = line + sizeof(line) - 5;
-
+static char *elinep; /* done in main(): = line + sizeof(line) - 5 */
 
 static struct here *inhere;     /* list of hear docs while parsing */
 static struct here *acthere;    /* list of active here documents */
@@ -742,8 +718,20 @@ static struct region *areanxt;  /* starting point of scan */
 static void *brktop;
 static void *brkaddr;
 
+/*
+ * parsing & execution environment
+ */
+struct env {
+	char *linep;
+	struct io *iobase;
+	struct io *iop;
+	xint *errpt;		/* void * */
+	int iofd;
+	struct env *oenv;
+};
+
 static struct env e = {
-	line,                   /* linep:  char ptr */
+	NULL /* set to line in main() */, /* linep:  char ptr */
 	iostack,                /* iobase:  struct io ptr */
 	iostack - 1,            /* iop:  struct io ptr */
 	(xint *) NULL,          /* errpt:  void ptr for errors? */
@@ -751,6 +739,29 @@ static struct env e = {
 	(struct env *) NULL     /* oenv:  struct env ptr */
 };
 
+
+struct globals {
+	char ourtrap[_NSIG + 1];
+	char *trap[_NSIG + 1];
+	struct iobuf sharedbuf; /* in main(): set to { AFID_NOBUF } */
+	struct iobuf mainbuf; /* in main(): set to { AFID_NOBUF } */
+	struct ioarg ioargstack[NPUSH];
+	char filechar_cmdbuf[BUFSIZ];
+	char line[LINELIM];
+	char child_cmd[LINELIM];
+};
+
+#define G (*ptr_to_globals)
+#define ourtrap         (G.ourtrap        )
+#define trap            (G.trap           )
+#define sharedbuf       (G.sharedbuf      )
+#define mainbuf         (G.mainbuf        )
+#define ioargstack      (G.ioargstack     )
+#define filechar_cmdbuf (G.filechar_cmdbuf)
+#define line            (G.line           )
+#define child_cmd       (G.child_cmd      )
+
+
 #ifdef MSHDEBUG
 void print_t(struct op *t)
 {
@@ -1517,7 +1528,7 @@ static void onintr(int s)					/* ANSI C requires a parameter */
 
 #define	CMASK	0377
 #define	QUOTE	0200
-#define	QMASK	(CMASK&~QUOTE)
+#define	QMASK	(CMASK & ~QUOTE)
 #define	NOT	'!'					/* might use ^ */
 
 static const char *cclass(const char *p, int sub)
@@ -4011,11 +4022,12 @@ static int dollar(int quoted)
 
 static int grave(int quoted)
 {
+	/* moved to G: static char child_cmd[LINELIM]; */
+
 	const char *cp;
 	int i;
 	int j;
 	int pf[2];
-	static char child_cmd[LINELIM];
 	const char *src;
 	char *dest;
 	int count;
@@ -4823,15 +4835,15 @@ static int filechar(struct ioarg *ap)
 	}
 #if ENABLE_FEATURE_EDITING
 	if (interactive && isatty(ap->afile)) {
-		static char mycommand[BUFSIZ];
+		/* moved to G: static char filechar_cmdbuf[BUFSIZ]; */
 		static int position = 0, size = 0;
 
 		while (size == 0 || position >= size) {
-			read_line_input(current_prompt, mycommand, BUFSIZ, line_input_state);
-			size = strlen(mycommand);
+			read_line_input(current_prompt, filechar_cmdbuf, BUFSIZ, line_input_state);
+			size = strlen(filechar_cmdbuf);
 			position = 0;
 		}
-		c = mycommand[position];
+		c = filechar_cmdbuf[position];
 		position++;
 		return c;
 	}
@@ -5176,6 +5188,12 @@ int msh_main(int argc, char **argv)
 	char *name, **ap;
 	int (*iof) (struct ioarg *);
 
+	PTR_TO_GLOBALS = xzalloc(sizeof(G));
+	sharedbuf.id = AFID_NOBUF;
+	mainbuf.id = AFID_NOBUF;
+	e.linep = line;
+	elinep = line + sizeof(line) - 5;
+
 #if ENABLE_FEATURE_EDITING
 	line_input_state = new_line_input_t(FOR_SHELL);
 #endif
-- 
cgit v1.2.3