still sig11 w/ "ps f"

This commit is contained in:
albert 2002-12-21 10:34:50 +00:00
parent 1158fdd1b1
commit d8367cbb3e
5 changed files with 108 additions and 27 deletions

View File

@ -9,9 +9,25 @@
* GNU Library General Public License for more details. * GNU Library General Public License for more details.
*/ */
#include <sys/types.h> #include <sys/types.h>
#include "../proc/procps.h" #include <string.h>
#include "common.h" #include "procps.h"
#include "escape.h"
#include "readproc.h"
// What it would be for a UTF-8 locale:
// "Z-------------------------------"
// "********************************"
// "********************************"
// "*******************************-"
// "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" Trailing UTF-8, and badness in 8-bit.
// "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" Trailing UTF-8, and safe in 8-bit.
// "--222222222222222222222222222222"
// ".333333333333.3.44444444555566--" The '.' means '3', with problem chars.
//
// Problems include non-shortest forms, UTF-16, and non-characters.
// The 4-byte, 5-byte, and 6-byte sequences are full of trouble too.
#if 0
/* sanitize a string, without the nice BSD library function: */ /* sanitize a string, without the nice BSD library function: */
/* strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH) */ /* strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH) */
int octal_escape_str(char *restrict dst, const char *restrict src, size_t n){ int octal_escape_str(char *restrict dst, const char *restrict src, size_t n){
@ -57,9 +73,10 @@ leave:
*(dst++) = '\0'; *(dst++) = '\0';
return i; return i;
} }
#endif
/* sanitize a string via one-way mangle */ /* sanitize a string via one-way mangle */
int simple_escape_str(char *restrict dst, const char *restrict src, size_t n){ int escape_str(char *restrict dst, const char *restrict src, size_t bytes){
unsigned char c; unsigned char c;
size_t i; size_t i;
const char codes[] = const char codes[] =
@ -71,7 +88,8 @@ int simple_escape_str(char *restrict dst, const char *restrict src, size_t n){
"********************************" "********************************"
"********************************" "********************************"
"********************************"; "********************************";
for(i=0; i<n;){ bytes--; // allow room for NUL character
for(i=0; i<bytes;){
c = (unsigned char) *(src++); c = (unsigned char) *(src++);
switch(codes[c]){ switch(codes[c]){
case 'Z': case 'Z':
@ -88,21 +106,70 @@ int simple_escape_str(char *restrict dst, const char *restrict src, size_t n){
} }
leave: leave:
*(dst++) = '\0'; *(dst++) = '\0';
return i; return i+1; // bytes of text, excluding the NUL
} }
/* escape a string as desired */ /////////////////////////////////////////////////
int escape_str(char *restrict dst, const char *restrict src, size_t n){
return simple_escape_str(dst, src, n);
}
/* escape an argv or environment string array */ // escape an argv or environment string array
int escape_strlist(char *restrict dst, const char *restrict const *restrict src, size_t n){ //
// bytes arg means sizeof(buf)
int escape_strlist(char *restrict dst, const char *restrict const *restrict src, size_t bytes){
size_t i = 0; size_t i = 0;
while(*src){
i += simple_escape_str(dst+i, *src, n-i); //if(!*src){ just never call this function without checking first
if((n-i > 1) && src[1]) dst[i++] = ' '; // do something nice
//}
for(;;){
i += escape_str(dst+i, *src, bytes-i);
if(bytes-i < 3) break; // need room for space, a character, and the NUL
src++; src++;
if(!*src) break; // need something to print
dst[i++] = ' ';
} }
return i; return i; // bytes of text, excluding the NUL
}
///////////////////////////////////////////////////
int escape_command(char *restrict const outbuf, const proc_t *restrict const pp, int bytes, int glyphs, unsigned flags){
int overhead = 1; // the trailing NUL
int end = 0;
if(bytes > glyphs+1) bytes=glyphs+1; // FIXME: assumes 8-bit locale
if(flags & ESC_ARGS){
const char **lc = (const char**)pp->cmdline;
if(lc && *lc) return escape_strlist(outbuf, lc, bytes);
}
if(flags & ESC_BRACKETS){
overhead += 2;
}
if(flags & ESC_DEFUNCT){
if(pp->state=='Z') overhead += 10; // chars in " <defunct>"
else flags &= ~ESC_DEFUNCT;
}
if(overhead >= bytes){ // if no room for even one byte of the command name
// you'd damn well better have _some_ space
outbuf[0] = '-';
outbuf[1] = '\0';
return 1;
}
if(flags & ESC_BRACKETS){
outbuf[end++] = '[';
}
end += escape_str(outbuf+end, pp->cmd, bytes-overhead);
// Hmmm, do we want "[foo] <defunct>" or "[foo <defunct>]"?
if(flags & ESC_BRACKETS){
outbuf[end++] = ']';
}
if(flags & ESC_DEFUNCT){
memcpy(outbuf+end, " <defunct>", 10);
end += 10;
}
outbuf[end] = '\0';
return end; // bytes or glyphs, not including the NUL
} }

23
proc/escape.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef PROCPS_PROC_ESCAPE_H
#define PROCPS_PROC_ESCAPE_H
//#include <stdio.h>
#include <sys/types.h>
#include "procps.h"
#include "readproc.h"
EXTERN_C_BEGIN
#define ESC_ARGS 0x1 // try to use cmdline instead of cmd
#define ESC_BRACKETS 0x2 // if using cmd, put '[' and ']' around it
#define ESC_DEFUNCT 0x4 // mark zombies with " <defunct>"
extern int escape_strlist(char *restrict dst, const char *restrict const *restrict src, size_t n);
extern int escape_str(char *restrict dst, const char *restrict src, size_t n);
extern int octal_escape_str(char *restrict dst, const char *restrict src, size_t n);
extern int simple_escape_str(char *restrict dst, const char *restrict src, size_t n);
extern int escape_command(char *restrict const outbuf, const proc_t *restrict const pp, int bytes, int glyphs, unsigned flags);
EXTERN_C_END
#endif

View File

@ -171,9 +171,8 @@ static void status2proc(const char *S, proc_t *restrict P){
/* Reads /proc/*/stat files, being careful not to trip over processes with // Reads /proc/*/stat files, being careful not to trip over processes with
* names like ":-) 1 2 3 4 5 6". // names like ":-) 1 2 3 4 5 6".
*/
static void stat2proc(const char* S, proc_t *restrict P) { static void stat2proc(const char* S, proc_t *restrict P) {
unsigned num; unsigned num;
char* tmp; char* tmp;

View File

@ -215,15 +215,6 @@ typedef struct sf_node {
int sf_code; int sf_code;
} sf_node; } sf_node;
/*********************** GENERAL GLOBALS *************************/
/* escape.c */
extern int escape_strlist(char *restrict dst, const char *restrict const *restrict src, size_t n);
extern int escape_str(char *restrict dst, const char *restrict src, size_t n);
extern int octal_escape_str(char *restrict dst, const char *restrict src, size_t n);
extern int simple_escape_str(char *restrict dst, const char *restrict src, size_t n);
/********************* UNDECIDED GLOBALS **************/ /********************* UNDECIDED GLOBALS **************/
/* output.c */ /* output.c */

View File

@ -65,6 +65,7 @@
#include "../proc/wchan.h" #include "../proc/wchan.h"
#include "../proc/procps.h" #include "../proc/procps.h"
#include "../proc/devname.h" #include "../proc/devname.h"
#include "../proc/escape.h"
#include "common.h" #include "common.h"
#ifdef FLASK_LINUX #ifdef FLASK_LINUX