r368: Additions to ctsimtext
[ctsim.git] / tools / ctsimtext.cpp
index 0362a7a8b28dbae984d3db1affcf56532f582f3a..cd02ac68fb8569aeb8ed8e2de5db45259d4814f3 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.3 2001/01/09 20:38:53 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
+#include <readline.h>
+#include <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.3 2001/01/09 20:38:53 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,6 +51,10 @@ 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)
@@ -60,14 +76,104 @@ ctsimtext_usage (const char *program)
 int 
 ctsimtext_main (int argc, char * argv[])
 {
-  if (argc < 2) {
-    ctsimtext_usage (argv[0]);
-    return (1);
+  int iReturn = 0;
+
+  if (strcmp(argv[0],s_szProgramName) == 0) {
+    argv = &argv[1];
+    argc--;
+    iReturn = processCommand (argc, argv);
+  } else if (argc > 1){
+    iReturn = processCommand (argc, argv);
+  } else {
+    char szPrompt[] = "CTSim> ";
+    std::string strInput;
+    while (1) {
+      std::cout << "CTSimText Interactive mode\nType quit to end";
+#ifdef HAVE_READLINE
+      char* pszInput = readline (szPrompt);
+#else
+      std::cout << szPrompt;
+      std::cin >> strInput;
+      char* pszInput = new char [strInput.length() + 1];
+      strncpy (pszInput, strInput.c_str(), sizeof(pszInput));
+#endif
+      if (strncasecmp (pszInput, "quit", 4) == 0) {
+        delete pszInput;
+        break;
+      }
+      convertStringToArgcv (pszInput, &argc, &argv);
+      delete pszInput;
+      iReturn = processCommand (argc, argv);
+    }
+  }
+
+  return iReturn;
+}
+
+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 (iswhite (*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;
+      } else {
+        bInSingleQuote = true;
+        if (! bInToken) {
+          bInToken = true;
+          nTokens++;
+          vecpszToken.push_back (pCurrentPos+1);
+        }
+      }
+    } else if (*pCurrentPos = '\'') {
+      if (bInDoubleQuote) {
+        bInDoubleQuote = false;
+      } 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++;
   }
 
-  const char* const pszFunction = argv[1];
-  argv = &argv[1];
-  argc--;
+  *piArgc = nTokens;
+  pppArgv = new char** [nTokens];
+  for (unsigned int iToken = 0; iToken < vecpszToken.size(); iToken++)
+    (*pppArgv)[iToken] = vecpszToken[iToken];
+}
+
+static int 
+processCommand (int argc, char* const argv[])
+{
+  const char* const pszFunction = argv[0];
 
   if (strcasecmp (pszFunction, "if1") == 0)
     return if1_main (argc, argv);