ash: in tryexec(), ensure we don't try to run embedded scripts as applets
[oweals/busybox.git] / procps / powertop.c
index ce85f4191e0f605dfd67e1f00f072956201a4b92..004b4ce195c62ecd56e06bd9f97404d5bb776919 100644 (file)
@@ -8,17 +8,24 @@
  *
  * Licensed under GPLv2, see file LICENSE in this source tree.
  */
+//config:config POWERTOP
+//config:      bool "powertop (9.1 kb)"
+//config:      default y
+//config:      help
+//config:      Analyze power consumption on Intel-based laptops
+//config:
+//config:config FEATURE_POWERTOP_INTERACTIVE
+//config:      bool "Accept keyboard commands"
+//config:      default y
+//config:      depends on POWERTOP
+//config:      help
+//config:      Without this, powertop will only refresh display every 10 seconds.
+//config:      No keyboard commands will work, only ^C to terminate.
 
 //applet:IF_POWERTOP(APPLET(powertop, BB_DIR_USR_SBIN, BB_SUID_DROP))
 
 //kbuild:lib-$(CONFIG_POWERTOP) += powertop.o
 
-//config:config POWERTOP
-//config:      bool "powertop"
-//config:      default y
-//config:      help
-//config:        Analyze power consumption on Intel-based laptops
-
 // XXX This should be configurable
 #define ENABLE_FEATURE_POWERTOP_PROCIRQ 1
 
@@ -44,6 +51,8 @@
 /* Max filename length of entry in /sys/devices subsystem */
 #define BIG_SYSNAME_LEN    16
 
+#define ESC "\033"
+
 typedef unsigned long long ullong;
 
 struct line {
@@ -82,7 +91,7 @@ struct globals {
        ullong last_usage[MAX_CSTATE_COUNT];
        ullong start_duration[MAX_CSTATE_COUNT];
        ullong last_duration[MAX_CSTATE_COUNT];
-#if ENABLE_FEATURE_USE_TERMIOS
+#if ENABLE_FEATURE_POWERTOP_INTERACTIVE
        struct termios init_settings;
 #endif
 };
@@ -91,7 +100,7 @@ struct globals {
        SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
 } while (0)
 
-#if ENABLE_FEATURE_USE_TERMIOS
+#if ENABLE_FEATURE_POWERTOP_INTERACTIVE
 static void reset_term(void)
 {
        tcsetattr_stdin_TCSANOW(&G.init_settings);
@@ -677,13 +686,12 @@ static void show_timerstats(void)
 //usage:       "Analyze power consumption on Intel-based laptops"
 
 int powertop_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int powertop_main(int UNUSED_PARAM argc, char UNUSED_PARAM **argv)
+int powertop_main(int argc UNUSED_PARAM, char UNUSED_PARAM **argv)
 {
        ullong cur_usage[MAX_CSTATE_COUNT];
        ullong cur_duration[MAX_CSTATE_COUNT];
        char cstate_lines[MAX_CSTATE_COUNT + 2][64];
-#if ENABLE_FEATURE_USE_TERMIOS
-       struct termios new_settings;
+#if ENABLE_FEATURE_POWERTOP_INTERACTIVE
        struct pollfd pfd[1];
 
        pfd[0].fd = 0;
@@ -706,15 +714,12 @@ int powertop_main(int UNUSED_PARAM argc, char UNUSED_PARAM **argv)
 
        puts("Collecting data for "DEFAULT_SLEEP_STR" seconds");
 
-#if ENABLE_FEATURE_USE_TERMIOS
-       tcgetattr(0, (void *)&G.init_settings);
-       memcpy(&new_settings, &G.init_settings, sizeof(new_settings));
-       /* Turn on unbuffered input, turn off echoing */
-       new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL);
-       /* So we don't forget to reset term settings */
-       atexit(reset_term);
+#if ENABLE_FEATURE_POWERTOP_INTERACTIVE
+       /* Turn on unbuffered input; turn off echoing, ^C ^Z etc */
+       set_termios_to_raw(STDIN_FILENO, &G.init_settings, TERMIOS_CLEAR_ISIG);
        bb_signals(BB_FATAL_SIGS, sig_handler);
-       tcsetattr_stdin_TCSANOW(&new_settings);
+       /* So we don't forget to reset term settings */
+       die_func = reset_term;
 #endif
 
        /* Collect initial data */
@@ -739,7 +744,7 @@ int powertop_main(int UNUSED_PARAM argc, char UNUSED_PARAM **argv)
                int i;
 
                G.cant_enable_timer_stats |= start_timer(); /* 1 on error */
-#if !ENABLE_FEATURE_USE_TERMIOS
+#if !ENABLE_FEATURE_POWERTOP_INTERACTIVE
                sleep(DEFAULT_SLEEP);
 #else
                if (safe_poll(pfd, 1, DEFAULT_SLEEP * 1000) > 0) {
@@ -773,8 +778,8 @@ int powertop_main(int UNUSED_PARAM argc, char UNUSED_PARAM **argv)
                        }
                }
 
-               /* Clear the screen */
-               printf("\033[H\033[J");
+               /* Home; clear screen */
+               printf(ESC"[H" ESC"[J");
 
                /* Clear C-state lines */
                memset(&cstate_lines, 0, sizeof(cstate_lines));
@@ -851,6 +856,9 @@ int powertop_main(int UNUSED_PARAM argc, char UNUSED_PARAM **argv)
        } /* for (;;) */
 
        bb_putchar('\n');
+#if ENABLE_FEATURE_POWERTOP_INTERACTIVE
+       reset_term();
+#endif
 
        return EXIT_SUCCESS;
 }