groff-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[groff] 03/23: [troff]: Make `pso` work more like `so` and `mso`.


From: G. Branden Robinson
Subject: [groff] 03/23: [troff]: Make `pso` work more like `so` and `mso`.
Date: Thu, 21 Nov 2024 14:47:46 -0500 (EST)

gbranden pushed a commit to branch master
in repository groff.

commit e347d3058d34a3a7a1007581492189980acffbaf
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Thu Nov 21 12:56:14 2024 -0600

    [troff]: Make `pso` work more like `so` and `mso`.
    
    * src/roff/troff/input.cpp (pipe_source_request): Refactor.  Return
      early when unsafe requests are not enabled, dropping the rest of the
      function body by an indentation level.  Replace input scanning logic
      with a call to `read_string()`, which is written to do exactly the
      same thing (except that it knows how to handle a leading neutral
      double quote, which this function didn't).
    
    * man/groff.7.man (Request short reference) <pso>:
    * man/groff_diff.7.man (New requests) <pso>: Recast, strengthing
      parallel with `so` request.
    
    * NEWS: Document `pso` request's new handling of a leading neutral
      double quote.
---
 ChangeLog                | 17 ++++++++++++++++
 NEWS                     |  4 ++--
 man/groff.7.man          | 15 +++++++-------
 man/groff_diff.7.man     |  7 +++++--
 src/roff/troff/input.cpp | 53 ++++++++++++++++--------------------------------
 5 files changed, 49 insertions(+), 47 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index b0dddc571..854666968 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2024-11-21  G. Branden Robinson <g.branden.robinson@gmail.com>
+
+       * src/roff/troff/input.cpp (pipe_source_request): Refactor.
+       Return early when unsafe requests are not enabled, dropping the
+       rest of the function body by an indentation level.  Replace
+       input scanning logic with a call to `read_string()`, which is
+       written to do exactly the same thing (except that it knows how
+       to handle a leading neutral double quote, which this function
+       didn't).
+
+       * man/groff.7.man (Request short reference) <pso>:
+       * man/groff_diff.7.man (New requests) <pso>: Recast, strengthing
+       parallel with `so` request.
+
+       * NEWS: Document `pso` request's new handling of a leading
+       neutral double quote.
+
 2024-11-21  G. Branden Robinson <g.branden.robinson@gmail.com>
 
        * src/roff/troff/number.cpp (is_valid_term): Accept scaling unit
diff --git a/NEWS b/NEWS
index ac143963f..eda984064 100644
--- a/NEWS
+++ b/NEWS
@@ -57,8 +57,8 @@ troff
    warning diagnostic in the "range" category.
 
 *  GNU troff now strips a leading neutral double quote from the argument
-   to the `mso`, `msoquiet`, `pi`, `so`, `soquiet`, and `sy` requests,
-   allowing it to contain embedded leading spaces.
+   to the `mso`, `msoquiet`, `pi`, `pso`, `so`, `soquiet`, and `sy`
+   requests, allowing it to contain embedded leading spaces.
 
 *  GNU troff now accepts space characters in the argument to the `mso`,
    `msoquiet`, `so`, and `soquiet` requests.  See "soelim" below.
diff --git a/man/groff.7.man b/man/groff.7.man
index 910973c88..fc34ee37d 100644
--- a/man/groff.7.man
+++ b/man/groff.7.man
@@ -4141,14 +4141,13 @@ See registers
 .BR ury .
 .
 .TPx
-.REQ .pso command-line
-Execute
-.IR command-line ,
-a sequence of ordinary characters,
-.\" ...limiting the shell commands you can express; see Savannah #65108.
-with
-.MR popen 3
-and interpolate its output.
+.REQ .pso command
+As
+.RB \[lq] so \[rq],
+except that input comes from the standard output stream of
+.IR command ,
+which is passed to
+.MR popen 3 .
 .
 Unsafe request;
 disabled by default.
diff --git a/man/groff_diff.7.man b/man/groff_diff.7.man
index 06267d479..df09e8950 100644
--- a/man/groff_diff.7.man
+++ b/man/groff_diff.7.man
@@ -3378,11 +3378,14 @@ these four registers are set to zero.
 .
 .
 .TP
-.BI .pso \~command
+.BR .pso\~ [ \[dq] ]\c
+.I command
 As
 .RB \[lq] so \[rq],
 except that input comes from the standard output stream of
-.IR command .
+.IR command ,
+which is passed to
+.MR popen 3 .
 .
 .
 .TP
diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp
index 0aeee3760..61029bab6 100644
--- a/src/roff/troff/input.cpp
+++ b/src/roff/troff/input.cpp
@@ -6643,42 +6643,23 @@ void pipe_source_request() // .pso
   if (!want_unsafe_requests) {
     error("piped command source request is not allowed in safer mode");
     skip_line();
+    return;
   }
-  else {
-    int c;
-    while ((c = get_copy(0)) == ' ' || c == '\t')
-      ;
-    size_t buf_size = 24;
-    char *buf = new char[buf_size]; // C++03: new char[buf_size]();
-    (void) memset(buf, 0, (buf_size * sizeof(char)));
-    size_t buf_used = 0;
-    for (; c != '\n' && c != EOF; c = get_copy(0)) {
-      const char *s = asciify(c);
-      size_t slen = strlen(s);
-      if ((buf_used + slen + 1) > buf_size) {
-       char *old_buf = buf;
-       size_t old_buf_size = buf_size;
-       buf_size *= 2;
-       buf = new char[buf_size]; // C++03: new char[buf_size]();
-       (void) memset(buf, 0, (buf_size * sizeof(char)));
-       memcpy(buf, old_buf, old_buf_size);
-       delete[] old_buf;
-      }
-      strcpy(buf + buf_used, s);
-      buf_used += slen;
-    }
-    buf[buf_used] = '\0';
-    errno = 0;
-    FILE *fp = popen(buf, POPEN_RT);
-    if (fp)
-      input_stack::push(new file_iterator(fp, symbol(buf).contents(),
-                                         true));
-    else
-      error("cannot open pipe to process '%1': %2", buf,
-           strerror(errno));
-    delete[] buf;
-    tok.next();
-  }
+  char *pcmd = read_string();
+  // This shouldn't happen thanks to `has_arg()` above.
+  assert(pcmd != 0 /* nullptr */);
+  if (0 /* nullptr */ == pcmd)
+    error("cannot apply piped command source request to empty"
+         " argument");
+  errno = 0;
+  FILE *fp = popen(pcmd, POPEN_RT);
+  if (fp != 0 /* nullptr */)
+    input_stack::push(new file_iterator(fp, pcmd, true /* popened */));
+  else
+    error("cannot open pipe to process '%1': %2", pcmd,
+         strerror(errno));
+  delete[] pcmd;
+  tok.next();
 }
 
 // .psbb
@@ -8434,6 +8415,8 @@ void abort_request()
 // subsequent spaces populate the returned string.
 //
 // The caller must use `tok.next()` to advance the input stream pointer.
+//
+// The caller has responsibility for freeing the returned array.
 char *read_string()
 {
   int len = 256;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]