]> granicus.if.org Git - procps-ng/commitdiff
top: add a search capability
authorJim Warner <james.warner@comcast.net>
Fri, 16 Dec 2011 20:04:38 +0000 (14:04 -0600)
committerCraig Small <csmall@enc.com.au>
Tue, 20 Dec 2011 11:15:31 +0000 (22:15 +1100)
top.c
top.h

diff --git a/top.c b/top.c
index d114910ba7de2fbe00449501976648458a3afcb9..dbcb8021d616a7cc531ecbdce08e5ff03c7af241 100644 (file)
--- a/top.c
+++ b/top.c
@@ -2666,6 +2666,37 @@ static void file_writerc (void) {
 } // end: file_writerc
 
 
+static void find_string (int ch) {
+ #define reDUX (found) ? "another " : ""
+   static char str[SCREENMAX];
+   static int found;
+   char buf[ROWMINSIZ];
+   int i;
+
+   if ('&' == ch && !str[0]) {
+      show_msg("Locate next inactive, use \"L\"");
+      return;
+   }
+   if ('L' == ch) {
+      strcpy(str, linein("Locate string"));
+      found = 0;
+   }
+   if (str[0]) {
+      for (i = Curwin->begtask; i < Frame_maxtask; i++) {
+         task_show(Curwin, Curwin->ppt[i], buf);
+         if (strstr(buf, str)) {
+            found = 1;
+            if (i == Curwin->begtask) continue;
+            Curwin->begtask = i;
+            return;
+         }
+      }
+      show_msg(fmtmk("%s\"%s\" not found", reDUX, str));
+   }
+ #undef reDUX
+} // end: find_string
+
+
 static void help_view (void) {
    WIN_t *w = Curwin;             // avoid gcc bloat with a local copy
    char ch;
@@ -2962,6 +2993,14 @@ static void keys_window (int ch) {
       case '_':
          if (ALTCHKw) wins_reflag(Flags_TOG, Show_TASKON);
          break;
+      case '&':
+      case 'L':
+         if (VIZCHKw(w)) {             // ( next 2 are strictly for the UI )
+            SETw(w, Show_IDLEPS);      // make sure we're showing idle tasks
+            w->usrseltyp = 0;          // make sure we're not user filtering
+            find_string(ch);           // ( we'll search entire ppt anyway )
+         }
+         break;
       case 'A':
          Rc.mode_altscr = !Rc.mode_altscr;
          break;
@@ -3157,7 +3196,7 @@ static void do_key (int ch) {
          { '#', '<', '>', 'b', 'c', 'i', 'n', 'R', 'S'
          , 'U', 'u', 'V', 'x', 'y', 'z' } },
       { keys_window,
-         { '+', '-', '=', '_', 'A', 'a', 'G', 'w'
+         { '+', '-', '=', '_', '&', 'A', 'a', 'G', 'L', 'w'
          , kbd_UP, kbd_DOWN, kbd_LEFT, kbd_RIGHT, kbd_PGUP, kbd_PGDN
          , kbd_HOME, kbd_END } },
       { keys_xtra,
@@ -3340,8 +3379,9 @@ static void summary_show (void) {
 
 
         /*
-         * Display information for a single task row. */
-static void task_show (const WIN_t *q, const proc_t *p) {
+         * Build the information for a single task row and
+         * display the results or return them to the caller. */
+static void task_show (const WIN_t *q, const proc_t *p, char *ptr) {
  #define makeCOL(va...)  snprintf(cbuf, sizeof(cbuf), f, ## va)
  #define makeVAR(v)  { f = VARCOL_fmts; makeCOL(q->varcolsz, q->varcolsz, v); }
  #define pages2K(n)  (unsigned long)( (n) << Pg2K_shft )
@@ -3364,6 +3404,7 @@ static void task_show (const WIN_t *q, const proc_t *p) {
          // these 2 aren't real procflgs, they're used in column highlighting!
          case X_XON:
          case X_XOF:
+            if (ptr) continue;
             /* treat running tasks specially - entire row may get highlighted
                so we needn't turn it on and we MUST NOT turn it off */
             if (!('R' == p->state && CHKw(q, Show_HIROWS)))
@@ -3527,10 +3568,13 @@ static void task_show (const WIN_t *q, const proc_t *p) {
         rp = scat(rp, cbuf);
    } // end: for 'maxpflgs'
 
-   PUFF("\n%s%s%s", (CHKw(q, Show_HIROWS) && 'R' == p->state)
-      ? q->capclr_rowhigh : q->capclr_rownorm
-      , rbuf
-      , q->eolcap);
+   if (ptr)
+      strcpy(ptr, rbuf);
+   else
+      PUFF("\n%s%s%s", (CHKw(q, Show_HIROWS) && 'R' == p->state)
+         ? q->capclr_rowhigh : q->capclr_rownorm
+         , rbuf
+         , q->eolcap);
  #undef makeCOL
  #undef makeVAR
  #undef pages2K
@@ -3570,14 +3614,14 @@ static int window_show (WIN_t *q, int wmax) {
       checking some stuff with each iteration and check it just once... */
    if (CHKw(q, Show_IDLEPS) && !q->usrseltyp)
       while (i < Frame_maxtask && lwin < wmax) {
-         task_show(q, q->ppt[i++]);
+         task_show(q, q->ppt[i++], NULL);
          ++lwin;
       }
    else
       while (i < Frame_maxtask && lwin < wmax) {
          if ((CHKw(q, Show_IDLEPS) || isBUSY(q->ppt[i]))
          && user_matched(q, q->ppt[i])) {
-            task_show(q, q->ppt[i]);
+            task_show(q, q->ppt[i], NULL);
             ++lwin;
          }
          ++i;
diff --git a/top.h b/top.h
index 08641ddc6d9f34aef8dc7a13300133b82965633f..a79286c0e8405fbddc9ffd2a35aeabea8e3ec8aa 100644 (file)
--- a/top.h
+++ b/top.h
@@ -511,7 +511,7 @@ typedef struct WIN_t {
    "  1,I       Toggle SMP view: '~11~2' single/separate states; '~1I~2' Irix/Solaris mode\n" \
    "  f,F       Manage Fields: add/remove; change order; select sort field\n" \
    "\n" \
-   "  <,>     . Move sort field: '~1<~2' next col left; '~1>~2' next col right\n" \
+   "  L,&,<,> . Locate: '~1L~2'/'~1&~2' find/again; Move sort column: '~1<~2'/'~1>~2' left/right\n" \
    "  R,H,V   . Toggle: '~1R~2' norm/rev sort; '~1H~2' show threads; '~1V~2' forest view\n" \
    "  c,i,S   . Toggle: '~1c~2' cmd name/line; '~1i~2' idle tasks; '~1S~2' cumulative time\n" \
    "  x~5,~1y~5     . Toggle highlights: '~1x~2' sort field; '~1y~2' running tasks\n" \
@@ -614,7 +614,8 @@ typedef struct WIN_t {
 
 /*######  Some Prototypes (ha!)  #########################################*/
 
-   /* These 'prototypes' are here solely for documentation purposes */
+   /* These 'prototypes' are here mostly for documentation purposes --    */
+   /* for now, the only essential one is task_show needed by find_string */
 /*------  Sort callbacks  ------------------------------------------------*/
 /*        for each possible field, in the form of:                        */
 /*atic int           sort_P_XXX (const proc_t **P, const proc_t **Q);     */
@@ -685,6 +686,7 @@ typedef struct WIN_t {
 //atic void          wins_stage_2 (void);
 /*------  Interactive Input support (do_key helpers)  --------------------*/
 //atic void          file_writerc (void);
+//atic void          find_string (int ch);
 //atic void          help_view (void);
 //atic void          keys_global (int ch);
 //atic void          keys_summary (int ch);
@@ -699,7 +701,7 @@ typedef struct WIN_t {
 //atic void          do_key (int ch);
 //atic void          summaryhlp (CPU_t *cpu, const char *pfx);
 //atic void          summary_show (void);
-//atic void          task_show (const WIN_t *q, const proc_t *p);
+static void          task_show (const WIN_t *q, const proc_t *p, char *ptr);
 //atic int           window_show (WIN_t *q, int wmax);
 /*------  Entry point plus two  ------------------------------------------*/
 //atic void          framehlp (int wix, int max);