0

i have read a lot about what im asking, but the closest thing i have found is using the ncurses library. What im doing is a simple http client which will monitory some remote folders to check if its files have changed or if there are new files. Also, it has to check the stdin for some commands, so, I'm planning on using the father process to monitory stdin and worker process to monitory the remote directories. I would like to use something like keyboard interrupt. Something like installing a signal handler to keyboard pressing, so the user doesn't have to press enter after each command.

So far, ncurses does what i need, but i cant print stuff on stdout, but instead in stdscr. The problem with it is that this project is an assignment for college, and it is very likely to be checked through a script that read mi program's stdout...

so, any suggestions?

the relevant piece of code is:

#include <ncurses.h>

  initscr();        /* Start curses mode              */
  raw();            /* Line buffering disabled        */
  noecho();         /* Don't echo() while we do getch */
  char key;
  while (TRUE) {
    key = getch();
    switch(key)
      {
      case 's':
        printw("Instrucción recibida: STOP\n");
        printf("Hasta Luego!\n");
        fflush(stdout);
        /*NOTICE THIS HAS NO EFFECT, */
        refresh();
        sleep(2);
        endwin();           /* End curses mode        */
        fflush(stdout);
        exit(0);
        break;
      case 'c':
        printw("Instrucción recibida: CONTINUE\n");
        refresh();
        break;
      case 'p':
        printw("Instrucción recibida: PAUSE\n");
        refresh();
        break;
      default:
        printw("Instrucción desconocida recibida: %c\n",key);
        refresh();
      }
  }
  endwin();           /* End curses mode        */

when i run my program, if i redirect the output to a file, i get an empty file

$ verific -d http://url.toMy.directory/ > output
$ cat output
$

Thanks for your help!!! =)

Throoze
  • 3,988
  • 8
  • 45
  • 67
  • 2
    If the program is going to be checked with a script, why do you need such an "interactive" feature as uncooked input ? – cnicutar Oct 20 '11 at 08:22
  • well, the program does some other stuff, like telling the user if files were modified or added to the folder, plus, there are thing like expect, that can interact with cli programs; although, sincerely, i think the two tasks (checking the remote directories and waiting for user's input) are going to be checked separately. Anyways, probably you are right, and they don't use a script at all... but i would prefer not having to take the risk... hehehe – Throoze Oct 20 '11 at 08:49

1 Answers1

1

This was surprisingly hard to find and I'm still not happy with the result: You can use stty() to go into raw mode. This avoids (n)curses and any tampering with stdout.

From a shell script, you'd use stty raw (longer explanation).

This answer contains C code: Capture characters from standard input without waiting for enter to be pressed

Don't forget to restore the TTY's settings before you exit or your terminal will behave odd. I suggest to wrap your code into a shell script which contains trap "stty sane" EXIT near the beginning. This will always execute stty sane when the script terminates.

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • This seems a lot what i need!, have not tried it yet. I have a couple of questions first. Is what you are suggesting shell script? can i embed shell scripting within c code? I'm not an expert in c, but i know many unix commands have their counterpart as c functions... is there any for stty? if so, which libraries do i need to include? i would like to use pure c code... thanks! – Throoze Oct 20 '11 at 09:05
  • Sorry for my ignorance! hehehe what does `trap` does? it has no manpage! and I dont know where to look for it =/... – Throoze Oct 20 '11 at 09:11
  • I Already found how to put this shell scripting code within my c code. Using the `system()` function. I wrote the following function: `char getChar(){system("stty -echo raw");char input = getchar();system("stty sane");return input;}`, and works perfectly!!! thanks a lot!!! =) – Throoze Oct 20 '11 at 11:02
  • `trap` is a built-in command of the shell. Try `help trap` or `man bash`. – Aaron Digulla Oct 21 '11 at 10:08