map_file = fopen(options->
map,
"r");
94 if(map_file ==
NULL) {
95 if(errno != ENOENT) {
97 "%s: Unable to open lock map %s: %s; leaving base as %s.\n",
102 while(fgets(line,
sizeof(line), map_file) !=
NULL) {
104 if(sscanf(line,
" %s %s",
key,
value) == 2
107sprintf(new_base,
"make_%s",
value);
108fprintf(stderr,
"%s: Adjusting base from %s to %s per %s.\n",
110options->
base= new_base;
130 if(
strcmp((*argv)[1],
"-base") == 0 && *argc > 2) {
131options->
base= (*argv)[2];
134}
else if(
strcmp((*argv)[1],
"-getter") == 0 && *argc > 2) {
135options->
getter= (*argv)[2];
138}
else if(
strcmp((*argv)[1],
"-log") == 0 && *argc > 2) {
139options->
logfile= (*argv)[2];
142}
else if(
strcmp((*argv)[1],
"-map") == 0 && *argc > 2) {
143options->
map= (*argv)[2];
146}
else if(
strcmp((*argv)[1],
"-reviewer") == 0 && *argc > 2) {
150}
else if((*argv)[1][0] ==
'-') {
151fprintf(stderr,
"%s: Unsupported option %s.\n",
154}
else if(
strcmp((*argv)[1],
"!") == 0) {
163 if(options->
base==
NULL&& *argc > 1) {
164 const char* p = strrchr((*argv)[1],
'/');
166options->
base= p + 1;
168options->
base= (*argv)[1];
177 while(
n> 0 && (*p & 0xe0) == 0x20) {
181 return n== 0 ?
NULL: p;
192 if(fwrite(
buffer, 1,
n, out1) <
n) {
193fprintf(stderr,
"%s: Error propagating process output: %s.\n",
202fputc(
'\033', out2);
217 const char* start = p;
218 const char* src = p;
219 char* dest = (
char*) p;
221&& (start = memchr(src,
'\033',
n- (src - p))) !=
NULL 222&& (start == p +
n- 1 || start[1] ==
'[')) {
223 if(src > dest && start > src) {
224 memmove(dest, src, start - src);
227 if(start == p +
n- 1) {
232 const char* end =
s_FindEnd(start + 2,
n- 2 - (start - p));
246 if(fwrite(p, 1,
n, out2) <
n) {
247fprintf(stderr,
"%s: Error logging process output: %s.\n",
251 if(
n< 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
253fprintf(stderr,
"%s: Error reading from process: %s.\n",
273 const char* term = getenv(
"TERM");
274 if(term !=
NULL&&
strcmp(term,
"dumb") && isatty(fd_to_mimic)) {
278 if((fds[0] = posix_openpt(O_RDONLY | O_NOCTTY)) < 0) {
279fprintf(stderr,
"%s: Error allocating pty master for %s: %s.\n",
282}
else if(grantpt(fds[0]) < 0 || unlockpt(fds[0]) < 0
283|| (name = ptsname(fds[0])) ==
NULL 284|| (fds[1] = open(name, O_WRONLY)) < 0) {
285fprintf(stderr,
"%s: Error opening pty slave for %s: %s.\n",
290 if(tcgetattr(fds[1], &attr) < 0) {
292 "%s: Warning: unable to get attributes for %s: %s.\n",
295attr.c_oflag |= ONLRET;
297attr.c_oflag &= ~ONLCR;
300 if(tcsetattr(fds[1], TCSADRAIN, &attr) < 0) {
302 "%s: Warning: unable to set attributes for %s: %s.\n",
308fprintf(stderr,
"%s: Error creating pipe for %s: %s.n",
321 intstdout_fds[2], stderr_fds[2];
330 if(
s_OpenPipeOrPty(STDERR_FILENO,
"stderr", stderr_fds, &stderr_state)
332 close(stdout_fds[0]);
333 close(stdout_fds[1]);
343}
else if(pid == 0) {
346 close(stdout_fds[0]);
347 close(stderr_fds[0]);
349dup2(stderr_fds[1], STDERR_FILENO);
351 for(
n= 1;
n<= NSIG; ++
n) {
352 if(
n!= SIGKILL &&
n!= SIGSTOP) {
356execvp(args[0], (
char*
const*) args);
357fprintf(stderr,
"%s: Unable to exec %s: %s.\n",
362 intstdout_done = 0, stderr_done = 0;
363 close(stdout_fds[1]);
364 close(stderr_fds[1]);
365fcntl(stdout_fds[0], F_SETFL,
366fcntl(stdout_fds[0], F_GETFL) | O_NONBLOCK);
367fcntl(stderr_fds[0], F_SETFL,
368fcntl(stderr_fds[0], F_GETFL) | O_NONBLOCK);
369 while( !stdout_done || !stderr_done ) {
371 unsigned intnfds = 0;
374 if( !stderr_done ) {
375FD_SET(stderr_fds[0], &rfds);
376FD_SET(stderr_fds[0], &efds);
377nfds = stderr_fds[0] + 1;
379 if( !stdout_done ) {
380FD_SET(stdout_fds[0], &rfds);
381FD_SET(stdout_fds[0], &efds);
382 if(stdout_fds[0] >= nfds) {
383nfds = stdout_fds[0] + 1;
386 if(select(nfds, &rfds,
NULL, &efds,
NULL) < 0
387&& errno != EINTR && errno != EAGAIN) {
389 "%s: Error checking for output to log: %s.\n",
393 if(FD_ISSET(stdout_fds[0], &rfds)
394|| FD_ISSET(stdout_fds[0], &efds)) {
395stdout_done =
s_Tee(stdout_fds[0], stdout,
log,
398 if(FD_ISSET(stderr_fds[0], &rfds)
399|| FD_ISSET(stderr_fds[0], &efds)) {
400stderr_done =
s_Tee(stderr_fds[0], stderr,
log,
404&& waitpid(pid, &status, WNOHANG) != 0) {
418waitpid(pid, &status, 0);
423 close(stdout_fds[0]);
424 close(stderr_fds[0]);
444fprintf(stderr,
"%s: Caught signal %d\n",
s_AppName,
n);
461 int main(
intargc,
const char*
const* argv)
464 charpid_str[
sizeof(pid_t) * 3 + 1], new_log[
PATH_MAX];
465 const char* getter_args[] = {
"get_lock",
"BASE", pid_str,
NULL};
477getter_args[0] = options.
getter;
479getter_args[1] = options.
base;
480sprintf(pid_str,
"%ld", (
long)getpid());
482 for(
n= 1;
n<= NSIG; ++
n) {
485 caseSIGQUIT:
caseSIGILL:
caseSIGABRT:
caseSIGFPE:
caseSIGSEGV:
486 caseSIGBUS:
caseSIGSYS:
caseSIGTRAP:
caseSIGXCPU:
caseSIGXFSZ:
490 caseSIGKILL:
caseSIGSTOP:
492 caseSIGTSTP:
caseSIGTTIN:
caseSIGTTOU:
494 caseSIGCHLD:
caseSIGCONT:
caseSIGURG:
caseSIGWINCH:
507sprintf(new_log,
"%s.new", options.
logfile);
508 log= fopen(new_log,
"w");
510fprintf(stderr,
"%s: Couldn't open log file %s: %s.\n",
523 if(access(options.
logfile, F_OK) != 0) {
526 const char* reviewer_args[] = { options.
reviewer, new_log,
NULL};
527 if(
s_Run(reviewer_args,
NULL) != 0) {
532 if(rename(new_log, options.
logfile) < 0) {
533fprintf(stderr,
"%s: Unable to rename log file %s: %s.\n",
544}
else if(WIFSIGNALED(status)) {
545status = WTERMSIG(status) | 0x80;
547status = WEXITSTATUS(status);
static void cleanup(void)
static const char label[]
const struct ncbi::grid::netcache::search::fields::KEY key
const GenericPointer< typename T::ValueType > T2 value
int strncmp(const char *str1, const char *str2, size_t count)
int strcmp(const char *str1, const char *str2)
std::istream & in(std::istream &in_, double &x_)
int main(int argc, const char *const *argv)
static int s_FinishingInBackground
static const char * s_FindEnd(const char *p, size_t n)
static void s_CleanUp(void)
static void s_ParseOptions(SOptions *options, int *argc, const char *const **argv)
static const char * s_AppName
static int s_OpenPipeOrPty(int fd_to_mimic, const char *label, int fds[2], EFilterState *state)
static int s_Run(const char *const *args, FILE *log)
static int s_CaughtSignal
static void s_OnSignal(int n)
static char s_LockName[4096]
static sig_t s_OrigHandlers[NSIG+1]
static void s_OnSIGCHLD(int n)
static void s_ApplyMap(SOptions *options)
static int s_MainChildDone
static int s_Tee(int in, FILE *out1, FILE *out2, EFilterState *state)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4