1 /* CPP main program, using CPP Library.
2 Copyright (C) 1995, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994-95.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 In other words, you are welcome to use, share and improve this program.
20 You are forbidden to forbid anyone else to use, share and improve
21 what you give them. Help stamp out software-hoarding! */
33 extern int main
PARAMS ((int, char **));
40 cpp_reader
*pfile
= &parse_in
;
41 int argi
= 1; /* Next argument to handle. */
44 const char *out_fname
;
46 p
= argv
[0] + strlen (argv
[0]);
47 while (p
!= argv
[0] && p
[-1] != '/') --p
;
50 xmalloc_set_program_name (progname
);
52 #ifdef HAVE_LC_MESSAGES
53 setlocale (LC_MESSAGES
, "");
55 (void) bindtextdomain (PACKAGE
, localedir
);
56 (void) textdomain (PACKAGE
);
58 cpp_reader_init (pfile
);
60 argi
+= cpp_handle_options (pfile
, argc
- argi
, argv
+ argi
);
61 if (argi
< argc
&& ! CPP_FATAL_ERRORS (pfile
))
62 cpp_fatal (pfile
, "Invalid option %s", argv
[argi
]);
63 if (CPP_FATAL_ERRORS (pfile
))
64 return (FATAL_EXIT_CODE
);
66 if (! cpp_start_read (pfile
, CPP_OPTION (pfile
, in_fname
)))
67 return (FATAL_EXIT_CODE
);
69 /* Now that we know the input file is valid, open the output. */
70 out_fname
= CPP_OPTION (pfile
, out_fname
);
71 if (*out_fname
== '\0')
78 out
= fopen (out_fname
, "w");
81 cpp_notice_from_errno (pfile
, CPP_OPTION (pfile
, out_fname
));
82 return (FATAL_EXIT_CODE
);
86 if (! CPP_OPTION (pfile
, no_output
))
90 kind
= cpp_get_token (pfile
);
91 if (CPP_WRITTEN (pfile
) >= BUFSIZ
|| kind
== CPP_EOF
)
93 size_t rem
, count
= CPP_WRITTEN (pfile
);
95 rem
= fwrite (parse_in
.token_buffer
, 1, count
, out
);
98 cpp_notice_from_errno (pfile
, CPP_OPTION (pfile
, out_fname
));
100 CPP_SET_WRITTEN (pfile
, 0);
103 while (kind
!= CPP_EOF
);
109 cpp_scan_buffer (pfile
);
110 kind
= cpp_get_token (pfile
);
112 while (kind
!= CPP_EOF
);
113 CPP_SET_WRITTEN (pfile
, 0);
117 if (fwrite (parse_in
.token_buffer
, 1, CPP_WRITTEN (pfile
), out
)
118 < CPP_WRITTEN (pfile
))
119 cpp_notice_from_errno (pfile
, CPP_OPTION (pfile
, out_fname
));
121 if (ferror (out
) || fclose (out
))
122 cpp_notice_from_errno (pfile
, CPP_OPTION (pfile
, out_fname
));
127 return (FATAL_EXIT_CODE
);
128 return (SUCCESS_EXIT_CODE
);