forked from mrirecon/bart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbart.c
174 lines (119 loc) · 2.86 KB
/
bart.c
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
/* Copyright 2015. The Regents of the University of California.
* Copyright 2015-2018. Martin Uecker.
+ Copyright 2018. Damien Nguyen.
* All rights reserved. Use of this source code is governed by
* a BSD-style license which can be found in the LICENSE file.
*
* Authors:
* 2014-2016 Martin Uecker <[email protected]>
*/
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <libgen.h>
#include <unistd.h>
#include <errno.h>
#include "misc/io.h"
#include "misc/misc.h"
#include "misc/debug.h"
#include "misc/cppmap.h"
#ifdef USE_CUDA
#include "num/gpuops.h"
#endif
#ifdef USE_LOCAL_FFTW
#include "fftw3_local.h"
#define MANGLE(name) local_ ## name
#else
#include <fftw3.h>
#define MANGLE(name) name
#endif
#include "main.h"
extern FILE* bart_output; // src/misc.c
static void bart_exit_cleanup(void)
{
if (NULL != command_line)
XFREE(command_line);
io_memory_cleanup();
#ifdef FFTWTHREADS
MANGLE(fftwf_cleanup_threads)();
#endif
#ifdef USE_CUDA
cuda_memcache_clear();
#endif
}
struct {
int (*main_fun)(int argc, char* argv[]);
const char* name;
} dispatch_table[] = {
#define DENTRY(x) { main_ ## x, # x },
MAP(DENTRY, MAIN_LIST)
#undef DENTRY
{ NULL, NULL }
};
static void usage(void)
{
printf("BART. Available commands are:");
for (int i = 0; NULL != dispatch_table[i].name; i++) {
if (0 == i % 6)
printf("\n");
printf("%-12s", dispatch_table[i].name);
}
printf("\n");
}
int main_bart(int argc, char* argv[argc])
{
char* bn = basename(argv[0]);
if (0 == strcmp(bn, "bart")) {
if (1 == argc) {
usage();
return 1;
}
const char* tpath[] = {
#ifdef TOOLBOX_PATH_OVERRIDE
getenv("TOOLBOX_PATH"),
#endif
"/usr/local/lib/bart/commands/",
"/usr/lib/bart/commands/",
};
for (unsigned int i = 0; i < ARRAY_SIZE(tpath); i++) {
if (NULL == tpath[i])
continue;
size_t len = strlen(tpath[i]) + strlen(argv[1]) + 2;
char cmd[len];
size_t r = snprintf(cmd, len, "%s/%s", tpath[i], argv[1]);
assert(r < len);
if (-1 == execv(cmd, argv + 1)) {
// only if it doesn't exist - try builtin
if (ENOENT != errno) {
perror("Executing bart command failed");
return 1;
}
} else {
assert(0);
}
}
return main_bart(argc - 1, argv + 1);
}
for (int i = 0; NULL != dispatch_table[i].name; i++)
if (0 == strcmp(bn, dispatch_table[i].name))
return dispatch_table[i].main_fun(argc, argv);
fprintf(stderr, "Unknown bart command: \"%s\".\n", bn);
return -1;
}
int bart_command(int len, char* buf, int argc, char* argv[])
{
int save = debug_level;
if (NULL != buf) {
buf[0] = '\0';
bart_output = fmemopen(buf, len, "w");
}
int ret = error_catcher(main_bart, argc, argv);
bart_exit_cleanup();
debug_level = save;
if (NULL != bart_output) {
fclose(bart_output); // write final nul
bart_output = NULL;
}
return ret;
}