X-Git-Url: http://git.kpe.io/?p=ctsim.git;a=blobdiff_plain;f=tools%2Fctsimtext.cpp;h=cd02ac68fb8569aeb8ed8e2de5db45259d4814f3;hp=0362a7a8b28dbae984d3db1affcf56532f582f3a;hb=da7c413d9b557a45c17a5af5d13de0825507a22d;hpb=265204d9e8774a723695b834755f1ee86e7717dc diff --git a/tools/ctsimtext.cpp b/tools/ctsimtext.cpp index 0362a7a..cd02ac6 100644 --- a/tools/ctsimtext.cpp +++ b/tools/ctsimtext.cpp @@ -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 @@ -27,7 +27,19 @@ #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 +#include +#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 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);