1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
// This file is part of the AspectC++ compiler 'ac++'.
// Copyright (C) 1999-2004 The 'ac++' developers (see aspectc.org)
//
// 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 the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
// MA 02111-1307 USA
#include "StdSystem.h"
#include "file.h"
using namespace file;
//Puma includes
#include "Puma/SysCall.h"
#include "Puma/VerboseMgr.h"
//stdc++ includes
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
StdSystem::StdSystem(ErrorStream& e, AGxxConfig& config, const string& exec_str) :
System(e, config, "")
{
// create temporary files
char* tmp_file;
// tmp file for stdout
tmp_file = (char*) Puma::SysCall::mktemp("agxx_stdout", &_err);
_tmp_stdout_file = tmp_file;
free(tmp_file);
// tmp file for stderr
tmp_file = (char*) Puma::SysCall::mktemp("agxx_stderr", &_err);
_tmp_stderr_file = tmp_file;
free(tmp_file);
// append shell commands to execution string which
// write the output into temproray files
#if defined (WIN32)
this->_exec_str="\""+exec_str +" 2>"+_tmp_stderr_file+" 1>"+_tmp_stdout_file+"\"";
#else
this->_exec_str = exec_str + " 2>" + _tmp_stderr_file + " 1>"
+ _tmp_stdout_file;
#endif
}
bool
StdSystem::execute()
{
VerboseMgr vm(cout, _config.verbose());
bool ret;
if ((ret = System::execute()))
{
//read files into strings
_stdout_str = read_file(_tmp_stdout_file.c_str());
_stderr_str = read_file(_tmp_stderr_file.c_str());
}
else
{
print_file(_tmp_stderr_file.c_str());
}
vm++;
vm << "removing temporary file: " << _tmp_stdout_file.c_str() << endvm;
if (remove(_tmp_stdout_file.c_str()) < 0)
{
_err << sev_warning << "Could not remove temporary file : "
<< _tmp_stdout_file.c_str() << endMessage;
}
vm << "removing temporary file: " << _tmp_stderr_file.c_str() << endvm;
if (remove(_tmp_stderr_file.c_str()) < 0)
{
_err << sev_warning << "Could not remove temporary file : "
<< _tmp_stderr_file.c_str() << endMessage;
}
return ret;
}
string
StdSystem::read_file(const char* fname)
{
string line, s;
ifstream fs(fname);
if (fs)
{
while (getline(fs, line))
{
s += line + '\n';
}
fs.close();
}
else
{
_err << sev_error << "Could not open file :" << fname << endMessage;
s = "";
}
return s;
}
void
StdSystem::print_file(const char* fname)
{
string line;
ifstream fs(fname);
if (fs)
{
while (getline(fs, line))
{
cerr << line << endl;
}
fs.close();
}
else
{
_err << sev_error << "Could not open file :" << fname << endMessage;
}
}
|