#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include "distcc.h"
#include "trace.h"
#include "util.h"
#include "exitcode.h"
#include "cpp_dialect.h"
#include "filename.h"
char * dcc_find_extension(char *sfile)
{
char *dot;
dot = strrchr(sfile, '.');
if (dot == NULL || dot[1] == '\0') {
return NULL;
}
return dot;
}
static int dcc_set_file_extension(const char *sfile,
const char *new_ext,
char **ofile)
{
char *dot, *o;
o = strdup(sfile);
dot = dcc_find_extension((char *) o);
if (!dot) {
rs_log_error("couldn't find extension in \"%s\"", o);
return -1;
}
if (strlen(dot) < strlen(new_ext)) {
rs_log_error("not enough space for new extension");
return -1;
}
strcpy(dot, new_ext);
*ofile = o;
return 0;
}
const char * dcc_preproc_exten(const char *e)
{
if (e[0] != '.')
return NULL;
e++;
if (seen_opt_x) {
return opt_x_ext;;
}
if (!strcmp(e, "i") || !strcmp(e, "c")) {
return ".i";
} else if (!strcmp(e, "c") || !strcmp(e, "cc")
|| !strcmp(e, "cpp") || !strcmp(e, "cxx")
|| !strcmp(e, "cp") || !strcmp(e, "c++")
|| !strcmp(e, "C") || !strcmp(e, "ii")) {
return ".ii";
} else if (!strcmp(e, "m") || !strcmp(e, "mi")) {
return ".mi";
} else if (!strcmp(e, "mm") || !strcmp(e, "M") || !strcmp(e, "mii")) {
return ".mii";
} else if (!strcasecmp(e, "s")) {
return ".s";
} else {
return NULL;
}
}
int dcc_is_preprocessed(const char *sfile)
{
const char *dot, *ext;
dot = dcc_find_extension((char *) sfile);
if (!dot)
return 0;
ext = dot+1;
switch (ext[0]) {
#ifdef ENABLE_REMOTE_ASSEMBLE
case 's':
return !strcmp(ext, "s");
#endif
case 'i':
return !strcmp(ext, "i")
|| !strcmp(ext, "ii");
case 'm':
return !strcmp(ext, "mi")
|| !strcmp(ext, "mii");
default:
return 0;
}
}
int dcc_is_source(const char *sfile)
{
const char *dot, *ext;
dot = dcc_find_extension((char *) sfile);
if (!dot)
return 0;
ext = dot+1;
switch (ext[0]) {
case 'i':
return !strcmp(ext, "i")
|| !strcmp(ext, "ii");
case 'c':
return !strcmp(ext, "c")
|| !strcmp(ext, "cc")
|| !strcmp(ext, "cpp")
|| !strcmp(ext, "cxx")
|| !strcmp(ext, "cp")
|| !strcmp(ext, "c++");
case 'C':
return !strcmp(ext, "C");
case 'm':
return !strcmp(ext, "m")
|| !strcmp(ext, "mi")
|| !strcmp(ext, "mii")
|| !strcmp(ext, "mm");
case 'M':
return !strcmp(ext, "M");
#ifdef ENABLE_REMOTE_ASSEMBLE
case 's':
return !strcmp(ext, "s");
case 'S':
return !strcmp(ext, "S");
#endif
default:
return 0;
}
}
int dcc_output_from_source(const char *sfile,
const char *out_extn,
char **ofile)
{
char *slash;
int l;
if ((slash = strrchr(sfile, '/')))
sfile = slash+1;
if ((l = strlen(sfile)) < 3) {
rs_log_error("source file %s is bogus", sfile);
return -1;
}
return dcc_set_file_extension(sfile, out_extn, ofile);
}