diff options
-rw-r--r-- | libps/spec.c | 507 |
1 files changed, 295 insertions, 212 deletions
diff --git a/libps/spec.c b/libps/spec.c index 46d7500e..96390c4e 100644 --- a/libps/spec.c +++ b/libps/spec.c @@ -42,23 +42,23 @@ static char *get_rpc_name (mach_msg_id_t it) { return 0; } typedef void (*vf)(); static int -ps_get_pid(proc_stat_t ps) +ps_get_pid (struct proc_stat *ps) { - return proc_stat_pid(ps); + return proc_stat_pid (ps); } const struct ps_getter ps_pid_getter = {"pid", PSTAT_PID, (vf) ps_get_pid}; static int -ps_get_thread_index(proc_stat_t ps) +ps_get_thread_index (struct proc_stat *ps) { - return proc_stat_thread_index(ps); + return proc_stat_thread_index (ps); } const struct ps_getter ps_thread_index_getter = {"thread_index", PSTAT_THREAD, (vf) ps_get_thread_index}; -static ps_user_t -ps_get_owner(proc_stat_t ps) +static struct ps_user * +ps_get_owner (struct proc_stat *ps) { return proc_stat_owner (ps); } @@ -66,7 +66,7 @@ const struct ps_getter ps_owner_getter = {"owner", PSTAT_OWNER, (vf) ps_get_owner}; static int -ps_get_owner_uid (proc_stat_t ps) +ps_get_owner_uid (struct proc_stat *ps) { return proc_stat_owner_uid (ps); } @@ -74,64 +74,64 @@ const struct ps_getter ps_owner_uid_getter = {"uid", PSTAT_OWNER_UID, (vf) ps_get_owner_uid}; static int -ps_get_ppid(proc_stat_t ps) +ps_get_ppid (struct proc_stat *ps) { - return proc_stat_proc_info(ps)->ppid; + return proc_stat_proc_info (ps)->ppid; } const struct ps_getter ps_ppid_getter = {"ppid", PSTAT_PROC_INFO, (vf) ps_get_ppid}; static int -ps_get_pgrp(proc_stat_t ps) +ps_get_pgrp (struct proc_stat *ps) { - return proc_stat_proc_info(ps)->pgrp; + return proc_stat_proc_info (ps)->pgrp; } const struct ps_getter ps_pgrp_getter = {"pgrp", PSTAT_PROC_INFO, (vf) ps_get_pgrp}; static int -ps_get_session(proc_stat_t ps) +ps_get_session (struct proc_stat *ps) { - return proc_stat_proc_info(ps)->session; + return proc_stat_proc_info (ps)->session; } const struct ps_getter ps_session_getter = {"session", PSTAT_PROC_INFO, (vf) ps_get_session}; static int -ps_get_login_col(proc_stat_t ps) +ps_get_login_col (struct proc_stat *ps) { - return proc_stat_proc_info(ps)->logincollection; + return proc_stat_proc_info (ps)->logincollection; } const struct ps_getter ps_login_col_getter = {"login_col", PSTAT_PROC_INFO, (vf) ps_get_login_col}; static int -ps_get_num_threads(proc_stat_t ps) +ps_get_num_threads (struct proc_stat *ps) { - return proc_stat_num_threads(ps); + return proc_stat_num_threads (ps); } const struct ps_getter ps_num_threads_getter = {"num_threads", PSTAT_NUM_THREADS, (vf)ps_get_num_threads}; static void -ps_get_args(proc_stat_t ps, char **args_p, int *args_len_p) +ps_get_args (struct proc_stat *ps, char **args_p, int *args_len_p) { - *args_p = proc_stat_args(ps); - *args_len_p = proc_stat_args_len(ps); + *args_p = proc_stat_args (ps); + *args_len_p = proc_stat_args_len (ps); } const struct ps_getter ps_args_getter = {"args", PSTAT_ARGS, ps_get_args}; static int -ps_get_state(proc_stat_t ps) +ps_get_state (struct proc_stat *ps) { - return proc_stat_state(ps); + return proc_stat_state (ps); } const struct ps_getter ps_state_getter = {"state", PSTAT_STATE, (vf) ps_get_state}; static void -ps_get_wait (proc_stat_t ps, char **wait, int *rpc) +ps_get_wait (struct proc_stat *ps, char **wait, int *rpc) { *wait = ps->thread_wait; *rpc = ps->thread_rpc; @@ -140,47 +140,47 @@ const struct ps_getter ps_wait_getter = {"wait", PSTAT_THREAD_WAIT, ps_get_wait}; static int -ps_get_vsize(proc_stat_t ps) +ps_get_vsize (struct proc_stat *ps) { - return proc_stat_task_basic_info(ps)->virtual_size; + return proc_stat_task_basic_info (ps)->virtual_size; } const struct ps_getter ps_vsize_getter = {"vsize", PSTAT_TASK_BASIC, (vf) ps_get_vsize}; static int -ps_get_rsize(proc_stat_t ps) +ps_get_rsize (struct proc_stat *ps) { - return proc_stat_task_basic_info(ps)->resident_size; + return proc_stat_task_basic_info (ps)->resident_size; } const struct ps_getter ps_rsize_getter = {"rsize", PSTAT_TASK_BASIC, (vf) ps_get_rsize}; static int -ps_get_cur_priority(proc_stat_t ps) +ps_get_cur_priority (struct proc_stat *ps) { - return proc_stat_thread_basic_info(ps)->cur_priority; + return proc_stat_thread_basic_info (ps)->cur_priority; } const struct ps_getter ps_cur_priority_getter = {"cur_priority", PSTAT_THREAD_BASIC, (vf) ps_get_cur_priority}; static int -ps_get_base_priority(proc_stat_t ps) +ps_get_base_priority (struct proc_stat *ps) { - return proc_stat_thread_basic_info(ps)->base_priority; + return proc_stat_thread_basic_info (ps)->base_priority; } const struct ps_getter ps_base_priority_getter = {"base_priority", PSTAT_THREAD_BASIC, (vf) ps_get_base_priority}; static int -ps_get_max_priority(proc_stat_t ps) +ps_get_max_priority (struct proc_stat *ps) { - return proc_stat_thread_sched_info(ps)->max_priority; + return proc_stat_thread_sched_info (ps)->max_priority; } const struct ps_getter ps_max_priority_getter = {"max_priority", PSTAT_THREAD_SCHED, (vf) ps_get_max_priority}; static void -ps_get_usr_time (proc_stat_t ps, struct timeval *tv) +ps_get_usr_time (struct proc_stat *ps, struct timeval *tv) { time_value_t tvt = proc_stat_thread_basic_info (ps)->user_time; tv->tv_sec = tvt.seconds; @@ -190,7 +190,7 @@ const struct ps_getter ps_usr_time_getter = {"usr_time", PSTAT_THREAD_BASIC, ps_get_usr_time}; static void -ps_get_sys_time (proc_stat_t ps, struct timeval *tv) +ps_get_sys_time (struct proc_stat *ps, struct timeval *tv) { time_value_t tvt = proc_stat_thread_basic_info (ps)->system_time; tv->tv_sec = tvt.seconds; @@ -200,7 +200,7 @@ const struct ps_getter ps_sys_time_getter = {"sys_time", PSTAT_THREAD_BASIC, ps_get_sys_time}; static void -ps_get_tot_time (proc_stat_t ps, struct timeval *tv) +ps_get_tot_time (struct proc_stat *ps, struct timeval *tv) { time_value_t tvt = proc_stat_thread_basic_info (ps)->user_time; time_value_add (&tvt, &proc_stat_thread_basic_info (ps)->system_time); @@ -211,21 +211,21 @@ const struct ps_getter ps_tot_time_getter = {"tot_time", PSTAT_THREAD_BASIC, ps_get_tot_time}; static float -ps_get_rmem_frac(proc_stat_t ps) +ps_get_rmem_frac (struct proc_stat *ps) { static int mem_size = 0; if (mem_size == 0) { host_basic_info_t info; - error_t err = ps_host_basic_info(&info); + error_t err = ps_host_basic_info (&info); if (err == 0) mem_size = info->memory_size; } if (mem_size > 0) return - (float)proc_stat_task_basic_info(ps)->resident_size + (float)proc_stat_task_basic_info (ps)->resident_size / (float)mem_size; else return 0.0; @@ -234,98 +234,98 @@ const struct ps_getter ps_rmem_frac_getter = {"rmem_frac", PSTAT_TASK_BASIC, (vf) ps_get_rmem_frac}; static float -ps_get_cpu_frac(proc_stat_t ps) +ps_get_cpu_frac (struct proc_stat *ps) { - return (float) proc_stat_thread_basic_info(ps)->cpu_usage + return (float) proc_stat_thread_basic_info (ps)->cpu_usage / (float) TH_USAGE_SCALE; } const struct ps_getter ps_cpu_frac_getter = {"cpu_frac", PSTAT_THREAD_BASIC, (vf) ps_get_cpu_frac}; static int -ps_get_sleep(proc_stat_t ps) +ps_get_sleep (struct proc_stat *ps) { - return proc_stat_thread_basic_info(ps)->sleep_time; + return proc_stat_thread_basic_info (ps)->sleep_time; } const struct ps_getter ps_sleep_getter = {"sleep", PSTAT_THREAD_BASIC, (vf) ps_get_sleep}; static int -ps_get_susp_count(proc_stat_t ps) +ps_get_susp_count (struct proc_stat *ps) { - return proc_stat_suspend_count(ps); + return proc_stat_suspend_count (ps); } const struct ps_getter ps_susp_count_getter = {"susp_count", PSTAT_SUSPEND_COUNT, (vf) ps_get_susp_count}; static int -ps_get_proc_susp_count(proc_stat_t ps) +ps_get_proc_susp_count (struct proc_stat *ps) { - return proc_stat_task_basic_info(ps)->suspend_count; + return proc_stat_task_basic_info (ps)->suspend_count; } const struct ps_getter ps_proc_susp_count_getter = {"proc_susp_count", PSTAT_TASK_BASIC, (vf) ps_get_proc_susp_count}; static int -ps_get_thread_susp_count(proc_stat_t ps) +ps_get_thread_susp_count (struct proc_stat *ps) { - return proc_stat_thread_basic_info(ps)->suspend_count; + return proc_stat_thread_basic_info (ps)->suspend_count; } const struct ps_getter ps_thread_susp_count_getter = {"thread_susp_count", PSTAT_SUSPEND_COUNT, (vf) ps_get_thread_susp_count}; -static ps_tty_t -ps_get_tty(proc_stat_t ps) +static struct ps_tty * +ps_get_tty (struct proc_stat *ps) { - return proc_stat_tty(ps); + return proc_stat_tty (ps); } const struct ps_getter ps_tty_getter = {"tty", PSTAT_TTY, (vf)ps_get_tty}; static int -ps_get_page_faults(proc_stat_t ps) +ps_get_page_faults (struct proc_stat *ps) { - return proc_stat_task_events_info(ps)->faults; + return proc_stat_task_events_info (ps)->faults; } const struct ps_getter ps_page_faults_getter = {"page_faults", PSTAT_TASK_EVENTS, (vf) ps_get_page_faults}; static int -ps_get_cow_faults(proc_stat_t ps) +ps_get_cow_faults (struct proc_stat *ps) { - return proc_stat_task_events_info(ps)->cow_faults; + return proc_stat_task_events_info (ps)->cow_faults; } const struct ps_getter ps_cow_faults_getter = {"cow_faults", PSTAT_TASK_EVENTS, (vf) ps_get_cow_faults}; static int -ps_get_pageins(proc_stat_t ps) +ps_get_pageins (struct proc_stat *ps) { - return proc_stat_task_events_info(ps)->pageins; + return proc_stat_task_events_info (ps)->pageins; } const struct ps_getter ps_pageins_getter = {"pageins", PSTAT_TASK_EVENTS, (vf) ps_get_pageins}; static int -ps_get_msgs_sent(proc_stat_t ps) +ps_get_msgs_sent (struct proc_stat *ps) { - return proc_stat_task_events_info(ps)->messages_sent; + return proc_stat_task_events_info (ps)->messages_sent; } const struct ps_getter ps_msgs_sent_getter = {"msgs_sent", PSTAT_TASK_EVENTS, (vf) ps_get_msgs_sent}; static int -ps_get_msgs_rcvd(proc_stat_t ps) +ps_get_msgs_rcvd (struct proc_stat *ps) { - return proc_stat_task_events_info(ps)->messages_received; + return proc_stat_task_events_info (ps)->messages_received; } const struct ps_getter ps_msgs_rcvd_getter = {"msgs_rcvd", PSTAT_TASK_EVENTS, (vf) ps_get_msgs_rcvd}; static int -ps_get_zero_fills(proc_stat_t ps) +ps_get_zero_fills (struct proc_stat *ps) { - return proc_stat_task_events_info(ps)->zero_fills; + return proc_stat_task_events_info (ps)->zero_fills; } const struct ps_getter ps_zero_fills_getter = {"zero_fills", PSTAT_TASK_EVENTS, (vf) ps_get_zero_fills}; @@ -333,22 +333,25 @@ const struct ps_getter ps_zero_fills_getter = /* ---------------------------------------------------------------- */ /* some printing functions */ -/* G() is a helpful macro that just returns the getter G's access function +/* G () is a helpful macro that just returns the getter G's access function cast into a function pointer returning TYPE, as how the function should be called varies depending on the getter */ #define G(g,type)((type (*)())ps_getter_function(g)) error_t -ps_emit_int(proc_stat_t ps, ps_getter_t getter, int width, ps_stream_t stream) +ps_emit_int (struct proc_stat *ps, + const struct ps_getter *getter, + int width, struct ps_stream *stream) { - return ps_stream_write_int_field (stream, G(getter, int)(ps), width); + return ps_stream_write_int_field (stream, G (getter, int)(ps), width); } error_t -ps_emit_nz_int (proc_stat_t ps, ps_getter_t getter, int width, - ps_stream_t stream) +ps_emit_nz_int (struct proc_stat *ps, + const struct ps_getter *getter, + int width, struct ps_stream *stream) { - int value = G(getter, int)(ps); + int value = G (getter, int)(ps); if (value) return ps_stream_write_int_field (stream, value, width); else @@ -356,26 +359,26 @@ ps_emit_nz_int (proc_stat_t ps, ps_getter_t getter, int width, } error_t -ps_emit_priority (proc_stat_t ps, ps_getter_t getter, int width, - ps_stream_t stream) +ps_emit_priority (struct proc_stat *ps, const struct ps_getter *getter, + int width, struct ps_stream *stream) { return ps_stream_write_int_field (stream, - MACH_PRIORITY_TO_NICE (G(getter, int)(ps)), + MACH_PRIORITY_TO_NICE (G (getter, int)(ps)), width); } error_t -ps_emit_num_blocks (proc_stat_t ps, ps_getter_t getter, int width, - ps_stream_t stream) +ps_emit_num_blocks (struct proc_stat *ps, const struct ps_getter *getter, + int width, struct ps_stream *stream) { char buf[20]; - sprintf(buf, "%d", G(getter, int)(ps) / 1024); + sprintf(buf, "%d", G (getter, int)(ps) / 1024); return ps_stream_write_field (stream, buf, width); } int -sprint_frac_value(char *buf, +sprint_frac_value (char *buf, int value, int min_value_len, int frac, int frac_scale, int width) @@ -399,37 +402,37 @@ sprint_frac_value(char *buf, frac /= 10; if (frac_len > 0) - sprintf(buf, "%d.%0*d", value, frac_len, frac); + sprintf (buf, "%d.%0*d", value, frac_len, frac); else - sprintf(buf, "%d", value); + sprintf (buf, "%d", value); - return strlen(buf); + return strlen (buf); } error_t -ps_emit_percent (proc_stat_t ps, ps_getter_t getter, int width, - ps_stream_t stream) +ps_emit_percent (struct proc_stat *ps, const struct ps_getter *getter, + int width, struct ps_stream *stream) { char buf[20]; - float perc = G(getter, float)(ps) * 100; + float perc = G (getter, float)(ps) * 100; if (width == 0) - sprintf(buf, "%g", perc); - else if (ABS(width) > 3) - sprintf(buf, "%.*f", ABS(width) - 3, perc); + sprintf (buf, "%g", perc); + else if (ABS (width) > 3) + sprintf(buf, "%.*f", ABS (width) - 3, perc); else - sprintf(buf, "%d", (int) perc); + sprintf (buf, "%d", (int) perc); return ps_stream_write_field (stream, buf, width); } /* prints its value nicely */ error_t -ps_emit_nice_int (proc_stat_t ps, ps_getter_t getter, int width, - ps_stream_t stream) +ps_emit_nice_int (struct proc_stat *ps, const struct ps_getter *getter, + int width, struct ps_stream *stream) { char buf[20]; - int value = G(getter, int)(ps); + int value = G (getter, int)(ps); char *sfx = " KMG"; int frac = 0; @@ -440,20 +443,20 @@ ps_emit_nice_int (proc_stat_t ps, ps_getter_t getter, int width, sfx++; } - sprintf(buf + sprint_frac_value (buf, value, 1, frac, 3, ABS(width) - 1), + sprintf(buf + sprint_frac_value (buf, value, 1, frac, 3, ABS (width) - 1), "%c", *sfx); return ps_stream_write_field (stream, buf, width); } error_t -ps_emit_seconds (proc_stat_t ps, ps_getter_t getter, int width, - ps_stream_t stream) +ps_emit_seconds (struct proc_stat *ps, const struct ps_getter *getter, + int width, struct ps_stream *stream) { char buf[20]; struct timeval tv; - G(getter, void)(ps, &tv); + G (getter, void)(ps, &tv); fmt_seconds (&tv, ABS (width), buf, sizeof (buf)); @@ -461,13 +464,13 @@ ps_emit_seconds (proc_stat_t ps, ps_getter_t getter, int width, } error_t -ps_emit_minutes (proc_stat_t ps, ps_getter_t getter, - int width, ps_stream_t stream) +ps_emit_minutes (struct proc_stat *ps, const struct ps_getter *getter, + int width, struct ps_stream *stream) { char buf[20]; struct timeval tv; - G(getter, int)(ps, &tv); + G (getter, int)(ps, &tv); fmt_minutes (&tv, ABS (width), buf, sizeof (buf)); @@ -475,16 +478,17 @@ ps_emit_minutes (proc_stat_t ps, ps_getter_t getter, } error_t -ps_emit_past_time (proc_stat_t ps, ps_getter_t getter, - int width, ps_stream_t stream) +ps_emit_past_time (struct proc_stat *ps, const struct ps_getter *getter, + int width, struct ps_stream *stream) { } error_t -ps_emit_uid (proc_stat_t ps, ps_getter_t getter, int width, ps_stream_t stream) +ps_emit_uid (struct proc_stat *ps, const struct ps_getter *getter, + int width, struct ps_stream *stream) { - int uid = G(getter, int)(ps); + int uid = G (getter, int)(ps); if (uid < 0) return ps_stream_write_field (stream, "-", width); else @@ -492,15 +496,15 @@ ps_emit_uid (proc_stat_t ps, ps_getter_t getter, int width, ps_stream_t stream) } error_t -ps_emit_uname (proc_stat_t ps, ps_getter_t getter, int width, - ps_stream_t stream) +ps_emit_uname (struct proc_stat *ps, const struct ps_getter *getter, + int width, struct ps_stream *stream) { - ps_user_t u = G(getter, ps_user_t)(ps); + struct ps_user *u = G (getter, struct ps_user *)(ps); if (u) { struct passwd *pw = ps_user_passwd (u); if (pw == NULL) - return ps_stream_write_int_field (stream, ps_user_uid(u), width); + return ps_stream_write_int_field (stream, ps_user_uid (u), width); else return ps_stream_write_field (stream, pw->pw_name, width); } @@ -510,16 +514,16 @@ ps_emit_uname (proc_stat_t ps, ps_getter_t getter, int width, /* prints a string with embedded nuls as spaces */ error_t -ps_emit_string0 (proc_stat_t ps, ps_getter_t getter, int width, - ps_stream_t stream) +ps_emit_string0 (struct proc_stat *ps, const struct ps_getter *getter, + int width, struct ps_stream *stream) { char *s0, *p, *q; int s0len; - int fwidth = ABS(width); + int fwidth = ABS (width); char static_buf[200]; char *buf = static_buf; - G(getter, void)(ps, &s0, &s0len); + G (getter, void)(ps, &s0, &s0len); if (s0 == NULL) *buf = '\0'; @@ -549,19 +553,19 @@ ps_emit_string0 (proc_stat_t ps, ps_getter_t getter, int width, { error_t err = ps_stream_write_trunc_field (stream, buf, width); if (buf != static_buf) - free(buf); + free (buf); return err; } } error_t -ps_emit_string (proc_stat_t ps, ps_getter_t getter, int width, - ps_stream_t stream) +ps_emit_string (struct proc_stat *ps, const struct ps_getter *getter, + int width, struct ps_stream *stream) { char *str; int len; - G(getter, void)(ps, &str, &len); + G (getter, void)(ps, &str, &len); if (str == NULL) str = ""; @@ -570,15 +574,15 @@ ps_emit_string (proc_stat_t ps, ps_getter_t getter, int width, } error_t -ps_emit_tty_name (proc_stat_t ps, ps_getter_t getter, int width, - ps_stream_t stream) +ps_emit_tty_name (struct proc_stat *ps, const struct ps_getter *getter, + int width, struct ps_stream *stream) { - char *name = "-"; - ps_tty_t tty = G(getter, ps_tty_t)(ps); + const char *name = "-"; + struct ps_tty *tty = G (getter, struct ps_tty *)(ps); if (tty) { - name = ps_tty_short_name(tty); + name = ps_tty_short_name (tty); if (name == NULL || *name == '\0') name = "?"; } @@ -611,11 +615,11 @@ state_shadows[] = { }; error_t -ps_emit_state (proc_stat_t ps, ps_getter_t getter, int width, - ps_stream_t stream) +ps_emit_state (struct proc_stat *ps, const struct ps_getter *getter, + int width, struct ps_stream *stream) { char *tags; - int raw_state = G(getter, int)(ps); + int raw_state = G (getter, int)(ps); int state = raw_state; char buf[20], *p = buf; const struct state_shadow *shadow = state_shadows; @@ -639,14 +643,14 @@ ps_emit_state (proc_stat_t ps, ps_getter_t getter, int width, } error_t -ps_emit_wait (proc_stat_t ps, ps_getter_t getter, int width, - ps_stream_t stream) +ps_emit_wait (struct proc_stat *ps, const struct ps_getter *getter, + int width, struct ps_stream *stream) { int rpc; char *wait; char buf[80]; - G(getter, void)(ps, &wait, &rpc); + G (getter, void)(ps, &wait, &rpc); if (wait == 0) return ps_stream_write_field (stream, "?", width); @@ -714,57 +718,63 @@ ps_emit_wait (proc_stat_t ps, ps_getter_t getter, int width, ((s1) == NULL ? (((s2) == NULL) ? 0 : -1) : ((s2) == NULL ? 1 : (call))) int -ps_cmp_ints(proc_stat_t ps1, proc_stat_t ps2, ps_getter_t getter) +ps_cmp_ints (struct proc_stat *ps1, struct proc_stat *ps2, + const struct ps_getter *getter) { - int (*gf)() = G(getter, int); - int v1 = gf(ps1), v2 = gf(ps2); + int (*gf)() = G (getter, int); + int v1 = gf(ps1), v2 = gf (ps2); return v1 == v2 ? 0 : v1 < v2 ? -1 : 1; } int -ps_cmp_floats(proc_stat_t ps1, proc_stat_t ps2, ps_getter_t getter) +ps_cmp_floats (struct proc_stat *ps1, struct proc_stat *ps2, + const struct ps_getter *getter) { - float (*gf)() = G(getter, float); - float v1 = gf(ps1), v2 = gf(ps2); + float (*gf)() = G (getter, float); + float v1 = gf(ps1), v2 = gf (ps2); return v1 == v2 ? 0 : v1 < v2 ? -1 : 1; } int -ps_cmp_uids(proc_stat_t ps1, proc_stat_t ps2, ps_getter_t getter) +ps_cmp_uids (struct proc_stat *ps1, struct proc_stat *ps2, + const struct ps_getter *getter) { - ps_user_t (*gf)() = G(getter, ps_user_t); - ps_user_t u1 = gf(ps1), u2 = gf(ps2); + struct ps_user *(*gf)() = G (getter, struct ps_user *); + struct ps_user *u1 = gf (ps1), *u2 = gf (ps2); return (u1 ? ps_user_uid (u1) : -1) - (u2 ? ps_user_uid (u2) : -1); } int -ps_cmp_unames(proc_stat_t ps1, proc_stat_t ps2, ps_getter_t getter) +ps_cmp_unames (struct proc_stat *ps1, struct proc_stat *ps2, + const struct ps_getter *getter) { - ps_user_t (*gf)() = G(getter, ps_user_t); - ps_user_t u1 = gf(ps1), u2 = gf(ps2); + struct ps_user *(*gf)() = G (getter, struct ps_user *); + struct ps_user *u1 = gf (ps1), *u2 = gf (ps2); struct passwd *pw1 = u1 ? ps_user_passwd (u1) : 0; struct passwd *pw2 = u2 ? ps_user_passwd (u2) : 0; return GUARDED_CMP (pw1, pw2, strcmp (pw1->pw_name, pw2->pw_name)); } int -ps_cmp_strings(proc_stat_t ps1, proc_stat_t ps2, ps_getter_t getter) +ps_cmp_strings (struct proc_stat *ps1, struct proc_stat *ps2, + const struct ps_getter *getter) { - void (*gf)() = G(getter, void); + void (*gf)() = G (getter, void); char *s1, *s2; int s1len, s2len; /* Get both strings */ - gf(ps1, &s1, &s1len); - gf(ps2, &s2, &s2len); + gf (ps1, &s1, &s1len); + gf (ps2, &s2, &s2len); - return GUARDED_CMP(s1, s2, strncmp(s1, s2, MIN(s1len, s2len))); + return GUARDED_CMP(s1, s2, strncmp(s1, s2, MIN (s1len, s2len))); } int -ps_cmp_times (proc_stat_t ps1, proc_stat_t ps2, ps_getter_t getter) +ps_cmp_times (struct proc_stat *ps1, struct proc_stat *ps2, + const struct ps_getter *getter) { - void (*g)() = G(getter, void); + void (*g)() = G (getter, void); struct timeval tv1, tv2; g (ps1, &tv1); @@ -782,112 +792,185 @@ ps_cmp_times (proc_stat_t ps1, proc_stat_t ps2, ps_getter_t getter) /* `Nominal' functions -- return true for `unexciting' values. */ /* For many things, zero is not so interesting. */ -bool -ps_nominal_zint (proc_stat_t ps, ps_getter_t getter) +int +ps_nominal_zint (struct proc_stat *ps, const struct ps_getter *getter) { - return G(getter, int)(ps) == 0; + return G (getter, int)(ps) == 0; } /* Priorities are similar, but have to be converted to the unix nice scale first. */ -bool -ps_nominal_pri (proc_stat_t ps, ps_getter_t getter) +int +ps_nominal_pri (struct proc_stat *ps, const struct ps_getter *getter) { - return MACH_PRIORITY_TO_NICE(G(getter, int)(ps)) == 0; + return MACH_PRIORITY_TO_NICE(G (getter, int)(ps)) == 0; } /* Hurd processes usually have 2 threads; XXX is there someplace we get get this number from? */ -bool -ps_nominal_nth (proc_stat_t ps, ps_getter_t getter) +int +ps_nominal_nth (struct proc_stat *ps, const struct ps_getter *getter) { - return G(getter, int)(ps) == 2; + return G (getter, int)(ps) == 2; } static int own_uid = -2; /* -1 means no uid at all. */ /* A user is nominal if it's the current user. */ -bool -ps_nominal_user (proc_stat_t ps, ps_getter_t getter) +int +ps_nominal_user (struct proc_stat *ps, const struct ps_getter *getter) { - ps_user_t u = G(getter, ps_user_t)(ps); + struct ps_user *u = G (getter, struct ps_user *)(ps); if (own_uid == -2) - own_uid = getuid(); + own_uid = getuid (); return own_uid >= 0 && u && u->uid == own_uid; } /* A uid is nominal if it's that of the current user. */ -bool -ps_nominal_uid (proc_stat_t ps, ps_getter_t getter) +int +ps_nominal_uid (struct proc_stat *ps, const struct ps_getter *getter) { - uid_t uid = G(getter, uid_t)(ps); + uid_t uid = G (getter, uid_t)(ps); if (own_uid == -2) own_uid = getuid (); return own_uid >= 0 && uid == own_uid; } - + /* ---------------------------------------------------------------- */ -ps_fmt_spec_t -ps_fmt_specs_find (ps_fmt_specs_t specs, char *name) +/* Returns the first entry called NAME in the vector of fmt_specs SPECS. If + the result is in fact an alias entry, returns in ALIASED_TO the name of + the desired source. */ +static const struct ps_fmt_spec * +specv_find (const struct ps_fmt_spec *specs, const char *name, + char **aliased_to) +{ + while (! ps_fmt_spec_is_end (specs)) + { + char *alias = index (specs->name, '='); + if (alias) + { + unsigned name_len = strlen (name); + + if (name_len == alias - specs->name + && strncasecmp (name, specs->name, name_len) == 0) + /* SPECS is an alias, lookup what it refs to. */ + { + *aliased_to = alias + 1; + return specs; + } + } + else + if (strcasecmp (specs->name, name) == 0) + return specs; + specs++; + } + + return 0; +} + +/* Adds a new alias expansion, using fields from ALIAS, where non-zero, + otherwise SRC, to SPECS. */ +struct ps_fmt_spec * +specs_add_alias (struct ps_fmt_specs *specs, + const struct ps_fmt_spec *alias, + const struct ps_fmt_spec *src) +{ + struct ps_fmt_spec *exp; + char *name_end = index (alias->name, '='); + size_t name_len = name_end ? name_end - alias->name : strlen (alias->name); + + if (specs->expansions == 0) + { + specs->expansions_alloced = 5; + specs->expansions = + malloc (sizeof (struct ps_fmt_spec) * specs->expansions_alloced); + exp = specs->expansions; + } + else + { + exp = specs->expansions; + while (! ps_fmt_spec_is_end (exp)) + exp++; + if (exp + 1 - specs->expansions == specs->expansions_alloced) + /* End marker's really at the end, so make room for some more. */ + { + size_t exp_offs = exp - specs->expansions; + size_t new_alloced = specs->expansions_alloced * 2; + struct ps_fmt_spec *new_exps = + realloc (specs->expansions, + sizeof (struct ps_fmt_spec) * new_alloced); + + if (! new_exps) + return 0; + + specs->expansions = new_exps; + specs->expansions_alloced = new_alloced; + exp = new_exps + exp_offs; + } + } + + /* EXP gets its name from ALIAS, but only the bit before the alias marker. */ + exp->name = malloc (name_len + 1); + if (! exp->name) + return 0; + bcopy ((char *)alias->name, (char *)exp->name, name_len); + ((char *)exp->name)[name_len] = '\0'; + + /* Copy the rest of the fields from ALIAS, but defaulting to SRC. */ + exp->title = alias->title ?: src->title; + exp->width = alias->width ?: src->width; + exp->getter = alias->getter ?: src->getter; + exp->output_fn = alias->output_fn ?: src->output_fn; + exp->cmp_fn = alias->cmp_fn ?: src->cmp_fn; + exp->nominal_fn = alias->nominal_fn ?: src->nominal_fn; + + /* Now add the list-end marker. */ + bzero (exp + 1, sizeof (*exp)); + + return exp; +} + +const struct ps_fmt_spec * +ps_fmt_specs_find (struct ps_fmt_specs *specs, const char *name) { if (specs) /* Allow NULL to make recursion more handy. */ { - ps_fmt_spec_t s = specs->specs; + char *aliased_to = 0; + const struct ps_fmt_spec *s = 0; + + if (specs->expansions) + /* If SPECS contains any alias expansions, look there first. */ + s = specv_find (specs->expansions, name, &aliased_to); + + if (! s) + /* Look in the local list of specs. */ + s = specv_find (specs->specs, name, &aliased_to); - while (! ps_fmt_spec_is_end (s)) + if (s) { - char *alias = index (s->name, '='); - if (alias) + if (aliased_to) { - unsigned name_len = strlen (name); - - if (name_len == alias - s->name - && strncasecmp (name, s->name, name_len) == 0) - /* S is an alias, lookup what it refs to. */ - { - ps_fmt_spec_t src; /* What S is an alias to. */ - - ++alias; /* Point at the alias name. */ - - if (strcasecmp (name, alias) == 0) - /* An alias to the same name (useful to just change some - property) -- start looking up in the parent. */ - src = ps_fmt_specs_find (specs->parent, alias); - else - src = ps_fmt_specs_find (specs, alias); - - if (! src) - return 0; - - /* Copy fields into the alias entry. */ - if (! s->title && src->title) - s->title = src->title; - if (! s->width && src->width) - s->width = src->width; - if (! s->getter && src->getter) - s->getter = src->getter; - if (! s->output_fn && src->output_fn) - s->output_fn = src->output_fn; - if (! s->cmp_fn && src->cmp_fn) - s->cmp_fn = src->cmp_fn; - if (! s->nominal_fn && src->nominal_fn) - s->nominal_fn = src->nominal_fn; - - /* Now make this not an alias. */ - *--alias = '\0'; - - return s; - } + const struct ps_fmt_spec *src; /* What S is an alias to. */ + + if (strcasecmp (name, aliased_to) == 0) + /* An alias to the same name (useful to just change some + property) -- start looking up in the parent. */ + src = ps_fmt_specs_find (specs->parent, aliased_to); + else + src = ps_fmt_specs_find (specs, aliased_to); + + if (! src) + return 0; + + s = specs_add_alias (specs, s, src); } - else - if (strcasecmp (s->name, name) == 0) - return s; - s++; } + else + /* Try again with our parent. */ + s = ps_fmt_specs_find (specs->parent, name); - /* Try again with our parent. */ - return ps_fmt_specs_find (specs->parent, name); + return s; } else return 0; @@ -968,4 +1051,4 @@ static const struct ps_fmt_spec specs[] = {0} }; -const struct ps_fmt_specs ps_std_fmt_specs = { (ps_fmt_spec_t)specs, 0 }; +struct ps_fmt_specs ps_std_fmt_specs = { specs, 0 }; |