help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: idle-time processing within octave


From: Richard KIrk
Subject: Re: idle-time processing within octave
Date: Thu, 6 Jun 2013 08:23:52 -0700 (PDT)

Hi.

Here is a partial solution. Cut out the code below, save it as echoFile.cpp
and compile it. If you then run...

  echoFile /tmp/oct_cmd | octave --interactive --line-editing

... then while this is running, if copy a file with an octave command in to
/tmp/oct_cmd. The file should disappear, and the command should be passed to
octave as though the user typed it. Copying or renaming file is a good way
of stopping echoFile trying to read the file while it is still being
written.

This lets me run octave from a terminal, but other programs can generate
command files which will be automatically picked up and run, so moving a
slider can generate new values that get applied in Octave. 

No rights reserved, and no responsibility taken for errors.

Enjoy.
Richard Kirk 




---8<---cut here---8<---

#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h> 
#include <curses.h> 
#include <time.h>

void usage(void)
{
  printf("echoFile <file_name>\n"
         "\n"
         "This program copies any keyboard input straight to stdout.\n"
         "When the keyboard is idle, the program tries to read\n"
         "commands from a file. The keyboard is deemed idle when\n"
         "the last character was '\\n' and the keyboard has not\n"
         "been used for over a second. This allows us to use keyboard\n"
         "entry for programs but also allows other tools to generate\n"
         "commands via standard file IO.\n"
         "\n"
         "The command string is terminated by a carriage return,\n"
         "or a null, or a hard limit of 1024 bytes.\n"
         "\n"
         "If there is no command file, the program sends nulls\n"
         "to stdout. This will trigger an exit if the output pipe\n"
         "is broken. This can happen if we have sent a quit command\n"
         "to the program we are trying to drive.\n"
         "\n"
         "When the program has read the command, it will remove it.\n"
         "This stops the command being read more than once, and also\n"
         "signals that we are ready for another command. If the program\n"
         "cannot remove the command file, the program gives a warning\n"
         "and stops trying to read it.\n"
         "\n"
         "Example:\n"
         "\n"
         "echoFile /tmp/oct_cmd | octave --interactive --line-editing\n"
         "\n"
         "While this is running, copy a file with an octave command in\n"
         "to /tmp/oct_cmd. The file should disappear, and the command\n"
         "should be passed to octave as though the user typed it.\n"
         "Copying or renaming file is a good way of stopping echoFile\n"
         "trying to read the file while it is still being written.\n"
         );
}

int main(int argc, char* argv[])
{
  int n, c, idleFlag, readFlag;
  time_t T0, T1;
  struct termios attr, old_attr;
  const char *file_name;
  char command[1024];
  FILE *cmd_file;

  if (argc != 2) {
    usage();
    exit(0);
  }

  file_name = argv[1];
  readFlag = 1;

  // Fast key read or ERR, no break or echo
  tcgetattr( STDIN_FILENO, &attr );
  old_attr = attr;
  attr.c_lflag &= ~( ICANON | BRKINT | ECHO );
  attr.c_cc[VMIN] = 0;
  attr.c_cc[VTIME] = 0;
  tcsetattr( STDIN_FILENO, TCSANOW, &attr );

  T0 = time(NULL);
  idleFlag = 1;
  for(;;) {
    c = getchar();
    if (c == ERR) {
      // No new keystrokes...
      if (idleFlag) {
        // Not half-way through editing a line...
        T1 = time(NULL);
        if (difftime(T1,T0) > 1.0) {
          // Over a second since something last happened...
          T0 = T1;
          cmd_file = NULL;
          if (readFlag) cmd_file = fopen(file_name, "r");
          if (cmd_file) {
            fgets(command, 1024, cmd_file);
            fclose(cmd_file);
            if (remove(file_name)) {
              readFlag = 0;
              fprintf(stderr, "echoFile: cannot delete %s.\n", file_name);
            }
            for (n=0; n<1023; ++n) {
              if (command[n] == '\0') break;
              if (command[n] == '\n') break;
            }
            command[n] = '\n';
            command[n+1] = '\0';
            fputs(command, stdout);
          } else {
            // Trigger an exit if the output pipe is broken
            fputc(0, stdout);
          }
          fflush(stdout);
        }
      }
    } else {
      // New keystroke...
      idleFlag = 0;
      fputc(c, stdout);
      fflush(stdout);
      if (c == '\n') {
        // Return ends command
        idleFlag = 1;
        T0 = time(NULL);
      }
    }
  }

  tcsetattr( STDIN_FILENO, TCSANOW, &old_attr );
  exit(0);
}





--
View this message in context: 
http://octave.1599824.n4.nabble.com/idle-time-processing-within-octave-tp4653734p4653796.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]