Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Richard Earnshaw <rearnsha@arm.com>
To: binutils@sourceware.org, gdb-patches@sourceware.org
Cc: Richard.Earnshaw@arm.com, Sunil Dora <sunilkumar.dora@windriver.com>
Subject: [committed] Port expandargstr from gcc/libiberty
Date: Mon, 15 Jun 2026 16:57:31 +0100	[thread overview]
Message-ID: <20260615155731.2965355-1-rearnsha@arm.com> (raw)

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


                 reply	other threads:[~2026-06-15 15:59 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260615155731.2965355-1-rearnsha@arm.com \
    --to=rearnsha@arm.com \
    --cc=Richard.Earnshaw@arm.com \
    --cc=binutils@sourceware.org \
    --cc=gdb-patches@sourceware.org \
    --cc=sunilkumar.dora@windriver.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox