Ejemplo n.º 1
0
void pConsole::executeCommand( const QString& command, bool writeCommand, bool showPrompt )
{
	const QStringList clearCommands = QStringList( "clear" ) << "cls";
	
	if ( clearCommands.contains( command, Qt::CaseInsensitive ) )
	{
		clear();
		
		if ( showPrompt )
		{
			displayPrompt();
		}
		
		return;
	}
	
	// write command to execute
	if ( writeCommand )
	{
		if ( !currentCommand().isEmpty() )
		{
			displayPrompt();
		}
		
		insertPlainText( command );
	}
	
	// execute command
	int res;
	QString strRes = interpretCommand( command, &res );
	
	// write output in different colors if needed
	if ( res == 0 )
	{
		useColor( ctOutput );
	}
	else
	{
		useColor( ctError );
	}
	
	if ( !strRes.isEmpty() )
	{
		appendPlainText( strRes );
	}
	
	useColor( ctCommand );
	
	// display the prompt again if needed
	if ( showPrompt )
	{
		displayPrompt();
	}
}
Ejemplo n.º 2
0
void KomLoop(void) {
  int defaultCmd;
  g_unreadRepliesStack = CreateStack();

  for(;;) {
    if(StackSize(g_unreadRepliesStack) > 0) {
      defaultCmd = CMD_NEXTREPLY;
    } else if(hasUnreadInConf(mote2)) {
      defaultCmd = CMD_NEXTTEXT;
    } else if(HasUnreadMail()) {
      defaultCmd = CMD_GOMAIL;
    } else if(FindNextUnreadConf(mote2) >= 0) {
      defaultCmd = CMD_NEXTCONF;
    } else {
      defaultCmd = CMD_SEETIME;
    }

    displayPrompt(defaultCmd);
    if(shouldLogout()) {
      break;
    }
  }

  DeleteStack(g_unreadRepliesStack);
}
Ejemplo n.º 3
0
void Console::handleReturn()
{
	QString command=getCommand();
	moveCursor(QTextCursor::End,QTextCursor::MoveAnchor);
	emit execCommand(command);
	displayPrompt();
}
Ejemplo n.º 4
0
void Game::play()
{
    m_tank.display(m_screen);
    displayStatus();  //  score, rows left, level
    displayPrompt("Press the Enter key to begin playing Imitris!");
    waitForEnter();  // [in UserInterface.h]

    for(;;)
    {
        if ( ! playOneLevel())
            break;
        displayPrompt("Good job!  Press the Enter key to start next level!");
        waitForEnter();
    }
    displayPrompt("Game Over!  Press the Enter key to exit!");
    waitForEnter();
}
Ejemplo n.º 5
0
/*
 * Called from a server to kill itself
 *
 */
void abortServer(char* childName, char* serverToAbort) {
  if(strcmp(serverToAbort, childName) == 0) {
    printf("Aborting server %s...\n", childName);
    fflush(stdout);
    displayPrompt();
    exit(0);
  }
}
Ejemplo n.º 6
0
/*
 * Called from a server to create a child process
 *
 */
void createGrandChild(int fd2[10][2], int activeProcs) {

  //Listen
  while(1) {
    char fromParent[1024];
    read(fd2[activeProcs][0], fromParent, sizeof(fromParent));

    fromParent[strlen(fromParent) - 1] = '\0';

    if(strstr(fromParent, "kill") != NULL) {
      printf("Killing grandchild process\n");
      fflush(stdout);
      displayPrompt();
      exit(0);  
    }
    displayPrompt();
  }
}
Ejemplo n.º 7
0
/*
 * Called from a server to print its status
 *
 */
void printServerStatus(char* serverName, pid_t serverId, pid_t children[10], int childrenNum) {
  printf("SERVER:\t %s (%d)\n", serverName, serverId); 
  int k = 0;
  while(k < childrenNum) {
    printf("----- Child %d (%d)\n", k + 1, children[k]);
    k++;
  }
  displayPrompt();
}
Ejemplo n.º 8
0
void pConsole::setPrompt( const QString& prompt )
{
	mPrompt = prompt;
	
	if ( isPromptVisible() )
	{
		displayPrompt();
	}
}
Ejemplo n.º 9
0
void printprompt(void)
{	char *pr;

	dbg_printmem();

	pr = getEnv(PROMPTVAR);        /* get PROMPT environment var. */

	displayPrompt(pr? pr: DEFAULT_PROMPT);
}
Ejemplo n.º 10
0
/*--------------------------------------------------------------------------*/
char *TerminalGetString(char *prompt)
{
    if (InitTerm)
    {
        InitializeTerminal();
        InitTerm = FALSE;
    }

    newLine();

    setCurrentPrompt(prompt);

    /* print the prompt */
    displayPrompt();

    /* initialize history search */
    setSearchedTokenInScilabHistory(NULL);

    for (;;)
    {
        unsigned char cur_char = TerminalGetchar();

        if (cur_char <= 0)
        {
            return NULL;
        }

        /* http://bugzilla.scilab.org/show_bug.cgi?id=1052 */
        if (ismenu () == 1)
        {
            /* Abort current line */
            return NULL;
        }

        if ( (cur_char == CR_1) || (cur_char == CR_2) )
        {
            if ( isHistorySearch() )
            {
                putLineSearchedHistory();
            }
            else
            {
                char *line = getCurrentLine();
                TerminalPutc('\n');
                appendLineToScilabHistory(line);
                return line;
            }
        }
        else
        {
            TerminalPutc(cur_char);
            addCharacterCurrentLine(cur_char);
        }
    }
    return NULL;
}
Ejemplo n.º 11
0
int productTest(void)
{
    char    c;
    
	while(1)
	{
        displayTest();
        displayPrompt();

        c = readch();

        if(c == '\n') {
            displayPrompt();
            continue;
        }

		if (c < '1' || c > '9')
			break;
    }

    return 0;
}
Ejemplo n.º 12
0
/*
 * Called from a server to abort one of it's children
 *
 */
void abortGrandChild(int fd[10][2], int numActive, int numMin, char* childToAbort, char* serverName) {
  if(strcmp(serverName, childToAbort) == 0) {
    if(numActive - 1 >= numMin) {
      char message[] = "kill";
      close(fd[numActive - 1][0]);
      write(fd[numActive - 1][1], message, strlen(message) + 1);
      fflush(stdout);
    } else {
      printf("Deleting child on %s will put it under minimum of %d\n", serverName, numMin);
      fflush(stdout);
      displayPrompt();
    }
  }
}
Ejemplo n.º 13
0
/*--------------------------------------------------------------------------*/
void redrawLine(void)
{
	int i =0;
	char *line = getCurrentLine();

	displayPrompt();
	for (i = max_pos; i > cur_pos; i--)  backSpace ();
	if (line)
	{
		copyLine(line);
		FREE(line);
		line = NULL;
	}
}
Ejemplo n.º 14
0
void Terminal::printEditLine(LineEditor &editor, bool erase_char) {
    CR();
    displayPrompt();
    // print the new edit line
    const char *buf = editor.buf().c_str();
    while (*buf) { putch(*buf++); }

    // erase the extra character if the user shortened the input line
    if (erase_char) {
        putch(' ');
        BS();
    }

    repositionPrinter(editor);
}
Ejemplo n.º 15
0
/*--------------------------------------------------------------------------*/
void clearCurrentLine(void)
{
	int i = 0;

	reallocLineBuffer();

	for (i = 0; i < max_pos; i++) cur_line[i] = '\0';

	moveBeginningLine();
	
	for (i = 0; i < max_pos; i++) TerminalPutc(VK_SPACE);

	TerminalPutc('\r');
	displayPrompt();

	newLine();
}
Ejemplo n.º 16
0
KeyMap::TerminalAction LocalTerminal::collectInput(LineEditor &editor) {
    unsigned char c = 0;
    KeyMap::TerminalAction action;
    bool escape = false;

    CR();
    displayPrompt();
    printEditLine(editor);
    while (true) {
        action = mKeyMap.getAction(c = getch(), escape);
        escape = false;

        // Here only handle local line editing events.
        // Propagate all other events to higher processing layers
        action = handleEditAction(action, editor, escape, c);
        if (action != KeyMap::TerminalAction::ACTION_IGNORE)
            return action;
    }
}
Ejemplo n.º 17
0
void	Console::run()
{
  char input[256];

  while (true)
    {
      std::vector<std::string> elems;

      displayPrompt();
      std::cin.getline(input,256);
      std::string s(input);

      split(elems, s, ' ');
      
      if (!elems.size())
	continue;
      
      if (elems[0] == "ls" || elems[0] == "dir")
	fs->ls();
      else if (elems[0] == "cd")
	{
	  if (elems.size() > 1)
	    fs->cd(elems[1].c_str());
	  else
	    fs->cd();
	}
      else if (elems[0] == "exit" || elems[0] == "quit")
	break;
      else if (elems[0] == "fdisk")
	fs->fdisk();
      else if (elems[0] == "cp")
	{
	  if (elems.size() < 3)
	    std::cerr << "Missing arguments. Usage : cp [file] [dest_dir]" << std::endl;
	  else
	    fs->cp(elems[1].c_str(), elems[2].c_str());
	}
    }
}
Ejemplo n.º 18
0
int main() {
  pid_t server[5];
  char str[1024];
  int fd[5][2];
  int serverNum = 0;
  int child_index = -1;

  displayPrompt();

  while(1) {

    fgets(str, 1024, stdin);

    if(strstr(str, "createServer") != NULL) {
      if(serverNum == 4) {
        printf("cannot spawn anymore servers\n");
        fflush(stdout);
        exit(0);
      }

      if(pipe(fd[serverNum]) < 0) {
        perror("pipe error");
      }

      server[serverNum] = fork();

      //Child
      if(server[serverNum] == 0) {
        char* serverName = NULL;
        int minProcs = 0;
        int maxProcs = 0;
        int activeProcs = 0;
        char* flag[3];
        int fd2[10][2];
        pid_t child[10];

        char* throwAway = strtok(str, " ");
        flag[0] = strtok(NULL, " ");

        int m = 0;

        //Pass arguments
        while(flag[m] != NULL) {
          m++;
          flag[m] = strtok(NULL, " ");
        }

        //Set important variables
        minProcs = atoi(flag[0]);
        maxProcs = atoi(flag[1]);
        serverName = flag[2];
        serverName[strlen(serverName) - 1] = '\0';
        child_index = serverNum;

        printf("Creating Child PID: %d Name: %s Min Procs:%d Max Procs:%d\n", getpid(), serverName, minProcs, maxProcs);
        fflush(stdout);
        displayPrompt();

        //Create children within server
        while(activeProcs < minProcs) {
          if(pipe(fd2[activeProcs]) < 0) {
            perror("pipe error 3");
          }

          child[activeProcs] = fork();

          //Child of server
          if(child[activeProcs] == 0) {
            fflush(stdout);
            createGrandChild(fd2, activeProcs);
            displayPrompt();
          }
          
          //Parent
          if(child[activeProcs]){
            activeProcs++;
          }
        }


        close(fd[child_index][1]);

        //Listen for command
        while(1) {
          char fromPipe[1024];

          read(fd[child_index][0], fromPipe, sizeof(fromPipe));
          fromPipe[strlen(fromPipe) - 1] = '\0';
          char* doNothing = strtok(fromPipe, " ");

          if(strstr(fromPipe, "abortServer") != NULL) {
            char* serverToAbort = strtok(NULL, " ");
            abortServer(serverName, serverToAbort);

          } else if(strstr(fromPipe, "abortProc") != NULL) {
            char* onServer = strtok(NULL, " ");
            abortGrandChild(fd2, activeProcs, minProcs, onServer, serverName);

          } else if(strstr(fromPipe, "displayStatus") != NULL) {
            printServerStatus(serverName, getpid(), child, activeProcs); 

          } else if(strstr(fromPipe, "createProc") != NULL) {
            char* onServer = strtok(NULL, " ");

            //Special case: Must create new process without function call :(
            if((activeProcs + 1) > maxProcs && strcmp(serverName, onServer ) == 0) {
              printf("Adding another proc would put %s over it's limit of %d procs", serverName, maxProcs);
              fflush(stdout);
              displayPrompt();
            } else {
              if(pipe(fd2[activeProcs]) < 0) {
                perror("pipe error 3");
              }

              child[activeProcs] = fork();

              //Server's new child
              if(child[activeProcs] == 0) {
                printf("Creating grandchild for process %d\n", getppid());
                fflush(stdout);
                displayPrompt();
                createGrandChild(fd2, activeProcs);
              }

              if(child[activeProcs]){
                activeProcs++;
              }
            }
          }
        }
        break;
      }

      //Parent
      if(server[serverNum]) {
        serverNum++;
      }

    } else if(strstr(str, "abortServer") != NULL) {
      sendCommandToChild(fd, str, serverNum);
      serverNum--;
    } else if(strstr(str, "abortProc") != NULL) {
      sendCommandToChild(fd, str, serverNum); 
    } else if(strstr(str, "createProc") != NULL) {
      sendCommandToChild(fd, str, serverNum);
    } else if(strstr(str, "displayStatus") != NULL) {
      sendCommandToChild(fd, str, serverNum);
    } else {
      printf("please enter a valid command\n");
      fflush(stdout);
      displayPrompt();
    }
  }
  return 0;
}
Ejemplo n.º 19
0
void Console::setPrompt(QString p)
{
	prompt=p;
	promptLength=prompt.length();
	displayPrompt();
}
Ejemplo n.º 20
0
/*--------------------------------------------------------------------------*/
static void TermCompletionOnAll(char *lineBeforeCaret, char *lineAfterCaret, char *defaultPattern)
{
    if (defaultPattern)
    {
        int numberWordFound = 0;
        char **completionDictionaryFunctions = NULL;
        int sizecompletionDictionaryFunctions = 0;

        char **completionDictionaryCommandWords = NULL;
        int sizecompletionDictionaryCommandWords = 0;

        char **completionDictionaryMacros = NULL;
        int sizecompletionDictionaryMacros = 0;

        char **completionDictionaryVariables = NULL;
        int sizecompletionDictionaryVariables = 0;

        char **completionDictionaryHandleGraphicsProperties = NULL;
        int sizecompletionDictionaryHandleGraphicsProperties = 0;

        char **completionDictionaryFields = NULL;
        int sizecompletionDictionaryFields = 0;

        completionDictionaryFields = completionOnFields(lineBeforeCaret, defaultPattern, &sizecompletionDictionaryFields);

        if ((completionDictionaryFields == NULL) && strcmp(defaultPattern, ""))
        {
            completionDictionaryFunctions = completionOnFunctions(defaultPattern, &sizecompletionDictionaryFunctions);
            completionDictionaryCommandWords = completionOnCommandWords(defaultPattern, &sizecompletionDictionaryCommandWords);
            completionDictionaryMacros = completionOnMacros(defaultPattern, &sizecompletionDictionaryMacros);
            completionDictionaryVariables = completionOnVariablesWithoutMacros(defaultPattern, &sizecompletionDictionaryVariables);
            completionDictionaryHandleGraphicsProperties = completionOnHandleGraphicsProperties(defaultPattern, &sizecompletionDictionaryHandleGraphicsProperties);
        }
        numberWordFound = sizecompletionDictionaryFunctions + sizecompletionDictionaryCommandWords +
                          sizecompletionDictionaryMacros + sizecompletionDictionaryVariables +
                          sizecompletionDictionaryHandleGraphicsProperties + sizecompletionDictionaryFields;

        if (numberWordFound > 0)
        {
            if (numberWordFound == 1)
            {
                char **completionDictionary = NULL;
                char *new_line = NULL;

                if (completionDictionaryFields)
                {
                    completionDictionary = completionDictionaryFields;
                }
                if (completionDictionaryFunctions)
                {
                    completionDictionary = completionDictionaryFunctions;
                }
                if (completionDictionaryCommandWords)
                {
                    completionDictionary = completionDictionaryCommandWords;
                }
                if (completionDictionaryMacros)
                {
                    completionDictionary = completionDictionaryMacros;
                }
                if (completionDictionaryVariables)
                {
                    completionDictionary = completionDictionaryVariables;
                }
                if (completionDictionaryHandleGraphicsProperties)
                {
                    completionDictionary = completionDictionaryHandleGraphicsProperties;
                }

                new_line = completeLine(lineBeforeCaret, completionDictionary[0], NULL, defaultPattern, FALSE, lineAfterCaret);
                if (new_line)
                {
                    clearCurrentLine();
                    copyLine(new_line);
                    FREE(new_line);
                }
            }
            else
            {
                char *commonAll = NULL;
                if (completionDictionaryFields)
                {
                    commonAll = getCommonPart(completionDictionaryFields, sizecompletionDictionaryFields);
                    displayCompletionDictionary(completionDictionaryFields, sizecompletionDictionaryFields, (char *)_("Scilab Fields"));
                    freeArrayOfString(completionDictionaryFields, sizecompletionDictionaryFields);
                }
                else
                {
                    char *commonFunctions = getCommonPart(completionDictionaryFunctions, sizecompletionDictionaryFunctions);
                    char *commonCommandWords = getCommonPart(completionDictionaryCommandWords, sizecompletionDictionaryCommandWords);
                    char *commonMacros = getCommonPart(completionDictionaryMacros, sizecompletionDictionaryMacros);
                    char *commonVariables = getCommonPart(completionDictionaryVariables, sizecompletionDictionaryVariables);
                    char *commonHandleGraphicsProperties = getCommonPart(completionDictionaryHandleGraphicsProperties, sizecompletionDictionaryHandleGraphicsProperties);

                    int sizecommonsDictionary = 0;
                    char **commonsDictionary = concatenateStrings(&sizecommonsDictionary, commonFunctions,
                                               commonMacros, commonCommandWords, commonVariables, commonHandleGraphicsProperties);

                    if (sizecommonsDictionary > 0)
                    {
                        if (sizecommonsDictionary == 1)
                        {
                            commonAll = strdup(commonsDictionary[0]);
                        }
                        else
                        {
                            commonAll = getCommonPart(commonsDictionary, sizecommonsDictionary);
                        }
                        freeArrayOfString(commonsDictionary, sizecommonsDictionary);
                    }

                    displayCompletionDictionary(completionDictionaryFunctions, sizecompletionDictionaryFunctions, (char *)_("Scilab Function"));
                    displayCompletionDictionary(completionDictionaryCommandWords, sizecompletionDictionaryCommandWords, (char *)_("Scilab Command"));
                    displayCompletionDictionary(completionDictionaryMacros, sizecompletionDictionaryMacros, (char *)_("Scilab Macro"));
                    displayCompletionDictionary(completionDictionaryVariables, sizecompletionDictionaryVariables, (char *)_("Scilab Variable"));
                    displayCompletionDictionary(completionDictionaryHandleGraphicsProperties, sizecompletionDictionaryHandleGraphicsProperties, (char *)_("Graphics handle field"));
                    freeArrayOfString(completionDictionaryFunctions, sizecompletionDictionaryFunctions);
                    freeArrayOfString(completionDictionaryCommandWords, sizecompletionDictionaryCommandWords);
                    freeArrayOfString(completionDictionaryMacros, sizecompletionDictionaryMacros);
                    freeArrayOfString(completionDictionaryVariables, sizecompletionDictionaryVariables);
                    freeArrayOfString(completionDictionaryHandleGraphicsProperties, sizecompletionDictionaryHandleGraphicsProperties);
                }

                displayPrompt();
                newLine();

                if (commonAll)
                {
                    char *newline = NULL;

                    newline = completeLine(lineBeforeCaret, commonAll, NULL, defaultPattern, FALSE, lineAfterCaret);

                    if (newline)
                    {
                        clearCurrentLine();
                        copyLine(newline);
                        FREE(newline);
                    }
                    FREE(commonAll);
                    commonAll = NULL;
                }
            }
        }
    }
}
Ejemplo n.º 21
0
/*--------------------------------------------------------------------------*/
static void TermCompletionOnFiles(char **dictionaryFiles, int sizedictionaryFiles,
                                  char *lineBeforeCaret, char *lineAfterCaret, char *filePattern, char *defaultPattern)
{
    if (dictionaryFiles)
    {
        if (sizedictionaryFiles == 1)
        {
            char *newline = completeLine(lineBeforeCaret, dictionaryFiles[0], filePattern, defaultPattern, TRUE, lineAfterCaret);
            if (newline)
            {
                clearCurrentLine();
                copyLine(newline);
                FREE(newline);
                return;
            }
        }
        else
        {
            char *common = getCommonPart(dictionaryFiles, sizedictionaryFiles);

            displayCompletionDictionary(dictionaryFiles,
                                        sizedictionaryFiles, gettext("File or Directory"));

            displayPrompt();
            newLine();

            if (defaultPattern[0] == 0)
            {
                int lennewline = (int)strlen(lineBeforeCaret) + (int)strlen(lineAfterCaret);
                char *newline = (char*)MALLOC(sizeof(char) * (lennewline + 1));

                clearCurrentLine();
                if (newline)
                {
                    strcpy(newline, lineBeforeCaret);
                    strcat(newline, lineAfterCaret);
                    copyLine(newline);
                    FREE(newline);
                    newline = NULL;
                }
            }
            else if (common)
            {
                char *newline = completeLine(lineBeforeCaret, common, filePattern, defaultPattern, TRUE, lineAfterCaret);
                if (newline)
                {
                    clearCurrentLine();
                    copyLine(newline);
                    FREE(newline);
                    return;
                }
                else
                {
                    int lennewline = (int)strlen(lineBeforeCaret) + (int)strlen(lineAfterCaret);
                    newline = (char*)MALLOC(sizeof(char) * (lennewline + 1));

                    clearCurrentLine();
                    if (newline)
                    {
                        strcpy(newline, lineBeforeCaret);
                        strcat(newline, lineAfterCaret);
                        copyLine(newline);
                        FREE(newline);
                        newline = NULL;
                    }
                }
                FREE(common);
                common = NULL;
            }
        }
    }
}
Ejemplo n.º 22
0
int main()
{
  const char *whitespace = " \n\r\f\t\v";  //Whitespace characters.
  char buffer[81];  //Buffer to hold 80 characters plus 1 null terminator.
  List* tokens;
  Command* command;
  
  /* enter into an infinite loop, prompting user for input, processing the input,
     until user decides to exit */
  while(1)
  {
    //Read in up to 80 characters or until the newline is encountered.

    displayPrompt();

    if (!fgets(buffer, 81, stdin))
    {
      printf("\nfgets() failed.\n");
      buffer[0] = 0;

      continue;
    }

    /* first we tokenize the command by whitespace */
    tokens = tokenize(buffer, whitespace);

    /* then we turn the command into a Command object */
    command = listToCommand(tokens);

    /* then we expand the environment variables (if any) */
    if(expandEnvironmentVars(command) == 0)
    {

      /* then we setup the command for redirection, if necessary */
      setupRedirection(command);

      /* check out our command, ensure it's been processed correctly and is ready for execution  */
      if(DEBUG) dumpCommand(command); 

      executeCommand(command);
    }

    /* cleanup after ourselves */
    linkedListClear(tokens);
    if(tokens != NULL)
      free(tokens);
    
    if(command != NULL)
    {
      linkedListClear(command->command);
      if(command->command != NULL)
        free(command->command);
      if(command->infile != NULL) 
        free(command->infile);
      if(command->outfile != NULL) 
        free(command->outfile);
      
      free(command);
    }
  }

  return 0;
}