1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module glib.ShellUtils; 26 27 private import glib.ErrorG; 28 private import glib.GException; 29 private import glib.Str; 30 private import glib.c.functions; 31 public import glib.c.types; 32 33 34 /** */ 35 public struct ShellUtils 36 { 37 38 /** */ 39 public static GQuark shellErrorQuark() 40 { 41 return g_shell_error_quark(); 42 } 43 44 /** 45 * Parses a command line into an argument vector, in much the same way 46 * the shell would, but without many of the expansions the shell would 47 * perform (variable expansion, globs, operators, filename expansion, 48 * etc. are not supported). The results are defined to be the same as 49 * those you would get from a UNIX98 /bin/sh, as long as the input 50 * contains none of the unsupported shell expansions. If the input 51 * does contain such expansions, they are passed through 52 * literally. Possible errors are those from the #G_SHELL_ERROR 53 * domain. Free the returned vector with g_strfreev(). 54 * 55 * Params: 56 * commandLine = command line to parse 57 * argvp = return location for array of args 58 * 59 * Returns: %TRUE on success, %FALSE if error set 60 * 61 * Throws: GException on failure. 62 */ 63 public static bool shellParseArgv(string commandLine, out string[] argvp) 64 { 65 int argcp; 66 char** outargvp = null; 67 GError* err = null; 68 69 auto __p = g_shell_parse_argv(Str.toStringz(commandLine), &argcp, &outargvp, &err) != 0; 70 71 if (err !is null) 72 { 73 throw new GException( new ErrorG(err) ); 74 } 75 76 argvp = Str.toStringArray(outargvp, argcp); 77 78 return __p; 79 } 80 81 /** 82 * Quotes a string so that the shell (/bin/sh) will interpret the 83 * quoted string to mean @unquoted_string. If you pass a filename to 84 * the shell, for example, you should first quote it with this 85 * function. The return value must be freed with g_free(). The 86 * quoting style used is undefined (single or double quotes may be 87 * used). 88 * 89 * Params: 90 * unquotedString = a literal string 91 * 92 * Returns: quoted string 93 */ 94 public static string shellQuote(string unquotedString) 95 { 96 auto retStr = g_shell_quote(Str.toStringz(unquotedString)); 97 98 scope(exit) Str.freeString(retStr); 99 return Str.toString(retStr); 100 } 101 102 /** 103 * Unquotes a string as the shell (/bin/sh) would. Only handles 104 * quotes; if a string contains file globs, arithmetic operators, 105 * variables, backticks, redirections, or other special-to-the-shell 106 * features, the result will be different from the result a real shell 107 * would produce (the variables, backticks, etc. will be passed 108 * through literally instead of being expanded). This function is 109 * guaranteed to succeed if applied to the result of 110 * g_shell_quote(). If it fails, it returns %NULL and sets the 111 * error. The @quoted_string need not actually contain quoted or 112 * escaped text; g_shell_unquote() simply goes through the string and 113 * unquotes/unescapes anything that the shell would. Both single and 114 * double quotes are handled, as are escapes including escaped 115 * newlines. The return value must be freed with g_free(). Possible 116 * errors are in the #G_SHELL_ERROR domain. 117 * 118 * Shell quoting rules are a bit strange. Single quotes preserve the 119 * literal string exactly. escape sequences are not allowed; not even 120 * \' - if you want a ' in the quoted text, you have to do something 121 * like 'foo'\''bar'. Double quotes allow $, `, ", \, and newline to 122 * be escaped with backslash. Otherwise double quotes preserve things 123 * literally. 124 * 125 * Params: 126 * quotedString = shell-quoted string 127 * 128 * Returns: an unquoted string 129 * 130 * Throws: GException on failure. 131 */ 132 public static string shellUnquote(string quotedString) 133 { 134 GError* err = null; 135 136 auto retStr = g_shell_unquote(Str.toStringz(quotedString), &err); 137 138 if (err !is null) 139 { 140 throw new GException( new ErrorG(err) ); 141 } 142 143 scope(exit) Str.freeString(retStr); 144 return Str.toString(retStr); 145 } 146 }