[ Previous | Next | Contents | Glossary | Home | Search ]
AIX Version 4.3 Base Operating System and Extensions Technical Reference, Volume 2

wordexp Subroutine

Purpose

Expands tokens from a stream of words.

Library

Standard C Library (libc.a)

Syntax

#include <wordexp.h>
int wordexp (WordsPwordexpFlags)
const char *Words;
wordexp_t *Pwordexp;
int Flags;

Description

The wordexp subroutine performs word expansions equivalent to the word expansion that would be performed by the shell if the contents of the Words parameter were arguments on the command line. The list of expanded words are placed in the Pwordexp parameter. The expansions are the same as that which would be performed by the shell if the Words parameter were the part of a command line representing the parameters to a command. Therefore, the Words parameter cannot contain an unquoted <newline> character or any of the unquoted shell special characters | (pipe), & (ampersand), ; (semicolon), < (less than sign), or > (greater than sign), except in the case of command substitution. The Words parameter also cannot contain unquoted parentheses or braces, except in the case of command or variable substitution. If the Words parameter contains an unquoted comment character # (number sign) that is the beginning of a token, the wordexp subroutine may treat the comment character as a regular character, or may interpret it as a comment indicator and ignore the remainder of the expression in the Words parameter.

The wordexp subroutine allows an application to perform all of the shell's expansions on a word or words obtained from a user. For example, if the application prompts for a file name (or a list of file names) and then uses the wordexp subroutine to process the input, the user could respond with anything that would be valid as input to the shell.

The wordexp subroutine stores the number of generated words and a pointer to a list of pointers to words in the Pwordexp parameter. Each individual field created during the field splitting or path name expansion is a separate word in the list specified by the Pwordexp parameter. The first pointer after the last last token in the list is a null pointer. The expansion of special parameters * (asterisk), @ (at sign), # (number sign), ? (question mark), - (minus sign), $ (dollar sign), ! (exclamation point), and 0 is unspecified.

The words are expanded in the order shown below:

  1. Tilde expansion is performed first.
  2. Parameter expansion, command substitution, and arithmetic expansion are performed next, from beginning to end.
  3. Field splitting is then performed on fields generated by step 2, unless the IFS (input field separators) is full.
  4. Path-name expansion is performed, unless the set -f command is in effect.
  5. Quote removal is always performed last.

Parameters

Flags Contains a bit flag specifying the configurable aspects of the wordexp subroutine.
Pwordexp Contains a pointer to a wordexp_t structure.
Words Specifies the string containing the tokens to be expanded.

The value of the Flags parameter is the bitwise, inclusive OR of the constants below, which are defined in the wordexp.h file.

WRDE_APPEND Appends words generated to those generated by a previous call to the wordexp subroutine.
WRDE_DOOFFS Makes use of the we_offs structure. If the WRDE_DOOFFS flag is set, the we_offs structure is used to specify the number of null pointers to add to the beginning of the we_words structure. If the WRDE_DOOFFS flag is not set in the first call to the wordexp subroutine with the Pwordexp parameter, it should not be set in subsequent calls to the wordexp subroutine with the Pwordexp parameter.
WRDE_NOCMD Fails if command substitution is requested.
WRDE_REUSE The Pwordexp parameter was passed to a previous successful call to the wordexp subroutine. Therefore, the memory previously allocated may be reused.
WRDE_SHOWERR Does not redirect standard error to /dev/null.
WRDE_UNDEF Reports error on an attempt to expand an undefined shell variable.

The WRDE_ APPEND flag can be used to append a new set of words to those generated by a previous call to the wordexp subroutine. The following rules apply when two or more calls to the wordexp subroutine are made with the same value of the Pwordexp parameter and without intervening calls to the wordfree subroutine:

  1. The first such call does not set the WRDE_ APPEND flag. All subsequent calls set it.
  2. For a single invocation of the wordexp subroutine, all calls either set the WRDE_DOOFFS flag, or do not set it.
  3. After the second and each subsequent call, the Pwordexp parameter points to a list containing the following:
    1. Zero or more null characters, as specified by the WRDE_DOOFFS flag and the we_offs structure.
    2. Pointers to the words that were in the Pwordexp parameter before the call, in the same order as before.
    3. Pointers to the new words generated by the latest call, in the specified order.
  4. The count returned in the Pwordexp parameter is the total number of words from all of the calls.
  5. The application should not modify the Pwordexp parameter between the calls.

The WRDE_NOCMD flag is provided for applications that, for security or other reasons, want to prevent a user from executing shell commands. Disallowing unquoted shell special characters also prevents unwanted side effects such as executing a command or writing to a file.

Unless the WRDE_SHOWERR flag is set in the Flags parameter, the wordexp subroutine redirects standard error to the /dev/null file for any utilities executed as a result of command substitution while expanding the Words parameter. If the WRDE_SHOWERR flag is set, the wordexp subroutine may write messages to standard error if syntax errors are detected while expanding the Words parameter.

The Pwordexp structure is allocated by the caller, but memory to contain the expanded tokens is allocated by the wordexp subroutine and added to the structure as needed.

The Words parameter cannot contain any <newline> characters, or any of the unquoted shell special characters |, &, ;, (), {}, <, or >, except in the context of command substitution.

Return Values

If no errors are encountered while expanding the Words parameter, the wordexp subroutine returns a value of 0. If an error occurs, it returns a nonzero value indicating the error.

Errors

If the wordexp subroutine terminates due to an error, it returns one of the nonzero constants below, which are defined in the wordexp.h file.

WRDE_BADCHAR One of the unquoted characters |, &, ;, <, >, parenthesis, or braces appears in the Words parameter in an inappropriate context.
WRDE_BADVAL Reference to undefined shell variable when the WRDE_UNDEF flag is set in the Flags parameter.
WRDE_CMDSUB Command substitution requested when the WRDE_NOCMD flag is set in the Flags parameter.
WRDE_NOSPACE Attempt to allocate memory was unsuccessful.
WRDE_SYNTAX Shell syntax error, such as unbalanced parentheses or unterminated string.

If the wordexp subroutine returns the error value WRDE_SPACE, then the expression in the Pwordexp parameter is updated to reflect any words that were successfully expanded. In other cases, the Pwordexp parameter is not modified.

Implementation Specifics

This subroutine is part of Base Operating System (BOS) Runtime.

Related Information

The glob subroutine, wordfree subroutine.

For more information on basic and extended regular expressions, see Manipulating Strings with sed.


[ Previous | Next | Contents | Glossary | Home | Search ]