Skip to content

Commit

Permalink
Various portability fixes.
Browse files Browse the repository at this point in the history
Compiler portability:

- Do not use gcc's -include option.
  * Include "config.h" in each source file.
  * Update generators to emit #include "config.h" too.
- Do not use gcc's -x option.
  * Use $(CPP) - instead, reading input from stdin.
- Do not hard-code gcc's -Wall option.
  * Use autoconf-archive's AX_CFLAGS_WARN_ALL instead.
- Do not include unused host headers in build utilities.

Standard library compatibility:

- Do not include traditional but non-standard headers <sys/ioctl.h>,
  <sys/param.h>.
  * Change MAXPATHLEN to PATH_MAX.
- Check header files for missing #includes that may be implicitly included
  by other headers.
  * As this causes some header files to be included multiple times, add
    missing header guards to all files.

Make compatibility:

- Do not use $(patsubst), $< and $^, and static pattern rules in Makefiles.
  • Loading branch information
hvdijk committed Jun 27, 2019
1 parent 7efcad5 commit 49b77c7
Show file tree
Hide file tree
Showing 64 changed files with 254 additions and 34 deletions.
9 changes: 7 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dnl Checks for programs.
AC_PROG_CC
AX_PROG_CC_FOR_BUILD
AC_USE_SYSTEM_EXTENSIONS
AX_CFLAGS_WARN_ALL
AC_SYS_LARGEFILE
AC_PROG_YACC

Expand Down Expand Up @@ -167,8 +168,12 @@ if test "$enable_locale" != "no"; then
[AS_HELP_STRING([--disable-parser-locale],
[Disable use of fixed locale during parsing.])])
if test "$enable_parser_locale" != "no"; then
AC_DEFINE([WITH_PARSER_LOCALE], [1],
[Define if you want parsing to ignore LC_CTYPE changes after the shell starts.])
AC_CHECK_FUNC([uselocale],
[AC_DEFINE([WITH_PARSER_LOCALE], [1],
[Define if you want parsing to ignore LC_CTYPE changes after the shell starts.])],
[if test "$enable_parser_locale" = "yes"; then
AC_MSG_ERROR([--enable-parser-locale requires the uselocale() function.])
fi])
fi
fi
AC_CONFIG_FILES([Makefile src/Makefile])
Expand Down
34 changes: 18 additions & 16 deletions src/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
AM_YFLAGS = -d

COMMON_CFLAGS = -Wall
COMMON_CPPFLAGS = \
-DBSD=1 -DSHELL
COMMON_CPPFLAGS = -DBSD -DSHELL

AM_CFLAGS = $(COMMON_CFLAGS)
AM_CPPFLAGS = -include $(top_builddir)/config.h $(COMMON_CPPFLAGS)
AM_CFLAGS_FOR_BUILD = -g -O2 $(COMMON_CFLAGS)
AM_CPPFLAGS = $(COMMON_CPPFLAGS)
AM_CPPFLAGS_FOR_BUILD = $(COMMON_CPPFLAGS)

COMPILE_FOR_BUILD = \
Expand Down Expand Up @@ -34,8 +30,8 @@ HELPERS = mkinit mksyntax mknodes

BUILT_SOURCES = builtins.h nodes.h syntax.h token.h token_vars.h
CLEANFILES = \
$(BUILT_SOURCES) $(patsubst %.o,%.c,$(gwsh_LDADD)) \
$(HELPERS) builtins.def
builtins.def builtins.c builtins.h init.c nodes.c nodes.h \
syntax.c syntax.h token.h token_vars.h $(HELPERS)

man_MANS = gwsh.1
EXTRA_DIST = \
Expand All @@ -44,24 +40,30 @@ EXTRA_DIST = \
mknodes.c nodetypes nodes.c.pat mksyntax.c

token.h token_vars.h: mktokens
$(AM_V_GEN)$(SHELL) $^
$(AM_V_GEN)$(SHELL) $(srcdir)/mktokens

builtins.def: builtins.def.in $(top_builddir)/config.h
$(AM_V_CC)$(COMPILE) -E -x c -o $@ $<
$(AM_V_CC)$(CPP) $(DEFAULT_INCLUDES) - < $(srcdir)/builtins.def.in > $@.tmp && mv $@.tmp $@

builtins.c builtins.h: mkbuiltins builtins.def
$(AM_V_GEN)$(SHELL) $^
$(AM_V_GEN)$(SHELL) $(srcdir)/mkbuiltins builtins.def

init.c: mkinit $(gwsh_CFILES)
$(AM_V_GEN)./$^
$(AM_V_GEN)./mkinit `echo " $(gwsh_CFILES)" | sed -e 's: : $(srcdir)/:g'`

nodes.c nodes.h: mknodes nodetypes nodes.c.pat
$(AM_V_GEN)./$^
$(AM_V_GEN)./mknodes $(srcdir)/nodetypes $(srcdir)/nodes.c.pat

syntax.c syntax.h: mksyntax
$(AM_V_GEN)./$^
$(AM_V_GEN)./mksyntax

mksyntax: token.h

$(HELPERS): %: %.c
$(AM_V_CC)$(COMPILE_FOR_BUILD) -o $@ $<
CCBUILD = $(AM_V_CC)$(COMPILE_FOR_BUILD) -o $@

mkinit: mkinit.c
$(CCBUILD) $(srcdir)/mkinit.c
mknodes: mknodes.c
$(CCBUILD) $(srcdir)/mknodes.c
mksyntax: mksyntax.c
$(CCBUILD) $(srcdir)/mksyntax.c
2 changes: 2 additions & 0 deletions src/alias.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* SUCH DAMAGE.
*/

#include "config.h"

#include <stdlib.h>
#include "shell.h"
#include "input.h"
Expand Down
5 changes: 5 additions & 0 deletions src/alias.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
* @(#)alias.h 8.2 (Berkeley) 5/4/95
*/

#ifndef H_ALIAS
#define H_ALIAS 1

#define ALIASINUSE 1
#define ALIASDEAD 2

Expand All @@ -56,3 +59,5 @@ void rmaliases(void);
int unalias(const char *);
void printalias(const struct alias *);
void endaliasuse(void);

#endif
3 changes: 3 additions & 0 deletions src/arith_yacc.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@
* SUCH DAMAGE.
*/

#include "config.h"

#include <inttypes.h>
#include <stdlib.h>
#include <limits.h>
#include "arith_yacc.h"
#include "expand.h"
#include "shell.h"
Expand Down
7 changes: 7 additions & 0 deletions src/arith_yacc.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
* SUCH DAMAGE.
*/

#ifndef H_ARITH_YACC
#define H_ARITH_YACC 1

#include <stdint.h>

#define ARITH_ASS 1

#define ARITH_OR 2
Expand Down Expand Up @@ -87,3 +92,5 @@ union yystype {
extern union yystype yylval;

int yylex(void);

#endif
2 changes: 2 additions & 0 deletions src/arith_yylex.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* SUCH DAMAGE.
*/

#include "config.h"

#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
Expand Down
2 changes: 2 additions & 0 deletions src/bltin/printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
* SUCH DAMAGE.
*/

#include "config.h"

#include <sys/types.h>

#include <ctype.h>
Expand Down
2 changes: 2 additions & 0 deletions src/bltin/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* This program is in the Public Domain.
*/

#include "config.h"

#include <sys/stat.h>
#include <sys/types.h>

Expand Down
2 changes: 2 additions & 0 deletions src/bltin/times.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* This file contains code for the times builtin.
*/

#include "config.h"

#include <sys/times.h>
#include <unistd.h>
#ifdef USE_GLIBC_STDIO
Expand Down
2 changes: 2 additions & 0 deletions src/builtins.def.in
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
* @(#)builtins.def 8.4 (Berkeley) 5/4/95
*/

#include "config.h"

/*
* This file lists all the builtin commands. The first column is the name
* of a C routine.
Expand Down
2 changes: 2 additions & 0 deletions src/cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* SUCH DAMAGE.
*/

#include "config.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
Expand Down
5 changes: 5 additions & 0 deletions src/cd.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
*
*/

#ifndef H_CD
#define H_CD 1

int cdcmd(int, char **);
int pwdcmd(int, char **);
void setpwd(const char *, int);

#endif
2 changes: 2 additions & 0 deletions src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
* SUCH DAMAGE.
*/

#include "config.h"

/*
* Errors and exceptions.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
* @(#)error.h 8.2 (Berkeley) 5/4/95
*/

#ifndef H_ERROR
#define H_ERROR 1

#include <setjmp.h>
#include <signal.h>

Expand Down Expand Up @@ -129,3 +132,5 @@ void exerror(int, const char *, ...) __attribute__((__noreturn__));
const char *errmsg(int, int);

void sh_warnx(const char *, ...);

#endif
2 changes: 2 additions & 0 deletions src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* SUCH DAMAGE.
*/

#include "config.h"

#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
Expand Down
5 changes: 5 additions & 0 deletions src/eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
* @(#)eval.h 8.2 (Berkeley) 5/4/95
*/

#ifndef H_EVAL
#define H_EVAL 1

extern char *dotfile; /* currently executing . file */
extern char *commandname; /* currently executing command */
extern int exitstatus; /* exit status of last command */
Expand Down Expand Up @@ -64,3 +67,5 @@ extern int evalskip;
#define SKIPBREAK (1 << 0)
#define SKIPCONT (1 << 1)
#define SKIPFUNC (1 << 2)

#endif
2 changes: 2 additions & 0 deletions src/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* SUCH DAMAGE.
*/

#include "config.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
Expand Down
7 changes: 7 additions & 0 deletions src/exec.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
* @(#)exec.h 8.3 (Berkeley) 6/8/95
*/

#ifndef H_EXEC
#define H_EXEC 1

/* values of cmdtype */
#define CMDUNKNOWN -1 /* no entry in table for command */
#define CMDNORMAL 0 /* command is an executable program */
Expand All @@ -60,6 +63,8 @@ struct cmdentry {

extern const char *pathopt; /* set by padvance */

union node;

void shellexec(char **, const char *, int)
__attribute__((__noreturn__));
int padvance(const char **, const char *);
Expand All @@ -75,3 +80,5 @@ void defun(union node *);
void unsetfunc(const char *);
int typecmd(int, char **);
int commandcmd(int, char **);

#endif
2 changes: 2 additions & 0 deletions src/expand.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* SUCH DAMAGE.
*/

#include "config.h"

#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
Expand Down
5 changes: 5 additions & 0 deletions src/expand.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
* @(#)expand.h 8.2 (Berkeley) 5/4/95
*/

#ifndef H_EXPAND
#define H_EXPAND 1

#include <inttypes.h>

struct strlist {
Expand Down Expand Up @@ -83,3 +86,5 @@ void arith_lex_reset(void);
#define arith_lex_reset()
#endif
int yylex(void);

#endif
5 changes: 3 additions & 2 deletions src/histedit.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
* SUCH DAMAGE.
*/

#include <sys/param.h>
#include "config.h"

#ifdef HAVE_PATHS_H
#include <paths.h>
#endif
Expand Down Expand Up @@ -211,7 +212,7 @@ histcmd(int argc, char **argv)
static int active = 0;
struct jmploc jmploc;
struct jmploc *volatile savehandler;
char editfile[MAXPATHLEN + 1];
char editfile[PATH_MAX + 1];
FILE *efp;
#ifdef __GNUC__
/* Avoid longjmp clobbering */
Expand Down
5 changes: 5 additions & 0 deletions src/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@
* @(#)init.h 8.2 (Berkeley) 5/4/95
*/

#ifndef H_INIT
#define H_INIT 1

void init(void);
void exitreset(void);
void envreset(void);
void reset(void);

#endif
2 changes: 2 additions & 0 deletions src/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* SUCH DAMAGE.
*/

#include "config.h"

#include <stdio.h> /* defines BUFSIZ */
#ifdef WITH_LOCALE
#include <wchar.h>
Expand Down
9 changes: 9 additions & 0 deletions src/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@
* @(#)input.h 8.2 (Berkeley) 5/4/95
*/

#ifndef H_INPUT
#define H_INPUT 1

#include "config.h"

#include <stddef.h>
#ifdef WITH_LOCALE
#include <limits.h>
#include <locale.h>
#endif
#if defined(WITH_PARSER_LOCALE) && defined(HAVE_XLOCALE_H)
Expand Down Expand Up @@ -120,3 +127,5 @@ void popfile(void);
void unwindfiles(struct parsefile *);
void popallfiles(void);
void closescript(void);

#endif
Loading

0 comments on commit 49b77c7

Please sign in to comment.