1 /*****************************************************************************
5 ** Purpose: Text mode shell for CTSim
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: Jan 2001
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2000 Kevin Rosenberg
12 ** $Id: ctsimtext.cpp,v 1.6 2001/01/10 18:32:40 kevin Exp $
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.
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.
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 ******************************************************************************/
30 #ifdef HAVE_READLINE_H
35 #elif defined(HAVE_READLINE_READLINE_H)
37 #include <readline/readline.h>
38 #include <readline/history.h>
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.
48 static const char* const g_szIdStr = "$Id: ctsimtext.cpp,v 1.6 2001/01/10 18:32:40 kevin Exp $";
49 static const char* const s_szProgramName = "ctsimtext";
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[]);
61 static int processCommand (int argc, char* const argv[]);
62 static void convertStringToArgcv (char* szLine, int* piArgc, char*** pppArgv);
66 ctsimtext_usage (const char *program)
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";
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";
99 static bool s_bInteractive = false;
103 ctsimtext_main (int argc, char * argv[])
107 if (argc > 1 && strcmp(argv[0], fileBasename (s_szProgramName)) == 0) {
110 iReturn = processCommand (argc, argv);
111 } else if (argc > 1){
112 iReturn = processCommand (argc, argv);
114 s_bInteractive = true;
115 char szPrompt[] = "CTSim> ";
116 std::cout << "CTSim Text Shell (Type \"quit\" to end)\n\n";
119 char* pszInput = readline (szPrompt);
122 if (*pszInput != EOS)
123 add_history (pszInput);
125 std::cout << szPrompt;
126 std::string strInput;
128 std::getline (std::cin, strInput);
130 std::cin >> strInput;
133 std::cout << "#" << strInput << "#\n";
135 std::cout << std::flush;
137 char* pszInput = new char [strlen(strInput.c_str()) + 1];
138 strcpy (pszInput, strInput.c_str());
140 if (strncasecmp (pszInput, "quit", 4) == 0) {
144 convertStringToArgcv (pszInput, &argc, &argv);
146 for (unsigned int i = 0; i < argc; i++)
147 std::cout << "Token " << i << ": " << argv[i] << "\n";
149 iReturn = processCommand (argc, argv);
158 convertStringToArgcv (char* pszLine, int* piArgc, char*** pppArgv)
160 char* pCurrentPos = pszLine;
162 std::vector<char*> vecpszToken;
165 bool bInDoubleQuote = false;
166 bool bInSingleQuote = false;
167 bool bInToken = false;
169 while (*pCurrentPos) {
170 if (isspace (*pCurrentPos)) {
173 else if (bInSingleQuote || bInDoubleQuote)
175 else { // in non-quote token
180 else if (*pCurrentPos == '\"') {
181 if (bInSingleQuote) {
182 bInSingleQuote = false;
185 bInSingleQuote = true;
189 vecpszToken.push_back (pCurrentPos+1);
192 } else if (*pCurrentPos == '\'') {
193 if (bInDoubleQuote) {
194 bInDoubleQuote = false;
197 bInDoubleQuote = true;
201 vecpszToken.push_back (pCurrentPos+1);
204 } else if (! bInToken) { // nonwhite, non-quote character
207 vecpszToken.push_back (pCurrentPos);
214 *pppArgv = new char* [nTokens];
215 for (unsigned int iToken = 0; iToken < vecpszToken.size(); iToken++)
216 (*pppArgv)[iToken] = vecpszToken[iToken];
220 processCommand (int argc, char* const argv[])
222 const char* const pszFunction = fileBasename (argv[0]);
224 if (strcasecmp (pszFunction, "if1") == 0)
225 return if1_main (argc, argv);
226 else if (strcasecmp (pszFunction, "if2") == 0)
227 return if2_main (argc, argv);
228 else if (strcasecmp (pszFunction, "ifexport") == 0)
229 return ifexport_main (argc, argv);
230 else if (strcasecmp (pszFunction, "ifinfo") == 0)
231 return ifinfo_main (argc, argv);
232 else if (strcasecmp (pszFunction, "phm2if") == 0)
233 return phm2if_main (argc, argv);
234 else if (strcasecmp (pszFunction, "phm2pj") == 0)
235 return phm2pj_main (argc, argv);
236 else if (strcasecmp (pszFunction, "pj2if") == 0)
237 return pj2if_main (argc, argv);
238 else if (strcasecmp (pszFunction, "pjinfo") == 0)
239 return pjinfo_main (argc, argv);
240 else if (strcasecmp (pszFunction, "pjrec") == 0)
241 return pjrec_main (argc, argv);
243 std::cout << "Unknown function name: " << pszFunction << "\n";
247 ctsimtext_usage (s_szProgramName);
253 main (int argc, char* argv[])
258 retval = ctsimtext_main(argc, argv);
259 } catch (exception e) {
260 std::cerr << "Exception: " << e.what() << std::endl;
262 std::cerr << "Unknown exception\n";