+
+void
+print_table (char *const *argv)
+{
+ int i;
+
+ for (i = 0; argv[i] != NULL; i += 2)
+ printf ("%s: %s\n", argv[i], argv[i+1]);
+}
+
+int
+is_true (const char *str)
+{
+ return
+ STRCASENEQ (str, "0") &&
+ STRCASENEQ (str, "f") &&
+ STRCASENEQ (str, "false") &&
+ STRCASENEQ (str, "n") &&
+ STRCASENEQ (str, "no");
+}
+
+/* Free strings from a non-NULL terminated char** */
+static void
+free_n_strings (char **str, size_t len)
+{
+ size_t i;
+
+ for (i = 0; i < len; i++) {
+ free (str[i]);
+ }
+ free (str);
+}
+
+char **
+parse_string_list (const char *str)
+{
+ char **argv = NULL;
+ size_t argv_len = 0;
+
+ /* Current position pointer */
+ const char *p = str;
+
+ /* Token might be simple:
+ * Token
+ * or be quoted:
+ * 'This is a single token'
+ * or contain embedded single-quoted sections:
+ * This' is a sing'l'e to'ken
+ *
+ * The latter may seem over-complicated, but it's what a normal shell does.
+ * Not doing it risks surprising somebody.
+ *
+ * This outer loop is over complete tokens.
+ */
+ while (*p) {
+ char *tok = NULL;
+ size_t tok_len = 0;
+
+ /* Skip leading whitespace */
+ p += strspn (p, " \t");
+
+ char in_quote = 0;
+
+ /* This loop is over token 'fragments'. A token can be in multiple bits if
+ * it contains single quotes. We also treat both sides of an escaped quote
+ * as separate fragments because we can't just copy it: we have to remove
+ * the \.
+ */
+ while (*p && (!c_isblank (*p) || in_quote)) {
+ const char *end = p;
+
+ /* Check if the fragment starts with a quote */
+ if ('\'' == *p) {
+ /* Toggle in_quote */
+ in_quote = !in_quote;
+
+ /* Skip the quote */
+ p++; end++;
+ }
+
+ /* If we're in a quote, look for an end quote */
+ if (in_quote) {
+ end += strcspn (end, "'");
+ }
+
+ /* Otherwise, look for whitespace or a quote */
+ else {
+ end += strcspn (end, " \t'");
+ }
+
+ /* Grow the token to accommodate the fragment */
+ size_t tok_end = tok_len;
+ tok_len += end - p;
+ char *tok_new = realloc (tok, tok_len + 1);
+ if (NULL == tok_new) {
+ perror ("realloc");
+ free_n_strings (argv, argv_len);
+ free (tok);
+ exit (EXIT_FAILURE);
+ }
+ tok = tok_new;
+
+ /* Check if we stopped on an escaped quote */
+ if ('\'' == *end && end != p && *(end-1) == '\\') {
+ /* Add everything before \' to the token */
+ memcpy (&tok[tok_end], p, end - p - 1);
+
+ /* Add the quote */
+ tok[tok_len-1] = '\'';
+
+ /* Already processed the quote */
+ p = end + 1;
+ }
+
+ else {
+ /* Add the whole fragment */
+ memcpy (&tok[tok_end], p, end - p);
+
+ p = end;
+ }
+ }
+
+ /* We've reached the end of a token. We shouldn't still be in quotes. */
+ if (in_quote) {
+ fprintf (stderr, _("Runaway quote in string \"%s\"\n"), str);
+
+ free_n_strings (argv, argv_len);
+
+ return NULL;
+ }
+
+ /* Add this token if there is one. There might not be if there was
+ * whitespace at the end of the input string */
+ if (tok) {
+ /* Add the NULL terminator */
+ tok[tok_len] = '\0';
+
+ /* Add the argument to the argument list */
+ argv_len++;
+ char **argv_new = realloc (argv, sizeof (*argv) * argv_len);
+ if (NULL == argv_new) {
+ perror ("realloc");
+ free_n_strings (argv, argv_len-1);
+ free (tok);
+ exit (EXIT_FAILURE);
+ }
+ argv = argv_new;
+
+ argv[argv_len-1] = tok;
+ }
+ }
+
+ /* NULL terminate the argument list */
+ argv_len++;
+ char **argv_new = realloc (argv, sizeof (*argv) * argv_len);
+ if (NULL == argv_new) {
+ perror ("realloc");
+ free_n_strings (argv, argv_len-1);
+ exit (EXIT_FAILURE);
+ }
+ argv = argv_new;
+
+ argv[argv_len-1] = NULL;
+
+ return argv;
+}
+
+#ifdef HAVE_LIBREADLINE
+static char histfile[1024];
+static int nr_history_lines = 0;
+#endif
+
+static void
+initialize_readline (void)
+{
+#ifdef HAVE_LIBREADLINE
+ const char *home;
+
+ home = getenv ("HOME");
+ if (home) {
+ snprintf (histfile, sizeof histfile, "%s/.guestfish", home);
+ using_history ();
+ (void) read_history (histfile);
+ }
+
+ rl_readline_name = "guestfish";
+ rl_attempted_completion_function = do_completion;
+
+ /* Note that .inputrc (or /etc/inputrc) is not read until the first
+ * call the readline(), which happens later. Therefore, these
+ * provide default values which can be overridden by the user if
+ * they wish.
+ */
+ (void) rl_variable_bind ("completion-ignore-case", "on");
+#endif
+}
+
+static void
+cleanup_readline (void)
+{
+#ifdef HAVE_LIBREADLINE
+ int fd;
+
+ if (histfile[0] != '\0') {
+ fd = open (histfile, O_WRONLY|O_CREAT, 0644);
+ if (fd == -1) {
+ perror (histfile);
+ return;
+ }
+ close (fd);
+
+#ifdef HAVE_APPEND_HISTORY
+ (void) append_history (nr_history_lines, histfile);
+#else
+ (void) write_history (histfile);
+#endif
+ clear_history ();
+ }
+#endif
+}
+
+#ifdef HAVE_LIBREADLINE
+static void
+add_history_line (const char *line)
+{
+ add_history (line);
+ nr_history_lines++;
+}
+#endif
+
+int
+xwrite (int fd, const void *v_buf, size_t len)
+{
+ int r;
+ const char *buf = v_buf;
+
+ while (len > 0) {
+ r = write (fd, buf, len);
+ if (r == -1) {
+ perror ("write");
+ return -1;
+ }
+ buf += r;
+ len -= r;
+ }
+
+ return 0;
+}
+
+/* Resolve the special "win:..." form for Windows-specific paths.
+ * This always returns a newly allocated string which is freed by the
+ * caller function in "cmds.c".
+ */
+char *
+resolve_win_path (const char *path)
+{
+ char *ret;
+ size_t i;
+
+ if (STRCASENEQLEN (path, "win:", 4)) {
+ ret = strdup (path);
+ if (ret == NULL)
+ perror ("strdup");
+ return ret;
+ }
+
+ path += 4;
+
+ /* Drop drive letter, if it's "C:". */
+ if (STRCASEEQLEN (path, "c:", 2))
+ path += 2;
+
+ if (!*path) {
+ ret = strdup ("/");
+ if (ret == NULL)
+ perror ("strdup");
+ return ret;
+ }
+
+ ret = strdup (path);
+ if (ret == NULL) {
+ perror ("strdup");
+ return NULL;
+ }
+
+ /* Blindly convert any backslashes into forward slashes. Is this good? */
+ for (i = 0; i < strlen (ret); ++i)
+ if (ret[i] == '\\')
+ ret[i] = '/';
+
+ char *t = guestfs_case_sensitive_path (g, ret);
+ free (ret);
+ ret = t;
+
+ return ret;
+}
+
+/* Resolve the special FileIn paths ("-" or "-<<END" or filename).
+ * The caller (cmds.c) will call free_file_in after the command has
+ * run which should clean up resources.
+ */
+static char *file_in_heredoc (const char *endmarker);
+static char *file_in_tmpfile = NULL;
+
+char *
+file_in (const char *arg)
+{
+ char *ret;
+
+ if (STREQ (arg, "-")) {
+ ret = strdup ("/dev/stdin");
+ if (!ret) {
+ perror ("strdup");
+ return NULL;
+ }
+ }
+ else if (STRPREFIX (arg, "-<<")) {
+ const char *endmarker = &arg[3];
+ if (*endmarker == '\0') {
+ fprintf (stderr, "%s: missing end marker in -<< expression\n",
+ program_name);
+ return NULL;
+ }
+ ret = file_in_heredoc (endmarker);
+ if (ret == NULL)
+ return NULL;
+ }
+ else {
+ ret = strdup (arg);
+ if (!ret) {
+ perror ("strdup");
+ return NULL;
+ }
+ }
+
+ return ret;
+}
+
+static char *
+file_in_heredoc (const char *endmarker)
+{
+ TMP_TEMPLATE_ON_STACK (template);
+ file_in_tmpfile = strdup (template);
+ if (file_in_tmpfile == NULL) {
+ perror ("strdup");
+ return NULL;
+ }
+
+ int fd = mkstemp (file_in_tmpfile);
+ if (fd == -1) {
+ perror ("mkstemp");
+ goto error1;
+ }
+
+ size_t markerlen = strlen (endmarker);
+
+ char buffer[BUFSIZ];
+ int write_error = 0;
+ while (fgets (buffer, sizeof buffer, stdin) != NULL) {
+ /* Look for "END"<EOF> or "END\n" in input. */
+ size_t blen = strlen (buffer);
+ if (STREQLEN (buffer, endmarker, markerlen) &&
+ (blen == markerlen ||
+ (blen == markerlen+1 && buffer[markerlen] == '\n')))
+ goto found_end;
+
+ if (xwrite (fd, buffer, blen) == -1) {
+ if (!write_error) perror ("write");
+ write_error = 1;
+ /* continue reading up to the end marker */
+ }
+ }
+
+ /* Reached EOF of stdin without finding the end marker, which
+ * is likely to be an error.
+ */
+ fprintf (stderr, "%s: end of input reached without finding '%s'\n",
+ program_name, endmarker);
+ goto error2;
+
+ found_end:
+ if (write_error) {
+ close (fd);
+ goto error2;
+ }
+
+ if (close (fd) == -1) {
+ perror ("close");
+ goto error2;
+ }
+
+ return file_in_tmpfile;
+
+ error2:
+ unlink (file_in_tmpfile);
+
+ error1:
+ free (file_in_tmpfile);
+ file_in_tmpfile = NULL;
+ return NULL;
+}
+
+void
+free_file_in (char *s)
+{
+ if (file_in_tmpfile) {
+ if (unlink (file_in_tmpfile) == -1)
+ perror (file_in_tmpfile);
+ file_in_tmpfile = NULL;
+ }
+
+ /* Free the device or file name which was strdup'd in file_in().
+ * Note it's not immediately clear, but for -<< heredocs,
+ * s == file_in_tmpfile, so this frees up that buffer.
+ */
+ free (s);
+}
+
+/* Resolve the special FileOut paths ("-" or filename).
+ * The caller (cmds.c) will call free (str) after the command has run.
+ */
+char *
+file_out (const char *arg)
+{
+ char *ret;
+
+ if (STREQ (arg, "-"))
+ ret = strdup ("/dev/stdout");
+ else
+ ret = strdup (arg);
+
+ if (!ret) {
+ perror ("strdup");
+ return NULL;
+ }
+ return ret;
+}