+#if ENABLE_FEATURE_USE_TERMIOS
+static unsigned handle_input(unsigned scan_mask, unsigned interval)
+{
+ unsigned char c;
+ struct pollfd pfd[1];
+
+ pfd[0].fd = 0;
+ pfd[0].events = POLLIN;
+
+ while (1) {
+ if (safe_poll(pfd, 1, interval * 1000) <= 0)
+ return scan_mask;
+ interval = 0;
+
+ if (safe_read(STDIN_FILENO, &c, 1) != 1) { /* error/EOF? */
+ option_mask32 |= OPT_EOF;
+ return scan_mask;
+ }
+
+ if (c == initial_settings.c_cc[VINTR])
+ return EXIT_MASK;
+ if (c == initial_settings.c_cc[VEOF])
+ return EXIT_MASK;
+ c |= 0x20; /* lowercase */
+ if (c == 'q')
+ return EXIT_MASK;
+
+ if (c == 'n') {
+ IF_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
+ sort_function[0] = pid_sort;
+ continue;
+ }
+ if (c == 'm') {
+ IF_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
+ sort_function[0] = mem_sort;
+# if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+ sort_function[1] = pcpu_sort;
+ sort_function[2] = time_sort;
+# endif
+ continue;
+ }
+# if ENABLE_FEATURE_SHOW_THREADS
+ if (c == 'h'
+ IF_FEATURE_TOPMEM(&& scan_mask != TOPMEM_MASK)
+ ) {
+ scan_mask ^= PSSCAN_TASKS;
+ continue;
+ }
+# endif
+# if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+ if (c == 'p') {
+ IF_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
+ sort_function[0] = pcpu_sort;
+ sort_function[1] = mem_sort;
+ sort_function[2] = time_sort;
+ continue;
+ }
+ if (c == 't') {
+ IF_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
+ sort_function[0] = time_sort;
+ sort_function[1] = mem_sort;
+ sort_function[2] = pcpu_sort;
+ continue;
+ }
+# if ENABLE_FEATURE_TOPMEM
+ if (c == 's') {
+ scan_mask = TOPMEM_MASK;
+ free(prev_hist);
+ prev_hist = NULL;
+ prev_hist_count = 0;
+ sort_field = (sort_field + 1) % NUM_SORT_FIELD;
+ continue;
+ }
+# endif
+ if (c == 'r') {
+ inverted ^= 1;
+ continue;
+ }
+# if ENABLE_FEATURE_TOP_SMP_CPU
+ /* procps-2.0.18 uses 'C', 3.2.7 uses '1' */
+ if (c == 'c' || c == '1') {
+ /* User wants to toggle per cpu <> aggregate */
+ if (smp_cpu_info) {
+ free(cpu_prev_jif);
+ free(cpu_jif);
+ cpu_jif = &cur_jif;
+ cpu_prev_jif = &prev_jif;
+ } else {
+ /* Prepare for xrealloc() */
+ cpu_jif = cpu_prev_jif = NULL;
+ }
+ num_cpus = 0;
+ smp_cpu_info = !smp_cpu_info;
+ get_jiffy_counts();
+ continue;
+ }
+# endif
+# endif
+ break; /* unknown key -> force refresh */
+ }
+
+ return scan_mask;
+}
+#endif
+
+//usage:#if ENABLE_FEATURE_SHOW_THREADS || ENABLE_FEATURE_TOP_SMP_CPU
+//usage:# define IF_SHOW_THREADS_OR_TOP_SMP(...) __VA_ARGS__
+//usage:#else
+//usage:# define IF_SHOW_THREADS_OR_TOP_SMP(...)
+//usage:#endif
+//usage:#define top_trivial_usage
+//usage: "[-b] [-nCOUNT] [-dSECONDS]" IF_FEATURE_TOPMEM(" [-m]")
+//usage:#define top_full_usage "\n\n"
+//usage: "Provide a view of process activity in real time."
+//usage: "\n""Read the status of all processes from /proc each SECONDS"
+//usage: "\n""and display a screenful of them."
+//usage: "\n""Keys:"
+//usage: "\n"" N/M"
+//usage: IF_FEATURE_TOP_CPU_USAGE_PERCENTAGE("/P")
+//usage: IF_FEATURE_TOP_CPU_USAGE_PERCENTAGE("/T")
+//usage: ": " IF_FEATURE_TOPMEM("show CPU usage, ") "sort by pid/mem"
+//usage: IF_FEATURE_TOP_CPU_USAGE_PERCENTAGE("/cpu")
+//usage: IF_FEATURE_TOP_CPU_USAGE_PERCENTAGE("/time")
+//usage: IF_FEATURE_TOPMEM(
+//usage: "\n"" S: show memory"
+//usage: )
+//usage: "\n"" R: reverse sort"
+//usage: IF_SHOW_THREADS_OR_TOP_SMP(
+//usage: "\n"" "
+//usage: IF_FEATURE_SHOW_THREADS("H: toggle threads")
+//usage: IF_FEATURE_SHOW_THREADS(IF_FEATURE_TOP_SMP_CPU(", "))
+//usage: IF_FEATURE_TOP_SMP_CPU("1: toggle SMP")
+//usage: )
+//usage: "\n"" Q,^C: exit"
+//usage: "\n"
+//usage: "\n""Options:"
+//usage: "\n"" -b Batch mode"
+//usage: "\n"" -n N Exit after N iterations"
+//usage: "\n"" -d N Delay between updates"
+//usage: IF_FEATURE_TOPMEM(
+//usage: "\n"" -m Same as 's' key"
+//usage: )
+
+/* Interactive testing:
+ * echo sss | ./busybox top
+ * - shows memory screen
+ * echo sss | ./busybox top -bn1 >mem
+ * - saves memory screen - the *whole* list, not first NROWS processes!
+ * echo .m.s.s.s.s.s.s.q | ./busybox top -b >z
+ * - saves several different screens, and exits
+ *
+ * TODO: -i STRING param as a better alternative?
+ */
+