Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: What does REDIRF_FROM_HEREDOC flag represent?



Jett Husher wrote on Thu, Oct 21, 2021 at 09:53:42 +0000:
> Good day!
> 
> I was peeking at the source code

Welcome :)

> and found REDIRF_FROM_HEREDOC flag.  The comments say that it's for a
> here-string that came from a here-document but I'm still not clear how
> this looks exactly.

A here-string is this thing:

    item(tt(<<<) var(word))(
    Perform shell expansion on var(word) and pass the result
    to standard input.  This is known as a em(here-string).
    Compare the use of var(word) in here-documents above, where var(word)
    does not undergo shell expansion.
    )

Example:

    % nl -ba <<< $'foo\nbar'  
         1	foo
         2	bar
    % 

> Is it that every line in heredoc is a here-string?

No.

> I've tried to read the part in parser.c where this flag is being set,
> but couldn't make sense of it since I'm not at all acquainted with C
> or the codebase as a whole.

I think what happens is this:

- After parsing, a here-doc is represented as a here-string with
  REDIRF_FROM_HEREDOC set

- text.c looks for that bit and does the reverse transformation, so
  stuff like
  .
      f() { foo <<< lorem; bar <<'EOF'
      ipsum
      EOF
      }
  .
  gets emitted by `which f` using the same type as input redirections as
  it used when the shell first lexed/parsed it.

- exec.c appends a newline to here-strings' data before passing it to
  commands; see:
  .
      % xxd <<<foo 
      00000000: 666f 6f0a                                foo.

- There are no other mentions of REDIRF_FROM_HEREDOC.

It did help that I already knew what each file does in general, but
yeah, I don't have access to zsh-with-comments.git either ☹

Cheers,

Daniel

> Would very much appreciate the explanation.
> 
> - Jett
> 




Messages sorted by: Reverse Date, Date, Thread, Author