Commit 6de8f7fc by Neil Booth Committed by Neil Booth

cppmain.c (do_preprocessing): New function; most of the old main.

        * cppmain.c (do_preprocessing): New function; most of the old
        main.
        (main): Call it to do most of the work.
        (cb): Move from global scope to set_callbacks ().
        (setup_callbacks): Get the callback pointer.
        (general_init, printer_init): Clean up code and comments.

From-SVN: r39012
parent 17879734
2001-01-14 Neil Booth <neil@daikokuya.demon.co.uk>
* cppmain.c (do_preprocessing): New function; most of the old
main.
(main): Call it to do most of the work.
(cb): Move from global scope to set_callbacks ().
(setup_callbacks): Get the callback pointer.
(general_init, printer_init): Clean up code and comments.
2001-01-14 Richard Earnshaw <rearnsha@arm.com> 2001-01-14 Richard Earnshaw <rearnsha@arm.com>
* config/arm/semi.h (ASM_SPEC): Pass -k to the assembler when * config/arm/semi.h (ASM_SPEC): Pass -k to the assembler when
......
...@@ -39,6 +39,7 @@ struct printer ...@@ -39,6 +39,7 @@ struct printer
int main PARAMS ((int, char **)); int main PARAMS ((int, char **));
static void general_init PARAMS ((const char *)); static void general_init PARAMS ((const char *));
static void do_preprocessing PARAMS ((int, char **));
static void setup_callbacks PARAMS ((void)); static void setup_callbacks PARAMS ((void));
/* General output routines. */ /* General output routines. */
...@@ -63,7 +64,6 @@ static void cb_def_pragma PARAMS ((cpp_reader *)); ...@@ -63,7 +64,6 @@ static void cb_def_pragma PARAMS ((cpp_reader *));
const char *progname; /* Needs to be global. */ const char *progname; /* Needs to be global. */
static cpp_reader *pfile; /* An opaque handle. */ static cpp_reader *pfile; /* An opaque handle. */
static cpp_options *options; /* Options of pfile. */ static cpp_options *options; /* Options of pfile. */
static cpp_callbacks *cb; /* Callbacks of pfile. */
static struct printer print; static struct printer print;
int int
...@@ -71,59 +71,17 @@ main (argc, argv) ...@@ -71,59 +71,17 @@ main (argc, argv)
int argc; int argc;
char **argv; char **argv;
{ {
int argi = 1; /* Next argument to handle. */
general_init (argv[0]); general_init (argv[0]);
/* Default language is GNU C89. */
/* Contruct a reader with default language GNU C89. */
pfile = cpp_create_reader (CLK_GNUC89); pfile = cpp_create_reader (CLK_GNUC89);
options = cpp_get_options (pfile); options = cpp_get_options (pfile);
cb = cpp_get_callbacks (pfile);
argi += cpp_handle_options (pfile, argc - argi , argv + argi); do_preprocessing (argc, argv);
if (argi < argc && ! CPP_FATAL_ERRORS (pfile))
cpp_fatal (pfile, "Invalid option %s", argv[argi]);
cpp_post_options (pfile);
if (CPP_FATAL_ERRORS (pfile))
return (FATAL_EXIT_CODE);
/* If cpp_handle_options saw --help or --version on the command
line, it will have set pfile->help_only to indicate this. Exit
successfully. [The library does not exit itself, because
e.g. cc1 needs to print its own --help message at this point.] */
if (options->help_only)
return (SUCCESS_EXIT_CODE);
/* Open the output now. We must do so even if no_output is on, /* Reader destructor. */
because there may be other output than from the actual
preprocessing (e.g. from -dM). */
if (printer_init (pfile))
return (FATAL_EXIT_CODE);
setup_callbacks ();
if (! cpp_start_read (pfile, options->in_fname))
return (FATAL_EXIT_CODE);
/* A successful cpp_start_read guarantees that we can call
cpp_scan_buffer_nooutput or cpp_get_token next. */
if (options->no_output)
cpp_scan_buffer_nooutput (pfile, 1);
else
scan_buffer (pfile);
/* -dM command line option. */
if (options->dump_macros == dump_only)
cpp_forall_identifiers (pfile, dump_macro, NULL);
cpp_finish (pfile);
cpp_cleanup (pfile); cpp_cleanup (pfile);
/* Flush any pending output. */
if (print.printed)
putc ('\n', print.outf);
if (ferror (print.outf) || fclose (print.outf))
cpp_notice_from_errno (pfile, options->out_fname);
if (cpp_errors (pfile)) if (cpp_errors (pfile))
return FATAL_EXIT_CODE; return FATAL_EXIT_CODE;
...@@ -141,8 +99,8 @@ general_init (const char *argv0) ...@@ -141,8 +99,8 @@ general_init (const char *argv0)
xmalloc_set_program_name (progname); xmalloc_set_program_name (progname);
/* LC_CTYPE determines the character set used by the terminal so it has be set /* LC_CTYPE determines the character set used by the terminal so it
to output messages correctly. */ has to be set to output messages correctly. */
#ifdef HAVE_LC_MESSAGES #ifdef HAVE_LC_MESSAGES
setlocale (LC_CTYPE, ""); setlocale (LC_CTYPE, "");
...@@ -155,10 +113,71 @@ general_init (const char *argv0) ...@@ -155,10 +113,71 @@ general_init (const char *argv0)
(void) textdomain (PACKAGE); (void) textdomain (PACKAGE);
} }
/* Handle switches, preprocess and output. */
static void
do_preprocessing (argc, argv)
int argc;
char **argv;
{
int argi = 1; /* Next argument to handle. */
argi += cpp_handle_options (pfile, argc - argi , argv + argi);
if (CPP_FATAL_ERRORS (pfile))
return;
if (argi < argc)
cpp_fatal (pfile, "Invalid option %s", argv[argi]);
else
cpp_post_options (pfile);
if (CPP_FATAL_ERRORS (pfile))
return;
/* If cpp_handle_options saw --help or --version on the command
line, it will have set pfile->help_only to indicate this. Exit
successfully. [The library does not exit itself, because
e.g. cc1 needs to print its own --help message at this point.] */
if (options->help_only)
return;
/* Open the output now. We must do so even if no_output is on,
because there may be other output than from the actual
preprocessing (e.g. from -dM). */
if (printer_init (pfile))
return;
setup_callbacks ();
if (cpp_start_read (pfile, options->in_fname))
{
/* A successful cpp_start_read guarantees that we can call
cpp_scan_buffer_nooutput or cpp_get_token next. */
if (options->no_output)
cpp_scan_buffer_nooutput (pfile, 1);
else
scan_buffer (pfile);
/* -dM command line option. Should this be in cpp_finish? */
if (options->dump_macros == dump_only)
cpp_forall_identifiers (pfile, dump_macro, NULL);
cpp_finish (pfile);
}
/* Flush any pending output. */
if (print.printed)
putc ('\n', print.outf);
if (ferror (print.outf) || fclose (print.outf))
cpp_notice_from_errno (pfile, options->out_fname);
}
/* Set up the callbacks as appropriate. */ /* Set up the callbacks as appropriate. */
static void static void
setup_callbacks () setup_callbacks ()
{ {
cpp_callbacks *cb = cpp_get_callbacks (pfile);
if (! options->no_output) if (! options->no_output)
{ {
cb->ident = cb_ident; cb->ident = cb_ident;
...@@ -264,10 +283,11 @@ printer_init (pfile) ...@@ -264,10 +283,11 @@ printer_init (pfile)
print.outf = fopen (options->out_fname, "w"); print.outf = fopen (options->out_fname, "w");
if (! print.outf) if (! print.outf)
{ {
cpp_notice_from_errno (pfile, options-> out_fname); cpp_notice_from_errno (pfile, options->out_fname);
return 1; return 1;
} }
} }
return 0; return 0;
} }
...@@ -324,7 +344,7 @@ print_line (special_flags) ...@@ -324,7 +344,7 @@ print_line (special_flags)
print.lineno, print.last_fname, special_flags, print.syshdr_flags); print.lineno, print.last_fname, special_flags, print.syshdr_flags);
} }
/* Callbacks */ /* Callbacks. */
static void static void
cb_ident (pfile, str) cb_ident (pfile, str)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment