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