Ronny Chevalier <chevalier.ronny@gmail.com>shared: add terminal-util.[ch]

Signed-off-by: Anthony G. Basile <blueness@gentoo.org>
This commit is contained in:
Ronny Chevalier 2015-04-15 16:05:41 -04:00 committed by Anthony G. Basile
parent 5ad9610b49
commit 38a4db66e7
6 changed files with 130 additions and 63 deletions

View File

@ -28,6 +28,7 @@ libudev_shared_la_SOURCES=\
strv.c \
strxcpyx.c \
sysctl-util.c \
terminal-util.c \
time-util.c \
util.c \
utf8.c \
@ -65,6 +66,7 @@ noinst_HEADERS = \
strv.h \
strxcpyx.h \
sysctl-util.h \
terminal-util.h \
time-util.h \
util.h \
utf8.h \

View File

@ -34,6 +34,7 @@
#include "socket-util.h"
#include "time-util.h"
#include "process-util.h"
#include "terminal-util.h"
#define SNDBUF_SIZE (8*1024*1024)

View File

@ -0,0 +1,87 @@
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <assert.h>
#include <poll.h>
#include <linux/vt.h>
#include <linux/tiocl.h>
#include <linux/kd.h>
#include "terminal-util.h"
#include "time-util.h"
#include "process-util.h"
#include "util.h"
#include "fileio.h"
#include "path-util.h"
static volatile unsigned cached_columns = 0;
static volatile unsigned cached_lines = 0;
int open_terminal(const char *name, int mode) {
int fd, r;
unsigned c = 0;
/*
* If a TTY is in the process of being closed opening it might
* cause EIO. This is horribly awful, but unlikely to be
* changed in the kernel. Hence we work around this problem by
* retrying a couple of times.
*
* https://bugs.launchpad.net/ubuntu/+source/linux/+bug/554172/comments/245
*/
assert(!(mode & O_CREAT));
for (;;) {
fd = open(name, mode, 0);
if (fd >= 0)
break;
if (errno != EIO)
return -errno;
/* Max 1s in total */
if (c >= 20)
return -errno;
usleep(50 * USEC_PER_MSEC);
c++;
}
r = isatty(fd);
if (r < 0) {
safe_close(fd);
return -errno;
}
if (!r) {
safe_close(fd);
return -ENOTTY;
}
return fd;
}

View File

@ -0,0 +1,39 @@
#pragma once
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdbool.h>
#include <stdarg.h>
#include <stdio.h>
#include "macro.h"
#include "time-util.h"
#define ANSI_HIGHLIGHT_ON "\x1B[1;39m"
#define ANSI_RED_ON "\x1B[31m"
#define ANSI_HIGHLIGHT_RED_ON "\x1B[1;31m"
#define ANSI_GREEN_ON "\x1B[32m"
#define ANSI_HIGHLIGHT_GREEN_ON "\x1B[1;32m"
#define ANSI_HIGHLIGHT_YELLOW_ON "\x1B[1;33m"
#define ANSI_HIGHLIGHT_BLUE_ON "\x1B[1;34m"
#define ANSI_HIGHLIGHT_OFF "\x1B[0m"
#define ANSI_ERASE_TO_END_OF_LINE "\x1B[K"
int open_terminal(const char *name, int mode);

View File

@ -33,9 +33,6 @@
#include <fcntl.h>
#include <dirent.h>
#include <sys/ioctl.h>
#include <linux/vt.h>
#include <linux/tiocl.h>
#include <termios.h>
#include <stdarg.h>
#include <poll.h>
#include <ctype.h>
@ -71,6 +68,7 @@
#include "virt.h"
#include "process-util.h"
#include "random-util.h"
#include "terminal-util.h"
/* Put this test here for a lack of better place */
assert_cc(EAGAIN == EWOULDBLOCK);
@ -78,9 +76,6 @@ assert_cc(EAGAIN == EWOULDBLOCK);
int saved_argc = 0;
char **saved_argv = NULL;
static volatile unsigned cached_columns = 0;
static volatile unsigned cached_lines = 0;
size_t page_size(void) {
static thread_local size_t pgsz = 0;
long r;
@ -600,51 +595,6 @@ bool hidden_file(const char *filename) {
return hidden_file_allow_backup(filename);
}
int open_terminal(const char *name, int mode) {
int fd, r;
unsigned c = 0;
/*
* If a TTY is in the process of being closed opening it might
* cause EIO. This is horribly awful, but unlikely to be
* changed in the kernel. Hence we work around this problem by
* retrying a couple of times.
*
* https://bugs.launchpad.net/ubuntu/+source/linux/+bug/554172/comments/245
*/
assert(!(mode & O_CREAT));
for (;;) {
fd = open(name, mode, 0);
if (fd >= 0)
break;
if (errno != EIO)
return -errno;
/* Max 1s in total */
if (c >= 20)
return -errno;
usleep(50 * USEC_PER_MSEC);
c++;
}
r = isatty(fd);
if (r < 0) {
safe_close(fd);
return -errno;
}
if (!r) {
safe_close(fd);
return -ENOTTY;
}
return fd;
}
int flush_fd(int fd) {
struct pollfd pollfd = {
.fd = fd,

View File

@ -50,16 +50,6 @@
#define FORMAT_BYTES_MAX 8
#define ANSI_HIGHLIGHT_ON "\x1B[1;39m"
#define ANSI_RED_ON "\x1B[31m"
#define ANSI_HIGHLIGHT_RED_ON "\x1B[1;31m"
#define ANSI_GREEN_ON "\x1B[32m"
#define ANSI_HIGHLIGHT_GREEN_ON "\x1B[1;32m"
#define ANSI_HIGHLIGHT_YELLOW_ON "\x1B[1;33m"
#define ANSI_HIGHLIGHT_BLUE_ON "\x1B[1;34m"
#define ANSI_HIGHLIGHT_OFF "\x1B[0m"
#define ANSI_ERASE_TO_END_OF_LINE "\x1B[K"
size_t page_size(void) _pure_;
#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
@ -223,8 +213,6 @@ bool hidden_file(const char *filename) _pure_;
} \
struct __useless_struct_to_allow_trailing_semicolon__
int open_terminal(const char *name, int mode);
int flush_fd(int fd);
int fopen_temporary(const char *path, FILE **_f, char **_temp_path);