문제

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!!! =)

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top