| 1 |
#include <getopt.h> |
| 2 |
#include <unistd.h> |
| 3 |
#include <setjmp.h> |
| 4 |
#include <stdio.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <string.h> |
| 7 |
|
| 8 |
#include "samtools.pysam.h" |
| 9 |
|
| 10 |
FILE * samtools_stderr = NULL; |
| 11 |
FILE * samtools_stdout = NULL; |
| 12 |
const char * samtools_stdout_fn = NULL; |
| 13 |
|
| 14 |
|
| 15 |
FILE * samtools_set_stderr(int fd) |
| 16 |
{ |
| 17 |
if (samtools_stderr != NULL) |
| 18 |
fclose(samtools_stderr); |
| 19 |
samtools_stderr = fdopen(fd, "w"); |
| 20 |
return samtools_stderr; |
| 21 |
} |
| 22 |
|
| 23 |
void samtools_close_stderr(void) |
| 24 |
{ |
| 25 |
fclose(samtools_stderr); |
| 26 |
samtools_stderr = NULL; |
| 27 |
} |
| 28 |
|
| 29 |
FILE * samtools_set_stdout(int fd) |
| 30 |
{ |
| 31 |
if (samtools_stdout != NULL) |
| 32 |
fclose(samtools_stdout); |
| 33 |
samtools_stdout = fdopen(fd, "w"); |
| 34 |
if (samtools_stdout == NULL) |
| 35 |
{ |
| 36 |
fprintf(samtools_stderr, "could not set stdout to fd %i", fd); |
| 37 |
} |
| 38 |
return samtools_stdout; |
| 39 |
} |
| 40 |
|
| 41 |
void samtools_set_stdout_fn(const char *fn) |
| 42 |
{ |
| 43 |
samtools_stdout_fn = fn; |
| 44 |
} |
| 45 |
|
| 46 |
void samtools_close_stdout(void) |
| 47 |
{ |
| 48 |
fclose(samtools_stdout); |
| 49 |
samtools_stdout = NULL; |
| 50 |
} |
| 51 |
|
| 52 |
int samtools_puts(const char *s) |
| 53 |
{ |
| 54 |
if (fputs(s, samtools_stdout) == EOF) return EOF; |
| 55 |
return putc('\n', samtools_stdout); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
static jmp_buf samtools_jmpbuf; |
| 60 |
static int samtools_status = 0; |
| 61 |
|
| 62 |
int samtools_dispatch(int argc, char *argv[]) |
| 63 |
{ |
| 64 |
|
| 65 |
#if defined __GLIBC__ |
| 66 |
optind = 0; |
| 67 |
#elif defined _OPTRESET || defined _OPTRESET_DECLARED |
| 68 |
optreset = optind = 1; |
| 69 |
#else |
| 70 |
optind = 1; |
| 71 |
#endif |
| 72 |
|
| 73 |
if (setjmp(samtools_jmpbuf) == 0) |
| 74 |
return samtools_main(argc, argv); |
| 75 |
else |
| 76 |
return samtools_status; |
| 77 |
} |
| 78 |
|
| 79 |
void samtools_exit(int status) |
| 80 |
{ |
| 81 |
samtools_status = status; |
| 82 |
longjmp(samtools_jmpbuf, 1); |
| 83 |
} |