2 * Copyright 1998-2002 by Albert Cahalan; all rights resered.
3 * This file may be used subject to the terms and conditions of the
4 * GNU Library General Public License Version 2, or any later version
5 * at your option, as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU Library General Public License for more details.
15 #include "../proc/procps.h"
16 #include "../proc/escape.h"
17 #include "../proc/readproc.h"
20 #define trace(args...) printf(## args)
22 #define trace(args...)
26 /***************** GENERAL DEFINE ********************/
45 /* Since an enum could be smashed by a #define, it would be bad. */
46 #define U98 0 /* Unix98 standard */ /* This must be 0 */
47 #define XXX 1 /* Common extension */
48 #define DEC 2 /* Digital Unix */
49 #define AIX 3 /* AIX */
50 #define SCO 4 /* SCO */
51 #define LNX 5 /* Linux original :-) */
52 #define BSD 6 /* FreeBSD and OpenBSD */
53 #define SUN 7 /* SunOS 5 (Solaris) */
54 #define HPU 8 /* HP-UX */
55 #define SGI 9 /* Irix */
56 #define SOE 10 /* IBM's S/390 OpenEdition */
57 #define TST 11 /* test code */
60 * Try not to overflow the output buffer:
61 * 32 pages for env+cmd
62 * 64 kB pages on IA-64
63 * 4 chars for "\377", or 1 when mangling to '?' (ESC_STRETCH)
64 * plus some slack for other stuff
65 * That is about 8.5 MB on IA-64, or 0.6 MB on i386
67 * Sadly, current kernels only supply one page of env/command data.
68 * The buffer is now protected with a guard page, and via other means
69 * to avoid hitting the guard page.
72 /* output buffer size */
73 #define OUTBUF_SIZE (2 * 64*1024 * ESC_STRETCH)
75 /******************* PS DEFINE *******************/
78 // Justification control for flags field comes first.
79 #define CF_JUST_MASK 0x0f
81 #define CF_USER 1 // left if text, right if numeric
84 #define CF_UNLIMITED 4
85 #define CF_WCHAN 5 // left if text, right if numeric
86 #define CF_SIGNAL 6 // right in 9, or 16 if screen_cols>107
87 // Then the other flags
88 #define CF_PIDMAX 0x00000010 // react to pid_max
89 // Only one allowed; use separate bits to catch errors.
90 #define CF_PRINT_THREAD_ONLY 0x10000000
91 #define CF_PRINT_PROCESS_ONLY 0x20000000
92 #define CF_PRINT_EVERY_TIME 0x40000000
93 #define CF_PRINT_AS_NEEDED 0x80000000 // means we have no clue, so assume EVERY TIME
94 #define CF_PRINT_MASK 0xf0000000
96 #define needs_for_select (PROC_FILLSTAT | PROC_FILLSTATUS)
100 #define TF_B_m 0x0002
101 #define TF_U_m 0x0004
102 #define TF_U_T 0x0008
103 #define TF_U_L 0x0010
104 #define TF_show_proc 0x0100 // show the summary line
105 #define TF_show_task 0x0200 // show the per-thread lines
106 #define TF_show_both 0x0400 // distinct proc/task format lists
107 #define TF_loose_tasks 0x0800 // let sorting break up task groups (BSDish)
108 #define TF_no_sort 0x1000 // don't know if thread-grouping should survive a sort
109 #define TF_no_forest 0x2000 // don't see how to do threads w/ forest option
110 #define TF_must_use 0x4000 // options only make sense if LWP/SPID column added
112 /* personality control flags */
113 #define PER_BROKEN_o 0x0001
114 #define PER_BSD_h 0x0002
115 #define PER_BSD_m 0x0004
116 #define PER_IRIX_l 0x0008
117 #define PER_FORCE_BSD 0x0010
118 #define PER_GOOD_o 0x0020
119 #define PER_OLD_m 0x0040
120 #define PER_NO_DEFAULT_g 0x0080
121 #define PER_ZAP_ADDR 0x0100
122 #define PER_SANE_USER 0x0200
123 #define PER_HPUX_x 0x0400
124 #define PER_SVR4_x 0x0800
125 #define PER_BSD_COLS 0x1000
126 #define PER_UNIX_COLS 0x2000
128 /* Simple selections by bit mask */
135 /* predefined format flags such as: -l -f l u s -j */
136 #define FF_Uf 0x0001 /* -f */
137 #define FF_Uj 0x0002 /* -j */
138 #define FF_Ul 0x0004 /* -l */
139 #define FF_Bj 0x0008 /* j */
140 #define FF_Bl 0x0010 /* l */
141 #define FF_Bs 0x0020 /* s */
142 #define FF_Bu 0x0040 /* u */
143 #define FF_Bv 0x0080 /* v */
144 #define FF_LX 0x0100 /* X */
145 #define FF_Lm 0x0200 /* m */ /* overloaded: threads, sort, format */
146 #define FF_Fc 0x0400 /* --context */ /* Flask security context format */
148 /* predefined format modifier flags such as: -l -f l u s -j */
149 #define FM_c 0x0001 /* -c */
150 #define FM_j 0x0002 /* -j */ /* only set when !sysv_j_format */
151 #define FM_y 0x0004 /* -y */
152 //#define FM_L 0x0008 /* -L */
153 #define FM_P 0x0010 /* -P */
154 #define FM_M 0x0020 /* -M */
155 //#define FM_T 0x0040 /* -T */
156 #define FM_F 0x0080 /* -F */ /* -F also sets the regular -f flags */
158 /* sorting & formatting */
159 /* U,B,G is Unix,BSD,Gnu and then there is the option itself */
164 #define SF_B_m 5 /* overloaded: threads, sort, format */
166 #define SF_G_format 7
169 #define HEAD_SINGLE 0 /* default, must be 0 */
174 /********************** GENERAL TYPEDEF *******************/
176 /* Other fields that might be useful:
178 * char *name; user-defined column name (format specification)
179 * int reverse; sorting in reverse (sort specification)
182 * reverse in place of n
185 typedef union sel_union {
191 char cmd[16]; /* this is _not_ \0 terminated */
194 typedef struct selection_node {
195 struct selection_node *next;
196 sel_union *u; /* used if selection type has a list of values */
197 int n; /* used if selection type has a list of values */
201 typedef struct sort_node {
202 struct sort_node *next;
203 int (*sr)(const proc_t* P, const proc_t* Q); /* sort function */
204 int reverse; /* can sort backwards */
209 typedef struct format_node {
210 struct format_node *next;
211 char *name; /* user can override default name */
212 int (*pr)(char *restrict const outbuf, const proc_t *restrict const pp); // print function
213 /* int (* const sr)(const proc_t* P, const proc_t* Q); */ /* sort function */
216 int vendor; /* Vendor that invented this */
221 typedef struct format_struct {
222 const char *spec; /* format specifier */
223 const char *head; /* default header in the POSIX locale */
224 int (* const pr)(char *restrict const outbuf, const proc_t *restrict const pp); // print function
225 int (* const sr)(const proc_t* P, const proc_t* Q); /* sort function */
227 const int need; /* data we will need (files to read, etc.) */
228 const int vendor; /* Where does this come from? */
232 /* though ps-specific, needed by general file */
233 typedef struct macro_struct {
234 const char *spec; /* format specifier */
235 const char *head; /* default header in the POSIX locale */
238 /**************** PS TYPEDEF ***********************/
240 typedef struct aix_struct {
241 const int desc; /* 1-character format code */
242 const char *spec; /* format specifier */
243 const char *head; /* default header in the POSIX locale */
246 typedef struct shortsort_struct {
247 const int desc; /* 1-character format code */
248 const char *spec; /* format specifier */
251 /* Save these options for later: -o o -O O --format --sort */
252 typedef struct sf_node {
253 struct sf_node *next; /* next arg */
254 format_node *f_cooked; /* convert each arg alone, then merge */
255 sort_node *s_cooked; /* convert each arg alone, then merge */
260 /********************* UNDECIDED GLOBALS **************/
263 extern void show_one_proc(const proc_t *restrict const p, const format_node *restrict fmt);
264 extern void print_format_specifiers(void);
265 extern const aix_struct *search_aix_array(const int findme);
266 extern const shortsort_struct *search_shortsort_array(const int findme);
267 extern const format_struct *search_format_array(const char *findme);
268 extern const macro_struct *search_macro_array(const char *findme);
269 extern void init_output(void);
270 extern int pr_nop(char *restrict const outbuf, const proc_t *restrict const pp);
273 extern void reset_global(void);
276 extern int all_processes;
277 extern const char *bsd_j_format;
278 extern const char *bsd_l_format;
279 extern const char *bsd_s_format;
280 extern const char *bsd_u_format;
281 extern const char *bsd_v_format;
282 extern int bsd_c_option;
283 extern int bsd_e_option;
284 extern uid_t cached_euid;
285 extern dev_t cached_tty;
286 extern char forest_prefix[4 * 32*1024 + 100];
287 extern int forest_type;
288 extern unsigned format_flags; /* -l -f l u s -j... */
289 extern format_node *format_list; /* digested formatting options */
290 extern unsigned format_modifiers; /* -c -j -y -P -L... */
291 extern int header_gap;
292 extern int header_type; /* none, single, multi... */
293 extern int include_dead_children;
294 extern int lines_to_next_header;
295 extern int max_line_width;
296 extern const char *namelist_file;
297 extern int negate_selection;
298 extern int page_size; // "int" for math reasons?
299 extern unsigned personality;
300 extern int prefer_bsd_defaults;
301 extern int running_only;
302 extern int screen_cols;
303 extern int screen_rows;
304 extern unsigned long seconds_since_boot;
305 extern selection_node *selection_list;
306 extern unsigned simple_select;
307 extern sort_node *sort_list;
308 extern const char *sysv_f_format;
309 extern const char *sysv_fl_format;
310 extern const char *sysv_j_format;
311 extern const char *sysv_l_format;
312 extern unsigned thread_flags;
313 extern int unix_f_option;
314 extern int user_is_number;
315 extern int wchan_is_number;
317 /************************* PS GLOBALS *********************/
320 extern int defer_sf_option(const char *arg, int source);
321 extern const char *process_sf_options(int localbroken);
322 extern void reset_sortformat(void);
325 extern int want_this_proc(proc_t *buf);
326 extern const char *select_bits_setup(void);
329 extern const char *help_message;
332 extern void self_info(void);
335 extern int arg_parse(int argc, char *argv[]);