* [committed] Port expandargstr from gcc/libiberty
@ 2026-06-15 15:57 Richard Earnshaw
0 siblings, 0 replies; only message in thread
From: Richard Earnshaw @ 2026-06-15 15:57 UTC (permalink / raw)
To: binutils, gdb-patches; +Cc: Richard.Earnshaw, Sunil Dora
From: Richard.Earnshaw@arm.com
Sync recent changes to libiberty.
include/ChangeLog:
* libiberty.h (expandargstr): Declare.
libiberty/ChangeLog:
* argv.c (expandargstr): New function.
Signed-off-by: Sunil Dora <sunilkumar.dora@windriver.com>
---
Committing as obvious.
diff --git a/include/libiberty.h b/include/libiberty.h
index ae0e94bc4c5..e905f2cf190 100644
--- a/include/libiberty.h
+++ b/include/libiberty.h
@@ -94,6 +94,11 @@ extern int writeargv (char * const *, FILE *);
extern int countargv (char * const *);
+/* Expand VAL as a response file if it begins with '@' and return the
+ result as a shell-quoted string. */
+
+extern char *expandargstr (const char *, const char *);
+
/* Return the last component of a path name. Note that we can't use a
prototype here because the parameter is declared inconsistently
across different systems, sometimes as "char *" and sometimes as
diff --git a/libiberty/argv.c b/libiberty/argv.c
index 64127f70c99..35e2750c5ff 100644
--- a/libiberty/argv.c
+++ b/libiberty/argv.c
@@ -492,6 +492,81 @@ countargv (char * const *argv)
return argc;
}
+/*
+
+@deftypefn Extension {char *} expandargstr @
+ (const char *@var{progname}, const char *@var{val})
+
+Expand @var{val} as a response file via @code{expandargv} if it begins
+with @samp{@@}, using @var{progname} as @code{argv[0]}, and return the
+expanded option list as a shell-quoted string. Returns a newly
+allocated string.
+
+@end deftypefn
+
+*/
+
+char *
+expandargstr (const char *progname, const char *val)
+{
+ int argc = 2;
+ char **argv;
+ char **orig;
+ size_t len;
+ char *buf;
+ char *p;
+ int i;
+
+ if (val[0] != '@')
+ return xstrdup (val);
+
+ argv = (char **) xcalloc (3, sizeof (char *));
+ orig = argv;
+ argv[0] = xstrdup (progname);
+ argv[1] = xstrdup (val);
+ argv[2] = NULL;
+ expandargv (&argc, &argv);
+ if (argv != orig)
+ freeargv (orig);
+
+ len = 1;
+ for (i = 1; argv[i] != NULL; i++)
+ {
+ const char *q;
+ if (i > 1)
+ len++;
+ len += 2;
+ for (q = argv[i]; *q; q++)
+ len += (*q == '\'') ? 4 : 1;
+ }
+
+ buf = (char *) xmalloc (len);
+ p = buf;
+ for (i = 1; argv[i] != NULL; i++)
+ {
+ const char *q;
+ if (i > 1)
+ *p++ = ' ';
+ *p++ = '\'';
+ for (q = argv[i]; *q; q++)
+ {
+ if (*q == '\'')
+ {
+ *p++ = '\'';
+ *p++ = '\\';
+ *p++ = '\'';
+ *p++ = '\'';
+ }
+ else
+ *p++ = *q;
+ }
+ *p++ = '\'';
+ }
+ *p = '\0';
+ freeargv (argv);
+ return buf;
+}
+
#ifdef MAIN
/* Simple little test driver. */
--
2.43.0
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-06-15 15:59 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-06-15 15:57 [committed] Port expandargstr from gcc/libiberty Richard Earnshaw
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox