proc/escape.c works for ps

This commit is contained in:
albert 2002-12-21 13:07:53 +00:00
parent 34dd01fa40
commit b67dc52160
3 changed files with 33 additions and 46 deletions

View File

@ -76,9 +76,10 @@ leave:
#endif
/* sanitize a string via one-way mangle */
int escape_str(char *restrict dst, const char *restrict src, size_t bytes){
int escape_str(char *restrict dst, const char *restrict src, int bufsize, int maxglyphs){
unsigned char c;
size_t i;
int my_glyphs = 0;
int my_bytes = 0;
const char codes[] =
"Z-------------------------------"
"********************************"
@ -88,25 +89,21 @@ int escape_str(char *restrict dst, const char *restrict src, size_t bytes){
"********************************"
"********************************"
"********************************";
bytes--; // allow room for NUL character
for(i=0; i<bytes;){
if(bufsize > maxglyphs+1) bufsize=maxglyphs+1; // FIXME: assumes 8-bit locale
for(;;){
if(my_glyphs >= maxglyphs) break;
if(my_bytes+1 >= bufsize) break;
c = (unsigned char) *(src++);
switch(codes[c]){
case 'Z':
goto leave;
case '*':
i++;
*(dst++) = c;
break;
case '-':
i++;
*(dst++) = '?';
break;
}
if(!c) break;
if(codes[c]=='-') c='?';
my_glyphs++;
my_bytes++;
*(dst++) = c;
}
leave:
*(dst++) = '\0';
return i+1; // bytes of text, excluding the NUL
return my_bytes; // bytes of text, excluding the NUL
}
/////////////////////////////////////////////////
@ -122,7 +119,7 @@ int escape_strlist(char *restrict dst, const char *restrict const *restrict src,
//}
for(;;){
i += escape_str(dst+i, *src, bytes-i);
i += escape_str(dst+i, *src, bytes-i, bytes-i); // FIXME: byte/glyph
if(bytes-i < 3) break; // need room for space, a character, and the NUL
src++;
if(!*src) break; // need something to print
@ -159,7 +156,7 @@ int escape_command(char *restrict const outbuf, const proc_t *restrict const pp,
if(flags & ESC_BRACKETS){
outbuf[end++] = '[';
}
end += escape_str(outbuf+end, pp->cmd, bytes-overhead);
end += escape_str(outbuf+end, pp->cmd, bytes-overhead, glyphs-overhead+1);
// Hmmm, do we want "[foo] <defunct>" or "[foo <defunct>]"?
if(flags & ESC_BRACKETS){

View File

@ -13,7 +13,7 @@ EXTERN_C_BEGIN
#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 escape_str(char *restrict dst, const char *restrict src, int bufsize, int maxglyphs);
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);

View File

@ -342,22 +342,18 @@ Modifications to the arguments are not shown.
* pp->environ environment
*/
// FIXME: some of these may hit the guard page in forest mode
/* "command" is the same thing: long unless c */
static int pr_args(char *restrict const outbuf, const proc_t *restrict const pp){
char *endp;
unsigned flags;
endp = outbuf + forest_helper(outbuf);
if(bsd_c_option){
endp += escape_str(endp, pp->cmd, PAGE_SIZE); /* short version */
}else{
const char **lc = (const char**)pp->cmdline; /* long version */
if(lc && *lc) {
endp += escape_strlist(endp, lc, OUTBUF_SIZE);
} else {
char buf[ESC_STRETCH*PAGE_SIZE]; /* TODO: avoid copy */
escape_str(buf, pp->cmd, ESC_STRETCH*PAGE_SIZE);
endp += snprintf(endp, COLWID, "[%s]", buf);
}
}
if(bsd_c_option) flags = ESC_DEFUNCT;
else flags = ESC_DEFUNCT | ESC_BRACKETS | ESC_ARGS;
endp += escape_command(endp, pp, OUTBUF_SIZE, OUTBUF_SIZE, flags);
if(bsd_e_option){
const char **env = (const char**)pp->environ;
if(env && *env){
@ -371,19 +367,13 @@ static int pr_args(char *restrict const outbuf, const proc_t *restrict const pp)
/* "ucomm" is the same thing: short unless -f */
static int pr_comm(char *restrict const outbuf, const proc_t *restrict const pp){
char *endp;
unsigned flags;
endp = outbuf + forest_helper(outbuf);
if(!unix_f_option){ /* does -f matter? */
endp += escape_str(endp, pp->cmd, PAGE_SIZE); /* short version */
}else{
const char **lc = (const char**)pp->cmdline; /* long version */
if(lc && *lc) {
endp += escape_strlist(endp, lc, OUTBUF_SIZE);
} else {
char buf[ESC_STRETCH*PAGE_SIZE]; /* TODO: avoid copy */
escape_str(buf, pp->cmd, ESC_STRETCH*PAGE_SIZE);
endp += snprintf(endp, COLWID, "[%s]", buf);
}
}
if(unix_f_option) flags = ESC_DEFUNCT | ESC_BRACKETS | ESC_ARGS;
else flags = ESC_DEFUNCT;
endp += escape_command(endp, pp, OUTBUF_SIZE, OUTBUF_SIZE, flags);
if(bsd_e_option){
const char **env = (const char**)pp->environ;
if(env && *env){
@ -397,7 +387,7 @@ static int pr_comm(char *restrict const outbuf, const proc_t *restrict const pp)
static int pr_fname(char *restrict const outbuf, const proc_t *restrict const pp){
char *endp;
endp = outbuf + forest_helper(outbuf);
endp += escape_str(endp, pp->cmd, 8);
endp += escape_str(endp, pp->cmd, OUTBUF_SIZE, 8);
return endp - outbuf;
}