home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 21 / CD_ASCQ_21_040595.iso / dos / prg / c / freedos3 / source / com020 / cmdinput.h < prev    next >
Text File  |  1995-01-15  |  7KB  |  354 lines

  1. /* CMDINPUT.H
  2.  * handles command input (tab completion, history, etc.)
  3.  * Tim Norman
  4.  * 1-14-95
  5. */
  6.  
  7. #define BS 8
  8. #define DELETE 256+83
  9. #define TAB 9
  10. #define HOME 256+71
  11. #define END 256+79
  12. #define UP 256+72
  13. #define DOWN 256+80
  14. #define LEFT 256+75
  15. #define RIGHT 256+77
  16. #define ENTER 13
  17. #define ESC 27
  18.  
  19. #define BEEP printf ("\a")
  20.  
  21. /* this is inefficient to call each time something is changed */
  22. /* make it better */
  23. void reprint (char *str, int x, int *y, int place)
  24. {
  25.    int gox, goy;
  26.  
  27.    str[127] = 0;        /* truncate in case it's too long */
  28.  
  29.    gotoxy (x, *y);
  30.  
  31.    /* figure out if it's going to scroll... */
  32.    gox = x + strlen (str);
  33.    goy = *y;
  34.  
  35.    while (gox > 80)
  36.    {
  37.       gox -= 80;
  38.       goy++;
  39.    }
  40.  
  41.    while (goy > 25)
  42.    {
  43.       goy--;
  44.       (*y)--;
  45.    }
  46.  
  47.    printf (str);
  48.    clreol ();
  49.  
  50.    gox = x + place;
  51.    goy = *y;
  52.  
  53.    while (gox > 80)
  54.    {
  55.       gox -= 80;
  56.       goy++;
  57.    }
  58.  
  59.    gotoxy (gox, goy);
  60. }
  61.  
  62. void reposition (int curx, int cury, int place)
  63. {
  64.    int gox, goy;
  65.  
  66.    gox = curx + place;
  67.    goy = cury;
  68.  
  69.    while (gox > 80)
  70.    {
  71.       gox -= 80;
  72.       goy++;
  73.    }
  74.  
  75.    gotoxy (gox, goy);
  76. }
  77.  
  78. void readcommand (char *str, int maxlen)
  79. {
  80.    struct ffblk file;
  81.    static char buffer[80][128] = {""};
  82.    static int bufstart = 0, bufend = 0;
  83.    int place = 0, len = 0, ch, bufplace = bufend;
  84.    int curx, cury, gox, goy, count, y;
  85.  
  86.    curx = wherex ();
  87.    cury = wherey ();
  88.  
  89.    str[0] = 0;
  90.  
  91.    do
  92.    {
  93.       ch = getch ();
  94.  
  95.       if (ch == 0)      /* special key */
  96.      ch = 256 + getch ();
  97.  
  98.       switch (ch)
  99.       {
  100.      case BS:
  101.         /* delete character to left of cursor */
  102.         if (place != 0)
  103.         {
  104.            place--;
  105.  
  106.            for (count = place; count < len; count++)
  107.           str[count] = str[count + 1];
  108.  
  109.            len--;
  110.  
  111.            if (wherex () != 1)
  112.           printf ("\b \b");
  113.            else
  114.            {
  115.           y = wherey ();
  116.           gotoxy (80, y - 1);
  117.           printf (" ");
  118.           gotoxy (80, y - 1);
  119.            }
  120.  
  121.            reprint (str, curx, &cury, place);
  122.         }
  123.  
  124.         break;
  125.  
  126.      case DELETE:
  127.         /* delete character under cursor */
  128.         if (place != len)
  129.         {
  130.            for (count = place; count < len; count++)
  131.           str[count] = str[count + 1];
  132.  
  133.            len--;
  134.  
  135.            if (place != 0)
  136.            if (wherex () != 1)
  137.           printf ("\b \b");
  138.            else
  139.            {
  140.           y = wherey ();
  141.           gotoxy (80, y - 1);
  142.           printf (" ");
  143.           gotoxy (80, y - 1);
  144.            }
  145.  
  146.            reprint (str, curx, &cury, place);
  147.         }
  148.  
  149.         break;
  150.  
  151.      case HOME:
  152.         /* goto beginning of string */
  153.         gotoxy (curx, cury);
  154.  
  155.         place = 0;
  156.  
  157.         break;
  158.  
  159.      case END:
  160.         /* goto end of string */
  161.         place = len;
  162.  
  163.         reposition (curx, cury, place);
  164.  
  165.         break;
  166.  
  167.      case TAB:
  168.         /* expand current file name */
  169.         if (place == len)  /* only works at end of line */
  170.         {
  171.            char path[128], fname[14], maxmatch[13] = "", directory[128];
  172.            int found_dot = 0, curplace = 0, start, perfectmatch = 1;
  173.  
  174.            count = len;
  175.  
  176.            while (str[count] != ' ' && count > 0) /* find front of word */
  177.           count--;
  178.  
  179.            if (str[count] == ' ')  /* if not at beginning, go forward 1 */
  180.           count++;
  181.  
  182.            start = count;
  183.  
  184.            /* extract directory from word */
  185.            strcpy (directory, &str[start]);
  186.            curplace = strlen (directory) - 1;
  187.            while (curplace >= 0 && directory[curplace] != '\\' &&
  188.               directory[curplace] != ':')
  189.            {
  190.           directory[curplace] = 0;
  191.           curplace--;
  192.            }
  193.  
  194.            strcpy (path, &str[start]);
  195.  
  196.            for (count = 0; path[count] != 0; count++)
  197.           if (path[count] == '.')
  198.           {
  199.              found_dot = 1;
  200.              break;
  201.           }
  202.  
  203.            if (found_dot)
  204.           strcat (path, "*");
  205.            else
  206.           strcat (path, "*.*");
  207.  
  208.            curplace = 0;    /* current fname */
  209.            if (findfirst (path, &file, 0x3F) == 0)  /* find anything */
  210.            {
  211.           do
  212.           {
  213.              if (file.ff_name[0] == '.')  /* ignore . and .. */
  214.             continue;
  215.  
  216.              strcpy (fname, file.ff_name);
  217.              if (file.ff_attrib == FA_DIREC)
  218.             strcat (fname, "\\");
  219.              else
  220.             strcat (fname, " ");
  221.  
  222.              if (!maxmatch[0] && perfectmatch)
  223.             strcpy (maxmatch, fname);
  224.              else
  225.              {
  226.             for (count = 0; maxmatch[count] && fname[count];
  227.                  count++)
  228.                if (maxmatch[count] != fname[count])
  229.                {
  230.                   perfectmatch = 0;
  231.                   maxmatch[count] = 0;
  232.                   break;
  233.                }
  234.              }
  235.           }
  236.           while (findnext (&file) == 0);
  237.  
  238.           strcpy (&str[start], directory);
  239.           strcat (&str[start], maxmatch);
  240.           len = strlen (str);
  241.           place = len;
  242.  
  243.           reprint (str, curx, &cury, place);
  244.  
  245.           if (!perfectmatch)
  246.              BEEP;
  247.            }
  248.            else
  249.           BEEP;
  250.         }
  251.         else
  252.            BEEP;
  253.  
  254.         break;
  255.  
  256.      case ENTER:
  257.         /* end input, return to main */
  258.         if (str[0])
  259.            history (0, str);  /* add to the history */
  260.  
  261.         place = len;
  262.         reprint (str, curx, &cury, place);
  263.  
  264.         printf ("\n\n");
  265.  
  266.         break;
  267.  
  268.      case ESC:
  269.         /* clear str */
  270.         gotoxy (curx, cury);
  271.  
  272.         for (count = 0; count < strlen (str); count++)
  273.            printf (" ");
  274.  
  275.         place = len = str[0] = 0;
  276.  
  277.         gotoxy (curx, cury);
  278.  
  279.         break;
  280.  
  281.      case UP:
  282.         /* get previous command from buffer */
  283.         gotoxy (curx, cury);
  284.  
  285.         for (count = 0; count < strlen (str); count++)
  286.            printf (" ");
  287.  
  288.         history (-1, str);
  289.  
  290.         len = strlen (str);
  291.  
  292.         place = len;
  293.  
  294.         reprint (str, curx, &cury, place);
  295.  
  296.         break;
  297.  
  298.      case DOWN:
  299.         /* get next command from buffer */
  300.         gotoxy (curx, cury);
  301.  
  302.         for (count = 0; count < strlen (str); count++)
  303.            printf (" ");
  304.  
  305.         history (1, str);
  306.  
  307.         len = strlen (str);
  308.  
  309.         place = len;
  310.  
  311.         reprint (str, curx, &cury, place);
  312.  
  313.         break;
  314.  
  315.      case LEFT:
  316.         /* move cursor left */
  317.         if (place != 0)
  318.            place--;
  319.         else
  320.            BEEP;
  321.  
  322.         reposition (curx, cury, place);
  323.  
  324.         break;
  325.  
  326.      case RIGHT:
  327.         /* move cursor right */
  328.         if (place != len)
  329.            place++;
  330.  
  331.         reposition (curx, cury, place);
  332.  
  333.         break;
  334.  
  335.      default:
  336.         if (ch >= 32 && ch <= 255 && len != maxlen - 2)
  337.         {
  338.            /* insert character into string... */
  339.            for (count = len + 1; count > place; count--)
  340.           str[count] = str[count - 1];
  341.  
  342.            str[place] = ch;
  343.            place++;
  344.            len++;
  345.  
  346.            reprint (str, curx, &cury, place);
  347.         }
  348.         else
  349.            BEEP;
  350.       }
  351.    }
  352.    while (ch != ENTER);
  353. }
  354.