示例#1
0
bool InputRedirection::process() {
    const int readlen = 5000;
    char* readt = new char[readlen]; // local test variable, used to help exit inf. loop

    // fill list with NULL zeroes. This clears list and prevents descrepancies.
    // these @s define the end of the pre-recorded list.
    for(int i=0; i<readlen; i++) readt[i] = 0x0;

    cin.getline(readt,readlen);
    CharString* Line = new CharString(readt,readlen);

    //if(!(Line->isEmpty()) && Line->getSize() > 1){ // is the line not empty? Is it longer then 1?
    handleInputLine(Line); // EXECUTE the line.
    //}else{
    //  return true;
    //}
    return false;
}
示例#2
0
void pollInput() {
  
  if (mchready()) {
    char ch = mgetch();

    if (ch == '\r') {
      mputs(NL);

      *inputBufferEnd = 0;
      handleInputLine();
      resetInputBuffer();

    } else {
      *inputBufferEnd = ch;
      inputBufferEnd++;
      mputchar(ch);

      if (inputBufferEnd == inputBuffer + INPUT_BUFFER_SIZE -1) {	
	mprintf(PSTR("ERROR: linebuffer overflow\n"));
	resetInputBuffer();
      }      
    }  
  }
}
示例#3
0
文件: caerctl.c 项目: sivapvarma/caer
int main(int argc, char *argv[]) {
    // First of all, parse the IP:Port we need to connect to.
    // Those are for now also the only two parameters permitted.
    // If none passed, attempt to connect to default IP:Port.
    const char *ipAddress = "127.0.0.1";
    uint16_t portNumber = 4040;

    if (argc != 1 && argc != 3) {
        fprintf(stderr, "Incorrect argument number. Either pass none for default IP:Port"
                "combination of 127.0.0.1:4040, or pass the IP followed by the Port.\n");
        return (EXIT_FAILURE);
    }

    // If explicitly passed, parse arguments.
    if (argc == 3) {
        ipAddress = argv[1];
        sscanf(argv[2], "%" SCNu16, &portNumber);
    }

    // Connect to the remote cAER config server.
    sockFd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sockFd < 0) {
        fprintf(stderr, "Failed to create TCP socket.\n");
        return (EXIT_FAILURE);
    }

    struct sockaddr_in configServerAddress;
    memset(&configServerAddress, 0, sizeof(struct sockaddr_in));

    configServerAddress.sin_family = AF_INET;
    configServerAddress.sin_port = htons(portNumber);
    inet_aton(ipAddress, &configServerAddress.sin_addr); // htonl() is implicit here.

    if (connect(sockFd, (struct sockaddr *) &configServerAddress, sizeof(struct sockaddr_in)) < 0) {
        fprintf(stderr, "Failed to connect to remote config server.\n");
        return (EXIT_FAILURE);
    }

    // Create a shell prompt with the IP:Port displayed.
    size_t shellPromptLength = (size_t) snprintf(NULL, 0, "cAER @ %s:%" PRIu16 " >> ", ipAddress, portNumber);
    char shellPrompt[shellPromptLength + 1]; // +1 for terminating NUL byte.
    snprintf(shellPrompt, shellPromptLength + 1, "cAER @ %s:%" PRIu16 " >> ", ipAddress, portNumber);

    // Set our own command completion function.
    linenoiseSetCompletionCallback(&handleCommandCompletion);

    // Generate command history file path (in user home).
    char commandHistoryFilePath[1024];

    char *userHomeDir = getUserHomeDirectory();
    snprintf(commandHistoryFilePath, 1024, "%s/.caerctl_history", userHomeDir);
    free(userHomeDir);

    // Load command history file.
    linenoiseHistoryLoad(commandHistoryFilePath);

    while (true) {
        // Display prompt and read input (NOTE: remember to free input after use!).
        char *inputLine = linenoise(shellPrompt);

        // Check for EOF first.
        if (inputLine == NULL) {
            // Exit loop.
            break;
        }

        // Add input to command history.
        linenoiseHistoryAdd(inputLine);

        // Then, after having added to history, check for termination commands.
        if (strncmp(inputLine, "quit", 4) == 0 || strncmp(inputLine, "exit", 4) == 0) {
            // Exit loop, free memory.
            free(inputLine);
            break;
        }

        // Try to generate a request, if there's any content.
        size_t inputLineLength = strlen(inputLine);

        if (inputLineLength > 0) {
            handleInputLine(inputLine, inputLineLength);
        }

        // Free input after use.
        free(inputLine);
    }

    // Close connection.
    close(sockFd);

    // Save command history file.
    linenoiseHistorySave(commandHistoryFilePath);

    return (EXIT_SUCCESS);
}