emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Martin Jerabek" <om@mailservice.ms>
To: emacs-orgmode@gnu.org
Subject: Babel C-mode corrupts double-quoted strings in output
Date: Wed, 31 Aug 2022 18:35:14 +0200	[thread overview]
Message-ID: <bc004962-679e-498f-b245-ae253aae6271@www.fastmail.com> (raw)

Hi!

I recently started to use Org Babel for C++ programs. One of the programs outputs several lines with double-quoted strings, similar to this:

#+NAME: doublequotes_cpp
#+begin_src cpp :includes <iostream> :results output verbatim raw
std::cout << "\"line 1\"\n";
std::cout << "\"line 2\"\n";
std::cout << "\"line 3\"\n";
#+end_src

#+RESULTS: doublequotes_cpp
line 1

As you can see, only the first line is copied to the RESULTS block, and it is stripped of the double quotes.

I tracked down the problem to org-babel-read (in ob-core.el). org-babel-C-execute (in ob-C.el) calls this function with the output of the C++ program. The problem is the following line:

((eq (string-to-char cell) ?\") (read cell))

i.e. if the output of the program starts with a double quote, it is passed to read which reads only the first string and also removes the double quotes, resulting in the observed output.

The original version of this piece of code was added with the following commit:

commit 60a8ba556d682849eafb0f84e689967cd2965549
Author: Eric Schulte <schulte.eric@gmail.com>
Date:   Wed Mar 2 07:55:39 2011 -0700

    ob: read string variable values wrapped in double quotes, removing the quotes
    
    * lisp/ob.el (org-babel-read): Read string variable values wrapped in
      double quotes, removing the quotes.

AFAICT this modification was done in response to the email thread "[Orgmode] org-babel-read should have option NOT to interpret as elisp" started on 2011-02-27, more specifically the email on "Wed, 02 Mar 2011 07:56:45 -0700" from Eric Schulte. This was obviously done for parsing variables in the header line, not for the program output, but the Babel C mode uses org-babel-read also for the output.

I assumed that ":results output verbatim raw" would prevent any postprocessing of the output but this is not the case for C mode.

I am not sure how to fix this without breaking backward compatibility. I assume it should be fixed directly in org-babel-C-execute, not in a central function like org-babel-read to minimize the impact. Surprisingly (for me) the equivalent shell script works as expected:

#+NAME: doublequotes
#+begin_src shell :results output verbatim raw
echo '"line 1"'
echo '"line 2"'
echo '"line 3"'
#+end_src

#+RESULTS: doublequotes
"line 1"
"line 2"
"line 3"

because org-babel-execute:shell does not process the output with org-babel-read. I do not know if languages other than the C family (C, C++, D) are affected.

At the very least, the documentation of org-babel-read should be expanded to document the fact that if the CELL parameter starts with a double quote, it is processed by the read function.

Best regards
Martin Jerabek


             reply	other threads:[~2022-08-31 16:38 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-31 16:35 Martin Jerabek [this message]
2022-08-31 18:03 ` Babel C-mode corrupts double-quoted strings in output Immanuel Litzroth
2022-09-02 11:02 ` tbanelwebmin
2022-09-02 17:34   ` tbanelwebmin
2022-09-25  8:23     ` Ihor Radchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bc004962-679e-498f-b245-ae253aae6271@www.fastmail.com \
    --to=om@mailservice.ms \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).