Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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