Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/next/external/cache/sources/hcitools/monitor/display.c
Views: 3959
/*1*2* BlueZ - Bluetooth protocol stack for Linux3*4* Copyright (C) 2011-2012 Intel Corporation5* Copyright (C) 2004-2010 Marcel Holtmann <[email protected]>6*7*8* This program is free software; you can redistribute it and/or modify9* it under the terms of the GNU General Public License as published by10* the Free Software Foundation; either version 2 of the License, or11* (at your option) any later version.12*13* This program is distributed in the hope that it will be useful,14* but WITHOUT ANY WARRANTY; without even the implied warranty of15* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16* GNU General Public License for more details.17*18* You should have received a copy of the GNU General Public License19* along with this program; if not, write to the Free Software20* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA21*22*/2324#ifdef HAVE_CONFIG_H25#include <config.h>26#endif2728#include <stdio.h>29#include <errno.h>30#include <unistd.h>31#include <stdlib.h>32#include <string.h>33#include <signal.h>34#include <sys/wait.h>35#include <sys/prctl.h>36#include <sys/ioctl.h>37#include <termios.h>3839#include "display.h"4041static pid_t pager_pid = 0;4243bool use_color(void)44{45static int cached_use_color = -1;4647if (__builtin_expect(!!(cached_use_color < 0), 0))48cached_use_color = isatty(STDOUT_FILENO) > 0 || pager_pid > 0;4950return cached_use_color;51}5253int num_columns(void)54{55static int cached_num_columns = -1;5657if (__builtin_expect(!!(cached_num_columns < 0), 0)) {58struct winsize ws;5960if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)61return -1;6263if (ws.ws_col > 0)64cached_num_columns = ws.ws_col;65}6667return cached_num_columns;68}6970static void close_pipe(int p[])71{72if (p[0] >= 0)73close(p[0]);74if (p[1] >= 0)75close(p[1]);76}7778static void wait_for_terminate(pid_t pid)79{80siginfo_t dummy;8182for (;;) {83memset(&dummy, 0, sizeof(dummy));8485if (waitid(P_PID, pid, &dummy, WEXITED) < 0) {86if (errno == EINTR)87continue;88return;89}9091return;92}93}9495void open_pager(void)96{97const char *pager;98pid_t parent_pid;99int fd[2];100101if (pager_pid > 0)102return;103104pager = getenv("PAGER");105if (pager) {106if (!*pager || strcmp(pager, "cat") == 0)107return;108}109110if (!(isatty(STDOUT_FILENO) > 0))111return;112113num_columns();114115if (pipe(fd) < 0) {116perror("Failed to create pager pipe");117return;118}119120parent_pid = getpid();121122pager_pid = fork();123if (pager_pid < 0) {124perror("Failed to fork pager");125close_pipe(fd);126return;127}128129if (pager_pid == 0) {130dup2(fd[0], STDIN_FILENO);131close_pipe(fd);132133setenv("LESS", "FRSX", 0);134135if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)136_exit(EXIT_FAILURE);137138if (getppid() != parent_pid)139_exit(EXIT_SUCCESS);140141if (pager) {142execlp(pager, pager, NULL);143execl("/bin/sh", "sh", "-c", pager, NULL);144}145146execlp("pager", "pager", NULL);147execlp("less", "less", NULL);148execlp("more", "more", NULL);149150_exit(EXIT_FAILURE);151}152153if (dup2(fd[1], STDOUT_FILENO) < 0) {154perror("Failed to duplicate pager pipe");155return;156}157158close_pipe(fd);159}160161void close_pager(void)162{163if (pager_pid <= 0)164return;165166fclose(stdout);167kill(pager_pid, SIGCONT);168wait_for_terminate(pager_pid);169pager_pid = 0;170}171172173