Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add macro '%-x**' containing all occurrences of the flag '-x' #2449

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/manual/macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ macros are available:
| %# | the number of arguments
| %{-f} | if present at invocation, the last occurence of flag f (flag and argument)
| %{-f*} | if present at invocation, the argument to the last occurence of flag f
| %{-f**} | if present at invocation, all occurrences of flag f (flag and argument, space separated)
| %1, %2, ...| the arguments themselves (after getopt(3) processing)

At the end of invocation of a parameterized macro, the above macros are
Expand Down
30 changes: 27 additions & 3 deletions rpmio/macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -832,12 +832,20 @@ static void doDump(MacroBuf mb, rpmMacroEntry me, ARGV_t argv, size_t *parsed)
rpmDumpMacroTable(mb->mc, NULL);
}

typedef struct {
MacroBuf mb;
char **argv;
} MacroOptData;

static int mbopt(int c, const char *oarg, int oint, void *data)
{
MacroBuf mb = data;
char *name = NULL, *body = NULL;
MacroOptData *optData = (MacroOptData *)data;
MacroBuf mb = optData->mb;
char *name = NULL, *body = NULL, *fill = " ";

if (oint > 1 && optData->argv[oint-1] && optData->argv[oint-1][0] == '-') {
fill = "";
}
/* Define option macros. */
rasprintf(&name, "-%c", c);
if (oarg) {
Expand All @@ -854,6 +862,21 @@ static int mbopt(int c, const char *oarg, int oint, void *data)
pushMacro(mb->mc, name, NULL, oarg, mb->level, ME_AUTO | ME_LITERAL);
free(name);
}

rasprintf(&name, "-%c**", c);
rpmMacroEntry *mep = findEntry(mb->mc, name, 0, NULL);
if (mep && oarg)
rasprintf(&body, "%s -%c%s%s", (*mep)->body, c, fill, oarg);
else if (mep)
rasprintf(&body, "%s -%c", (*mep)->body, c);
else if (oarg)
rasprintf(&body, "-%c%s%s", c, fill, oarg);
else
rasprintf(&body, "-%c", c);
pushMacro(mb->mc, name, NULL, body, mb->level, ME_AUTO | ME_LITERAL);
free(name);
free(body);

return 0;
}

Expand Down Expand Up @@ -890,8 +913,9 @@ setupArgs(MacroBuf mb, const rpmMacroEntry me, ARGV_t argv)
pushMacro(mb->mc, "**", NULL, args, mb->level, ME_AUTO | ME_LITERAL);
free(args);

MacroOptData data = { mb, argv };
argc = argvCount(argv);
ind = rgetopt(argc, argv, me->opts, mbopt, mb);
ind = rgetopt(argc, argv, me->opts, mbopt, &data);

if (ind < 0) {
mbErr(mb, 1, _("Unknown option %c in %s(%s)\n"), -ind,
Expand Down
12 changes: 12 additions & 0 deletions tests/rpmmacro.at
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,18 @@ runroot rpm \
])
AT_CLEANUP

AT_SETUP([parametrized macro 8])
AT_KEYWORDS([macros])
AT_CHECK([
runroot rpm \
--define '%foo(b:c) %{-b**}' \
--eval '%foo 5 a -b2 -bd=1 -c -b e=2'
],
[0],
[-b2 -bd=1 -b e=2
])
AT_CLEANUP

AT_SETUP([uncompress macro 1])
AT_KEYWORDS([macros])
AT_CHECK([
Expand Down