diff options
Diffstat (limited to 'libps/fmt.c')
-rw-r--r-- | libps/fmt.c | 60 |
1 files changed, 36 insertions, 24 deletions
diff --git a/libps/fmt.c b/libps/fmt.c index 8fed27a3..0465555d 100644 --- a/libps/fmt.c +++ b/libps/fmt.c @@ -1,7 +1,7 @@ /* Implements the ps_fmt type, which describes how to output a user-readable version of a proc_stat. - Copyright (C) 1995, 1996 Free Software Foundation, Inc. + Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc. Written by Miles Bader <miles@gnu.ai.mit.edu> @@ -194,7 +194,9 @@ _fmt_create (char *src, int posix, struct ps_fmt_specs *fmt_specs, while (*src != '\0' && *src != stop) src++; } - *src++ = '\0'; /* NUL terminhate NAME. */ + + if (*src) + *src++ = '\0'; /* NUL terminate NAME. */ } else /* A gnu-style field spec: `NAME' or `NAME:TITLE'. */ @@ -240,11 +242,13 @@ _fmt_create (char *src, int posix, struct ps_fmt_specs *fmt_specs, } if (! field->title) - /* No explicit title specified in the fmt string. */ - if (field->spec->title) - field->title = field->spec->title; /* But the spec has one. */ - else - field->title = field->spec->name; /* Just use the field name. */ + { + /* No explicit title specified in the fmt string. */ + if (field->spec->title) + field->title = field->spec->title; /* But the spec has one. */ + else + field->title = field->spec->name; /* Just use field name. */ + } /* Add FIELD's required pstat_flags to FMT's set */ needs |= ps_getter_needs (ps_fmt_spec_getter (field->spec)); @@ -255,7 +259,7 @@ _fmt_create (char *src, int posix, struct ps_fmt_specs *fmt_specs, field->precision = field->spec->precision; field->flags = (field->spec->flags & ~clr_flags) ^ inv_flags; - + if (quoted_name && *src == '}') /* Skip optional trailing `}' after the spec name. */ src++; @@ -288,7 +292,8 @@ _fmt_create (char *src, int posix, struct ps_fmt_specs *fmt_specs, new_fmt->fields = fields; new_fmt->num_fields = field - fields; new_fmt->needs = needs; - new_fmt->inval = posix ? "-" : 0; + new_fmt->inapp = posix ? "-" : 0; + new_fmt->error = "?"; *fmt = new_fmt; @@ -323,7 +328,7 @@ ps_fmt_creation_error (char *src, int posix, struct ps_fmt_specs *fmt_specs, } /* Free FMT, and any resources it consumes. */ -void +void ps_fmt_free (struct ps_fmt *fmt) { FREE (fmt->src); @@ -358,14 +363,15 @@ ps_fmt_clone (struct ps_fmt *fmt, struct ps_fmt **copy) new->num_fields = fmt->num_fields; new->src = src; new->src_len = fmt->src_len; - new->inval = fmt->inval; + new->inapp = fmt->inapp; + new->error = fmt->error; *copy = new; return 0; } -/* Write an appropiate header line for FMT, containing the titles of all its - fields appropiately aligned with where the values would be printed, to +/* Write an appropriate header line for FMT, containing the titles of all its + fields appropriately aligned with where the values would be printed, to STREAM (without a trailing newline). If count is non-NULL, the total number number of characters output is added to the integer it points to. If any fatal error occurs, the error code is returned, otherwise 0. */ @@ -418,7 +424,8 @@ ps_fmt_write_proc_stat (struct ps_fmt *fmt, struct proc_stat *ps, struct ps_stre error_t err = 0; struct ps_fmt_field *field = ps_fmt_fields (fmt); int nfields = ps_fmt_num_fields (fmt); - int have = proc_stat_flags (ps); + ps_flags_t have = ps->flags; + ps_flags_t inapp = ps->inapp; while (nfields-- > 0 && !err) { @@ -431,16 +438,20 @@ ps_fmt_write_proc_stat (struct ps_fmt *fmt, struct proc_stat *ps, struct ps_stre if (spec != NULL && !err) { - int need = ps_getter_needs (ps_fmt_spec_getter (spec)); + ps_flags_t need = ps_getter_needs (ps_fmt_spec_getter (spec)); /* do we have the resources to print this field? */ if ((need & have) == need) /* Yup */ err = (*spec->output_fn) (ps, field, stream); + else if (need & ~have & inapp) + /* This field is inappropriate for PS. */ + err = + ps_stream_write_field (stream, fmt->inapp ?: "", field->width); else - /* Something to display in invalid fields. */ - err = ps_stream_write_field (stream, fmt->inval ?: "", - ps_fmt_field_width (field)); + /* This field is appropriate, but couldn't be fetched. */ + err = + ps_stream_write_field (stream, fmt->error ?: "", field->width); } field++; @@ -450,9 +461,9 @@ ps_fmt_write_proc_stat (struct ps_fmt *fmt, struct proc_stat *ps, struct ps_stre } /* Remove those fields from FMT for which the function FN, when called on the - field, returns true. Appropiate inter-field characters are also removed: + field, returns true. Appropriate inter-field characters are also removed: those *following* deleted fields at the beginning of the fmt, and those - *preceeding* deleted fields *not* at the beginning. */ + *preceding* deleted fields *not* at the beginning. */ void ps_fmt_squash (struct ps_fmt *fmt, int (*fn)(struct ps_fmt_field *field)) { @@ -468,7 +479,7 @@ ps_fmt_squash (struct ps_fmt *fmt, int (*fn)(struct ps_fmt_field *field)) { /* Save the old prefix, in case we're deleting the first field, and need to prepend it to the next field. */ - const char *beg_pfx = field->pfx; + const char *beg_pfx = field->pfx; int beg_pfx_len = field->pfx_len; nfields--; @@ -519,7 +530,8 @@ ps_fmt_squash (struct ps_fmt *fmt, int (*fn)(struct ps_fmt_field *field)) else /* don't squash this field, just move to the next one */ { - need |= ps_getter_needs (ps_fmt_spec_getter (field->spec)); + if (field->spec) + need |= ps_getter_needs (field->spec->getter); field++; } @@ -528,8 +540,8 @@ ps_fmt_squash (struct ps_fmt *fmt, int (*fn)(struct ps_fmt_field *field)) } /* Remove those fields from FMT which would need the proc_stat flags FLAGS. - Appropiate inter-field characters are also removed: those *following* - deleted fields at the beginning of the fmt, and those *preceeding* deleted + Appropriate inter-field characters are also removed: those *following* + deleted fields at the beginning of the fmt, and those *preceding* deleted fields *not* at the beginning. */ void ps_fmt_squash_flags (struct ps_fmt *fmt, ps_flags_t flags) |