home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1780 / edline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  2.9 KB  |  124 lines

  1. /******************************************************************************
  2. *                                                                             *
  3. *                                  edline.c                                   *
  4. *                                                                             *
  5. ******************************************************************************/
  6.  
  7. /*--------------------------  INITIAL CODING DATE -----------------------------
  8. Tue Sep 12 09:19:55 EDT 1989 by George M. Bogatko
  9.  
  10. --------------------------------  HEADER FILES  -----------------------------*/
  11. #include <stdio.h>
  12. #include <assert.h>
  13.  
  14. /*------------------  TYPEDEF'S, DEFINES, STRUCTURE DEF'S  ------------------*/
  15. #define BUFRSIZE 81
  16. #define MAXSLOTS 50
  17.  
  18. /*----------------  IMPORTED GLOBAL VARIABLE/FUNCTION DEF'S  ----------------*/
  19. extern int edit();
  20.  
  21. /*----------------  EXPORTED GLOBAL VARIABLE/FUNCTION DEF'S  ----------------*/
  22.  
  23. /*----------------  INTERNAL GLOBAL VARIABLE/FUNCTION DEF'S  ----------------*/
  24. #ident "@(#)edline.c    2.5 9/4/90 - George M. Bogatko -"
  25.  
  26. /*-----------------------------------------------------------------------------
  27.  
  28. SYNOPSIS:
  29.     char *edline(char *prompt)
  30.  
  31. DESCRIPTION:
  32.     EDLINE is the feeder function in the 'edline' command line editor.
  33.  
  34.     It takes the prompt passed in and passes it along to 'edit()'
  35.  
  36.     It also keeps a 50 line circular array of strings as a 'history'
  37.     similar to 'ksh'.
  38.  
  39. CAVEATS:
  40.  
  41. =============================================================================*/
  42.  
  43. char *edline(prompt)
  44. char *prompt;
  45. {
  46. int ret;
  47.  
  48. static char (*buf)[BUFRSIZE] = (char (*)[BUFRSIZE])NULL;
  49. static char *tbuf = (char *)NULL;
  50. static int curslot = 0;
  51. static int hit_the_top = 0;
  52. static int lastslot = 0;
  53.  
  54.     if( buf == (char (*)[BUFRSIZE])NULL )
  55.         assert( (buf = (char (*)[BUFRSIZE])calloc(MAXSLOTS, BUFRSIZE)) != (char (*)[BUFRSIZE])NULL );
  56.  
  57.     if( tbuf == (char *)NULL )
  58.         assert( (tbuf = (char *)calloc(BUFRSIZE, sizeof(char))) != (char *)NULL );
  59.     for(ret = 0;;)
  60.     {
  61.         switch(ret)
  62.         {
  63.         case 255:
  64.             strcpy(tbuf, " ");
  65.             ret = edit(tbuf,prompt);
  66.             break;
  67.         case 254:
  68.             putchar('\n');
  69.             return (char *)NULL;
  70.             break;
  71.         case 0:
  72.             *tbuf = '\0';
  73.             ret = edit(tbuf,prompt);
  74.             break;
  75.         case '\n':
  76.             if( *tbuf != '\0' )
  77.             {
  78.                 strcpy(buf[lastslot], tbuf);
  79.                 if( ++lastslot >= MAXSLOTS )
  80.                 {
  81.                     hit_the_top = 1;
  82.                     lastslot = 0;
  83.                 }
  84.                 curslot = lastslot;
  85.             }
  86.             putchar('\n');
  87.             return tbuf;
  88.             break;
  89.         case 'j':
  90.             if( hit_the_top )
  91.             {
  92.                 if( ++curslot >= MAXSLOTS )
  93.                     curslot = 0;
  94.             }
  95.             else
  96.             {
  97.                 if( ++curslot >= lastslot )
  98.                     curslot = 0;
  99.             }
  100.  
  101.             strcpy(tbuf, buf[curslot]);
  102.             ret = edit(tbuf,prompt);
  103.             break;
  104.         case 'k':
  105.             if( --curslot < 0 )
  106.             {
  107.                 if( hit_the_top )
  108.                 {
  109.                     curslot = MAXSLOTS-1;
  110.                 }
  111.                 else
  112.                     curslot = lastslot-1;
  113.             }
  114.  
  115.             strcpy(tbuf, buf[curslot]);
  116.             ret = edit(tbuf,prompt);
  117.             break;
  118.         default:
  119.             putchar(7);
  120.             break;
  121.         }
  122.     }
  123. }
  124.