aboutsummaryrefslogtreecommitdiff
path: root/procps/top.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2011-05-06 20:47:54 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2011-05-06 20:47:54 +0200
commit4d6059eedc665b46c274f40067af20e3aad46894 (patch)
tree38f7dc38919ba017c10dbc22ea39457ef50f18e6 /procps/top.c
parent7d9a1d25e53544c922d5e362180b2703a86d32df (diff)
downloadbusybox-w32-4d6059eedc665b46c274f40067af20e3aad46894.tar.gz
busybox-w32-4d6059eedc665b46c274f40067af20e3aad46894.tar.bz2
busybox-w32-4d6059eedc665b46c274f40067af20e3aad46894.zip
top: another scripting improvement
function old new delta handle_input 492 493 +1 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to '')
-rw-r--r--procps/top.c43
1 files changed, 27 insertions, 16 deletions
diff --git a/procps/top.c b/procps/top.c
index ed8b56173..c46797e78 100644
--- a/procps/top.c
+++ b/procps/top.c
@@ -856,24 +856,22 @@ enum {
856#if ENABLE_FEATURE_USE_TERMIOS 856#if ENABLE_FEATURE_USE_TERMIOS
857static unsigned handle_input(unsigned scan_mask, unsigned interval) 857static unsigned handle_input(unsigned scan_mask, unsigned interval)
858{ 858{
859 unsigned char c, *p, buf[64]; 859 unsigned char c;
860 int len;
861 struct pollfd pfd[1]; 860 struct pollfd pfd[1];
862 861
863 pfd[0].fd = 0; 862 pfd[0].fd = 0;
864 pfd[0].events = POLLIN; 863 pfd[0].events = POLLIN;
865 if (safe_poll(pfd, 1, interval * 1000) <= 0)
866 return scan_mask;
867 864
868 len = safe_read(STDIN_FILENO, &buf, sizeof(buf)-1); 865 while (1) {
869 if (len <= 0) { /* error/EOF? */ 866 if (safe_poll(pfd, 1, interval * 1000) <= 0)
870 option_mask32 |= OPT_EOF; 867 return scan_mask;
871 return scan_mask; 868 interval = 0;
872 } 869
870 if (safe_read(STDIN_FILENO, &c, 1) != 1) { /* error/EOF? */
871 option_mask32 |= OPT_EOF;
872 return scan_mask;
873 }
873 874
874 buf[len] = 0;
875 p = buf;
876 while ((c = *p++) != 0) {
877 if (c == initial_settings.c_cc[VINTR]) 875 if (c == initial_settings.c_cc[VINTR])
878 return EXIT_MASK; 876 return EXIT_MASK;
879 if (c == initial_settings.c_cc[VEOF]) 877 if (c == initial_settings.c_cc[VEOF])
@@ -881,9 +879,11 @@ static unsigned handle_input(unsigned scan_mask, unsigned interval)
881 c |= 0x20; /* lowercase */ 879 c |= 0x20; /* lowercase */
882 if (c == 'q') 880 if (c == 'q')
883 return EXIT_MASK; 881 return EXIT_MASK;
882
884 if (c == 'n') { 883 if (c == 'n') {
885 IF_FEATURE_TOPMEM(scan_mask = TOP_MASK;) 884 IF_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
886 sort_function[0] = pid_sort; 885 sort_function[0] = pid_sort;
886 continue;
887 } 887 }
888 if (c == 'm') { 888 if (c == 'm') {
889 IF_FEATURE_TOPMEM(scan_mask = TOP_MASK;) 889 IF_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
@@ -892,12 +892,14 @@ static unsigned handle_input(unsigned scan_mask, unsigned interval)
892 sort_function[1] = pcpu_sort; 892 sort_function[1] = pcpu_sort;
893 sort_function[2] = time_sort; 893 sort_function[2] = time_sort;
894# endif 894# endif
895 continue;
895 } 896 }
896# if ENABLE_FEATURE_SHOW_THREADS 897# if ENABLE_FEATURE_SHOW_THREADS
897 if (c == 'h' 898 if (c == 'h'
898 IF_FEATURE_TOPMEM(&& scan_mask != TOPMEM_MASK) 899 IF_FEATURE_TOPMEM(&& scan_mask != TOPMEM_MASK)
899 ) { 900 ) {
900 scan_mask ^= PSSCAN_TASKS; 901 scan_mask ^= PSSCAN_TASKS;
902 continue;
901 } 903 }
902# endif 904# endif
903# if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE 905# if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
@@ -906,12 +908,14 @@ static unsigned handle_input(unsigned scan_mask, unsigned interval)
906 sort_function[0] = pcpu_sort; 908 sort_function[0] = pcpu_sort;
907 sort_function[1] = mem_sort; 909 sort_function[1] = mem_sort;
908 sort_function[2] = time_sort; 910 sort_function[2] = time_sort;
911 continue;
909 } 912 }
910 if (c == 't') { 913 if (c == 't') {
911 IF_FEATURE_TOPMEM(scan_mask = TOP_MASK;) 914 IF_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
912 sort_function[0] = time_sort; 915 sort_function[0] = time_sort;
913 sort_function[1] = mem_sort; 916 sort_function[1] = mem_sort;
914 sort_function[2] = pcpu_sort; 917 sort_function[2] = pcpu_sort;
918 continue;
915 } 919 }
916# if ENABLE_FEATURE_TOPMEM 920# if ENABLE_FEATURE_TOPMEM
917 if (c == 's') { 921 if (c == 's') {
@@ -920,10 +924,13 @@ static unsigned handle_input(unsigned scan_mask, unsigned interval)
920 prev_hist = NULL; 924 prev_hist = NULL;
921 prev_hist_count = 0; 925 prev_hist_count = 0;
922 sort_field = (sort_field + 1) % NUM_SORT_FIELD; 926 sort_field = (sort_field + 1) % NUM_SORT_FIELD;
927 continue;
923 } 928 }
924# endif 929# endif
925 if (c == 'r') 930 if (c == 'r') {
926 inverted ^= 1; 931 inverted ^= 1;
932 continue;
933 }
927# if ENABLE_FEATURE_TOP_SMP_CPU 934# if ENABLE_FEATURE_TOP_SMP_CPU
928 /* procps-2.0.18 uses 'C', 3.2.7 uses '1' */ 935 /* procps-2.0.18 uses 'C', 3.2.7 uses '1' */
929 if (c == 'c' || c == '1') { 936 if (c == 'c' || c == '1') {
@@ -940,9 +947,11 @@ static unsigned handle_input(unsigned scan_mask, unsigned interval)
940 num_cpus = 0; 947 num_cpus = 0;
941 smp_cpu_info = !smp_cpu_info; 948 smp_cpu_info = !smp_cpu_info;
942 get_jiffy_counts(); 949 get_jiffy_counts();
950 continue;
943 } 951 }
944# endif 952# endif
945# endif 953# endif
954 break; /* unknown key -> force refresh */
946 } 955 }
947 956
948 return scan_mask; 957 return scan_mask;
@@ -991,7 +1000,9 @@ static unsigned handle_input(unsigned scan_mask, unsigned interval)
991 * echo sss | ./busybox top 1000 * echo sss | ./busybox top
992 * - shows memory screen 1001 * - shows memory screen
993 * echo sss | ./busybox top -bn1 >mem 1002 * echo sss | ./busybox top -bn1 >mem
994 * - saves memory screen - the *whole* list, not first NROWS porcesses! 1003 * - saves memory screen - the *whole* list, not first NROWS processes!
1004 * echo .m.s.s.s.s.s.s.q | ./busybox top -b >z
1005 * - saves several different screens, and exits
995 * 1006 *
996 * TODO: -i STRING param as a better alternative? 1007 * TODO: -i STRING param as a better alternative?
997 */ 1008 */
@@ -1165,8 +1176,8 @@ int top_main(int argc UNUSED_PARAM, char **argv)
1165#if !ENABLE_FEATURE_USE_TERMIOS 1176#if !ENABLE_FEATURE_USE_TERMIOS
1166 sleep(interval); 1177 sleep(interval);
1167#else 1178#else
1168 if (option_mask32 & (OPT_b|OPT_EOF)) 1179 if (option_mask32 & OPT_EOF)
1169 /* batch mode, or EOF on stdin ("top </dev/null") */ 1180 /* EOF on stdin ("top </dev/null") */
1170 sleep(interval); 1181 sleep(interval);
1171 else 1182 else
1172 scan_mask = handle_input(scan_mask, interval); 1183 scan_mask = handle_input(scan_mask, interval);