~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/lib/subcmd/pager.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include <sys/select.h>
  3 #include <stdlib.h>
  4 #include <stdio.h>
  5 #include <string.h>
  6 #include <signal.h>
  7 #include <sys/ioctl.h>
  8 #include "pager.h"
  9 #include "run-command.h"
 10 #include "sigchain.h"
 11 #include "subcmd-config.h"
 12 
 13 /*
 14  * This is split up from the rest of git so that we can do
 15  * something different on Windows.
 16  */
 17 
 18 static int spawned_pager;
 19 static int pager_columns;
 20 
 21 void pager_init(const char *pager_env)
 22 {
 23         subcmd_config.pager_env = pager_env;
 24 }
 25 
 26 static const char *forced_pager;
 27 
 28 void force_pager(const char *pager)
 29 {
 30         forced_pager = pager;
 31 }
 32 
 33 static void pager_preexec(void)
 34 {
 35         /*
 36          * Work around bug in "less" by not starting it until we
 37          * have real input
 38          */
 39         fd_set in;
 40         fd_set exception;
 41 
 42         FD_ZERO(&in);
 43         FD_ZERO(&exception);
 44         FD_SET(0, &in);
 45         FD_SET(0, &exception);
 46         select(1, &in, NULL, &exception, NULL);
 47 
 48         setenv("LESS", "FRSX", 0);
 49 }
 50 
 51 static const char *pager_argv[] = { "sh", "-c", NULL, NULL };
 52 static struct child_process pager_process;
 53 
 54 static void wait_for_pager(void)
 55 {
 56         fflush(stdout);
 57         fflush(stderr);
 58         /* signal EOF to pager */
 59         close(1);
 60         close(2);
 61         finish_command(&pager_process);
 62 }
 63 
 64 static void wait_for_pager_signal(int signo)
 65 {
 66         wait_for_pager();
 67         sigchain_pop(signo);
 68         raise(signo);
 69 }
 70 
 71 void setup_pager(void)
 72 {
 73         const char *pager = getenv(subcmd_config.pager_env);
 74         struct winsize sz;
 75 
 76         if (forced_pager)
 77                 pager = forced_pager;
 78         if (!isatty(1) && !forced_pager)
 79                 return;
 80         if (ioctl(1, TIOCGWINSZ, &sz) == 0)
 81                 pager_columns = sz.ws_col;
 82         if (!pager)
 83                 pager = getenv("PAGER");
 84         if (!(pager || access("/usr/bin/pager", X_OK)))
 85                 pager = "/usr/bin/pager";
 86         if (!(pager || access("/usr/bin/less", X_OK)))
 87                 pager = "/usr/bin/less";
 88         if (!pager)
 89                 pager = "cat";
 90         if (!*pager || !strcmp(pager, "cat"))
 91                 return;
 92 
 93         spawned_pager = 1; /* means we are emitting to terminal */
 94 
 95         /* spawn the pager */
 96         pager_argv[2] = pager;
 97         pager_process.argv = pager_argv;
 98         pager_process.in = -1;
 99         pager_process.preexec_cb = pager_preexec;
100 
101         if (start_command(&pager_process))
102                 return;
103 
104         /* original process continues, but writes to the pipe */
105         dup2(pager_process.in, 1);
106         if (isatty(2))
107                 dup2(pager_process.in, 2);
108         close(pager_process.in);
109 
110         /* this makes sure that the parent terminates after the pager */
111         sigchain_push_common(wait_for_pager_signal);
112         atexit(wait_for_pager);
113 }
114 
115 int pager_in_use(void)
116 {
117         return spawned_pager;
118 }
119 
120 int pager_get_columns(void)
121 {
122         char *s;
123 
124         s = getenv("COLUMNS");
125         if (s)
126                 return atoi(s);
127 
128         return (pager_columns ? pager_columns : 80) - 2;
129 }
130 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php