diff --git a/src/frontend/control.c b/src/frontend/control.c index cb546dd1d..073693045 100644 --- a/src/frontend/control.c +++ b/src/frontend/control.c @@ -533,6 +533,51 @@ doblock(struct control *bl, int *num) return (NORMAL_STR); } +/* Maxiumum number of cheverons used for the alternative prompt */ +#define MAX_CHEVRONS 16 + +/* Get the alternate prompt. + Number of chevrons indicates stack depth. + Returns NULL when there is no alternate prompt. + SJB 28th April 2005 */ +char * +get_alt_prompt(void) +{ + int i = 0, j; + static char buf[MAX_CHEVRONS + 2]; /* includes terminating space & null */ + struct control *c; + + /* if nothing on the command stack return NULL */ + if (cend[stackp] <= 0) + return NULL; + + /* measure stack depth */ + for (c = cend[stackp]->co_parent; c; c = c->co_parent) + i++; + + if (i <= 0) + return NULL; + + /* Avoid overflow of buffer and + indicate when we've limited the chevrons by starting with a '+' */ + if(i > MAX_CHEVRONS) { + i = MAX_CHEVRONS; + buf[0]='+'; + } else { + buf[0]='>'; + } + + /* return one chevron per command stack depth */ + for (j = 1; j < i; j++) + buf[j] = '>'; + + /* Add space and terminate */ + buf[j] = ' '; + buf[j + 1] = '\0'; + + return buf; +} + /* Get a command. This does all the bookkeeping things like turning * command completion on and off... */ @@ -540,27 +585,15 @@ static wordlist * getcommand(char *string) { wordlist *wlist; - int i = 0, j; - static char buf[64]; - struct control *c; if (cp_debug) - fprintf(cp_err, "calling getcommand %s\n", - string ? string : ""); - if (cend[stackp]) { - for (c = cend[stackp]->co_parent; c; c = c->co_parent) - i++; - if (i) { - for (j = 0; j < i; j++) - buf[j] = '>'; - buf[j] = ' '; - buf[j + 1] = '\0'; - cp_altprompt = buf; - } else - cp_altprompt = NULL; - } else - cp_altprompt = NULL; + fprintf(cp_err, "calling getcommand %s\n", string ? string : ""); +#ifndef HAVE_GNUREADLINE + /* set cp_altprompt for use by the lexer - see parser/lexical.c */ + cp_altprompt = get_alt_prompt(); +#endif + cp_cwait = TRUE; wlist = cp_parse(string); cp_cwait = FALSE; diff --git a/src/include/cpextern.h b/src/include/cpextern.h index 975c2d4b7..35a77e4d0 100644 --- a/src/include/cpextern.h +++ b/src/include/cpextern.h @@ -66,18 +66,22 @@ extern void cp_ioreset(); extern wordlist *cp_redirect(); extern wordlist *cp_parse(); -/* front.c */ +/* control.c */ extern bool cp_cwait; extern bool cp_dounixcom; extern char *cp_csep; +extern char * get_alt_prompt(void); extern int cp_evloop(char *string); -extern void com_cdump(wordlist *wl); extern void cp_resetcontrol(void); extern void cp_toplevel(void); extern void cp_popcontrol(void); extern void cp_pushcontrol(void); +/* com_cdump.c */ + +extern void com_cdump(wordlist *wl); + /* glob.c */ extern bool cp_globmatch(char *p, char *s);