1 /* top.h - Header file: show Linux processes */
3 * Copyright (c) 2002-2022, by: Jim Warner <james.warner@comcast.net
5 * This file may be used subject to the terms and conditions of the
6 * GNU Library General Public License Version 2, or any later version
7 * at your option, as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Library General Public License for more details.
13 /* For contributions to this program, the author wishes to thank:
14 * Craig Small, <csmall@dropbear.xyz>
15 * Albert D. Cahalan, <albert@users.sf.net>
16 * Sami Kerola, <kerolasa@iki.fi>
21 #include "../proc/readproc.h"
23 /* Defines represented in configure.ac ----------------------------- */
24 //#define BOOST_MEMORY /* enable extra precision in memory fields */
25 //#define BOOST_PERCNT /* enable extra precision for two % fields */
26 //#define ORIG_TOPDEFS /* with no rcfile retain original defaults */
27 //#define SIGNALS_LESS /* favor reduced signal load over response */
29 /* Development/Debugging defines ----------------------------------- */
30 //#define ATEOJ_RPTHSH /* report on hash specifics, at end-of-job */
31 //#define ATEOJ_RPTSTD /* report on some miscellany at end-of-job */
32 //#define BOT_DEAD_ZAP /* zap Ctrl bottom window when target dies */
33 //#define BOT_STRV_OFF /* don't emphasize strv w/ focus if spaces */
34 //#define CASEUP_HEXES /* show all those hex values in upper case */
35 //#define CASEUP_SUFIX /* show time/mem/cnts suffix in upper case */
36 //#define CPU_ZEROTICS /* tolerate few tics when cpu off vs. idle */
37 //#define EQUCOLHDRYES /* yes, equalize the column header lengths */
38 //#define FOCUS_HARD_Y /* 'F' will avoid topmost task distortions */
39 //#define FOCUS_TREE_X /* 'F' resets forest view indentation to 0 */
40 //#define FOCUS_VIZOFF /* 'F' doesn't provide the visual clue '|' */
41 //#define GETOPTFIX_NO /* do not address getopt_long deficiencies */
42 //#define INSP_JUSTNOT /* do not smooth unprintable right margins */
43 //#define INSP_OFFDEMO /* disable demo screens, issue msg instead */
44 //#define INSP_SAVEBUF /* preserve 'Insp_buf' contents via a file */
45 //#define INSP_SLIDE_1 /* when scrolling left/right, don't move 8 */
46 //#define MEMGRAPH_OLD /* don't use 'available' when graphing Mem */
47 //#define OFF_HST_HASH /* use BOTH qsort+bsrch vs. hashing scheme */
48 //#define OFF_NUMASKIP /* do NOT skip numa nodes if discontinuous */
49 //#define OFF_SCROLLBK /* disable tty emulators scrollback buffer */
50 //#define OFF_STDERROR /* disable our stderr buffering (redirect) */
51 //#define OFF_STDIOLBF /* disable our own stdout 'IOFBF' override */
52 //#define OFF_XTRAWIDE /* disable our extra wide multi-byte logic */
53 //#define OVERTYPE_SEE /* display a visual hint for overtype mode */
54 //#define PRETEND0NUMA /* pretend that there ain't any numa nodes */
55 //#define PRETEND2_5_X /* pretend we're linux 2.5.x (for IO-wait) */
56 //#define PRETEND48CPU /* pretend we're smp with 48 ticsers (sic) */
57 //#define PRETENDNOCAP /* pretend terminal missing essential caps */
58 //#define RCFILE_NOERR /* rcfile errs silently default, vs. fatal */
59 //#define RECALL_FIXED /* don't reorder saved strings if recalled */
60 //#define RMAN_IGNORED /* don't consider auto right margin glitch */
61 //#define SCALE_FORMER /* scale_tics() guy shouldn't mimic uptime */
62 //#define SCALE_POSTFX /* scale_tics() try without a 'h,d' suffix */
63 //#define SCROLLVAR_NO /* disable intra-column horizontal scrolls */
64 //#define SCROLLV_BY_1 /* when scrolling left/right do not move 8 */
65 //#define STRINGCASENO /* case insenstive compare/locate versions */
66 //#define TERMIOS_ONLY /* use native input only (just limp along) */
67 //#define TOG4_OFF_MEM /* don't show two abreast memory statistic */
68 //#define TOG4_OFF_SEP /* don't show two abreast visual separator */
69 //#define TREE_NORESET /* sort keys should not force 'V' view off */
70 //#define TREE_SCANALL /* rescan array w/ forest view, avoid sort */
71 //#define TREE_VALTMRK /* use an indented '+' with collapsed pids */
72 //#define TREE_VCPUOFF /* a collapsed parent excludes child's cpu */
73 //#define TREE_VPROMPT /* pid collapse/expand prompt, vs. top row */
74 //#define TREE_VWINALL /* pid collapse/expand impacts all windows */
75 //#define USE_X_COLHDR /* emphasize header vs. whole col, for 'x' */
76 //#define VALIDATE_NLS /* ensure the integrity of four nls tables */
77 //#define WIDEN_COLUMN /* base column widths on translated header */
80 /*###### Notes, etc. ###################################################*/
82 /* The following convention is used to identify those areas where
83 adaptations for hotplugging are to be found ...
84 *** hotplug_acclimated ***
85 ( hopefully libproc will also be supportive of our efforts ) */
87 /* For introducing inaugural cgroup support, thanks to:
88 Jan Gorig <jgorig@redhat.com> - April, 2011 */
90 /* For the motivation and path to nls support, thanks to:
91 Sami Kerola, <kerolasa@iki.fi> - December, 2011 */
93 /* There are still some short strings that may yet be candidates
94 for nls support inclusion. They're identified with:
97 /* For initiating the topic of potential % CPU distortions due to
98 to kernel and/or cpu anomalies (see CPU_ZEROTICS), thanks to:
99 Jaromir Capik, <jcapik@redhat.com> - February, 2012 */
101 /* For the impetus and NUMA/Node prototype design, thanks to:
102 Lance Shelton <LShelton@fusionio.com> - April, 2013 */
104 /* For prompting & helping with top's utf-8 support, thanks to:
105 Göran Uddeborg <goeran@uddeborg.se> - September, 2017 */
107 // pretend as if #define _GNU_SOURCE
108 char *strcasestr(const char *haystack, const char *needle);
111 #define STRSTR strcasestr
112 #define STRCMP strcasecmp
114 #define STRSTR strstr
115 #define STRCMP strcmp
119 /*###### Some Miscellaneous constants ##################################*/
121 /* The default delay twix updates */
123 #define DEF_DELAY 3.0
125 #define DEF_DELAY 1.5
128 /* Length of time a message is displayed and the duration
129 of a 'priming' wait during library startup (in microseconds) */
130 #define MSG_USLEEP 1250000
131 #define LIB_USLEEP 150000
133 /* Specific process id monitoring support (command line only) */
136 /* Output override minimums (the -w switch and/or env vars) */
140 /* Miscellaneous buffers with liberal values and some other defines
141 -- mostly just to pinpoint source code usage/dependancies */
142 #define SCREENMAX 512
143 /* the above might seem pretty stingy, until you consider that with every
144 field displayed the column header would be approximately 250 bytes
145 -- so SCREENMAX provides for all fields plus a 250+ byte command line */
148 #define PFLAGSSIZ 100
149 #define SMLBUFSIZ 128
150 #define MEDBUFSIZ 256
151 #define LRGBUFSIZ 512
152 #define OURPATHSZ 1024
153 #define BIGBUFSIZ 2048
154 #define BOTBUFSIZ 16384
155 // next is same as library's max buffer size
156 #define MAXBUFSIZ (1024*64*2)
157 /* in addition to the actual display data, our row might have to accommodate
158 many termcap/color transitions - these definitions ensure we have room */
159 #define ROWMINSIZ ( SCREENMAX + 8 * (CAPBUFSIZ + CLRBUFSIZ) )
160 #define ROWMAXSIZ ( SCREENMAX + 16 * (CAPBUFSIZ + CLRBUFSIZ) )
161 // minimum size guarantee for dynamically acquired 'readfile' buffer
162 #define READMINSZ 2048
163 // size of preallocated search string buffers, same as ioline()
164 #define FNDBUFSIZ MEDBUFSIZ
167 // space between task fields/columns
168 #define COLPADSTR " "
169 #define COLPADSIZ ( sizeof(COLPADSTR) - 1 )
170 // continuation ch when field/column truncated
171 #define COLPLUSCH '+'
173 // support for keyboard stuff (cursor motion keystrokes, mostly)
174 #define kbd_ESC '\033'
175 #define kbd_SPACE ' '
176 #define kbd_ENTER '\n'
180 #define kbd_RIGHT 132
189 #define kbd_CtrlE '\005'
190 #define kbd_CtrlG '\007'
191 #define kbd_CtrlI '\011'
192 #define kbd_CtrlK '\013'
193 #define kbd_CtrlL '\014'
194 #define kbd_CtrlN '\016'
195 #define kbd_CtrlO '\017'
196 #define kbd_CtrlP '\020'
197 #define kbd_CtrlU '\025'
199 /* Special value in Pseudo_row to force an additional procs refresh
200 -- used at startup and for task/thread mode transitions */
204 /* This is the % used in establishing the tics threshold below
205 which a cpu is treated as 'idle' rather than displaying
206 misleading state percentages */
211 /* ##### Enum's and Typedef's ############################################ */
213 /* Flags for each possible field (and then some) --
214 these MUST be kept in sync with the FLD_t Fieldstab[] array !! */
217 EU_UED, EU_UEN, EU_URD, EU_URN, EU_USD, EU_USN,
218 EU_GID, EU_GRP, EU_PGD, EU_TTY, EU_TPG, EU_SID,
219 EU_PRI, EU_NCE, EU_THD,
220 EU_CPN, EU_CPU, EU_TME, EU_TM2,
221 EU_MEM, EU_VRT, EU_SWP, EU_RES, EU_COD, EU_DAT, EU_SHR,
222 EU_FL1, EU_FL2, EU_DRT,
223 EU_STA, EU_CMD, EU_WCH, EU_FLG, EU_CGR,
224 EU_SGD, EU_SGN, EU_TGD,
229 EU_NS1, EU_NS2, EU_NS3, EU_NS4, EU_NS5, EU_NS6,
231 EU_RZA, EU_RZF, EU_RZL, EU_RZS,
236 // not really pflags, used with tbl indexing
239 // not really pflags, used with tbl indexing & col highlighting
240 EU_MAXPFLGS, EU_XON, EU_XOF
244 /* The scaling 'target' used with memory fields */
246 SK_Kb, SK_Mb, SK_Gb, SK_Tb, SK_Pb, SK_Eb
249 /* Used to manipulate (and document) the Frames_signal states */
251 BREAK_off = 0, BREAK_kbd, BREAK_sig, BREAK_autox, BREAK_screen
254 /* This typedef just ensures consistent 'process flags' handling */
255 typedef unsigned char FLG_t;
257 /* These typedefs attempt to ensure consistent 'ticks' handling */
258 typedef unsigned long long TIC_t;
259 typedef long long SIC_t;
261 /* Sort support, callback function signature */
262 typedef int (*QFP_t)(const void *, const void *);
264 /* This structure consolidates the information that's used
265 in a variety of display roles. */
266 typedef struct FLD_t {
267 int width; // field width, if applicable
268 int scale; // scaled target, if applicable
269 const int align; // the default column alignment flag
270 const QFP_t sort; // sort function
271 const int lflg; // PROC_FILLxxx flag(s) needed by this field
275 /* This structure supports 'history' processing and records the
276 bare minimum of needed information from one frame to the next --
277 we don't calc and save data that goes unused like the old top. */
278 typedef struct HST_t {
279 TIC_t tics; // last frame's tics count
280 unsigned long maj, min; // last frame's maj/min_flt counts
281 int pid; // record 'key'
284 /* This structure supports 'history' processing and records the bare
285 minimum of needed information from one frame to the next -- we do
286 not calc and save data that goes unused like the old top nor will
287 we incur the additional overhead of sort to support binary search
288 (or worse, a friggin' for loop) when retrievals become necessary! */
289 typedef struct HST_t {
290 TIC_t tics; // last frame's tics count
291 unsigned long maj, min; // last frame's maj/min_flt counts
292 int pid; // record 'key'
293 int lnk; // next on hash chain
297 /* These 2 structures store a frame's cpu tics used in history
298 calculations. They exist primarily for SMP support but serve
300 typedef struct CT_t {
301 /* other kernels: u == user/us, n == nice/ni, s == system/sy, i == idle/id
302 2.5.41 kernel: w == IO-wait/wa (io wait time)
303 2.6.0 kernel: x == hi (hardware irq time), y == si (software irq time)
304 2.6.11 kernel: z == st (virtual steal time) */
305 TIC_t u, n, s, i, w, x, y, z; // as represented in /proc/stat
307 SIC_t tot; // total from /proc/stat line 1
311 typedef struct CPU_t {
312 CT_t cur; // current frame's cpu tics
313 CT_t sav; // prior frame's cpu tics
315 SIC_t edge; // tics adjustment threshold boundary
317 int id; // cpu number (0 - nn), or numa active flag
318 int node; // the numa node it belongs to
321 /* /////////////////////////////////////////////////////////////// */
322 /* Special Section: multiple windows/field groups --------------- */
323 /* ( kind of a header within a header: constants, types & macros ) */
325 #define CAPTABMAX 9 /* max entries in each win's caps table */
326 #define GROUPSMAX 4 /* the max number of simultaneous windows */
327 #define WINNAMSIZ 4 /* size of RCW_t winname buf (incl '\0') */
328 #define GRPNAMSIZ WINNAMSIZ+2 /* window's name + number as in: '#:...' */
330 /* The Persistent 'Mode' flags!
331 These are preserved in the rc file, as a single integer and the
332 letter shown is the corresponding 'command' toggle */
333 // 'View_' flags affect the summary (minimum), taken from 'Curwin'
334 #define View_CPUSUM 0x008000 // '1' - show combined cpu stats (vs. each)
335 #define View_CPUNOD 0x400000 // '2' - show numa node cpu stats ('3' also)
336 #define View_LOADAV 0x004000 // 'l' - display load avg and uptime summary
337 #define View_STATES 0x002000 // 't' - display task/cpu(s) states summary
338 #define View_MEMORY 0x001000 // 'm' - display memory summary
339 #define View_NOBOLD 0x000008 // 'B' - disable 'bold' attribute globally
340 #define View_SCROLL 0x080000 // 'C' - enable coordinates msg w/ scrolling
341 // 'Show_' & 'Qsrt_' flags are for task display in a visible window
342 #define Show_COLORS 0x000800 // 'z' - show in color (vs. mono)
343 #define Show_HIBOLD 0x000400 // 'b' - rows and/or cols bold (vs. reverse)
344 #define Show_HICOLS 0x000200 // 'x' - show sort column emphasized
345 #define Show_HIROWS 0x000100 // 'y' - show running tasks highlighted
346 #define Show_CMDLIN 0x000080 // 'c' - show cmdline vs. name
347 #define Show_CTIMES 0x000040 // 'S' - show times as cumulative
348 #define Show_IDLEPS 0x000020 // 'i' - show idle processes (all tasks)
349 #define Show_TASKON 0x000010 // '-' - tasks showable when Mode_altscr
350 #define Show_FOREST 0x000002 // 'V' - show cmd/cmdlines with ascii art
351 #define Qsrt_NORMAL 0x000004 // 'R' - reversed column sort (high to low)
352 #define Show_JRSTRS 0x040000 // 'j' - right justify "string" data cols
353 #define Show_JRNUMS 0x020000 // 'J' - right justify "numeric" data cols
354 // these flag(s) have no command as such - they're for internal use
355 #define NOPRINT_xxx 0x010000 // build task rows only (not for display)
356 #define EQUWINS_xxx 0x000001 // rebalance all wins & tasks (off i,n,u/U)
358 // Default flags if there's no rcfile to provide user customizations
360 #define DEF_WINFLGS ( View_LOADAV | View_STATES | View_CPUSUM | View_MEMORY \
361 | Show_HIBOLD | Show_HIROWS | Show_IDLEPS | Show_TASKON | Show_JRNUMS \
363 #define DEF_GRAPHS2 0, 0
364 #define DEF_SCALES2 SK_Mb, SK_Kb
365 #define ALT_WINFLGS DEF_WINFLGS
366 #define ALT_GRAPHS2 0, 0
368 #define DEF_WINFLGS ( View_LOADAV | View_STATES | View_MEMORY | Show_CMDLIN \
369 | Show_COLORS | Show_FOREST | Show_HIROWS | Show_IDLEPS | Show_JRNUMS | Show_TASKON \
371 #define DEF_GRAPHS2 1, 2
372 #define DEF_SCALES2 SK_Gb, SK_Mb
373 #define ALT_WINFLGS (DEF_WINFLGS | Show_HIBOLD) & ~Show_FOREST
374 #define ALT_GRAPHS2 2, 0
377 /* These are used to direct wins_reflag */
379 Flags_TOG, Flags_SET, Flags_OFF
382 /* These are used to direct win_warn */
387 /* This type helps support both a window AND the rcfile */
388 typedef struct RCW_t { // the 'window' portion of an rcfile
389 int sortindx, // sort field (represented as procflag)
390 winflags, // 'view', 'show' and 'sort' mode flags
391 maxtasks, // user requested maximum, 0 equals all
392 graph_cpus, // 't' - View_STATES supplementary vals
393 graph_mems, // 'm' - View_MEMORY supplememtary vals
394 double_up, // '4' - show multiple cpus on one line
395 combine_cpus, // '!' - keep combining additional cpus
396 summclr, // a colors 'number' used for summ info
397 msgsclr, // " in msgs/pmts
398 headclr, // " in cols head
399 taskclr; // " in task rows
400 char winname [WINNAMSIZ], // name for the window, user changeable
401 fieldscur [PFLAGSSIZ]; // the fields for display & their order
404 /* This represents the complete rcfile */
405 typedef struct RCF_t {
406 char id; // rcfile version id
407 int mode_altscr; // 'A' - Alt display mode (multi task windows)
408 int mode_irixps; // 'I' - Irix vs. Solaris mode (SMP-only)
409 float delay_time; // 'd'/'s' - How long to sleep twixt updates
410 int win_index; // Curwin, as index
411 RCW_t win [GROUPSMAX]; // a 'WIN_t.rc' for each window
412 int fixed_widest; // 'X' - wider non-scalable col addition
413 int summ_mscale; // 'E' - scaling of summary memory values
414 int task_mscale; // 'e' - scaling of process memory values
415 int zero_suppress; // '0' - suppress scaled zeros toggle
416 int tics_scaled; // ^E - scale TIME and/or TIME+ columns
419 /* This structure stores configurable information for each window.
420 By expending a little effort in its creation and user requested
421 maintenance, the only real additional per frame cost of having
422 windows is an extra sort -- but that's just on pointers! */
423 typedef struct WIN_t {
424 FLG_t pflgsall [PFLAGSSIZ], // all 'active/on' fieldscur, as enum
425 procflgs [PFLAGSSIZ]; // fieldscur subset, as enum
426 RCW_t rc; // stuff that gets saved in the rcfile
427 int winnum, // a window's number (array pos + 1)
428 winlines, // current task window's rows (volatile)
429 maxpflgs, // number of displayed procflgs ("on" in fieldscur)
430 totpflgs, // total of displayable procflgs in pflgsall array
431 begpflg, // scrolled beginning pos into pflgsall array
432 endpflg, // scrolled ending pos into pflgsall array
433 begtask, // scrolled beginning pos into Frame_maxtask
434 begnext, // new scrolled delta for next frame's begtask
436 varcolbeg, // scrolled position within variable width col
438 varcolsz, // max length of variable width column(s)
439 usrseluid, // validated uid for 'u/U' user selection
440 usrseltyp, // the basis for matching above uid
441 usrselflg, // flag denoting include/exclude matches
442 hdrcaplen; // column header xtra caps len, if any
443 char capclr_sum [CLRBUFSIZ], // terminfo strings built from
444 capclr_msg [CLRBUFSIZ], // RCW_t colors (& rebuilt too),
445 capclr_pmt [CLRBUFSIZ], // but NO recurring costs !
446 capclr_hdr [CLRBUFSIZ], // note: sum, msg and pmt strs
447 capclr_rowhigh [SMLBUFSIZ], // are only used when this
448 capclr_rownorm [CLRBUFSIZ], // window is the 'Curwin'!
449 cap_bold [CAPBUFSIZ], // support for View_NOBOLD toggle
450 grpname [GRPNAMSIZ], // window number:name, printable
452 columnhdr [ROWMINSIZ], // column headings for procflgs
454 columnhdr [SCREENMAX], // column headings for procflgs
456 *captab [CAPTABMAX]; // captab needed by show_special()
457 struct osel_s *osel_1st; // other selection criteria anchor
458 int osel_tot; // total of other selection criteria
459 char *findstr; // window's current/active search string
460 int findlen; // above's strlen, without call overhead
461 int focus_pid; // target pid when 'F' toggle is active
462 int focus_beg; // ppt index where 'F' toggle has begun
463 int focus_end; // ppt index where 'F' toggle has ended
465 int focus_lvl; // the indentation level of parent task
467 proc_t **ppt; // this window's proc_t ptr array
468 struct WIN_t *next, // next window in window stack
469 *prev; // prior window in window stack
472 // Used to test/manipulate the window flags
473 #define CHKw(q,f) (int)((q)->rc.winflags & (f))
474 #define TOGw(q,f) (q)->rc.winflags ^= (f)
475 #define SETw(q,f) (q)->rc.winflags |= (f)
476 #define OFFw(q,f) (q)->rc.winflags &= ~(f)
477 #define ALTCHKw (Rc.mode_altscr ? 1 : win_warn(Warn_ALT))
478 #define VIZISw(q) (!Rc.mode_altscr || CHKw(q,Show_TASKON))
479 #define VIZCHKw(q) (VIZISw(q)) ? 1 : win_warn(Warn_VIZ)
480 #define VIZTOGw(q,f) (VIZISw(q)) ? TOGw(q,(f)) : win_warn(Warn_VIZ)
482 // Used to test/manipulte fieldscur values
483 #define FLDon(c) ((c) |= 0x80)
484 #define FLDget(q,i) ((FLG_t)((q)->rc.fieldscur[i] & 0x7f) - FLD_OFFSET)
485 #define FLDtog(q,i) ((q)->rc.fieldscur[i] ^= 0x80)
486 #define FLDviz(q,i) ((q)->rc.fieldscur[i] & 0x80)
487 #define ENUviz(w,E) (NULL != memchr((w)->procflgs, E, (w)->maxpflgs))
488 #define ENUpos(w,E) ((int)((FLG_t*)memchr((w)->pflgsall, E, (w)->totpflgs) - (w)->pflgsall))
490 // Support for variable width columns (and potentially scrolling too)
491 #define VARcol(E) (-1 == Fieldstab[E].width)
494 #define VARright(w) (1 == w->maxpflgs && VARcol(w->procflgs[0]))
496 #define VARright(w) ((1 == w->maxpflgs && VARcol(w->procflgs[0])) || \
497 (3 == w->maxpflgs && EU_XON == w->procflgs[0] && VARcol(w->procflgs[1])))
499 #define VARleft(w) (w->varcolbeg && VARright(w))
507 // Support for a proper (visible) row #1 whenever Curwin changes
508 // ( or a key which might affect vertical scrolling was struck )
509 #define mkVIZyes ( Curwin->begnext != 0 )
510 #define mkVIZrow1 { Curwin->begnext = +1; Curwin->begtask -= 1; }
511 #define mkVIZrowX(n) { Curwin->begnext = (n); }
512 #define mkVIZoff(w) { w->begnext = 0; }
514 /* Special Section: end ------------------------------------------ */
515 /* /////////////////////////////////////////////////////////////// */
518 /*###### Some Miscellaneous Macro definitions ##########################*/
520 /* Yield table size as 'int' */
521 #define MAXTBL(t) (int)(sizeof(t) / sizeof(t[0]))
523 /* A null-terminating strncpy, assuming strlcpy is not available.
524 ( and assuming callers don't need the string length returned ) */
525 #define STRLCPY(dst,src) { memccpy(dst, src, '\0', sizeof(dst)); dst[sizeof(dst) - 1] = '\0'; }
527 /* Used to clear all or part of our Pseudo_screen */
528 #define PSU_CLREOS(y) memset(&Pseudo_screen[ROWMAXSIZ*y], '\0', Pseudo_size-(ROWMAXSIZ*y))
530 /* Used as return arguments in *some* of the sort callbacks */
531 #define SORT_lt ( Frame_srtflg > 0 ? 1 : -1 )
532 #define SORT_gt ( Frame_srtflg > 0 ? -1 : 1 )
535 /* Used to create *most* of the sort callback functions
536 note: some of the callbacks are NOT your father's callbacks, they're
537 highly optimized to save them ol' precious cycles! */
538 #define SCB_NAME(f) sort_EU_ ## f
539 #define SCB_NUM1(f,n) \
540 static int SCB_NAME(f) (const proc_t **P, const proc_t **Q) { \
541 if ( (*P)->n < (*Q)->n ) return SORT_lt; \
542 if ( (*P)->n > (*Q)->n ) return SORT_gt; \
544 #define SCB_NUM2(f,n1,n2) \
545 static int SCB_NAME(f) (const proc_t **P, const proc_t **Q) { \
546 if ( ((*P)->n1+(*P)->n2) < ((*Q)->n1+(*Q)->n2) ) return SORT_lt; \
547 if ( ((*P)->n1+(*P)->n2) > ((*Q)->n1+(*Q)->n2) ) return SORT_gt; \
549 #define SCB_NUMx(f,n) \
550 static int SCB_NAME(f) (const proc_t **P, const proc_t **Q) { \
551 return Frame_srtflg * ( (*Q)->n - (*P)->n ); }
552 #define SCB_STRS(f,s) \
553 static int SCB_NAME(f) (const proc_t **P, const proc_t **Q) { \
554 if (!(*P)->s || !(*Q)->s) return SORT_eq; \
555 return Frame_srtflg * STRCMP((*Q)->s, (*P)->s); }
556 #define SCB_STRV(f,b,v,s) \
557 static int SCB_NAME(f) (const proc_t **P, const proc_t **Q) { \
559 if (!(*P)->v || !(*Q)->v) return SORT_eq; \
560 return Frame_srtflg * STRCMP((*Q)->v[0], (*P)->v[0]); } \
561 return Frame_srtflg * STRCMP((*Q)->s, (*P)->s); }
562 #define SCB_STRX(f,s) \
563 int strverscmp(const char *s1, const char *s2); \
564 static int SCB_NAME(f) (const proc_t **P, const proc_t **Q) { \
565 if (!(*P)->s || !(*Q)->s) return SORT_eq; \
566 return Frame_srtflg * strverscmp((*Q)->s, (*P)->s); }
569 * The following three macros are used to 'inline' those portions of the
570 * display process involved in formatting, while protecting against any
571 * potential embedded 'millesecond delay' escape sequences.
573 /** PUTT - Put to Tty (used in many places)
574 . for temporary, possibly interactive, 'replacement' output
575 . may contain ANY valid terminfo escape sequences
576 . need NOT represent an entire screen row */
577 #define PUTT(fmt,arg...) do { \
578 char _str[ROWMAXSIZ]; \
579 snprintf(_str, sizeof(_str), fmt, ## arg); \
583 /** PUFF - Put for Frame (used in only 3 places)
584 . for more permanent frame-oriented 'update' output
585 . may NOT contain cursor motion terminfo escapes
586 . assumed to represent a complete screen ROW
587 . subject to optimization, thus MAY be discarded */
588 #define PUFF(fmt,arg...) do { \
589 char _str[ROWMAXSIZ]; \
590 const int _len = snprintf(_str, sizeof(_str), fmt, ## arg); \
592 char *_eol = _str + (_len < 0 ? 0 : (size_t)_len >= sizeof(_str) ? sizeof(_str)-1 : (size_t)_len); \
593 while (_eol > _str && _eol[-1] == ' ') _eol--; \
594 *_eol = '\0'; putp(_str); } \
595 else if (Pseudo_row >= 0 && Pseudo_row < Screen_rows) { \
596 char *_ptr = &Pseudo_screen[Pseudo_row++ * ROWMAXSIZ]; \
597 if (!strcmp(_ptr, _str)) putp("\n"); \
599 strcpy(_ptr, _str); \
603 /** POOF - Pulled Out of Frame (used in only 1 place)
604 . for output that is/was sent directly to the terminal
605 but would otherwise have been counted as a Pseudo_row */
606 #define POOF(str,cap) do { \
607 putp(str); putp(cap); \
608 Pseudo_screen[Pseudo_row * ROWMAXSIZ] = '\0'; \
609 if (Pseudo_row + 1 < Screen_rows) ++Pseudo_row; \
612 /* Orderly end, with any sort of message - see fmtmk */
613 #define debug_END(s) { \
614 void error_exit (const char *); \
615 fputs(Cap_clr_scr, stdout); \
619 /* A poor man's breakpoint, if he's too lazy to learn gdb */
620 #define its_YOUR_fault { *((char *)0) = '!'; }
623 /*###### Some Display Support *Data* ###################################*/
624 /* ( see module top_nls.c for the nls translatable data ) */
626 /* Configuration files support */
627 #define SYS_RCRESTRICT "/etc/toprc"
628 #define SYS_RCDEFAULTS "/etc/topdefaultrc"
629 #define RCF_EYECATCHER "Config File (Linux processes with windows)\n"
630 #define RCF_PLUS_H "\\]^_`abcdefghij"
631 #define RCF_PLUS_J "klmnopqrstuvwxyz"
632 #define RCF_VERSION_ID 'k'
634 /* The default fields displayed and their order, if nothing is
635 specified by the loser, oops user.
636 note: any *contiguous* ascii sequence can serve as fieldscur
637 characters as long as the initial value is coordinated
638 with that specified for FLD_OFFSET
639 ( we're providing for up to 86 fields currently, )
640 ( with just one escaped value, the '\' character ) */
641 #define FLD_OFFSET '%'
642 // seq_fields "%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz"
644 #define DEF_FIELDS "¥¨³´»½ÀÄ·º¹Å&')*+,-./012568<>?ABCFGHIJKLMNOPQRSTUVWXYZ[" RCF_PLUS_H RCF_PLUS_J
646 #define DEF_FIELDS "¥&K¨³´»½@·º¹56ÄFÅ')*+,-./0128<>?ABCGHIJLMNOPQRSTUVWXYZ[" RCF_PLUS_H RCF_PLUS_J
648 /* Pre-configured windows/field groups */
649 #define JOB_FIELDS "¥¦¹·º(³´Ä»½@<§Å)*+,-./012568>?ABCFGHIJKLMNOPQRSTUVWXYZ[" RCF_PLUS_H RCF_PLUS_J
650 #define MEM_FIELDS "¥º»<½¾¿ÀÁMBNÃD34·Å&'()*+,-./0125689FGHIJKLOPQRSTUVWXYZ[" RCF_PLUS_H RCF_PLUS_J
651 #define USR_FIELDS "¥¦§¨ª°¹·ºÄÅ)+,-./1234568;<=>?@ABCFGHIJKLMNOPQRSTUVWXYZ[" RCF_PLUS_H RCF_PLUS_J
652 // old top fields ( 'a'-'z' ) in positions 0-25
653 // other suse old top fields ( '{|' ) in positions 26-27
654 #define CVT_FIELDS "%&*'(-0346789:;<=>?@ACDEFGML)+,./125BHIJKNOPQRSTUVWXYZ["
655 #define CVT_FLDMAX 28
658 /* The default values for the local config file */
659 #define DEF_RCFILE { \
660 RCF_VERSION_ID, 0, 1, DEF_DELAY, 0, { \
661 { EU_CPU, DEF_WINFLGS, 0, DEF_GRAPHS2, 1, 0, \
662 COLOR_RED, COLOR_RED, COLOR_YELLOW, COLOR_RED, \
663 "Def", DEF_FIELDS }, \
664 { EU_PID, ALT_WINFLGS, 0, ALT_GRAPHS2, 0, 0, \
665 COLOR_CYAN, COLOR_CYAN, COLOR_WHITE, COLOR_CYAN, \
666 "Job", JOB_FIELDS }, \
667 { EU_MEM, ALT_WINFLGS, 0, ALT_GRAPHS2, 0, 0, \
668 COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLUE, COLOR_MAGENTA, \
669 "Mem", MEM_FIELDS }, \
670 { EU_UEN, ALT_WINFLGS, 0, ALT_GRAPHS2, 0, 0, \
671 COLOR_YELLOW, COLOR_YELLOW, COLOR_GREEN, COLOR_YELLOW, \
672 "Usr", USR_FIELDS } \
673 }, 0, DEF_SCALES2, 0, 0 }
675 /* Summary Lines specially formatted string(s) --
676 see 'show_special' for syntax details + other cautions. */
677 #define LOADAV_line "%s -%s\n"
678 #define LOADAV_line_alt "%s~6 -%s\n"
681 /*###### For Piece of mind #############################################*/
683 /* just sanity check(s)... */
684 #if defined(ATEOJ_RPTHSH) && defined(OFF_HST_HASH)
685 # error 'ATEOJ_RPTHSH' conflicts with 'OFF_HST_HASH'
687 #if defined(RECALL_FIXED) && defined(TERMIOS_ONLY)
688 # error 'RECALL_FIXED' conflicts with 'TERMIOS_ONLY'
690 #if (LRGBUFSIZ < SCREENMAX)
691 # error 'LRGBUFSIZ' must NOT be less than 'SCREENMAX'
693 #if defined(TERMIOS_ONLY)
694 # warning 'TERMIOS_ONLY' disables input recall and makes man doc incorrect
696 #if defined(MEMGRAPH_OLD)
697 # warning 'MEMGRAPH_OLD' will make the man document Section 2c. misleading
699 #if defined(SCALE_FORMER) && defined(SCALE_POSTFX)
700 # warning 'SCALE_POSTFX' is ignored when 'SCALE_FORMER' is active
703 /*###### Some Prototypes (ha!) #########################################*/
705 /* These 'prototypes' are here exclusively for documentation purposes. */
706 /* ( see the find_string function for the one true required protoype ) */
707 /*------ Sort callbacks ------------------------------------------------*/
708 /* for each possible field, in the form of: */
709 /*atic int sort_EU_XXX (const proc_t **P, const proc_t **Q); */
710 /*------ Tiny useful routine(s) ----------------------------------------*/
711 //atic const char *fmtmk (const char *fmts, ...);
712 //atic inline char *scat (char *dst, const char *src);
713 //atic const char *tg2 (int x, int y);
714 /*------ Exit/Interrput routines ---------------------------------------*/
715 //atic void at_eoj (void);
716 //atic void bye_bye (const char *str);
717 //atic void error_exit (const char *str);
718 //atic void sig_abexit (int sig);
719 //atic void sig_endpgm (int dont_care_sig);
720 //atic void sig_paused (int dont_care_sig);
721 //atic void sig_resize (int dont_care_sig);
722 //atic void xalloc_our_handler (const char *fmts, ...);
723 /*------ Special UTF-8 Multi-Byte support ------------------------------*/
724 /*atic char UTF8_tab[] = { ... } */
725 //atic inline int utf8_cols (const unsigned char *p, int n);
726 //atic int utf8_delta (const char *str);
727 //atic int utf8_embody (const char *str, int width);
728 //atic const char *utf8_justify (const char *str, int width, int justr);
729 //atic int utf8_proper_col (const char *str, int col, int tophysical);
730 /*------ Misc Color/Display support ------------------------------------*/
731 //atic void capsmk (WIN_t *q);
732 //atic void show_msg (const char *str);
733 //atic int show_pmt (const char *str);
734 //atic void show_scroll (void);
735 //atic void show_special (int interact, const char *glob);
736 /*------ Low Level Memory/Keyboard/File I/O support --------------------*/
737 //atic void *alloc_c (size_t num);
738 //atic void *alloc_r (void *ptr, size_t num);
739 //atic char *alloc_s (const char *str);
740 //atic inline int ioa (struct timespec *ts);
741 //atic int ioch (int ech, char *buf, unsigned cnt);
742 //atic int iokey (int action);
743 //atic char *ioline (const char *prompt);
744 //atic int mkfloat (const char *str, float *num, int whole);
745 //atic int readfile (FILE *fp, char **baddr, size_t *bsize, size_t *bread);
746 /*------ Small Utility routines ----------------------------------------*/
747 //atic float get_float (const char *prompt);
748 //atic int get_int (const char *prompt);
749 //atic inline const char *hex_make (KLONG num, int noz);
750 //atic const char *user_certify (WIN_t *q, const char *str, char typ);
751 /*------ Basic Formatting support --------------------------------------*/
752 //atic inline const char *justify_pad (const char *str, int width, int justr);
753 //atic inline const char *make_chr (const char ch, int width, int justr);
754 //atic inline const char *make_num (long num, int width, int justr, int col, int noz);
755 //atic inline const char *make_str (const char *str, int width, int justr, int col);
756 //atic inline const char *make_str_utf8 (const char *str, int width, int justr, int col);
757 //atic const char *scale_mem (int target, float num, int width, int justr);
758 //atic const char *scale_num (float num, int width, int justr);
759 //atic const char *scale_pcnt (float num, int width, int justr, int xtra);
760 //atic const char *scale_tics (TIC_t tics, int width, int justr, int target);
761 /*------ Fields Management support -------------------------------------*/
762 /*atic FLD_t Fieldstab[] = { ... } */
763 //atic void adj_geometry (void);
764 //atic void build_headers (void);
765 //atic void calibrate_fields (void);
766 //atic void display_fields (int focus, int extend);
767 //atic void fields_utility (void);
768 //atic inline void widths_resize (void);
769 //atic void zap_fieldstab (void);
770 /*------ Library Interface ---------------------------------------------*/
771 //atic void cpus_refresh (void);
773 //atic inline HST_t *hstbsrch (HST_t *hst, int max, int pid);
775 //atic inline HST_t *hstget (int pid);
776 //atic inline void hstput (unsigned idx);
778 //atic void procs_hlp (proc_t *p);
779 //atic void procs_refresh (void);
780 //atic void sysinfo_refresh (int forced);
781 /*------ Inspect Other Output ------------------------------------------*/
782 //atic void insp_cnt_nl (void);
784 //atic void insp_do_demo (char *fmts, int pid);
786 //atic void insp_do_file (char *fmts, int pid);
787 //atic void insp_do_pipe (char *fmts, int pid);
788 //atic inline int insp_find_ofs (int col, int row);
789 //atic void insp_find_str (int ch, int *col, int *row);
790 //atic void insp_mkrow_raw (int col, int row);
791 //atic void insp_mkrow_utf8 (int col, int row);
792 //atic void insp_show_pgs (int col, int row, int max);
793 //atic int insp_view_choice (proc_t *p);
794 //atic void inspection_utility (int pid);
795 /*------ Other Filtering ------------------------------------------------*/
796 //atic const char *osel_add (WIN_t *q, int ch, char *glob, int push);
797 //atic void osel_clear (WIN_t *q);
798 //atic inline int osel_matched (const WIN_t *q, FLG_t enu, const char *str);
799 /*------ Startup routines ----------------------------------------------*/
800 //atic void before (char *me);
801 //atic int config_cvt (WIN_t *q);
802 //atic int config_insp (FILE *fp, char *buf, size_t size);
803 //atic int config_osel (FILE *fp, char *buf, size_t size);
804 //atic const char *configs_file (FILE *fp, const char *name, float *delay);
805 //atic int configs_path (const char *const fmts, ...);
806 //atic void configs_reads (void);
807 //atic void parse_args (int argc, char **argv);
808 //atic void signals_set (void);
809 //atic void whack_terminal (void);
810 /*------ Windows/Field Groups support ----------------------------------*/
811 //atic void win_names (WIN_t *q, const char *name);
812 //atic void win_reset (WIN_t *q);
813 //atic WIN_t *win_select (int ch);
814 //atic int win_warn (int what);
815 //atic void wins_clrhlp (WIN_t *q, int save);
816 //atic void wins_colors (void);
817 //atic void wins_reflag (int what, int flg);
818 //atic void wins_stage_1 (void);
819 //atic void wins_stage_2 (void);
820 //atic inline int wins_usrselect (const WIN_t *q, const int idx);
821 /*------ Forest View support -------------------------------------------*/
822 //atic void forest_adds (const int self, int level);
824 //atic int forest_based (const proc_t **x, const proc_t **y);
826 //atic void forest_create (WIN_t *q);
827 //atic inline const char *forest_display (const WIN_t *q, int idx);
828 //atic void forest_excluded (WIN_t *q);
829 /*------ Special Separate Bottom Window support ------------------------*/
830 //atic void bot_do (const char *str, int focus);
831 //atic int bot_focus_str (const char *hdr, const char *str);
832 //atic int bot_focus_strv (const char *hdr, const char **strv);
833 //atic void *bot_item_hlp (proc_t *p);
834 //atic void bot_item_show (void);
835 //atic void bot_item_toggle (int what, const char *head, char sep);
836 /*------ Interactive Input Tertiary support ----------------------------*/
837 //atic inline int find_ofs (const WIN_t *q, const char *buf);
838 //atic void find_string (int ch);
839 //atic void help_view (void);
840 //atic void other_filters (int ch);
841 //atic void write_rcfile (void);
842 /*------ Interactive Input Secondary support (do_key helpers) ----------*/
843 //atic void keys_global (int ch);
844 //atic void keys_summary (int ch);
845 //atic void keys_task (int ch);
846 //atic void keys_window (int ch);
847 //atic void keys_xtra (int ch);
848 /*------ Tertiary summary display support (summary_show helpers) -------*/
849 //atic struct rx_st *sum_rx (long total, long part1, long part2, int style);
850 //atic inline int sum_see (const char *str, int nobuf);
851 //atic int sum_tics (CPU_t *cpu, const char *pfx, int nobuf);
852 //atic int sum_unify (CPU_t *cpu, int nobuf);
853 /*------ Secondary summary display support (summary_show helpers) ------*/
854 //atic void do_cpus (void);
855 //atic void do_memory (void);
856 /*------ Main Screen routines ------------------------------------------*/
857 //atic void do_key (int ch);
858 //atic void summary_show (void);
859 //atic const char *task_show (const WIN_t *q, const int idx);
860 //atic void window_hlp (void);
861 //atic int window_show (WIN_t *q, int wmax);
862 /*------ Entry point plus two ------------------------------------------*/
863 //atic void frame_hlp (int wix, int max);
864 //atic void frame_make (void);
865 // int main (int argc, char *argv[]);