Skip to content

Commit

Permalink
Reorder expansion functions
Browse files Browse the repository at this point in the history
The expand_word function should immediately follow the expand_multiple
function since these two have similar behaviors. This reordering also
makes the expand_single_cc, expand_single, and expand_single_with_glob
functions adjacent.
  • Loading branch information
magicant committed Jan 4, 2025
1 parent 291a87b commit bf803fd
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions expand.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Yash: yet another shell */
/* expand.c: word expansion */
/* (C) 2007-2024 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 @@ -187,9 +187,10 @@ bool expand_line(void *const *restrict args,
return true;
}

/* Expands a word.
* The results, which are added to `list' as newly-malloced wide strings, may
* be multiple words.
/* Expands a word to (possibly any number of) fields.
* The operation includes the four expansions, brace expansion, field splitting,
* pathname expansion, and quote removal.
* The results are added to `list' as newly-malloced wide strings.
* The return value is true iff successful.
* On error in a non-interactive shell, the shell exits. */
bool expand_multiple(const wordunit_T *w, plist_T *list)
Expand Down Expand Up @@ -228,6 +229,29 @@ bool expand_multiple(const wordunit_T *w, plist_T *list)
return true;
}

/* Expands a word to (possibly any number of) fields.
* The operation includes the four expansions and quote removal. Unlike
* `expand_multiple', this function does not perform brace expansion, field
* splitting, or pathname expansion.
* If successful, the return value is a plist_T containing newly malloced wide
* strings. In most cases, the plist_T contains one string. If the word contains
* "$@", however, it may contain any number of strings.
* On error, the return value is a plist_T with `contents' being NULL. */
plist_T expand_word(const wordunit_T *w)
{
/* four expansions */
struct expand_four_T expand = expand_four(w, TT_NONE, Q_WORD, CC_LITERAL);

/* quote removal */
for (size_t i = 0; i < expand.valuelist.length; i++)
expand.valuelist.contents[i] = quote_removal_free(
expand.valuelist.contents[i], expand.cclist.contents[i], ES_NONE);

pl_destroy(&expand.cclist);

return expand.valuelist;
}

/* Expands a word to a single field.
* If successful, the result is a pair of newly malloced strings.
* On error, an error message is printed and a NULL pair is returned.
Expand Down Expand Up @@ -276,26 +300,6 @@ struct cc_word_T expand_single_cc(
return (struct cc_word_T) { wb_towcs(&valuebuf), sb_tostr(&ccbuf) };
}

/* Expands a word to (possibly any number of) fields.
* If successful, the return value is a plist_T containing newly malloced wide
* strings. In most cases, the plist_T contains one string. If the word contains
* "$@", however, it may contain any number of strings.
* On error, the return value is a plist_T with `contents' being NULL. */
plist_T expand_word(const wordunit_T *w)
{
/* four expansions */
struct expand_four_T expand = expand_four(w, TT_NONE, Q_WORD, CC_LITERAL);

/* quote removal */
for (size_t i = 0; i < expand.valuelist.length; i++)
expand.valuelist.contents[i] = quote_removal_free(
expand.valuelist.contents[i], expand.cclist.contents[i], ES_NONE);

pl_destroy(&expand.cclist);

return expand.valuelist;
}

/* Expands a single word: the four expansions and quote removal.
* This function doesn't perform brace expansion, field splitting, or globbing.
* If successful, the resulting word is returned as a newly malloced string.
Expand Down

0 comments on commit bf803fd

Please sign in to comment.