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

expand.c File Reference

String expansion functions. More...

#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <string.h>
#include <wctype.h>
#include <errno.h>
#include <pwd.h>
#include <unistd.h>
#include <sys/types.h>
#include <termios.h>
#include <dirent.h>
#include <sys/stat.h>
#include <signal.h>
#include <assert.h>
#include "fallback.h"
#include "util.h"
#include "common.h"
#include "wutil.h"
#include "env.h"
#include "proc.h"
#include "parser.h"
#include "expand.h"
#include "wildcard.h"
#include "exec.h"
#include "tokenizer.h"
#include "complete.h"
#include "parse_util.h"
#include "halloc.h"
#include "halloc_util.h"

Defines

#define COMPLETE_VAR_DESC   _( L"The '$' character begins a variable name. The character '%lc', which directly followed a '$', is not allowed as a part of a variable name, and variable names may not be zero characters long. To learn more about variable expansion in fish, type 'help expand-variable'.")
 Error issued on invalid variable name.
#define COMPLETE_VAR_NULL_DESC   _( L"The '$' begins a variable name. It was given at the end of an argument. Variable names may not be zero characters long. To learn more about variable expansion in fish, type 'help expand-variable'.")
 Error issued on invalid variable name.
#define COMPLETE_VAR_BRACKET_DESC   _( L"Did you mean %ls{$%ls}%ls? The '$' character begins a variable name. A bracket, which directly followed a '$', is not allowed as a part of a variable name, and variable names may not be zero characters long. To learn more about variable expansion in fish, type 'help expand-variable'." )
 Error issued on invalid variable name.
#define COMPLETE_VAR_PARAN_DESC   _( L"Did you mean (COMMAND)? In fish, the '$' character is only used for accessing variables. To learn more about command substitution in fish, type 'help expand-command-substitution'.")
 Error issued on invalid variable name.
#define COMPLETE_CHILD_PROCESS_DESC   _( L"Child process")
 Description for child process.
#define COMPLETE_PROCESS_DESC   _( L"Process")
 Description for non-child process.
#define COMPLETE_JOB_DESC   _( L"Job")
 Description for long job.
#define COMPLETE_JOB_DESC_VAL   _( L"Job: %ls")
 Description for short job.
#define COMPLETE_SELF_DESC   _( L"Shell process")
 Description for the shells own pid.
#define COMPLETE_LAST_DESC   _( L"Last background job")
 Description for the shells own pid.
#define SELF_STR   L"self"
 String in process expansion denoting ourself.
#define LAST_STR   L"last"
 String in process expansion denoting last background job.
#define UNCLEAN_FIRST   L"~%"
 Characters which make a string unclean if they are the first character of the string.
#define UNCLEAN   L"$*?\\\"'({})"
 Unclean characters.

Functions

int expand_is_clean (const wchar_t *in)
 Test if the specified argument is clean, i.e.
static wchar_t * expand_var (wchar_t *in)
 Return the environment variable value for the string starting at in.
static int is_quotable (wchar_t *str)
 Test if the specified string does not contain character which can not be used inside a quoted string.
wchar_t * expand_escape_variable (const wchar_t *in)
 Convert the variable value to a human readable form, i.e.
static int iswnumeric (const wchar_t *n)
 Tests if all characters in the wide string are numeric.
static int match_pid (const wchar_t *cmd, const wchar_t *proc, int flags, int *offset)
 See if the process described by proc matches the commandline cmd.
static int find_process (const wchar_t *proc, int flags, array_list_t *out)
 Searches for a job with the specified job id, or a job or process which has the string proc as a prefix of its commandline.
static int expand_pid (wchar_t *in, int flags, array_list_t *out)
 Process id expansion.
void expand_variable_error (const wchar_t *token, int token_pos, int error_pos)
 Perform error reporting for a syntax error related to the variable expansion beginning at the specified character of the specified token.
static int parse_slice (wchar_t *in, wchar_t **end_ptr, array_list_t *idx)
static int expand_variables (wchar_t *in, array_list_t *out, int last_idx)
 Expand all environment variables in the string *ptr.
static int expand_brackets (wchar_t *in, int flags, array_list_t *out)
 Perform bracket expansion.
static int expand_cmdsubst (wchar_t *in, array_list_t *out)
 Perform cmdsubst expansion.
static wchar_t * expand_unescape (const wchar_t *in, int escape_special)
 Wrapper around unescape funtion.
static wchar_t * expand_tilde_internal (wchar_t *in)
 Attempts tilde expansion.
wchar_t * expand_tilde (wchar_t *in)
 Perform tilde expansion and nothing else on the specified string.
static void remove_internal_separator (const void *s, int conv)
 Remove any internal separators.
int expand_string (void *context, wchar_t *str, array_list_t *end_out, int flags)
 Perform various forms of expansion on in, such as tilde expansion (.
wchar_t * expand_one (void *context, wchar_t *string, int flags)
 expand_one is identical to expand_string, except it will fail if in expands to more than one string.


Detailed Description

String expansion functions.

These functions perform several kinds of parameter expansion.


Define Documentation

#define COMPLETE_JOB_DESC_VAL   _( L"Job: %ls")
 

Description for short job.

The job command is concatenated

#define UNCLEAN   L"$*?\\\"'({})"
 

Unclean characters.

See is_clean().

#define UNCLEAN_FIRST   L"~%"
 

Characters which make a string unclean if they are the first character of the string.

See is_clean().


Function Documentation

wchar_t* expand_escape_variable const wchar_t *  in  ) 
 

Convert the variable value to a human readable form, i.e.

escape things, handle arrays, etc. Suitable for pretty-printing.

Parameters:
in the value to escape

int expand_is_clean const wchar_t *  in  ) 
 

Test if the specified argument is clean, i.e.

it does not contain any tokens which need to be expanded or otherwise altered. Clean strings can be passed through expand_string and expand_one without changing them. About two thirds of all strings are clean, so skipping expansion on them actually does save a small amount of time, since it avoids multiple memory allocations during the expansion process.

Parameters:
in the string to test

wchar_t* expand_one void *  context,
wchar_t *  in,
int  flag
 

expand_one is identical to expand_string, except it will fail if in expands to more than one string.

This is used for expanding command names.

If context is non-null, the returning string ill be registered to be free'd when context is free'd.

Parameters:
context the halloc context to use for automatic deallocation
in The parameter to expand
flag Specifies if any expansion pass should be skipped. Legal values are any combination of EXPAND_SKIP_CMDSUBST EXPAND_SKIP_VARIABLES and EXPAND_SKIP_WILDCARDS
Returns:
The expanded parameter, or 0 on failiure

int expand_string void *  context,
wchar_t *  str,
array_list_t end_out,
int  flags
 

Perform various forms of expansion on in, such as tilde expansion (.

expand_one is just a wrapper around this one.

wchar_t* expand_tilde wchar_t *  in  ) 
 

Perform tilde expansion and nothing else on the specified string.

If tilde expansion is needed, the original string is freed and a new string, allocated using malloc, is returned.

Parameters:
in the string to tilde expand

static wchar_t* expand_tilde_internal wchar_t *  in  )  [static]
 

Attempts tilde expansion.

Of the string specified. If tilde expansion is performed, the original string is freed and a new string allocated using malloc is returned, otherwise, the original string is returned.

static wchar_t* expand_unescape const wchar_t *  in,
int  escape_special
[static]
 

Wrapper around unescape funtion.

Issues an error() on failiure.

void expand_variable_error const wchar_t *  token,
int  token_pos,
int  error_pos
 

Perform error reporting for a syntax error related to the variable expansion beginning at the specified character of the specified token.

This function will call the error function with an explanatory string about what is wrong with the specified token.

Parameters:
token The token containing the error
token_pos The position where the expansion begins
error_pos The position on the line to report to the error function.

static int expand_variables wchar_t *  in,
array_list_t out,
int  last_idx
[static]
 

Expand all environment variables in the string *ptr.

This function is slow, fragile and complicated. There are lots of little corner cases, like $$foo should do a double expansion, $foo$bar should not double expand bar, etc. Also, it's easy to accidentally leak memory on array out of bounds errors an various other situations. All in all, this function should be rewritten, split out into multiple logical units and carefully tested. After that, it can probably be optimized to do fewer memory allocations, fewer string scans and overall just less work. But until that happens, don't edit it unless you know exactly what you are doing, and do proper testing afterwards.

static int find_process const wchar_t *  proc,
int  flags,
array_list_t out
[static]
 

Searches for a job with the specified job id, or a job or process which has the string proc as a prefix of its commandline.

If the ACCEPT_INCOMPLETE flag is set, the remaining string for any matches are inserted.

Otherwise, any job matching the specified string is matched, and the job pgid is returned. If no job matches, all child processes are searched. If no child processes match, and fish can understand the contents of the /proc filesystem, all the users processes are searched for matches.

static void remove_internal_separator const void *  s,
int  conv
[static]
 

Remove any internal separators.

Also optionally convert wildcard characters to regular equivalents. This is done to support EXPAND_SKIP_WILDCARDS.


Generated on Sun Jan 13 02:52:45 2008 for fish by  doxygen 1.4.4