/*
- * Copyright (c) 2003 The Regents of The University of Michigan
+ * Copyright (c) 2001-2005 The Regents of The University of Michigan
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
*/
#define USE_CPP
-// #define CPP_PIPE
-
#ifdef USE_CPP
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/wait.h>
-#if defined(__OpenBSD__)
#include <libgen.h>
-#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fstream>
#include <iostream>
-#if __GNUC__ >= 3
-#include <ext/stdio_filebuf.h>
-#endif
#include <vector>
#include <string>
bool
IniFile::loadCPP(const string &file, vector<char *> &cppArgs)
{
- int fd[2];
-
// Open the file just to verify that we can. Otherwise if the
// file doesn't exist or has bad permissions the user will get
// confusing errors from cpp/g++.
tmpf.close();
- const char *cfile = file.c_str();
- char *dir = basename(cfile);
+ char *cfile = strncpy(new char[file.size() + 1], file.c_str(),
+ file.size());
+ char *dir = dirname(cfile);
char *dir_arg = NULL;
- if (*dir != '.' && dir != cfile) {
+ if (*dir != '.') {
string arg = "-I";
arg += dir;
dir_arg = new char[arg.size() + 1];
- strcpy(dir_arg, arg.c_str());
+ strncpy(dir_arg, arg.c_str(), arg.size());
}
-#ifdef CPP_PIPE
- if (pipe(fd) == -1)
- return false;
-#else
+ delete [] cfile;
+
char tempfile[] = "/tmp/configXXXXXX";
- fd[0] = fd[1] = mkstemp(tempfile);
-#endif
+ int tmp_fd = mkstemp(tempfile);
int pid = fork();
if (pid == -1)
- return 1;
+ return false;
if (pid == 0) {
char filename[FILENAME_MAX];
args[nextArg++] = NULL;
close(STDOUT_FILENO);
- if (dup2(fd[1], STDOUT_FILENO) == -1)
+ if (dup2(tmp_fd, STDOUT_FILENO) == -1)
exit(1);
execvp("g++", args);
- exit(1);
+ exit(0);
}
int retval;
if (!WIFEXITED(retval) || WEXITSTATUS(retval) != 0)
return false;
-#ifdef CPP_PIPE
- close(fd[1]);
-#else
- lseek(fd[0], 0, SEEK_SET);
-#endif
+ close(tmp_fd);
bool status = false;
-#if __GNUC__ >= 3
- using namespace __gnu_cxx;
- stdio_filebuf<char> fbuf(fd[0], ios_base::in, true,
- static_cast<stdio_filebuf<char>::int_type>(BUFSIZ));
-
- if (fbuf.is_open()) {
- istream f(&fbuf);
- status = load(f);
- }
-
-#else
- ifstream f(fd[0]);
- if (f.is_open())
- status = load(f);
-#endif
+ status = load(tempfile);
-#ifndef CPP_PIPE
unlink(tempfile);
-#endif
return status;
}
return true;
}
-bool
-IniFile::findDefault(const string &_section, const string &entry,
- string &value) const
-{
- string section = _section;
- while (!findAppend(section, entry, value)) {
- if (!find(section, "default", section)) {
- return false;
- }
- }
-
- return true;
-}
-
-bool
-IniFile::findAppend(const string &_section, const string &entry,
- string &value) const
-{
- string section = _section;
- bool ret = false;
- bool first = true;
-
- do {
- string val;
- if (find(section, entry, val)) {
- ret = true;
- if (first) {
- value = val;
- first = false;
- } else {
- value += " ";
- value += val;
- }
-
- }
- } while (find(section, "append", section));
-
- return ret;
-}
-
-
bool
IniFile::sectionExists(const string §ionName) const
{