1 // SPDX-License-Identifier: LGPL-2.1-or-later
4 * BlueZ - Bluetooth protocol stack for Linux
6 * Copyright (C) 2011-2014 Intel Corporation
7 * Copyright (C) 2002-2010 Marcel Holtmann <marcel@holtmann.org>
23 #include <sys/prctl.h>
24 #include <sys/ioctl.h>
29 static pid_t pager_pid = 0;
30 #ifndef TIZEN_FEATURE_BLUEZ_MODIFY
31 int default_pager_num_columns = FALLBACK_TERMINAL_WIDTH;
34 enum monitor_color setting_monitor_color = COLOR_AUTO;
36 void set_monitor_color(enum monitor_color color)
38 setting_monitor_color = color;
43 static int cached_use_color = -1;
45 if (setting_monitor_color == COLOR_ALWAYS)
47 else if (setting_monitor_color == COLOR_NEVER)
49 else if (__builtin_expect(!!(cached_use_color < 0), 0))
50 cached_use_color = isatty(STDOUT_FILENO) > 0 || pager_pid > 0;
52 return cached_use_color;
55 #ifndef TIZEN_FEATURE_BLUEZ_MODIFY
56 void set_default_pager_num_columns(int num_columns)
58 default_pager_num_columns = num_columns;
64 static int cached_num_columns = -1;
66 if (__builtin_expect(!!(cached_num_columns < 0), 0)) {
69 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0 ||
71 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
72 cached_num_columns = FALLBACK_TERMINAL_WIDTH;
74 cached_num_columns = default_pager_num_columns;
77 cached_num_columns = ws.ws_col;
80 return cached_num_columns;
83 static void close_pipe(int p[])
91 static void wait_for_terminate(pid_t pid)
96 memset(&dummy, 0, sizeof(dummy));
98 if (waitid(P_PID, pid, &dummy, WEXITED) < 0) {
108 void open_pager(void)
117 pager = getenv("PAGER");
119 if (!*pager || strcmp(pager, "cat") == 0)
123 if (!(isatty(STDOUT_FILENO) > 0))
129 perror("Failed to create pager pipe");
133 parent_pid = getpid();
137 perror("Failed to fork pager");
142 if (pager_pid == 0) {
143 dup2(fd[0], STDIN_FILENO);
146 setenv("LESS", "FRSX", 0);
148 if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
151 if (getppid() != parent_pid)
155 execlp(pager, pager, NULL);
156 execl("/bin/sh", "sh", "-c", pager, NULL);
159 execlp("pager", "pager", NULL);
160 execlp("less", "less", NULL);
161 execlp("more", "more", NULL);
166 if (dup2(fd[1], STDOUT_FILENO) < 0) {
167 perror("Failed to duplicate pager pipe");
174 void close_pager(void)
180 kill(pager_pid, SIGCONT);
181 wait_for_terminate(pager_pid);