[BusyBox] strict function pointer casts in msh.c

Ilguiz Latypov ilatypov at superbt.com
Sat Apr 13 22:46:04 UTC 2002


I tried to strengthen the loose function pointer declarations in msh.c to
avoid implicit assumptions on compiler's capabilities.  This is not a 
critical problem.  The only effect of the original loose declarations was 
compiler warnings so far.

The patch is in the attachment.

Ilguiz

P.S. The NOFILE macro was already defined by uClibc.  However, its
definition was prepended with a suggestion to not rely on it anywhere.  
This is why I undef'ined the uClibc's macro in msh.c.

-------------- next part --------------
Index: msh.c
===================================================================
RCS file: /usr/local/cvsroot/busybox/shell/msh.c,v
retrieving revision 1.1.1.1
retrieving revision 1.4
diff -u -r1.1.1.1 -r1.4
--- msh.c	25 Mar 2002 16:39:25 -0000	1.1.1.1
+++ msh.c	11 Apr 2002 03:53:22 -0000	1.4
@@ -52,6 +52,9 @@
 #define	LINELIM	2100
 #define	NPUSH	8	/* limit to input nesting */
 
+#ifdef NOFILE
+#undef NOFILE
+#endif
 #define	NOFILE	20	/* Number of open files */
 #define	NUFILE	10	/* Number of user-accessible files */
 #define	FDBASE	10	/* First file usable by Shell */
@@ -66,7 +69,7 @@
 /*
  * library and system defintions
  */
-typedef void xint;	/* base type of jmp_buf, for not broken compilers */
+typedef jmp_buf xint;	/* base type of jmp_buf, for not broken compilers */
 
 /*
  * shell components
@@ -92,6 +95,22 @@
 	char	*str;	/* identifier for case and for */
 };
 
+/*
+ * Builtin function pointer types.  
+ * Often the actual function is supposed to receive parameters.
+ * We suppress compiler warnings by casting every function pointer
+ * to bfunc_t.
+ */
+struct var;         /* declaration follows */
+struct io;
+struct ioarg;
+
+typedef void        (*fvoid_varp_t) (struct var *);
+typedef void        (*fvoid_int_t)  (int);
+typedef int         (*fint_t)       (void);
+typedef int         (*fint_op_t)    (struct op *);
+typedef int         (*fint_ioargp_iop_t)    (struct ioarg *, struct io *);
+
 #define	TCOM	1	/* command */
 #define	TPAREN	2	/* (c-list) */
 #define	TPIPE	3	/* a | b */
@@ -213,7 +232,7 @@
 /*
  * other functions
  */
-static int (*inbuilt(char *s ))(void);
+static fint_t inbuilt(char *s);
 
 static char *rexecve (char *c , char **v, char **envp );
 static char *space (int n );
@@ -250,8 +269,8 @@
  */
 static void leave (void); /* abort shell (or fail in subshell) */
 static void fail (void);	 /* fail but return to process next command */
-static void warn (char *s );
-static void sig (int i );	 /* default signal handler */
+static void warn (char *s);
+static void sig (int i);	 /* default signal handler */
 
 
 
@@ -394,14 +413,15 @@
 
 /* an input generator's state */
 struct	io {
-	int	(*iofn)();
-	struct	ioarg	*argp;
-	int	peekc;
-	char	prev;		/* previous character read by readc() */
-	char	nlcount;	/* for `'s */
-	char	xchar;		/* for `'s */
-	char	task;		/* reason for pushed IO */
+	fint_ioargp_iop_t	iofn;
+	struct				ioarg	*argp;
+	int	    			peekc;
+	char				prev;		/* previous character read by readc() */
+	char				nlcount;	/* for `'s */
+	char				xchar;		/* for `'s */
+	char				task;		/* reason for pushed IO */
 };
+
 //static	struct	io	iostack[NPUSH];
 #define	XOTHER	0	/* none of the below */
 #define	XDOLL	1	/* expanding ${} */
@@ -429,7 +449,7 @@
 static void gethere (void);
 static void markhere (char *s, struct ioword *iop );
 static int herein (char *hname, int xdoll );
-static int run (struct ioarg *argp, int (*f)());
+static int run (struct ioarg *argp, fint_t f);
 
 /*
  * IO functions
@@ -446,15 +466,17 @@
 /*
  * IO control
  */
-static void pushio (struct ioarg *argp, int (*fn)());
+static void pushio (struct ioarg *argp, fint_t fn);
 static int remap (int fd );
 static int openpipe (int *pv );
 static void closepipe (int *pv );
 static struct io *setbase (struct io *ip );
 
 static	struct	ioarg	temparg;	/* temporary for PUSHIO */
-#define	PUSHIO(what,arg,gen) ((temparg.what = (arg)),pushio(&temparg,(gen)))
-#define	RUN(what,arg,gen) ((temparg.what = (arg)), run(&temparg,(gen)))
+#define	PUSHIO(what,arg,gen) ((temparg.what = (arg)), \
+							pushio(&temparg,(fint_t)(gen)))
+#define	RUN(what,arg,gen) ((temparg.what = (arg)), \
+							run(&temparg,(fint_t)(gen)))
 
 /* -------- word.h -------- */
 
@@ -509,7 +531,7 @@
 static int doeval(struct op *t );
 static int dotrap(struct op *t );
 static int getsig(char *s );
-static void setsig (int n, void (*f)());
+static void setsig (int n, fvoid_int_t f);
 static int getn(char *as );
 static int dobreak(struct op *t );
 static int docontinue(struct op *t );
@@ -517,7 +539,7 @@
 static int doexit(struct op *t );
 static int doexport(struct op *t );
 static int doreadonly(struct op *t );
-static void rdexp (char **wp, void (*f)(), int key);
+static void rdexp (char **wp, fvoid_varp_t f, int key);
 static void badid(char *s );
 static int doset(struct op *t );
 static void varput (char *s, int out );
@@ -535,7 +557,7 @@
 static void glob2 (char *i, char *j );
 static void glob3 (char *i, char *j, char *k );
 static void readhere (char **name, char *s, int ec );
-static void pushio(struct ioarg *argp, int (*fn)());
+static void pushio(struct ioarg *argp, int (*fn)(void));
 static int xxchar(struct ioarg *ap );
 
 struct	here {
@@ -595,29 +617,29 @@
 
 struct builtincmd {
 	const char *name;
-	int (*builtinfunc)();
+	fint_t builtinfunc;
 };
 static const struct	builtincmd	builtincmds[] = {
-    {".",		dodot},
-    {":",		dolabel},
-    {"break",	dobreak},
-    {"cd",		dochdir},
-    {"continue",docontinue},
-    {"eval",	doeval},
-    {"exec",	doexec},
-    {"exit",	doexit},
-    {"export",	doexport},
-    {"help",	dohelp},
-    {"login",	dologin},
-    {"newgrp",	dologin},
-    {"read",	doread},
-    {"readonly",doreadonly},
-    {"set",		doset},
-    {"shift",	doshift},
-    {"times",	dotimes},
-    {"trap",	dotrap},
-    {"umask",	doumask},
-    {"wait",	dowait},
+    {".",		(fint_t)dodot},
+    {":",		(fint_t)dolabel},
+    {"break",	(fint_t)dobreak},
+    {"cd",		(fint_t)dochdir},
+    {"continue",(fint_t)docontinue},
+    {"eval",	(fint_t)doeval},
+    {"exec",	(fint_t)doexec},
+    {"exit",	(fint_t)doexit},
+    {"export",	(fint_t)doexport},
+    {"help",	(fint_t)dohelp},
+    {"login",	(fint_t)dologin},
+    {"newgrp",	(fint_t)dologin},
+    {"read",	(fint_t)doread},
+    {"readonly",(fint_t)doreadonly},
+    {"set",		(fint_t)doset},
+    {"shift",	(fint_t)doshift},
+    {"times",	(fint_t)dotimes},
+    {"trap",	(fint_t)dotrap},
+    {"umask",	(fint_t)doumask},
+    {"wait",	(fint_t)dowait},
     {0,0}
 };
 
@@ -662,7 +684,7 @@
 static struct env e ={line, iostack, iostack-1, (xint *)NULL, FDBASE, (struct env *)NULL};
 static void (*qflag)(int) = SIG_IGN;
 static char	shellname[] = "/bin/sh";
-static char	search[] = ":/bin:/usr/bin";
+static char	search[] = "/usr/bin:/bin:/usr/sbin:/sbin";
 static	int	startl;
 static	int	peeksym;
 static	int	nlseen;
@@ -698,7 +720,7 @@
 	register char *s;
 	int cflag;
 	char *name, **ap;
-	int (*iof)();
+	fint_t iof;
 
 	initarea();
 	if ((ap = environ) != NULL) {
@@ -746,7 +768,7 @@
 #endif
 		setval(cprompt, "> ");
 
-	iof = filechar;
+	iof = (fint_t)filechar;
 	cflag = 0;
 	name = *argv++;
 	if (--argc >= 1) {
@@ -760,7 +782,7 @@
 					setval(cprompt, "");
 					cflag = 1;
 					if (--argc > 0)
-						PUSHIO(aword, *++argv, iof = nlchar);
+						PUSHIO(aword, *++argv, iof = (fint_t)nlchar);
 					break;
 	
 				case 'q':
@@ -774,7 +796,7 @@
 				case 't':
 					prompt->status &= ~EXPORT;
 					setval(prompt, "");
-					iof = linechar;
+					iof = (fint_t)linechar;
 					break;
 	
 				case 'i':
@@ -787,7 +809,7 @@
 			argv--;
 			argc++;
 		}
-		if (iof == filechar && --argc > 0) {
+		if (iof == (fint_t)filechar && --argc > 0) {
 			setval(prompt, "");
 			setval(cprompt, "");
 			prompt->status &= ~EXPORT;
@@ -900,8 +922,8 @@
 	inparse = 1;
 	intr = 0;
 	execflg = 0;
-	setjmp(failpt = m1);	/* Bruce Evans' fix */
-	if (setjmp(failpt = m1) || yyparse() || intr) {
+	setjmp(*(failpt = &m1));	/* Bruce Evans' fix */
+	if (setjmp(*failpt) || yyparse() || intr) {
 		while (e.oenv)
 			quitenv();
 		scraphere();
@@ -930,7 +952,7 @@
 static void
 fail()
 {
-	longjmp(failpt, 1);
+	longjmp(*failpt, 1);
 	/* NOTREACHED */
 }
 
@@ -969,7 +991,7 @@
 	if (!interactive)
 		leave();
 	if (e.errpt)
-		longjmp(e.errpt, 1);
+		longjmp(*e.errpt, 1);
 	closeall();
 	e.iop = e.iobase = iostack;
 }
@@ -1075,7 +1097,7 @@
 next(f)
 int f;
 {
-	PUSHIO(afile, f, filechar);
+	PUSHIO(afile, f, (fint_t)filechar);
 }
 
 static void
@@ -2476,7 +2498,7 @@
 forkexec( register struct op *t, int *pin, int *pout, int act, char **wp, int *pforked)
 {
 	int i, rv;
-	int (*shcom)() = NULL;
+	fint_t shcom = NULL;
 	register int f;
 	char *cp = NULL;
 	struct ioword **iopp;
@@ -2587,7 +2609,7 @@
 		closepipe(pout);
 	}
 	if ((iopp = t->ioact) != NULL) {
-		if (shcom != NULL && shcom != doexec) {
+		if (shcom != NULL && shcom != (fint_t)doexec) {
 			prs(cp);
 			err(": cannot redirect shell command");
 			return(-1);
@@ -2597,7 +2619,7 @@
 				return(rv);
 	}
 	if (shcom)
-		return(setstatus((*shcom)(t)));
+		return(setstatus((*(fint_op_t)shcom)(t)));
 	/* should use FIOCEXCL */
 	for (i=FDBASE; i<NOFILE; i++)
 		close(i);
@@ -2899,7 +2921,7 @@
 static int
 run(argp, f)
 struct ioarg *argp;
-int (*f)();
+fint_t f;
 {
 	struct op *otree;
 	struct wdblock *swdlist;
@@ -2919,13 +2941,13 @@
 	otree = outtree;
 	ofail = failpt;
 	rv = -1;
-	if (newenv(setjmp(errpt = ev)) == 0) {
+	if (newenv(setjmp(*(errpt = &ev))) == 0) {
 		wdlist = 0;
 		iolist = 0;
 		pushio(argp, f);
 		e.iobase = e.iop;
 		yynerrs = 0;
-		if (setjmp(failpt = rt) == 0 && yyparse() == 0)
+		if (setjmp(*(failpt = &rt)) == 0 && yyparse() == 0)
 			rv = execute(outtree, NOPIPE, NOPIPE, 0);
 		quitenv();
 	}
@@ -3081,7 +3103,7 @@
 		return(1);
 	execflg = 1;
 	ofail = failpt;
-	if (setjmp(failpt = ex) == 0)
+	if (setjmp(*(failpt = &ex)) == 0)
 		execute(t, NOPIPE, NOPIPE, FEXEC);
 	failpt = ofail;
 	execflg = 0;
@@ -3224,7 +3246,7 @@
 }
 
 static void
-setsig( register int n, void (*f)(int))
+setsig(register int n, fvoid_int_t f)
 {
 	if (n == 0)
 		return;
@@ -3328,7 +3350,7 @@
 static void
 rdexp(wp, f, key)
 register char **wp;
-void (*f)();
+fvoid_varp_t f;
 int key;
 {
 	if (*wp != NULL) {
@@ -3438,7 +3460,7 @@
 }
 
 
-static int (*inbuilt(char *s))()
+static fint_t inbuilt(char *s)
 {
 	const struct builtincmd *bp;
 
@@ -3446,7 +3468,7 @@
 		if (strcmp(bp->name, s) == 0)
 			return(bp->builtinfunc);
 
-	return((int(*)())NULL);
+	return((fint_t)NULL);
 }
 
 /* -------- eval.c -------- */
@@ -3474,7 +3496,7 @@
 	wp = NULL;
 	wb = NULL;
 	wf = NULL;
-	if (newenv(setjmp(errpt = ev)) == 0) {
+	if (newenv(setjmp(*(errpt = &ev))) == 0) {
 		while (*ap && isassign(*ap))
 			expand(*ap++, &wb, f & ~DOGLOB);
 		if (flag['k']) {
@@ -3553,7 +3575,7 @@
 		*wbp = addword(cp, *wbp);
 		return(1);
 	}
-	if (newenv(setjmp(errpt = ev)) == 0) {
+	if (newenv(setjmp(*(errpt = &ev))) == 0) {
 		PUSHIO(aword, cp, strchar);
 		e.iobase = e.iop;
 		while ((cp = blank(f)) && gflg == 0) {
@@ -4282,7 +4304,7 @@
 static void
 pushio(argp, fn)
 struct ioarg *argp;
-int (*fn)();
+fint_t fn;
 {
 	if (++e.iop >= &iostack[NPUSH]) {
 		e.iop--;
@@ -4290,7 +4312,7 @@
 		gflg++;
 		return;
 	}
-	e.iop->iofn = fn;
+	e.iop->iofn = (fint_ioargp_iop_t) fn;
 
 	if (argp->afid != AFID_NOBUF)
 	  e.iop->argp = argp;
@@ -4311,9 +4333,9 @@
 	e.iop->peekc = 0;
 	e.iop->xchar = 0;
 	e.iop->nlcount = 0;
-	if (fn == filechar || fn == linechar)
+	if (fn == (fint_t)filechar || fn == (fint_t)linechar)
 		e.iop->task = XIO;
-	else if (fn == gravechar || fn == qgravechar)
+	else if (fn == (fint_t)gravechar || fn == (fint_t)qgravechar)
 		e.iop->task = XGRAVE;
 	else
 		e.iop->task = XOTHER;
@@ -4714,10 +4736,10 @@
 	if (tf < 0)
 		return;
 	*name = strsave(tname, areanum);
-	if (newenv(setjmp(errpt = ev)) != 0)
+	if (newenv(setjmp(*(errpt = &ev))) != 0)
 		unlink(tname);
 	else {
-		pushio(e.iop->argp, e.iop->iofn);
+		pushio(e.iop->argp, (fint_t)(e.iop->iofn));
 		e.iobase = e.iop;
 		for (;;) {
 		    if (interactive && e.iop <= iostack) {
@@ -4780,7 +4802,7 @@
 		tf = mkstemp(tname);
 		if (tf < 0)
 			return (-1);
-		if (newenv(setjmp(errpt = ev)) == 0) {
+		if (newenv(setjmp(*(errpt = &ev))) == 0) {
 			PUSHIO(afile, hf, herechar);
 			setbase(e.iop);
 			while ((c = subgetc(0, 0)) != 0) {


More information about the busybox mailing list