pager.con commit editor: add a function to launch the sequence editor (2aed018)
   1#include "cache.h"
   2#include "config.h"
   3#include "run-command.h"
   4#include "sigchain.h"
   5#include "alias.h"
   6
   7#ifndef DEFAULT_PAGER
   8#define DEFAULT_PAGER "less"
   9#endif
  10
  11static struct child_process pager_process = CHILD_PROCESS_INIT;
  12static const char *pager_program;
  13
  14static void wait_for_pager(int in_signal)
  15{
  16        if (!in_signal) {
  17                fflush(stdout);
  18                fflush(stderr);
  19        }
  20        /* signal EOF to pager */
  21        close(1);
  22        close(2);
  23        if (in_signal)
  24                finish_command_in_signal(&pager_process);
  25        else
  26                finish_command(&pager_process);
  27}
  28
  29static void wait_for_pager_atexit(void)
  30{
  31        wait_for_pager(0);
  32}
  33
  34static void wait_for_pager_signal(int signo)
  35{
  36        wait_for_pager(1);
  37        sigchain_pop(signo);
  38        raise(signo);
  39}
  40
  41static int core_pager_config(const char *var, const char *value, void *data)
  42{
  43        if (!strcmp(var, "core.pager"))
  44                return git_config_string(&pager_program, var, value);
  45        return 0;
  46}
  47
  48const char *git_pager(int stdout_is_tty)
  49{
  50        const char *pager;
  51
  52        if (!stdout_is_tty)
  53                return NULL;
  54
  55        pager = getenv("GIT_PAGER");
  56        if (!pager) {
  57                if (!pager_program)
  58                        read_early_config(core_pager_config, NULL);
  59                pager = pager_program;
  60        }
  61        if (!pager)
  62                pager = getenv("PAGER");
  63        if (!pager)
  64                pager = DEFAULT_PAGER;
  65        if (!*pager || !strcmp(pager, "cat"))
  66                pager = NULL;
  67
  68        return pager;
  69}
  70
  71static void setup_pager_env(struct argv_array *env)
  72{
  73        const char **argv;
  74        int i;
  75        char *pager_env = xstrdup(PAGER_ENV);
  76        int n = split_cmdline(pager_env, &argv);
  77
  78        if (n < 0)
  79                die("malformed build-time PAGER_ENV: %s",
  80                        split_cmdline_strerror(n));
  81
  82        for (i = 0; i < n; i++) {
  83                char *cp = strchr(argv[i], '=');
  84
  85                if (!cp)
  86                        die("malformed build-time PAGER_ENV");
  87
  88                *cp = '\0';
  89                if (!getenv(argv[i])) {
  90                        *cp = '=';
  91                        argv_array_push(env, argv[i]);
  92                }
  93        }
  94        free(pager_env);
  95        free(argv);
  96}
  97
  98void prepare_pager_args(struct child_process *pager_process, const char *pager)
  99{
 100        argv_array_push(&pager_process->args, pager);
 101        pager_process->use_shell = 1;
 102        setup_pager_env(&pager_process->env_array);
 103}
 104
 105void setup_pager(void)
 106{
 107        const char *pager = git_pager(isatty(1));
 108
 109        if (!pager)
 110                return;
 111
 112        /*
 113         * After we redirect standard output, we won't be able to use an ioctl
 114         * to get the terminal size. Let's grab it now, and then set $COLUMNS
 115         * to communicate it to any sub-processes.
 116         */
 117        {
 118                char buf[64];
 119                xsnprintf(buf, sizeof(buf), "%d", term_columns());
 120                setenv("COLUMNS", buf, 0);
 121        }
 122
 123        setenv("GIT_PAGER_IN_USE", "true", 1);
 124
 125        /* spawn the pager */
 126        prepare_pager_args(&pager_process, pager);
 127        pager_process.in = -1;
 128        argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
 129        if (start_command(&pager_process))
 130                return;
 131
 132        /* original process continues, but writes to the pipe */
 133        dup2(pager_process.in, 1);
 134        if (isatty(2))
 135                dup2(pager_process.in, 2);
 136        close(pager_process.in);
 137
 138        /* this makes sure that the parent terminates after the pager */
 139        sigchain_push_common(wait_for_pager_signal);
 140        atexit(wait_for_pager_atexit);
 141}
 142
 143int pager_in_use(void)
 144{
 145        return git_env_bool("GIT_PAGER_IN_USE", 0);
 146}
 147
 148/*
 149 * Return cached value (if set) or $COLUMNS environment variable (if
 150 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
 151 * and default to 80 if all else fails.
 152 */
 153int term_columns(void)
 154{
 155        static int term_columns_at_startup;
 156
 157        char *col_string;
 158        int n_cols;
 159
 160        if (term_columns_at_startup)
 161                return term_columns_at_startup;
 162
 163        term_columns_at_startup = 80;
 164
 165        col_string = getenv("COLUMNS");
 166        if (col_string && (n_cols = atoi(col_string)) > 0)
 167                term_columns_at_startup = n_cols;
 168#ifdef TIOCGWINSZ
 169        else {
 170                struct winsize ws;
 171                if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
 172                        term_columns_at_startup = ws.ws_col;
 173        }
 174#endif
 175
 176        return term_columns_at_startup;
 177}
 178
 179/*
 180 * How many columns do we need to show this number in decimal?
 181 */
 182int decimal_width(uintmax_t number)
 183{
 184        int width;
 185
 186        for (width = 1; number >= 10; width++)
 187                number /= 10;
 188        return width;
 189}
 190
 191struct pager_command_config_data {
 192        const char *cmd;
 193        int want;
 194        char *value;
 195};
 196
 197static int pager_command_config(const char *var, const char *value, void *vdata)
 198{
 199        struct pager_command_config_data *data = vdata;
 200        const char *cmd;
 201
 202        if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
 203                int b = git_parse_maybe_bool(value);
 204                if (b >= 0)
 205                        data->want = b;
 206                else {
 207                        data->want = 1;
 208                        data->value = xstrdup(value);
 209                }
 210        }
 211
 212        return 0;
 213}
 214
 215/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
 216int check_pager_config(const char *cmd)
 217{
 218        struct pager_command_config_data data;
 219
 220        data.cmd = cmd;
 221        data.want = -1;
 222        data.value = NULL;
 223
 224        read_early_config(pager_command_config, &data);
 225
 226        if (data.value)
 227                pager_program = data.value;
 228        return data.want;
 229}