Main Page | Data Structures | File List | Data Fields | Globals | Related Pages

reader.c File Reference

Functions for reading data from stdin and passing to the parser. More...

#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/termios.h>
#include <sys/ioctl.h>
#include <time.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/poll.h>
#include <unistd.h>
#include <wctype.h>
#include <ncurses.h>
#include <termio.h>
#include <term.h>
#include <sys/select.h>
#include <signal.h>
#include <fcntl.h>
#include <dirent.h>
#include <wchar.h>
#include <assert.h>
#include "fallback.h"
#include "util.h"
#include "wutil.h"
#include "highlight.h"
#include "reader.h"
#include "proc.h"
#include "parser.h"
#include "complete.h"
#include "history.h"
#include "common.h"
#include "sanity.h"
#include "env.h"
#include "exec.h"
#include "expand.h"
#include "tokenizer.h"
#include "kill.h"
#include "input_common.h"
#include "input.h"
#include "function.h"
#include "output.h"
#include "screen.h"
#include "halloc.h"
#include "halloc_util.h"
#include "parse_util.h"

Data Structures

struct  reader_data
 A struct describing the state of the interactive reader. More...

Defines

#define PREFIX_MAX_LEN   8
 Maximum length of prefix string when printing completion list.
#define DEFAULT_PROMPT   L"echo \"$USER@\"; hostname|cut -d . -f 1; echo \" \"; pwd; printf '> ';"
 A simple prompt for reading shell commands that does not rely on fish specific commands, meaning it will work even if fish is not installed.
#define PROMPT_FUNCTION_NAME   L"fish_prompt"
#define DEFAULT_TITLE   L"echo $_ \" \"; pwd"
 The default title for the reader.
#define READAHEAD_MAX   256
 The maximum number of characters to read from the keyboard without repainting.
#define KILL_APPEND   0
#define KILL_PREPEND   1
#define NO_SEARCH   0
#define LINE_SEARCH   1
#define TOKEN_SEARCH   2
#define SEARCH_BACKWARD   0
#define SEARCH_FORWARD   1

Typedefs

typedef reader_data reader_data_t
 A struct describing the state of the interactive reader.

Functions

static void reader_super_highlight_me_plenty (int match_highlight_pos, array_list_t *error)
 Call specified external highlighting function and then do search highlighting.
static void term_donate ()
 Give up control of terminal.
static void term_steal ()
 Grab control of terminal.
int reader_exit_forced ()
 This function returns true if fish is exiting by force, i.e.
static void reader_repaint ()
 Repaint the entire commandline.
static void reader_kill (wchar_t *begin, int length, int mode, int new)
 Internal helper function for handling killing parts of text.
void reader_handle_int (int sig)
 The readers interrupt signal handler.
wchar_t * reader_current_filename ()
 Returns the filename of the file currently read.
void reader_push_current_filename (const wchar_t *fn)
 Push a new filename on the stack of read files.
wchar_t * reader_pop_current_filename ()
 Pop the current filename from the stack of read files.
static int check_size ()
 Make sure buffers are large enough to hold current data plus one extra character.
static int completion_cmp (const void *a, const void *b)
static void sort_completion_list (array_list_t *comp)
static void remove_duplicates (array_list_t *l)
 Remove any duplicate completions in the list.
int reader_interrupted ()
 Return the value of the interrupted flag, which is set by the sigint handler, and clear it if it was set.
void reader_write_title ()
 Write the title to the titlebar.
static void exec_prompt ()
 Reexecute the prompt command.
void reader_init ()
 Initialize the reader.
void reader_destroy ()
 Destroy and free resources used by the reader.
void reader_exit (int do_exit, int forced)
 Tell the shell that it should exit after the currently running command finishes.
void reader_repaint_needed ()
 Call this function to tell the reader that a repaint is needed, and should be performed when possible.
static void remove_backward ()
 Remove the previous character in the character buffer and on the screen using syntax highlighting, etc.
static int insert_str (wchar_t *str)
 Insert the characters of the string into the command line buffer and print them to the screen using syntax highlighting, etc.
static int insert_char (int c)
 Insert the character into the command line buffer and print it to the screen using syntax highlighting, etc.
static int comp_len (const wchar_t *a, const wchar_t *b)
 Calculate the length of the common prefix substring of two strings.
static int comp_ilen (const wchar_t *a, const wchar_t *b)
 Calculate the case insensitive length of the common prefix substring of two strings.
static wchar_t get_quote (wchar_t *cmd, int len)
 Find the outermost quoting style of current token.
static void get_param (wchar_t *cmd, int pos, wchar_t *quote, wchar_t **offset, wchar_t **string, int *type)
 Calculates information on the parameter at the specified index.
static void completion_insert (const wchar_t *val, int flags)
 Insert the string at the current cursor position.
static void run_pager (wchar_t *prefix, int is_quoted, array_list_t *comp)
 Run the fish_pager command to display the completion list.
static void reader_flash ()
static int handle_completions (array_list_t *comp)
 Handle the list of completions.
static void reader_interactive_init ()
 Initialize data for interactive use.
static void reader_interactive_destroy ()
 Destroy data for interactive use.
void reader_sanity_check ()
 Check that the reader is in a sane state.
void reader_replace_current_token (wchar_t *new_token)
 Replace the current token with the specified string.
static void handle_history (const wchar_t *new_str)
 Set the specified string from the history as the current buffer.
static int contains_al (const wchar_t *needle, array_list_t *haystack)
 Check if the specified string is contained in the list, using wcscmp as a comparison function.
static void reset_token_history ()
 Reset the data structures associated with the token search.
static void handle_token_history (int forward, int reset)
 Handles a token search command.
static void move_word (int dir, int erase, int new)
 Move buffer position one word or erase one word.
wchar_t * reader_get_buffer ()
 Get the string of character currently entered into the command buffer, or 0 if interactive mode is uninitialized.
void reader_set_buffer (wchar_t *b, int p)
 Set the string of characters in the command buffer, as well as the cursor position.
int reader_get_cursor_pos ()
 Get the current cursor position in the command line.
void reader_run_command (const wchar_t *cmd)
 Run the specified command with the correct terminal modes, and while taking care to perform job notification, set the title, etc.
int reader_shell_test (wchar_t *b)
 Test if the given shell command contains errors.
static int default_test (wchar_t *b)
 Test if the given string contains error.
void reader_push (wchar_t *name)
 Push a new reader environment.
void reader_pop ()
 Return to previous reader environment.
void reader_set_prompt (wchar_t *new_prompt)
 Specify string of shell commands to be run in order to generate the prompt.
void reader_set_complete_function (void(*f)(const wchar_t *, array_list_t *))
 Specify function to use for finding possible tab completions.
void reader_set_highlight_function (void(*f)(wchar_t *, int *, int, array_list_t *))
 Specify function for syntax highlighting.
void reader_set_test_function (int(*f)(wchar_t *))
 Specify function for testing if the command buffer contains syntax errors that must be corrected before returning.
int exit_status ()
 Returns true if the shell is exiting, 0 otherwise.
static void handle_end_loop ()
 This function is called when the main loop notices that end_loop has been set while in interactive mode.
static int read_i ()
 Read interactively.
static int can_read (int fd)
 Test if there are bytes available for reading on the specified file descriptor.
static int wchar_private (wchar_t c)
 Test if the specified character is in the private use area that fish uses to store internal characters.
static int is_backslashed (const wchar_t *str, int pos)
 Test if the specified character in the specified string is backslashed.
wchar_t * reader_readline ()
 Read one line of input.
int reader_search_mode ()
 Test whether the interactive reader is in search mode.
static int read_ni (int fd, io_data_t *io)
 Read non-interactively.
int reader_read (int fd, io_data_t *io)
 Read commands from fd until encountering EOF.

Variables

static reader_data_tdata = 0
 The current interactive reading context.
static int is_interactive_read
 This flag is set to true when fish is interactively reading from stdin.
static int end_loop = 0
 Flag for ending non-interactive shell.
static array_list_t current_filename
 The list containing names of files that are being parsed.
static pid_t original_pid
 Store the pid of the parent process, so the exit function knows whether it should reset the terminal or not.
static int interrupted = 0
 This variable is set to true by the signal handler when ^C is pressed.
static struct termios saved_modes
 Stores the previous termios mode so we can reset the modes when we execute programs and when the shell exits.
static int exit_forced
 Variable to keep track of forced exits - see reader_exit_forced();.


Detailed Description

Functions for reading data from stdin and passing to the parser.

If stdin is a keyboard, it supplies a killring, history, syntax highlighting, tab-completion and various other interactive features.

Internally the interactive mode functions rely in the functions of the input library to read individual characters of input.

Token search is handled incrementally. Actual searches are only done on when searching backwards, since the previous results are saved. The last search position is remembered and a new search continues from the last search position. All search results are saved in the list 'search_prev'. When the user searches forward, i.e. presses Alt-down, the list is consulted for previous search result, and subsequent backwards searches are also handled by consultiung the list up until the end of the list is reached, at which point regular searching will commence.


Define Documentation

#define DEFAULT_PROMPT   L"echo \"$USER@\"; hostname|cut -d . -f 1; echo \" \"; pwd; printf '> ';"
 

A simple prompt for reading shell commands that does not rely on fish specific commands, meaning it will work even if fish is not installed.

This is used by read_i.

#define DEFAULT_TITLE   L"echo $_ \" \"; pwd"
 

The default title for the reader.

This is used by reader_readline.

#define PREFIX_MAX_LEN   8
 

Maximum length of prefix string when printing completion list.

Longer prefixes will be ellipsized.

#define READAHEAD_MAX   256
 

The maximum number of characters to read from the keyboard without repainting.

Note that this readahead will only occur if new characters are avaialble for reading, fish will never block for more input without repainting.


Typedef Documentation

typedef struct reader_data reader_data_t
 

A struct describing the state of the interactive reader.

These states can be stacked, in case reader_readline() calls are nested. This happens when the 'read' builtin is used.


Function Documentation

static void completion_insert const wchar_t *  val,
int  flags
[static]
 

Insert the string at the current cursor position.

The function checks if the string is quoted or not and correctly escapes the string.

Parameters:
val the string to insert
is_complete Whether this completion is the whole string or just the common prefix of several completions. If the former, end by printing a space (and an end quote if the parameter is quoted).

static int default_test wchar_t *  b  )  [static]
 

Test if the given string contains error.

Since this is the error detection for general purpose, there are no invalid strings, so this function always returns false.

static void exec_prompt  )  [static]
 

Reexecute the prompt command.

The output is inserted into data->prompt_buff.

static void get_param wchar_t *  cmd,
int  pos,
wchar_t *  quote,
wchar_t **  offset,
wchar_t **  string,
int *  type
[static]
 

Calculates information on the parameter at the specified index.

Parameters:
cmd The command to be analyzed
pos An index in the string which is inside the parameter
quote If not 0, store the type of quote this parameter has, can be either ', " or \0, meaning the string is not quoted.
offset If not 0, get_param will store a pointer to the beginning of the parameter.
string If not 0, get_parm will store a copy of the parameter string as returned by the tokenizer.
type If not 0, get_param will store the token type as returned by tok_last.

static wchar_t get_quote wchar_t *  cmd,
int  len
[static]
 

Find the outermost quoting style of current token.

Returns 0 if token is not quoted.

static int handle_completions array_list_t comp  )  [static]
 

Handle the list of completions.

This means the following:

  • If the list is empty, flash the terminal.
  • If the list contains one element, write the whole element, and if the element does not end on a '/', '@', ':', or a '=', also write a trailing space.
  • If the list contains multiple elements with a common prefix, write the prefix.
  • If the list contains multiple elements without. a common prefix, call run_pager to display a list of completions. Depending on terminal size and the length of the list, run_pager may either show less than a screenfull and exit or use an interactive pager to allow the user to scroll through the completions.

Parameters:
comp the list of completion strings

static void handle_end_loop  )  [static]
 

This function is called when the main loop notices that end_loop has been set while in interactive mode.

It checks if it is ok to exit.

static void handle_history const wchar_t *  new_str  )  [static]
 

Set the specified string from the history as the current buffer.

Do not modify prefix_width.

static void handle_token_history int  forward,
int  reset
[static]
 

Handles a token search command.

Parameters:
forward if the search should be forward or reverse
reset whether the current token should be made the new search token

static void move_word int  dir,
int  erase,
int  new
[static]
 

Move buffer position one word or erase one word.

This function updates both the internal buffer and the screen. It is used by M-left, M-right and ^W to do block movement or block erase.

Parameters:
dir Direction to move/erase. 0 means move left, 1 means move right.
erase Whether to erase the characters along the way or only move past them.
do_append if erase is true, this flag decides if the new kill item should be appended to the previous kill item.

static int read_i  )  [static]
 

Read interactively.

Read input from stdin while providing editing facilities.

static int read_ni int  fd,
io_data_t io
[static]
 

Read non-interactively.

Read input from stdin without displaying the prompt, using syntax highlighting. This is used for reading scripts and init files.

int reader_exit_forced  ) 
 

This function returns true if fish is exiting by force, i.e.

because stdin died

int reader_get_cursor_pos  ) 
 

Get the current cursor position in the command line.

If interactive mode is uninitialized, return -1.

void reader_handle_int int  signal  ) 
 

The readers interrupt signal handler.

Cancels all currently running blocks.

void reader_push_current_filename const wchar_t *  fn  ) 
 

Push a new filename on the stack of read files.

Parameters:
fn The fileanme to push

wchar_t* reader_readline  ) 
 

Read one line of input.

Remove the current character in the character buffer and on the screen using syntax highlighting, etc.

static void reader_repaint  )  [static]
 

Repaint the entire commandline.

This means reset and clear the commandline, write the prompt, perform syntax highlighting, write the commandline and move the cursor.

int reader_search_mode  ) 
 

Test whether the interactive reader is in search mode.

Returns:
o if not in search mode, 1 if in search mode and -1 if not in interactive mode

void reader_set_buffer wchar_t *  b,
int  p
 

Set the string of characters in the command buffer, as well as the cursor position.

Parameters:
b the new buffer value
p the cursor position. If p is less than zero, the cursor is placed on the last character.

void reader_set_complete_function void(*)(const wchar_t *, array_list_t *)  f  ) 
 

Specify function to use for finding possible tab completions.

The function must take these arguments:

  • The command to be completed as a null terminated array of wchar_t
  • An array_list_t in which completions will be inserted.

void reader_set_highlight_function void(*)(wchar_t *, int *, int, array_list_t *)  f  ) 
 

Specify function for syntax highlighting.

The function must take these arguments:

  • The command to be highlighted as a null terminated array of wchar_t
  • The color code of each character as an array of ints
  • The cursor position
  • An array_list_t used for storing error messages

int reader_shell_test wchar_t *  b  ) 
 

Test if the given shell command contains errors.

Uses parser_test for testing. Suitable for reader_set_test_function().

static void reader_super_highlight_me_plenty int  match_highlight_pos,
array_list_t error
[static]
 

Call specified external highlighting function and then do search highlighting.

Lastly, clear the background color under the cursor to avoid repaint issues on terminals where e.g. syntax highligthing maykes characters under the sursor unreadable.

Parameters:
match_highlight_pos the position to use for bracket matching. This need not be the same as the surrent cursor position
error if non-null, any possible errors in the buffer are further descibed by the strings inserted into the specified arraylist

void reader_write_title  ) 
 

Write the title to the titlebar.

This function is called just before a new application starts executing and just after it finishes.

static void remove_duplicates array_list_t l  )  [static]
 

Remove any duplicate completions in the list.

This relies on the list first beeing sorted.

static void run_pager wchar_t *  prefix,
int  is_quoted,
array_list_t comp
[static]
 

Run the fish_pager command to display the completion list.

If the fish_pager outputs any text, it is inserted into the input backbuffer.

Parameters:
prefix the string to display before every completion.
is_quoted should be set if the argument is quoted. This will change the display style.
comp the list of completions to display


Variable Documentation

int is_interactive_read [static]
 

This flag is set to true when fish is interactively reading from stdin.

It changes how a ^C is handled by the fish interrupt handler.


Generated on Sun Jan 13 02:53:07 2008 for fish by  doxygen 1.4.4