aboutsummaryrefslogtreecommitdiff
path: root/console-tools
diff options
context:
space:
mode:
authorBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2008-05-19 08:18:50 +0000
committerBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2008-05-19 08:18:50 +0000
commitae4342ca3e30f7b11732ecda3ad15821e81bc314 (patch)
tree1878104940eac4649bc8add4d0b2893ab1cb0a94 /console-tools
parentee56e013cfb6304f66129afee7978b0864699419 (diff)
downloadbusybox-ae4342ca3e30f7b11732ecda3ad15821e81bc314.tar.gz
- Rename getpty() to xgetpty() and adjust callers.
- Rewrite kbd_mode and setconsole - Introduce and use console_make_active() and xopen_xwrite_close() - honour buffer-reservation method as set by the user (dumpkmap, loadkmap) - shrink rtcwake and some console-tools Saves about 270 Bytes
Diffstat (limited to 'console-tools')
-rw-r--r--console-tools/chvt.c12
-rw-r--r--console-tools/dumpkmap.c19
-rw-r--r--console-tools/kbd_mode.c73
-rw-r--r--console-tools/loadkmap.c25
-rw-r--r--console-tools/openvt.c23
-rw-r--r--console-tools/reset.c2
-rw-r--r--console-tools/setconsole.c32
-rw-r--r--console-tools/setlogcons.c9
8 files changed, 85 insertions, 110 deletions
diff --git a/console-tools/chvt.c b/console-tools/chvt.c
index ea96d1360..ea3e7c048 100644
--- a/console-tools/chvt.c
+++ b/console-tools/chvt.c
@@ -9,25 +9,17 @@
#include "libbb.h"
-/* From <linux/vt.h> */
-enum {
- VT_ACTIVATE = 0x5606, /* make vt active */
- VT_WAITACTIVE = 0x5607 /* wait for vt active */
-};
-
int chvt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int chvt_main(int argc, char **argv)
{
- int fd, num;
+ int num;
if (argc != 2) {
bb_show_usage();
}
- fd = get_console_fd();
num = xatou_range(argv[1], 1, 63);
/* double cast suppresses "cast to ptr from int of different size" */
- xioctl(fd, VT_ACTIVATE, (void *)(ptrdiff_t)num);
- xioctl(fd, VT_WAITACTIVE, (void *)(ptrdiff_t)num);
+ console_make_active(get_console_fd(), num);
return EXIT_SUCCESS;
}
diff --git a/console-tools/dumpkmap.c b/console-tools/dumpkmap.c
index 40b58f716..1adfdd738 100644
--- a/console-tools/dumpkmap.c
+++ b/console-tools/dumpkmap.c
@@ -7,6 +7,7 @@
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*
*/
+/* no options, no getopt */
#include "libbb.h"
@@ -23,18 +24,17 @@ struct kbentry {
#define MAX_NR_KEYMAPS 256
int dumpkmap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int dumpkmap_main(int argc, char **argv)
+int dumpkmap_main(int ATTRIBUTE_UNUSED argc, char ATTRIBUTE_UNUSED **argv)
{
struct kbentry ke;
int i, j, fd;
- char flags[MAX_NR_KEYMAPS];
+ RESERVE_CONFIG_BUFFER(flags,MAX_NR_KEYMAPS);
- if (argc >= 2 && argv[1][0] == '-')
- bb_show_usage();
+/* bb_warn_ignoring_args(argc>=2);*/
fd = xopen(CURRENT_VC, O_RDWR);
- write(1, "bkeymap", 7);
+ write(STDOUT_FILENO, "bkeymap", 7);
/* Here we want to set everything to 0 except for indexes:
* [0-2] [4-6] [8-10] [12] */
@@ -43,7 +43,7 @@ int dumpkmap_main(int argc, char **argv)
flags[3] = flags[7] = flags[11] = 0;
/* dump flags */
- write(1, flags, MAX_NR_KEYMAPS);
+ write(STDOUT_FILENO, flags, MAX_NR_KEYMAPS);
for (i = 0; i < MAX_NR_KEYMAPS; i++) {
if (flags[i] == 1) {
@@ -56,11 +56,14 @@ int dumpkmap_main(int argc, char **argv)
(char *)&ke.kb_table,
&ke.kb_value)
) {
- write(1, (void*)&ke.kb_value, 2);
+ write(STDOUT_FILENO, (void*)&ke.kb_value, 2);
}
}
}
}
- close(fd);
+ if (ENABLE_FEATURE_CLEAN_UP) {
+ close(fd);
+ RELEASE_CONFIG_BUFFER(flags);
+ }
return EXIT_SUCCESS;
}
diff --git a/console-tools/kbd_mode.c b/console-tools/kbd_mode.c
index 46ec3fd28..161495719 100644
--- a/console-tools/kbd_mode.c
+++ b/console-tools/kbd_mode.c
@@ -1,6 +1,6 @@
/* vi: set sw=4 ts=4: */
/*
- * Mini loadkmap implementation for busybox
+ * Mini kbd_mode implementation for busybox
*
* Copyright (C) 2007 Loïc Grenié <loic.grenie@gmail.com>
* written using Andries Brouwer <aeb@cwi.nl>'s kbd_mode from
@@ -14,54 +14,39 @@
#include <linux/kd.h>
int kbd_mode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int kbd_mode_main(int argc, char **argv)
+int kbd_mode_main(int ATTRIBUTE_UNUSED argc, char **argv)
{
- static const char opts[] = "saku";
-
- const char *opt = argv[1];
- const char *p;
int fd;
-
+ unsigned opt;
+ enum {
+ SCANCODE = (1<<0),
+ ASCII = (1<<1),
+ MEDIUMRAW= (1<<2),
+ UNICODE = (1<<3)
+ };
+ static const char KD_xxx[] ALIGN1 = "saku";
+ opt = getopt32(argv, KD_xxx);
fd = get_console_fd();
- if (fd < 0) /* get_console_fd() already complained */
+/* if (fd < 0)
return EXIT_FAILURE;
+*/
+ if (!opt) { /* print current setting */
+ const char *mode = "unknown";
+ int m;
- if (opt == NULL) {
- /* No arg */
- const char *msg = "unknown";
- int mode;
-
- ioctl(fd, KDGKBMODE, &mode);
- switch(mode) {
- case K_RAW:
- msg = "raw (scancode)";
- break;
- case K_XLATE:
- msg = "default (ASCII)";
- break;
- case K_MEDIUMRAW:
- msg = "mediumraw (keycode)";
- break;
- case K_UNICODE:
- msg = "Unicode (UTF-8)";
- break;
- }
- printf("The keyboard is in %s mode\n", msg);
- }
- else if (argc > 2 /* more than 1 arg */
- || *opt != '-' /* not an option */
- || (p = strchr(opts, opt[1])) == NULL /* not an option we expect */
- || opt[2] != '\0' /* more than one option char */
- ) {
- bb_show_usage();
- /* return EXIT_FAILURE; - not reached */
- }
- else {
-#if K_RAW != 0 || K_XLATE != 1 || K_MEDIUMRAW != 2 || K_UNICODE != 3
-#error kbd_mode must be changed
-#endif
- /* The options are in the order of the various K_xxx */
- ioctl(fd, KDSKBMODE, p - opts);
+ ioctl(fd, KDGKBMODE, &m);
+ if (m == K_RAW)
+ mode = "raw (scancode)";
+ else if (m == K_XLATE)
+ mode = "default (ASCII)";
+ else if (m == K_MEDIUMRAW)
+ mode = "mediumraw (keycode)";
+ else if (m == K_UNICODE)
+ mode = "Unicode (UTF-8)";
+ printf("The keyboard is in %s mode\n", mode);
+ } else {
+ opt = opt & UNICODE ? 3 : opt >> 1;
+ xioctl(fd, KDSKBMODE, &opt);
}
if (ENABLE_FEATURE_CLEAN_UP)
diff --git a/console-tools/loadkmap.c b/console-tools/loadkmap.c
index bea5a771f..28e53ebca 100644
--- a/console-tools/loadkmap.c
+++ b/console-tools/loadkmap.c
@@ -26,28 +26,26 @@ struct kbentry {
#define MAX_NR_KEYMAPS 256
int loadkmap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int loadkmap_main(int argc, char **argv ATTRIBUTE_UNUSED)
+int loadkmap_main(int ATTRIBUTE_UNUSED argc, char **argv ATTRIBUTE_UNUSED)
{
struct kbentry ke;
int i, j, fd;
uint16_t ibuff[NR_KEYS];
- char flags[MAX_NR_KEYMAPS];
- char buff[7];
+ RESERVE_CONFIG_BUFFER(flags,MAX_NR_KEYMAPS);
- if (argc != 1)
- bb_show_usage();
+/* bb_warn_ignoring_args(argc>=2);*/
fd = xopen(CURRENT_VC, O_RDWR);
- xread(0, buff, 7);
- if (strncmp(buff, BINARY_KEYMAP_MAGIC, 7))
- bb_error_msg_and_die("this is not a valid binary keymap");
+ xread(STDIN_FILENO, flags, 7);
+ if (strncmp(flags, BINARY_KEYMAP_MAGIC, 7))
+ bb_error_msg_and_die("not a valid binary keymap");
- xread(0, flags, MAX_NR_KEYMAPS);
+ xread(STDIN_FILENO, flags, MAX_NR_KEYMAPS);
for (i = 0; i < MAX_NR_KEYMAPS; i++) {
if (flags[i] == 1) {
- xread(0, ibuff, NR_KEYS * sizeof(uint16_t));
+ xread(STDIN_FILENO, ibuff, NR_KEYS * sizeof(uint16_t));
for (j = 0; j < NR_KEYS; j++) {
ke.kb_index = j;
ke.kb_table = i;
@@ -57,6 +55,9 @@ int loadkmap_main(int argc, char **argv ATTRIBUTE_UNUSED)
}
}
- if (ENABLE_FEATURE_CLEAN_UP) close(fd);
- return 0;
+ if (ENABLE_FEATURE_CLEAN_UP) {
+ close(fd);
+ RELEASE_CONFIG_BUFFER(flags);
+ }
+ return EXIT_SUCCESS;
}
diff --git a/console-tools/openvt.c b/console-tools/openvt.c
index c4746dfd3..208522418 100644
--- a/console-tools/openvt.c
+++ b/console-tools/openvt.c
@@ -61,7 +61,8 @@ static int get_vt_fd(void)
for (fd = 0; fd < 3; fd++)
if (!not_vt_fd(fd))
return fd;
- /* _only_ O_NONBLOCK: ask for neither read not write perms */
+ /* _only_ O_NONBLOCK: ask for neither read nor write perms */
+ /*FIXME: use? device_open(DEV_CONSOLE,0); */
fd = open(DEV_CONSOLE, O_NONBLOCK);
if (fd >= 0 && !not_vt_fd(fd))
return fd;
@@ -93,7 +94,7 @@ static NOINLINE void vfork_child(char **argv)
/* CHILD */
/* Try to make this VT our controlling tty */
setsid(); /* lose old ctty */
- ioctl(0, TIOCSCTTY, 0 /* 0: don't forcibly steal */);
+ ioctl(STDIN_FILENO, TIOCSCTTY, 0 /* 0: don't forcibly steal */);
//bb_error_msg("our sid %d", getsid(0));
//bb_error_msg("our pgrp %d", getpgrp());
//bb_error_msg("VT's sid %d", tcgetsid(0));
@@ -135,14 +136,13 @@ int openvt_main(int argc ATTRIBUTE_UNUSED, char **argv)
sprintf(vtname, VC_FORMAT, vtno);
/* (Try to) clean up stray open fds above fd 2 */
bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS | DAEMON_ONLY_SANITIZE, NULL);
- close(0);
+ close(STDIN_FILENO);
/*setsid(); - BAD IDEA: after we exit, child is SIGHUPed... */
xopen(vtname, O_RDWR);
- xioctl(0, VT_GETSTATE, &vtstat);
+ xioctl(STDIN_FILENO, VT_GETSTATE, &vtstat);
if (flags & OPT_s) {
- xioctl(0, VT_ACTIVATE, (void*)(ptrdiff_t)vtno);
- xioctl(0, VT_WAITACTIVE, (void*)(ptrdiff_t)vtno);
+ console_make_active(STDIN_FILENO, vtno);
}
if (!argv[0]) {
@@ -153,14 +153,16 @@ int openvt_main(int argc ATTRIBUTE_UNUSED, char **argv)
/*argv[1] = NULL; - already is */
}
- xdup2(0, STDOUT_FILENO);
- xdup2(0, STDERR_FILENO);
+ xdup2(STDIN_FILENO, STDOUT_FILENO);
+ xdup2(STDIN_FILENO, STDERR_FILENO);
#ifdef BLOAT
+ {
/* Handle -l (login shell) option */
const char *prog = argv[0];
if (flags & OPT_l)
argv[0] = xasprintf("-%s", argv[0]);
+ }
#endif
vfork_child(argv);
@@ -168,12 +170,11 @@ int openvt_main(int argc ATTRIBUTE_UNUSED, char **argv)
/* We have only one child, wait for it */
safe_waitpid(-1, NULL, 0); /* loops on EINTR */
if (flags & OPT_s) {
- xioctl(0, VT_ACTIVATE, (void*)(ptrdiff_t)(vtstat.v_active));
- xioctl(0, VT_WAITACTIVE, (void*)(ptrdiff_t)(vtstat.v_active));
+ console_make_active(STDIN_FILENO, vtstat.v_active);
// Compat: even with -c N (try to) disallocate:
// # /usr/app/kbd-1.12/bin/openvt -f -c 9 -ws sleep 5
// openvt: could not deallocate console 9
- xioctl(0, VT_DISALLOCATE, (void*)(ptrdiff_t)vtno);
+ xioctl(STDIN_FILENO, VT_DISALLOCATE, (void*)(ptrdiff_t)vtno);
}
}
return EXIT_SUCCESS;
diff --git a/console-tools/reset.c b/console-tools/reset.c
index a2bf44d9f..5d5d4e27b 100644
--- a/console-tools/reset.c
+++ b/console-tools/reset.c
@@ -26,7 +26,7 @@ int reset_main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
/* no options, no getopt */
- if (isatty(0) && isatty(1)) {
+ if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) {
/* See 'man 4 console_codes' for details:
* "ESC c" -- Reset
* "ESC ( K" -- Select user mapping
diff --git a/console-tools/setconsole.c b/console-tools/setconsole.c
index 8765a7c2e..82fe83f22 100644
--- a/console-tools/setconsole.c
+++ b/console-tools/setconsole.c
@@ -3,40 +3,34 @@
* setconsole.c - redirect system console output
*
* Copyright (C) 2004,2005 Enrik Berkhan <Enrik.Berkhan@inka.de>
+ * Copyright (C) 2008 Bernhard Fischer
*
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/
#include "libbb.h"
-#if ENABLE_FEATURE_SETCONSOLE_LONG_OPTIONS
-static const char setconsole_longopts[] ALIGN1 =
- "reset\0" No_argument "r"
- ;
-#endif
-
-#define OPT_SETCONS_RESET 1
-
int setconsole_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int setconsole_main(int argc, char **argv)
+int setconsole_main(int ATTRIBUTE_UNUSED argc, char **argv)
{
- unsigned long flags;
const char *device = CURRENT_TTY;
+ bool reset;
#if ENABLE_FEATURE_SETCONSOLE_LONG_OPTIONS
+ static const char setconsole_longopts[] ALIGN1 =
+ "reset\0" No_argument "r"
+ ;
applet_long_options = setconsole_longopts;
#endif
- flags = getopt32(argv, "r");
-
- if (argc - optind > 1)
- bb_show_usage();
+ /* at most one non-option argument */
+ opt_complementary = "?1";
+ reset = getopt32(argv, "r");
- if (argc - optind == 1) {
- if (flags & OPT_SETCONS_RESET)
- bb_show_usage();
- device = argv[optind];
+ argv += 1 + reset;
+ if (*argv) {
+ device = *argv;
} else {
- if (flags & OPT_SETCONS_RESET)
+ if (reset)
device = DEV_CONSOLE;
}
diff --git a/console-tools/setlogcons.c b/console-tools/setlogcons.c
index b312fa76e..aa8e0806f 100644
--- a/console-tools/setlogcons.c
+++ b/console-tools/setlogcons.c
@@ -17,15 +17,14 @@ int setlogcons_main(int argc ATTRIBUTE_UNUSED, char **argv)
struct {
char fn;
char subarg;
- } arg;
-
- arg.fn = 11; /* redirect kernel messages */
- arg.subarg = 0; /* to specified console (current as default) */
+ } arg = { 11, /* redirect kernel messages */
+ 0 /* to specified console (current as default) */
+ };
if (argv[1])
arg.subarg = xatou_range(argv[1], 0, 63);
xioctl(xopen(VC_1, O_RDONLY), TIOCLINUX, &arg);
- return 0;
+ return EXIT_SUCCESS;
}