<refentry id="glib-Shell-related-Utilities">
<refmeta>
<refentrytitle>Shell-related Utilities</refentrytitle>
<manvolnum>3</manvolnum>
<refmiscinfo>GLIB Library</refmiscinfo>
</refmeta>

<refnamediv>
<refname>Shell-related Utilities</refname><refpurpose>shell-like commandline handling.</refpurpose>
</refnamediv>

<refsynopsisdiv><title>Synopsis</title>
<synopsis>

#include &lt;glib.h&gt;


enum        <link linkend="GShellError">GShellError</link>;
#define     <link linkend="G-SHELL-ERROR-CAPS">G_SHELL_ERROR</link>
<link linkend="gboolean">gboolean</link>    <link linkend="g-shell-parse-argv">g_shell_parse_argv</link>              (const <link linkend="gchar">gchar</link> *command_line,
                                             <link linkend="gint">gint</link> *argcp,
                                             <link linkend="gchar">gchar</link> ***argvp,
                                             <link linkend="GError">GError</link> **error);
<link linkend="gchar">gchar</link>*      <link linkend="g-shell-quote">g_shell_quote</link>                   (const <link linkend="gchar">gchar</link> *unquoted_string);
<link linkend="gchar">gchar</link>*      <link linkend="g-shell-unquote">g_shell_unquote</link>                 (const <link linkend="gchar">gchar</link> *quoted_string,
                                             <link linkend="GError">GError</link> **error);

</synopsis>
</refsynopsisdiv>





<refsect1>
<title>Description</title>
<para>

</para>
</refsect1>

<refsect1>
<title>Details</title>
<refsect2>
<title><anchor id="GShellError">enum GShellError</title>
<programlisting>typedef enum
{
  /* mismatched or otherwise mangled quoting */
  G_SHELL_ERROR_BAD_QUOTING,
  /* string to be parsed was empty */
  G_SHELL_ERROR_EMPTY_STRING,
  G_SHELL_ERROR_FAILED
} GShellError;
</programlisting>
<para>
Error codes returned by shell functions.
</para><informaltable pgwide="1" frame="none" role="enum">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row>
<entry><literal>G_SHELL_ERROR_BAD_QUOTING</literal></entry>
<entry>Mismatched or otherwise mangled quoting.
</entry>
</row>
<row>
<entry><literal>G_SHELL_ERROR_EMPTY_STRING</literal></entry>
<entry>String to be parsed was empty.
</entry>
</row>
<row>
<entry><literal>G_SHELL_ERROR_FAILED</literal></entry>
<entry>Some other error.

</entry>
</row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="G-SHELL-ERROR-CAPS">G_SHELL_ERROR</title>
<programlisting>#define G_SHELL_ERROR g_shell_error_quark ()
</programlisting>
<para>
Error domain for shell functions. Errors in this domain will
be from the <link linkend="GShellError">GShellError</link> enumeration. See <link linkend="GError">GError</link> for information on
error domains.
</para></refsect2>
<refsect2>
<title><anchor id="g-shell-parse-argv">g_shell_parse_argv ()</title>
<programlisting><link linkend="gboolean">gboolean</link>    g_shell_parse_argv              (const <link linkend="gchar">gchar</link> *command_line,
                                             <link linkend="gint">gint</link> *argcp,
                                             <link linkend="gchar">gchar</link> ***argvp,
                                             <link linkend="GError">GError</link> **error);</programlisting>
<para>
Parses a command line into an argument vector, in much the same way
the shell would, but without many of the expansions the shell would
perform (variable expansion, globs, operators, filename expansion,
etc. are not supported). The results are defined to be the same as
those you would get from a UNIX98 /bin/sh, as long as the input
contains none of the unsupported shell expansions. If the input
does contain such expansions, they are passed through
literally. Possible errors are those from the <link linkend="G-SHELL-ERROR-CAPS">G_SHELL_ERROR</link>
domain. Free the returned vector with <link linkend="g-strfreev">g_strfreev</link>().</para>
<para>

</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>command_line</parameter>&nbsp;:</entry>
<entry> command line to parse
</entry></row>
<row><entry align="right"><parameter>argcp</parameter>&nbsp;:</entry>
<entry> return location for number of args
</entry></row>
<row><entry align="right"><parameter>argvp</parameter>&nbsp;:</entry>
<entry> return location for array of args
</entry></row>
<row><entry align="right"><parameter>error</parameter>&nbsp;:</entry>
<entry> return location for error
</entry></row>
<row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry> <literal>TRUE</literal> on success, <literal>FALSE</literal> if error set
</entry></row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="g-shell-quote">g_shell_quote ()</title>
<programlisting><link linkend="gchar">gchar</link>*      g_shell_quote                   (const <link linkend="gchar">gchar</link> *unquoted_string);</programlisting>
<para>
Quotes a string so that the shell (/bin/sh) will interpret the
quoted string to mean <parameter>unquoted_string</parameter>. If you pass a filename to
the shell, for example, you should first quote it with this
function.  The return value must be freed with <link linkend="g-free">g_free</link>(). The
quoting style used is undefined (single or double quotes may be
used).</para>
<para>

</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>unquoted_string</parameter>&nbsp;:</entry>
<entry> a literal string
</entry></row>
<row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry> quoted string
</entry></row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="g-shell-unquote">g_shell_unquote ()</title>
<programlisting><link linkend="gchar">gchar</link>*      g_shell_unquote                 (const <link linkend="gchar">gchar</link> *quoted_string,
                                             <link linkend="GError">GError</link> **error);</programlisting>
<para>
Unquotes a string as the shell (/bin/sh) would. Only handles
quotes; if a string contains file globs, arithmetic operators,
variables, backticks, redirections, or other special-to-the-shell
features, the result will be different from the result a real shell
would produce (the variables, backticks, etc. will be passed
through literally instead of being expanded). This function is
guaranteed to succeed if applied to the result of
<link linkend="g-shell-quote">g_shell_quote</link>(). If it fails, it returns <literal>NULL</literal> and sets the
error. The <parameter>quoted_string</parameter> need not actually contain quoted or
escaped text; <link linkend="g-shell-unquote">g_shell_unquote</link>() simply goes through the string and
unquotes/unescapes anything that the shell would. Both single and
double quotes are handled, as are escapes including escaped
newlines. The return value must be freed with <link linkend="g-free">g_free</link>(). Possible
errors are in the <link linkend="G-SHELL-ERROR-CAPS">G_SHELL_ERROR</link> domain.
</para>
<para>
Shell quoting rules are a bit strange. Single quotes preserve the
literal string exactly. escape sequences are not allowed; not even
\' - if you want a ' in the quoted text, you have to do something
like 'foo'\''bar'.  Double quotes allow $, `, ", \, and newline to
be escaped with backslash. Otherwise double quotes preserve things
literally.</para>
<para>

</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>quoted_string</parameter>&nbsp;:</entry>
<entry> shell-quoted string
</entry></row>
<row><entry align="right"><parameter>error</parameter>&nbsp;:</entry>
<entry> error return location or NULL
</entry></row>
<row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry> an unquoted string
</entry></row>
</tbody></tgroup></informaltable></refsect2>

</refsect1>




</refentry>
