r1018: *** empty log message ***
[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-2000 Kevin Rosenberg
11 **
12 **  $Id: ctsimtext.cpp,v 1.20 2001/09/24 09:40:42 kevin Exp $
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28 #include "ct.h"
29
30 #ifdef HAVE_READLINE_H
31 extern "C" {
32 #include <readline.h>
33 #include <history.h>
34 };
35 #elif defined(HAVE_READLINE_READLINE_H)
36 extern "C" {
37 #include <readline/readline.h>
38 #include <readline/history.h>
39 };
40 #endif
41
42
43 // Master shell for all command-line tools
44 // If called as ctsimtext, program will look to next token on command-line as the function name
45 // If linked to ctsimtext, but executed as another name, eg pjrec, then program will use that
46 // linked name as name of function.
47
48 static const char* const g_szIdStr = "$Id: ctsimtext.cpp,v 1.20 2001/09/24 09:40:42 kevin Exp $";
49 static const char* const s_szProgramName = "ctsimtext";
50 static const char* const s_szProgramName2 = "ctsimtext.exe";
51 static const char* const s_szProgramName3 = "ctsimtext-lam";
52
53 extern int if1_main (int argc, char* const argv[]);
54 extern int if2_main (int argc, char* const argv[]);
55 extern int ifexport_main (int argc, char* const argv[]);
56 extern int ifinfo_main (int argc, char* const argv[]);
57 extern int phm2if_main (int argc, char* const argv[]);
58 extern int phm2pj_main (int argc, char* const argv[]);
59 extern int phm2helix_main (int argc, char* const argv[]);
60 extern int pjHinterp_main (int argc, char* const argv[]);
61 extern int pj2if_main (int argc, char* const argv[]);
62 extern int pjinfo_main (int argc, char* const argv[]);
63 extern int pjrec_main (int argc, char* const argv[]);
64
65 static int processCommand (int argc, char* const argv[]);
66 static void convertStringToArgcv (char* szLine, int* piArgc, char*** pppArgv);
67
68
69 void 
70 ctsimtext_usage (const char *program)
71 {
72   std::cout << "usage: " << fileBasename(program) << " ctsim-function-name ctstim-function-parameters...\n";
73   std::cout << "CTSim text shell";
74 #ifdef VERSION
75   std::cout << ", Version " <<VERSION;
76 #endif
77   std::cout << "\n\n";
78   std::cout << "  if1           Single image file conversion\n";
79   std::cout << "  if2           Dual image file conversions\n";
80   std::cout << "  ifexport      Export an imagefile to a graphics file\n";
81   std::cout << "  ifinfo        Image file information\n";
82   std::cout << "  pj2if         Convert an projection file into an imagefile\n";
83   std::cout << "  pjinfo        Projection file information\n";
84   std::cout << "  pjrec         Projection reconstruction\n";
85   std::cout << "  phm2if        Convert a geometric phantom into an imagefile\n";
86   std::cout << "  phm2pj        Take projections of a phantom object\n";
87   std::cout << "  phm2helix     Take projections of a phantom object\n";
88   std::cout << "  pjHinterp     Interpolate helical projections of a phantom object\n";
89
90 }
91
92 void 
93 interactive_usage ()
94 {
95   std::cout << "usage: function-name parameters...\n";
96   std::cout << "Available functions:\n";
97   std::cout << "  ifexport      Export an imagefile to a graphics file\n";
98   std::cout << "  ifinfo        Image file information\n";
99   std::cout << "  if1           Single image file conversion\n";
100   std::cout << "  if2           Dual image file conversions\n";
101   std::cout << "  phm2if        Convert a geometric phantom into an imagefile\n";
102   std::cout << "  phm2pj        Take projections of a phantom object\n";
103   std::cout << "  phm2helix        Take projections of a phantom object\n";
104   std::cout << "  pjinfo        Projection file information\n";
105   std::cout << "  pj2if         Convert an projection file into an imagefile\n";
106   std::cout << "  pjHinterp      Interpolate helical projections of a phantom object\n";
107   std::cout << "  pjrec         Projection reconstruction\n";
108   std::cout << "  quit          Quits shell\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       
149 #ifdef DEBUG
150       std::cout << "#" << pszInputLine << "#\n";
151 #endif
152       
153       std::cout << std::flush;
154       std::cout << "\n";
155 #endif  // DONT_HAVE_READLINE
156       
157       if (strncasecmp (pszInputLine, "quit", 4) == 0) 
158         break;
159       
160       convertStringToArgcv (pszInputLine, &argc, &argv);
161 #ifdef DEBUG
162       for (unsigned int i = 0; i < argc; i++)
163         std::cout << "Token " << i << ": " << argv[i] << "\n";
164 #endif
165       iReturn = processCommand (argc, argv);
166       
167       delete pszInputLine;
168     }
169   }
170   
171   return iReturn;
172 }
173
174 static void
175 convertStringToArgcv (char* pszLine, int* piArgc, char*** pppArgv)
176 {
177   char* pCurrentPos = pszLine;
178   int nTokens = 0;
179   std::vector<char*> vecpszToken;
180   
181   // Process line
182   bool bInDoubleQuote = false;
183   bool bInSingleQuote = false;
184   bool bInToken = false;
185   
186   while (*pCurrentPos) {
187     if (isspace (*pCurrentPos)) {
188       if (! bInToken)
189         *pCurrentPos = 0;
190       else if (bInSingleQuote || bInDoubleQuote)
191         ;
192       else { // in non-quote token
193         *pCurrentPos = 0;
194         bInToken = false;
195       }
196     }
197     else if (*pCurrentPos == '\"') {
198       if (bInSingleQuote) {
199         bInSingleQuote = false;
200         *pCurrentPos = 0;
201       } else {
202         bInSingleQuote = true;
203         if (! bInToken) {
204           bInToken = true;
205           nTokens++;
206           vecpszToken.push_back (pCurrentPos+1);
207         }
208       }
209     } else if (*pCurrentPos == '\'') {
210       if (bInDoubleQuote) {
211         bInDoubleQuote = false;
212         *pCurrentPos = 0;
213       } else {
214         bInDoubleQuote = true;
215         if (! bInToken) {
216           bInToken = true;
217           nTokens++;
218           vecpszToken.push_back (pCurrentPos+1);
219         }
220       }
221     } else if (! bInToken) {   // nonwhite, non-quote character
222       bInToken = true;
223       nTokens++;
224       vecpszToken.push_back (pCurrentPos);
225     }
226     
227     pCurrentPos++;
228   }
229   
230   *piArgc = nTokens;
231   if (nTokens > 0) {
232     *pppArgv = new char* [nTokens];
233     for (unsigned int iToken = 0; iToken < vecpszToken.size(); iToken++)
234       (*pppArgv)[iToken] = vecpszToken[iToken];
235   } else
236     *pppArgv = NULL;
237 }
238
239 static int 
240 processCommand (int argc, char* const argv[])
241 {
242   if (argc < 1)
243     return 1;
244   
245   const char* const pszFunction = fileBasename (argv[0]);
246   
247   try {  
248     if (strcasecmp (pszFunction, "if1") == 0)
249       return if1_main (argc, argv);
250     else if (strcasecmp (pszFunction, "if2") == 0)
251       return if2_main (argc, argv);
252     else if (strcasecmp (pszFunction, "ifexport") == 0)
253       return ifexport_main (argc, argv);
254     else if (strcasecmp (pszFunction, "ifinfo") == 0)
255       return ifinfo_main (argc, argv);
256     else if (strcasecmp (pszFunction, "phm2if") == 0)
257       return phm2if_main (argc, argv);
258     else if (strcasecmp (pszFunction, "phm2pj") == 0)
259       return phm2pj_main (argc, argv);
260         else if (strcasecmp (pszFunction, "phm2helix") == 0)
261           return phm2helix_main (argc, argv);
262         else if (strcasecmp (pszFunction, "pjHinterp") == 0)
263           return pjHinterp_main (argc, argv);
264     else if (strcasecmp (pszFunction, "pj2if") == 0)
265       return pj2if_main (argc, argv);
266     else if (strcasecmp (pszFunction, "pjinfo") == 0)
267       return pjinfo_main (argc, argv);
268     else if (strcasecmp (pszFunction, "pjrec") == 0)
269       return pjrec_main (argc, argv);
270     else {
271       std::cout << "Unknown function name: " << pszFunction << "\n";
272       if (s_bInteractive)
273         interactive_usage();
274       else
275         ctsimtext_usage (s_szProgramName);
276       return 1;
277     }
278   } catch (exception e) {
279     std::cerr << "Exception: " << e.what() << std::endl;
280   } catch (...) {
281     std::cerr << "Unknown exception caught\n";
282   }  
283
284   return 1;
285 }
286
287 int 
288 main (int argc, char* argv[])
289 {
290   int retval = 1;
291   
292   retval = ctsimtext_main(argc, argv);
293   
294   return retval;
295 }
296