home *** CD-ROM | disk | FTP | other *** search
/ ftp.whtech.com / ftp.whtech.com.7z / ftp.whtech.com / emulators / v9t9 / linux / sources / V9t9 / source / command_rl.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-10-19  |  2.3 KB  |  129 lines

  1.  
  2. /*
  3.  *    Interface to GNU readline for command parser.
  4.  *
  5.  */
  6.  
  7. #include "v9t9_common.h"
  8.  
  9. #if HAVE_READLINE
  10.  
  11. #ifdef __unix__
  12. #include <readline/readline.h>
  13. #else
  14. #include <readline.h>
  15. #endif
  16. #include <signal.h>
  17. #include "command.h"
  18. #include "command_rl.h"
  19.  
  20. static command_symbol **command_matches;
  21. static int  command_nmatches;
  22. static int  command_match_idx;
  23. static char *command_match_idx_ptr;
  24.  
  25. static char *
  26. commands_matching(char *text, int state)
  27. {
  28.     int         ln;
  29.     char       *ret;
  30.  
  31.     if (!state) {
  32.         if (command_matches) {
  33.             xfree(command_matches);
  34.             command_matches = NULL;
  35.             command_nmatches = 0;
  36.         }
  37.         command_match_symbols(universe, text,
  38.                               &command_matches, &command_nmatches);
  39.         command_match_idx = 0;
  40.         command_match_idx_ptr = 0;
  41.     }
  42.  
  43.     if (command_match_idx >= command_nmatches)
  44.         return NULL;
  45.  
  46.     if (!command_match_idx_ptr || !*command_match_idx_ptr)
  47.         command_match_idx_ptr = command_matches[command_match_idx]->name;
  48.  
  49.     ln = 0;
  50.     while (command_match_idx_ptr[ln]
  51.            && command_match_idx_ptr[ln] != '|')
  52.         ln++;
  53.  
  54.     ret = xstrdup(command_match_idx_ptr);
  55.     ret[ln] = 0;
  56.  
  57.     command_match_idx_ptr += ln;
  58.     if (*command_match_idx_ptr == '|')
  59.         command_match_idx_ptr++;
  60.  
  61.     if (!*command_match_idx_ptr)
  62.         command_match_idx++;
  63.  
  64.     return ret;
  65. }
  66.  
  67. static char **
  68. readline_completion(char *text, int start, int end)
  69. {
  70.     char      **matches = NULL;
  71.     int         nwc = 1;        // new command?
  72.     int         ptr;
  73.  
  74.     ptr = -start;
  75.     while (ptr < 0) {
  76.         if (text[ptr] == '\"')
  77.             nwc = !nwc;
  78.         ptr++;
  79.     }
  80.  
  81.     if (nwc && *text)
  82.         matches = completion_matches(text, commands_matching);
  83.  
  84.     return matches;
  85. }
  86.  
  87. void
  88. readline_getcommands(FILE * in, FILE * out)
  89. {
  90.     while ((stateflag & ST_INTERACTIVE) && !feof(in)) {
  91.         char       *buf;
  92.  
  93.         rl_instream = in;
  94.         rl_outstream = out;
  95.         rl_readline_name = "v9t9";
  96.         rl_basic_word_break_characters = "\t\n\"\\'`@$<>=;|&{(,";
  97.         rl_attempted_completion_function = readline_completion;
  98.  
  99.         buf = readline("Enter a command> ");
  100.         if (buf) {
  101.             if (*buf)
  102.                 add_history(buf);
  103.             command_parse_text(buf);
  104.             free(buf);
  105.         } else
  106.             fprintf(out, "<EOF>\n");
  107.     }
  108. }
  109.  
  110. #else // !HAVE_READLINE
  111.  
  112. void
  113. readline_getcommands(FILE * in, FILE * out)
  114. {
  115.     while ((stateflag & ST_INTERACTIVE) && !feof(in)) {
  116.         char       buf[1024];
  117.  
  118.         fprintf(out, "Enter a command> ");
  119.         fgets(buf, sizeof(buf), in);
  120.         if (*buf)
  121.             command_parse_text(buf);
  122.         else
  123.             command_parse_text("Die\n");
  124.     }
  125. }
  126.  
  127.  
  128. #endif
  129.