1996-09-18 05:35:50 +00:00
|
|
|
|
/* fix-header.c - Make C header file suitable for C++.
|
2002-02-01 18:16:02 +00:00
|
|
|
|
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998,
|
|
|
|
|
1999, 2000, 2001 Free Software Foundation, Inc.
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it
|
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
|
Free Software Foundation; either version 2, or (at your option) any
|
|
|
|
|
later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
|
|
|
|
|
|
|
|
|
/* This program massages a system include file (such as stdio.h),
|
1999-08-26 09:30:50 +00:00
|
|
|
|
into a form that is compatible with GNU C and GNU C++.
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
|
|
* extern "C" { ... } braces are added (inside #ifndef __cplusplus),
|
|
|
|
|
if they seem to be needed. These prevent C++ compilers from name
|
|
|
|
|
mangling the functions inside the braces.
|
|
|
|
|
|
|
|
|
|
* If an old-style incomplete function declaration is seen (without
|
|
|
|
|
an argument list), and it is a "standard" function listed in
|
|
|
|
|
the file sys-protos.h (and with a non-empty argument list), then
|
|
|
|
|
the declaration is converted to a complete prototype by replacing
|
1999-08-26 09:30:50 +00:00
|
|
|
|
the empty parameter list with the argument list from sys-protos.h.
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
|
|
* The program can be given a list of (names of) required standard
|
|
|
|
|
functions (such as fclose for stdio.h). If a required function
|
|
|
|
|
is not seen in the input, then a prototype for it will be
|
|
|
|
|
written to the output.
|
|
|
|
|
|
|
|
|
|
* If all of the non-comment code of the original file is protected
|
|
|
|
|
against multiple inclusion:
|
|
|
|
|
#ifndef FOO
|
|
|
|
|
#define FOO
|
|
|
|
|
<body of include file>
|
|
|
|
|
#endif
|
|
|
|
|
then extra matter added to the include file is placed inside the <body>.
|
|
|
|
|
|
|
|
|
|
* If the input file is OK (nothing needs to be done);
|
|
|
|
|
the output file is not written (nor removed if it exists).
|
|
|
|
|
|
|
|
|
|
There are also some special actions that are done for certain
|
|
|
|
|
well-known standard include files:
|
|
|
|
|
|
|
|
|
|
* If argv[1] is "sys/stat.h", the Posix.1 macros
|
|
|
|
|
S_ISBLK, S_ISCHR, S_ISDIR, S_ISFIFO, S_ISLNK, S_ISREG are added if
|
|
|
|
|
they were missing, and the corresponding "traditional" S_IFxxx
|
|
|
|
|
macros were defined.
|
|
|
|
|
|
|
|
|
|
* If argv[1] is "errno.h", errno is declared if it was missing.
|
|
|
|
|
|
|
|
|
|
* TODO: The input file should be read complete into memory, because:
|
|
|
|
|
a) it needs to be scanned twice anyway, and
|
|
|
|
|
b) it would be nice to allow update in place.
|
|
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
fix-header FOO.H INFILE.H OUTFILE.H [OPTIONS]
|
|
|
|
|
where:
|
|
|
|
|
* FOO.H is the relative file name of the include file,
|
|
|
|
|
as it would be #include'd by a C file. (E.g. stdio.h)
|
|
|
|
|
* INFILE.H is a full pathname for the input file (e.g. /usr/include/stdio.h)
|
|
|
|
|
* OUTFILE.H is the full pathname for where to write the output file,
|
|
|
|
|
if anything needs to be done. (e.g. ./include/stdio.h)
|
|
|
|
|
* OPTIONS are such as you would pass to cpp.
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
Written by Per Bothner <bothner@cygnus.com>, July 1993. */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
|
|
#include "hconfig.h"
|
1999-08-26 09:30:50 +00:00
|
|
|
|
#include "system.h"
|
1996-09-18 05:35:50 +00:00
|
|
|
|
#include "obstack.h"
|
|
|
|
|
#include "scan.h"
|
|
|
|
|
#include "cpplib.h"
|
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
static void v_fatal PARAMS ((const char *, va_list)) ATTRIBUTE_PRINTF (1,0) ATTRIBUTE_NORETURN;
|
|
|
|
|
static void fatal PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
|
|
sstring buf;
|
|
|
|
|
|
|
|
|
|
int verbose = 0;
|
|
|
|
|
int partial_count = 0;
|
|
|
|
|
int warnings = 0;
|
|
|
|
|
|
|
|
|
|
/* We no longer need to add extern "C", because cpp implicitly
|
|
|
|
|
forces the standard include files to be treated as C. */
|
|
|
|
|
/*#define ADD_MISSING_EXTERN_C 1 */
|
|
|
|
|
|
|
|
|
|
#if ADD_MISSING_EXTERN_C
|
|
|
|
|
int missing_extern_C_count = 0;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "xsys-protos.h"
|
|
|
|
|
|
|
|
|
|
#ifdef FIXPROTO_IGNORE_LIST
|
1999-08-26 09:30:50 +00:00
|
|
|
|
/* This is a currently unused feature. */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
|
|
/* List of files and directories to ignore.
|
|
|
|
|
A directory name (ending in '/') means ignore anything in that
|
|
|
|
|
directory. (It might be more efficient to do directory pruning
|
|
|
|
|
earlier in fixproto, but this is simpler and easier to customize.) */
|
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
static const char *const files_to_ignore[] = {
|
1996-09-18 05:35:50 +00:00
|
|
|
|
"X11/",
|
|
|
|
|
FIXPROTO_IGNORE_LIST
|
|
|
|
|
0
|
|
|
|
|
};
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
char *inf_buffer;
|
|
|
|
|
char *inf_limit;
|
|
|
|
|
char *inf_ptr;
|
2002-02-01 18:16:02 +00:00
|
|
|
|
static const char *cur_file;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
|
|
/* Certain standard files get extra treatment */
|
|
|
|
|
|
|
|
|
|
enum special_file
|
|
|
|
|
{
|
|
|
|
|
no_special,
|
1999-08-26 09:30:50 +00:00
|
|
|
|
#ifdef errno_h
|
|
|
|
|
#undef errno_h
|
|
|
|
|
#endif
|
1996-09-18 05:35:50 +00:00
|
|
|
|
errno_h,
|
1999-08-26 09:30:50 +00:00
|
|
|
|
#ifdef stdio_h
|
|
|
|
|
#undef stdio_h
|
|
|
|
|
#endif
|
1996-09-18 05:35:50 +00:00
|
|
|
|
stdio_h,
|
1999-08-26 09:30:50 +00:00
|
|
|
|
#ifdef stdlib_h
|
|
|
|
|
#undef stdlib_h
|
|
|
|
|
#endif
|
|
|
|
|
stdlib_h,
|
|
|
|
|
#ifdef sys_stat_h
|
|
|
|
|
#undef sys_stat_h
|
|
|
|
|
#endif
|
1996-09-18 05:35:50 +00:00
|
|
|
|
sys_stat_h
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* A NAMELIST is a sequence of names, separated by '\0', and terminated
|
1999-08-26 09:30:50 +00:00
|
|
|
|
by an empty name (i.e. by "\0\0"). */
|
|
|
|
|
|
|
|
|
|
typedef const char *namelist;
|
|
|
|
|
|
|
|
|
|
/* The following macros provide the bits for symbol_flags. */
|
|
|
|
|
typedef int symbol_flags;
|
|
|
|
|
|
|
|
|
|
/* Used to mark names defined in the ANSI/ISO C standard. */
|
|
|
|
|
#define ANSI_SYMBOL 1
|
|
|
|
|
|
|
|
|
|
/* We no longer massage include files for POSIX or XOPEN symbols,
|
|
|
|
|
as there are now several versions of the POSIX and XOPEN standards,
|
|
|
|
|
and it would be a maintenance nightmare for us to track them all.
|
|
|
|
|
Better to be compatible with the system include files. */
|
|
|
|
|
/*#define ADD_MISSING_POSIX 1 */
|
|
|
|
|
/*#define ADD_MISSING_XOPEN 1 */
|
|
|
|
|
|
|
|
|
|
#if ADD_MISSING_POSIX
|
|
|
|
|
/* Used to mark names defined in the Posix.1 or Posix.2 standard. */
|
|
|
|
|
#define POSIX1_SYMBOL 2
|
|
|
|
|
#define POSIX2_SYMBOL 4
|
|
|
|
|
#else
|
|
|
|
|
#define POSIX1_SYMBOL 0
|
|
|
|
|
#define POSIX2_SYMBOL 0
|
|
|
|
|
#endif
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
#if ADD_MISSING_XOPEN
|
|
|
|
|
/* Used to mark names defined in X/Open Portability Guide. */
|
|
|
|
|
#define XOPEN_SYMBOL 8
|
|
|
|
|
/* Used to mark names defined in X/Open UNIX Extensions. */
|
|
|
|
|
#define XOPEN_EXTENDED_SYMBOL 16
|
|
|
|
|
#else
|
|
|
|
|
#define XOPEN_SYMBOL 0
|
|
|
|
|
#define XOPEN_EXTENDED_SYMBOL 0
|
|
|
|
|
#endif
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
/* Used to indicate names that are not functions */
|
|
|
|
|
#define MACRO_SYMBOL 512
|
|
|
|
|
|
|
|
|
|
struct symbol_list {
|
|
|
|
|
symbol_flags flags;
|
|
|
|
|
namelist names;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
};
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
#define SYMBOL_TABLE_SIZE 10
|
|
|
|
|
struct symbol_list symbol_table[SYMBOL_TABLE_SIZE];
|
|
|
|
|
int cur_symbol_table_size;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
static void add_symbols PARAMS ((symbol_flags, namelist));
|
|
|
|
|
static struct fn_decl *lookup_std_proto PARAMS ((const char *, int));
|
|
|
|
|
static void write_lbrac PARAMS ((void));
|
|
|
|
|
static void recognized_macro PARAMS ((const char *));
|
|
|
|
|
static void check_macro_names PARAMS ((cpp_reader *, namelist));
|
|
|
|
|
static void read_scan_file PARAMS ((char *, int, char **));
|
|
|
|
|
static void write_rbrac PARAMS ((void));
|
|
|
|
|
static int inf_skip_spaces PARAMS ((int));
|
|
|
|
|
static int inf_read_upto PARAMS ((sstring *, int));
|
|
|
|
|
static int inf_scan_ident PARAMS ((sstring *, int));
|
|
|
|
|
static int check_protection PARAMS ((int *, int *));
|
|
|
|
|
static void cb_file_change PARAMS ((cpp_reader *, const struct line_map *));
|
|
|
|
|
|
|
|
|
|
static void
|
1999-08-26 09:30:50 +00:00
|
|
|
|
add_symbols (flags, names)
|
|
|
|
|
symbol_flags flags;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
namelist names;
|
|
|
|
|
{
|
1999-08-26 09:30:50 +00:00
|
|
|
|
symbol_table[cur_symbol_table_size].flags = flags;
|
|
|
|
|
symbol_table[cur_symbol_table_size].names = names;
|
|
|
|
|
cur_symbol_table_size++;
|
|
|
|
|
if (cur_symbol_table_size >= SYMBOL_TABLE_SIZE)
|
|
|
|
|
fatal ("too many calls to add_symbols");
|
|
|
|
|
symbol_table[cur_symbol_table_size].names = NULL; /* Termination. */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
struct std_include_entry {
|
2002-02-01 18:16:02 +00:00
|
|
|
|
const char *const name;
|
|
|
|
|
const symbol_flags flags;
|
|
|
|
|
const namelist names;
|
1999-08-26 09:30:50 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const char NONE[] = ""; /* The empty namelist. */
|
|
|
|
|
|
|
|
|
|
/* Special name to indicate a continuation line in std_include_table. */
|
|
|
|
|
const char CONTINUED[] = "";
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
const struct std_include_entry *include_entry;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
const struct std_include_entry std_include_table [] = {
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{ "ctype.h", ANSI_SYMBOL,
|
1996-09-18 05:35:50 +00:00
|
|
|
|
"isalnum\0isalpha\0iscntrl\0isdigit\0isgraph\0islower\0\
|
1999-08-26 09:30:50 +00:00
|
|
|
|
isprint\0ispunct\0isspace\0isupper\0isxdigit\0tolower\0toupper\0" },
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{ "dirent.h", POSIX1_SYMBOL, "closedir\0opendir\0readdir\0rewinddir\0"},
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{ "errno.h", ANSI_SYMBOL|MACRO_SYMBOL, "errno\0" },
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
/* ANSI_SYMBOL is wrong, but ... */
|
|
|
|
|
{ "curses.h", ANSI_SYMBOL, "box\0delwin\0endwin\0getcurx\0getcury\0initscr\0\
|
1996-09-18 05:35:50 +00:00
|
|
|
|
mvcur\0mvwprintw\0mvwscanw\0newwin\0overlay\0overwrite\0\
|
|
|
|
|
scroll\0subwin\0touchwin\0waddstr\0wclear\0wclrtobot\0wclrtoeol\0\
|
|
|
|
|
waddch\0wdelch\0wdeleteln\0werase\0wgetch\0wgetstr\0winsch\0winsertln\0\
|
1999-08-26 09:30:50 +00:00
|
|
|
|
wmove\0wprintw\0wrefresh\0wscanw\0wstandend\0wstandout\0" },
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{ "fcntl.h", POSIX1_SYMBOL, "creat\0fcntl\0open\0" },
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
|
|
/* Maybe also "getgrent fgetgrent setgrent endgrent" */
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{ "grp.h", POSIX1_SYMBOL, "getgrgid\0getgrnam\0" },
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
|
|
/*{ "limit.h", ... provided by gcc }, */
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{ "locale.h", ANSI_SYMBOL, "localeconv\0setlocale\0" },
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{ "math.h", ANSI_SYMBOL,
|
|
|
|
|
"acos\0asin\0atan\0atan2\0ceil\0cos\0cosh\0exp\0\
|
1996-09-18 05:35:50 +00:00
|
|
|
|
fabs\0floor\0fmod\0frexp\0ldexp\0log10\0log\0modf\0pow\0sin\0sinh\0sqrt\0\
|
1999-08-26 09:30:50 +00:00
|
|
|
|
tan\0tanh\0" },
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{ CONTINUED, ANSI_SYMBOL|MACRO_SYMBOL, "HUGE_VAL\0" },
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{ "pwd.h", POSIX1_SYMBOL, "getpwnam\0getpwuid\0" },
|
|
|
|
|
|
|
|
|
|
/* Left out siglongjmp sigsetjmp - these depend on sigjmp_buf. */
|
|
|
|
|
{ "setjmp.h", ANSI_SYMBOL, "longjmp\0setjmp\0" },
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
|
|
/* Left out signal() - its prototype is too complex for us!
|
|
|
|
|
Also left out "sigaction sigaddset sigdelset sigemptyset
|
|
|
|
|
sigfillset sigismember sigpending sigprocmask sigsuspend"
|
|
|
|
|
because these need sigset_t or struct sigaction.
|
1999-08-26 09:30:50 +00:00
|
|
|
|
Most systems that provide them will also declare them. */
|
|
|
|
|
{ "signal.h", ANSI_SYMBOL, "kill\0raise\0" },
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{ "stdio.h", ANSI_SYMBOL,
|
|
|
|
|
"clearerr\0fclose\0feof\0ferror\0fflush\0fgetc\0fgetpos\0\
|
1996-09-18 05:35:50 +00:00
|
|
|
|
fgets\0fopen\0fprintf\0fputc\0fputs\0fread\0freopen\0fscanf\0fseek\0\
|
1999-08-26 09:30:50 +00:00
|
|
|
|
fsetpos\0ftell\0fwrite\0getc\0getchar\0gets\0perror\0\
|
1996-09-18 05:35:50 +00:00
|
|
|
|
printf\0putc\0putchar\0puts\0remove\0rename\0rewind\0scanf\0setbuf\0\
|
|
|
|
|
setvbuf\0sprintf\0sscanf\0vprintf\0vsprintf\0vfprintf\0tmpfile\0\
|
1999-08-26 09:30:50 +00:00
|
|
|
|
tmpnam\0ungetc\0" },
|
|
|
|
|
{ CONTINUED, POSIX1_SYMBOL, "fdopen\0fileno\0" },
|
|
|
|
|
{ CONTINUED, POSIX2_SYMBOL, "pclose\0popen\0" }, /* I think ... */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
/* Should perhaps also handle NULL, EOF, ... ? */
|
|
|
|
|
|
|
|
|
|
/* "div ldiv", - ignored because these depend on div_t, ldiv_t
|
|
|
|
|
ignore these: "mblen mbstowcs mbstowc wcstombs wctomb"
|
|
|
|
|
Left out getgroups, because SunOS4 has incompatible BSD and SVR4 versions.
|
|
|
|
|
Should perhaps also add NULL */
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{ "stdlib.h", ANSI_SYMBOL,
|
|
|
|
|
"abort\0abs\0atexit\0atof\0atoi\0atol\0bsearch\0calloc\0\
|
1996-09-18 05:35:50 +00:00
|
|
|
|
exit\0free\0getenv\0labs\0malloc\0putenv\0qsort\0rand\0realloc\0\
|
1999-08-26 09:30:50 +00:00
|
|
|
|
srand\0strtod\0strtol\0strtoul\0system\0" },
|
|
|
|
|
{ CONTINUED, ANSI_SYMBOL|MACRO_SYMBOL, "EXIT_FAILURE\0EXIT_SUCCESS\0" },
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{ "string.h", ANSI_SYMBOL, "memchr\0memcmp\0memcpy\0memmove\0memset\0\
|
1996-09-18 05:35:50 +00:00
|
|
|
|
strcat\0strchr\0strcmp\0strcoll\0strcpy\0strcspn\0strerror\0\
|
|
|
|
|
strlen\0strncat\0strncmp\0strncpy\0strpbrk\0strrchr\0strspn\0strstr\0\
|
1999-08-26 09:30:50 +00:00
|
|
|
|
strtok\0strxfrm\0" },
|
1996-09-18 05:35:50 +00:00
|
|
|
|
/* Should perhaps also add NULL and size_t */
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{ "strings.h", XOPEN_EXTENDED_SYMBOL,
|
|
|
|
|
"bcmp\0bcopy\0bzero\0ffs\0index\0rindex\0strcasecmp\0strncasecmp\0" },
|
|
|
|
|
|
|
|
|
|
{ "strops.h", XOPEN_EXTENDED_SYMBOL, "ioctl\0" },
|
|
|
|
|
|
|
|
|
|
/* Actually, XPG4 does not seem to have <sys/ioctl.h>, but defines
|
|
|
|
|
ioctl in <strops.h>. However, many systems have it is sys/ioctl.h,
|
|
|
|
|
and many systems do have <sys/ioctl.h> but not <strops.h>. */
|
|
|
|
|
{ "sys/ioctl.h", XOPEN_EXTENDED_SYMBOL, "ioctl\0" },
|
|
|
|
|
|
|
|
|
|
{ "sys/socket.h", XOPEN_EXTENDED_SYMBOL, "socket\0" },
|
|
|
|
|
|
|
|
|
|
{ "sys/stat.h", POSIX1_SYMBOL,
|
|
|
|
|
"chmod\0fstat\0mkdir\0mkfifo\0stat\0lstat\0umask\0" },
|
|
|
|
|
{ CONTINUED, POSIX1_SYMBOL|MACRO_SYMBOL,
|
1996-09-18 05:35:50 +00:00
|
|
|
|
"S_ISDIR\0S_ISBLK\0S_ISCHR\0S_ISFIFO\0S_ISREG\0S_ISLNK\0S_IFDIR\0\
|
|
|
|
|
S_IFBLK\0S_IFCHR\0S_IFIFO\0S_IFREG\0S_IFLNK\0" },
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{ CONTINUED, XOPEN_EXTENDED_SYMBOL, "fchmod\0" },
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
/* How do we handle fd_set? */
|
|
|
|
|
{ "sys/time.h", XOPEN_EXTENDED_SYMBOL, "select\0" },
|
|
|
|
|
{ "sys/select.h", XOPEN_EXTENDED_SYMBOL /* fake */, "select\0" },
|
|
|
|
|
#endif
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{ "sys/times.h", POSIX1_SYMBOL, "times\0" },
|
1996-09-18 05:35:50 +00:00
|
|
|
|
/* "sys/types.h" add types (not in old g++-include) */
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{ "sys/utsname.h", POSIX1_SYMBOL, "uname\0" },
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{ "sys/wait.h", POSIX1_SYMBOL, "wait\0waitpid\0" },
|
|
|
|
|
{ CONTINUED, POSIX1_SYMBOL|MACRO_SYMBOL,
|
1996-09-18 05:35:50 +00:00
|
|
|
|
"WEXITSTATUS\0WIFEXITED\0WIFSIGNALED\0WIFSTOPPED\0WSTOPSIG\0\
|
|
|
|
|
WTERMSIG\0WNOHANG\0WNOTRACED\0" },
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{ "tar.h", POSIX1_SYMBOL, NONE },
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{ "termios.h", POSIX1_SYMBOL,
|
|
|
|
|
"cfgetispeed\0cfgetospeed\0cfsetispeed\0cfsetospeed\0tcdrain\0tcflow\0tcflush\0tcgetattr\0tcsendbreak\0tcsetattr\0" },
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{ "time.h", ANSI_SYMBOL,
|
|
|
|
|
"asctime\0clock\0ctime\0difftime\0gmtime\0localtime\0mktime\0strftime\0time\0tzset\0" },
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{ "unistd.h", POSIX1_SYMBOL,
|
|
|
|
|
"_exit\0access\0alarm\0chdir\0chown\0close\0ctermid\0cuserid\0\
|
1996-09-18 05:35:50 +00:00
|
|
|
|
dup\0dup2\0execl\0execle\0execlp\0execv\0execve\0execvp\0fork\0fpathconf\0\
|
1999-08-26 09:30:50 +00:00
|
|
|
|
getcwd\0getegid\0geteuid\0getgid\0getlogin\0getpgrp\0getpid\0\
|
1996-09-18 05:35:50 +00:00
|
|
|
|
getppid\0getuid\0isatty\0link\0lseek\0pathconf\0pause\0pipe\0read\0rmdir\0\
|
|
|
|
|
setgid\0setpgid\0setsid\0setuid\0sleep\0sysconf\0tcgetpgrp\0tcsetpgrp\0\
|
1999-08-26 09:30:50 +00:00
|
|
|
|
ttyname\0unlink\0write\0" },
|
|
|
|
|
{ CONTINUED, POSIX2_SYMBOL, "getopt\0" },
|
|
|
|
|
{ CONTINUED, XOPEN_EXTENDED_SYMBOL,
|
1999-10-16 06:09:09 +00:00
|
|
|
|
"lockf\0gethostid\0gethostname\0readlink\0symlink\0" },
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{ "utime.h", POSIX1_SYMBOL, "utime\0" },
|
|
|
|
|
|
|
|
|
|
{ NULL, 0, NONE }
|
1996-09-18 05:35:50 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum special_file special_file_handling = no_special;
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
/* They are set if the corresponding macro has been seen. */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
/* The following are only used when handling sys/stat.h */
|
|
|
|
|
int seen_S_IFBLK = 0, seen_S_ISBLK = 0;
|
|
|
|
|
int seen_S_IFCHR = 0, seen_S_ISCHR = 0;
|
|
|
|
|
int seen_S_IFDIR = 0, seen_S_ISDIR = 0;
|
|
|
|
|
int seen_S_IFIFO = 0, seen_S_ISFIFO = 0;
|
|
|
|
|
int seen_S_IFLNK = 0, seen_S_ISLNK = 0;
|
|
|
|
|
int seen_S_IFREG = 0, seen_S_ISREG = 0;
|
1999-08-26 09:30:50 +00:00
|
|
|
|
/* The following are only used when handling errno.h */
|
|
|
|
|
int seen_errno = 0;
|
|
|
|
|
/* The following are only used when handling stdlib.h */
|
|
|
|
|
int seen_EXIT_FAILURE = 0, seen_EXIT_SUCCESS = 0;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
|
|
#define obstack_chunk_alloc xmalloc
|
2002-02-01 18:16:02 +00:00
|
|
|
|
#define obstack_chunk_free free
|
1996-09-18 05:35:50 +00:00
|
|
|
|
struct obstack scan_file_obstack;
|
|
|
|
|
|
|
|
|
|
/* NOTE: If you edit this, also edit gen-protos.c !! */
|
1999-08-26 09:30:50 +00:00
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
static struct fn_decl *
|
1996-09-18 05:35:50 +00:00
|
|
|
|
lookup_std_proto (name, name_length)
|
|
|
|
|
const char *name;
|
|
|
|
|
int name_length;
|
|
|
|
|
{
|
2002-02-01 18:16:02 +00:00
|
|
|
|
int i = hashstr (name, name_length) % HASH_SIZE;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
int i0 = i;
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
struct fn_decl *fn;
|
|
|
|
|
if (hash_tab[i] == 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
fn = &std_protos[hash_tab[i]];
|
1999-10-16 06:09:09 +00:00
|
|
|
|
if ((int) strlen (fn->fname) == name_length
|
1996-09-18 05:35:50 +00:00
|
|
|
|
&& strncmp (fn->fname, name, name_length) == 0)
|
|
|
|
|
return fn;
|
|
|
|
|
i = (i+1) % HASH_SIZE;
|
|
|
|
|
if (i == i0)
|
|
|
|
|
abort ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *inc_filename;
|
|
|
|
|
int inc_filename_length;
|
2002-02-01 18:16:02 +00:00
|
|
|
|
const char *progname = "fix-header";
|
1996-09-18 05:35:50 +00:00
|
|
|
|
FILE *outf;
|
|
|
|
|
sstring line;
|
|
|
|
|
|
|
|
|
|
int lbrac_line, rbrac_line;
|
|
|
|
|
|
|
|
|
|
int required_unseen_count = 0;
|
1999-08-26 09:30:50 +00:00
|
|
|
|
int required_other = 0;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
static void
|
1996-09-18 05:35:50 +00:00
|
|
|
|
write_lbrac ()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
#if ADD_MISSING_EXTERN_C
|
|
|
|
|
if (missing_extern_C_count + required_unseen_count > 0)
|
|
|
|
|
fprintf (outf, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (partial_count)
|
|
|
|
|
{
|
|
|
|
|
fprintf (outf, "#ifndef _PARAMS\n");
|
|
|
|
|
fprintf (outf, "#if defined(__STDC__) || defined(__cplusplus)\n");
|
|
|
|
|
fprintf (outf, "#define _PARAMS(ARGS) ARGS\n");
|
|
|
|
|
fprintf (outf, "#else\n");
|
|
|
|
|
fprintf (outf, "#define _PARAMS(ARGS) ()\n");
|
|
|
|
|
fprintf (outf, "#endif\n#endif /* _PARAMS */\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct partial_proto
|
|
|
|
|
{
|
|
|
|
|
struct partial_proto *next;
|
|
|
|
|
struct fn_decl *fn;
|
|
|
|
|
int line_seen;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct partial_proto *partial_proto_list = NULL;
|
|
|
|
|
|
|
|
|
|
struct partial_proto required_dummy_proto, seen_dummy_proto;
|
|
|
|
|
#define REQUIRED(FN) ((FN)->partial == &required_dummy_proto)
|
|
|
|
|
#define SET_REQUIRED(FN) ((FN)->partial = &required_dummy_proto)
|
|
|
|
|
#define SET_SEEN(FN) ((FN)->partial = &seen_dummy_proto)
|
|
|
|
|
#define SEEN(FN) ((FN)->partial == &seen_dummy_proto)
|
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
static void
|
1996-09-18 05:35:50 +00:00
|
|
|
|
recognized_macro (fname)
|
2002-02-01 18:16:02 +00:00
|
|
|
|
const char *fname;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
{
|
1999-08-26 09:30:50 +00:00
|
|
|
|
/* The original include file defines fname as a macro. */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
struct fn_decl *fn = lookup_std_proto (fname, strlen (fname));
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
/* Since fname is a macro, don't require a prototype for it. */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
if (fn)
|
|
|
|
|
{
|
|
|
|
|
if (REQUIRED (fn))
|
|
|
|
|
required_unseen_count--;
|
|
|
|
|
SET_SEEN (fn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (special_file_handling)
|
|
|
|
|
{
|
|
|
|
|
case errno_h:
|
1999-08-26 09:30:50 +00:00
|
|
|
|
if (strcmp (fname, "errno") == 0 && !seen_errno)
|
|
|
|
|
seen_errno = 1, required_other--;
|
|
|
|
|
break;
|
|
|
|
|
case stdlib_h:
|
|
|
|
|
if (strcmp (fname, "EXIT_FAILURE") == 0 && !seen_EXIT_FAILURE)
|
|
|
|
|
seen_EXIT_FAILURE = 1, required_other--;
|
|
|
|
|
if (strcmp (fname, "EXIT_SUCCESS") == 0 && !seen_EXIT_SUCCESS)
|
|
|
|
|
seen_EXIT_SUCCESS = 1, required_other--;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
break;
|
|
|
|
|
case sys_stat_h:
|
|
|
|
|
if (fname[0] == 'S' && fname[1] == '_')
|
|
|
|
|
{
|
|
|
|
|
if (strcmp (fname, "S_IFBLK") == 0) seen_S_IFBLK++;
|
|
|
|
|
else if (strcmp (fname, "S_ISBLK") == 0) seen_S_ISBLK++;
|
|
|
|
|
else if (strcmp (fname, "S_IFCHR") == 0) seen_S_IFCHR++;
|
|
|
|
|
else if (strcmp (fname, "S_ISCHR") == 0) seen_S_ISCHR++;
|
|
|
|
|
else if (strcmp (fname, "S_IFDIR") == 0) seen_S_IFDIR++;
|
|
|
|
|
else if (strcmp (fname, "S_ISDIR") == 0) seen_S_ISDIR++;
|
|
|
|
|
else if (strcmp (fname, "S_IFIFO") == 0) seen_S_IFIFO++;
|
|
|
|
|
else if (strcmp (fname, "S_ISFIFO") == 0) seen_S_ISFIFO++;
|
|
|
|
|
else if (strcmp (fname, "S_IFLNK") == 0) seen_S_IFLNK++;
|
|
|
|
|
else if (strcmp (fname, "S_ISLNK") == 0) seen_S_ISLNK++;
|
|
|
|
|
else if (strcmp (fname, "S_IFREG") == 0) seen_S_IFREG++;
|
|
|
|
|
else if (strcmp (fname, "S_ISREG") == 0) seen_S_ISREG++;
|
|
|
|
|
}
|
1999-08-26 09:30:50 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2002-02-01 18:16:02 +00:00
|
|
|
|
recognized_extern (name)
|
|
|
|
|
const cpp_token *name;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
{
|
|
|
|
|
switch (special_file_handling)
|
|
|
|
|
{
|
|
|
|
|
case errno_h:
|
2002-02-01 18:16:02 +00:00
|
|
|
|
if (cpp_ideq (name, "errno"))
|
1999-08-26 09:30:50 +00:00
|
|
|
|
seen_errno = 1, required_other--;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
1996-09-18 05:35:50 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Called by scan_decls if it saw a function definition for a function
|
2002-02-01 18:16:02 +00:00
|
|
|
|
named FNAME. KIND is 'I' for an inline function; 'F' if a normal
|
|
|
|
|
function declaration preceded by 'extern "C"' (or nested inside
|
|
|
|
|
'extern "C"' braces); or 'f' for other function declarations. */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
|
|
void
|
2002-02-01 18:16:02 +00:00
|
|
|
|
recognized_function (fname, line, kind, have_arg_list)
|
|
|
|
|
const cpp_token *fname;
|
|
|
|
|
unsigned int line;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
int kind; /* One of 'f' 'F' or 'I' */
|
|
|
|
|
int have_arg_list;
|
|
|
|
|
{
|
|
|
|
|
struct partial_proto *partial;
|
|
|
|
|
int i;
|
|
|
|
|
struct fn_decl *fn;
|
|
|
|
|
#if ADD_MISSING_EXTERN_C
|
|
|
|
|
if (kind == 'f')
|
|
|
|
|
missing_extern_C_count++;
|
|
|
|
|
#endif
|
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
fn = lookup_std_proto ((const char *) NODE_NAME (fname->val.node),
|
|
|
|
|
NODE_LEN (fname->val.node));
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
/* Remove the function from the list of required function. */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
if (fn)
|
|
|
|
|
{
|
|
|
|
|
if (REQUIRED (fn))
|
|
|
|
|
required_unseen_count--;
|
|
|
|
|
SET_SEEN (fn);
|
|
|
|
|
}
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
/* If we have a full prototype, we're done. */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
if (have_arg_list)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (kind == 'I') /* don't edit inline function */
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* If the partial prototype was included from some other file,
|
1999-08-26 09:30:50 +00:00
|
|
|
|
we don't need to patch it up (in this run). */
|
2002-02-01 18:16:02 +00:00
|
|
|
|
i = strlen (cur_file);
|
1996-09-18 05:35:50 +00:00
|
|
|
|
if (i < inc_filename_length
|
2002-02-01 18:16:02 +00:00
|
|
|
|
|| strcmp (inc_filename, cur_file + (i - inc_filename_length)) != 0)
|
1996-09-18 05:35:50 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (fn == NULL)
|
|
|
|
|
return;
|
2002-02-01 18:16:02 +00:00
|
|
|
|
if (fn->params[0] == '\0')
|
1996-09-18 05:35:50 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* We only have a partial function declaration,
|
1999-08-26 09:30:50 +00:00
|
|
|
|
so remember that we have to add a complete prototype. */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
partial_count++;
|
1999-08-26 09:30:50 +00:00
|
|
|
|
partial = (struct partial_proto *)
|
1996-09-18 05:35:50 +00:00
|
|
|
|
obstack_alloc (&scan_file_obstack, sizeof (struct partial_proto));
|
2002-02-01 18:16:02 +00:00
|
|
|
|
partial->line_seen = line;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
partial->fn = fn;
|
|
|
|
|
fn->partial = partial;
|
|
|
|
|
partial->next = partial_proto_list;
|
|
|
|
|
partial_proto_list = partial;
|
|
|
|
|
if (verbose)
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "(%s: %s non-prototype function declaration.)\n",
|
2002-02-01 18:16:02 +00:00
|
|
|
|
inc_filename, fn->fname);
|
1996-09-18 05:35:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* For any name in NAMES that is defined as a macro,
|
1999-08-26 09:30:50 +00:00
|
|
|
|
call recognized_macro on it. */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
static void
|
1996-09-18 05:35:50 +00:00
|
|
|
|
check_macro_names (pfile, names)
|
1999-08-26 09:30:50 +00:00
|
|
|
|
cpp_reader *pfile;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
namelist names;
|
|
|
|
|
{
|
2002-02-01 18:16:02 +00:00
|
|
|
|
size_t len;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
while (*names)
|
|
|
|
|
{
|
2002-02-01 18:16:02 +00:00
|
|
|
|
len = strlen (names);
|
|
|
|
|
if (cpp_defined (pfile, (const unsigned char *)names, len))
|
1996-09-18 05:35:50 +00:00
|
|
|
|
recognized_macro (names);
|
2002-02-01 18:16:02 +00:00
|
|
|
|
names += len + 1;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
static void
|
|
|
|
|
cb_file_change (pfile, map)
|
|
|
|
|
cpp_reader *pfile ATTRIBUTE_UNUSED;
|
|
|
|
|
const struct line_map *map;
|
|
|
|
|
{
|
|
|
|
|
/* Just keep track of current file name. */
|
|
|
|
|
cur_file = map->to_file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
1996-09-18 05:35:50 +00:00
|
|
|
|
read_scan_file (in_fname, argc, argv)
|
|
|
|
|
char *in_fname;
|
|
|
|
|
int argc;
|
|
|
|
|
char **argv;
|
|
|
|
|
{
|
2002-02-01 18:16:02 +00:00
|
|
|
|
cpp_reader *scan_in;
|
|
|
|
|
cpp_callbacks *cb;
|
|
|
|
|
cpp_options *options;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
struct fn_decl *fn;
|
|
|
|
|
int i;
|
2002-02-01 18:16:02 +00:00
|
|
|
|
struct symbol_list *cur_symbols;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
|
|
obstack_init (&scan_file_obstack);
|
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
scan_in = cpp_create_reader (CLK_GNUC89);
|
|
|
|
|
cb = cpp_get_callbacks (scan_in);
|
|
|
|
|
cb->file_change = cb_file_change;
|
|
|
|
|
|
|
|
|
|
/* We are going to be scanning a header file out of its proper context,
|
|
|
|
|
so ignore warnings and errors. */
|
|
|
|
|
options = cpp_get_options (scan_in);
|
|
|
|
|
options->inhibit_warnings = 1;
|
|
|
|
|
options->inhibit_errors = 1;
|
|
|
|
|
|
|
|
|
|
i = cpp_handle_options (scan_in, argc, argv);
|
|
|
|
|
if (i < argc && ! CPP_FATAL_ERRORS (scan_in))
|
|
|
|
|
cpp_fatal (scan_in, "Invalid option `%s'", argv[i]);
|
|
|
|
|
cpp_post_options (scan_in);
|
|
|
|
|
if (CPP_FATAL_ERRORS (scan_in))
|
1999-08-26 09:30:50 +00:00
|
|
|
|
exit (FATAL_EXIT_CODE);
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
if (! cpp_read_main_file (scan_in, in_fname, NULL))
|
1999-08-26 09:30:50 +00:00
|
|
|
|
exit (FATAL_EXIT_CODE);
|
1996-09-18 05:45:16 +00:00
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
cpp_finish_options (scan_in);
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
/* We are scanning a system header, so mark it as such. */
|
|
|
|
|
cpp_make_system_header (scan_in, 1, 0);
|
|
|
|
|
|
|
|
|
|
scan_decls (scan_in, argc, argv);
|
|
|
|
|
for (cur_symbols = &symbol_table[0]; cur_symbols->names; cur_symbols++)
|
|
|
|
|
check_macro_names (scan_in, cur_symbols->names);
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
|
|
/* Traditionally, getc and putc are defined in terms of _filbuf and _flsbuf.
|
1999-08-26 09:30:50 +00:00
|
|
|
|
If so, those functions are also required. */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
if (special_file_handling == stdio_h
|
|
|
|
|
&& (fn = lookup_std_proto ("_filbuf", 7)) != NULL)
|
|
|
|
|
{
|
2002-02-01 18:16:02 +00:00
|
|
|
|
static const unsigned char getchar_call[] = "getchar();";
|
1996-09-18 05:35:50 +00:00
|
|
|
|
int seen_filbuf = 0;
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
/* Scan the macro expansion of "getchar();". */
|
2002-02-01 18:16:02 +00:00
|
|
|
|
cpp_push_buffer (scan_in, getchar_call, sizeof(getchar_call) - 1,
|
|
|
|
|
/* from_stage3 */ true, 1);
|
1996-09-18 05:35:50 +00:00
|
|
|
|
for (;;)
|
|
|
|
|
{
|
2002-02-01 18:16:02 +00:00
|
|
|
|
const cpp_token *t = cpp_get_token (scan_in);
|
|
|
|
|
|
|
|
|
|
if (t->type == CPP_EOF)
|
1996-09-18 05:35:50 +00:00
|
|
|
|
break;
|
2002-02-01 18:16:02 +00:00
|
|
|
|
else if (cpp_ideq (t, "_filbuf"))
|
1996-09-18 05:35:50 +00:00
|
|
|
|
seen_filbuf++;
|
|
|
|
|
}
|
2002-02-01 18:16:02 +00:00
|
|
|
|
|
1996-09-18 05:35:50 +00:00
|
|
|
|
if (seen_filbuf)
|
|
|
|
|
{
|
|
|
|
|
int need_filbuf = !SEEN (fn) && !REQUIRED (fn);
|
|
|
|
|
struct fn_decl *flsbuf_fn = lookup_std_proto ("_flsbuf", 7);
|
|
|
|
|
int need_flsbuf
|
|
|
|
|
= flsbuf_fn && !SEEN (flsbuf_fn) && !REQUIRED (flsbuf_fn);
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
/* Append "_filbuf" and/or "_flsbuf" to the required functions. */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
if (need_filbuf + need_flsbuf)
|
|
|
|
|
{
|
1999-10-16 06:09:09 +00:00
|
|
|
|
const char *new_list;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
if (need_filbuf)
|
1999-08-26 09:30:50 +00:00
|
|
|
|
SET_REQUIRED (fn);
|
1996-09-18 05:35:50 +00:00
|
|
|
|
if (need_flsbuf)
|
1999-08-26 09:30:50 +00:00
|
|
|
|
SET_REQUIRED (flsbuf_fn);
|
2002-02-01 18:16:02 +00:00
|
|
|
|
if (need_flsbuf && need_filbuf)
|
1999-08-26 09:30:50 +00:00
|
|
|
|
new_list = "_filbuf\0_flsbuf\0";
|
|
|
|
|
else if (need_flsbuf)
|
|
|
|
|
new_list = "_flsbuf\0";
|
|
|
|
|
else /* if (need_flsbuf) */
|
|
|
|
|
new_list = "_filbuf\0";
|
|
|
|
|
add_symbols (ANSI_SYMBOL, new_list);
|
1996-09-18 05:35:50 +00:00
|
|
|
|
required_unseen_count += need_filbuf + need_flsbuf;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
if (required_unseen_count + partial_count + required_other
|
1996-09-18 05:35:50 +00:00
|
|
|
|
#if ADD_MISSING_EXTERN_C
|
|
|
|
|
+ missing_extern_C_count
|
|
|
|
|
#endif
|
|
|
|
|
== 0)
|
|
|
|
|
{
|
|
|
|
|
if (verbose)
|
|
|
|
|
fprintf (stderr, "%s: OK, nothing needs to be done.\n", inc_filename);
|
1999-08-26 09:30:50 +00:00
|
|
|
|
exit (SUCCESS_EXIT_CODE);
|
1996-09-18 05:35:50 +00:00
|
|
|
|
}
|
|
|
|
|
if (!verbose)
|
|
|
|
|
fprintf (stderr, "%s: fixing %s\n", progname, inc_filename);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (required_unseen_count)
|
|
|
|
|
fprintf (stderr, "%s: %d missing function declarations.\n",
|
|
|
|
|
inc_filename, required_unseen_count);
|
|
|
|
|
if (partial_count)
|
|
|
|
|
fprintf (stderr, "%s: %d non-prototype function declarations.\n",
|
|
|
|
|
inc_filename, partial_count);
|
|
|
|
|
#if ADD_MISSING_EXTERN_C
|
|
|
|
|
if (missing_extern_C_count)
|
|
|
|
|
fprintf (stderr,
|
|
|
|
|
"%s: %d declarations not protected by extern \"C\".\n",
|
|
|
|
|
inc_filename, missing_extern_C_count);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
static void
|
1996-09-18 05:35:50 +00:00
|
|
|
|
write_rbrac ()
|
|
|
|
|
{
|
|
|
|
|
struct fn_decl *fn;
|
|
|
|
|
const char *cptr;
|
2002-02-01 18:16:02 +00:00
|
|
|
|
struct symbol_list *cur_symbols;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
|
|
if (required_unseen_count)
|
|
|
|
|
{
|
|
|
|
|
#ifdef NO_IMPLICIT_EXTERN_C
|
|
|
|
|
fprintf (outf, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
/* Now we print out prototypes for those functions that we haven't seen. */
|
|
|
|
|
for (cur_symbols = &symbol_table[0]; cur_symbols->names; cur_symbols++)
|
1996-09-18 05:35:50 +00:00
|
|
|
|
{
|
1999-08-26 09:30:50 +00:00
|
|
|
|
int if_was_emitted = 0;
|
|
|
|
|
int name_len;
|
|
|
|
|
cptr = cur_symbols->names;
|
|
|
|
|
for ( ; (name_len = strlen (cptr)) != 0; cptr+= name_len + 1)
|
|
|
|
|
{
|
|
|
|
|
int macro_protect = 0;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
if (cur_symbols->flags & MACRO_SYMBOL)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
fn = lookup_std_proto (cptr, name_len);
|
|
|
|
|
if (fn == NULL || !REQUIRED (fn))
|
|
|
|
|
continue;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
if (!if_was_emitted)
|
|
|
|
|
{
|
|
|
|
|
/* what about curses. ??? or _flsbuf/_filbuf ??? */
|
|
|
|
|
if (cur_symbols->flags & ANSI_SYMBOL)
|
|
|
|
|
fprintf (outf,
|
|
|
|
|
"#if defined(__USE_FIXED_PROTOTYPES__) || defined(__cplusplus) || defined (__STRICT_ANSI__)\n");
|
|
|
|
|
else if (cur_symbols->flags & (POSIX1_SYMBOL|POSIX2_SYMBOL))
|
|
|
|
|
fprintf (outf,
|
|
|
|
|
"#if defined(__USE_FIXED_PROTOTYPES__) || (defined(__cplusplus) \\\n\
|
|
|
|
|
? (!defined(__STRICT_ANSI__) || defined(_POSIX_SOURCE)) \\\n\
|
|
|
|
|
: (defined(__STRICT_ANSI__) && defined(_POSIX_SOURCE)))\n");
|
|
|
|
|
else if (cur_symbols->flags & XOPEN_SYMBOL)
|
|
|
|
|
{
|
|
|
|
|
fprintf (outf,
|
|
|
|
|
"#if defined(__USE_FIXED_PROTOTYPES__) \\\n\
|
|
|
|
|
|| (defined(__STRICT_ANSI__) && defined(_XOPEN_SOURCE))\n");
|
|
|
|
|
}
|
|
|
|
|
else if (cur_symbols->flags & XOPEN_EXTENDED_SYMBOL)
|
|
|
|
|
{
|
|
|
|
|
fprintf (outf,
|
|
|
|
|
"#if defined(__USE_FIXED_PROTOTYPES__) \\\n\
|
|
|
|
|
|| (defined(__STRICT_ANSI__) && defined(_XOPEN_EXTENDED_SOURCE))\n");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
fatal ("internal error for function %s", fn->fname);
|
|
|
|
|
}
|
|
|
|
|
if_was_emitted = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* In the case of memmove, protect in case the application
|
|
|
|
|
defines it as a macro before including the header. */
|
|
|
|
|
if (!strcmp (fn->fname, "memmove")
|
2002-02-01 18:16:02 +00:00
|
|
|
|
|| !strcmp (fn->fname, "putc")
|
|
|
|
|
|| !strcmp (fn->fname, "getc")
|
1999-08-26 09:30:50 +00:00
|
|
|
|
|| !strcmp (fn->fname, "vprintf")
|
|
|
|
|
|| !strcmp (fn->fname, "vfprintf")
|
|
|
|
|
|| !strcmp (fn->fname, "vsprintf")
|
|
|
|
|
|| !strcmp (fn->fname, "rewinddir")
|
|
|
|
|
|| !strcmp (fn->fname, "abort"))
|
|
|
|
|
macro_protect = 1;
|
|
|
|
|
|
|
|
|
|
if (macro_protect)
|
|
|
|
|
fprintf (outf, "#ifndef %s\n", fn->fname);
|
|
|
|
|
fprintf (outf, "extern %s %s (%s);\n",
|
|
|
|
|
fn->rtype, fn->fname, fn->params);
|
|
|
|
|
if (macro_protect)
|
|
|
|
|
fprintf (outf, "#endif\n");
|
|
|
|
|
}
|
|
|
|
|
if (if_was_emitted)
|
|
|
|
|
fprintf (outf,
|
|
|
|
|
"#endif /* defined(__USE_FIXED_PROTOTYPES__) || ... */\n");
|
1996-09-18 05:35:50 +00:00
|
|
|
|
}
|
|
|
|
|
if (required_unseen_count)
|
|
|
|
|
{
|
|
|
|
|
#ifdef NO_IMPLICIT_EXTERN_C
|
|
|
|
|
fprintf (outf, "#ifdef __cplusplus\n}\n#endif\n");
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (special_file_handling)
|
|
|
|
|
{
|
|
|
|
|
case errno_h:
|
1999-08-26 09:30:50 +00:00
|
|
|
|
if (!seen_errno)
|
1996-09-18 05:35:50 +00:00
|
|
|
|
fprintf (outf, "extern int errno;\n");
|
|
|
|
|
break;
|
1999-08-26 09:30:50 +00:00
|
|
|
|
case stdlib_h:
|
|
|
|
|
if (!seen_EXIT_FAILURE)
|
|
|
|
|
fprintf (outf, "#define EXIT_FAILURE 1\n");
|
|
|
|
|
if (!seen_EXIT_SUCCESS)
|
|
|
|
|
fprintf (outf, "#define EXIT_SUCCESS 0\n");
|
|
|
|
|
break;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
case sys_stat_h:
|
|
|
|
|
if (!seen_S_ISBLK && seen_S_IFBLK)
|
|
|
|
|
fprintf (outf,
|
|
|
|
|
"#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)\n");
|
|
|
|
|
if (!seen_S_ISCHR && seen_S_IFCHR)
|
|
|
|
|
fprintf (outf,
|
|
|
|
|
"#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)\n");
|
|
|
|
|
if (!seen_S_ISDIR && seen_S_IFDIR)
|
|
|
|
|
fprintf (outf,
|
|
|
|
|
"#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)\n");
|
|
|
|
|
if (!seen_S_ISFIFO && seen_S_IFIFO)
|
|
|
|
|
fprintf (outf,
|
|
|
|
|
"#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)\n");
|
|
|
|
|
if (!seen_S_ISLNK && seen_S_IFLNK)
|
|
|
|
|
fprintf (outf,
|
|
|
|
|
"#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)\n");
|
|
|
|
|
if (!seen_S_ISREG && seen_S_IFREG)
|
|
|
|
|
fprintf (outf,
|
|
|
|
|
"#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)\n");
|
|
|
|
|
break;
|
1999-08-26 09:30:50 +00:00
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if ADD_MISSING_EXTERN_C
|
|
|
|
|
if (missing_extern_C_count + required_unseen_count > 0)
|
|
|
|
|
fprintf (outf, "#ifdef __cplusplus\n}\n#endif\n");
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Returns 1 iff the file is properly protected from multiple inclusion:
|
|
|
|
|
#ifndef PROTECT_NAME
|
|
|
|
|
#define PROTECT_NAME
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
#define INF_GET() (inf_ptr < inf_limit ? *(unsigned char *) inf_ptr++ : EOF)
|
1996-09-18 05:35:50 +00:00
|
|
|
|
#define INF_UNGET(c) ((c)!=EOF && inf_ptr--)
|
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
static int
|
1996-09-18 05:35:50 +00:00
|
|
|
|
inf_skip_spaces (c)
|
|
|
|
|
int c;
|
|
|
|
|
{
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
if (c == ' ' || c == '\t')
|
|
|
|
|
c = INF_GET ();
|
|
|
|
|
else if (c == '/')
|
|
|
|
|
{
|
|
|
|
|
c = INF_GET ();
|
|
|
|
|
if (c != '*')
|
|
|
|
|
{
|
1999-08-26 09:30:50 +00:00
|
|
|
|
(void) INF_UNGET (c);
|
1996-09-18 05:35:50 +00:00
|
|
|
|
return '/';
|
|
|
|
|
}
|
|
|
|
|
c = INF_GET ();
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
if (c == EOF)
|
|
|
|
|
return EOF;
|
|
|
|
|
else if (c != '*')
|
|
|
|
|
{
|
|
|
|
|
if (c == '\n')
|
|
|
|
|
source_lineno++, lineno++;
|
|
|
|
|
c = INF_GET ();
|
|
|
|
|
}
|
|
|
|
|
else if ((c = INF_GET ()) == '/')
|
|
|
|
|
return INF_GET ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
/* Read into STR from inf_buffer upto DELIM. */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
static int
|
1996-09-18 05:35:50 +00:00
|
|
|
|
inf_read_upto (str, delim)
|
|
|
|
|
sstring *str;
|
|
|
|
|
int delim;
|
|
|
|
|
{
|
|
|
|
|
int ch;
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
ch = INF_GET ();
|
|
|
|
|
if (ch == EOF || ch == delim)
|
|
|
|
|
break;
|
|
|
|
|
SSTRING_PUT (str, ch);
|
|
|
|
|
}
|
|
|
|
|
MAKE_SSTRING_SPACE (str, 1);
|
|
|
|
|
*str->ptr = 0;
|
|
|
|
|
return ch;
|
|
|
|
|
}
|
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
static int
|
1996-09-18 05:35:50 +00:00
|
|
|
|
inf_scan_ident (s, c)
|
2002-02-01 18:16:02 +00:00
|
|
|
|
sstring *s;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
int c;
|
|
|
|
|
{
|
|
|
|
|
s->ptr = s->base;
|
2002-02-01 18:16:02 +00:00
|
|
|
|
if (ISIDST (c))
|
1996-09-18 05:35:50 +00:00
|
|
|
|
{
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
SSTRING_PUT (s, c);
|
|
|
|
|
c = INF_GET ();
|
2002-02-01 18:16:02 +00:00
|
|
|
|
if (c == EOF || !(ISIDNUM (c)))
|
1996-09-18 05:35:50 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
MAKE_SSTRING_SPACE (s, 1);
|
|
|
|
|
*s->ptr = 0;
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Returns 1 if the file is correctly protected against multiple
|
|
|
|
|
inclusion, setting *ifndef_line to the line number of the initial #ifndef
|
|
|
|
|
and setting *endif_line to the final #endif.
|
1999-08-26 09:30:50 +00:00
|
|
|
|
Otherwise return 0. */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
static int
|
1996-09-18 05:35:50 +00:00
|
|
|
|
check_protection (ifndef_line, endif_line)
|
|
|
|
|
int *ifndef_line, *endif_line;
|
|
|
|
|
{
|
|
|
|
|
int c;
|
|
|
|
|
int if_nesting = 1; /* Level of nesting of #if's */
|
|
|
|
|
char *protect_name = NULL; /* Identifier following initial #ifndef */
|
|
|
|
|
int define_seen = 0;
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
/* Skip initial white space (including comments). */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
for (;; lineno++)
|
|
|
|
|
{
|
|
|
|
|
c = inf_skip_spaces (' ');
|
|
|
|
|
if (c == EOF)
|
|
|
|
|
return 0;
|
|
|
|
|
if (c != '\n')
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (c != '#')
|
|
|
|
|
return 0;
|
|
|
|
|
c = inf_scan_ident (&buf, inf_skip_spaces (' '));
|
|
|
|
|
if (SSTRING_LENGTH (&buf) == 0 || strcmp (buf.base, "ifndef") != 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
/* So far so good: We've seen an initial #ifndef. */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
*ifndef_line = lineno;
|
|
|
|
|
c = inf_scan_ident (&buf, inf_skip_spaces (c));
|
|
|
|
|
if (SSTRING_LENGTH (&buf) == 0 || c == EOF)
|
|
|
|
|
return 0;
|
|
|
|
|
protect_name = xstrdup (buf.base);
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
(void) INF_UNGET (c);
|
1996-09-18 05:35:50 +00:00
|
|
|
|
c = inf_read_upto (&buf, '\n');
|
|
|
|
|
if (c == EOF)
|
|
|
|
|
return 0;
|
|
|
|
|
lineno++;
|
|
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
c = inf_skip_spaces (' ');
|
|
|
|
|
if (c == EOF)
|
|
|
|
|
return 0;
|
|
|
|
|
if (c == '\n')
|
|
|
|
|
{
|
|
|
|
|
lineno++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (c != '#')
|
|
|
|
|
goto skip_to_eol;
|
|
|
|
|
c = inf_scan_ident (&buf, inf_skip_spaces (' '));
|
|
|
|
|
if (SSTRING_LENGTH (&buf) == 0)
|
|
|
|
|
;
|
|
|
|
|
else if (!strcmp (buf.base, "ifndef")
|
|
|
|
|
|| !strcmp (buf.base, "ifdef") || !strcmp (buf.base, "if"))
|
|
|
|
|
{
|
|
|
|
|
if_nesting++;
|
|
|
|
|
}
|
|
|
|
|
else if (!strcmp (buf.base, "endif"))
|
|
|
|
|
{
|
|
|
|
|
if_nesting--;
|
|
|
|
|
if (if_nesting == 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else if (!strcmp (buf.base, "else"))
|
|
|
|
|
{
|
|
|
|
|
if (if_nesting == 1)
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else if (!strcmp (buf.base, "define"))
|
|
|
|
|
{
|
|
|
|
|
c = inf_skip_spaces (c);
|
|
|
|
|
c = inf_scan_ident (&buf, c);
|
|
|
|
|
if (buf.base[0] > 0 && strcmp (buf.base, protect_name) == 0)
|
|
|
|
|
define_seen = 1;
|
|
|
|
|
}
|
|
|
|
|
skip_to_eol:
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
if (c == '\n' || c == EOF)
|
|
|
|
|
break;
|
|
|
|
|
c = INF_GET ();
|
|
|
|
|
}
|
|
|
|
|
if (c == EOF)
|
|
|
|
|
return 0;
|
|
|
|
|
lineno++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!define_seen)
|
|
|
|
|
return 0;
|
|
|
|
|
*endif_line = lineno;
|
1999-08-26 09:30:50 +00:00
|
|
|
|
/* Skip final white space (including comments). */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
c = inf_skip_spaces (' ');
|
|
|
|
|
if (c == EOF)
|
|
|
|
|
break;
|
|
|
|
|
if (c != '\n')
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
extern int main PARAMS ((int, char **));
|
|
|
|
|
|
1996-09-18 05:35:50 +00:00
|
|
|
|
int
|
|
|
|
|
main (argc, argv)
|
|
|
|
|
int argc;
|
|
|
|
|
char **argv;
|
|
|
|
|
{
|
|
|
|
|
int inf_fd;
|
|
|
|
|
struct stat sbuf;
|
|
|
|
|
int c;
|
1999-08-26 09:30:50 +00:00
|
|
|
|
#ifdef FIXPROTO_IGNORE_LIST
|
|
|
|
|
int i;
|
|
|
|
|
#endif
|
|
|
|
|
const char *cptr;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
int ifndef_line;
|
|
|
|
|
int endif_line;
|
|
|
|
|
long to_read;
|
|
|
|
|
long int inf_size;
|
2002-02-01 18:16:02 +00:00
|
|
|
|
struct symbol_list *cur_symbols;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
|
|
if (argv[0] && argv[0][0])
|
|
|
|
|
{
|
2002-02-01 18:16:02 +00:00
|
|
|
|
char *p;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
|
|
progname = 0;
|
|
|
|
|
for (p = argv[0]; *p; p++)
|
|
|
|
|
if (*p == '/')
|
|
|
|
|
progname = p;
|
|
|
|
|
progname = progname ? progname+1 : argv[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (argc < 4)
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "%s: Usage: foo.h infile.h outfile.h options\n",
|
|
|
|
|
progname);
|
1999-08-26 09:30:50 +00:00
|
|
|
|
exit (FATAL_EXIT_CODE);
|
1996-09-18 05:35:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inc_filename = argv[1];
|
|
|
|
|
inc_filename_length = strlen (inc_filename);
|
|
|
|
|
|
|
|
|
|
#ifdef FIXPROTO_IGNORE_LIST
|
|
|
|
|
for (i = 0; files_to_ignore[i] != NULL; i++)
|
|
|
|
|
{
|
2002-02-01 18:16:02 +00:00
|
|
|
|
const char *const ignore_name = files_to_ignore[i];
|
1996-09-18 05:35:50 +00:00
|
|
|
|
int ignore_len = strlen (ignore_name);
|
|
|
|
|
if (strncmp (inc_filename, ignore_name, ignore_len) == 0)
|
|
|
|
|
{
|
|
|
|
|
if (ignore_name[ignore_len-1] == '/'
|
|
|
|
|
|| inc_filename[ignore_len] == '\0')
|
|
|
|
|
{
|
|
|
|
|
if (verbose)
|
|
|
|
|
fprintf (stderr, "%s: ignoring %s\n", progname, inc_filename);
|
1999-08-26 09:30:50 +00:00
|
|
|
|
exit (SUCCESS_EXIT_CODE);
|
1996-09-18 05:35:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (strcmp (inc_filename, "sys/stat.h") == 0)
|
|
|
|
|
special_file_handling = sys_stat_h;
|
|
|
|
|
else if (strcmp (inc_filename, "errno.h") == 0)
|
1999-08-26 09:30:50 +00:00
|
|
|
|
special_file_handling = errno_h, required_other++;
|
|
|
|
|
else if (strcmp (inc_filename, "stdlib.h") == 0)
|
|
|
|
|
special_file_handling = stdlib_h, required_other+=2;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
else if (strcmp (inc_filename, "stdio.h") == 0)
|
|
|
|
|
special_file_handling = stdio_h;
|
|
|
|
|
include_entry = std_include_table;
|
|
|
|
|
while (include_entry->name != NULL
|
1999-10-16 06:09:09 +00:00
|
|
|
|
&& ((strcmp (include_entry->name, CONTINUED) == 0)
|
1999-08-26 09:30:50 +00:00
|
|
|
|
|| strcmp (inc_filename, include_entry->name) != 0))
|
1996-09-18 05:35:50 +00:00
|
|
|
|
include_entry++;
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
if (include_entry->name != NULL)
|
|
|
|
|
{
|
2002-02-01 18:16:02 +00:00
|
|
|
|
const struct std_include_entry *entry;
|
1999-08-26 09:30:50 +00:00
|
|
|
|
cur_symbol_table_size = 0;
|
|
|
|
|
for (entry = include_entry; ;)
|
|
|
|
|
{
|
|
|
|
|
if (entry->flags)
|
|
|
|
|
add_symbols (entry->flags, entry->names);
|
|
|
|
|
entry++;
|
1999-11-01 08:28:22 +00:00
|
|
|
|
if (!entry->name || strcmp (entry->name, CONTINUED) != 0)
|
1999-08-26 09:30:50 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
symbol_table[0].names = NULL;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
/* Count and mark the prototypes required for this include file. */
|
|
|
|
|
for (cur_symbols = &symbol_table[0]; cur_symbols->names; cur_symbols++)
|
1996-09-18 05:35:50 +00:00
|
|
|
|
{
|
1999-08-26 09:30:50 +00:00
|
|
|
|
int name_len;
|
|
|
|
|
if (cur_symbols->flags & MACRO_SYMBOL)
|
|
|
|
|
continue;
|
|
|
|
|
cptr = cur_symbols->names;
|
|
|
|
|
for ( ; (name_len = strlen (cptr)) != 0; cptr+= name_len + 1)
|
|
|
|
|
{
|
|
|
|
|
struct fn_decl *fn = lookup_std_proto (cptr, name_len);
|
|
|
|
|
required_unseen_count++;
|
|
|
|
|
if (fn == NULL)
|
|
|
|
|
fprintf (stderr, "Internal error: No prototype for %s\n", cptr);
|
|
|
|
|
else
|
|
|
|
|
SET_REQUIRED (fn);
|
|
|
|
|
}
|
1996-09-18 05:35:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
read_scan_file (argv[2], argc - 4, argv + 4);
|
|
|
|
|
|
|
|
|
|
inf_fd = open (argv[2], O_RDONLY, 0666);
|
|
|
|
|
if (inf_fd < 0)
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "%s: Cannot open '%s' for reading -",
|
|
|
|
|
progname, argv[2]);
|
|
|
|
|
perror (NULL);
|
1999-08-26 09:30:50 +00:00
|
|
|
|
exit (FATAL_EXIT_CODE);
|
1996-09-18 05:35:50 +00:00
|
|
|
|
}
|
|
|
|
|
if (fstat (inf_fd, &sbuf) < 0)
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "%s: Cannot get size of '%s' -", progname, argv[2]);
|
|
|
|
|
perror (NULL);
|
1999-08-26 09:30:50 +00:00
|
|
|
|
exit (FATAL_EXIT_CODE);
|
1996-09-18 05:35:50 +00:00
|
|
|
|
}
|
|
|
|
|
inf_size = sbuf.st_size;
|
1999-08-26 09:30:50 +00:00
|
|
|
|
inf_buffer = (char *) xmalloc (inf_size + 2);
|
1996-09-18 05:35:50 +00:00
|
|
|
|
inf_ptr = inf_buffer;
|
|
|
|
|
|
|
|
|
|
to_read = inf_size;
|
|
|
|
|
while (to_read > 0)
|
|
|
|
|
{
|
|
|
|
|
long i = read (inf_fd, inf_buffer + inf_size - to_read, to_read);
|
|
|
|
|
if (i < 0)
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "%s: Failed to read '%s' -", progname, argv[2]);
|
|
|
|
|
perror (NULL);
|
1999-08-26 09:30:50 +00:00
|
|
|
|
exit (FATAL_EXIT_CODE);
|
1996-09-18 05:35:50 +00:00
|
|
|
|
}
|
|
|
|
|
if (i == 0)
|
|
|
|
|
{
|
|
|
|
|
inf_size -= to_read;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
to_read -= i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close (inf_fd);
|
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
/* Inf_size may have changed if read was short (as on VMS) */
|
|
|
|
|
inf_buffer[inf_size] = '\n';
|
|
|
|
|
inf_buffer[inf_size + 1] = '\0';
|
|
|
|
|
inf_limit = inf_buffer + inf_size;
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
/* If file doesn't end with '\n', add one. */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
if (inf_limit > inf_buffer && inf_limit[-1] != '\n')
|
|
|
|
|
inf_limit++;
|
|
|
|
|
|
|
|
|
|
unlink (argv[3]);
|
|
|
|
|
outf = fopen (argv[3], "w");
|
|
|
|
|
if (outf == NULL)
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "%s: Cannot open '%s' for writing -",
|
|
|
|
|
progname, argv[3]);
|
|
|
|
|
perror (NULL);
|
1999-08-26 09:30:50 +00:00
|
|
|
|
exit (FATAL_EXIT_CODE);
|
1996-09-18 05:35:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lineno = 1;
|
|
|
|
|
|
|
|
|
|
if (check_protection (&ifndef_line, &endif_line))
|
|
|
|
|
{
|
|
|
|
|
lbrac_line = ifndef_line+1;
|
|
|
|
|
rbrac_line = endif_line;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
lbrac_line = 1;
|
|
|
|
|
rbrac_line = -1;
|
|
|
|
|
}
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
/* Reset input file. */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
inf_ptr = inf_buffer;
|
|
|
|
|
lineno = 1;
|
|
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
if (lineno == lbrac_line)
|
|
|
|
|
write_lbrac ();
|
|
|
|
|
if (lineno == rbrac_line)
|
|
|
|
|
write_rbrac ();
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
struct fn_decl *fn;
|
|
|
|
|
c = INF_GET ();
|
|
|
|
|
if (c == EOF)
|
|
|
|
|
break;
|
2002-02-01 18:16:02 +00:00
|
|
|
|
if (ISIDST (c))
|
1996-09-18 05:35:50 +00:00
|
|
|
|
{
|
|
|
|
|
c = inf_scan_ident (&buf, c);
|
1999-08-26 09:30:50 +00:00
|
|
|
|
(void) INF_UNGET (c);
|
1996-09-18 05:35:50 +00:00
|
|
|
|
fputs (buf.base, outf);
|
|
|
|
|
fn = lookup_std_proto (buf.base, strlen (buf.base));
|
|
|
|
|
/* We only want to edit the declaration matching the one
|
|
|
|
|
seen by scan-decls, as there can be multiple
|
1999-08-26 09:30:50 +00:00
|
|
|
|
declarations, selected by #ifdef __STDC__ or whatever. */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
if (fn && fn->partial && fn->partial->line_seen == lineno)
|
|
|
|
|
{
|
|
|
|
|
c = inf_skip_spaces (' ');
|
|
|
|
|
if (c == EOF)
|
|
|
|
|
break;
|
|
|
|
|
if (c == '(')
|
|
|
|
|
{
|
|
|
|
|
c = inf_skip_spaces (' ');
|
|
|
|
|
if (c == ')')
|
|
|
|
|
{
|
|
|
|
|
fprintf (outf, " _PARAMS((%s))", fn->params);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
putc ('(', outf);
|
1999-08-26 09:30:50 +00:00
|
|
|
|
(void) INF_UNGET (c);
|
1996-09-18 05:35:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
fprintf (outf, " %c", c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
putc (c, outf);
|
|
|
|
|
if (c == '\n')
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (c == EOF)
|
|
|
|
|
break;
|
|
|
|
|
lineno++;
|
|
|
|
|
}
|
|
|
|
|
if (rbrac_line < 0)
|
|
|
|
|
write_rbrac ();
|
|
|
|
|
|
|
|
|
|
fclose (outf);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
v_fatal (str, ap)
|
|
|
|
|
const char * str;
|
|
|
|
|
va_list ap;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "%s: %s: ", progname, inc_filename);
|
1999-08-26 09:30:50 +00:00
|
|
|
|
vfprintf (stderr, str, ap);
|
1996-09-18 05:35:50 +00:00
|
|
|
|
fprintf (stderr, "\n");
|
1999-08-26 09:30:50 +00:00
|
|
|
|
|
1996-09-18 05:35:50 +00:00
|
|
|
|
exit (FATAL_EXIT_CODE);
|
|
|
|
|
}
|
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
static void
|
|
|
|
|
fatal VPARAMS ((const char *str, ...))
|
1999-08-26 09:30:50 +00:00
|
|
|
|
{
|
2002-02-01 18:16:02 +00:00
|
|
|
|
VA_OPEN (ap, str);
|
|
|
|
|
VA_FIXEDARG (ap, const char *, str);
|
1999-08-26 09:30:50 +00:00
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
|
v_fatal (str, ap);
|
|
|
|
|
VA_CLOSE (ap);
|
1999-08-26 09:30:50 +00:00
|
|
|
|
}
|