r390: no 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.11 2001/01/13 04:44:47 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.11 2001/01/13 04:44:47 kevin Exp $";
49 static const char* const s_szProgramName = "ctsimtext";
50 static const char* const s_szProgramName2 = "ctsimtext.exe";
51
52 extern int if1_main (int argc, char* const argv[]);
53 extern int if2_main (int argc, char* const argv[]);
54 extern int ifexport_main (int argc, char* const argv[]);
55 extern int ifinfo_main (int argc, char* const argv[]);
56 extern int phm2if_main (int argc, char* const argv[]);
57 extern int phm2pj_main (int argc, char* const argv[]);
58 extern int pj2if_main (int argc, char* const argv[]);
59 extern int pjinfo_main (int argc, char* const argv[]);
60 extern int pjrec_main (int argc, char* const argv[]);
61
62 static int processCommand (int argc, char* const argv[]);
63 static void convertStringToArgcv (char* szLine, int* piArgc, char*** pppArgv);
64
65
66 void 
67 ctsimtext_usage (const char *program)
68 {
69   std::cout << "usage: " << fileBasename(program) << " ctsim-function-name ctstim-function-parameters...\n";
70   std::cout << "CTSim text shell\n\n";
71   std::cout << "  if1           Single image file conversion\n";
72   std::cout << "  if2           Dual image file conversions\n";
73   std::cout << "  ifexport      Export an imagefile to a graphics file\n";
74   std::cout << "  ifinfo        Image file information\n";
75   std::cout << "  pj2if         Convert an projection file into an imagefile\n";
76   std::cout << "  pjinfo        Projection file information\n";
77   std::cout << "  pjrec         Projection reconstruction\n";
78   std::cout << "  phm2if        Convert a geometric phantom into an imagefile\n";
79   std::cout << "  phm2pj        Take projections of a phantom object\n";
80 }
81
82 void 
83 interactive_usage ()
84 {
85   std::cout << "usage: function-name parameters...\n";
86   std::cout << "Available functions:\n";
87   std::cout << "  ifexport      Export an imagefile to a graphics file\n";
88   std::cout << "  ifinfo        Image file information\n";
89   std::cout << "  if1           Single image file conversion\n";
90   std::cout << "  if2           Dual image file conversions\n";
91   std::cout << "  phm2if        Convert a geometric phantom into an imagefile\n";
92   std::cout << "  phm2pj        Take projections of a phantom object\n";
93   std::cout << "  pjinfo        Projection file information\n";
94   std::cout << "  pj2if         Convert an projection file into an imagefile\n";
95   std::cout << "  pjrec         Projection reconstruction\n";
96   std::cout << "  quit          Quits shell\n";
97   std::cout << "All functions accept --help as parameter for online help\n\n";
98 }
99
100 static bool s_bInteractive = false;
101
102 int 
103 ctsimtext_main (int argc, char * argv[])
104 {
105   int iReturn = 0;  
106   
107   if (argc > 1 && (strcmp(argv[0], fileBasename (s_szProgramName)) == 0 || strcmp(argv[0], fileBasename (s_szProgramName2)) == 0)) {
108     argv = &argv[1];
109     argc--;
110     if (argc > 1 && (strcmp(argv[0], fileBasename (s_szProgramName)) == 0 || strcmp(argv[0], fileBasename (s_szProgramName2)) == 0)) {
111       argv = &argv[2];
112       argc--;
113     }
114     iReturn = processCommand (argc, argv);
115   } else if (argc > 1){
116     iReturn = processCommand (argc, argv);
117   } else {
118     s_bInteractive = true;
119     char szPrompt[] = "CTSim> ";
120     std::cout << "CTSim Text Shell (Type \"quit\" to end)\n\n";
121     
122     while (1) {
123 #ifdef HAVE_READLINE
124       char* pszInputLine = readline (szPrompt);
125       if (! pszInputLine)
126         break;
127       if (*pszInputLine != EOS)
128         add_history (pszInputLine);
129       
130 #else  // DONT_HAVE_READLINE
131       
132       static const int s_MaxLineLength = 1024;
133       char* pszInputLine = new char [s_MaxLineLength+1];
134       std::cout << szPrompt;
135       std::cin.getline (pszInputLine, s_MaxLineLength);
136
137 #ifdef DEBUG
138       std::cout << "#" << pszInputLine << "#\n";
139 #endif
140       
141       std::cout << std::flush;
142       std::cout << "\n";
143 #endif  // DONT_HAVE_READLINE
144       
145       if (strncasecmp (pszInputLine, "quit", 4) == 0) 
146         break;
147       
148       convertStringToArgcv (pszInputLine, &argc, &argv);
149 #ifdef DEBUG
150       for (unsigned int i = 0; i < argc; i++)
151         std::cout << "Token " << i << ": " << argv[i] << "\n";
152 #endif
153       iReturn = processCommand (argc, argv);
154
155       delete pszInputLine;
156     }
157   }
158   
159   
160   return iReturn;
161 }
162
163 static void
164 convertStringToArgcv (char* pszLine, int* piArgc, char*** pppArgv)
165 {
166   char* pCurrentPos = pszLine;
167   int nTokens = 0;
168   std::vector<char*> vecpszToken;
169   
170   // Process line
171   bool bInDoubleQuote = false;
172   bool bInSingleQuote = false;
173   bool bInToken = false;
174   
175   while (*pCurrentPos) {
176     if (isspace (*pCurrentPos)) {
177       if (! bInToken)
178         *pCurrentPos = 0;
179       else if (bInSingleQuote || bInDoubleQuote)
180         ;
181       else { // in non-quote token
182         *pCurrentPos = 0;
183         bInToken = false;
184       }
185     }
186     else if (*pCurrentPos == '\"') {
187       if (bInSingleQuote) {
188         bInSingleQuote = false;
189         *pCurrentPos = 0;
190       } else {
191         bInSingleQuote = true;
192         if (! bInToken) {
193           bInToken = true;
194           nTokens++;
195           vecpszToken.push_back (pCurrentPos+1);
196         }
197       }
198     } else if (*pCurrentPos == '\'') {
199       if (bInDoubleQuote) {
200         bInDoubleQuote = false;
201         *pCurrentPos = 0;
202       } else {
203         bInDoubleQuote = true;
204         if (! bInToken) {
205           bInToken = true;
206           nTokens++;
207           vecpszToken.push_back (pCurrentPos+1);
208         }
209       }
210     } else if (! bInToken) {   // nonwhite, non-quote character
211       bInToken = true;
212       nTokens++;
213       vecpszToken.push_back (pCurrentPos);
214     }
215     
216     pCurrentPos++;
217   }
218   
219   *piArgc = nTokens;
220   if (nTokens > 0) {
221     *pppArgv = new char* [nTokens];
222     for (unsigned int iToken = 0; iToken < vecpszToken.size(); iToken++)
223       (*pppArgv)[iToken] = vecpszToken[iToken];
224   } else
225     *pppArgv = NULL;
226 }
227
228 static int 
229 processCommand (int argc, char* const argv[])
230 {
231   if (argc < 1)
232     return 1;
233
234   const char* const pszFunction = fileBasename (argv[0]);
235   
236   if (strcasecmp (pszFunction, "if1") == 0)
237     return if1_main (argc, argv);
238   else if (strcasecmp (pszFunction, "if2") == 0)
239     return if2_main (argc, argv);
240   else if (strcasecmp (pszFunction, "ifexport") == 0)
241     return ifexport_main (argc, argv);
242   else if (strcasecmp (pszFunction, "ifinfo") == 0)
243     return ifinfo_main (argc, argv);
244   else if (strcasecmp (pszFunction, "phm2if") == 0)
245     return phm2if_main (argc, argv);
246   else if (strcasecmp (pszFunction, "phm2pj") == 0)
247     return phm2pj_main (argc, argv);
248   else if (strcasecmp (pszFunction, "pj2if") == 0)
249     return pj2if_main (argc, argv);
250   else if (strcasecmp (pszFunction, "pjinfo") == 0)
251     return pjinfo_main (argc, argv);
252   else if (strcasecmp (pszFunction, "pjrec") == 0)
253     return pjrec_main (argc, argv);
254   else {
255     std::cout << "Unknown function name: " << pszFunction << "\n";
256     if (s_bInteractive)
257       interactive_usage();
258     else
259       ctsimtext_usage (s_szProgramName);
260     return 1;
261   }
262 }
263
264 int 
265 main (int argc, char* argv[])
266 {
267   int retval = 1;
268   
269   try {
270     retval = ctsimtext_main(argc, argv);
271   } catch (exception e) {
272     std::cerr << "Exception: " << e.what() << std::endl;
273   } catch (...) {
274     std::cerr << "Unknown exception\n";
275   }
276   
277   return (retval);
278 }
279
280
281 // Hack to fix linking problems when not linking with wxWindows and CTSim GUI routines
282 #ifdef MSVC
283 unsigned long
284 wxDialog::OnCtlColor(unsigned long a,unsigned long b,unsigned int c,unsigned int d,unsigned int e,long f)
285 {return 0;}
286 #endif
287
288 #if 0
289 #include "../src/dlgezplot.h"
290
291 EZPlotDialog::EZPlotDialog (wxWindow* parent)
292   : wxDialog(), m_pEZPlotCtrl(NULL)
293 {}
294
295 EZPlotDialog::~EZPlotDialog()
296 {}
297
298 #endif