rush
Revamped UNIX Shell
 All Data Structures Files Functions Variables Enumerations Enumerator Macros
Data Structures | Macros | Enumerations | Functions
shellutils.h File Reference

Helper functions for shell implementations. More...

#include <limits.h>
#include <stdbool.h>
#include <sys/types.h>

Go to the source code of this file.

Data Structures

struct  ProcInfo
 Process-information structure. More...
struct  ShCommand
 The ShCommand structure contains a parsed command. More...

Macros

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

Enumerations

enum  ProcState { PS_RUNNING, PS_STOPPED, PS_TERMINATED }
 Process state. More...

Functions

void shPrompt (void)
 Prints a prompt symbol including the shell's current working directory.
ShCommandshParseCmdLine (char cmdLine[])
 Parses a command line.
int shPrintProcState (const ProcInfo *info)
 Prints a textual description of a process's current state to stderr.

Detailed Description

Helper functions for shell implementations.

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

Enumeration Type Documentation

enum ProcState

Process state.

Enumerator:
PS_RUNNING 

The process is running.

PS_STOPPED 

The process is currently in a stopped state.

PS_TERMINATED 

The process has been terminated.

Function Documentation

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.
int shPrintProcState ( const ProcInfo info)

Prints a textual description of a process's current state to stderr.

For instance, the output can look like this:

[20122] Running "ls -l"

or like this:

[20131] Stopped "sleep 60"
Parameters
infoProcess-information structure containing the process ID, its current state and its command line.
Returns
Always 0.
Note
This function should not be used in a signal handler since it uses a potentially locking output mechanism to write to stderr.
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.