6

Command processor with debug mode

  1. /*
  2.  * Improved command processor with debug level.
  3.  *
  4.  *   signal
  5.  *   EINTR
  6.  *
  7.  *   ONDEBUGn
  8.  */
  9.  
  10. #include <sys/select.h>
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <strings.h>
  15. #include <errno.h>
  16. #if defined( LINUX )
  17. #include <string.h>
  18. #endif
  19.  
  20. #include <signal.h>
  21.  
  22. #include "debug.h"
  23.  
  24. #if defined( DEBUG )
  25. int debug = 0;
  26. #endif
  27.  
  28. void prompt() {
  29.     static const char *msg = "? ";
  30.  
  31.     fputs( msg, stdout );
  32. }
  33.  
  34. void intr( int sig ) {
  35.     ONDEBUG9( fprintf( stdout, "<sig=%i>\n", sig ); );
  36.     signal(sig, intr); /* catch again */
  37. }
  38.  
  39. void startup() {
  40.     signal(SIGHUP, intr);
  41.     signal(SIGINT, intr);
  42.     signal(SIGTERM, intr);
  43.  
  44.     prompt();
  45. }
  46.  
  47. void read_input() {
  48.     char line[4096];
  49.     char *tok;
  50.  
  51.     static char *usage = "echo text\ndebug [0-9]\nquit\n";
  52.  
  53.     if ( fgets(line, sizeof (line), stdin) == 0 )
  54.         exit(0);
  55.     /* command? */
  56.     if ( (tok = strtok(line, " \t\n")) ) {
  57.         ONDEBUG5( fprintf( stdout, "[%s]\n", tok ); );
  58.         if ( tok[0] == 'q' && (tok[1] == '\0' || strcmp(tok, "quit") == 0) )
  59.             exit(0);
  60. #if defined( DEBUG )
  61.         if ( tok[ 0 ] == 'd' && (tok[ 1 ] == '\0' || strcmp( tok, "debug" ) == 0) ) {
  62.             tok = strtok( 0, " \t\n" );
  63.             if ( tok )
  64.                 debug = atoi( tok );
  65.             else
  66.                 fprintf( stdout, "%i\n", debug );
  67.         }
  68. #endif
  69.         else if ( tok[0] == 'e' && (tok[1] == '\0' || strcmp(tok, "echo") == 0) )
  70.             fputs( line + strlen(tok) + 1, stdout );
  71.         else
  72.             fputs( usage, stdout );
  73.     }
  74.     prompt();
  75. }
  76.  
  77. void main_loop() {
  78.     fd_set read_fds;
  79.     int n_fds = FD_SETSIZE;
  80.  
  81.     for (;;) { /* forever */
  82.         FD_ZERO(&read_fds);
  83.  
  84.         /* standard input? */
  85.         FD_SET(0, &read_fds);
  86.  
  87.         switch (select(n_fds, &read_fds, 0, 0, 0)) {
  88.         case -1: /* trouble */
  89.             ONDEBUG8(fprintf(stdout, "<errno=%i>\n", errno));
  90.             if (errno != EINTR) {
  91.                 fprintf(stderr, "%s\n", strerror(errno));
  92.                 exit( errno);
  93.             }
  94.             break;
  95.         case 0: /* time out */
  96.             break;
  97.         default: /* event */
  98.             if (FD_ISSET(0, &read_fds))
  99.                 read_input(); /* from operator */
  100.             break;
  101.         }
  102.     }
  103. }
  104.  
  105. int main(int argc, char **argv) {
  106.     setbuf(stdout, 0);
  107.  
  108.     startup();
  109.     main_loop();
  110. }

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].