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

Don't let macro '%-x' add extra space between flag and argument #2455

Closed
Closed
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 @@ expanded, the following shell-like 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{-f**} | if present at invocation, all occurrences of flag f (flag and argument, space separated)
| %1, %2, ...| the arguments themselves (after getopt(3) processing)

With rpm >= 4.17 and disabled option processing the mentioned macros are defined as:
Expand Down
32 changes: 28 additions & 4 deletions rpmio/macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -832,16 +832,24 @@ 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) {
rasprintf(&body, "-%c %s", c, oarg);
rasprintf(&body, "-%c%s%s", c, fill, oarg);
} else {
rasprintf(&body, "-%c", c);
}
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
30 changes: 30 additions & 0 deletions tests/rpmmacro.at
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,36 @@ runroot rpm \
])
RPMTEST_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([parametrized macro 9])
AT_KEYWORDS([macros])
AT_CHECK([
runroot rpm \
--define '%foo(b:) %{-b}' \
--eval '%foo -bb' \
--eval '%foo -bb=1' \
--eval '%foo -b b' \
--eval '%foo -b b=1' \
],
[0],
[-bb
-bb=1
-b b
-b b=1
])
AT_CLEANUP

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