1 #include <ctype.h>
2 #include <assert.h>
3 #include <unistd.h>
4 #include <setjmp.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include "bcftools.pysam.h"
10
11 FILE * bcftools_stderr = NULL;
12 FILE * bcftools_stdout = NULL;
13 const char * bcftools_stdout_fn = NULL;
14
15
bcftools_set_stderr(int fd)16 FILE * bcftools_set_stderr(int fd)
17 {
18 if (bcftools_stderr != NULL)
19 fclose(bcftools_stderr);
20 bcftools_stderr = fdopen(fd, "w");
21 return bcftools_stderr;
22 }
23
bcftools_close_stderr(void)24 void bcftools_close_stderr(void)
25 {
26 fclose(bcftools_stderr);
27 bcftools_stderr = NULL;
28 }
29
bcftools_set_stdout(int fd)30 FILE * bcftools_set_stdout(int fd)
31 {
32 if (bcftools_stdout != NULL)
33 fclose(bcftools_stdout);
34 bcftools_stdout = fdopen(fd, "w");
35 if (bcftools_stdout == NULL)
36 {
37 fprintf(bcftools_stderr, "could not set stdout to fd %i", fd);
38 }
39 return bcftools_stdout;
40 }
41
bcftools_set_stdout_fn(const char * fn)42 void bcftools_set_stdout_fn(const char *fn)
43 {
44 bcftools_stdout_fn = fn;
45 }
46
bcftools_close_stdout(void)47 void bcftools_close_stdout(void)
48 {
49 fclose(bcftools_stdout);
50 bcftools_stdout = NULL;
51 }
52
bcftools_puts(const char * s)53 int bcftools_puts(const char *s)
54 {
55 if (fputs(s, bcftools_stdout) == EOF) return EOF;
56 return putc('\n', bcftools_stdout);
57 }
58
59
60 static jmp_buf bcftools_jmpbuf;
61 static int bcftools_status = 0;
62
bcftools_dispatch(int argc,char * argv[])63 int bcftools_dispatch(int argc, char *argv[])
64 {
65 if (setjmp(bcftools_jmpbuf) == 0)
66 return bcftools_main(argc, argv);
67 else
68 return bcftools_status;
69 }
70
bcftools_exit(int status)71 void bcftools_exit(int status)
72 {
73 bcftools_status = status;
74 longjmp(bcftools_jmpbuf, 1);
75 }
76
77
bcftools_set_optind(int val)78 void bcftools_set_optind(int val)
79 {
80 // setting this in cython via
81 // "from posix.unistd cimport optind"
82 // did not work.
83 //
84 // setting to 0 forces a complete re-initialization
85 optind = val;
86 }
87
88
89
90