rush
Revamped UNIX Shell
Data Structures | Macros | Functions
shellutils.h File Reference

Helper functions for shell implementations. More...

#include <limits.h>
#include <stdbool.h>

Go to the source code of this file.

Data Structures

struct  ShCommand
 The ShCommand structure contains a parsed command. More...
 

Macros

#define MAX_CMDLINE_LEN   1048576
 Maximum length of the command line (including '\0').
 

Functions

void shPrompt (void)
 Prints a prompt symbol including the shell's current working directory. More...
 
ShCommandshParseCmdLine (char cmdLine[])
 Parses a command line. More...
 

Detailed Description

Helper functions for shell implementations.

This module offers the following functionality to facilitate implementing a simple UNIX shell:

Function Documentation

◆ shParseCmdLine()

ShCommand * shParseCmdLine ( char  cmdLine[])

Parses a command line.

This function parses a command-line string that may contain '&' (for background execution) as well as '<' and '>' for stdin and stdout redirection, respectively. shParseCmdLine() will generate the argv array for the given command line and return an ShCommand structure containing the result. The members of the argv array will point into the original string, whose contents are modified for this purpose.

The returned ShCommand structure lies in statically allocated memory and is overwritten by subsequent calls to shParseCmdLine().

Parameters
cmdLineThe command line to be parsed. This string is tokenized during shParseCmdLine(). It must be no more than MAX_CMDLINE_LEN characters long (including the terminating '\0'), otherwise NULL is returned and errno is set to EINVAL.
Returns
Pointer to the parsed ShCommand or NULL on error, with errno set appropriately. In case a custom error message is supplied, the function returns an ShCommand structure that has its parseError field set to a custom error message. If the parseError field is set to a value other than NULL, parsing the command line has failed and the remaining fields of the ShCommand structure must not be interpreted.
Note
This function does not interfere with any other functions, but it is not reentrant since it uses and reuses statically allocated memory.

◆ shPrompt()

void shPrompt ( void  )

Prints a prompt symbol including the shell's current working directory.

Note
This function should not be used in a signal handler since it uses a potentially locking output mechanism to write to stderr.