]> granicus.if.org Git - procps-ng/commitdiff
proc/escape.c works for ps
authoralbert <>
Sat, 21 Dec 2002 13:07:53 +0000 (13:07 +0000)
committeralbert <>
Sat, 21 Dec 2002 13:07:53 +0000 (13:07 +0000)
proc/escape.c
proc/escape.h
ps/output.c

index 8668729608210fad4320e52612329441d4539831..060e0a314328e0a366c1a7392d612e12615eb3f5 100644 (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){
index 0df8ed05766b67d5ac541fcda4143ee67cf7dce7..5ad3d42b5ef29e75fd592be119a4e50e43410410 100644 (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);
 
index 094d1ea3d9e530438facf36957fa49cffe3d9ca4..be28c2f5b6b1e83c7f85ae5e9d243a996d77a6ab 100644 (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;
 }