diff options
author | Otto Moerbeek <otto@cvs.openbsd.org> | 2006-05-29 18:22:25 +0000 |
---|---|---|
committer | Otto Moerbeek <otto@cvs.openbsd.org> | 2006-05-29 18:22:25 +0000 |
commit | d7495aa806c73e8d51b2f8c98a766f5e7d2258d3 (patch) | |
tree | 01e527d718802be2e2a1f2b89d988b5190b45250 /bin | |
parent | 407dcc19ca6759aa3052ee762e44d1239a87b442 (diff) |
Implement \$ and \# expansion for PS1. Whoever thought it a clever
idea to assign a special meaning to "\$" -- two chars that are already
(very) special -- deserves a spanking.
Diffstat (limited to 'bin')
-rw-r--r-- | bin/ksh/history.c | 3 | ||||
-rw-r--r-- | bin/ksh/ksh.1 | 4 | ||||
-rw-r--r-- | bin/ksh/lex.c | 34 | ||||
-rw-r--r-- | bin/ksh/lex.h | 3 |
4 files changed, 28 insertions, 16 deletions
diff --git a/bin/ksh/history.c b/bin/ksh/history.c index bcd7ee6fbf3..d7f38e152c1 100644 --- a/bin/ksh/history.c +++ b/bin/ksh/history.c @@ -1,4 +1,4 @@ -/* $OpenBSD: history.c,v 1.34 2006/03/17 16:30:13 millert Exp $ */ +/* $OpenBSD: history.c,v 1.35 2006/05/29 18:22:24 otto Exp $ */ /* * command history @@ -851,6 +851,7 @@ histload(Source *s, unsigned char *base, int bytes) } else { s->line = lno; + s->cmd_offset = lno; histsave(lno, (char *)line, 0); } state = shdr; diff --git a/bin/ksh/ksh.1 b/bin/ksh/ksh.1 index 2714ada803f..7c43779fc8d 100644 --- a/bin/ksh/ksh.1 +++ b/bin/ksh/ksh.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: ksh.1,v 1.112 2006/04/22 14:10:36 jmc Exp $ +.\" $OpenBSD: ksh.1,v 1.113 2006/05/29 18:22:24 otto Exp $ .\" .\" Public Domain .\" @@ -1634,7 +1634,6 @@ The current history number (see .Sq \e# , below). -Note: this sequence is not yet implemented. .It Li \e# The current command number. This could be different to the current history number, @@ -1647,7 +1646,6 @@ The default prompt i.e.\& if the effective UID is 0, otherwise .Sq $ \& . -Note: this sequence is not yet implemented. .It Li \e Ns Ar nnn The octal character .Ar nnn . diff --git a/bin/ksh/lex.c b/bin/ksh/lex.c index 860c6bf1365..db5392f434f 100644 --- a/bin/ksh/lex.c +++ b/bin/ksh/lex.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lex.c,v 1.39 2006/04/10 14:38:59 jaredy Exp $ */ +/* $OpenBSD: lex.c,v 1.40 2006/05/29 18:22:24 otto Exp $ */ /* * lexical analysis and source input @@ -63,6 +63,7 @@ static const char *ungetsc(int); static void gethere(void); static Lex_state *push_state_(State_info *, Lex_state *); static Lex_state *pop_state_(State_info *, Lex_state *); +static char *special_prompt_expand(char *); static int dopprompt(const char *, int, const char **, int); static int backslash_skip; @@ -909,6 +910,7 @@ pushs(int type, Area *areap) s->str = null; s->start = NULL; s->line = 0; + s->cmd_offset = 0; s->errline = 0; s->file = NULL; s->flags = 0; @@ -1120,6 +1122,18 @@ getsc_line(Source *s) set_prompt(PS2, (Source *) 0); } +static char * +special_prompt_expand(char *str) +{ + char *p = str; + + while ((p = strstr(p, "\\$")) != NULL) { + memmove(p, p + 1, strlen(p)); + *p = ksheuid ? '$' : '#'; + } + return str; +} + void set_prompt(int to, Source *s) { @@ -1141,9 +1155,11 @@ set_prompt(int to, Source *s) * unwinding its stack through this code as it * exits. */ - } else - prompt = str_save(substitute(ps1, 0), - saved_atemp); + } else { + /* expand \$ before other substitutions are done */ + char *tmp = special_prompt_expand(ps1); + prompt = str_save(substitute(tmp, 0), saved_atemp); + } quitenv(NULL); break; case PS2: /* command continuation */ @@ -1309,17 +1325,13 @@ dopprompt(const char *sp, int ntruncate, const char **spp, int doprint) p = str_val(global("PWD")); strlcpy(strbuf, basename(p), sizeof strbuf); break; - case '!': /* '\' '!' history line number XXX busted */ + case '!': /* '\' '!' history line number */ snprintf(strbuf, sizeof strbuf, "%d", source->line + 1); break; - case '#': /* '\' '#' command line number XXX busted */ + case '#': /* '\' '#' command line number */ snprintf(strbuf, sizeof strbuf, "%d", - source->line + 1); - break; - case '$': /* '\' '$' $ or # XXX busted */ - strbuf[0] = ksheuid ? '$' : '#'; - strbuf[1] = '\0'; + source->line - source->cmd_offset + 1); break; case '0': /* '\' '#' '#' ' #' octal numeric handling */ case '1': diff --git a/bin/ksh/lex.h b/bin/ksh/lex.h index 82cadf151f0..0904fbda1d9 100644 --- a/bin/ksh/lex.h +++ b/bin/ksh/lex.h @@ -1,4 +1,4 @@ -/* $OpenBSD: lex.h,v 1.10 2005/09/11 18:02:27 otto Exp $ */ +/* $OpenBSD: lex.h,v 1.11 2006/05/29 18:22:24 otto Exp $ */ /* * Source input, lexer and parser @@ -22,6 +22,7 @@ struct source { char ugbuf[2]; /* buffer for ungetsc() (SREREAD) and * alias (SALIAS) */ int line; /* line number */ + int cmd_offset; /* line number - command number */ int errline; /* line the error occurred on (0 if not set) */ const char *file; /* input file name */ int flags; /* SF_* */ |