MiniDevil As beautiful as a shell
heredoc.c File Reference

Heredoc processing: reads lines and applies variable expansion. More...

#include <readline/readline.h>
#include <stdio.h>
#include "executor.h"
#include "token.h"
#include "env.h"
#include "signals.h"
#include "libft.h"
#include "get_next_line.h"
+ Include dependency graph for heredoc.c:

Detailed Description

Heredoc processing: reads lines and applies variable expansion.

Definition in file heredoc.c.

Functions

static void write_heredoc_line (int fd, char *line, t_shell *shell, int expand)
 Write one heredoc line (optionally expanding variables) More...
 
static char * read_heredoc_line (void)
 Read a line from STDIN. More...
 
static void print_heredoc_eof_error (char *delim)
 Print an EOF warning for an unterminated heredoc (bash style) More...
 
static int read_heredoc_lines (int fd, char *delim, t_shell *shell, int expand)
 Read the lines until the delimiter or EOF/SIGINT. More...
 
int handle_heredoc (char *delimiter, int quoted, t_shell *shell)
 Execute a heredoc (read input and return a readable fd) More...
 

Function Documentation

◆ write_heredoc_line()

static void write_heredoc_line ( int  fd,
char *  line,
t_shell shell,
int  expand 
)
static

Write one heredoc line (optionally expanding variables)

Parameters
fdWrite end of the heredoc pipe
lineLine to write
shellShell context (for variable expansion)
expandWhether to expand variables or not

Definition at line 30 of file heredoc.c.

References t_shell::env, t_shell::exit_status, expand_variables(), and QUOTE_NONE.

Referenced by read_heredoc_lines().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ read_heredoc_line()

static char* read_heredoc_line ( void  )
static

Read a line from STDIN.

Reads lines interactively (readline) or from a pipe (get_next_line)

Returns
The line

Definition at line 52 of file heredoc.c.

Referenced by read_heredoc_lines().

+ Here is the caller graph for this function:

◆ print_heredoc_eof_error()

static void print_heredoc_eof_error ( char *  delim)
static

Print an EOF warning for an unterminated heredoc (bash style)

Parameters
delimDelimiter that was expected for the heredoc

Definition at line 73 of file heredoc.c.

Referenced by read_heredoc_lines().

+ Here is the caller graph for this function:

◆ read_heredoc_lines()

static int read_heredoc_lines ( int  fd,
char *  delim,
t_shell shell,
int  expand 
)
static

Read the lines until the delimiter or EOF/SIGINT.

Each line is written to fd via write_heredoc_line()

Parameters
fdWrite end of the heredoc pipe
delimDelimiter for the heredoc
shellShell context (for variable expansion)
expandWhether to expand variables or not
Returns
0 on normal (delimiter/EOF) and 1 on SIGINT

Definition at line 92 of file heredoc.c.

References g_signal, print_heredoc_eof_error(), read_heredoc_line(), and write_heredoc_line().

Referenced by handle_heredoc().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handle_heredoc()

int handle_heredoc ( char *  delimiter,
int  quoted,
t_shell shell 
)

Execute a heredoc (read input and return a readable fd)

  • Creates a pipe
  • Reads lines into the write end until the delimiter is reached
  • Closes the write end and returns the read end
  • If the delimiter was quoted, no expansion occurs
  • On SIGINT closes both pipe ends and restores STDIN
Parameters
delimiterHeredoc delimiter
quotedWhether the delimiter was quoted or not
shellShell context (for signals and variable expansion)
Returns
Read end fd of the heredoc pipe or -1 on error/interrupted

Definition at line 133 of file heredoc.c.

References g_signal, read_heredoc_lines(), restore_stdin(), setup_heredoc_signals(), and setup_interactive_signals().

Referenced by collect_one().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Go to the source code of this file.