145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/*
245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Invoke an external C preprocessor
345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *  Copyright (C) 2007       Paul Barker
545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *  Copyright (C) 2001-2007  Peter Johnson
645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Redistribution and use in source and binary forms, with or without
845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * modification, are permitted provided that the following conditions
945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * are met:
1045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 1. Redistributions of source code must retain the above copyright
1145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    notice, this list of conditions and the following disclaimer.
1245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 2. Redistributions in binary form must reproduce the above copyright
1345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    notice, this list of conditions and the following disclaimer in the
1445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    documentation and/or other materials provided with the distribution.
1545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
1645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
1745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
2045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * POSSIBILITY OF SUCH DAMAGE.
2745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org */
2845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
2945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <util.h>
3045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <libyasm.h>
3145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
3245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* TODO: Use autoconf to get the limit on the command line length. */
3345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define CMDLINE_SIZE 32770
3445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
3545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define BSIZE 512
3645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
3745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* Pre-declare the preprocessor module object. */
3845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgyasm_preproc_module yasm_cpp_LTX_preproc;
3945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/*******************************************************************************
4145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    Structures.
4245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org*******************************************************************************/
4345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* An entry in a list of arguments to pass to cpp. */
4545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgtypedef struct cpp_arg_entry {
4645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    TAILQ_ENTRY(cpp_arg_entry) entry;
4745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /*
4945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        The operator (eg "-I") and the parameter (eg "include/"). op is expected
5045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        to point to a string literal, whereas param is expected to be a copy of
5145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        the parameter which is free'd when no-longer needed (in
5245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        cpp_preproc_destroy()).
5345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    */
5445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    const char *op;
5545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    char *param;
5645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} cpp_arg_entry;
5745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
5845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgtypedef struct yasm_preproc_cpp {
5945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yasm_preproc_base preproc;   /* base structure */
6045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
6145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /* List of arguments to pass to cpp. */
62a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org    TAILQ_HEAD(cpp_arg_head, cpp_arg_entry) cpp_args;
6345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
6445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    char *filename;
6545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    FILE *f, *f_deps;
6645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yasm_linemap *cur_lm;
6745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yasm_errwarns *errwarns;
6845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
6945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    int flags;
7045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} yasm_preproc_cpp;
7145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
7245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* Flag values for yasm_preproc_cpp->flags. */
7345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define CPP_HAS_BEEN_INVOKED        0x01
7445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define CPP_HAS_GENERATED_DEPS      0x02
7545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
7645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/*******************************************************************************
7745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    Internal functions and helpers.
7845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org*******************************************************************************/
7945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
8045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/*
8145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    Append a string to the command line, ensuring that we don't overflow the
8245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    buffer.
8345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org*/
8445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define APPEND(s) do {                              \
8545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    size_t _len = strlen(s);                        \
8645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (p + _len >= limit)                          \
8745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        yasm__fatal(N_("command line too long!"));  \
8845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    strcpy(p, s);                                   \
8945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    p += _len;                                      \
9045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} while (0)
9145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
9245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/*
9345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    Put all the options together into a command line that can be used to invoke
9445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    cpp.
9545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org*/
9645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic char *
9745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcpp_build_cmdline(yasm_preproc_cpp *pp, const char *extra)
9845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
9945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    char *cmdline, *p, *limit;
10045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    cpp_arg_entry *arg;
10145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
10245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /* Initialize command line. */
10345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    cmdline = p = yasm_xmalloc(strlen(CPP_PROG)+CMDLINE_SIZE);
10445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    limit = p + CMDLINE_SIZE;
10545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    strcpy(p, CPP_PROG);
10645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    p += strlen(CPP_PROG);
10745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
10845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    arg = TAILQ_FIRST(&pp->cpp_args);
10945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
11045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /* Append arguments from the list. */
11145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    while ( arg ) {
11245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        APPEND(" ");
11345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        APPEND(arg->op);
11445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        APPEND(" ");
11545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        APPEND(arg->param);
11645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
11745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        arg = TAILQ_NEXT(arg, entry);
11845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    }
11945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
12045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /* Append extra arguments. */
12145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (extra) {
12245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        APPEND(" ");
12345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        APPEND(extra);
12445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    }
12545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /* Append final arguments. */
12645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    APPEND(" -x assembler-with-cpp ");
12745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    APPEND(pp->filename);
12845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
12945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return cmdline;
13045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
13145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
13245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* Invoke the c preprocessor. */
13345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void
13445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcpp_invoke(yasm_preproc_cpp *pp)
13545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
13645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    char *cmdline;
13745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
13845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    cmdline = cpp_build_cmdline(pp, NULL);
13945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
14045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifdef HAVE_POPEN
14145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    pp->f = popen(cmdline, "r");
14245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (!pp->f)
14345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        yasm__fatal( N_("Failed to execute preprocessor") );
14445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#else
14545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yasm__fatal( N_("Cannot execute preprocessor, no popen available") );
14645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif
14745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
14845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yasm_xfree(cmdline);
14945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
15045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
15145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* Free memory used by the list of arguments. */
15245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void
15345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcpp_destroy_args(yasm_preproc_cpp *pp)
15445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
15545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    cpp_arg_entry *arg;
15645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
15745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    while ( (arg = TAILQ_FIRST(&pp->cpp_args)) ) {
15845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        TAILQ_REMOVE(&pp->cpp_args, arg, entry);
15945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        yasm_xfree(arg->param);
16045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        yasm_xfree(arg);
16145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    }
16245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
16345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
16445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* Invoke the c preprocessor to generate dependency info. */
16545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void
16645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcpp_generate_deps(yasm_preproc_cpp *pp)
16745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
16845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    char *cmdline;
16945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
17045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    cmdline = cpp_build_cmdline(pp, "-M");
17145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
17245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifdef HAVE_POPEN
17345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    pp->f_deps = popen(cmdline, "r");
17445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (!pp->f_deps)
17545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        yasm__fatal( N_("Failed to execute preprocessor") );
17645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#else
17745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yasm__fatal( N_("Cannot execute preprocessor, no popen available") );
17845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif
17945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
18045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yasm_xfree(cmdline);
18145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
18245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
18345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/*******************************************************************************
18445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    Interface functions.
18545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org*******************************************************************************/
18645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic yasm_preproc *
18745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcpp_preproc_create(const char *in, yasm_symtab *symtab, yasm_linemap *lm,
18845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                   yasm_errwarns *errwarns)
18945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
19045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yasm_preproc_cpp *pp = yasm_xmalloc(sizeof(yasm_preproc_cpp));
19145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    void * iter;
19245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    const char * inc_dir;
19345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
19445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    pp->preproc.module = &yasm_cpp_LTX_preproc;
19545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    pp->f = pp->f_deps = NULL;
19645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    pp->cur_lm = lm;
19745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    pp->errwarns = errwarns;
19845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    pp->flags = 0;
19945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    pp->filename = yasm__xstrdup(in);
20045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
20145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    TAILQ_INIT(&pp->cpp_args);
20245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
20345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /* Iterate through the list of include dirs. */
20445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    iter = NULL;
20545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    while ((inc_dir = yasm_get_include_dir(&iter)) != NULL) {
20645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        cpp_arg_entry *arg = yasm_xmalloc(sizeof(cpp_arg_entry));
20745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        arg->op = "-I";
20845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        arg->param = yasm__xstrdup(inc_dir);
20945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
21045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        TAILQ_INSERT_TAIL(&pp->cpp_args, arg, entry);
21145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    }
21245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
21345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return (yasm_preproc *)pp;
21445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
21545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
21645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void
21745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcpp_preproc_destroy(yasm_preproc *preproc)
21845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
21945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yasm_preproc_cpp *pp = (yasm_preproc_cpp *)preproc;
22045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
22145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (pp->f) {
22245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifdef HAVE_POPEN
22345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        if (pclose(pp->f) != 0)
22445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            yasm__fatal( N_("Preprocessor exited with failure") );
22545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif
22645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    }
22745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
22845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    cpp_destroy_args(pp);
22945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
23045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yasm_xfree(pp->filename);
23145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yasm_xfree(pp);
23245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
23345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
23445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic char *
23545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcpp_preproc_get_line(yasm_preproc *preproc)
23645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
23745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yasm_preproc_cpp *pp = (yasm_preproc_cpp *)preproc;
23845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    int bufsize = BSIZE;
23945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    char *buf, *p;
24045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
24145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (! (pp->flags & CPP_HAS_BEEN_INVOKED) ) {
24245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        pp->flags |= CPP_HAS_BEEN_INVOKED;
24345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
24445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        cpp_invoke(pp);
24545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    }
24645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
24745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /*
24845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        Once the preprocessor has been run, we're just dealing with a normal
24945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        file.
25045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    */
25145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
25245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /* Loop to ensure entire line is read (don't want to limit line length). */
25345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    buf = yasm_xmalloc((size_t)bufsize);
25445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    p = buf;
25545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    for (;;) {
25645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        if (!fgets(p, bufsize-(p-buf), pp->f)) {
25745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            if (ferror(pp->f)) {
25845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                yasm_error_set(YASM_ERROR_IO,
25945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                               N_("error when reading from file"));
26045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                yasm_errwarn_propagate(pp->errwarns,
26145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                    yasm_linemap_get_current(pp->cur_lm));
26245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            }
26345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            break;
26445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        }
26545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        p += strlen(p);
26645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        if (p > buf && p[-1] == '\n')
26745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            break;
26845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        if ((p-buf) >= bufsize) {
26945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            /* Increase size of buffer */
27045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            char *oldbuf = buf;
27145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            bufsize *= 2;
27245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            buf = yasm_xrealloc(buf, (size_t)bufsize);
27345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            p = buf + (p-oldbuf);
27445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        }
27545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    }
27645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
27745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (p == buf) {
27845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        /* No data; must be at EOF */
27945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        yasm_xfree(buf);
28045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        return NULL;
28145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    }
28245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
28345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /* Strip the line ending */
28445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    buf[strcspn(buf, "\r\n")] = '\0';
28545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
28645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return buf;
28745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
28845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
28945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic size_t
29045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcpp_preproc_get_included_file(yasm_preproc *preproc, char *buf,
29145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                              size_t max_size)
29245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
29345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    char *p = buf;
29445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    int ch = '\0';
29545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    size_t n = 0;
29645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yasm_preproc_cpp *pp = (yasm_preproc_cpp *)preproc;
29745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
29845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (! (pp->flags & CPP_HAS_GENERATED_DEPS) ) {
29945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        pp->flags |= CPP_HAS_GENERATED_DEPS;
30045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
30145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        cpp_generate_deps(pp);
30245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
30345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        /* Skip target name and first dependency. */
30445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        while (ch != ':')
30545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            ch = fgetc(pp->f_deps);
30645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
30745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        fgetc(pp->f_deps);      /* Discard space after colon. */
30845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
30945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        while (ch != ' ' && ch != EOF)
31045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            ch = fgetc(pp->f_deps);
31145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
31245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        if (ch == EOF)
31345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            return 0;
31445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    }
31545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
31645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    while (n < max_size) {
31745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        ch = fgetc(pp->f_deps);
31845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
31945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        if (ch == ' ' || ch == EOF) {
32045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            *p = '\0';
32145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            return n;
32245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        }
32345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
32445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        /* Eat any silly characters. */
32545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        if (ch < ' ')
32645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            continue;
32745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
32845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        *p++ = ch;
32945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        n++;
33045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    }
33145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
33245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /* Ensure the buffer is null-terminated. */
33345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    *(p - 1) = '\0';
33445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return n;
33545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
33645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
33745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void
33845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcpp_preproc_add_include_file(yasm_preproc *preproc, const char *filename)
33945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
34045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yasm_preproc_cpp *pp = (yasm_preproc_cpp *)preproc;
34145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
34245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    cpp_arg_entry *arg = yasm_xmalloc(sizeof(cpp_arg_entry));
34345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    arg->op = "-include";
34445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    arg->param = yasm__xstrdup(filename);
34545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
34645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    TAILQ_INSERT_TAIL(&pp->cpp_args, arg, entry);
34745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
34845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
34945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void
35045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcpp_preproc_predefine_macro(yasm_preproc *preproc, const char *macronameval)
35145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
35245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yasm_preproc_cpp *pp = (yasm_preproc_cpp *)preproc;
35345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
35445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    cpp_arg_entry *arg = yasm_xmalloc(sizeof(cpp_arg_entry));
35545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    arg->op = "-D";
35645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    arg->param = yasm__xstrdup(macronameval);
35745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
35845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    TAILQ_INSERT_TAIL(&pp->cpp_args, arg, entry);
35945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
36045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
36145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void
36245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcpp_preproc_undefine_macro(yasm_preproc *preproc, const char *macroname)
36345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
36445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yasm_preproc_cpp *pp = (yasm_preproc_cpp *)preproc;
36545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
36645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    cpp_arg_entry *arg = yasm_xmalloc(sizeof(cpp_arg_entry));
36745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    arg->op = "-U";
36845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    arg->param = yasm__xstrdup(macroname);
36945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
37045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    TAILQ_INSERT_TAIL(&pp->cpp_args, arg, entry);
37145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
37245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
37345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void
37445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcpp_preproc_define_builtin(yasm_preproc *preproc, const char *macronameval)
37545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
37645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /* Handle a builtin as if it were a predefine. */
37745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    cpp_preproc_predefine_macro(preproc, macronameval);
37845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
37945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
38045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void
38145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcpp_preproc_add_standard(yasm_preproc *preproc, const char **macros)
38245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
38345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /* TODO */
38445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
38545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
38645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/*******************************************************************************
38745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    Preprocessor module object.
38845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org*******************************************************************************/
38945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
39045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgyasm_preproc_module yasm_cpp_LTX_preproc = {
39145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    "Run input through external C preprocessor",
39245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    "cpp",
39345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    cpp_preproc_create,
39445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    cpp_preproc_destroy,
39545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    cpp_preproc_get_line,
39645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    cpp_preproc_get_included_file,
39745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    cpp_preproc_add_include_file,
39845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    cpp_preproc_predefine_macro,
39945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    cpp_preproc_undefine_macro,
40045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    cpp_preproc_define_builtin,
40145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    cpp_preproc_add_standard
40245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org};
403