r387: no message
[ctsim.git] / tools / ctsimtext.cpp
index 0362a7a8b28dbae984d3db1affcf56532f582f3a..6983cb0fe6e1614017cffa53c66412509fc56671 100644 (file)
@@ -9,7 +9,7 @@
 **  This is part of the CTSim program
 **  Copyright (C) 1983-2000 Kevin Rosenberg
 **
-**  $Id: ctsimtext.cpp,v 1.2 2001/01/08 03:06:37 kevin Exp $
+**  $Id: ctsimtext.cpp,v 1.9 2001/01/13 03:51:35 kevin Exp $
 **
 **  This program is free software; you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License (version 2) as
 
 #include "ct.h"
 
-static const char* g_szIdStr = "$Id: ctsimtext.cpp,v 1.2 2001/01/08 03:06:37 kevin Exp $";
+#ifdef HAVE_READLINE_H
+extern "C" {
+#include <readline.h>
+#include <history.h>
+};
+#elif defined(HAVE_READLINE_READLINE_H)
+extern "C" {
+#include <readline/readline.h>
+#include <readline/history.h>
+};
+#endif
+
+
+// Master shell for all command-line tools
+// If called as ctsimtext, program will look to next token on command-line as the function name
+// If linked to ctsimtext, but executed as another name, eg pjrec, then program will use that
+// linked name as name of function.
+
+static const char* const g_szIdStr = "$Id: ctsimtext.cpp,v 1.9 2001/01/13 03:51:35 kevin Exp $";
+static const char* const s_szProgramName = "ctsimtext";
 
 extern int if1_main (int argc, char* const argv[]);
 extern int if2_main (int argc, char* const argv[]);
@@ -39,36 +58,176 @@ extern int pj2if_main (int argc, char* const argv[]);
 extern int pjinfo_main (int argc, char* const argv[]);
 extern int pjrec_main (int argc, char* const argv[]);
 
+static int processCommand (int argc, char* const argv[]);
+static void convertStringToArgcv (char* szLine, int* piArgc, char*** pppArgv);
+
 
 void 
 ctsimtext_usage (const char *program)
 {
   std::cout << "usage: " << fileBasename(program) << " ctsim-function-name ctstim-function-parameters...\n";
   std::cout << "CTSim text shell\n\n";
-  std::cout << "  ifinfo        Image file information\n";
   std::cout << "  if1           Single image file conversion\n";
   std::cout << "  if2           Dual image file conversions\n";
-  std::cout << "  pjrec         Projection reconstruction\n";
+  std::cout << "  ifexport      Export an imagefile to a graphics file\n";
+  std::cout << "  ifinfo        Image file information\n";
+  std::cout << "  pj2if         Convert an projection file into an imagefile\n";
   std::cout << "  pjinfo        Projection file information\n";
+  std::cout << "  pjrec         Projection reconstruction\n";
   std::cout << "  phm2if        Convert a geometric phantom into an imagefile\n";
   std::cout << "  phm2pj        Take projections of a phantom object\n";
+}
+
+void 
+interactive_usage ()
+{
+  std::cout << "usage: function-name parameters...\n";
+  std::cout << "Available functions:\n";
   std::cout << "  ifexport      Export an imagefile to a graphics file\n";
+  std::cout << "  ifinfo        Image file information\n";
+  std::cout << "  if1           Single image file conversion\n";
+  std::cout << "  if2           Dual image file conversions\n";
+  std::cout << "  phm2if        Convert a geometric phantom into an imagefile\n";
+  std::cout << "  phm2pj        Take projections of a phantom object\n";
+  std::cout << "  pjinfo        Projection file information\n";
   std::cout << "  pj2if         Convert an projection file into an imagefile\n";
+  std::cout << "  pjrec         Projection reconstruction\n";
+  std::cout << "  quit          Quits shell\n";
+  std::cout << "All functions accept --help as parameter for online help\n\n";
 }
 
+static bool s_bInteractive = false;
 
 int 
 ctsimtext_main (int argc, char * argv[])
 {
-  if (argc < 2) {
-    ctsimtext_usage (argv[0]);
-    return (1);
+  int iReturn = 0;  
+  
+  if (argc > 1 && strcmp(argv[0], fileBasename (s_szProgramName)) == 0) {
+    argv = &argv[1];
+    argc--;
+    iReturn = processCommand (argc, argv);
+  } else if (argc > 1){
+    iReturn = processCommand (argc, argv);
+  } else {
+    s_bInteractive = true;
+    char szPrompt[] = "CTSim> ";
+    std::cout << "CTSim Text Shell (Type \"quit\" to end)\n\n";
+    
+    while (1) {
+#ifdef HAVE_READLINE
+      char* pszInputLine = readline (szPrompt);
+      if (! pszInputLine)
+        break;
+      if (*pszInputLine != EOS)
+        add_history (pszInputLine);
+      
+#else  // DONT_HAVE_READLINE
+      
+      static const int s_MaxLineLength = 1024;
+      char* pszInputLine = new char [s_MaxLineLength+1];
+      std::cout << szPrompt;
+      std::cin.getline (pszInputLine, s_MaxLineLength);
+
+#ifdef DEBUG
+      std::cout << "#" << pszInputLine << "#\n";
+#endif
+      
+      std::cout << std::flush;
+      std::cout << "\n";
+#endif  // DONT_HAVE_READLINE
+      
+      if (strncasecmp (pszInputLine, "quit", 4) == 0) 
+        break;
+      
+      convertStringToArgcv (pszInputLine, &argc, &argv);
+#ifdef DEBUG
+      for (unsigned int i = 0; i < argc; i++)
+        std::cout << "Token " << i << ": " << argv[i] << "\n";
+#endif
+      iReturn = processCommand (argc, argv);
+
+      delete pszInputLine;
+    }
   }
+  
+  
+  return iReturn;
+}
 
-  const char* const pszFunction = argv[1];
-  argv = &argv[1];
-  argc--;
+static void
+convertStringToArgcv (char* pszLine, int* piArgc, char*** pppArgv)
+{
+  char* pCurrentPos = pszLine;
+  int nTokens = 0;
+  std::vector<char*> vecpszToken;
+  
+  // Process line
+  bool bInDoubleQuote = false;
+  bool bInSingleQuote = false;
+  bool bInToken = false;
+  
+  while (*pCurrentPos) {
+    if (isspace (*pCurrentPos)) {
+      if (! bInToken)
+        *pCurrentPos = 0;
+      else if (bInSingleQuote || bInDoubleQuote)
+        ;
+      else { // in non-quote token
+        *pCurrentPos = 0;
+        bInToken = false;
+      }
+    }
+    else if (*pCurrentPos == '\"') {
+      if (bInSingleQuote) {
+        bInSingleQuote = false;
+        *pCurrentPos = 0;
+      } else {
+        bInSingleQuote = true;
+        if (! bInToken) {
+          bInToken = true;
+          nTokens++;
+          vecpszToken.push_back (pCurrentPos+1);
+        }
+      }
+    } else if (*pCurrentPos == '\'') {
+      if (bInDoubleQuote) {
+        bInDoubleQuote = false;
+        *pCurrentPos = 0;
+      } else {
+        bInDoubleQuote = true;
+        if (! bInToken) {
+          bInToken = true;
+          nTokens++;
+          vecpszToken.push_back (pCurrentPos+1);
+        }
+      }
+    } else if (! bInToken) {   // nonwhite, non-quote character
+      bInToken = true;
+      nTokens++;
+      vecpszToken.push_back (pCurrentPos);
+    }
+    
+    pCurrentPos++;
+  }
+  
+  *piArgc = nTokens;
+  if (nTokens > 0) {
+    *pppArgv = new char* [nTokens];
+    for (unsigned int iToken = 0; iToken < vecpszToken.size(); iToken++)
+      (*pppArgv)[iToken] = vecpszToken[iToken];
+  } else
+    *pppArgv = NULL;
+}
 
+static int 
+processCommand (int argc, char* const argv[])
+{
+  if (argc < 1)
+    return 1;
+
+  const char* const pszFunction = fileBasename (argv[0]);
+  
   if (strcasecmp (pszFunction, "if1") == 0)
     return if1_main (argc, argv);
   else if (strcasecmp (pszFunction, "if2") == 0)
@@ -88,8 +247,11 @@ ctsimtext_main (int argc, char * argv[])
   else if (strcasecmp (pszFunction, "pjrec") == 0)
     return pjrec_main (argc, argv);
   else {
-    std::cout << "Unknown function name" << pszFunction << "\n\n";
-    ctsimtext_usage (argv[0]);
+    std::cout << "Unknown function name: " << pszFunction << "\n";
+    if (s_bInteractive)
+      interactive_usage();
+    else
+      ctsimtext_usage (s_szProgramName);
     return 1;
   }
 }
@@ -98,15 +260,32 @@ int
 main (int argc, char* argv[])
 {
   int retval = 1;
-
+  
   try {
     retval = ctsimtext_main(argc, argv);
   } catch (exception e) {
-         std::cerr << "Exception: " << e.what() << std::endl;
+    std::cerr << "Exception: " << e.what() << std::endl;
   } catch (...) {
-         std::cerr << "Unknown exception\n";
+    std::cerr << "Unknown exception\n";
   }
-
+  
   return (retval);
 }
 
+
+// Hack to fix linking problems when not linking with wxWindows and CTSim GUI routines
+
+#ifdef HAVE_WXWINDOWS
+#include "../src/dlgezplot.h"
+
+EZPlotDialog::EZPlotDialog (wxWindow* parent)
+{}
+
+EZPlotDialog::~EZPlotDialog()
+{}
+
+unsigned long
+wxDialog::OnCtlColor(unsigned long a,unsigned long b,unsigned int c,unsigned int d,unsigned int e,long f)
+{return 0;}
+
+#endif
\ No newline at end of file