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

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