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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
/*
* run.cc: Part of GNU CSSC.
*
*
* Copyright (C) 1997,1998,1999,2001,2002,2007 Free Software Foundation, Inc.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* CSSC was originally Based on MySC, by Ross Ridge, which was
* placed in the Public Domain.
*
*
* Routines for running programmes.
*
*/
#include "cssc.h"
#include "run.h"
#include "mylist.h"
#include "sysdep.h"
#include "err_no.h"
#ifdef CONFIG_SCCS_IDS
static const char rcs_id[] = "CSSC $Id: run.cc,v 1.29 2007/12/17 21:59:50 jay Exp $";
#endif
#if !defined(HAVE_FORK) && !defined(HAVE_SPAWN)
#define NEED_CALL_SYSTEM
#else
#undef NEED_CALL_SYSTEM
#endif
// According to the ANSI standard, id the argument to system()
// is not NULL, the return value is implementation-defined.
//
// PROBLEM: system() returns an implementation-defined value
// unless its argument is NULL. This means that we cannot use it where
// we care about the meaning of the return value. This in turn means that
// MR validation will never fail (that is, it won't fail when it is
// supposed to, it will instead succeed all the time).
//
// Under Unix, system() returns a value which should be examined with
// the use of the WEXITSTATUS macro. The diagnosed return value is -1
// or 127 for two kinds of failure not involving the called program,
// and otherwise the return value of the program. Success is
// indicated by a zero return value. However, Unix also has fork() and
// so we wouldn't be using this function on Unix. Nevertheless it's a
// valid choice to manually undefine HAVE_FORK, and so we support this
// by the use of WEXITSTATUS.
//
// XXX: this code probably won't work on many systems because we don't
// know how to interpret the result of system().
#ifdef NEED_CALL_SYSTEM
static bool call_system(const char *s)
{
int failed;
int ret;
errno = 0;
ret = system(s);
#ifdef WEXITSTATUS
failed = (WEXITSTATUS(ret) != 0);
#else
#ifdef SYSTEM_FAILS_RETURNING_MINUS_ONE
failed = (-1 == ret);
#else
failed = (ret != 0);
#endif
#endif
if (errno)
{
errormsg_with_errno("call_system(\"%s\") failed (returned %d).", s, ret);
return false;
}
if (failed)
return false;
else
return true;
}
#endif
/* Runs a programme and returns its exit status. */
int
run(const char *prg, mylist<const char *> const &args) {
int i;
int len = args.length();
#ifdef NEED_CALL_SYSTEM
int cmdlen = strlen(prg) + 1;
for(i = 0; i < len; i++) {
cmdlen += strlen(args[i]) + 1;
}
char *s = new char[cmdlen+1];
strcpy(s, prg);
for(i = 0; i < len; i++) {
strcat(s, " ");
strcat(s, args[i]);
}
bool sysfail = !call_system(s);
delete [] s;
if (sysfail)
return -1;
else
return 0;
#else /* !(HAVE_FORK) && !(HAVE_SPAWN) */
const char * *argv = new const char*[len+2];
argv[0] = prg;
for(i = 0; i < len; i++) {
argv[i + 1] = args[i];
}
argv[i + 1] = NULL;
#ifndef HAVE_FORK
// Use spawn() instead then
int ret = spawnvp(P_WAIT, (char *) prg, (char **) argv);
if (ret == -1)
{
errormsg_with_errno("spawnvp(\"%s\") failed.", prg);
}
#else /* HAVE_FORK */
// We _do_ have fork().
// SunOS 4.1.3 appears not to like fflush(NULL).
#if 0
fflush(NULL);
#else
fflush(stdout);
fflush(stderr);
#endif
pid_t pid = fork();
if (pid < 0) {
fatal_quit(errno, "fork() failed.");
}
if (pid == 0) {
cleanup::set_in_child();
execvp(prg, (char **) argv);
fatal_quit(errno, "execvp(\"%s\") failed.", prg);
}
int ret;
pid_t r = wait(&ret);
while (r != pid) {
if (r == -1 && errno != EINTR) {
perror("wait()"); // probably ECHILD.
break;
}
r = wait(&ret);
}
#endif /* CONFIG_NO_FORK */
delete [] argv;
return ret;
#endif /* !(HAVE_FORK) && !(HAVE_SPAWN) */
}
inline mylist<const char*> &
operator +=(mylist<const char*> &l1, mylist<mystring> const &l2)
{
int len = l2.length();
int i;
for(i = 0; i < len; i++)
{
// This add operation would be push_back() under STL.
// When everybody supports STL, we'll switch.
l1.add(l2[i].c_str());
}
return l1;
}
/* Runs a program to check MRs. */
int
run_mr_checker(const char *prg, const char *arg1, mylist<mystring> mrs)
{
// If the validation flag is set but has no value, PRG will be an
// empty string and the validation should succeed silently. This is
// for compatibility with "real" SCCS.
if (prg[0])
{
mylist<const char *> args;
args.add(arg1);
int len = mrs.length();
for(int i = 0; i < len; i++)
args.add(mrs[i].c_str()); // STL's push_back
return run(prg, args);
}
else
{
return 0;
}
}
/* Local variables: */
/* mode: c++ */
/* End: */
|