summaryrefslogtreecommitdiff
path: root/src/ostree/ot-editor.c
blob: 608c58b69ebbca68c4a958291cb192c9290bc00b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
 *
 * Copyright (C) 2013 Stef Walter <stefw@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Stef Walter <stefw@redhat.com>
 */

#include "config.h"

#include "ot-editor.h"
#include "libgsystem.h"

#include <sys/wait.h>
#include <string.h>

#ifndef DEFAULT_EDITOR
#define DEFAULT_EDITOR "vi"
#endif

/* Logic pulled from git */

static const char *
get_editor (void)
{
  const char *editor = g_getenv ("OSTREE_EDITOR");
  const char *terminal = g_getenv ("TERM");
  int terminal_is_dumb = !terminal || g_str_equal (terminal, "dumb");

  if (!editor && !terminal_is_dumb)
    editor = g_getenv ("VISUAL");
  if (!editor)
    editor = g_getenv ("EDITOR");

  if (!editor && terminal_is_dumb)
    return NULL;

  if (!editor)
    editor = DEFAULT_EDITOR;

  return editor;
}

char *
ot_editor_prompt (OstreeRepo *repo,
                  const char *input,
                  GCancellable *cancellable,
                  GError **error)
{
  gs_unref_object GSSubprocessContext *ctx = NULL;
  gs_unref_object GSSubprocess *proc = NULL;
  gs_unref_object GFile *file = NULL;
  gs_unref_object GFileIOStream *io = NULL;
  GOutputStream *output;
  const char *editor;
  char *ret = NULL;
  gs_free gchar *args = NULL;

  editor = get_editor ();
  if (editor == NULL)
    {
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                           "Terminal is dumb, but EDITOR unset");
      goto out;
    }

  file = g_file_new_tmp (NULL, &io, error);
  if (file == NULL)
    goto out;

  output = g_io_stream_get_output_stream (G_IO_STREAM (io));
  if (!g_output_stream_write_all (output, input, strlen (input), NULL, cancellable, error) ||
      !g_io_stream_close (G_IO_STREAM (io), cancellable, error))
    goto out;

  {
    gs_free gchar *quoted_file = g_shell_quote (gs_file_get_path_cached (file));
    args = g_strconcat (editor, " ", quoted_file, NULL);
  }
  ctx = gs_subprocess_context_newv ("/bin/sh", "-c", args, NULL);
  gs_subprocess_context_set_stdin_disposition (ctx, GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT);
  gs_subprocess_context_set_stdout_disposition (ctx, GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT);
  gs_subprocess_context_set_stderr_disposition (ctx, GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT);

  proc = gs_subprocess_new (ctx, cancellable, error);
  if (proc == NULL)
    goto out;

  if (!gs_subprocess_wait_sync_check (proc, cancellable, error))
    {
      g_prefix_error (error, "There was a problem with the editor '%s'.", editor);
      goto out;
    }

  ret = gs_file_load_contents_utf8 (file, cancellable, error);

out:
  if (file)
    (void )g_file_delete (file, NULL, NULL);
  return ret;
}