#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-------------------------------"
"********************************"
"********************************"
"********************************"
"********************************";
- 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
}
/////////////////////////////////////////////////
//}
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
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){
#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);
* 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){
/* "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){
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;
}