2006-01-30 16:51:50 +00:00
|
|
|
/** \file parse_util.h
|
|
|
|
|
2006-02-19 01:54:38 +00:00
|
|
|
Various mostly unrelated utility functions related to parsing,
|
|
|
|
loading and evaluating fish code.
|
2006-01-30 16:51:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FISH_PARSE_UTIL_H
|
|
|
|
#define FISH_PARSE_UTIL_H
|
|
|
|
|
2012-01-25 19:47:45 +00:00
|
|
|
#include "autoload.h"
|
2013-12-16 00:05:37 +00:00
|
|
|
#include "parse_tree.h"
|
2006-01-30 16:51:50 +00:00
|
|
|
#include <wchar.h>
|
2012-01-23 19:42:41 +00:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
/**
|
2006-02-19 01:54:38 +00:00
|
|
|
Find the beginning and end of the first subshell in the specified string.
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
\param in the string to search for subshells
|
|
|
|
\param begin the starting paranthesis of the subshell
|
|
|
|
\param end the ending paranthesis of the subshell
|
2013-07-17 08:35:30 +00:00
|
|
|
\param accept_incomplete whether to permit missing closing parenthesis
|
2006-01-30 16:51:50 +00:00
|
|
|
\return -1 on syntax error, 0 if no subshells exist and 1 on sucess
|
|
|
|
*/
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
int parse_util_locate_cmdsubst(const wchar_t *in,
|
|
|
|
wchar_t **begin,
|
|
|
|
wchar_t **end,
|
2013-07-17 08:35:30 +00:00
|
|
|
bool accept_incomplete);
|
2006-01-30 16:51:50 +00:00
|
|
|
|
2014-02-03 22:13:42 +00:00
|
|
|
/** Same as parse_util_locate_cmdsubst, but handles square brackets [ ] */
|
|
|
|
int parse_util_locate_slice(const wchar_t *in,
|
|
|
|
wchar_t **begin,
|
|
|
|
wchar_t **end,
|
|
|
|
bool accept_incomplete);
|
|
|
|
|
2013-10-09 01:41:35 +00:00
|
|
|
/**
|
|
|
|
Alternative API. Iterate over command substitutions.
|
|
|
|
|
|
|
|
\param str the string to search for subshells
|
|
|
|
\param inout_cursor_offset On input, the location to begin the search. On output, either the end of the string, or just after the closed-paren.
|
|
|
|
\param out_contents On output, the contents of the command substitution
|
|
|
|
\param out_start On output, the offset of the start of the command substitution (open paren)
|
|
|
|
\param out_end On output, the offset of the end of the command substitution (close paren), or the end of the string if it was incomplete
|
|
|
|
\param accept_incomplete whether to permit missing closing parenthesis
|
|
|
|
\return -1 on syntax error, 0 if no subshells exist and 1 on sucess
|
|
|
|
*/
|
|
|
|
|
|
|
|
int parse_util_locate_cmdsubst_range(const wcstring &str,
|
2014-01-15 09:40:40 +00:00
|
|
|
size_t *inout_cursor_offset,
|
|
|
|
wcstring *out_contents,
|
|
|
|
size_t *out_start,
|
|
|
|
size_t *out_end,
|
|
|
|
bool accept_incomplete);
|
2013-10-09 01:41:35 +00:00
|
|
|
|
2006-02-08 18:47:37 +00:00
|
|
|
/**
|
2006-07-03 10:46:47 +00:00
|
|
|
Find the beginning and end of the command substitution under the
|
|
|
|
cursor. If no subshell is found, the entire string is returned. If
|
|
|
|
the current command substitution is not ended, i.e. the closing
|
|
|
|
parenthesis is missing, then the string from the beginning of the
|
|
|
|
substitution to the end of the string is returned.
|
2006-02-19 01:54:38 +00:00
|
|
|
|
|
|
|
\param buff the string to search for subshells
|
|
|
|
\param cursor_pos the position of the cursor
|
|
|
|
\param a the start of the searched string
|
|
|
|
\param b the end of the searched string
|
2006-02-08 18:47:37 +00:00
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
void parse_util_cmdsubst_extent(const wchar_t *buff,
|
|
|
|
size_t cursor_pos,
|
|
|
|
const wchar_t **a,
|
|
|
|
const wchar_t **b);
|
2006-01-30 16:51:50 +00:00
|
|
|
|
2006-02-08 18:47:37 +00:00
|
|
|
/**
|
|
|
|
Find the beginning and end of the process definition under the cursor
|
2006-02-19 01:54:38 +00:00
|
|
|
|
|
|
|
\param buff the string to search for subshells
|
|
|
|
\param cursor_pos the position of the cursor
|
|
|
|
\param a the start of the searched string
|
|
|
|
\param b the end of the searched string
|
2006-02-08 18:47:37 +00:00
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
void parse_util_process_extent(const wchar_t *buff,
|
|
|
|
size_t cursor_pos,
|
|
|
|
const wchar_t **a,
|
|
|
|
const wchar_t **b);
|
2006-01-30 16:51:50 +00:00
|
|
|
|
|
|
|
|
2006-02-08 18:47:37 +00:00
|
|
|
/**
|
|
|
|
Find the beginning and end of the job definition under the cursor
|
2006-02-19 01:54:38 +00:00
|
|
|
|
|
|
|
\param buff the string to search for subshells
|
|
|
|
\param cursor_pos the position of the cursor
|
|
|
|
\param a the start of the searched string
|
|
|
|
\param b the end of the searched string
|
2006-02-08 18:47:37 +00:00
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
void parse_util_job_extent(const wchar_t *buff,
|
|
|
|
size_t cursor_pos,
|
|
|
|
const wchar_t **a,
|
|
|
|
const wchar_t **b);
|
2006-01-30 16:51:50 +00:00
|
|
|
|
2006-02-08 18:47:37 +00:00
|
|
|
/**
|
2006-06-20 00:50:10 +00:00
|
|
|
Find the beginning and end of the token under the cursor and the
|
2012-02-06 08:57:43 +00:00
|
|
|
token before the current token. Any combination of tok_begin,
|
2006-06-20 00:50:10 +00:00
|
|
|
tok_end, prev_begin and prev_end may be null.
|
2006-02-19 01:54:38 +00:00
|
|
|
|
|
|
|
\param buff the string to search for subshells
|
|
|
|
\param cursor_pos the position of the cursor
|
2006-06-20 00:50:10 +00:00
|
|
|
\param tok_begin the start of the current token
|
|
|
|
\param tok_end the end of the current token
|
|
|
|
\param prev_begin the start o the token before the current token
|
|
|
|
\param prev_end the end of the token before the current token
|
2006-02-08 18:47:37 +00:00
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
void parse_util_token_extent(const wchar_t *buff,
|
|
|
|
size_t cursor_pos,
|
|
|
|
const wchar_t **tok_begin,
|
|
|
|
const wchar_t **tok_end,
|
|
|
|
const wchar_t **prev_begin,
|
|
|
|
const wchar_t **prev_end);
|
2006-01-30 16:51:50 +00:00
|
|
|
|
2006-02-08 18:47:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Get the linenumber at the specified character offset
|
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
int parse_util_lineno(const wchar_t *str, size_t len);
|
2006-02-05 13:10:35 +00:00
|
|
|
|
2007-09-21 14:05:49 +00:00
|
|
|
/**
|
|
|
|
Calculate the line number of the specified cursor position
|
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
int parse_util_get_line_from_offset(const wcstring &str, size_t pos);
|
2007-09-21 14:05:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Get the offset of the first character on the specified line
|
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
size_t parse_util_get_offset_from_line(const wcstring &str, int line);
|
2007-09-21 14:05:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Return the total offset of the buffer for the cursor position nearest to the specified poition
|
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
size_t parse_util_get_offset(const wcstring &str, int line, long line_offset);
|
2007-09-21 14:05:49 +00:00
|
|
|
|
2006-02-15 02:24:15 +00:00
|
|
|
/**
|
|
|
|
Set the argv environment variable to the specified null-terminated
|
2012-11-18 10:23:22 +00:00
|
|
|
array of strings.
|
2006-02-15 02:24:15 +00:00
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
void parse_util_set_argv(const wchar_t * const *argv, const wcstring_list_t &named_arguments);
|
2006-02-15 02:24:15 +00:00
|
|
|
|
2006-02-19 01:14:32 +00:00
|
|
|
/**
|
2006-02-19 01:54:38 +00:00
|
|
|
Make a duplicate of the specified string, unescape wildcard
|
|
|
|
characters but not performing any other character transformation.
|
2006-02-19 01:14:32 +00:00
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
wchar_t *parse_util_unescape_wildcards(const wchar_t *in);
|
2006-02-19 01:14:32 +00:00
|
|
|
|
2014-03-18 21:42:38 +00:00
|
|
|
/**
|
|
|
|
Checks if the specified string is a help option.
|
|
|
|
|
|
|
|
\param s the string to test
|
|
|
|
\param min_match is the minimum number of characters that must match in a long style option, i.e. the longest common prefix between --help and any other option. If less than 3, 3 will be assumed.
|
|
|
|
*/
|
|
|
|
bool parse_util_argument_is_help(const wchar_t *s, int min_match);
|
|
|
|
|
|
|
|
|
2012-07-06 21:34:53 +00:00
|
|
|
/**
|
|
|
|
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 NULL, store the type of quote this parameter has, can be either ', " or \\0, meaning the string is not quoted.
|
|
|
|
\param offset If not NULL, get_param will store the offset to the beginning of the parameter.
|
|
|
|
\param type If not NULL, get_param will store the token type as returned by tok_last.
|
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
void parse_util_get_parameter_info(const wcstring &cmd, const size_t pos, wchar_t *quote, size_t *offset, int *type);
|
2012-07-06 21:34:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Attempts to escape the string 'cmd' using the given quote type, as determined by the quote character. The quote can be a single quote or double quote, or L'\0' to indicate no quoting (and thus escaping should be with backslashes).
|
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring parse_util_escape_string_with_quote(const wcstring &cmd, wchar_t quote);
|
2007-04-22 09:50:26 +00:00
|
|
|
|
2013-12-08 21:41:12 +00:00
|
|
|
/** Given a string, parse it as fish code and then return the indents. The return value has the same size as the string */
|
|
|
|
std::vector<int> parse_util_compute_indents(const wcstring &src);
|
2007-04-22 09:50:26 +00:00
|
|
|
|
2013-12-17 01:18:32 +00:00
|
|
|
parser_test_error_bits_t parse_util_detect_errors(const wcstring &buff_src, parse_error_list_t *out_errors = NULL);
|
2013-12-16 00:05:37 +00:00
|
|
|
|
2014-03-18 15:51:23 +00:00
|
|
|
/**
|
|
|
|
Test if this argument contains any errors. Detected errors include syntax errors in command substitutions, improperly escaped characters and improper use of the variable expansion operator.
|
|
|
|
|
|
|
|
This does NOT currently detect unterminated quotes.
|
|
|
|
*/
|
2014-03-04 10:53:34 +00:00
|
|
|
parser_test_error_bits_t parse_util_detect_errors_in_argument(const parse_node_t &node, const wcstring &arg_src, parse_error_list_t *out_errors = NULL);
|
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
#endif
|