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