home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************
- * *
- * edline.c *
- * *
- ******************************************************************************/
-
- /*-------------------------- INITIAL CODING DATE -----------------------------
- Tue Sep 12 09:19:55 EDT 1989 by George M. Bogatko
-
- -------------------------------- HEADER FILES -----------------------------*/
- #include <stdio.h>
- #include <assert.h>
-
- /*------------------ TYPEDEF'S, DEFINES, STRUCTURE DEF'S ------------------*/
- #define BUFRSIZE 81
- #define MAXSLOTS 50
-
- /*---------------- IMPORTED GLOBAL VARIABLE/FUNCTION DEF'S ----------------*/
- extern int edit();
-
- /*---------------- EXPORTED GLOBAL VARIABLE/FUNCTION DEF'S ----------------*/
-
- /*---------------- INTERNAL GLOBAL VARIABLE/FUNCTION DEF'S ----------------*/
- #ident "@(#)edline.c 2.5 9/4/90 - George M. Bogatko -"
-
- /*-----------------------------------------------------------------------------
-
- SYNOPSIS:
- char *edline(char *prompt)
-
- DESCRIPTION:
- EDLINE is the feeder function in the 'edline' command line editor.
-
- It takes the prompt passed in and passes it along to 'edit()'
-
- It also keeps a 50 line circular array of strings as a 'history'
- similar to 'ksh'.
-
- CAVEATS:
-
- =============================================================================*/
-
- char *edline(prompt)
- char *prompt;
- {
- int ret;
-
- static char (*buf)[BUFRSIZE] = (char (*)[BUFRSIZE])NULL;
- static char *tbuf = (char *)NULL;
- static int curslot = 0;
- static int hit_the_top = 0;
- static int lastslot = 0;
-
- if( buf == (char (*)[BUFRSIZE])NULL )
- assert( (buf = (char (*)[BUFRSIZE])calloc(MAXSLOTS, BUFRSIZE)) != (char (*)[BUFRSIZE])NULL );
-
- if( tbuf == (char *)NULL )
- assert( (tbuf = (char *)calloc(BUFRSIZE, sizeof(char))) != (char *)NULL );
- for(ret = 0;;)
- {
- switch(ret)
- {
- case 255:
- strcpy(tbuf, " ");
- ret = edit(tbuf,prompt);
- break;
- case 254:
- putchar('\n');
- return (char *)NULL;
- break;
- case 0:
- *tbuf = '\0';
- ret = edit(tbuf,prompt);
- break;
- case '\n':
- if( *tbuf != '\0' )
- {
- strcpy(buf[lastslot], tbuf);
- if( ++lastslot >= MAXSLOTS )
- {
- hit_the_top = 1;
- lastslot = 0;
- }
- curslot = lastslot;
- }
- putchar('\n');
- return tbuf;
- break;
- case 'j':
- if( hit_the_top )
- {
- if( ++curslot >= MAXSLOTS )
- curslot = 0;
- }
- else
- {
- if( ++curslot >= lastslot )
- curslot = 0;
- }
-
- strcpy(tbuf, buf[curslot]);
- ret = edit(tbuf,prompt);
- break;
- case 'k':
- if( --curslot < 0 )
- {
- if( hit_the_top )
- {
- curslot = MAXSLOTS-1;
- }
- else
- curslot = lastslot-1;
- }
-
- strcpy(tbuf, buf[curslot]);
- ret = edit(tbuf,prompt);
- break;
- default:
- putchar(7);
- break;
- }
- }
- }
-