2002-06-04 05:25:37 +00:00
|
|
|
/*
|
|
|
|
cpp.c
|
|
|
|
|
|
|
|
cpp preprocessing support
|
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
Copyright (C) 2002 Bill Currie <bill@taniwha.org>
|
2002-06-04 05:25:37 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2002/06/04
|
2002-06-04 05:25:37 +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
|
|
|
|
of the License, 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:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
|
|
|
# include <sys/wait.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_PROCESS_H
|
|
|
|
# include <process.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
2007-04-11 22:03:36 +00:00
|
|
|
#ifdef HAVE_IO_H
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
2002-06-04 05:25:37 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "QF/dstring.h"
|
|
|
|
|
2020-06-21 14:15:17 +00:00
|
|
|
#include "tools/qfcc/include/cpp.h"
|
|
|
|
#include "tools/qfcc/include/diagnostic.h"
|
|
|
|
#include "tools/qfcc/include/options.h"
|
2002-06-04 05:25:37 +00:00
|
|
|
|
|
|
|
typedef struct cpp_arg_s {
|
|
|
|
struct cpp_arg_s *next;
|
|
|
|
const char *arg;
|
|
|
|
} cpp_arg_t;
|
|
|
|
|
|
|
|
cpp_arg_t *cpp_arg_list;
|
|
|
|
cpp_arg_t **cpp_arg_tail = &cpp_arg_list;
|
|
|
|
cpp_arg_t *cpp_def_list;
|
|
|
|
cpp_arg_t **cpp_def_tail = &cpp_def_list;
|
2020-02-18 17:31:16 +00:00
|
|
|
cpp_arg_t *cpp_undef_list;
|
|
|
|
cpp_arg_t **cpp_undef_tail = &cpp_undef_list;
|
|
|
|
cpp_arg_t *cpp_sysinc_list;
|
|
|
|
cpp_arg_t **cpp_sysinc_tail = &cpp_sysinc_list;
|
2002-06-04 05:25:37 +00:00
|
|
|
const char **cpp_argv;
|
|
|
|
const char *cpp_name = CPP_NAME;
|
|
|
|
static int cpp_argc = 0;
|
|
|
|
dstring_t *tempname;
|
|
|
|
|
2020-02-18 17:31:16 +00:00
|
|
|
static const char **
|
|
|
|
append_cpp_args (const char **arg, cpp_arg_t *arg_list)
|
|
|
|
{
|
|
|
|
cpp_arg_t *cpp_arg;
|
|
|
|
|
|
|
|
for (cpp_arg = arg_list; cpp_arg; cpp_arg = cpp_arg->next)
|
|
|
|
*arg++ = cpp_arg->arg;
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
2002-06-04 05:25:37 +00:00
|
|
|
static void
|
|
|
|
add_cpp_arg (const char *arg)
|
|
|
|
{
|
|
|
|
cpp_arg_t *cpp_arg = malloc (sizeof (cpp_arg_t));
|
|
|
|
cpp_arg->next = 0;
|
|
|
|
cpp_arg->arg = arg;
|
|
|
|
*cpp_arg_tail = cpp_arg;
|
|
|
|
cpp_arg_tail = &(*cpp_arg_tail)->next;
|
|
|
|
cpp_argc++;
|
|
|
|
}
|
|
|
|
|
2020-02-18 17:31:16 +00:00
|
|
|
void
|
|
|
|
add_cpp_sysinc (const char *arg)
|
|
|
|
{
|
|
|
|
cpp_arg_t *cpp_arg = malloc (sizeof (cpp_arg_t));
|
|
|
|
cpp_arg->next = 0;
|
|
|
|
cpp_arg->arg = arg;
|
|
|
|
*cpp_sysinc_tail = cpp_arg;
|
|
|
|
cpp_sysinc_tail = &(*cpp_sysinc_tail)->next;
|
|
|
|
cpp_argc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
add_cpp_undef (const char *arg)
|
|
|
|
{
|
|
|
|
cpp_arg_t *cpp_arg = malloc (sizeof (cpp_arg_t));
|
|
|
|
cpp_arg->next = 0;
|
|
|
|
cpp_arg->arg = arg;
|
|
|
|
*cpp_undef_tail = cpp_arg;
|
|
|
|
cpp_undef_tail = &(*cpp_undef_tail)->next;
|
|
|
|
cpp_argc++;
|
|
|
|
}
|
|
|
|
|
2002-06-04 05:25:37 +00:00
|
|
|
void
|
|
|
|
add_cpp_def (const char *arg)
|
|
|
|
{
|
|
|
|
cpp_arg_t *cpp_def = malloc (sizeof (cpp_arg_t));
|
|
|
|
cpp_def->next = 0;
|
|
|
|
cpp_def->arg = arg;
|
|
|
|
*cpp_def_tail = cpp_def;
|
|
|
|
cpp_def_tail = &(*cpp_def_tail)->next;
|
|
|
|
cpp_argc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-01-06 18:28:13 +00:00
|
|
|
parse_cpp_name (void)
|
2002-06-04 05:25:37 +00:00
|
|
|
{
|
|
|
|
char *n;
|
|
|
|
|
2002-06-09 03:57:20 +00:00
|
|
|
if (!cpp_name)
|
|
|
|
return;
|
2002-06-04 05:25:37 +00:00
|
|
|
n = strdup (cpp_name);
|
|
|
|
while (*n) {
|
|
|
|
while (*n && *n == ' ')
|
|
|
|
n++;
|
|
|
|
add_cpp_arg (n);
|
|
|
|
while (*n && *n != ' ')
|
|
|
|
n++;
|
|
|
|
if (*n)
|
|
|
|
*n++ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
build_cpp_args (const char *in_name, const char *out_name)
|
|
|
|
{
|
|
|
|
cpp_arg_t *cpp_arg;
|
|
|
|
const char **arg;
|
|
|
|
|
|
|
|
if (cpp_argv)
|
|
|
|
free (cpp_argv);
|
2018-09-07 11:00:57 +00:00
|
|
|
cpp_argv = (const char **)malloc ((cpp_argc + 1) * sizeof (char *));
|
2002-06-04 05:25:37 +00:00
|
|
|
for (arg = cpp_argv, cpp_arg = cpp_arg_list;
|
|
|
|
cpp_arg;
|
|
|
|
cpp_arg = cpp_arg->next) {
|
2020-02-18 17:31:16 +00:00
|
|
|
if (!strcmp (cpp_arg->arg, "%u")) {
|
|
|
|
arg = append_cpp_args (arg, cpp_undef_list);
|
|
|
|
} else if (!strcmp (cpp_arg->arg, "%s")) {
|
|
|
|
arg = append_cpp_args (arg, cpp_sysinc_list);
|
|
|
|
} else if (!strcmp (cpp_arg->arg, "%d")) {
|
|
|
|
arg = append_cpp_args (arg, cpp_def_list);
|
2002-06-04 05:25:37 +00:00
|
|
|
} else if (!strcmp (cpp_arg->arg, "%i")) {
|
|
|
|
*arg++ = in_name;
|
|
|
|
} else if (!strcmp (cpp_arg->arg, "%o")) {
|
2002-08-13 22:02:07 +00:00
|
|
|
if (!options.preprocess_only) {
|
|
|
|
*arg++ = out_name;
|
|
|
|
}
|
2002-06-04 05:25:37 +00:00
|
|
|
} else {
|
2002-08-13 22:02:07 +00:00
|
|
|
if (!options.preprocess_only || strcmp (cpp_arg->arg, "-o") != 0)
|
|
|
|
*arg++ = cpp_arg->arg;
|
2002-06-04 05:25:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*arg = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
|
2006-05-24 14:35:39 +00:00
|
|
|
void
|
2006-08-20 06:20:30 +00:00
|
|
|
intermediate_file (dstring_t *ifile, const char *filename, const char *ext,
|
|
|
|
int local)
|
2006-05-24 14:35:39 +00:00
|
|
|
{
|
|
|
|
if (options.save_temps) {
|
|
|
|
char *basename = strdup (filename);
|
|
|
|
char *temp;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2006-05-24 14:50:24 +00:00
|
|
|
temp = strrchr (basename, '/');
|
|
|
|
if (!temp)
|
|
|
|
temp = basename;
|
|
|
|
temp = strrchr (temp, '.');
|
2006-05-24 14:35:39 +00:00
|
|
|
if (temp)
|
|
|
|
*temp = '\0'; // ignore the rest of the string
|
|
|
|
|
|
|
|
temp = strrchr (basename, '/');
|
|
|
|
if (!temp)
|
|
|
|
temp = basename;
|
|
|
|
else
|
|
|
|
temp++;
|
|
|
|
|
|
|
|
if (*sourcedir) {
|
|
|
|
dsprintf (ifile, "%s%c%s.%s", sourcedir,
|
|
|
|
PATH_SEPARATOR, temp, ext);
|
|
|
|
} else {
|
|
|
|
dsprintf (ifile, "%s.%s", temp, ext);
|
|
|
|
}
|
|
|
|
free (basename);
|
2007-03-31 15:26:58 +00:00
|
|
|
} else if (local) {
|
2006-08-20 06:20:30 +00:00
|
|
|
char *temp2 = strrchr (this_program, PATH_SEPARATOR);
|
|
|
|
dsprintf (ifile, "%sXXXXXX", temp2 ? temp2 + 1 : this_program);
|
2006-05-24 14:35:39 +00:00
|
|
|
} else {
|
2006-05-24 14:50:24 +00:00
|
|
|
const char *temp1 = getenv ("TMPDIR");
|
|
|
|
char *temp2 = strrchr (this_program, PATH_SEPARATOR);
|
|
|
|
|
2006-05-24 14:35:39 +00:00
|
|
|
if ((!temp1) || (!temp1[0])) {
|
|
|
|
temp1 = getenv ("TEMP");
|
|
|
|
if ((!temp1) || (!temp1[0])) {
|
|
|
|
temp1 = "/tmp";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dsprintf (ifile, "%s%c%sXXXXXX", temp1,
|
|
|
|
PATH_SEPARATOR, temp2 ? temp2 + 1 : this_program);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-04 05:25:37 +00:00
|
|
|
FILE *
|
2006-05-24 14:50:24 +00:00
|
|
|
preprocess_file (const char *filename, const char *ext)
|
2002-06-04 05:25:37 +00:00
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
pid_t pid;
|
|
|
|
int tempfd = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (cpp_name) {
|
2006-08-20 06:20:30 +00:00
|
|
|
intermediate_file (tempname, filename, ext ? ext : "p", 0);
|
2002-06-04 05:25:37 +00:00
|
|
|
build_cpp_args (filename, tempname->str);
|
2018-09-07 13:30:44 +00:00
|
|
|
if (!cpp_argv[0]) {
|
|
|
|
internal_error(0, "cpp_argv[0] is null");
|
|
|
|
}
|
2002-06-04 05:25:37 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2003-03-24 17:40:27 +00:00
|
|
|
if (!options.save_temps && !options.preprocess_only)
|
2002-06-04 05:25:37 +00:00
|
|
|
mktemp (tempname->str);
|
|
|
|
|
|
|
|
{
|
2007-04-07 10:24:34 +00:00
|
|
|
FILE *tmp = fopen (tempname->str, "wb");
|
2003-03-10 21:00:48 +00:00
|
|
|
if (tmp == NULL) {
|
|
|
|
fprintf (stderr, "%s: qfcc was unable to open\n",
|
|
|
|
tempname->str);
|
2006-08-20 06:20:30 +00:00
|
|
|
return 0;
|
2003-03-10 21:00:48 +00:00
|
|
|
}
|
2002-06-04 05:25:37 +00:00
|
|
|
fclose (tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2002-08-02 19:09:02 +00:00
|
|
|
int status;
|
|
|
|
|
|
|
|
if (options.verbosity > 1) {
|
|
|
|
const char **a;
|
|
|
|
for (a = cpp_argv; *a; a++)
|
|
|
|
printf ("%s ", *a);
|
|
|
|
puts("");
|
|
|
|
}
|
|
|
|
|
2021-03-27 11:09:37 +00:00
|
|
|
#if defined(_WIN64) || defined(_WIN32)
|
2013-01-22 04:27:39 +00:00
|
|
|
status = spawnvp (_P_WAIT, cpp_argv[0], (char **) cpp_argv);
|
|
|
|
#else
|
2004-01-13 03:07:36 +00:00
|
|
|
status = spawnvp (_P_WAIT, cpp_argv[0], cpp_argv);
|
2013-01-22 04:27:39 +00:00
|
|
|
#endif
|
2002-06-04 05:25:37 +00:00
|
|
|
|
|
|
|
if (status) {
|
|
|
|
fprintf (stderr, "%s: cpp returned error code %d\n",
|
|
|
|
filename,
|
|
|
|
status);
|
2006-08-20 06:20:30 +00:00
|
|
|
return 0;
|
2002-06-04 05:25:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-13 22:02:07 +00:00
|
|
|
if (options.preprocess_only)
|
|
|
|
return 0;
|
2007-04-07 10:24:34 +00:00
|
|
|
return fopen (tempname->str, "rb");
|
2002-06-04 05:25:37 +00:00
|
|
|
#else
|
2003-03-24 17:40:27 +00:00
|
|
|
if (!options.save_temps && !options.preprocess_only)
|
2002-06-04 05:25:37 +00:00
|
|
|
tempfd = mkstemp (tempname->str);
|
|
|
|
|
|
|
|
if ((pid = fork ()) == -1) {
|
|
|
|
perror ("fork");
|
2006-08-20 06:20:30 +00:00
|
|
|
return 0;
|
2002-06-04 05:25:37 +00:00
|
|
|
}
|
|
|
|
if (!pid) {
|
|
|
|
// we're a child, check for abuse
|
2002-07-20 05:20:40 +00:00
|
|
|
if (options.verbosity > 1) {
|
|
|
|
const char **a;
|
|
|
|
for (a = cpp_argv; *a; a++)
|
|
|
|
printf ("%s ", *a);
|
|
|
|
puts("");
|
|
|
|
}
|
2012-08-19 04:18:38 +00:00
|
|
|
#ifdef HAVE_EXECVP
|
2002-06-04 05:25:37 +00:00
|
|
|
execvp (cpp_argv[0], (char **)cpp_argv);
|
2012-08-19 04:18:38 +00:00
|
|
|
#else
|
|
|
|
execve (cpp_argv[0], (char **)cpp_argv, environ);
|
|
|
|
#endif
|
2002-07-17 19:56:30 +00:00
|
|
|
perror (cpp_argv[0]);
|
2002-06-04 05:25:37 +00:00
|
|
|
exit (1);
|
|
|
|
} else {
|
|
|
|
// give parental guidance (or bury it in the back yard)
|
|
|
|
int status;
|
|
|
|
pid_t rc;
|
|
|
|
|
|
|
|
// printf ("pid = %d\n", pid);
|
2012-08-19 04:18:38 +00:00
|
|
|
#ifdef HAVE_WAITPID
|
|
|
|
rc = waitpid (0, &status, 0 | WUNTRACED);
|
|
|
|
#else
|
|
|
|
rc = wait (&status);
|
|
|
|
#endif
|
|
|
|
if ((rc) != pid) {
|
2002-06-04 05:25:37 +00:00
|
|
|
if (rc == -1) {
|
|
|
|
perror ("wait");
|
2006-08-20 06:20:30 +00:00
|
|
|
return 0;
|
2002-06-04 05:25:37 +00:00
|
|
|
}
|
|
|
|
fprintf (stderr, "%s: The wrong child (%ld) died. Don't ask me, I don't know either.\n",
|
|
|
|
this_program,
|
|
|
|
(long) rc);
|
2006-08-20 06:20:30 +00:00
|
|
|
return 0;
|
2002-06-04 05:25:37 +00:00
|
|
|
}
|
|
|
|
if (WIFEXITED (status)) {
|
|
|
|
if (WEXITSTATUS (status)) {
|
|
|
|
fprintf (stderr, "%s: cpp returned error code %d\n",
|
|
|
|
filename,
|
|
|
|
WEXITSTATUS (status));
|
2006-08-20 06:20:30 +00:00
|
|
|
return 0;
|
2002-06-04 05:25:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf (stderr, "%s: cpp returned prematurely.\n", filename);
|
2006-08-20 06:20:30 +00:00
|
|
|
return 0;
|
2002-06-04 05:25:37 +00:00
|
|
|
}
|
|
|
|
}
|
2002-08-13 22:02:07 +00:00
|
|
|
if (options.preprocess_only)
|
|
|
|
return 0;
|
|
|
|
else if (options.save_temps)
|
2007-04-07 10:24:34 +00:00
|
|
|
return fopen (tempname->str, "rb");
|
2003-03-24 17:40:27 +00:00
|
|
|
else {
|
2007-04-07 10:24:34 +00:00
|
|
|
return fdopen (tempfd, "rb");
|
2003-03-24 17:40:27 +00:00
|
|
|
}
|
2002-06-04 05:25:37 +00:00
|
|
|
#endif
|
|
|
|
}
|
2007-04-07 10:24:34 +00:00
|
|
|
return fopen (filename, "rb");
|
2002-06-04 05:25:37 +00:00
|
|
|
}
|