Exit on EOF
[ctsim.git] / tools / ctsimtext.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          ctsimtext.cpp
5 **   Purpose:       Text mode shell for CTSim
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  Jan 2001
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2009 Kevin Rosenberg
11 **
12 **  This program is free software; you can redistribute it and/or modify
13 **  it under the terms of the GNU General Public License (version 2) as
14 **  published by the Free Software Foundation.
15 **
16 **  This program is distributed in the hope that it will be useful,
17 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 **  GNU General Public License for more details.
20 **
21 **  You should have received a copy of the GNU General Public License
22 **  along with this program; if not, write to the Free Software
23 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 ******************************************************************************/
25
26 #include "ct.h"
27
28 #ifdef HAVE_READLINE_H
29 extern "C" {
30 #include <readline.h>
31 #include <history.h>
32 };
33 #elif defined(HAVE_READLINE_READLINE_H)
34 extern "C" {
35 #include <readline/readline.h>
36 #include <readline/history.h>
37 };
38 #endif
39 #include <exception>
40
41 // Master shell for all command-line tools
42 // If called as ctsimtext, program will look to next token on command-line as the function name
43 // If linked to ctsimtext, but executed as another name, eg pjrec, then program will use that
44 // linked name as name of function.
45
46 static const char* const g_szIdStr = "$Id$";
47 static const char* const s_szProgramName = "ctsimtext";
48 static const char* const s_szProgramName2 = "ctsimtext.exe";
49 static const char* const s_szProgramName3 = "ctsimtext-lam";
50
51 extern int if1_main (int argc, char* const argv[]);
52 extern int if2_main (int argc, char* const argv[]);
53 extern int ifexport_main (int argc, char* const argv[]);
54 extern int ifinfo_main (int argc, char* const argv[]);
55 extern int phm2if_main (int argc, char* const argv[]);
56 extern int phm2pj_main (int argc, char* const argv[]);
57 extern int phm2helix_main (int argc, char* const argv[]);
58 extern int pjHinterp_main (int argc, char* const argv[]);
59 extern int pj2if_main (int argc, char* const argv[]);
60 extern int pjinfo_main (int argc, char* const argv[]);
61 extern int pjrec_main (int argc, char* const argv[]);
62 extern int linogram_main (int argc, char* const argv[]);
63
64 static int processCommand (int argc, char* const argv[]);
65 static void convertStringToArgcv (char* szLine, int* piArgc, char*** pppArgv);
66
67
68 void
69 ctsimtext_usage (const char *program)
70 {
71   std::cout << "usage: " << fileBasename(program) << " ctsim-function-name ctstim-function-parameters...\n";
72   std::cout << "CTSim text shell";
73 #ifdef VERSION
74   std::cout << ", Version " <<VERSION;
75 #endif
76   std::cout << "\n\n";
77   std::cout << "  if1           Single image file conversion\n";
78   std::cout << "  if2           Dual image file conversions\n";
79   std::cout << "  ifexport      Export an imagefile to a graphics file\n";
80   std::cout << "  ifinfo        Image file information\n";
81   std::cout << "  pj2if         Convert an projection file into an imagefile\n";
82   std::cout << "  pjinfo        Projection file information\n";
83   std::cout << "  pjrec         Projection reconstruction\n";
84   std::cout << "  phm2if        Convert a geometric phantom into an imagefile\n";
85   std::cout << "  phm2pj        Take projections of a phantom object\n";
86   std::cout << "  phm2helix     Take projections of a phantom object\n";
87   std::cout << "  pjHinterp     Interpolate helical projections of a phantom object\n";
88   std::cout << "  linogram      Print linogram sampling\n";
89 }
90
91 void
92 interactive_usage ()
93 {
94   std::cout << "usage: function-name parameters...\n";
95   std::cout << "Available functions:\n";
96   std::cout << "  ifexport      Export an imagefile to a graphics file\n";
97   std::cout << "  ifinfo        Image file information\n";
98   std::cout << "  if1           Single image file conversion\n";
99   std::cout << "  if2           Dual image file conversions\n";
100   std::cout << "  phm2if        Convert a geometric phantom into an imagefile\n";
101   std::cout << "  phm2pj        Take projections of a phantom object\n";
102   std::cout << "  phm2helix     Take projections of a phantom object\n";
103   std::cout << "  pjinfo        Projection file information\n";
104   std::cout << "  pj2if         Convert an projection file into an imagefile\n";
105   std::cout << "  pjHinterp     Interpolate helical projections of a phantom object\n";
106   std::cout << "  pjrec         Projection reconstruction\n";
107   std::cout << "  quit          Quits shell\n";
108   std::cout << "  linogram      Display linogram sampling\n";
109   std::cout << "All functions accept --help as parameter for online help\n\n";
110 }
111
112 static bool s_bInteractive = false;
113
114 int
115 ctsimtext_main (int argc, char * argv[])
116 {
117   int iReturn = 0;
118
119   if (argc > 1 && (strcmp(s_szProgramName, fileBasename (argv[0])) == 0 || strcmp(s_szProgramName2, fileBasename (argv[0])) == 0 || strcmp(s_szProgramName3, fileBasename (argv[0])) == 0)) {
120     argv++;
121     argc--;
122     iReturn = processCommand (argc, argv);
123   } else if (argc >= 1 && ! (strcmp(s_szProgramName, fileBasename (argv[0])) == 0 || strcmp(s_szProgramName2, fileBasename (argv[0])) == 0 || strcmp(s_szProgramName3, fileBasename (argv[0])) == 0)) {
124     iReturn = processCommand (argc, argv);
125   } else {
126     s_bInteractive = true;
127     char szPrompt[] = "CTSim> ";
128     std::cout << "CTSim Text Shell";
129 #ifdef VERSION
130     std::cout << ", Version " << VERSION;
131 #endif
132     std::cout << " (Type \"quit\" to end)\n\n";
133
134     while (1) {
135 #ifdef HAVE_READLINE
136       char* pszInputLine = readline (szPrompt);
137       if (! pszInputLine)
138         break;
139       if (*pszInputLine != EOS)
140         add_history (pszInputLine);
141
142 #else  // DONT_HAVE_READLINE
143
144       static const int s_MaxLineLength = 1024;
145       char* pszInputLine = new char [s_MaxLineLength+1];
146       std::cout << szPrompt;
147       std::cin.getline (pszInputLine, s_MaxLineLength);
148       if (std::cin.eof()) {
149         std::cout << "\n";
150         break;
151       }
152
153 #ifdef DEBUG
154       std::cout << "#" << pszInputLine << "#\n";
155 #endif
156
157       std::cout << std::flush;
158       std::cout << "\n";
159 #endif  // DONT_HAVE_READLINE
160
161       if (strncasecmp (pszInputLine, "quit", 4) == 0)
162         break;
163
164       convertStringToArgcv (pszInputLine, &argc, &argv);
165 #ifdef DEBUG
166       for (int i = 0; i < argc; i++)
167         std::cout << "Token " << i << ": " << argv[i] << "\n";
168 #endif
169       iReturn = processCommand (argc, argv);
170
171       delete pszInputLine;
172     }
173   }
174
175   return iReturn;
176 }
177
178 static void
179 convertStringToArgcv (char* pszLine, int* piArgc, char*** pppArgv)
180 {
181   char* pCurrentPos = pszLine;
182   int nTokens = 0;
183   std::vector<char*> vecpszToken;
184
185   // Process line
186   bool bInDoubleQuote = false;
187   bool bInSingleQuote = false;
188   bool bInToken = false;
189
190   while (*pCurrentPos) {
191     if (isspace (*pCurrentPos)) {
192       if (! bInToken)
193         *pCurrentPos = 0;
194       else if (bInSingleQuote || bInDoubleQuote)
195         ;
196       else { // in non-quote token
197         *pCurrentPos = 0;
198         bInToken = false;
199       }
200     }
201     else if (*pCurrentPos == '\"') {
202       if (bInSingleQuote) {
203         bInSingleQuote = false;
204         *pCurrentPos = 0;
205       } else {
206         bInSingleQuote = true;
207         if (! bInToken) {
208           bInToken = true;
209           nTokens++;
210           vecpszToken.push_back (pCurrentPos+1);
211         }
212       }
213     } else if (*pCurrentPos == '\'') {
214       if (bInDoubleQuote) {
215         bInDoubleQuote = false;
216         *pCurrentPos = 0;
217       } else {
218         bInDoubleQuote = true;
219         if (! bInToken) {
220           bInToken = true;
221           nTokens++;
222           vecpszToken.push_back (pCurrentPos+1);
223         }
224       }
225     } else if (! bInToken) {   // nonwhite, non-quote character
226       bInToken = true;
227       nTokens++;
228       vecpszToken.push_back (pCurrentPos);
229     }
230
231     pCurrentPos++;
232   }
233
234   *piArgc = nTokens;
235   if (nTokens > 0) {
236     *pppArgv = new char* [nTokens];
237     for (unsigned int iToken = 0; iToken < vecpszToken.size(); iToken++)
238       (*pppArgv)[iToken] = vecpszToken[iToken];
239   } else
240     *pppArgv = NULL;
241 }
242
243 static int
244 processCommand (int argc, char* const argv[])
245 {
246   if (argc < 1)
247     return 1;
248
249   const char* const pszFunction = fileBasename (argv[0]);
250
251   try {
252     if (strcasecmp (pszFunction, "if1") == 0)
253       return if1_main (argc, argv);
254     else if (strcasecmp (pszFunction, "if2") == 0)
255       return if2_main (argc, argv);
256     else if (strcasecmp (pszFunction, "ifexport") == 0)
257       return ifexport_main (argc, argv);
258     else if (strcasecmp (pszFunction, "ifinfo") == 0)
259       return ifinfo_main (argc, argv);
260     else if (strcasecmp (pszFunction, "phm2if") == 0)
261       return phm2if_main (argc, argv);
262     else if (strcasecmp (pszFunction, "phm2pj") == 0)
263       return phm2pj_main (argc, argv);
264     else if (strcasecmp (pszFunction, "phm2helix") == 0)
265       return phm2helix_main (argc, argv);
266     else if (strcasecmp (pszFunction, "pjHinterp") == 0)
267       return pjHinterp_main (argc, argv);
268     else if (strcasecmp (pszFunction, "pj2if") == 0)
269       return pj2if_main (argc, argv);
270     else if (strcasecmp (pszFunction, "pjinfo") == 0)
271       return pjinfo_main (argc, argv);
272     else if (strcasecmp (pszFunction, "pjrec") == 0)
273       return pjrec_main (argc, argv);
274     else if (strcasecmp (pszFunction, "linogram") == 0)
275       return linogram_main (argc, argv);
276     else {
277       std::cout << "Unknown function name: " << pszFunction << "\n";
278       if (s_bInteractive)
279         interactive_usage();
280       else
281         ctsimtext_usage (s_szProgramName);
282       return 1;
283     }
284   } catch (std::exception e) {
285     std::cerr << "Exception: " << e.what() << std::endl;
286   } catch (...) {
287     std::cerr << "Unknown exception caught\n";
288   }
289
290   return 1;
291 }
292
293 int
294 main (int argc, char* argv[])
295 {
296 #if defined(HAVE_FFTW) && defined(HAVE_GETENV)
297   const char* const pszHome = getenv("HOME");
298   char* pszWisdom = NULL;
299
300   if (pszHome) {
301     const char szFileBase[] = ".fftw3-wisdom";
302     int nHome = strlen(pszHome);
303     int nBase = strlen(szFileBase);
304     int len = nHome + nBase + 1;
305     pszWisdom = new char [ len + 1 ];
306     strcpy (pszWisdom, pszHome);
307     pszWisdom[nHome] = '/';
308     strcpy(pszWisdom+nHome+1,szFileBase);
309     pszWisdom[nHome+nBase+2] = 0;
310
311     FILE *wisdom = fopen(pszWisdom,"r");
312     if (wisdom) {
313       fftw_import_wisdom_from_file(wisdom);
314       fclose(wisdom);
315     }
316   }
317 #endif
318
319   int retval = ctsimtext_main(argc, argv);
320
321 #if defined(HAVE_FFTW) && defined(HAVE_GETENV)
322   if (pszWisdom) {
323     FILE* wisdom = fopen(pszWisdom,"w+");
324     if (wisdom) {
325       fftw_export_wisdom_to_file(wisdom);
326       fclose(wisdom);
327       delete [] pszWisdom;
328     }
329   }
330 #endif
331
332   return retval;
333 }