Skip to content

Commit

Permalink
Add TT_ASSIGN
Browse files Browse the repository at this point in the history
  • Loading branch information
magicant committed Jan 4, 2025
1 parent bf803fd commit 14c911b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
28 changes: 18 additions & 10 deletions expand.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static inline void fill_ccbuf(
__attribute__((nonnull));

static wchar_t *expand_tilde(const wchar_t **ss,
bool hasnextwordunit, tildetype_T tt)
bool hasnextwordunit, bool stopatcolon)
__attribute__((nonnull,malloc,warn_unused_result));

enum indextype_T { IDX_NONE, IDX_ALL, IDX_CONCAT, IDX_NUMBER, };
Expand Down Expand Up @@ -432,8 +432,8 @@ struct expand_four_T expand_four(const wordunit_T *restrict w,
switch (w->wu_type) {
case WT_STRING:
ss = w->wu_string;
if (first && tilde != TT_NONE) {
s = expand_tilde(&ss, w->next, tilde);
if (first && (tilde == TT_SINGLE || tilde == TT_MULTI)) {
s = expand_tilde(&ss, w->next != NULL, tilde == TT_MULTI);
if (s != NULL) {
wb_catfree(&valuebuf, s);
fill_ccbuf(&valuebuf, &ccbuf,
Expand Down Expand Up @@ -527,15 +527,22 @@ struct expand_four_T expand_four(const wordunit_T *restrict w,
case L':':
if (indq || tilde != TT_MULTI)
goto default_;

/* perform tilde expansion after a colon */
wb_wccat(&valuebuf, L':');
/* perform tilde expansion after the colon */
goto tilde;
case L'=':
if (indq || tilde != TT_ASSIGN)
goto default_;
/* perform tilde expansion after the equal */
tilde:
tilde = TT_MULTI;
wb_wccat(&valuebuf, *ss);
sb_ccat(&ccbuf, defaultcc);
ss++;
s = expand_tilde(&ss, w->next, tilde);
s = expand_tilde(&ss, w->next != NULL, true);
if (s != NULL) {
wb_catfree(&valuebuf, s);
fill_ccbuf(&valuebuf, &ccbuf, CC_HARD_EXPANSION);
fill_ccbuf(&valuebuf, &ccbuf,
CC_HARD_EXPANSION | (defaultcc & CC_QUOTED));
}
continue;
default_:
Expand Down Expand Up @@ -614,14 +621,15 @@ void fill_ccbuf(const xwcsbuf_T *restrict valuebuf, xstrbuf_T *restrict ccbuf,
* If `**ss' is not L'~' or expansion fails, this function has no side effects
* and returns NULL. If successful, `*ss' is incremented and the result is
* returned as a newly malloced string. */
wchar_t *expand_tilde(const wchar_t **ss, bool hasnextwordunit, tildetype_T tt)
wchar_t *expand_tilde(
const wchar_t **ss, bool hasnextwordunit, bool stopatcolon)
{
const wchar_t *s = *ss;
if (*s != L'~')
return NULL;
s++;

const wchar_t *end = wcspbrk(s, tt == TT_SINGLE ? L"/" : L"/:");
const wchar_t *end = wcspbrk(s, stopatcolon ? L"/:" : L"/");
wchar_t *username;
const wchar_t *home;
size_t usernamelen;
Expand Down
9 changes: 7 additions & 2 deletions expand.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Yash: yet another shell */
/* expand.h: word expansion */
/* (C) 2007-2020 magicant */
/* (C) 2007-2025 magicant */

/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -28,7 +28,12 @@
#define CHARS_ESCAPABLE L"$`\"\\"

/* type of tilde expansion */
typedef enum { TT_NONE, TT_SINGLE, TT_MULTI, } tildetype_T;
typedef enum {
TT_NONE, /* No tilde expansion */
TT_SINGLE, /* Tilde expansion only at the beginning of a word */
TT_MULTI, /* TT_SINGLE plus tilde expansion after each colon in a word */
TT_ASSIGN, /* Like TT_MULTI, but only after an unquoted '=' */
} tildetype_T;

/* treatment of quotation marks during expansion */
typedef enum {
Expand Down

0 comments on commit 14c911b

Please sign in to comment.