Skip to content

Commit

Permalink
Add macro '%-x**' containing all occurrences of the flag '-x<arg>' or…
Browse files Browse the repository at this point in the history
… '-x <arg>'.

Flags are added to this macro with the flag and argument
in the original notation and separated from previous by spaces.

Fix rpm-software-management#546
  • Loading branch information
rhabacker committed Mar 24, 2023
1 parent 909e0ae commit d71cec2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
3 changes: 2 additions & 1 deletion docs/manual/macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ macros are available:
%** all arguments (including any processed flags)
%# the number of arguments
%{-f} if present at invocation, the flag f itself
%{-f*} if present at invocation, the argument to flag f
%{-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) (rpm >= 4.18)
%1, %2 the arguments themselves (after getopt(3) processing)
```

Expand Down
28 changes: 26 additions & 2 deletions rpmio/macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -829,10 +829,15 @@ 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;
MacroOptData *optData = (MacroOptData *)data;
MacroBuf mb = optData->mb;
char *name = NULL, *body = NULL;

/* Define option macros. */
Expand All @@ -851,6 +856,22 @@ 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);
}

char *fill = strlen(optData->argv[oint-1]) > 2 ? "" : " ";
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 @@ -887,8 +908,11 @@ setupArgs(MacroBuf mb, const rpmMacroEntry me, ARGV_t argv)
pushMacro(mb->mc, "**", NULL, args, mb->level, ME_AUTO | ME_LITERAL);
free(args);

MacroOptData data;
data.mb = mb;
data.argv = 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

0 comments on commit d71cec2

Please sign in to comment.