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