home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 21 / CD_ASCQ_21_040595.iso / dos / prg / c / freedos3 / source / com020 / history.h < prev    next >
C/C++ Source or Header  |  1995-01-15  |  3KB  |  140 lines

  1. /* HISTORY.H
  2.  * command line history stuff
  3.  * Tim Norman
  4.  * 1-14-95
  5. */
  6.  
  7. #define MAXLINES 128
  8.  
  9. #include <stdio.h>
  10. #include <alloc.h>
  11.  
  12. /*
  13. long history_size = 80;
  14.  
  15. long history_used = 0;
  16. */
  17.  
  18. void history (int dir, char *commandline)
  19. {
  20.    static char *history = NULL, *lines[MAXLINES];
  21.    static long pos = 0, curline = 0, numlines = 0, maxpos = 0;
  22.    int count, length;
  23.  
  24.    if (!history)
  25.    {
  26.       history = malloc (history_size * sizeof (char));
  27.  
  28.       lines[0] = history;
  29.    }
  30.  
  31.    if (dir > 0)         /* next command */
  32.    {
  33.       if (curline < numlines)
  34.      curline++;
  35.  
  36.       if (curline == numlines)
  37.      commandline[0] = 0;
  38.       else
  39.      strcpy (commandline, lines[curline]);
  40.    }
  41.    else if (dir < 0)    /* prev command */
  42.    {
  43.       if (curline > 0)
  44.      curline--;
  45.  
  46.       strcpy (commandline, lines[curline]);
  47.    }
  48.    else                 /* add to history */
  49.    {
  50.       /* remove oldest string until there's enough room for next one */
  51.       /* strlen (commandline) must be less than history_size!!!!! */
  52.       while (maxpos + strlen (commandline) + 1 > history_size ||
  53.          numlines >= MAXLINES)
  54.       {
  55.      length = strlen (lines[0]) + 1;
  56.  
  57.      for (count = 0; count < maxpos &&
  58.               count + (lines[1] - lines[0]) < history_size; count++)
  59.         history[count] = history[count + length];
  60.  
  61.      maxpos -= length;
  62.  
  63.      for (count = 0; count <= numlines && count < MAXLINES; count++)
  64.         lines[count] = lines[count + 1] - length;
  65.  
  66.      numlines--;
  67.  
  68.      printf ("Reduced size:  %ld lines\n", numlines);
  69.  
  70.      for (count = 0; count < numlines; count++)
  71.         printf ("%d: %s\n", count, lines[count]);
  72.       }
  73.  
  74.       strcpy (lines[numlines], commandline);
  75.  
  76.       numlines++;
  77.  
  78.       lines[numlines] = lines[numlines - 1] + strlen (commandline) + 1;
  79.  
  80.       maxpos += strlen (commandline) + 1;
  81.  
  82.       curline = numlines;  /* last line, empty */
  83.    }
  84.  
  85. /*
  86.    history_used = maxpos;
  87. */
  88.  
  89. /*
  90.    for (count = 0; count < numlines; count++)
  91.       printf ("%d:  %s\n", count, lines[count]);
  92. */
  93. }
  94.  
  95. /*      TEST main()
  96. void main ()
  97. {
  98.    int ch;
  99.    char s[128];
  100.  
  101.    do
  102.    {
  103.       printf (" 1.  Add string.\n");
  104.       printf (" 2.  Back up string.\n");
  105.       printf (" 3.  Go forward string.\n");
  106.       printf (" 4.  Quit.\n");
  107.  
  108.       ch = getche ();
  109.       puts ("");
  110.  
  111.       switch (ch)
  112.       {
  113.      case '1':
  114.         printf ("Enter string: ");
  115.         gets (s);
  116.         history (0, s);
  117.         break;
  118.      case '2':
  119.         printf ("Prev String: ");
  120.         history (-1, s);
  121.         puts (s);
  122.         break;
  123.      case '3':
  124.         printf ("Next String: ");
  125.         history (1, s);
  126.         puts (s);
  127.         break;
  128.      case '4':
  129.         puts ("Quitting");
  130.         break;
  131.      default:
  132.         puts ("Invalid choice!");
  133.       }
  134.  
  135.       printf ("%ld bytes used in buffer.\n", used);
  136.    }
  137.    while (ch != '4');
  138. }
  139. */
  140.