2005-09-20 13:26:39 +00:00
|
|
|
/** \file reader.c
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2012-01-16 16:56:47 +00:00
|
|
|
#include <algorithm>
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2006-08-09 22:53:38 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_SYS_TERMIOS_H
|
|
|
|
#include <sys/termios.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <sys/ioctl.h>
|
2006-08-09 22:53:38 +00:00
|
|
|
#endif
|
|
|
|
|
2006-11-30 23:57:49 +00:00
|
|
|
#include <time.h>
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/wait.h>
|
2005-10-13 14:08:33 +00:00
|
|
|
#include <sys/poll.h>
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <wctype.h>
|
|
|
|
|
|
|
|
#if HAVE_NCURSES_H
|
|
|
|
#include <ncurses.h>
|
|
|
|
#else
|
|
|
|
#include <curses.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAVE_TERMIO_H
|
|
|
|
#include <termio.h>
|
|
|
|
#endif
|
|
|
|
|
2006-01-19 12:22:07 +00:00
|
|
|
#if HAVE_TERM_H
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <term.h>
|
2006-01-19 12:22:07 +00:00
|
|
|
#elif HAVE_NCURSES_TERM_H
|
|
|
|
#include <ncurses/term.h>
|
|
|
|
#endif
|
|
|
|
|
2006-07-30 20:26:59 +00:00
|
|
|
#ifdef HAVE_SIGINFO_H
|
|
|
|
#include <siginfo.h>
|
|
|
|
#endif
|
|
|
|
|
2006-08-09 22:26:05 +00:00
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
|
2005-12-25 22:00:44 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2006-02-28 13:17:16 +00:00
|
|
|
|
|
|
|
#include "fallback.h"
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "util.h"
|
2006-02-28 13:17:16 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#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"
|
2005-10-15 00:51:26 +00:00
|
|
|
#include "signal.h"
|
2006-10-01 16:02:58 +00:00
|
|
|
#include "screen.h"
|
2007-02-09 09:33:50 +00:00
|
|
|
#include "halloc.h"
|
|
|
|
#include "halloc_util.h"
|
2011-12-27 03:18:46 +00:00
|
|
|
#include "iothread.h"
|
2006-07-19 22:55:49 +00:00
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
#include "parse_util.h"
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
2005-12-30 16:29:19 +00:00
|
|
|
Maximum length of prefix string when printing completion
|
|
|
|
list. Longer prefixes will be ellipsized.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
|
|
|
#define PREFIX_MAX_LEN 8
|
|
|
|
|
|
|
|
/**
|
|
|
|
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.
|
|
|
|
*/
|
2007-08-22 08:00:52 +00:00
|
|
|
#define DEFAULT_PROMPT L"echo \"$USER@\"; hostname|cut -d . -f 1; echo \" \"; pwd; printf '> ';"
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2008-01-13 16:47:47 +00:00
|
|
|
/**
|
|
|
|
The name of the function that prints the fish prompt
|
|
|
|
*/
|
2007-01-15 17:51:44 +00:00
|
|
|
#define PROMPT_FUNCTION_NAME L"fish_prompt"
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
|
|
|
The default title for the reader. This is used by reader_readline.
|
|
|
|
*/
|
|
|
|
#define DEFAULT_TITLE L"echo $_ \" \"; pwd"
|
|
|
|
|
2005-10-13 14:08:33 +00:00
|
|
|
/**
|
|
|
|
The maximum number of characters to read from the keyboard without
|
2005-10-26 10:51:02 +00:00
|
|
|
repainting. Note that this readahead will only occur if new
|
2005-10-13 14:08:33 +00:00
|
|
|
characters are avaialble for reading, fish will never block for
|
|
|
|
more input without repainting.
|
|
|
|
*/
|
|
|
|
#define READAHEAD_MAX 256
|
|
|
|
|
2008-01-13 16:47:47 +00:00
|
|
|
/**
|
|
|
|
A mode for calling the reader_kill function. In this mode, the new
|
|
|
|
string is appended to the current contents of the kill buffer.
|
|
|
|
*/
|
2006-10-12 13:27:32 +00:00
|
|
|
#define KILL_APPEND 0
|
2008-01-13 16:47:47 +00:00
|
|
|
/**
|
|
|
|
A mode for calling the reader_kill function. In this mode, the new
|
|
|
|
string is prepended to the current contents of the kill buffer.
|
|
|
|
*/
|
2006-10-12 13:27:32 +00:00
|
|
|
#define KILL_PREPEND 1
|
|
|
|
|
2008-01-13 16:47:47 +00:00
|
|
|
/**
|
|
|
|
History search mode. This value means that no search is currently
|
|
|
|
performed.
|
|
|
|
*/
|
2007-04-16 21:26:15 +00:00
|
|
|
#define NO_SEARCH 0
|
2008-01-13 16:47:47 +00:00
|
|
|
/**
|
|
|
|
History search mode. This value means that we are perforing a line
|
|
|
|
history search.
|
|
|
|
*/
|
2007-04-16 21:26:15 +00:00
|
|
|
#define LINE_SEARCH 1
|
2008-01-13 16:47:47 +00:00
|
|
|
/**
|
|
|
|
History search mode. This value means that we are perforing a token
|
|
|
|
history search.
|
|
|
|
*/
|
2007-04-16 21:26:15 +00:00
|
|
|
#define TOKEN_SEARCH 2
|
|
|
|
|
2008-01-13 16:47:47 +00:00
|
|
|
/**
|
|
|
|
History search mode. This value means we are searching backwards.
|
|
|
|
*/
|
2007-04-16 21:26:15 +00:00
|
|
|
#define SEARCH_BACKWARD 0
|
2008-01-13 16:47:47 +00:00
|
|
|
/**
|
|
|
|
History search mode. This value means we are searching forwards.
|
|
|
|
*/
|
2007-04-16 21:26:15 +00:00
|
|
|
#define SEARCH_FORWARD 1
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
|
|
|
A struct describing the state of the interactive reader. These
|
2007-01-15 17:53:46 +00:00
|
|
|
states can be stacked, in case reader_readline() calls are
|
|
|
|
nested. This happens when the 'read' builtin is used.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2011-12-28 02:41:38 +00:00
|
|
|
class reader_data_t
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2011-12-28 02:41:38 +00:00
|
|
|
public:
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
2006-10-01 15:59:18 +00:00
|
|
|
Buffer containing the whole current commandline
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
|
|
|
wchar_t *buff;
|
|
|
|
|
2008-01-13 16:47:47 +00:00
|
|
|
/**
|
|
|
|
The representation of the current screen contents
|
|
|
|
*/
|
2006-10-01 16:02:58 +00:00
|
|
|
screen_t screen;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Buffer containing the current search item
|
|
|
|
*/
|
2007-04-16 21:26:15 +00:00
|
|
|
string_buffer_t search_buff;
|
2006-10-01 15:59:18 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
2005-12-30 16:29:19 +00:00
|
|
|
Saved position used by token history search
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
|
|
|
int token_history_pos;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Saved search string for token history search. Not handled by check_size.
|
|
|
|
*/
|
|
|
|
const wchar_t *token_history_buff;
|
|
|
|
|
|
|
|
/**
|
|
|
|
List for storing previous search results. Used to avoid duplicates.
|
|
|
|
*/
|
|
|
|
array_list_t search_prev;
|
|
|
|
|
|
|
|
/**
|
|
|
|
The current position in search_prev
|
|
|
|
*/
|
|
|
|
|
|
|
|
int search_pos;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Current size of the buffers
|
|
|
|
*/
|
|
|
|
size_t buff_sz;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Length of the command in buff. (Not the length of buff itself)
|
|
|
|
*/
|
|
|
|
|
|
|
|
size_t buff_len;
|
|
|
|
|
|
|
|
/**
|
2005-12-30 16:29:19 +00:00
|
|
|
The current position of the cursor in buff.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
|
|
|
size_t buff_pos;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Name of the current application
|
|
|
|
*/
|
2011-12-28 20:36:47 +00:00
|
|
|
wcstring app_name;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-10-01 16:02:58 +00:00
|
|
|
/** The prompt command */
|
2011-12-28 20:36:47 +00:00
|
|
|
wcstring prompt;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-10-01 16:02:58 +00:00
|
|
|
/** The output of the last evaluation of the prompt command */
|
2011-12-28 20:36:47 +00:00
|
|
|
wcstring prompt_buff;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
|
|
|
Color is the syntax highlighting for buff. The format is that
|
|
|
|
color[i] is the classification (according to the enum in
|
|
|
|
highlight.h) of buff[i].
|
|
|
|
*/
|
|
|
|
int *color;
|
|
|
|
|
|
|
|
/**
|
2006-10-16 15:32:26 +00:00
|
|
|
An array defining the block level at each character.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2006-10-07 00:56:25 +00:00
|
|
|
int *indent;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Function for tab completion
|
|
|
|
*/
|
|
|
|
void (*complete_func)( const wchar_t *,
|
2012-01-16 16:56:47 +00:00
|
|
|
std::vector<completion_t>& );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Function for syntax highlighting
|
|
|
|
*/
|
2011-12-27 03:18:46 +00:00
|
|
|
highlight_function_t highlight_function;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Function for testing if the string can be returned
|
|
|
|
*/
|
|
|
|
int (*test_func)( wchar_t * );
|
|
|
|
|
|
|
|
/**
|
2005-12-30 16:29:19 +00:00
|
|
|
When this is true, the reader will exit
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
|
|
|
int end_loop;
|
2006-10-01 16:02:58 +00:00
|
|
|
|
2006-05-14 10:16:23 +00:00
|
|
|
/**
|
|
|
|
If this is true, exit reader even if there are running
|
|
|
|
jobs. This happens if we press e.g. ^D twice.
|
|
|
|
*/
|
|
|
|
int prev_end_loop;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2008-01-13 16:47:47 +00:00
|
|
|
/**
|
2011-12-27 03:18:46 +00:00
|
|
|
The current contents of the top item in the kill ring.
|
2008-01-13 16:47:47 +00:00
|
|
|
*/
|
2006-10-12 13:27:32 +00:00
|
|
|
string_buffer_t kill_item;
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
|
|
|
Pointer to previous reader_data
|
|
|
|
*/
|
2011-12-28 02:41:38 +00:00
|
|
|
reader_data_t *next;
|
2007-09-21 14:05:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
This variable keeps state on if we are in search mode, and
|
|
|
|
if yes, what mode
|
|
|
|
*/
|
|
|
|
int search_mode;
|
2007-10-05 14:59:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Keep track of whether any internal code has done something
|
|
|
|
which is known to require a repaint.
|
|
|
|
*/
|
|
|
|
int repaint_needed;
|
2011-12-28 02:41:38 +00:00
|
|
|
};
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
The current interactive reading context
|
|
|
|
*/
|
|
|
|
static reader_data_t *data=0;
|
|
|
|
|
2007-01-31 23:58:10 +00:00
|
|
|
/**
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
static int is_interactive_read;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
2005-12-30 16:29:19 +00:00
|
|
|
Flag for ending non-interactive shell
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
|
|
|
static int end_loop = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
The list containing names of files that are being parsed
|
|
|
|
*/
|
|
|
|
static array_list_t current_filename;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Store the pid of the parent process, so the exit function knows whether it should reset the terminal or not.
|
|
|
|
*/
|
|
|
|
static pid_t original_pid;
|
|
|
|
|
2006-01-23 20:40:14 +00:00
|
|
|
/**
|
|
|
|
This variable is set to true by the signal handler when ^C is pressed
|
|
|
|
*/
|
2006-10-04 21:42:04 +00:00
|
|
|
static int interrupted=0;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-01-23 20:40:14 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/*
|
|
|
|
Prototypes for a bunch of functions defined later on.
|
|
|
|
*/
|
|
|
|
|
2006-03-10 13:38:09 +00:00
|
|
|
/**
|
|
|
|
Stores the previous termios mode so we can reset the modes when
|
|
|
|
we execute programs and when the shell exits.
|
|
|
|
*/
|
|
|
|
static struct termios saved_modes;
|
|
|
|
|
2006-10-16 15:32:26 +00:00
|
|
|
static void reader_super_highlight_me_plenty( int pos, array_list_t *error );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-05-14 10:16:23 +00:00
|
|
|
/**
|
|
|
|
Variable to keep track of forced exits - see \c reader_exit_forced();
|
|
|
|
*/
|
|
|
|
static int exit_forced;
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-01-23 20:40:14 +00:00
|
|
|
/**
|
|
|
|
Give up control of terminal
|
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
static void term_donate()
|
|
|
|
{
|
|
|
|
set_color(FISH_COLOR_NORMAL, FISH_COLOR_NORMAL);
|
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
if( tcsetattr(0,TCSANOW,&saved_modes) )
|
|
|
|
{
|
|
|
|
if( errno != EINTR )
|
|
|
|
{
|
2006-01-04 12:51:02 +00:00
|
|
|
debug( 1, _( L"Could not set terminal mode for new job" ) );
|
2005-09-20 13:26:39 +00:00
|
|
|
wperror( L"tcsetattr" );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2006-01-23 20:40:14 +00:00
|
|
|
/**
|
|
|
|
Grab control of terminal
|
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
static void term_steal()
|
2006-01-30 19:53:10 +00:00
|
|
|
{
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
if( tcsetattr(0,TCSANOW,&shell_modes) )
|
|
|
|
{
|
|
|
|
if( errno != EINTR )
|
|
|
|
{
|
2006-01-04 12:51:02 +00:00
|
|
|
debug( 1, _( L"Could not set terminal mode for shell" ) );
|
2005-09-20 13:26:39 +00:00
|
|
|
wperror( L"tcsetattr" );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-01-30 19:53:10 +00:00
|
|
|
common_handle_winch(0 );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-05-14 10:16:23 +00:00
|
|
|
int reader_exit_forced()
|
|
|
|
{
|
|
|
|
return exit_forced;
|
|
|
|
}
|
|
|
|
|
2007-10-05 14:59:19 +00:00
|
|
|
/**
|
|
|
|
Repaint the entire commandline. This means reset and clear the
|
|
|
|
commandline, write the prompt, perform syntax highlighting, write
|
|
|
|
the commandline and move the cursor.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void reader_repaint()
|
|
|
|
{
|
2012-01-23 05:40:08 +00:00
|
|
|
//PCA INSTANCED_PARSER what is this call for?
|
2012-01-27 19:43:52 +00:00
|
|
|
parser_t::principal_parser().test( data->buff, data->indent, 0, 0 );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-10-05 14:59:19 +00:00
|
|
|
s_write( &data->screen,
|
2011-12-28 20:36:47 +00:00
|
|
|
data->prompt_buff.c_str(),
|
2007-10-05 14:59:19 +00:00
|
|
|
data->buff,
|
2011-12-27 03:18:46 +00:00
|
|
|
data->color,
|
|
|
|
data->indent,
|
2007-10-05 14:59:19 +00:00
|
|
|
data->buff_pos );
|
|
|
|
data->repaint_needed = 0;
|
|
|
|
}
|
|
|
|
|
2006-10-25 20:36:08 +00:00
|
|
|
/**
|
|
|
|
Internal helper function for handling killing parts of text.
|
|
|
|
*/
|
2011-12-27 03:18:46 +00:00
|
|
|
static void reader_kill( wchar_t *begin, int length, int mode, int newv )
|
2006-10-12 13:27:32 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
if( newv )
|
2006-10-12 13:27:32 +00:00
|
|
|
{
|
|
|
|
sb_clear( &data->kill_item );
|
|
|
|
sb_append_substring( &data->kill_item, begin, length );
|
|
|
|
kill_add( (wchar_t *)data->kill_item.buff );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
wchar_t *old = wcsdup( (wchar_t *)data->kill_item.buff);
|
|
|
|
|
|
|
|
if( mode == KILL_APPEND )
|
|
|
|
{
|
|
|
|
sb_append_substring( &data->kill_item, begin, length );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sb_clear( &data->kill_item );
|
|
|
|
sb_append_substring( &data->kill_item, begin, length );
|
|
|
|
sb_append( &data->kill_item, old );
|
|
|
|
}
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-12 13:27:32 +00:00
|
|
|
kill_replace( old, (wchar_t *)data->kill_item.buff );
|
|
|
|
free( old );
|
|
|
|
}
|
2006-10-12 19:30:00 +00:00
|
|
|
|
2012-01-15 06:48:53 +00:00
|
|
|
if( data->buff_pos > (size_t)(begin-data->buff) )
|
2006-10-12 19:30:00 +00:00
|
|
|
{
|
|
|
|
data->buff_pos = maxi( begin-data->buff, data->buff_pos-length );
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-12 19:30:00 +00:00
|
|
|
data->buff_len -= length;
|
|
|
|
memmove( begin, begin+length, sizeof( wchar_t )*(wcslen( begin+length )+1) );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-16 15:32:26 +00:00
|
|
|
reader_super_highlight_me_plenty( data->buff_pos, 0 );
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-12 13:27:32 +00:00
|
|
|
}
|
2006-05-14 10:16:23 +00:00
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
void reader_handle_int( int sig )
|
|
|
|
{
|
2012-01-23 05:40:08 +00:00
|
|
|
//PCA INSTANCED_PARSER what is this?
|
|
|
|
block_t *c = NULL;//current_block;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-01-31 23:58:10 +00:00
|
|
|
if( !is_interactive_read )
|
2005-10-05 22:37:08 +00:00
|
|
|
{
|
2007-01-31 23:58:10 +00:00
|
|
|
while( c )
|
|
|
|
{
|
|
|
|
c->type=FAKE;
|
|
|
|
c->skip=1;
|
|
|
|
c=c->outer;
|
|
|
|
}
|
2005-10-05 22:37:08 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-04 21:42:04 +00:00
|
|
|
interrupted = 1;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
wchar_t *reader_current_filename()
|
|
|
|
{
|
2006-01-26 14:48:10 +00:00
|
|
|
return al_get_count( ¤t_filename )?(wchar_t *)al_peek( ¤t_filename ):0;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-02 15:23:56 +00:00
|
|
|
void reader_push_current_filename( const wchar_t *fn )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
al_push( ¤t_filename, fn );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wchar_t *reader_pop_current_filename()
|
|
|
|
{
|
|
|
|
return (wchar_t *)al_pop( ¤t_filename );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Make sure buffers are large enough to hold current data plus one extra character.
|
|
|
|
*/
|
|
|
|
static int check_size()
|
|
|
|
{
|
|
|
|
if( data->buff_sz < data->buff_len + 2 )
|
|
|
|
{
|
|
|
|
data->buff_sz = maxi( 128, data->buff_len*2 );
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
data->buff = (wchar_t *)realloc( data->buff,
|
2005-09-20 13:26:39 +00:00
|
|
|
sizeof(wchar_t)*data->buff_sz);
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
data->color = (int *)realloc( data->color,
|
2005-09-20 13:26:39 +00:00
|
|
|
sizeof(int)*data->buff_sz);
|
2006-10-01 16:02:58 +00:00
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
data->indent = (int *)realloc( data->indent,
|
2006-10-07 00:56:25 +00:00
|
|
|
sizeof(int)*data->buff_sz);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
if( data->buff==0 ||
|
|
|
|
data->color==0 ||
|
2006-10-07 00:56:25 +00:00
|
|
|
data->indent == 0 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-07-03 10:39:57 +00:00
|
|
|
DIE_MEM();
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-02-18 23:25:20 +00:00
|
|
|
|
2008-01-13 16:47:47 +00:00
|
|
|
/**
|
|
|
|
Compare two completion entrys
|
|
|
|
*/
|
2012-01-16 16:56:47 +00:00
|
|
|
/*
|
2007-02-18 23:25:20 +00:00
|
|
|
static int completion_cmp( const void *a, const void *b )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2007-02-18 23:25:20 +00:00
|
|
|
completion_t *c= *((completion_t **)a);
|
|
|
|
completion_t *d= *((completion_t **)b);
|
|
|
|
|
|
|
|
return wcsfilecmp( c->completion, d->completion );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
}
|
2012-01-16 16:56:47 +00:00
|
|
|
*/
|
2008-01-13 16:47:47 +00:00
|
|
|
/**
|
|
|
|
Sort an array_list_t containing compltion_t structs.
|
|
|
|
*/
|
2012-01-16 16:56:47 +00:00
|
|
|
/*
|
2007-02-18 23:25:20 +00:00
|
|
|
static void sort_completion_list( array_list_t *comp )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
qsort( comp->arr,
|
2007-02-18 23:25:20 +00:00
|
|
|
al_get_count( comp ),
|
|
|
|
sizeof( void*),
|
|
|
|
&completion_cmp );
|
|
|
|
}
|
2012-01-16 16:56:47 +00:00
|
|
|
*/
|
|
|
|
static void sort_completion_list( std::vector<completion_t> &comp ) {
|
|
|
|
sort(comp.begin(), comp.end());
|
|
|
|
}
|
2007-02-18 23:25:20 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
|
|
|
Remove any duplicate completions in the list. This relies on the
|
|
|
|
list first beeing sorted.
|
|
|
|
*/
|
2012-01-16 16:56:47 +00:00
|
|
|
/*
|
2005-09-20 13:26:39 +00:00
|
|
|
static void remove_duplicates( array_list_t *l )
|
|
|
|
{
|
|
|
|
int in, out;
|
2007-02-18 23:25:20 +00:00
|
|
|
const wchar_t *prev;
|
2007-03-17 23:16:23 +00:00
|
|
|
completion_t *first;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( al_get_count( l ) == 0 )
|
|
|
|
return;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-03-17 23:16:23 +00:00
|
|
|
first = (completion_t *)al_get( l, 0 );
|
|
|
|
prev = first->completion;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
for( in=1, out=1; in < al_get_count( l ); in++ )
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
2007-02-18 23:25:20 +00:00
|
|
|
completion_t *curr = (completion_t *)al_get( l, in );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-18 23:25:20 +00:00
|
|
|
if( wcscmp( prev, curr->completion )!=0 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
al_set( l, out++, curr );
|
|
|
|
}
|
2007-03-17 23:16:23 +00:00
|
|
|
prev = curr->completion;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
al_truncate( l, out );
|
|
|
|
}
|
2012-01-16 16:56:47 +00:00
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-01-16 16:56:47 +00:00
|
|
|
static void remove_duplicates(std::vector<completion_t> &l) {
|
|
|
|
|
|
|
|
l.erase(std::unique( l.begin(), l.end()), l.end());
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-10-04 21:42:04 +00:00
|
|
|
int reader_interrupted()
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-10-04 21:42:04 +00:00
|
|
|
int res=interrupted;
|
2005-09-20 13:26:39 +00:00
|
|
|
if( res )
|
2006-10-04 21:42:04 +00:00
|
|
|
interrupted=0;
|
2005-09-20 13:26:39 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reader_write_title()
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
const wchar_t *title;
|
2012-01-14 10:42:17 +00:00
|
|
|
const env_var_t term_str = env_get_string( L"TERM" );
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/*
|
|
|
|
This is a pretty lame heuristic for detecting terminals that do
|
|
|
|
not support setting the title. If we recognise the terminal name
|
|
|
|
as that of a virtual terminal, we assume it supports setting the
|
2007-03-17 23:38:46 +00:00
|
|
|
title. If we recognise it as that of a console, we assume it
|
|
|
|
does not support setting the title. Otherwise we check the
|
|
|
|
ttyname and see if we belive it is a virtual terminal.
|
|
|
|
|
|
|
|
One situation in which this breaks down is with screen, since
|
|
|
|
screen supports setting the terminal title if the underlying
|
|
|
|
terminal does so, but will print garbage on terminals that
|
|
|
|
don't. Since we can't see the underlying terminal below screen
|
|
|
|
there is no way to fix this.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2012-01-14 10:42:17 +00:00
|
|
|
if ( term_str.missing() )
|
2010-10-06 12:58:13 +00:00
|
|
|
return;
|
|
|
|
|
2012-01-14 10:42:17 +00:00
|
|
|
const wchar_t *term = term_str.c_str();
|
2012-01-02 21:40:03 +00:00
|
|
|
bool recognized = false;
|
|
|
|
recognized = recognized || contains( term, L"xterm", L"screen", L"nxterm", L"rxvt" );
|
|
|
|
recognized = recognized || ! wcsncmp(term, L"xterm-", wcslen(L"xterm-"));
|
|
|
|
|
|
|
|
if( ! recognized )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
char *n = ttyname( STDIN_FILENO );
|
2007-03-17 23:38:46 +00:00
|
|
|
|
2010-10-06 12:58:13 +00:00
|
|
|
|
2007-09-28 21:32:27 +00:00
|
|
|
if( contains( term, L"linux" ) )
|
2007-03-17 23:38:46 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( strstr( n, "tty" ) || strstr( n, "/vc/") )
|
|
|
|
return;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
title = function_exists( L"fish_title" )?L"fish_title":DEFAULT_TITLE;
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
if( wcslen( title ) ==0 )
|
2005-09-20 13:26:39 +00:00
|
|
|
return;
|
|
|
|
|
2011-12-28 20:36:47 +00:00
|
|
|
wcstring_list_t lst;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-02-16 13:36:32 +00:00
|
|
|
proc_push_interactive(0);
|
2011-12-28 20:36:47 +00:00
|
|
|
if( exec_subshell2( title, lst ) != -1 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-01-15 06:00:00 +00:00
|
|
|
size_t i;
|
2011-12-28 20:36:47 +00:00
|
|
|
if( lst.size() > 0 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2007-08-22 07:52:39 +00:00
|
|
|
writestr( L"\x1b]2;" );
|
2011-12-28 20:36:47 +00:00
|
|
|
for( i=0; i<lst.size(); i++ )
|
2006-08-09 11:34:24 +00:00
|
|
|
{
|
2011-12-28 20:36:47 +00:00
|
|
|
writestr( lst.at(i).c_str() );
|
2006-08-09 11:34:24 +00:00
|
|
|
}
|
|
|
|
writestr( L"\7" );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-28 20:36:47 +00:00
|
|
|
proc_pop_interactive();
|
2005-09-20 13:26:39 +00:00
|
|
|
set_color( FISH_COLOR_RESET, FISH_COLOR_RESET );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-10-16 15:32:26 +00:00
|
|
|
Reexecute the prompt command. The output is inserted into data->prompt_buff.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2006-10-16 15:32:26 +00:00
|
|
|
static void exec_prompt()
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-01-10 20:51:09 +00:00
|
|
|
size_t i;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-01-10 20:51:09 +00:00
|
|
|
wcstring_list_t prompt_list;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2011-12-28 20:36:47 +00:00
|
|
|
if( data->prompt.size() )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-10-16 15:32:26 +00:00
|
|
|
proc_push_interactive( 0 );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-01-10 20:51:09 +00:00
|
|
|
if( exec_subshell2( data->prompt.c_str(), prompt_list ) == -1 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-10-16 15:32:26 +00:00
|
|
|
/* If executing the prompt fails, make sure we at least don't print any junk */
|
2012-01-10 20:51:09 +00:00
|
|
|
prompt_list.clear();
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2006-10-16 15:32:26 +00:00
|
|
|
proc_pop_interactive();
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-16 15:32:26 +00:00
|
|
|
reader_write_title();
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2011-12-28 20:36:47 +00:00
|
|
|
data->prompt_buff.resize(0);
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-01-10 20:51:09 +00:00
|
|
|
for( i = 0; i < prompt_list.size(); i++ )
|
2006-10-16 15:32:26 +00:00
|
|
|
{
|
2012-01-10 20:51:09 +00:00
|
|
|
if (i > 0) data->prompt_buff += L'\n';
|
|
|
|
data->prompt_buff += prompt_list.at(i);
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void reader_init()
|
|
|
|
{
|
2006-08-24 10:43:54 +00:00
|
|
|
|
2006-03-10 13:38:09 +00:00
|
|
|
tcgetattr(0,&shell_modes); /* get the current terminal modes */
|
|
|
|
memcpy( &saved_modes,
|
|
|
|
&shell_modes,
|
|
|
|
sizeof(saved_modes)); /* save a copy so we can reset the terminal later */
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-03-10 13:38:09 +00:00
|
|
|
shell_modes.c_lflag &= ~ICANON; /* turn off canonical mode */
|
|
|
|
shell_modes.c_lflag &= ~ECHO; /* turn off echo mode */
|
|
|
|
shell_modes.c_cc[VMIN]=1;
|
|
|
|
shell_modes.c_cc[VTIME]=0;
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
al_init( ¤t_filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void reader_destroy()
|
|
|
|
{
|
|
|
|
al_destroy( ¤t_filename);
|
2006-03-10 13:38:09 +00:00
|
|
|
tcsetattr(0, TCSANOW, &saved_modes);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-14 10:16:23 +00:00
|
|
|
void reader_exit( int do_exit, int forced )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-02-08 09:24:29 +00:00
|
|
|
if( data )
|
2005-09-20 13:26:39 +00:00
|
|
|
data->end_loop=do_exit;
|
2006-02-08 09:24:29 +00:00
|
|
|
end_loop=do_exit;
|
2006-05-14 10:16:23 +00:00
|
|
|
if( forced )
|
|
|
|
exit_forced = 1;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2007-10-05 14:59:19 +00:00
|
|
|
void reader_repaint_needed()
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2007-10-05 14:59:19 +00:00
|
|
|
if( data )
|
|
|
|
{
|
|
|
|
data->repaint_needed = 1;
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2007-09-21 14:05:49 +00:00
|
|
|
|
2007-10-05 14:59:19 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
|
|
|
Remove the previous character in the character buffer and on the
|
|
|
|
screen using syntax highlighting, etc.
|
|
|
|
*/
|
|
|
|
static void remove_backward()
|
|
|
|
{
|
|
|
|
|
|
|
|
if( data->buff_pos <= 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( data->buff_pos < data->buff_len )
|
|
|
|
{
|
|
|
|
memmove( &data->buff[data->buff_pos-1],
|
|
|
|
&data->buff[data->buff_pos],
|
|
|
|
sizeof(wchar_t)*(data->buff_len-data->buff_pos+1) );
|
|
|
|
}
|
|
|
|
data->buff_pos--;
|
|
|
|
data->buff_len--;
|
2006-10-01 16:02:58 +00:00
|
|
|
data->buff[data->buff_len]=0;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-10-16 15:32:26 +00:00
|
|
|
reader_super_highlight_me_plenty( data->buff_pos,
|
2005-09-20 13:26:39 +00:00
|
|
|
0 );
|
|
|
|
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2006-10-01 16:02:58 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Insert the characters of the string into the command line buffer
|
|
|
|
and print them to the screen using syntax highlighting, etc.
|
|
|
|
*/
|
2012-01-14 11:41:50 +00:00
|
|
|
static int insert_str(const wchar_t *str)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2005-10-13 14:08:33 +00:00
|
|
|
int len = wcslen( str );
|
2012-01-15 06:00:00 +00:00
|
|
|
size_t old_len = data->buff_len;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-12 13:27:32 +00:00
|
|
|
assert( data->buff_pos >= 0 );
|
|
|
|
assert( data->buff_pos <= data->buff_len );
|
|
|
|
assert( len >= 0 );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-01 16:02:58 +00:00
|
|
|
data->buff_len += len;
|
|
|
|
check_size();
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-01 16:02:58 +00:00
|
|
|
/* Insert space for extra characters at the right position */
|
|
|
|
if( data->buff_pos < old_len )
|
2005-10-13 14:08:33 +00:00
|
|
|
{
|
2006-10-01 16:02:58 +00:00
|
|
|
memmove( &data->buff[data->buff_pos+len],
|
|
|
|
&data->buff[data->buff_pos],
|
2007-01-07 14:04:10 +00:00
|
|
|
sizeof(wchar_t)*(old_len-data->buff_pos) );
|
2005-10-13 14:08:33 +00:00
|
|
|
}
|
2006-10-01 16:02:58 +00:00
|
|
|
memmove( &data->buff[data->buff_pos], str, sizeof(wchar_t)*len );
|
|
|
|
data->buff_pos += len;
|
|
|
|
data->buff[data->buff_len]='\0';
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-11-02 13:50:19 +00:00
|
|
|
/*
|
2011-12-27 03:18:46 +00:00
|
|
|
Syntax highlight
|
2006-11-02 13:50:19 +00:00
|
|
|
*/
|
2006-10-16 15:32:26 +00:00
|
|
|
reader_super_highlight_me_plenty( data->buff_pos-1,
|
2006-10-01 16:02:58 +00:00
|
|
|
0 );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2005-09-20 13:26:39 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-11-02 13:50:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Insert the character into the command line buffer and print it to
|
|
|
|
the screen using syntax highlighting, etc.
|
|
|
|
*/
|
|
|
|
static int insert_char( int c )
|
|
|
|
{
|
|
|
|
wchar_t str[]=
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
0, 0
|
2006-11-02 13:50:19 +00:00
|
|
|
}
|
|
|
|
;
|
|
|
|
str[0] = c;
|
|
|
|
return insert_str( str );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
|
|
|
Calculate the length of the common prefix substring of two strings.
|
|
|
|
*/
|
2007-02-18 23:25:20 +00:00
|
|
|
static int comp_len( const wchar_t *a, const wchar_t *b )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for( i=0;
|
|
|
|
a[i] != '\0' && b[i] != '\0' && a[i]==b[i];
|
|
|
|
i++ )
|
|
|
|
;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2007-02-28 21:43:27 +00:00
|
|
|
/**
|
|
|
|
Calculate the case insensitive length of the common prefix substring of two strings.
|
|
|
|
*/
|
|
|
|
static int comp_ilen( const wchar_t *a, const wchar_t *b )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for( i=0;
|
|
|
|
a[i] != '\0' && b[i] != '\0' && towlower(a[i])==towlower(b[i]);
|
|
|
|
i++ )
|
|
|
|
;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
2006-11-02 13:50:19 +00:00
|
|
|
Find the outermost quoting style of current token. Returns 0 if
|
|
|
|
token is not quoted.
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
*/
|
2006-11-02 13:50:19 +00:00
|
|
|
static wchar_t get_quote( wchar_t *cmd, int len )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
int i=0;
|
|
|
|
wchar_t res=0;
|
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
if( !cmd[i] )
|
|
|
|
break;
|
|
|
|
|
2006-09-14 15:18:21 +00:00
|
|
|
if( cmd[i] == L'\\' )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-09-14 15:18:21 +00:00
|
|
|
i++;
|
|
|
|
if( !cmd[i] )
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
i++;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
2006-09-14 15:18:21 +00:00
|
|
|
{
|
|
|
|
if( cmd[i] == L'\'' || cmd[i] == L'\"' )
|
|
|
|
{
|
|
|
|
const wchar_t *end = quote_end( &cmd[i] );
|
|
|
|
//fwprintf( stderr, L"Jump %d\n", end-cmd );
|
2006-11-02 13:50:19 +00:00
|
|
|
if(( end == 0 ) || (!*end) || (end-cmd > len))
|
2006-09-14 15:18:21 +00:00
|
|
|
{
|
|
|
|
res = cmd[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i = end-cmd+1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
i++;
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2006-09-14 15:18:21 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Calculates information on the parameter at the specified index.
|
|
|
|
|
|
|
|
\param cmd The command to be analyzed
|
|
|
|
\param pos An index in the string which is inside the parameter
|
|
|
|
\param quote If not 0, store the type of quote this parameter has, can be either ', " or \\0, meaning the string is not quoted.
|
|
|
|
\param offset If not 0, get_param will store a pointer to the beginning of the parameter.
|
2005-12-04 01:56:13 +00:00
|
|
|
\param string If not 0, get_parm will store a copy of the parameter string as returned by the tokenizer.
|
2005-09-20 13:26:39 +00:00
|
|
|
\param type If not 0, get_param will store the token type as returned by tok_last.
|
|
|
|
*/
|
|
|
|
static void get_param( wchar_t *cmd,
|
|
|
|
int pos,
|
|
|
|
wchar_t *quote,
|
|
|
|
wchar_t **offset,
|
|
|
|
wchar_t **string,
|
|
|
|
int *type )
|
|
|
|
{
|
|
|
|
int prev_pos=0;
|
|
|
|
wchar_t last_quote = '\0';
|
|
|
|
int unfinished;
|
|
|
|
|
|
|
|
tokenizer tok;
|
|
|
|
tok_init( &tok, cmd, TOK_ACCEPT_UNFINISHED );
|
|
|
|
|
|
|
|
for( ; tok_has_next( &tok ); tok_next( &tok ) )
|
|
|
|
{
|
|
|
|
if( tok_get_pos( &tok ) > pos )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if( tok_last_type( &tok ) == TOK_STRING )
|
|
|
|
last_quote = get_quote( tok_last( &tok ),
|
|
|
|
pos - tok_get_pos( &tok ) );
|
|
|
|
|
|
|
|
if( type != 0 )
|
|
|
|
*type = tok_last_type( &tok );
|
|
|
|
if( string != 0 )
|
|
|
|
wcscpy( *string, tok_last( &tok ) );
|
2005-12-04 01:56:13 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
prev_pos = tok_get_pos( &tok );
|
|
|
|
}
|
|
|
|
|
|
|
|
tok_destroy( &tok );
|
|
|
|
|
|
|
|
wchar_t c = cmd[pos];
|
|
|
|
cmd[pos]=0;
|
|
|
|
int cmdlen = wcslen( cmd );
|
|
|
|
unfinished = (cmdlen==0);
|
|
|
|
if( !unfinished )
|
|
|
|
{
|
|
|
|
unfinished = (quote != 0);
|
|
|
|
|
|
|
|
if( !unfinished )
|
|
|
|
{
|
|
|
|
if( wcschr( L" \t\n\r", cmd[cmdlen-1] ) != 0 )
|
|
|
|
{
|
|
|
|
if( ( cmdlen == 1) || (cmd[cmdlen-2] != L'\\') )
|
|
|
|
{
|
|
|
|
unfinished=1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( quote )
|
|
|
|
*quote = last_quote;
|
|
|
|
|
|
|
|
if( offset != 0 )
|
|
|
|
{
|
|
|
|
if( !unfinished )
|
|
|
|
{
|
|
|
|
while( (cmd[prev_pos] != 0) && (wcschr( L";|",cmd[prev_pos])!= 0) )
|
|
|
|
prev_pos++;
|
|
|
|
|
|
|
|
*offset = cmd + prev_pos;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*offset = cmd + pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cmd[pos]=c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Insert the string at the current cursor position. The function
|
|
|
|
checks if the string is quoted or not and correctly escapes the
|
|
|
|
string.
|
|
|
|
|
|
|
|
\param val the string to insert
|
2008-01-13 16:47:47 +00:00
|
|
|
\param flags A union of all flags describing the completion to insert. See the completion_t struct for more information on possible values.
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2007-02-28 21:43:27 +00:00
|
|
|
static void completion_insert( const wchar_t *val, int flags )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
wchar_t *replaced;
|
|
|
|
|
|
|
|
wchar_t quote;
|
2007-02-28 21:43:27 +00:00
|
|
|
int add_space = !(flags & COMPLETE_NO_SPACE);
|
2008-01-13 19:32:21 +00:00
|
|
|
int do_replace = (flags & COMPLETE_NO_CASE);
|
|
|
|
int do_escape = !(flags & COMPLETE_DONT_ESCAPE);
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2008-01-13 19:32:21 +00:00
|
|
|
// debug( 0, L"Insert completion %ls with flags %d", val, flags);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2007-02-28 21:43:27 +00:00
|
|
|
if( do_replace )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2011-09-21 13:40:45 +00:00
|
|
|
int tok_start, tok_len, move_cursor;
|
2007-02-28 21:43:27 +00:00
|
|
|
wchar_t *begin, *end;
|
|
|
|
string_buffer_t sb;
|
2008-01-09 00:00:46 +00:00
|
|
|
wchar_t *escaped;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-28 21:43:27 +00:00
|
|
|
parse_util_token_extent( data->buff, data->buff_pos, &begin, 0, 0, 0 );
|
|
|
|
end = data->buff+data->buff_pos;
|
|
|
|
|
|
|
|
tok_start = begin - data->buff;
|
|
|
|
tok_len = end-begin;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-28 21:43:27 +00:00
|
|
|
sb_init( &sb );
|
|
|
|
sb_append_substring( &sb, data->buff, begin - data->buff );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2008-01-13 19:32:21 +00:00
|
|
|
if( do_escape )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
escaped = escape( val, ESCAPE_ALL | ESCAPE_NO_QUOTED );
|
2008-01-13 19:32:21 +00:00
|
|
|
sb_append( &sb, escaped );
|
2011-09-21 13:40:45 +00:00
|
|
|
move_cursor = wcslen(escaped);
|
2008-01-13 19:32:21 +00:00
|
|
|
free( escaped );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sb_append( &sb, val );
|
2011-09-21 13:40:45 +00:00
|
|
|
move_cursor = wcslen(val);
|
2008-01-13 19:32:21 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2008-01-13 19:32:21 +00:00
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
if( add_space )
|
2007-02-28 21:43:27 +00:00
|
|
|
{
|
|
|
|
sb_append( &sb, L" " );
|
2011-09-21 13:40:45 +00:00
|
|
|
move_cursor += 1;
|
2007-02-28 21:43:27 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-28 21:43:27 +00:00
|
|
|
sb_append( &sb, end );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2011-09-21 13:40:45 +00:00
|
|
|
reader_set_buffer( (wchar_t *)sb.buff, (begin-data->buff)+move_cursor );
|
2007-02-28 21:43:27 +00:00
|
|
|
sb_destroy( &sb );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-28 21:43:27 +00:00
|
|
|
reader_super_highlight_me_plenty( data->buff_pos, 0 );
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-28 21:43:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2008-01-13 19:32:21 +00:00
|
|
|
if( do_escape )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2008-01-13 19:32:21 +00:00
|
|
|
get_param( data->buff,
|
2007-03-24 19:07:38 +00:00
|
|
|
data->buff_pos,
|
|
|
|
"e,
|
|
|
|
0, 0, 0 );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2008-01-13 19:32:21 +00:00
|
|
|
if( quote == L'\0' )
|
|
|
|
{
|
|
|
|
replaced = escape( val, ESCAPE_ALL | ESCAPE_NO_QUOTED );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int unescapable=0;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2008-01-13 19:32:21 +00:00
|
|
|
const wchar_t *pin;
|
|
|
|
wchar_t *pout;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2008-01-13 19:32:21 +00:00
|
|
|
replaced = pout =
|
2011-12-27 03:18:46 +00:00
|
|
|
(wchar_t *)malloc( sizeof(wchar_t)*(wcslen(val) + 1) );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2008-01-13 19:32:21 +00:00
|
|
|
for( pin=val; *pin; pin++ )
|
|
|
|
{
|
|
|
|
switch( *pin )
|
|
|
|
{
|
|
|
|
case L'\n':
|
|
|
|
case L'\t':
|
|
|
|
case L'\b':
|
|
|
|
case L'\r':
|
|
|
|
unescapable=1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*pout++ = *pin;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( unescapable )
|
2007-03-24 19:07:38 +00:00
|
|
|
{
|
2008-01-13 19:32:21 +00:00
|
|
|
free( replaced );
|
|
|
|
wchar_t *tmp = escape( val, ESCAPE_ALL | ESCAPE_NO_QUOTED );
|
|
|
|
replaced = wcsdupcat( L" ", tmp );
|
|
|
|
free( tmp);
|
|
|
|
replaced[0]=quote;
|
2007-03-24 19:07:38 +00:00
|
|
|
}
|
2008-01-13 19:32:21 +00:00
|
|
|
else
|
|
|
|
*pout = 0;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
2008-01-13 19:32:21 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
replaced = wcsdup(val);
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
if( insert_str( replaced ) )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2007-03-24 19:07:38 +00:00
|
|
|
/*
|
2011-12-27 03:18:46 +00:00
|
|
|
Print trailing space since this is the only completion
|
2007-03-24 19:07:38 +00:00
|
|
|
*/
|
2011-12-27 03:18:46 +00:00
|
|
|
if( add_space )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2007-03-24 19:07:38 +00:00
|
|
|
|
2008-01-14 01:00:32 +00:00
|
|
|
if( quote &&
|
2011-12-27 03:18:46 +00:00
|
|
|
(data->buff[data->buff_pos] != quote ) )
|
2007-03-24 19:07:38 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
/*
|
|
|
|
This is a quoted parameter, first print a quote
|
2007-03-24 19:07:38 +00:00
|
|
|
*/
|
|
|
|
insert_char( quote );
|
|
|
|
}
|
|
|
|
insert_char( L' ' );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
free(replaced);
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-28 21:43:27 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-11-02 13:50:19 +00:00
|
|
|
Run the fish_pager command to display the completion list. If the
|
|
|
|
fish_pager outputs any text, it is inserted into the input
|
|
|
|
backbuffer.
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
\param prefix the string to display before every completion.
|
2006-11-02 13:50:19 +00:00
|
|
|
\param is_quoted should be set if the argument is quoted. This will change the display style.
|
|
|
|
\param comp the list of completions to display
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
|
|
|
|
2012-01-16 16:56:47 +00:00
|
|
|
static void run_pager( wchar_t *prefix, int is_quoted, const std::vector<completion_t> &comp )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
string_buffer_t cmd;
|
2006-08-13 01:46:02 +00:00
|
|
|
string_buffer_t msg;
|
2005-09-20 13:26:39 +00:00
|
|
|
wchar_t * prefix_esc;
|
2006-08-13 01:46:02 +00:00
|
|
|
char *foo;
|
2007-02-09 09:33:50 +00:00
|
|
|
io_data_t *in;
|
|
|
|
wchar_t *escaped_separator;
|
2008-01-09 00:09:28 +00:00
|
|
|
int has_case_sensitive=0;
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( !prefix || (wcslen(prefix)==0))
|
2007-02-09 09:33:50 +00:00
|
|
|
{
|
2005-09-20 13:26:39 +00:00
|
|
|
prefix_esc = wcsdup(L"\"\"");
|
2007-02-09 09:33:50 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
else
|
2007-02-09 09:33:50 +00:00
|
|
|
{
|
2005-10-07 10:36:51 +00:00
|
|
|
prefix_esc = escape( prefix,1);
|
2007-02-09 09:33:50 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
sb_init( &cmd );
|
2006-08-13 01:46:02 +00:00
|
|
|
sb_init( &msg );
|
2006-01-30 19:53:10 +00:00
|
|
|
sb_printf( &cmd,
|
2007-01-07 14:13:36 +00:00
|
|
|
L"fish_pager -c 3 -r 4 %ls -p %ls",
|
2006-02-15 02:49:00 +00:00
|
|
|
// L"valgrind --track-fds=yes --log-file=pager.txt --leak-check=full ./fish_pager %d %ls",
|
2007-01-07 14:13:36 +00:00
|
|
|
is_quoted?L"-q":L"",
|
2005-09-20 13:26:39 +00:00
|
|
|
prefix_esc );
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
free( prefix_esc );
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2007-02-09 09:33:50 +00:00
|
|
|
in= io_buffer_create( 1 );
|
2007-01-07 14:13:36 +00:00
|
|
|
in->fd = 3;
|
2007-02-09 09:33:50 +00:00
|
|
|
|
|
|
|
escaped_separator = escape( COMPLETE_SEP_STR, 1);
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-01 01:01:19 +00:00
|
|
|
for( size_t i=0; i< comp.size(); i++ )
|
2008-01-09 00:09:28 +00:00
|
|
|
{
|
2012-01-16 16:56:47 +00:00
|
|
|
const completion_t &el = comp.at( i );
|
|
|
|
has_case_sensitive |= !(el.flags & COMPLETE_NO_CASE );
|
2008-01-09 00:09:28 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-01 01:01:19 +00:00
|
|
|
for( size_t i=0; i< comp.size(); i++ )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2007-02-09 09:33:50 +00:00
|
|
|
|
2007-02-28 21:43:27 +00:00
|
|
|
int base_len=-1;
|
2012-01-16 16:56:47 +00:00
|
|
|
const completion_t &el = comp.at( i );
|
2007-02-09 09:33:50 +00:00
|
|
|
|
|
|
|
wchar_t *foo=0;
|
|
|
|
wchar_t *baz=0;
|
|
|
|
|
2012-01-16 16:56:47 +00:00
|
|
|
if( has_case_sensitive && (el.flags & COMPLETE_NO_CASE ))
|
2008-01-09 00:09:28 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-01-20 16:50:31 +00:00
|
|
|
if( el.completion.empty() ){
|
|
|
|
continue;
|
2007-02-28 21:43:27 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-01-20 16:50:31 +00:00
|
|
|
if( el.flags & COMPLETE_NO_CASE )
|
|
|
|
{
|
|
|
|
if( base_len == -1 )
|
|
|
|
{
|
|
|
|
wchar_t *begin;
|
|
|
|
|
|
|
|
parse_util_token_extent( data->buff, data->buff_pos, &begin, 0, 0, 0 );
|
|
|
|
base_len = data->buff_pos - (begin-data->buff);
|
|
|
|
}
|
|
|
|
|
|
|
|
wcstring foo_wstr = escape_string( el.completion.c_str() + base_len, ESCAPE_ALL | ESCAPE_NO_QUOTED );
|
|
|
|
foo = wcsdup(foo_wstr.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wcstring foo_wstr = escape_string( el.completion, ESCAPE_ALL | ESCAPE_NO_QUOTED );
|
|
|
|
foo = wcsdup(foo_wstr.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-16 16:56:47 +00:00
|
|
|
if( !el.description.empty() )
|
2007-02-09 09:33:50 +00:00
|
|
|
{
|
2012-01-16 16:56:47 +00:00
|
|
|
wcstring baz_wstr = escape_string( el.description, 1 );
|
2012-01-18 18:33:19 +00:00
|
|
|
baz = wcsdup(baz_wstr.c_str());
|
2007-02-09 09:33:50 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-09 09:33:50 +00:00
|
|
|
if( !foo )
|
|
|
|
{
|
|
|
|
debug( 0, L"Run pager called with bad argument." );
|
|
|
|
bugreport();
|
|
|
|
show_stackframe();
|
|
|
|
}
|
|
|
|
else if( baz )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
sb_printf( &msg, L"%ls%ls%ls\n",
|
2007-02-09 09:33:50 +00:00
|
|
|
foo,
|
|
|
|
escaped_separator,
|
|
|
|
baz );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
sb_printf( &msg, L"%ls\n",
|
2007-02-09 09:33:50 +00:00
|
|
|
foo );
|
|
|
|
}
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
free( foo );
|
|
|
|
free( baz );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2007-02-09 09:33:50 +00:00
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
free( escaped_separator );
|
|
|
|
|
2006-08-13 01:46:02 +00:00
|
|
|
foo = wcs2str( (wchar_t *)msg.buff );
|
|
|
|
b_append( in->param2.out_buffer, foo, strlen(foo) );
|
|
|
|
free( foo );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-01-30 19:53:10 +00:00
|
|
|
term_donate();
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-08-13 01:46:02 +00:00
|
|
|
io_data_t *out = io_buffer_create( 0 );
|
|
|
|
out->next = in;
|
2007-01-07 14:13:36 +00:00
|
|
|
out->fd = 4;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-01-23 05:57:30 +00:00
|
|
|
parser_t &parser = parser_t::principal_parser();
|
2012-01-23 05:40:08 +00:00
|
|
|
parser.eval( (wchar_t *)cmd.buff, out, TOP);
|
2006-01-30 19:53:10 +00:00
|
|
|
term_steal();
|
2005-09-20 14:51:00 +00:00
|
|
|
|
2005-10-08 11:20:51 +00:00
|
|
|
io_buffer_read( out );
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-24 19:31:17 +00:00
|
|
|
sb_destroy( &cmd );
|
2006-08-13 01:46:02 +00:00
|
|
|
sb_destroy( &msg );
|
2006-01-30 19:53:10 +00:00
|
|
|
|
|
|
|
int nil=0;
|
2005-10-11 19:31:16 +00:00
|
|
|
b_append( out->param2.out_buffer, &nil, 1 );
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
wchar_t *tmp;
|
2005-10-11 19:31:16 +00:00
|
|
|
wchar_t *str = str2wcs((char *)out->param2.out_buffer->buff);
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( str )
|
|
|
|
{
|
|
|
|
for( tmp = str + wcslen(str)-1; tmp >= str; tmp-- )
|
|
|
|
{
|
|
|
|
input_unreadch( *tmp );
|
2006-01-30 19:53:10 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
free( str );
|
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2006-08-13 01:46:02 +00:00
|
|
|
|
2005-10-08 11:20:51 +00:00
|
|
|
io_buffer_destroy( out);
|
2006-08-13 01:46:02 +00:00
|
|
|
io_buffer_destroy( in);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2008-01-13 16:47:47 +00:00
|
|
|
/**
|
2006-11-30 23:57:49 +00:00
|
|
|
Flash the screen. This function only changed the color of the
|
|
|
|
current line, since the flash_screen sequnce is rather painful to
|
|
|
|
look at in most terminal emulators.
|
|
|
|
*/
|
|
|
|
static void reader_flash()
|
|
|
|
{
|
|
|
|
struct timespec pollint;
|
|
|
|
|
2012-01-15 06:48:53 +00:00
|
|
|
for( size_t i=0; i<data->buff_pos; i++ )
|
2006-11-30 23:57:49 +00:00
|
|
|
{
|
|
|
|
data->color[i] = HIGHLIGHT_SEARCH_MATCH<<16;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-11-30 23:57:49 +00:00
|
|
|
pollint.tv_sec = 0;
|
|
|
|
pollint.tv_nsec = 100 * 1000000;
|
|
|
|
nanosleep( &pollint, NULL );
|
|
|
|
|
|
|
|
reader_super_highlight_me_plenty( data->buff_pos, 0 );
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
|
2006-11-30 23:57:49 +00:00
|
|
|
}
|
|
|
|
|
2008-01-14 01:00:32 +00:00
|
|
|
/**
|
|
|
|
Characters that may not be part of a token that is to be replaced
|
|
|
|
by a case insensitive completion.
|
|
|
|
*/
|
|
|
|
#define REPLACE_UNCLEAN L"$*?({})"
|
2008-01-13 19:32:21 +00:00
|
|
|
|
2008-01-14 01:00:32 +00:00
|
|
|
/**
|
|
|
|
Check if the specified string can be replaced by a case insensitive
|
|
|
|
complition with the specified flags.
|
|
|
|
|
|
|
|
Advanced tokens like those containing {}-style expansion can not at
|
|
|
|
the moment be replaced, other than if the new token is already an
|
|
|
|
exact replacement, e.g. if the COMPLETE_DONT_ESCAPE flag is set.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int reader_can_replace( const wchar_t *in, int flags )
|
2008-01-13 19:32:21 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
const wchar_t * str = in;
|
|
|
|
|
2008-01-14 01:00:32 +00:00
|
|
|
if( flags & COMPLETE_DONT_ESCAPE )
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2008-01-14 01:00:32 +00:00
|
|
|
|
2008-01-13 19:32:21 +00:00
|
|
|
CHECK( in, 1 );
|
|
|
|
|
|
|
|
/*
|
|
|
|
Test characters that have a special meaning in any character position
|
|
|
|
*/
|
|
|
|
while( *str )
|
|
|
|
{
|
2008-01-14 01:00:32 +00:00
|
|
|
if( wcschr( REPLACE_UNCLEAN, *str ) )
|
2008-01-13 19:32:21 +00:00
|
|
|
return 0;
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2006-11-30 23:57:49 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
|
|
|
Handle the list of completions. This means the following:
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
- 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.
|
2006-10-16 15:32:26 +00:00
|
|
|
- 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.
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
\param comp the list of completion strings
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2012-01-16 16:56:47 +00:00
|
|
|
static int handle_completions( std::vector<completion_t> &comp )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2007-02-28 21:43:27 +00:00
|
|
|
void *context = 0;
|
|
|
|
wchar_t *base = 0;
|
|
|
|
int len = 0;
|
|
|
|
int done = 0;
|
|
|
|
int count = 0;
|
|
|
|
int flags=0;
|
2007-03-24 19:07:38 +00:00
|
|
|
wchar_t *begin, *end;
|
|
|
|
wchar_t *tok;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
parse_util_token_extent( data->buff, data->buff_pos, &begin, 0, 0, 0 );
|
|
|
|
end = data->buff+data->buff_pos;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
context = halloc( 0, 0 );
|
|
|
|
tok = halloc_wcsndup( context, begin, end-begin );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2008-01-13 19:32:21 +00:00
|
|
|
/*
|
|
|
|
Check trivial cases
|
|
|
|
*/
|
2012-01-16 16:56:47 +00:00
|
|
|
switch( comp.size() )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2008-01-13 19:32:21 +00:00
|
|
|
/*
|
|
|
|
No suitable completions found, flash screen and retur
|
|
|
|
*/
|
2007-03-24 19:07:38 +00:00
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
reader_flash();
|
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
}
|
2008-01-13 19:32:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Exactly one suitable completion found - insert it
|
|
|
|
*/
|
2007-03-24 19:07:38 +00:00
|
|
|
case 1:
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-01-16 16:56:47 +00:00
|
|
|
const completion_t &c = comp.at( 0 );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2008-01-13 19:32:21 +00:00
|
|
|
/*
|
|
|
|
If this is a replacement completion, check
|
|
|
|
that we know how to replace it, e.g. that
|
|
|
|
the token doesn't contain evil operators
|
|
|
|
like {}
|
|
|
|
*/
|
2012-01-16 16:56:47 +00:00
|
|
|
if( !(c.flags & COMPLETE_NO_CASE) || reader_can_replace( tok, c.flags ) )
|
2007-03-24 19:07:38 +00:00
|
|
|
{
|
2012-01-16 16:56:47 +00:00
|
|
|
completion_insert( c.completion.c_str(),
|
|
|
|
c.flags );
|
2007-03-24 19:07:38 +00:00
|
|
|
}
|
|
|
|
done = 1;
|
|
|
|
len = 1;
|
2007-10-28 09:08:40 +00:00
|
|
|
break;
|
2007-03-24 19:07:38 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
if( !done )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2008-01-13 19:32:21 +00:00
|
|
|
/*
|
|
|
|
Try to find something to insert whith the correct case
|
|
|
|
*/
|
2012-02-01 01:01:19 +00:00
|
|
|
for( size_t i=0; i< comp.size() ; i++ )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-01-16 16:56:47 +00:00
|
|
|
const completion_t &c = comp.at( i );
|
2007-02-28 21:43:27 +00:00
|
|
|
int new_len;
|
|
|
|
|
2008-01-13 19:32:21 +00:00
|
|
|
/*
|
|
|
|
Ignore case insensitive completions for now
|
|
|
|
*/
|
2012-01-16 16:56:47 +00:00
|
|
|
if( c.flags & COMPLETE_NO_CASE )
|
2007-02-28 21:43:27 +00:00
|
|
|
continue;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-28 21:43:27 +00:00
|
|
|
count++;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-28 21:43:27 +00:00
|
|
|
if( base )
|
|
|
|
{
|
2012-01-16 16:56:47 +00:00
|
|
|
new_len = comp_len( base, c.completion.c_str() );
|
2007-02-28 21:43:27 +00:00
|
|
|
len = new_len < len ? new_len: len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-16 16:56:47 +00:00
|
|
|
base = wcsdup( c.completion.c_str() );
|
2007-02-28 21:43:27 +00:00
|
|
|
len = wcslen( base );
|
2012-01-16 16:56:47 +00:00
|
|
|
flags = c.flags;
|
2007-02-28 21:43:27 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2007-02-28 21:43:27 +00:00
|
|
|
|
2008-01-13 19:32:21 +00:00
|
|
|
/*
|
|
|
|
If we found something to insert, do it.
|
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
if( len > 0 )
|
|
|
|
{
|
2007-02-28 21:43:27 +00:00
|
|
|
if( count > 1 )
|
|
|
|
flags = flags | COMPLETE_NO_SPACE;
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
base[len]=L'\0';
|
2007-02-28 21:43:27 +00:00
|
|
|
completion_insert(base, flags);
|
|
|
|
done = 1;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2007-03-24 19:07:38 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
|
2007-02-28 21:43:27 +00:00
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
if( !done && base == 0 )
|
|
|
|
{
|
2008-01-13 19:32:21 +00:00
|
|
|
/*
|
|
|
|
Try to find something to insert ignoring case
|
|
|
|
*/
|
2007-02-28 21:43:27 +00:00
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
if( begin )
|
|
|
|
{
|
2007-02-28 21:43:27 +00:00
|
|
|
|
2008-01-14 01:00:32 +00:00
|
|
|
int offset = wcslen( tok );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2008-01-14 01:00:32 +00:00
|
|
|
count = 0;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-01 01:01:19 +00:00
|
|
|
for( size_t i=0; i< comp.size(); i++ )
|
2007-02-28 21:43:27 +00:00
|
|
|
{
|
2012-01-16 16:56:47 +00:00
|
|
|
const completion_t &c = comp.at( i );
|
2008-01-14 01:00:32 +00:00
|
|
|
int new_len;
|
2007-02-28 21:43:27 +00:00
|
|
|
|
|
|
|
|
2012-01-16 16:56:47 +00:00
|
|
|
if( !(c.flags & COMPLETE_NO_CASE) )
|
2008-01-14 01:00:32 +00:00
|
|
|
continue;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-01-16 16:56:47 +00:00
|
|
|
if( !reader_can_replace( tok, c.flags ) )
|
2008-01-14 01:00:32 +00:00
|
|
|
{
|
|
|
|
len=0;
|
|
|
|
break;
|
2007-03-24 19:07:38 +00:00
|
|
|
}
|
2007-02-28 21:43:27 +00:00
|
|
|
|
2008-01-14 01:00:32 +00:00
|
|
|
count++;
|
2007-02-28 21:43:27 +00:00
|
|
|
|
2008-01-14 01:00:32 +00:00
|
|
|
if( base )
|
|
|
|
{
|
2012-01-16 16:56:47 +00:00
|
|
|
new_len = offset + comp_ilen( base+offset, c.completion.c_str()+offset );
|
2008-01-14 01:00:32 +00:00
|
|
|
len = new_len < len ? new_len: len;
|
2007-02-28 21:43:27 +00:00
|
|
|
}
|
2008-01-14 01:00:32 +00:00
|
|
|
else
|
|
|
|
{
|
2012-01-16 16:56:47 +00:00
|
|
|
base = wcsdup( c.completion.c_str() );
|
2008-01-14 01:00:32 +00:00
|
|
|
len = wcslen( base );
|
2012-01-16 16:56:47 +00:00
|
|
|
flags = c.flags;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2008-01-14 01:00:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( len > offset )
|
|
|
|
{
|
|
|
|
if( count > 1 )
|
|
|
|
flags = flags | COMPLETE_NO_SPACE;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2008-01-14 01:00:32 +00:00
|
|
|
base[len]=L'\0';
|
|
|
|
completion_insert( base, flags );
|
|
|
|
done = 1;
|
2007-02-28 21:43:27 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-28 21:43:27 +00:00
|
|
|
}
|
2007-03-24 19:07:38 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
free( base );
|
2007-02-28 21:43:27 +00:00
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
if( !done )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
There is no common prefix in the completions, and show_list
|
|
|
|
is true, so we print the list
|
|
|
|
*/
|
|
|
|
int len;
|
|
|
|
wchar_t * prefix;
|
|
|
|
wchar_t * prefix_start;
|
|
|
|
get_param( data->buff,
|
|
|
|
data->buff_pos,
|
|
|
|
0,
|
|
|
|
&prefix_start,
|
|
|
|
0,
|
|
|
|
0 );
|
|
|
|
|
|
|
|
len = &data->buff[data->buff_pos]-prefix_start+1;
|
|
|
|
|
|
|
|
if( len <= PREFIX_MAX_LEN )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
prefix = (wchar_t *)malloc( sizeof(wchar_t)*(len+1) );
|
2007-03-24 19:07:38 +00:00
|
|
|
wcslcpy( prefix, prefix_start, len );
|
|
|
|
prefix[len]=L'\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wchar_t tmp[2]=
|
|
|
|
{
|
|
|
|
ellipsis_char,
|
|
|
|
0
|
|
|
|
}
|
|
|
|
;
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
prefix = wcsdupcat( tmp,
|
|
|
|
prefix_start + (len - PREFIX_MAX_LEN) );
|
|
|
|
prefix[PREFIX_MAX_LEN] = 0;
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
{
|
|
|
|
int is_quoted;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
wchar_t quote;
|
|
|
|
get_param( data->buff, data->buff_pos, "e, 0, 0, 0 );
|
|
|
|
is_quoted = (quote != L'\0');
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2009-02-22 20:28:52 +00:00
|
|
|
write_loop(1, "\n", 1 );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
run_pager( prefix, is_quoted, comp );
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
free( prefix );
|
2007-09-24 08:13:01 +00:00
|
|
|
s_reset( &data->screen, 1 );
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
halloc_free( context );
|
2007-02-28 21:43:27 +00:00
|
|
|
|
|
|
|
return len;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-28 21:43:27 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Initialize data for interactive use
|
|
|
|
*/
|
|
|
|
static void reader_interactive_init()
|
|
|
|
{
|
|
|
|
/* See if we are running interactively. */
|
|
|
|
pid_t shell_pgid;
|
|
|
|
|
|
|
|
input_init();
|
|
|
|
kill_init();
|
|
|
|
shell_pgid = getpgrp ();
|
|
|
|
|
2006-02-05 13:12:53 +00:00
|
|
|
/*
|
2006-10-16 15:32:26 +00:00
|
|
|
This should enable job control on fish, even if our parent process did
|
2006-02-05 13:12:53 +00:00
|
|
|
not enable it for us.
|
|
|
|
*/
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
/*
|
2009-03-01 02:14:41 +00:00
|
|
|
Check if we are in control of the terminal, so that we don't do
|
|
|
|
semi-expensive things like reset signal handlers unless we
|
|
|
|
really have to, which we often don't.
|
|
|
|
*/
|
|
|
|
if (tcgetpgrp( 0 ) != shell_pgid)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2009-03-01 02:14:41 +00:00
|
|
|
int block_count = 0;
|
|
|
|
int i;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2009-03-01 02:14:41 +00:00
|
|
|
/*
|
|
|
|
Bummer, we are not in control of the terminal. Stop until
|
|
|
|
parent has given us control of it. Stopping in fish is a bit
|
|
|
|
of a challange, what with all the signal fidgeting, we need
|
|
|
|
to reset a bunch of signal state, making this coda a but
|
|
|
|
unobvious.
|
|
|
|
|
|
|
|
In theory, reseting signal handlers could cause us to miss
|
|
|
|
signal deliveries. In practice, this code should only be run
|
|
|
|
suring startup, when we're not waiting for any signals.
|
|
|
|
*/
|
2011-12-27 03:18:46 +00:00
|
|
|
while (signal_is_blocked())
|
2009-03-01 02:14:41 +00:00
|
|
|
{
|
|
|
|
signal_unblock();
|
|
|
|
block_count++;
|
|
|
|
}
|
|
|
|
signal_reset_handlers();
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2009-03-01 02:14:41 +00:00
|
|
|
/*
|
|
|
|
Ok, signal handlers are taken out of the picture. Stop ourself in a loop
|
|
|
|
until we are in control of the terminal.
|
|
|
|
*/
|
|
|
|
while (tcgetpgrp( 0 ) != shell_pgid)
|
|
|
|
{
|
|
|
|
killpg( shell_pgid, SIGTTIN);
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2009-03-01 02:14:41 +00:00
|
|
|
signal_set_handlers();
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
for( i=0; i<block_count; i++ )
|
2009-03-01 02:14:41 +00:00
|
|
|
{
|
|
|
|
signal_block();
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/* Put ourselves in our own process group. */
|
|
|
|
shell_pgid = getpid ();
|
|
|
|
if( getpgrp() != shell_pgid )
|
|
|
|
{
|
|
|
|
if (setpgid (shell_pgid, shell_pgid) < 0)
|
|
|
|
{
|
|
|
|
debug( 1,
|
2006-01-04 12:51:02 +00:00
|
|
|
_( L"Couldn't put the shell in its own process group" ));
|
2005-09-20 13:26:39 +00:00
|
|
|
wperror( L"setpgid" );
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Grab control of the terminal. */
|
|
|
|
if( tcsetpgrp (STDIN_FILENO, shell_pgid) )
|
|
|
|
{
|
|
|
|
debug( 1,
|
2006-01-04 12:51:02 +00:00
|
|
|
_( L"Couldn't grab control of terminal" ) );
|
2005-09-20 13:26:39 +00:00
|
|
|
wperror( L"tcsetpgrp" );
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2006-01-30 19:53:10 +00:00
|
|
|
common_handle_winch(0);
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( tcsetattr(0,TCSANOW,&shell_modes)) /* set the new modes */
|
|
|
|
{
|
|
|
|
wperror(L"tcsetattr");
|
|
|
|
}
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
/*
|
2006-02-05 13:12:53 +00:00
|
|
|
We need to know our own pid so we'll later know if we are a
|
2011-12-27 03:18:46 +00:00
|
|
|
fork
|
2006-02-05 13:12:53 +00:00
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
original_pid = getpid();
|
|
|
|
|
|
|
|
env_set( L"_", L"fish", ENV_GLOBAL );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Destroy data for interactive use
|
|
|
|
*/
|
|
|
|
static void reader_interactive_destroy()
|
|
|
|
{
|
|
|
|
kill_destroy();
|
|
|
|
writestr( L"\n" );
|
|
|
|
set_color( FISH_COLOR_RESET, FISH_COLOR_RESET );
|
|
|
|
input_destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void reader_sanity_check()
|
|
|
|
{
|
|
|
|
if( is_interactive)
|
|
|
|
{
|
2007-10-06 10:54:53 +00:00
|
|
|
if( !data )
|
|
|
|
sanity_lose();
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if(!( data->buff_pos <= data->buff_len ))
|
|
|
|
sanity_lose();
|
2007-10-06 10:54:53 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if(!( data->buff_len == wcslen( data->buff ) ))
|
|
|
|
sanity_lose();
|
2007-10-06 10:54:53 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void reader_replace_current_token( wchar_t *new_token )
|
|
|
|
{
|
|
|
|
|
2006-06-14 13:22:40 +00:00
|
|
|
wchar_t *begin, *end;
|
2005-09-20 13:26:39 +00:00
|
|
|
string_buffer_t sb;
|
|
|
|
int new_pos;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Find current token
|
|
|
|
*/
|
2006-01-30 16:51:50 +00:00
|
|
|
parse_util_token_extent( data->buff, data->buff_pos, &begin, &end, 0, 0 );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
if( !begin || !end )
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Make new string
|
|
|
|
*/
|
|
|
|
sb_init( &sb );
|
|
|
|
sb_append_substring( &sb, data->buff, begin-data->buff );
|
|
|
|
sb_append( &sb, new_token );
|
|
|
|
sb_append( &sb, end );
|
|
|
|
|
|
|
|
new_pos = (begin-data->buff) + wcslen(new_token);
|
|
|
|
|
|
|
|
reader_set_buffer( (wchar_t *)sb.buff, new_pos );
|
|
|
|
sb_destroy( &sb );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Set the specified string from the history as the current buffer. Do
|
|
|
|
not modify prefix_width.
|
|
|
|
*/
|
|
|
|
static void handle_history( const wchar_t *new_str )
|
|
|
|
{
|
2006-10-25 20:36:08 +00:00
|
|
|
if( new_str )
|
|
|
|
{
|
|
|
|
data->buff_len = wcslen( new_str );
|
|
|
|
check_size();
|
|
|
|
wcscpy( data->buff, new_str );
|
|
|
|
data->buff_pos=wcslen(data->buff);
|
|
|
|
reader_super_highlight_me_plenty( data->buff_pos, 0 );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2006-10-25 20:36:08 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Check if the specified string is contained in the list, using
|
|
|
|
wcscmp as a comparison function
|
|
|
|
*/
|
2007-09-28 21:32:27 +00:00
|
|
|
static int contains_al( const wchar_t *needle,
|
|
|
|
array_list_t *haystack )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for( i=0; i<al_get_count( haystack ); i++ )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
if( !wcscmp( needle, (const wchar_t *)al_get( haystack, i ) ) )
|
2005-09-20 13:26:39 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2006-01-23 20:40:14 +00:00
|
|
|
/**
|
|
|
|
Reset the data structures associated with the token search
|
|
|
|
*/
|
2005-09-24 16:31:22 +00:00
|
|
|
static void reset_token_history()
|
|
|
|
{
|
2006-06-14 13:22:40 +00:00
|
|
|
wchar_t *begin, *end;
|
2005-09-24 16:31:22 +00:00
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
parse_util_token_extent( data->buff, data->buff_pos, &begin, &end, 0, 0 );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-04-16 21:26:15 +00:00
|
|
|
sb_clear( &data->search_buff );
|
2005-09-24 16:31:22 +00:00
|
|
|
if( begin )
|
|
|
|
{
|
2007-04-16 21:26:15 +00:00
|
|
|
sb_append_substring( &data->search_buff, begin, end-begin);
|
2005-09-24 16:31:22 +00:00
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-24 16:31:22 +00:00
|
|
|
data->token_history_pos = -1;
|
|
|
|
data->search_pos=0;
|
2006-06-12 21:47:42 +00:00
|
|
|
al_foreach( &data->search_prev, &free );
|
2005-09-24 16:31:22 +00:00
|
|
|
al_truncate( &data->search_prev, 0 );
|
2007-04-16 21:26:15 +00:00
|
|
|
al_push( &data->search_prev, wcsdup( (wchar_t *)data->search_buff.buff ) );
|
2005-09-24 16:31:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
|
|
|
Handles a token search command.
|
|
|
|
|
|
|
|
\param forward if the search should be forward or reverse
|
|
|
|
\param reset whether the current token should be made the new search token
|
|
|
|
*/
|
|
|
|
static void handle_token_history( int forward, int reset )
|
|
|
|
{
|
|
|
|
wchar_t *str=0;
|
|
|
|
int current_pos;
|
|
|
|
tokenizer tok;
|
|
|
|
|
2005-12-11 04:30:17 +00:00
|
|
|
if( reset )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
Start a new token search using the current token
|
|
|
|
*/
|
2005-09-24 16:31:22 +00:00
|
|
|
reset_token_history();
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2005-12-12 18:30:55 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
current_pos = data->token_history_pos;
|
|
|
|
|
|
|
|
if( forward || data->search_pos < (al_get_count( &data->search_prev )-1) )
|
|
|
|
{
|
|
|
|
if( forward )
|
|
|
|
{
|
|
|
|
if( data->search_pos > 0 )
|
|
|
|
{
|
|
|
|
data->search_pos--;
|
|
|
|
}
|
|
|
|
str = (wchar_t *)al_get( &data->search_prev, data->search_pos );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data->search_pos++;
|
|
|
|
str = (wchar_t *)al_get( &data->search_prev, data->search_pos );
|
|
|
|
}
|
|
|
|
|
|
|
|
reader_replace_current_token( str );
|
2006-10-16 15:32:26 +00:00
|
|
|
reader_super_highlight_me_plenty( data->buff_pos, 0 );
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( current_pos == -1 )
|
|
|
|
{
|
2006-11-18 21:24:59 +00:00
|
|
|
const wchar_t *item;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/*
|
|
|
|
Move to previous line
|
|
|
|
*/
|
2011-12-27 03:18:46 +00:00
|
|
|
free( (void *)data->token_history_buff );
|
2006-11-17 23:37:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Search for previous item that contains this substring
|
|
|
|
*/
|
2007-04-16 21:26:15 +00:00
|
|
|
item = history_prev_match( (wchar_t *)data->search_buff.buff);
|
2006-11-17 23:37:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
If there is no match, the original string is returned
|
|
|
|
|
|
|
|
If so, we clear the match string to avoid infinite loop
|
|
|
|
*/
|
2007-04-16 21:26:15 +00:00
|
|
|
if( wcscmp( item, (wchar_t *)data->search_buff.buff ) == 0 )
|
2006-11-17 23:37:26 +00:00
|
|
|
{
|
|
|
|
item=L"";
|
|
|
|
}
|
|
|
|
|
|
|
|
data->token_history_buff = wcsdup( item );
|
2005-09-20 13:26:39 +00:00
|
|
|
current_pos = wcslen(data->token_history_buff);
|
2006-11-17 23:37:26 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ! wcslen( data->token_history_buff ) )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
We have reached the end of the history - check if the
|
|
|
|
history already contains the search string itself, if so
|
|
|
|
return, otherwise add it.
|
|
|
|
*/
|
2005-12-12 18:30:55 +00:00
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
const wchar_t *last = (const wchar_t *)al_get( &data->search_prev, al_get_count( &data->search_prev ) -1 );
|
2007-04-16 21:26:15 +00:00
|
|
|
if( wcscmp( last, (wchar_t *)data->search_buff.buff ) )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2007-04-16 21:26:15 +00:00
|
|
|
str = wcsdup( (wchar_t *)data->search_buff.buff );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-01-30 19:53:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-12-12 18:30:55 +00:00
|
|
|
|
2006-11-20 13:14:12 +00:00
|
|
|
//debug( 3, L"new '%ls'", data->token_history_buff );
|
2005-12-12 18:30:55 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
for( tok_init( &tok, data->token_history_buff, TOK_ACCEPT_UNFINISHED );
|
|
|
|
tok_has_next( &tok);
|
|
|
|
tok_next( &tok ))
|
|
|
|
{
|
|
|
|
switch( tok_last_type( &tok ) )
|
|
|
|
{
|
|
|
|
case TOK_STRING:
|
|
|
|
{
|
2007-04-16 21:26:15 +00:00
|
|
|
if( wcsstr( tok_last( &tok ), (wchar_t *)data->search_buff.buff ) )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-11-20 13:14:12 +00:00
|
|
|
//debug( 3, L"Found token at pos %d\n", tok_get_pos( &tok ) );
|
2005-09-20 13:26:39 +00:00
|
|
|
if( tok_get_pos( &tok ) >= current_pos )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2006-11-20 13:14:12 +00:00
|
|
|
//debug( 3, L"ok pos" );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2007-09-28 21:32:27 +00:00
|
|
|
if( !contains_al( tok_last( &tok ), &data->search_prev ) )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
free(str);
|
|
|
|
data->token_history_pos = tok_get_pos( &tok );
|
|
|
|
str = wcsdup(tok_last( &tok ));
|
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tok_destroy( &tok );
|
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( str )
|
|
|
|
{
|
|
|
|
reader_replace_current_token( str );
|
2006-10-16 15:32:26 +00:00
|
|
|
reader_super_highlight_me_plenty( data->buff_pos, 0 );
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2005-09-20 13:26:39 +00:00
|
|
|
al_push( &data->search_prev, str );
|
|
|
|
data->search_pos = al_get_count( &data->search_prev )-1;
|
|
|
|
}
|
2006-11-17 13:06:12 +00:00
|
|
|
else if( ! reader_interrupted() )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
data->token_history_pos=-1;
|
|
|
|
handle_token_history( 0, 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
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.
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
\param dir Direction to move/erase. 0 means move left, 1 means move right.
|
|
|
|
\param erase Whether to erase the characters along the way or only move past them.
|
2008-01-13 16:47:47 +00:00
|
|
|
\param new if the new kill item should be appended to the previous kill item or not.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2011-12-27 03:18:46 +00:00
|
|
|
static void move_word( int dir, int erase, int newv )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-01-15 06:48:53 +00:00
|
|
|
size_t end_buff_pos=data->buff_pos;
|
2005-09-20 13:26:39 +00:00
|
|
|
int step = dir?1:-1;
|
|
|
|
|
2006-10-12 16:13:17 +00:00
|
|
|
/*
|
|
|
|
Return if we are already at the edge
|
|
|
|
*/
|
|
|
|
if( !dir && data->buff_pos == 0 )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-12 16:13:17 +00:00
|
|
|
if( dir && data->buff_pos == data->buff_len )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-08-31 00:45:25 +00:00
|
|
|
/*
|
|
|
|
If we are beyond the last character and moving left, start by
|
|
|
|
moving one step, since otehrwise we'll start on the \0, which
|
|
|
|
should be ignored.
|
|
|
|
*/
|
|
|
|
if( !dir && (end_buff_pos == data->buff_len) )
|
|
|
|
{
|
|
|
|
if( !end_buff_pos )
|
|
|
|
return;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-08-31 00:45:25 +00:00
|
|
|
end_buff_pos--;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-12 16:13:17 +00:00
|
|
|
/*
|
|
|
|
When moving left, ignore the character under the cursor
|
|
|
|
*/
|
|
|
|
if( !dir )
|
|
|
|
{
|
|
|
|
end_buff_pos+=2*step;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-09-16 23:05:32 +00:00
|
|
|
/*
|
|
|
|
Remove all whitespace characters before finding a word
|
|
|
|
*/
|
2006-08-31 00:45:25 +00:00
|
|
|
while( 1 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-08-31 00:45:25 +00:00
|
|
|
wchar_t c;
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( !dir )
|
|
|
|
{
|
2006-10-16 14:39:12 +00:00
|
|
|
if( end_buff_pos <= 0 )
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-16 14:39:12 +00:00
|
|
|
if( end_buff_pos >= data->buff_len )
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-09-16 23:05:32 +00:00
|
|
|
/*
|
|
|
|
Always eat at least one character
|
|
|
|
*/
|
|
|
|
if( end_buff_pos != data->buff_pos )
|
2006-08-31 00:45:25 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-09-16 23:05:32 +00:00
|
|
|
c = data->buff[end_buff_pos];
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-09-16 23:05:32 +00:00
|
|
|
if( !iswspace( c ) )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2006-08-31 00:45:25 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-08-31 00:45:25 +00:00
|
|
|
end_buff_pos+=step;
|
2006-09-16 23:05:32 +00:00
|
|
|
|
2006-08-31 00:45:25 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-09-16 23:05:32 +00:00
|
|
|
/*
|
|
|
|
Remove until we find a character that is not alphanumeric
|
|
|
|
*/
|
2006-08-31 00:45:25 +00:00
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
wchar_t c;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-08-31 00:45:25 +00:00
|
|
|
if( !dir )
|
|
|
|
{
|
2006-10-16 14:39:12 +00:00
|
|
|
if( end_buff_pos <= 0 )
|
2006-08-31 00:45:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-16 14:39:12 +00:00
|
|
|
if( end_buff_pos >= data->buff_len )
|
2006-08-31 00:45:25 +00:00
|
|
|
break;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-08-31 00:45:25 +00:00
|
|
|
c = data->buff[end_buff_pos];
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-08-31 00:45:25 +00:00
|
|
|
if( !iswalnum( c ) )
|
|
|
|
{
|
|
|
|
/*
|
2006-10-12 16:13:17 +00:00
|
|
|
Don't gobble the boundary character when moving to the
|
|
|
|
right
|
2006-08-31 00:45:25 +00:00
|
|
|
*/
|
2006-10-12 16:13:17 +00:00
|
|
|
if( !dir )
|
2006-08-31 00:45:25 +00:00
|
|
|
end_buff_pos -= step;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
end_buff_pos+=step;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2006-10-12 16:13:17 +00:00
|
|
|
/*
|
|
|
|
Make sure we move at least one character
|
|
|
|
*/
|
|
|
|
if( end_buff_pos==data->buff_pos )
|
|
|
|
{
|
|
|
|
end_buff_pos+=step;
|
|
|
|
}
|
2006-10-16 14:39:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Make sure we don't move beyond begining or end of buffer
|
|
|
|
*/
|
|
|
|
end_buff_pos = maxi( 0, mini( end_buff_pos, data->buff_len ) );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-16 15:32:26 +00:00
|
|
|
|
2006-10-12 16:13:17 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( erase )
|
|
|
|
{
|
|
|
|
int remove_count = abs(data->buff_pos - end_buff_pos);
|
|
|
|
int first_char = mini( data->buff_pos, end_buff_pos );
|
|
|
|
// fwprintf( stderr, L"Remove from %d to %d\n", first_char, first_char+remove_count );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
reader_kill( data->buff + first_char, remove_count, dir?KILL_APPEND:KILL_PREPEND, newv );
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-01 16:02:58 +00:00
|
|
|
data->buff_pos = end_buff_pos;
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
wchar_t *reader_get_buffer(void)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
|
|
|
return data?data->buff:NULL;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
void reader_set_buffer( const wchar_t *b, int p )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
int l = wcslen( b );
|
|
|
|
|
|
|
|
if( !data )
|
|
|
|
return;
|
|
|
|
|
|
|
|
data->buff_len = l;
|
|
|
|
check_size();
|
2006-10-04 21:39:48 +00:00
|
|
|
|
|
|
|
if( data->buff != b )
|
|
|
|
wcscpy( data->buff, b );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
if( p>=0 )
|
|
|
|
{
|
2006-10-04 21:39:48 +00:00
|
|
|
data->buff_pos=mini( p, l );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data->buff_pos=l;
|
|
|
|
}
|
|
|
|
|
2009-02-21 15:44:20 +00:00
|
|
|
data->search_mode = NO_SEARCH;
|
|
|
|
sb_clear( &data->search_buff );
|
|
|
|
history_reset();
|
|
|
|
|
2006-10-16 15:32:26 +00:00
|
|
|
reader_super_highlight_me_plenty( data->buff_pos,
|
2007-10-05 14:59:19 +00:00
|
|
|
0 );
|
|
|
|
reader_repaint_needed();
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int reader_get_cursor_pos()
|
|
|
|
{
|
|
|
|
if( !data )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return data->buff_pos;
|
|
|
|
}
|
|
|
|
|
2009-11-24 15:28:42 +00:00
|
|
|
#define ENV_CMD_DURATION L"CMD_DURATION"
|
|
|
|
|
|
|
|
void set_env_cmd_duration(struct timeval *after, struct timeval *before)
|
|
|
|
{
|
|
|
|
time_t secs = after->tv_sec - before->tv_sec;
|
|
|
|
suseconds_t usecs = after->tv_usec - before->tv_usec;
|
|
|
|
wchar_t buf[16];
|
|
|
|
|
|
|
|
if (after->tv_usec < before->tv_usec) {
|
|
|
|
usecs += 1000000;
|
|
|
|
secs -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (secs < 1) {
|
|
|
|
env_remove( ENV_CMD_DURATION, 0 );
|
|
|
|
} else {
|
|
|
|
if (secs < 10) { // 10 secs
|
|
|
|
swprintf(buf, 16, L"%lu.%02us", secs, usecs / 10000);
|
|
|
|
} else if (secs < 60) { // 1 min
|
|
|
|
swprintf(buf, 16, L"%lu.%01us", secs, usecs / 100000);
|
|
|
|
} else if (secs < 600) { // 10 mins
|
|
|
|
swprintf(buf, 16, L"%lum %lu.%01us", secs / 60, secs % 60, usecs / 100000);
|
|
|
|
} else if (secs < 5400) { // 1.5 hours
|
|
|
|
swprintf(buf, 16, L"%lum %lus", secs / 60, secs % 60);
|
|
|
|
} else {
|
|
|
|
swprintf(buf, 16, L"%.1fh", secs / 3600.0f);
|
|
|
|
}
|
|
|
|
env_set( ENV_CMD_DURATION, buf, ENV_EXPORT );
|
|
|
|
}
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-01-23 05:40:08 +00:00
|
|
|
void reader_run_command( parser_t &parser, const wchar_t *cmd )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
wchar_t *ft;
|
2009-11-24 15:28:42 +00:00
|
|
|
struct timeval time_before, time_after;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
ft= tok_first( cmd );
|
|
|
|
|
|
|
|
if( ft != 0 )
|
|
|
|
env_set( L"_", ft, ENV_GLOBAL );
|
|
|
|
free(ft);
|
|
|
|
|
|
|
|
reader_write_title();
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
term_donate();
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2009-11-24 15:28:42 +00:00
|
|
|
gettimeofday(&time_before, NULL);
|
|
|
|
|
2012-01-23 05:40:08 +00:00
|
|
|
parser.eval( cmd, 0, TOP );
|
2005-10-11 19:23:43 +00:00
|
|
|
job_reap( 1 );
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2009-11-24 15:28:42 +00:00
|
|
|
gettimeofday(&time_after, NULL);
|
|
|
|
set_env_cmd_duration(&time_after, &time_before);
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
term_steal();
|
|
|
|
|
2006-05-27 13:49:18 +00:00
|
|
|
env_set( L"_", program_name, ENV_GLOBAL );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
#ifdef HAVE__PROC_SELF_STAT
|
|
|
|
proc_update_jiffies();
|
|
|
|
#endif
|
|
|
|
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-29 16:26:24 +00:00
|
|
|
int reader_shell_test( wchar_t *b )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-01-23 05:40:08 +00:00
|
|
|
int res = parser_t::principal_parser().test( b, 0, 0, 0 );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-01 16:02:58 +00:00
|
|
|
if( res & PARSER_TEST_ERROR )
|
2006-05-27 11:14:56 +00:00
|
|
|
{
|
2006-06-02 02:15:17 +00:00
|
|
|
string_buffer_t sb;
|
|
|
|
sb_init( &sb );
|
2006-10-01 16:02:58 +00:00
|
|
|
|
|
|
|
int tmp[1];
|
2006-10-07 00:56:25 +00:00
|
|
|
int tmp2[1];
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-07 00:56:25 +00:00
|
|
|
s_write( &data->screen, L"", L"", tmp, tmp2, 0 );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-01-23 05:40:08 +00:00
|
|
|
parser_t::principal_parser().test( b, 0, &sb, L"fish" );
|
2006-06-02 02:15:17 +00:00
|
|
|
fwprintf( stderr, L"%ls", sb.buff );
|
|
|
|
sb_destroy( &sb );
|
2006-05-27 11:14:56 +00:00
|
|
|
}
|
2006-10-01 16:02:58 +00:00
|
|
|
return res;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
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 int default_test( wchar_t *b )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
void reader_push( const wchar_t *name )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2011-12-28 02:41:38 +00:00
|
|
|
// use placement new to guarantee zero initialization :(
|
|
|
|
void *buff = calloc(1, sizeof(reader_data_t));
|
|
|
|
if( !buff )
|
2007-01-29 17:52:23 +00:00
|
|
|
{
|
|
|
|
DIE_MEM();
|
|
|
|
}
|
2011-12-28 02:41:38 +00:00
|
|
|
reader_data_t *n = new(buff) reader_data_t;
|
|
|
|
|
2011-12-28 20:36:47 +00:00
|
|
|
n->app_name = name;
|
2005-09-20 13:26:39 +00:00
|
|
|
n->next = data;
|
2006-10-12 13:27:32 +00:00
|
|
|
sb_init( &n->kill_item );
|
2006-10-01 16:02:58 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
data=n;
|
2006-10-01 16:02:58 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
check_size();
|
2007-04-16 21:26:15 +00:00
|
|
|
data->buff[0]=0;
|
|
|
|
sb_init( &data->search_buff );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
if( data->next == 0 )
|
|
|
|
{
|
|
|
|
reader_interactive_init();
|
|
|
|
}
|
2006-10-16 15:32:26 +00:00
|
|
|
|
|
|
|
exec_prompt();
|
2005-09-20 13:26:39 +00:00
|
|
|
reader_set_highlight_function( &highlight_universal );
|
|
|
|
reader_set_test_function( &default_test );
|
|
|
|
reader_set_prompt( L"" );
|
|
|
|
history_set_mode( name );
|
|
|
|
|
|
|
|
al_init( &data->search_prev );
|
|
|
|
data->token_history_buff=0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void reader_pop()
|
|
|
|
{
|
|
|
|
reader_data_t *n = data;
|
|
|
|
|
|
|
|
if( data == 0 )
|
|
|
|
{
|
2006-01-04 12:51:02 +00:00
|
|
|
debug( 0, _( L"Pop null reader block" ) );
|
2005-09-20 13:26:39 +00:00
|
|
|
sanity_lose();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
data=data->next;
|
|
|
|
|
|
|
|
free( n->buff );
|
|
|
|
free( n->color );
|
2006-10-07 00:56:25 +00:00
|
|
|
free( n->indent );
|
2007-04-16 21:26:15 +00:00
|
|
|
sb_destroy( &n->search_buff );
|
2006-10-12 13:27:32 +00:00
|
|
|
sb_destroy( &n->kill_item );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/*
|
|
|
|
Clean up after history search
|
|
|
|
*/
|
2006-06-12 21:47:42 +00:00
|
|
|
al_foreach( &n->search_prev, &free );
|
2006-01-30 19:53:10 +00:00
|
|
|
al_destroy( &n->search_prev );
|
2005-09-20 13:26:39 +00:00
|
|
|
free( (void *)n->token_history_buff);
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2011-12-28 02:41:38 +00:00
|
|
|
/* Invoke the destructor to balance our placement new */
|
|
|
|
n->~reader_data_t();
|
2005-09-20 13:26:39 +00:00
|
|
|
free(n);
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( data == 0 )
|
|
|
|
{
|
|
|
|
reader_interactive_destroy();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-01-29 17:45:01 +00:00
|
|
|
end_loop = 0;
|
2011-12-28 20:36:47 +00:00
|
|
|
history_set_mode( data->app_name.c_str() );
|
2007-09-24 08:13:01 +00:00
|
|
|
s_reset( &data->screen, 1 );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
void reader_set_prompt( const wchar_t *new_prompt )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2011-12-28 20:36:47 +00:00
|
|
|
data->prompt = new_prompt;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void reader_set_complete_function( void (*f)( const wchar_t *,
|
2012-01-16 16:56:47 +00:00
|
|
|
std::vector<completion_t>& ) )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
data->complete_func = f;
|
|
|
|
}
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
void reader_set_highlight_function( highlight_function_t func )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
data->highlight_function = func;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void reader_set_test_function( int (*f)( wchar_t * ) )
|
|
|
|
{
|
|
|
|
data->test_func = f;
|
|
|
|
}
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
/** A class as the context pointer for a background (threaded) highlight operation. */
|
|
|
|
class background_highlight_context {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
The string to highlight
|
|
|
|
*/
|
|
|
|
wcstring buff;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Malloc'd color buffer (same size as buff)
|
|
|
|
*/
|
|
|
|
int *color;
|
|
|
|
|
|
|
|
/**
|
|
|
|
The position to use for bracket matching
|
|
|
|
*/
|
|
|
|
int match_highlight_pos;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Function for syntax highlighting
|
|
|
|
*/
|
|
|
|
highlight_function_t highlight_function;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Environment variables
|
|
|
|
*/
|
|
|
|
env_vars vars;
|
|
|
|
|
|
|
|
/**
|
|
|
|
When the request was made
|
|
|
|
*/
|
|
|
|
double when;
|
|
|
|
|
|
|
|
background_highlight_context() : vars(env_vars::highlighting_keys)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
~background_highlight_context()
|
|
|
|
{
|
|
|
|
free(color);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-01 01:01:19 +00:00
|
|
|
__attribute__((unused))
|
2011-12-27 03:18:46 +00:00
|
|
|
static void highlight_complete2( wchar_t *command, const int *colors, int position, void *ctx_ptr ) {
|
|
|
|
background_highlight_context *ctx = (background_highlight_context *)ctx_ptr;
|
|
|
|
if (ctx->buff == data->buff) {
|
|
|
|
/* The data hasn't changed, so swap in our colors */
|
|
|
|
free(data->color);
|
|
|
|
data->color = ctx->color;
|
|
|
|
ctx->color = NULL;
|
|
|
|
data->repaint_needed = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free our context */
|
|
|
|
delete ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void highlight_complete(void *ctx_ptr, int result) {
|
|
|
|
background_highlight_context *ctx = (background_highlight_context *)ctx_ptr;
|
|
|
|
if (ctx->buff == data->buff) {
|
|
|
|
/* The data hasn't changed, so swap in our colors */
|
|
|
|
free(data->color);
|
|
|
|
data->color = ctx->color;
|
|
|
|
ctx->color = NULL;
|
|
|
|
//data->repaint_needed = 1;
|
|
|
|
//s_reset( &data->screen, 1 );
|
|
|
|
reader_repaint();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free our context */
|
|
|
|
delete ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int threaded_highlight(void *ctx_ptr) {
|
|
|
|
background_highlight_context *ctx = (background_highlight_context *)ctx_ptr;
|
|
|
|
array_list_t *error = 0;
|
|
|
|
const wchar_t *delayer = ctx->vars.get(L"HIGHLIGHT_DELAY");
|
|
|
|
double secDelay = 0;
|
|
|
|
if (delayer) {
|
|
|
|
wcstring tmp = delayer;
|
|
|
|
secDelay = from_string<double>(tmp);
|
|
|
|
}
|
|
|
|
if (secDelay > 0) usleep((useconds_t)(secDelay * 1E6));
|
|
|
|
//write(0, "Start", 5);
|
|
|
|
ctx->highlight_function( ctx->buff.c_str(), ctx->color, ctx->match_highlight_pos, error, ctx->vars );
|
|
|
|
//write(0, "End", 3);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
|
|
|
Call specified external highlighting function and then do search
|
2006-09-24 17:57:23 +00:00
|
|
|
highlighting. Lastly, clear the background color under the cursor
|
2006-10-22 09:40:18 +00:00
|
|
|
to avoid repaint issues on terminals where e.g. syntax highligthing
|
|
|
|
maykes characters under the sursor unreadable.
|
2006-09-24 17:57:23 +00:00
|
|
|
|
|
|
|
\param match_highlight_pos the position to use for bracket matching. This need not be the same as the surrent cursor position
|
2006-10-16 15:32:26 +00:00
|
|
|
\param error if non-null, any possible errors in the buffer are further descibed by the strings inserted into the specified arraylist
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2006-10-16 15:32:26 +00:00
|
|
|
static void reader_super_highlight_me_plenty( int match_highlight_pos, array_list_t *error )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
/* Ensure we don't repaint with stale data */
|
|
|
|
size_t i;
|
|
|
|
for (i=0; i < data->buff_sz; i++) {
|
|
|
|
data->color[i] = HIGHLIGHT_NORMAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
background_highlight_context *ctx = new background_highlight_context;
|
|
|
|
ctx->buff = data->buff;
|
|
|
|
ctx->color = (int *)calloc(data->buff_sz, sizeof *ctx->color);
|
|
|
|
ctx->match_highlight_pos = match_highlight_pos;
|
|
|
|
ctx->highlight_function = data->highlight_function;
|
|
|
|
ctx->when = timef();
|
|
|
|
#if 1
|
|
|
|
iothread_perform(threaded_highlight, highlight_complete, ctx);
|
|
|
|
#else
|
|
|
|
data->highlight_function( ctx->buff, ctx->color, match_highlight_pos, error, highlight_complete2, ctx );
|
|
|
|
#endif
|
2006-10-16 15:32:26 +00:00
|
|
|
|
2007-04-16 21:26:15 +00:00
|
|
|
if( wcslen((wchar_t *)data->search_buff.buff) )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2007-04-16 21:26:15 +00:00
|
|
|
wchar_t * match = wcsstr( data->buff, (wchar_t *)data->search_buff.buff );
|
2005-09-20 13:26:39 +00:00
|
|
|
if( match )
|
|
|
|
{
|
2006-09-24 17:57:23 +00:00
|
|
|
int start = match-data->buff;
|
2007-04-16 21:26:15 +00:00
|
|
|
int count = wcslen( (wchar_t *)data->search_buff.buff );
|
2005-09-20 13:26:39 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for( i=0; i<count; i++ )
|
|
|
|
{
|
2006-10-22 09:40:18 +00:00
|
|
|
data->color[start+i] |= HIGHLIGHT_SEARCH_MATCH<<16;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int exit_status()
|
|
|
|
{
|
|
|
|
if( is_interactive )
|
2012-01-30 00:36:21 +00:00
|
|
|
return job_list().empty() && data->end_loop;
|
2005-09-20 13:26:39 +00:00
|
|
|
else
|
|
|
|
return end_loop;
|
|
|
|
}
|
|
|
|
|
2007-09-26 09:01:59 +00:00
|
|
|
/**
|
|
|
|
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_end_loop()
|
|
|
|
{
|
|
|
|
job_t *j;
|
|
|
|
int job_count=0;
|
|
|
|
int is_breakpoint=0;
|
|
|
|
block_t *b;
|
2012-01-23 05:40:08 +00:00
|
|
|
parser_t &parser = parser_t::principal_parser();
|
|
|
|
|
|
|
|
for( b = parser.current_block;
|
2011-12-27 03:18:46 +00:00
|
|
|
b;
|
2007-09-26 09:01:59 +00:00
|
|
|
b = b->outer )
|
|
|
|
{
|
|
|
|
if( b->type == BREAKPOINT )
|
|
|
|
{
|
|
|
|
is_breakpoint = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-01-30 00:36:21 +00:00
|
|
|
job_iterator_t jobs;
|
|
|
|
while ((j = jobs.next()))
|
2007-09-26 09:01:59 +00:00
|
|
|
{
|
|
|
|
if( !job_is_completed(j) )
|
|
|
|
{
|
|
|
|
job_count++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-09-26 09:01:59 +00:00
|
|
|
if( !reader_exit_forced() && !data->prev_end_loop && job_count && !is_breakpoint )
|
|
|
|
{
|
2009-02-01 15:16:01 +00:00
|
|
|
writestr(_( L"There are stopped jobs. A second attempt to exit will enforce their termination.\n" ));
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-09-26 09:01:59 +00:00
|
|
|
reader_exit( 0, 0 );
|
|
|
|
data->prev_end_loop=1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( !isatty(0) )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
We already know that stdin is a tty since we're
|
|
|
|
in interactive mode. If isatty returns false, it
|
2011-12-27 03:18:46 +00:00
|
|
|
means stdin must have been closed.
|
2007-09-26 09:01:59 +00:00
|
|
|
*/
|
2012-01-30 00:36:21 +00:00
|
|
|
job_iterator_t jobs;
|
|
|
|
while ((j = jobs.next()))
|
2007-09-26 09:01:59 +00:00
|
|
|
{
|
|
|
|
if( ! job_is_completed( j ) )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
job_signal( j, SIGHUP );
|
2007-09-26 09:01:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
|
|
|
Read interactively. Read input from stdin while providing editing
|
|
|
|
facilities.
|
|
|
|
*/
|
|
|
|
static int read_i()
|
|
|
|
{
|
2007-08-19 16:42:30 +00:00
|
|
|
event_fire_generic(L"fish_prompt");
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
reader_push(L"fish");
|
2012-01-16 16:56:47 +00:00
|
|
|
reader_set_complete_function( &complete2 );
|
2005-09-20 13:26:39 +00:00
|
|
|
reader_set_highlight_function( &highlight_shell );
|
2007-01-29 16:26:24 +00:00
|
|
|
reader_set_test_function( &reader_shell_test );
|
2012-01-23 05:40:08 +00:00
|
|
|
parser_t &parser = parser_t::principal_parser();
|
|
|
|
|
2006-05-14 10:16:23 +00:00
|
|
|
data->prev_end_loop=0;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
while( (!data->end_loop) && (!sanity_check()) )
|
|
|
|
{
|
|
|
|
wchar_t *tmp;
|
|
|
|
|
2007-01-15 17:51:44 +00:00
|
|
|
if( function_exists( PROMPT_FUNCTION_NAME ) )
|
|
|
|
reader_set_prompt( PROMPT_FUNCTION_NAME );
|
2005-09-20 13:26:39 +00:00
|
|
|
else
|
|
|
|
reader_set_prompt( DEFAULT_PROMPT );
|
|
|
|
|
|
|
|
/*
|
|
|
|
Put buff in temporary string and clear buff, so
|
|
|
|
that we can handle a call to reader_set_buffer
|
|
|
|
during evaluation.
|
|
|
|
*/
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-05-14 10:16:23 +00:00
|
|
|
tmp = reader_readline();
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
if( data->end_loop)
|
|
|
|
{
|
2007-09-26 09:01:59 +00:00
|
|
|
handle_end_loop();
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2007-01-15 17:53:46 +00:00
|
|
|
else if( tmp )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-05-14 10:16:23 +00:00
|
|
|
tmp = wcsdup( tmp );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-05-14 10:16:23 +00:00
|
|
|
data->buff_pos=data->buff_len=0;
|
|
|
|
data->buff[data->buff_len]=L'\0';
|
2012-01-23 05:40:08 +00:00
|
|
|
reader_run_command( parser, tmp );
|
2006-05-14 10:16:23 +00:00
|
|
|
free( tmp );
|
2007-09-26 09:01:59 +00:00
|
|
|
if( data->end_loop)
|
|
|
|
{
|
|
|
|
handle_end_loop();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data->prev_end_loop=0;
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-01-18 12:42:48 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
reader_pop();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-10-20 12:06:10 +00:00
|
|
|
/**
|
|
|
|
Test if there are bytes available for reading on the specified file
|
|
|
|
descriptor
|
|
|
|
*/
|
2005-10-13 14:08:33 +00:00
|
|
|
static int can_read( int fd )
|
|
|
|
{
|
2005-10-25 11:22:47 +00:00
|
|
|
struct timeval can_read_timeout = { 0, 0 };
|
|
|
|
fd_set fds;
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-10-25 11:22:47 +00:00
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(fd, &fds);
|
|
|
|
return select(fd + 1, &fds, 0, 0, &can_read_timeout) == 1;
|
2005-10-13 14:08:33 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2005-10-20 11:27:54 +00:00
|
|
|
/**
|
2005-10-20 12:06:10 +00:00
|
|
|
Test if the specified character is in the private use area that
|
|
|
|
fish uses to store internal characters
|
2005-10-20 11:27:54 +00:00
|
|
|
*/
|
|
|
|
static int wchar_private( wchar_t c )
|
|
|
|
{
|
|
|
|
return ( (c >= 0xe000) && (c <= 0xf8ff ) );
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-11-20 02:19:34 +00:00
|
|
|
/**
|
|
|
|
Test if the specified character in the specified string is
|
|
|
|
backslashed.
|
|
|
|
*/
|
|
|
|
static int is_backslashed( const wchar_t *str, int pos )
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
int i;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-11-20 02:19:34 +00:00
|
|
|
for( i=pos-1; i>=0; i-- )
|
|
|
|
{
|
|
|
|
if( str[i] != L'\\' )
|
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-11-20 02:19:34 +00:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count %2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
wchar_t *reader_readline()
|
|
|
|
{
|
|
|
|
|
2005-10-11 19:48:31 +00:00
|
|
|
wint_t c;
|
2005-09-20 13:26:39 +00:00
|
|
|
int i;
|
|
|
|
int last_char=0, yank=0;
|
2012-01-14 11:41:50 +00:00
|
|
|
const wchar_t *yank_str;
|
2012-01-16 16:56:47 +00:00
|
|
|
std::vector<completion_t> comp;
|
2005-09-20 13:26:39 +00:00
|
|
|
int comp_empty=1;
|
|
|
|
int finished=0;
|
|
|
|
struct termios old_modes;
|
|
|
|
|
|
|
|
check_size();
|
2007-04-16 21:26:15 +00:00
|
|
|
sb_clear( &data->search_buff );
|
|
|
|
data->buff[data->buff_len]='\0';
|
2007-09-21 14:05:49 +00:00
|
|
|
data->search_mode = NO_SEARCH;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
|
2006-10-16 15:32:26 +00:00
|
|
|
exec_prompt();
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-10-16 15:32:26 +00:00
|
|
|
reader_super_highlight_me_plenty( data->buff_pos, 0 );
|
2007-09-30 22:53:54 +00:00
|
|
|
s_reset( &data->screen, 1 );
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
/*
|
2006-11-02 13:50:19 +00:00
|
|
|
get the current terminal modes. These will be restored when the
|
|
|
|
function returns.
|
|
|
|
*/
|
2011-12-27 03:18:46 +00:00
|
|
|
tcgetattr(0,&old_modes);
|
2006-11-02 13:50:19 +00:00
|
|
|
/* set the new modes */
|
2006-11-20 13:14:12 +00:00
|
|
|
if( tcsetattr(0,TCSANOW,&shell_modes))
|
2005-12-30 16:29:19 +00:00
|
|
|
{
|
2006-11-20 13:14:12 +00:00
|
|
|
wperror(L"tcsetattr");
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while( !finished && !data->end_loop)
|
|
|
|
{
|
2007-04-16 21:26:15 +00:00
|
|
|
int regular_char = 0;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/*
|
2005-10-11 19:48:31 +00:00
|
|
|
Sometimes strange input sequences seem to generate a zero
|
|
|
|
byte. I believe these simply mean a character was pressed
|
|
|
|
but it should be ignored. (Example: Trying to add a tilde
|
|
|
|
(~) to digit)
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2005-10-13 14:08:33 +00:00
|
|
|
while( 1 )
|
|
|
|
{
|
2007-01-31 23:58:10 +00:00
|
|
|
int was_interactive_read = is_interactive_read;
|
|
|
|
is_interactive_read = 1;
|
2005-10-13 14:08:33 +00:00
|
|
|
c=input_readch();
|
2007-01-31 23:58:10 +00:00
|
|
|
is_interactive_read = was_interactive_read;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-10-20 11:27:54 +00:00
|
|
|
if( ( (!wchar_private(c))) && (c>31) && (c != 127) )
|
2005-10-13 14:08:33 +00:00
|
|
|
{
|
|
|
|
if( can_read(0) )
|
|
|
|
{
|
|
|
|
|
|
|
|
wchar_t arr[READAHEAD_MAX+1];
|
|
|
|
int i;
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-10-13 14:08:33 +00:00
|
|
|
memset( arr, 0, sizeof( arr ) );
|
|
|
|
arr[0] = c;
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-10-13 14:08:33 +00:00
|
|
|
for( i=1; i<READAHEAD_MAX; i++ )
|
|
|
|
{
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-10-13 14:08:33 +00:00
|
|
|
if( !can_read( 0 ) )
|
|
|
|
{
|
|
|
|
c = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
c = input_readch();
|
2005-10-20 11:27:54 +00:00
|
|
|
if( (!wchar_private(c)) && (c>31) && (c != 127) )
|
2005-10-13 14:08:33 +00:00
|
|
|
{
|
|
|
|
arr[i]=c;
|
|
|
|
c=0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-10-13 14:08:33 +00:00
|
|
|
insert_str( arr );
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-10-13 14:08:33 +00:00
|
|
|
}
|
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-10-13 14:08:33 +00:00
|
|
|
if( c != 0 )
|
|
|
|
break;
|
|
|
|
}
|
2007-02-09 09:33:50 +00:00
|
|
|
/*
|
2007-03-24 19:07:38 +00:00
|
|
|
if( (last_char == R_COMPLETE) && (c != R_COMPLETE) && (!comp_empty) )
|
|
|
|
{
|
|
|
|
halloc_destroy( comp );
|
|
|
|
comp = 0;
|
|
|
|
}
|
2007-02-09 09:33:50 +00:00
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
if( last_char != R_YANK && last_char != R_YANK_POP )
|
|
|
|
yank=0;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-12-13 10:18:59 +00:00
|
|
|
switch( c )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
/* go to beginning of line*/
|
|
|
|
case R_BEGINNING_OF_LINE:
|
2006-10-31 22:01:49 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
while( ( data->buff_pos>0 ) &&
|
2006-11-02 13:50:19 +00:00
|
|
|
( data->buff[data->buff_pos-1] != L'\n' ) )
|
|
|
|
{
|
2006-10-31 22:01:49 +00:00
|
|
|
data->buff_pos--;
|
2006-11-02 13:50:19 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2006-10-31 22:01:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case R_END_OF_LINE:
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
while( data->buff[data->buff_pos] &&
|
2006-11-02 13:50:19 +00:00
|
|
|
data->buff[data->buff_pos] != L'\n' )
|
|
|
|
{
|
2006-10-31 22:01:49 +00:00
|
|
|
data->buff_pos++;
|
2006-11-02 13:50:19 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2006-10-31 22:01:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-31 22:01:49 +00:00
|
|
|
case R_BEGINNING_OF_BUFFER:
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
data->buff_pos = 0;
|
|
|
|
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* go to EOL*/
|
2006-10-31 22:01:49 +00:00
|
|
|
case R_END_OF_BUFFER:
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
data->buff_pos = data->buff_len;
|
|
|
|
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case R_NULL:
|
|
|
|
{
|
2007-10-05 14:59:19 +00:00
|
|
|
if( data->repaint_needed )
|
|
|
|
reader_repaint();
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-12-12 10:13:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case R_REPAINT:
|
|
|
|
{
|
|
|
|
exec_prompt();
|
2009-02-22 20:28:52 +00:00
|
|
|
write_loop( 1, "\r", 1 );
|
2007-09-30 22:53:54 +00:00
|
|
|
s_reset( &data->screen, 0 );
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2006-10-04 21:45:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-05-14 10:16:23 +00:00
|
|
|
case R_EOF:
|
|
|
|
{
|
|
|
|
exit_forced = 1;
|
|
|
|
data->end_loop=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/* complete */
|
|
|
|
case R_COMPLETE:
|
|
|
|
{
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( !data->complete_func )
|
|
|
|
break;
|
|
|
|
|
2006-11-30 23:58:52 +00:00
|
|
|
if( comp_empty || last_char != R_COMPLETE)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-06-14 13:22:40 +00:00
|
|
|
wchar_t *begin, *end;
|
|
|
|
wchar_t *token_begin, *token_end;
|
2005-09-20 13:26:39 +00:00
|
|
|
wchar_t *buffcpy;
|
2006-03-11 11:56:12 +00:00
|
|
|
int len;
|
|
|
|
int cursor_steps;
|
2007-02-09 09:33:50 +00:00
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
parse_util_cmdsubst_extent( data->buff, data->buff_pos, &begin, &end );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-03-11 11:56:12 +00:00
|
|
|
parse_util_token_extent( begin, data->buff_pos - (begin-data->buff), &token_begin, &token_end, 0, 0 );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-03-11 11:56:12 +00:00
|
|
|
cursor_steps = token_end - data->buff- data->buff_pos;
|
|
|
|
data->buff_pos += cursor_steps;
|
2007-01-26 17:14:13 +00:00
|
|
|
if( is_backslashed( data->buff, data->buff_pos ) )
|
|
|
|
{
|
|
|
|
remove_backward();
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-07-07 15:36:38 +00:00
|
|
|
len = data->buff_pos - (begin-data->buff);
|
2005-09-20 13:26:39 +00:00
|
|
|
buffcpy = wcsndup( begin, len );
|
|
|
|
|
2012-01-16 16:56:47 +00:00
|
|
|
// comp = al_halloc( 0 );
|
2007-02-09 09:33:50 +00:00
|
|
|
data->complete_func( buffcpy, comp );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-18 23:25:20 +00:00
|
|
|
sort_completion_list( comp );
|
|
|
|
remove_duplicates( comp );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
free( buffcpy );
|
2007-02-09 09:33:50 +00:00
|
|
|
comp_empty = handle_completions( comp );
|
2012-01-16 16:56:47 +00:00
|
|
|
comp.clear();
|
|
|
|
// halloc_free( comp );
|
|
|
|
// comp = 0;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-10-12 13:27:32 +00:00
|
|
|
/* kill */
|
2005-09-20 13:26:39 +00:00
|
|
|
case R_KILL_LINE:
|
|
|
|
{
|
2006-10-12 13:27:32 +00:00
|
|
|
wchar_t *begin = &data->buff[data->buff_pos];
|
|
|
|
wchar_t *end = begin;
|
|
|
|
int len;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-12 13:27:32 +00:00
|
|
|
while( *end && *end != L'\n' )
|
|
|
|
end++;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-12 13:27:32 +00:00
|
|
|
if( end==begin && *end )
|
|
|
|
end++;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-12 13:27:32 +00:00
|
|
|
len = end-begin;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-12 13:27:32 +00:00
|
|
|
if( len )
|
|
|
|
{
|
|
|
|
reader_kill( begin, len, KILL_APPEND, last_char!=R_KILL_LINE );
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case R_BACKWARD_KILL_LINE:
|
|
|
|
{
|
2006-10-12 13:27:32 +00:00
|
|
|
if( data->buff_pos > 0 )
|
|
|
|
{
|
|
|
|
wchar_t *end = &data->buff[data->buff_pos];
|
|
|
|
wchar_t *begin = end;
|
|
|
|
int len;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-12 13:27:32 +00:00
|
|
|
while( begin > data->buff && *begin != L'\n' )
|
|
|
|
begin--;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-12 13:27:32 +00:00
|
|
|
if( *begin == L'\n' )
|
|
|
|
begin++;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-12 13:27:32 +00:00
|
|
|
len = maxi( end-begin, 1 );
|
|
|
|
begin = end - len;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
reader_kill( begin, len, KILL_PREPEND, last_char!=R_BACKWARD_KILL_LINE );
|
|
|
|
|
2006-10-12 13:27:32 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
2006-10-12 13:27:32 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case R_KILL_WHOLE_LINE:
|
|
|
|
{
|
2006-10-12 13:27:32 +00:00
|
|
|
wchar_t *end = &data->buff[data->buff_pos];
|
|
|
|
wchar_t *begin = end;
|
|
|
|
int len;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-12 13:27:32 +00:00
|
|
|
while( begin > data->buff && *begin != L'\n' )
|
|
|
|
begin--;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-12 13:27:32 +00:00
|
|
|
if( *begin == L'\n' )
|
|
|
|
begin++;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-12 13:27:32 +00:00
|
|
|
len = maxi( end-begin, 0 );
|
|
|
|
begin = end - len;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-10-12 13:27:32 +00:00
|
|
|
while( *end && *end != L'\n' )
|
|
|
|
end++;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-12 13:27:32 +00:00
|
|
|
if( begin == end && *end )
|
|
|
|
end++;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-12 13:27:32 +00:00
|
|
|
len = end-begin;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-12 13:27:32 +00:00
|
|
|
if( len )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
reader_kill( begin, len, KILL_APPEND, last_char!=R_KILL_WHOLE_LINE );
|
2006-10-12 13:27:32 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* yank*/
|
|
|
|
case R_YANK:
|
2006-10-12 13:27:32 +00:00
|
|
|
{
|
|
|
|
yank_str = kill_yank();
|
2005-09-20 13:26:39 +00:00
|
|
|
insert_str( yank_str );
|
|
|
|
yank = wcslen( yank_str );
|
|
|
|
break;
|
2005-12-30 16:29:19 +00:00
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-12-30 16:29:19 +00:00
|
|
|
/* rotate killring*/
|
2005-09-20 13:26:39 +00:00
|
|
|
case R_YANK_POP:
|
2005-12-30 16:29:19 +00:00
|
|
|
{
|
2005-09-20 13:26:39 +00:00
|
|
|
if( yank )
|
|
|
|
{
|
|
|
|
for( i=0; i<yank; i++ )
|
|
|
|
remove_backward();
|
|
|
|
|
|
|
|
yank_str = kill_yank_rotate();
|
|
|
|
insert_str(yank_str);
|
|
|
|
yank = wcslen(yank_str);
|
|
|
|
}
|
|
|
|
break;
|
2005-12-30 16:29:19 +00:00
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-12-30 16:29:19 +00:00
|
|
|
/* Escape was pressed */
|
2007-08-22 07:52:39 +00:00
|
|
|
case L'\x1b':
|
2005-12-30 16:29:19 +00:00
|
|
|
{
|
2007-09-21 14:05:49 +00:00
|
|
|
if( data->search_mode )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2007-09-21 14:05:49 +00:00
|
|
|
data->search_mode= NO_SEARCH;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-24 16:31:22 +00:00
|
|
|
if( data->token_history_pos==-1 )
|
2006-01-30 19:53:10 +00:00
|
|
|
{
|
2005-09-24 16:31:22 +00:00
|
|
|
history_reset();
|
2007-04-16 21:26:15 +00:00
|
|
|
reader_set_buffer( (wchar_t *)data->search_buff.buff,
|
|
|
|
wcslen( (wchar_t *)data->search_buff.buff ) );
|
2005-09-24 16:31:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-04-16 21:26:15 +00:00
|
|
|
reader_replace_current_token( (wchar_t *)data->search_buff.buff );
|
2005-09-24 16:31:22 +00:00
|
|
|
}
|
2007-04-16 21:26:15 +00:00
|
|
|
sb_clear( &data->search_buff );
|
2006-11-16 12:58:03 +00:00
|
|
|
reader_super_highlight_me_plenty( data->buff_pos, 0 );
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
2005-12-30 16:29:19 +00:00
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-12-30 16:29:19 +00:00
|
|
|
/* delete backward*/
|
2005-09-20 13:26:39 +00:00
|
|
|
case R_BACKWARD_DELETE_CHAR:
|
2005-12-30 16:29:19 +00:00
|
|
|
{
|
2005-09-20 13:26:39 +00:00
|
|
|
remove_backward();
|
|
|
|
break;
|
2005-12-30 16:29:19 +00:00
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-12-30 16:29:19 +00:00
|
|
|
/* delete forward*/
|
2005-09-20 13:26:39 +00:00
|
|
|
case R_DELETE_CHAR:
|
2005-12-30 16:29:19 +00:00
|
|
|
{
|
2006-10-16 15:32:26 +00:00
|
|
|
/**
|
|
|
|
Remove the current character in the character buffer and on the
|
|
|
|
screen using syntax highlighting, etc.
|
|
|
|
*/
|
|
|
|
if( data->buff_pos < data->buff_len )
|
|
|
|
{
|
|
|
|
data->buff_pos++;
|
|
|
|
remove_backward();
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
2005-12-30 16:29:19 +00:00
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2006-10-09 01:15:29 +00:00
|
|
|
/*
|
|
|
|
Evaluate. If the current command is unfinished, or if
|
|
|
|
the charater is escaped using a backslash, insert a
|
|
|
|
newline
|
|
|
|
*/
|
|
|
|
case R_EXECUTE:
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-10-01 16:02:58 +00:00
|
|
|
/*
|
|
|
|
Allow backslash-escaped newlines
|
|
|
|
*/
|
2006-11-20 02:19:34 +00:00
|
|
|
if( is_backslashed( data->buff, data->buff_pos ) )
|
2006-10-01 16:02:58 +00:00
|
|
|
{
|
|
|
|
insert_char( '\n' );
|
|
|
|
break;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-01 16:02:58 +00:00
|
|
|
switch( data->test_func( data->buff ) )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
|
2006-10-01 16:02:58 +00:00
|
|
|
case 0:
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-10-01 16:02:58 +00:00
|
|
|
/*
|
|
|
|
Finished commend, execute it
|
|
|
|
*/
|
|
|
|
if( wcslen( data->buff ) )
|
|
|
|
{
|
2005-09-20 13:26:39 +00:00
|
|
|
// wcscpy(data->search_buff,L"");
|
2006-10-01 16:02:58 +00:00
|
|
|
history_add( data->buff );
|
|
|
|
}
|
|
|
|
finished=1;
|
|
|
|
data->buff_pos=data->buff_len;
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2006-10-01 16:02:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-01 16:02:58 +00:00
|
|
|
/*
|
|
|
|
We are incomplete, continue editing
|
|
|
|
*/
|
|
|
|
case PARSER_TEST_INCOMPLETE:
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
2006-10-01 16:02:58 +00:00
|
|
|
insert_char( '\n' );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-11-02 13:50:19 +00:00
|
|
|
Result must be some combination including an
|
|
|
|
error. The error message will already be
|
|
|
|
printed, all we need to do is repaint
|
2006-10-01 16:02:58 +00:00
|
|
|
*/
|
|
|
|
default:
|
|
|
|
{
|
2007-09-24 08:13:01 +00:00
|
|
|
s_reset( &data->screen, 1 );
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2006-10-01 16:02:58 +00:00
|
|
|
break;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2006-10-01 16:02:58 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-04-16 21:26:15 +00:00
|
|
|
/* History functions */
|
2005-09-20 13:26:39 +00:00
|
|
|
case R_HISTORY_SEARCH_BACKWARD:
|
2007-04-16 21:26:15 +00:00
|
|
|
case R_HISTORY_TOKEN_SEARCH_BACKWARD:
|
2005-09-20 13:26:39 +00:00
|
|
|
case R_HISTORY_SEARCH_FORWARD:
|
2007-04-16 21:26:15 +00:00
|
|
|
case R_HISTORY_TOKEN_SEARCH_FORWARD:
|
2005-12-30 16:29:19 +00:00
|
|
|
{
|
2007-04-16 21:26:15 +00:00
|
|
|
int reset = 0;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-09-21 14:05:49 +00:00
|
|
|
if( data->search_mode == NO_SEARCH )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2007-04-16 21:26:15 +00:00
|
|
|
reset = 1;
|
|
|
|
if( ( c == R_HISTORY_SEARCH_BACKWARD ) ||
|
2007-09-21 14:05:49 +00:00
|
|
|
( c == R_HISTORY_SEARCH_FORWARD ) )
|
2007-04-16 21:26:15 +00:00
|
|
|
{
|
2007-09-21 14:05:49 +00:00
|
|
|
data->search_mode = LINE_SEARCH;
|
2007-04-16 21:26:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-09-21 14:05:49 +00:00
|
|
|
data->search_mode = TOKEN_SEARCH;
|
2007-04-16 21:26:15 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-04-16 21:26:15 +00:00
|
|
|
sb_append( &data->search_buff, data->buff );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2007-09-21 14:05:49 +00:00
|
|
|
switch( data->search_mode )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
|
2007-04-16 21:26:15 +00:00
|
|
|
case LINE_SEARCH:
|
|
|
|
{
|
|
|
|
const wchar_t *it = 0;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-04-16 21:26:15 +00:00
|
|
|
if( ( c == R_HISTORY_SEARCH_BACKWARD ) ||
|
|
|
|
( c == R_HISTORY_TOKEN_SEARCH_BACKWARD ) )
|
|
|
|
{
|
|
|
|
it = history_prev_match((wchar_t *)data->search_buff.buff);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
it = history_next_match((wchar_t *)data->search_buff.buff);
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-04-16 21:26:15 +00:00
|
|
|
handle_history( it );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-04-16 21:26:15 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-04-16 21:26:15 +00:00
|
|
|
case TOKEN_SEARCH:
|
|
|
|
{
|
|
|
|
if( ( c == R_HISTORY_SEARCH_BACKWARD ) ||
|
|
|
|
( c == R_HISTORY_TOKEN_SEARCH_BACKWARD ) )
|
|
|
|
{
|
|
|
|
handle_token_history( SEARCH_BACKWARD, reset );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
handle_token_history( SEARCH_FORWARD, reset );
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-04-16 21:26:15 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/* Move left*/
|
|
|
|
case R_BACKWARD_CHAR:
|
2006-04-05 12:48:25 +00:00
|
|
|
{
|
2005-09-20 13:26:39 +00:00
|
|
|
if( data->buff_pos > 0 )
|
|
|
|
{
|
|
|
|
data->buff_pos--;
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
break;
|
2006-04-05 12:48:25 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-04-05 12:48:25 +00:00
|
|
|
/* Move right*/
|
2005-09-20 13:26:39 +00:00
|
|
|
case R_FORWARD_CHAR:
|
2005-12-30 16:29:19 +00:00
|
|
|
{
|
2005-09-20 13:26:39 +00:00
|
|
|
if( data->buff_pos < data->buff_len )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
data->buff_pos++;
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
break;
|
2005-12-30 16:29:19 +00:00
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-12-30 16:29:19 +00:00
|
|
|
/* kill one word left */
|
2005-09-20 13:26:39 +00:00
|
|
|
case R_BACKWARD_KILL_WORD:
|
2005-12-30 16:29:19 +00:00
|
|
|
{
|
2006-10-12 19:30:00 +00:00
|
|
|
move_word(0,1, last_char!=R_BACKWARD_KILL_WORD);
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
2005-12-30 16:29:19 +00:00
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-12-30 16:29:19 +00:00
|
|
|
/* kill one word right */
|
2005-09-20 13:26:39 +00:00
|
|
|
case R_KILL_WORD:
|
2005-12-30 16:29:19 +00:00
|
|
|
{
|
2006-10-12 19:30:00 +00:00
|
|
|
move_word(1,1, last_char!=R_KILL_WORD);
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
2005-12-30 16:29:19 +00:00
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-12-30 16:29:19 +00:00
|
|
|
/* move one word left*/
|
2005-09-20 13:26:39 +00:00
|
|
|
case R_BACKWARD_WORD:
|
2005-12-30 16:29:19 +00:00
|
|
|
{
|
2006-10-12 19:30:00 +00:00
|
|
|
move_word(0,0,0);
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
2005-12-30 16:29:19 +00:00
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-12-30 16:29:19 +00:00
|
|
|
/* move one word right*/
|
2005-09-20 13:26:39 +00:00
|
|
|
case R_FORWARD_WORD:
|
2005-12-30 16:29:19 +00:00
|
|
|
{
|
2006-10-12 19:30:00 +00:00
|
|
|
move_word( 1,0,0);
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
2005-12-30 16:29:19 +00:00
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
case R_BEGINNING_OF_HISTORY:
|
|
|
|
{
|
|
|
|
history_first();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case R_END_OF_HISTORY:
|
|
|
|
{
|
|
|
|
history_reset();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-09-21 14:05:49 +00:00
|
|
|
case R_UP_LINE:
|
|
|
|
case R_DOWN_LINE:
|
|
|
|
{
|
2007-09-21 14:23:01 +00:00
|
|
|
int line_old = parse_util_get_line_from_offset( data->buff,
|
|
|
|
data->buff_pos );
|
|
|
|
int line_new;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-09-21 14:05:49 +00:00
|
|
|
if( c == R_UP_LINE )
|
2007-09-21 14:23:01 +00:00
|
|
|
line_new = line_old-1;
|
2007-09-21 14:05:49 +00:00
|
|
|
else
|
2007-09-21 14:23:01 +00:00
|
|
|
line_new = line_old+1;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-09-21 14:05:49 +00:00
|
|
|
int line_count = parse_util_lineno( data->buff, data->buff_len )-1;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-09-21 14:23:01 +00:00
|
|
|
if( line_new >= 0 && line_new <= line_count)
|
2007-09-21 14:05:49 +00:00
|
|
|
{
|
2007-09-21 14:23:01 +00:00
|
|
|
int base_pos_new;
|
|
|
|
int base_pos_old;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-09-21 14:05:49 +00:00
|
|
|
int indent_old;
|
|
|
|
int indent_new;
|
2007-09-21 14:23:01 +00:00
|
|
|
int line_offset_old;
|
|
|
|
int total_offset_new;
|
2007-09-21 14:05:49 +00:00
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
base_pos_new = parse_util_get_offset_from_line( data->buff,
|
2007-09-21 14:23:01 +00:00
|
|
|
line_new );
|
2007-09-21 14:44:26 +00:00
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
base_pos_old = parse_util_get_offset_from_line( data->buff,
|
2007-09-21 14:23:01 +00:00
|
|
|
line_old );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
|
2007-09-21 14:23:01 +00:00
|
|
|
indent_old = data->indent[base_pos_old];
|
|
|
|
indent_new = data->indent[base_pos_new];
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-09-21 14:23:01 +00:00
|
|
|
line_offset_old = data->buff_pos - parse_util_get_offset_from_line( data->buff,
|
|
|
|
line_old );
|
|
|
|
total_offset_new = parse_util_get_offset( data->buff, line_new, line_offset_old - 4*(indent_new-indent_old));
|
|
|
|
data->buff_pos = total_offset_new;
|
2007-10-05 14:59:19 +00:00
|
|
|
reader_repaint();
|
2007-09-21 14:05:49 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-09-21 14:05:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-09-21 14:05:49 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/* Other, if a normal character, we add it to the command */
|
|
|
|
default:
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-11-03 00:34:57 +00:00
|
|
|
if( (!wchar_private(c)) && (( (c>31) || (c==L'\n'))&& (c != 127)) )
|
2006-10-09 01:15:29 +00:00
|
|
|
{
|
2007-04-16 21:26:15 +00:00
|
|
|
regular_char = 1;
|
2005-09-20 13:26:39 +00:00
|
|
|
insert_char( c );
|
2006-10-09 01:15:29 +00:00
|
|
|
}
|
2005-10-13 14:08:33 +00:00
|
|
|
else
|
2006-03-10 13:38:09 +00:00
|
|
|
{
|
2006-04-05 12:48:25 +00:00
|
|
|
/*
|
2006-06-15 13:50:23 +00:00
|
|
|
Low priority debug message. These can happen if
|
|
|
|
the user presses an unefined control
|
|
|
|
sequnece. No reason to report.
|
2006-04-05 12:48:25 +00:00
|
|
|
*/
|
2006-06-15 13:50:23 +00:00
|
|
|
debug( 2, _( L"Unknown keybinding %d" ), c );
|
2006-03-10 13:38:09 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( (c != R_HISTORY_SEARCH_BACKWARD) &&
|
2007-09-21 14:05:49 +00:00
|
|
|
(c != R_HISTORY_SEARCH_FORWARD) &&
|
|
|
|
(c != R_HISTORY_TOKEN_SEARCH_BACKWARD) &&
|
|
|
|
(c != R_HISTORY_TOKEN_SEARCH_FORWARD) &&
|
|
|
|
(c != R_NULL) )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2007-09-21 14:05:49 +00:00
|
|
|
data->search_mode = NO_SEARCH;
|
2007-04-16 21:26:15 +00:00
|
|
|
sb_clear( &data->search_buff );
|
2005-09-20 13:26:39 +00:00
|
|
|
history_reset();
|
2005-09-24 16:31:22 +00:00
|
|
|
data->token_history_pos=-1;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
last_char = c;
|
|
|
|
}
|
|
|
|
|
2007-01-29 17:45:01 +00:00
|
|
|
writestr( L"\n" );
|
2007-02-09 09:33:50 +00:00
|
|
|
/*
|
2007-03-24 19:07:38 +00:00
|
|
|
if( comp )
|
|
|
|
halloc_free( comp );
|
2007-02-09 09:33:50 +00:00
|
|
|
*/
|
2006-05-14 10:16:23 +00:00
|
|
|
if( !reader_exit_forced() )
|
|
|
|
{
|
|
|
|
if( tcsetattr(0,TCSANOW,&old_modes)) /* return to previous mode */
|
|
|
|
{
|
|
|
|
wperror(L"tcsetattr");
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-05-14 10:16:23 +00:00
|
|
|
set_color( FISH_COLOR_RESET, FISH_COLOR_RESET );
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-01-15 17:53:46 +00:00
|
|
|
return finished ? data->buff : 0;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2007-09-21 14:05:49 +00:00
|
|
|
int reader_search_mode()
|
|
|
|
{
|
|
|
|
if( !data )
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
return !!data->search_mode;
|
2007-09-21 14:05:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
|
|
|
Read non-interactively. Read input from stdin without displaying
|
|
|
|
the prompt, using syntax highlighting. This is used for reading
|
|
|
|
scripts and init files.
|
|
|
|
*/
|
2007-04-25 18:30:02 +00:00
|
|
|
static int read_ni( int fd, io_data_t *io )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-01-23 05:40:08 +00:00
|
|
|
parser_t &parser = parser_t::principal_parser();
|
2005-09-20 13:26:39 +00:00
|
|
|
FILE *in_stream;
|
|
|
|
wchar_t *buff=0;
|
|
|
|
buffer_t acc;
|
|
|
|
|
2005-10-19 12:07:44 +00:00
|
|
|
int des = fd == 0 ? dup(0) : fd;
|
2005-09-20 13:26:39 +00:00
|
|
|
int res=0;
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if (des == -1)
|
|
|
|
{
|
|
|
|
wperror( L"dup" );
|
|
|
|
return 1;
|
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
b_init( &acc );
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
in_stream = fdopen( des, "r" );
|
|
|
|
if( in_stream != 0 )
|
|
|
|
{
|
|
|
|
wchar_t *str;
|
2005-10-19 12:07:44 +00:00
|
|
|
int acc_used;
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
while(!feof( in_stream ))
|
|
|
|
{
|
|
|
|
char buff[4096];
|
2005-12-16 15:50:10 +00:00
|
|
|
int c;
|
|
|
|
|
|
|
|
c = fread(buff, 1, 4096, in_stream);
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-08-01 17:38:01 +00:00
|
|
|
if( ferror( in_stream ) && ( errno != EINTR ) )
|
2005-12-16 15:50:10 +00:00
|
|
|
{
|
2006-01-30 19:53:10 +00:00
|
|
|
debug( 1,
|
2007-08-01 17:38:01 +00:00
|
|
|
_( L"Error while reading from file descriptor" ) );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-12-16 15:50:10 +00:00
|
|
|
/*
|
2006-04-05 12:48:25 +00:00
|
|
|
Reset buffer on error. We won't evaluate incomplete files.
|
2005-12-16 15:50:10 +00:00
|
|
|
*/
|
|
|
|
acc.used=0;
|
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-12-16 15:50:10 +00:00
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
b_append( &acc, buff, c );
|
|
|
|
}
|
|
|
|
b_append( &acc, "\0", 1 );
|
2005-10-19 12:07:44 +00:00
|
|
|
acc_used = acc.used;
|
2005-09-20 13:26:39 +00:00
|
|
|
str = str2wcs( acc.buff );
|
|
|
|
b_destroy( &acc );
|
|
|
|
|
2005-10-19 12:07:44 +00:00
|
|
|
if( fclose( in_stream ))
|
|
|
|
{
|
2006-01-30 19:53:10 +00:00
|
|
|
debug( 1,
|
2006-01-11 14:17:35 +00:00
|
|
|
_( L"Error while closing input stream" ) );
|
2005-10-19 12:07:44 +00:00
|
|
|
wperror( L"fclose" );
|
|
|
|
res = 1;
|
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( str )
|
2006-01-30 19:53:10 +00:00
|
|
|
{
|
2007-03-24 19:07:38 +00:00
|
|
|
string_buffer_t sb;
|
|
|
|
sb_init( &sb );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-01-23 05:40:08 +00:00
|
|
|
if( ! parser.test( str, 0, &sb, L"fish" ) )
|
2007-03-24 19:07:38 +00:00
|
|
|
{
|
2012-01-23 05:40:08 +00:00
|
|
|
parser.eval( str, io, TOP );
|
2007-03-24 19:07:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fwprintf( stderr, L"%ls", sb.buff );
|
|
|
|
res = 1;
|
|
|
|
}
|
|
|
|
sb_destroy( &sb );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
free( str );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-10-19 12:07:44 +00:00
|
|
|
if( acc_used > 1 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
debug( 1,
|
2006-01-30 19:53:10 +00:00
|
|
|
_( L"Could not convert input. Read %d bytes." ),
|
2005-10-19 12:07:44 +00:00
|
|
|
acc_used-1 );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-01-30 19:53:10 +00:00
|
|
|
debug( 1,
|
2006-01-04 12:51:02 +00:00
|
|
|
_( L"Could not read input stream" ) );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
res=1;
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-01-30 19:53:10 +00:00
|
|
|
debug( 1,
|
2006-01-11 14:17:35 +00:00
|
|
|
_( L"Error while opening input stream" ) );
|
2005-09-20 13:26:39 +00:00
|
|
|
wperror( L"fdopen" );
|
|
|
|
free( buff );
|
|
|
|
res=1;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2007-04-25 18:30:02 +00:00
|
|
|
int reader_read( int fd, io_data_t *io )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
int res;
|
2006-04-05 12:48:25 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/*
|
2006-04-05 12:48:25 +00:00
|
|
|
If reader_read is called recursively through the '.' builtin, we
|
|
|
|
need to preserve is_interactive. This, and signal handler setup
|
|
|
|
is handled by proc_push_interactive/proc_pop_interactive.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2007-10-06 10:55:27 +00:00
|
|
|
int inter = ((fd == STDIN_FILENO) && isatty(STDIN_FILENO));
|
|
|
|
proc_push_interactive( inter );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-04-25 18:30:02 +00:00
|
|
|
res= is_interactive?read_i():read_ni( fd, io );
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/*
|
2005-10-19 12:07:44 +00:00
|
|
|
If the exit command was called in a script, only exit the
|
2006-04-05 12:48:25 +00:00
|
|
|
script, not the program.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2006-02-08 09:24:29 +00:00
|
|
|
if( data )
|
|
|
|
data->end_loop = 0;
|
2005-09-20 13:26:39 +00:00
|
|
|
end_loop = 0;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-02-16 13:36:32 +00:00
|
|
|
proc_pop_interactive();
|
2005-09-20 13:26:39 +00:00
|
|
|
return res;
|
|
|
|
}
|