home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / A / PS / PROCPS-0.000 / PROCPS-0 / procps-0.97 / compare.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-25  |  10.0 KB  |  292 lines

  1. /*
  2.  *
  3.  * Copyright 1994 Charles Blake and Michael K. Johnson
  4.  * This file is a part of procps, which is distributable
  5.  * under the conditions of the GNU Public License.  See the
  6.  * file COPYING for details.
  7.  *
  8.  */
  9.  
  10.  
  11. #include "ps.h"      /* for ps_proc */
  12. #include <string.h>  /* for strcmp */
  13. #include <stdio.h>  /* for parse error output */
  14.  
  15. /*
  16.  
  17.   This module was written by Charles Blake for procps.
  18.  
  19. mult_lvl_cmp:
  20.     slick general purpose multi-level compare function I invented.
  21. sort_depth:
  22.     the number of levels of functions *to use*.  This means many more levels
  23.     can be defined than mult_lvl_cmp tres out.  If this is 1 then mult_lvl_cmp
  24.     is just a trivial wrapper around (*sort_function[0]).
  25. sort_direction:
  26.     multiplicative factor for the output of cmp_whatever.
  27.     1 ==> default order, -1 ==> reverse order, 0 ==> forced equality
  28.     The 0 bit is the neat part.  Since a value of zero is the code for equality
  29.     multiplying the output of cmp_foo(a,b) forces a==b to be true.  This is a
  30.     convenient way to turn sorting off in middle levels of a multi-level sort.
  31.     If time is a problem, reforming the whole sort_function array to not include
  32.     these unsorted middle levels will be faster since then cmp_foo won't even
  33.     be called.  It might simplify some code depending upon how you organize it.
  34. sort_function[]:
  35.     array of function pointers that points to our family of comparison functions
  36.     (I have named them cmp_* but mult_lvl_cmp doesn't care what they're named).
  37.     This may be declared and initialized like so:
  38.        int (*sort_function[])(void* a, void* b)={&cmp_foo, &cmp_bar, &cmp_hiho};
  39.     You could also use my command line '-O' parser below.
  40.  
  41. Note that we only descend levels until the order is determined.  If we descend
  42. all levels, that means that the items are equal at all levels, so we return 0.
  43. Otherwise we return whatever the level's cmp_foo function would have returned.
  44. This allows whatever default behavior you want for cmp_foo.  sort_direction[]
  45. reverses this default behavior, but mult_lvl_cmp doesn't decide that ascending
  46. or descending is the default.  That is the job of your cmp_foo's.
  47. */
  48.  
  49. /* the only reason these are global is because qsort(3) likes it that way.
  50.    It's also a little more efficient if mult_lvl_cmp() is called many times.
  51. */
  52. extern int sort_depth;
  53. extern int sort_direction[];
  54. extern int (*sort_function[])(/* void* a, void* b */);
  55.  
  56. int mult_lvl_cmp(void* a, void* b) {
  57.     int i, cmp_val;
  58.     for(i = 0; i < sort_depth; i++) {
  59.         cmp_val = sort_direction[i] * (*sort_function[i])(a,b);
  60.         if (cmp_val != 0)
  61.             return cmp_val;
  62.     }
  63.     return 0;
  64. }
  65.  
  66. int node_mult_lvl_cmp(void* a, void* b) {
  67.     int i, cmp_val;
  68.     for(i = 0; i < sort_depth; i++) {
  69.         cmp_val = sort_direction[i] * (*sort_function[i])(&(((struct tree_node *)a)->proc),&(((struct tree_node *)b)->proc));
  70.         if (cmp_val != 0)
  71.             return cmp_val;
  72.     }
  73.     return 0;
  74. }
  75.  
  76. /* qsort(3) compliant comparison functions for all members of the ps_proc
  77.    structure (in the same order in which they appear in the struct ps_proc
  78.    declaration).
  79.    return is {-1,0,1} as {a<b, a==b, a>b}.
  80.    default ordering is ascending for all members. (flip 1,-1 to reverse)
  81. */
  82. /* pre-processor macros to cut down on source size (and typing!)
  83.    Note the use of the string concatenation operator ##
  84. */
  85. #define CMP_STR(NAME) int cmp_ ## NAME(struct ps_proc** P, struct ps_proc** Q){\
  86.     return strcmp((*P)-> ## NAME, (*Q)-> ## NAME);\
  87. }
  88.  
  89. #define CMP_INT(NAME) int cmp_ ## NAME (struct ps_proc** P, struct ps_proc** Q){\
  90.     if ((*P)-> ## NAME < (*Q)-> ## NAME) return -1; \
  91.     if ((*P)-> ## NAME > (*Q)-> ## NAME) return  1; \
  92.     return 0; \
  93. }
  94.  
  95. #define STATM_INT(NAME) int cmp_ ## NAME(struct ps_proc** P, struct ps_proc** Q){\
  96.     if ((*P)->statm. ## NAME < (*Q)->statm. ## NAME) return -1; \
  97.     if ((*P)->statm. ## NAME > (*Q)->statm. ## NAME) return  1; \
  98.     return 0; \
  99. }
  100.  
  101. /* Define the (46!) cmp_ functions with the above macros for every element
  102.    of struct ps_proc.  If the binary gets too big, we could nuke inessentials.
  103. */
  104.  
  105. CMP_STR(cmdline)
  106. CMP_STR(user)
  107. CMP_STR(cmd)
  108. /* CMP_INT(state) */
  109. /* CMP_STR(ttyc) */
  110. CMP_INT(uid)
  111. CMP_INT(pid)
  112. CMP_INT(ppid)
  113. CMP_INT(pgrp)
  114. CMP_INT(session)
  115. CMP_INT(tty)
  116. CMP_INT(tpgid)
  117. CMP_INT(utime)
  118. CMP_INT(stime)
  119. CMP_INT(cutime)
  120. CMP_INT(cstime)
  121. /* CMP_INT(counter) */
  122. CMP_INT(priority)
  123. CMP_INT(start_time)
  124. /* CMP_INT(signal) */
  125. /* CMP_INT(blocked) */
  126. /* CMP_INT(sigignore) */
  127. /* CMP_INT(sigcatch) */
  128. CMP_INT(flags)
  129. CMP_INT(min_flt)
  130. CMP_INT(cmin_flt)
  131. CMP_INT(maj_flt)
  132. CMP_INT(cmaj_flt)
  133. /* CMP_INT(timeout) */
  134. CMP_INT(vsize)
  135. CMP_INT(rss)
  136. /* CMP_INT(rss_rlim) */
  137. /* CMP_INT(start_code) */
  138. /* CMP_INT(end_code) */
  139. /* CMP_INT(start_stack) */
  140. /* CMP_INT(kstk_esp) */
  141. /* CMP_INT(kstk_eip) */
  142. /* CMP_INT(wchan) */
  143. STATM_INT(size)
  144. STATM_INT(resident)
  145. STATM_INT(share)
  146. /* STATM_INT(trs) */
  147. /* STATM_INT(lrs) */
  148. /* STATM_INT(drs) */
  149. /* STATM_INT(dt) */
  150.  
  151. /* define user interface to sort keys.  Fairly self-explanatory. */
  152.  
  153. struct cmp_fun_struct {
  154.     char letter;                           /* single option-letter for key */
  155.     char name[15];                             /* long option name for key */
  156.     int (*fun)(struct ps_proc**, struct ps_proc**);  /* pointer to cmp_key */
  157. } cmp[] = {
  158.     { 'C', "cmdline",       &cmp_cmdline       },
  159.     { 'u', "user",          &cmp_user          },
  160.     { 'c', "cmd",           &cmp_cmd           },
  161. /*  { '?', "state",         &cmp_state         }, */
  162. /*  { '?', "ttyc",          &cmp_ttyc          }, */
  163.     { 'U', "uid",           &cmp_uid           },
  164.     { 'p', "pid",           &cmp_pid           },
  165.     { 'P', "ppid",          &cmp_ppid          },
  166.     { 'g', "pgrp",          &cmp_pgrp          },
  167.     { 'o', "session",       &cmp_session       },
  168.     { 't', "tty",           &cmp_tty           },
  169.     { 'G', "tpgid",         &cmp_tpgid         },
  170.     { 'k', "utime",         &cmp_utime         },
  171.     { 'K', "stime",         &cmp_stime         },
  172.     { 'j', "cutime",        &cmp_cutime        },
  173.     { 'J', "cstime",        &cmp_cstime        },
  174. /*  { '?', "counter",       &cmp_counter       }, */
  175.     { 'y', "priority",      &cmp_priority      },
  176.     { 'T', "start_time",    &cmp_start_time    },
  177. /*  { '?', "signal",        &cmp_signal        }, */
  178. /*  { '?', "blocked",       &cmp_blocked       }, */
  179. /*  { '?', "sigignore",     &cmp_sigignore     }, */
  180. /*  { '?', "sigcatch",      &cmp_sigcatch      }, */
  181.     { 'f', "flags",         &cmp_flags         },
  182.     { 'm', "min_flt",       &cmp_min_flt       },
  183.     { 'n', "cmin_flt",      &cmp_cmin_flt      },
  184.     { 'M', "maj_flt",       &cmp_maj_flt       },
  185.     { 'N', "cmaj_flt",      &cmp_cmaj_flt      },
  186. /*  { 'C', "timeout",       &cmp_timeout       }, */
  187.     { 'v', "vsize",         &cmp_vsize         },
  188.     { 'r', "rss",           &cmp_rss           },
  189. /*  { '?', "rss_rlim",      &cmp_rss_rlim      }, */
  190. /*  { '?', "start_code",    &cmp_start_code    }, */
  191. /*  { '?', "end_code",      &cmp_end_code      }, */
  192. /*  { '?', "start_stack",   &cmp_start_stack   }, */
  193. /*  { '?', "kstk_esp",      &cmp_kstk_esp      }, */
  194. /*  { '?', "kstk_eip",      &cmp_kstk_eip      }, */
  195. /*  { '?', "wchan",         &cmp_wchan         }, */
  196.     { 's', "size",          &cmp_size          },
  197.     { 'R', "resident",      &cmp_resident      },
  198.     { 'S', "share",         &cmp_share         },
  199. /*  { '?', "trs",           &cmp_trs           }, */
  200. /*  { '?', "lrs",           &cmp_lrs           }, */
  201. /*  { '?', "drs",           &cmp_drs           }, */
  202. /*  { '?', "dt",            &cmp_dt            }, */
  203.     { '\0',"terminator",    NULL               }
  204. };
  205.  
  206. void dump_keys(void) {
  207.     int i;
  208.     for(i=0; cmp[i].letter; i++)
  209.         fprintf(stderr, "%s-O%c , --sort:%-15.15s%s",
  210.         i%2?"":"           ",
  211.         cmp[i].letter, cmp[i].name,
  212.         i%2?"\n":"");
  213.     if (i%2)
  214.       fprintf(stderr, "\n");
  215. }
  216.  
  217. /* command line option parsing.  Assign sort_{depth,direction[],function[]}
  218.    based upon a string of the form:
  219.         [+-]a[+-]b[+-]c...
  220.    with a,b,c,... being letter flags corresponding to a particular sort
  221.    key and the optional '-' specifying a reverse sort on that key.  + doesn't
  222.    mean anything, but it keeps things looking balanced...
  223. */
  224. int parse_sort_opt(char* opt) {
  225.     int i, next_dir=1;
  226.     for(; *opt ; ++opt) {
  227.         if (*opt == '-' || *opt == '+') {
  228.             if (*opt == '-')
  229.                 next_dir = -1;
  230.             opt = opt + 1;
  231.             continue;
  232.         }
  233.         for (i = 0; cmp[i].letter; i++)
  234.             if (*opt == cmp[i].letter)
  235.                 break;
  236.         if (!cmp[i].letter) {
  237.         fprintf(stderr,
  238.             "ps: no such sort key -- %c.  Possibilities are:\n", *opt);
  239.             dump_keys();
  240.             return -1;
  241.         } else {
  242. #ifdef DEBUG
  243.         fprintf(stderr,
  244.             "sort level %d: key %s, direction % d\n",
  245.             sort_depth, cmp[i].name, next_dir);
  246. #endif
  247.             sort_function[sort_depth] = cmp[i].fun;
  248.             sort_direction[sort_depth++] = next_dir;
  249.             next_dir = 1;
  250.         }
  251.     }
  252.     return 0;
  253. }
  254.  
  255. int parse_long_sort(char* opt) {
  256.     char* comma;
  257.     int i, more_keys, next_dir=1;
  258.     do {
  259.         if (*opt == '-' || *opt == '+') {
  260.             if (*opt == '-')
  261.                 next_dir = -1;
  262.             more_keys = 1;
  263.             opt = opt + 1;
  264.             continue;
  265.         }
  266.         more_keys = ((comma=index(opt,',')) != NULL);
  267.                           /* keys are ',' delimited */
  268.         if (more_keys)
  269.             *comma='\0';      /* terminate for strcmp() */
  270.         for(i = 0; cmp[i].letter; ++i)
  271.             if (strcmp(opt, cmp[i].name) == 0)
  272.                 break;
  273.         if (!cmp[i].letter) {
  274.             fprintf(stderr,
  275.             "ps: no such sort key -- %s.  Possibilities are:\n", opt);
  276.             dump_keys();
  277.             return -1;
  278.         } else {
  279. #ifdef DEBUG
  280.         fprintf(stderr,
  281.             "sort level %d: key %s, direction % d\n",
  282.             sort_depth, cmp[i].name, next_dir);
  283. #endif
  284.             sort_function[sort_depth]=cmp[i].fun;
  285.             sort_direction[sort_depth++]=next_dir;
  286.             next_dir=1;
  287.         }
  288.         opt = comma + 1; /* do next loop on next key, if more keys, else done*/
  289.     } while (more_keys);
  290.     return 0;
  291. }
  292.