Nut/OS  4.10.3
API Reference
EdLine Editor

Line Editor. More...

Collaboration diagram for EdLine Editor:

Data Structures

struct  _EDITHISTORY
 Line input history. More...
struct  _EDLINE
 Edit line information structure. More...

Defines

#define EDIT_MAX_HISTORY   16
 Maximum number of history entries.

Typedefs

typedef struct _EDITHISTORY EDITHISTORY
 Line input history.
typedef int(* EDLINEGET )(void *)
 Input routine type.
typedef int(* EDLINEPUT )(void *, int)
 Output routine type.
typedef int(* EDLINEMAP )(int, int *)
 Character mapping routine type.
typedef struct _EDLINE EDLINE
 Edit line information structure.

Functions

void EdLineRegisterInput (EDLINE *el, EDLINEGET get, void *param)
 Register an input routine.
void EdLineRegisterOutput (EDLINE *el, EDLINEPUT put, void *param)
 Register an output routine.
int EdLineKeyMap (int key, int *seq)
 Default key mapping routine.
void EdLineRegisterKeymap (EDLINE *el, EDLINEMAP map)
 Register a key mapping routine.
EDLINEEdLineOpen (uint16_t mode)
 Open a line editor.
void EdLineClose (EDLINE *el)
 Close a line editor.
int EdLineRead (EDLINE *el, char *buf, int siz)
 Read a line.
EDITHISTORYEditHistoryCreate (int siz)
 Create an editor history table.
void EditHistoryDestroy (EDITHISTORY *hist)
 Create an editor history table.
void EditHistorySet (EDITHISTORY *hist, int idx, char *buf)
 Replace the specified history table entry.
int EditHistoryGet (EDITHISTORY *hist, int idx, char *buf, int siz)
 Retrieve the specified history table entry.
void EditHistoryInsert (EDITHISTORY *hist, int idx, char *buf)
 Insert a new history table entry at the specified position.
int EdLineKeyMapVt100 (int key, int *seq)
 Optional VT100 key mapping routine.

Edit mode flags

Used when calling EdLineOpen to enable specific line editor features.

#define EDIT_MODE_ECHO   0x0001
 Enables echoing of input characters.
#define EDIT_MODE_BINARY   0x0002
 Enables binary mode (currently unused).
#define EDIT_MODE_HISTORY   0x0004
 Enables input line history.

Line editor input commands

Special keys which are interpreted by the editor as commands.

#define EDIT_KEY_IGNORE   0x00
 No operation. Default is Ctrl-@.
#define EDIT_KEY_HOME   0x01
 Move cursor to line begin. Default is Ctrl-A.
#define EDIT_KEY_LEFT   0x02
 Move cursor left. Default is Ctrl-B.
#define EDIT_KEY_END   0x05
 Move cursor to line end. Default is Ctrl-E.
#define EDIT_KEY_RIGHT   0x06
 Move cursor right. Default is Ctrl-F.
#define EDIT_KEY_REMOVE   0x08
 Delete left character. Default is Ctrl-H.
#define EDIT_KEY_ENTER   0x0a
 Confirm input. Default is Ctrl-J.
#define EDIT_KEY_UP   0x12
 History upwards. Default is Ctrl-R.
#define EDIT_KEY_DOWN   0x16
 History downwards. Default is Ctrl-V.
#define EDIT_KEY_RESTORE   0x1b
 Restore default. Default is ESC.

Line editor output commands

Special characters used by the editor to update the output.

#define EDIT_CHAR_SPACE   ' '
 Clear character right.
#define EDIT_CHAR_BACKSPACE   '\b'
 Move cursor left.
#define EDIT_CHAR_ALARM   '\a'
 Invoke audible alarm.
#define EDIT_STR_EOL   "\r\n"
 Terminate input.

Detailed Description

Line Editor.


Define Documentation

#define EDIT_MODE_ECHO   0x0001

Enables echoing of input characters.

Examples:
caltime/caltime.c.

Definition at line 63 of file edline.h.

Referenced by EdLineRead(), LuaThread(), and main().

#define EDIT_MODE_BINARY   0x0002

Enables binary mode (currently unused).

Definition at line 65 of file edline.h.

#define EDIT_MODE_HISTORY   0x0004

Enables input line history.

Definition at line 67 of file edline.h.

Referenced by EdLineOpen(), EdLineRead(), and LuaThread().

#define EDIT_KEY_IGNORE   0x00

No operation. Default is Ctrl-@.

Definition at line 78 of file edline.h.

Referenced by EdLineKeyMap(), and EdLineKeyMapVt100().

#define EDIT_KEY_HOME   0x01

Move cursor to line begin. Default is Ctrl-A.

Definition at line 82 of file edline.h.

Referenced by EdLineRead().

#define EDIT_KEY_LEFT   0x02

Move cursor left. Default is Ctrl-B.

Definition at line 86 of file edline.h.

Referenced by EdLineKeyMapVt100(), and EdLineRead().

#define EDIT_KEY_END   0x05

Move cursor to line end. Default is Ctrl-E.

Definition at line 90 of file edline.h.

Referenced by EdLineRead().

#define EDIT_KEY_RIGHT   0x06

Move cursor right. Default is Ctrl-F.

Definition at line 94 of file edline.h.

Referenced by EdLineKeyMapVt100(), and EdLineRead().

#define EDIT_KEY_REMOVE   0x08

Delete left character. Default is Ctrl-H.

Definition at line 98 of file edline.h.

Referenced by EdLineRead().

#define EDIT_KEY_ENTER   0x0a

Confirm input. Default is Ctrl-J.

Definition at line 102 of file edline.h.

Referenced by EdLineKeyMap(), and EdLineRead().

#define EDIT_KEY_UP   0x12

History upwards. Default is Ctrl-R.

Definition at line 107 of file edline.h.

Referenced by EdLineKeyMapVt100(), and EdLineRead().

#define EDIT_KEY_DOWN   0x16

History downwards. Default is Ctrl-V.

Definition at line 111 of file edline.h.

Referenced by EdLineKeyMapVt100(), and EdLineRead().

#define EDIT_KEY_RESTORE   0x1b

Restore default. Default is ESC.

Definition at line 115 of file edline.h.

Referenced by EdLineRead().

#define EDIT_CHAR_SPACE   ' '

Clear character right.

Definition at line 128 of file edline.h.

Referenced by EdLineRead().

#define EDIT_CHAR_BACKSPACE   '\b'

Move cursor left.

Definition at line 132 of file edline.h.

Referenced by EdLineRead().

#define EDIT_CHAR_ALARM   '\a'

Invoke audible alarm.

Definition at line 136 of file edline.h.

Referenced by EdLineRead().

#define EDIT_STR_EOL   "\r\n"

Terminate input.

Definition at line 140 of file edline.h.

Referenced by EdLineRead().

#define EDIT_MAX_HISTORY   16

Maximum number of history entries.

The first entry with index 0 is used to restore the default value.

Definition at line 61 of file edline.c.

Referenced by EdLineOpen().


Typedef Documentation

typedef struct _EDITHISTORY EDITHISTORY

Line input history.

typedef int(* EDLINEGET)(void *)

Input routine type.

Definition at line 155 of file edline.h.

typedef int(* EDLINEPUT)(void *, int)

Output routine type.

Definition at line 157 of file edline.h.

typedef int(* EDLINEMAP)(int, int *)

Character mapping routine type.

Definition at line 159 of file edline.h.

typedef struct _EDLINE EDLINE

Edit line information structure.


Function Documentation

void EdLineRegisterInput ( EDLINE el,
EDLINEGET  get,
void *  param 
)

Register an input routine.

Applications may use this function to replace the default character input routine.

Parameters:
elPointer to an EDLINE structure, obtained by a previous call to EdLineOpen.
getPointer to the new input routine. If NULL, then the default routine is restored.
paramOptional parameter, which is passed to the input routine.

Definition at line 105 of file edline.c.

References _EDLINE::el_get, and _EDLINE::el_iparm.

Referenced by EdLineOpen().

void EdLineRegisterOutput ( EDLINE el,
EDLINEPUT  put,
void *  param 
)

Register an output routine.

Applications may use this function to replace the default character output routine.

Parameters:
elPointer to an EDLINE structure, obtained by a previous call to EdLineOpen.
putPointer to the new output routine. If NULL, then the default routine is restored.
paramOptional parameter, which is passed to the output routine.

Definition at line 128 of file edline.c.

References _EDLINE::el_oparm, and _EDLINE::el_put.

Referenced by EdLineOpen().

int EdLineKeyMap ( int  key,
int *  seq 
)

Default key mapping routine.

Replaces carriage returns and linefeeds by EDIT_KEY_ENTER. Linefeeds immediately following carriage returns are ignored. May be replaced by calling EdLineRegisterKeymap().

This routine may be called by other key mapping routines.

Parameters:
keyInput character to remap.
seqPointer to an integer, which is used by the key mapping routine to store the current state. Note, that its value must be preserved by the caller between calls to EdLineRead.
Returns:
The mapped character. If EDIT_KEY_IGNORE is returned the caller must ignore this character.

Definition at line 156 of file edline.c.

References EDIT_KEY_ENTER, and EDIT_KEY_IGNORE.

Referenced by EdLineKeyMapVt100(), and EdLineRegisterKeymap().

void EdLineRegisterKeymap ( EDLINE el,
EDLINEMAP  map 
)

Register a key mapping routine.

Parameters:
elPointer to an EDLINE structure, obtained by a previous call to EdLineOpen.
mapPointer to the new mapping routine. If NULL, then the default routine is restored.

Definition at line 183 of file edline.c.

References EdLineKeyMap(), and _EDLINE::el_map.

Referenced by EdLineOpen(), and LuaThread().

Here is the call graph for this function:

EDLINE* EdLineOpen ( uint16_t  mode)

Open a line editor.

Parameters:
modeMode flags, may contain any combination of
  • EDIT_MODE_ECHO enables echoing of input characters.
  • EDIT_MODE_BINARY enables binary mode (currently unused).
  • EDIT_MODE_HISTORY enables input line history.
Returns:
Pointer to a new EDLINE structure.

Definition at line 202 of file edline.c.

References calloc, EDIT_MAX_HISTORY, EDIT_MODE_HISTORY, EditHistoryCreate(), EdLineRegisterInput(), EdLineRegisterKeymap(), EdLineRegisterOutput(), _EDLINE::el_hist, _EDLINE::el_mode, stdin, and stdout.

Referenced by LuaThread(), and main().

Here is the call graph for this function:

void EdLineClose ( EDLINE el)

Close a line editor.

Releases all occupied memory.

Parameters:
elPointer to an EDLINE structure, obtained by a previous call to EdLineOpen.

Definition at line 229 of file edline.c.

References EditHistoryDestroy(), _EDLINE::el_hist, and free().

Referenced by LuaThread().

Here is the call graph for this function:

int EdLineRead ( EDLINE el,
char *  buf,
int  siz 
)

Read a line.

This functions offers some editing capabilities and is typically used for text input by human users.

Line editing can be done by entering any of the following control characters:

  • EDIT_KEY_RESTORE Restores initial default line (default ESC)
  • EDIT_KEY_REMOVE Deletes character on the left of the cursor (default CTRL-H)
  • EDIT_KEY_HOME Moves cursor to the beginning of the line (default Ctrl-A).
  • EDIT_KEY_END Moves cursor to the beginning of the line (default CTRL-E)
  • EDIT_KEY_LEFT Moves cursor one character to the left (default CTRL-B)
  • EDIT_KEY_RIGHT Moves cursor one character to the right (default CTRL-F)
  • EDIT_KEY_UP Walks up the list of previously entered lines (default CTRL-R)
  • EDIT_KEY_DOWN Walks down the list of previously entered lines (default CTRL-V)

Note, that these commands may be modified by the currently registered remapping routine.

Parameters:
elPointer to an EDLINE structure, obtained by a previous call to EdLineOpen.
bufPointer to the buffer that receives the text line. If it contains a string on entry, this will be used as the default value.
sizNumber of bytes available in the buffer. The maximum length of the text string is 1 less, so the string is always properly terminated.
Returns:
Number of characters or -1 on errors.
Todo:
Hidden entry for password input.

Definition at line 326 of file edline.c.

References EDIT_CHAR_ALARM, EDIT_CHAR_BACKSPACE, EDIT_CHAR_SPACE, EDIT_KEY_DOWN, EDIT_KEY_END, EDIT_KEY_ENTER, EDIT_KEY_HOME, EDIT_KEY_LEFT, EDIT_KEY_REMOVE, EDIT_KEY_RESTORE, EDIT_KEY_RIGHT, EDIT_KEY_UP, EDIT_MODE_ECHO, EDIT_MODE_HISTORY, EDIT_STR_EOL, EditHistoryGet(), EditHistoryInsert(), EditHistorySet(), _EDLINE::el_get, _EDLINE::el_hist, _EDLINE::el_iparm, _EDLINE::el_map, _EDLINE::el_mode, _EDLINE::el_oparm, _EDLINE::el_put, _EDLINE::el_seq, EOF, and strlen().

Here is the call graph for this function:

EDITHISTORY* EditHistoryCreate ( int  siz)

Create an editor history table.

Parameters:
sizMaximum number of history entries.
Returns:
Pointer to an EDITHISTORY structure on success. A NULL pointer is returned in case of an error.

Definition at line 64 of file history.c.

References calloc, free(), _EDITHISTORY::hist_siz, _EDITHISTORY::hist_tab, and malloc().

Referenced by EdLineOpen().

Here is the call graph for this function:

void EditHistoryDestroy ( EDITHISTORY hist)

Create an editor history table.

Releases occupied memory.

Parameters:
histPointer to an EDITHISTORY structure, obtained by a previous call to EditHistoryCreate.

Definition at line 87 of file history.c.

References free(), _EDITHISTORY::hist_siz, and _EDITHISTORY::hist_tab.

Referenced by EdLineClose().

Here is the call graph for this function:

void EditHistorySet ( EDITHISTORY hist,
int  idx,
char *  buf 
)

Replace the specified history table entry.

Parameters:
idxIndex in to the history table.
bufPointer to the string that will replace the existing one.
histPointer to an EDITHISTORY structure, obtained by a previous call to EditHistoryCreate.

Definition at line 109 of file history.c.

References free(), _EDITHISTORY::hist_tab, and strdup().

Referenced by EdLineRead().

Here is the call graph for this function:

int EditHistoryGet ( EDITHISTORY hist,
int  idx,
char *  buf,
int  siz 
)

Retrieve the specified history table entry.

Parameters:
idxIndex in to the history table.
histPointer to an EDITHISTORY structure, obtained by a previous call to EditHistoryCreate.

Definition at line 131 of file history.c.

References _EDITHISTORY::hist_tab, memcpy(), and strlen().

Referenced by EdLineRead().

Here is the call graph for this function:

void EditHistoryInsert ( EDITHISTORY hist,
int  idx,
char *  buf 
)

Insert a new history table entry at the specified position.

All following entries are moved up by 1. If the table is full, then the top most entry will be removed.

Parameters:
idxIndex in to the history table.
bufPointer to the new string. If it doesn't differ from the existing entry, than nothing will be changed.
histPointer to an EDITHISTORY structure, obtained by a previous call to EditHistoryCreate.

Definition at line 165 of file history.c.

References free(), _EDITHISTORY::hist_siz, _EDITHISTORY::hist_tab, strcmp(), and strdup().

Referenced by EdLineRead().

Here is the call graph for this function:

int EdLineKeyMapVt100 ( int  key,
int *  seq 
)

Optional VT100 key mapping routine.

Pass this function to EdLineRegisterKeymap to enable VT100 key mapping.

This routine has been tested with the TeraTerm terminal emulator after loading the keymap contained in ibmkeyb.cnf. It allows to use the arrow keys for moving the cursor and walking through the history. This should work with most other VT100 emulations as well.

The following command sequences are remapped:

Internally calls EdLineKeyMap to handle carriage return and linefeed.

Parameters:
keyInput character to remap.
seqPointer to an integer, which is used by the key mapping routine to store the current state. Note, that its value must be preserved by the caller between calls to EdLineRead.
Returns:
The mapped character. If EDIT_KEY_IGNORE is returned the caller must ignore this character.

Definition at line 82 of file vt100.c.

References EDIT_KEY_DOWN, EDIT_KEY_IGNORE, EDIT_KEY_LEFT, EDIT_KEY_RIGHT, EDIT_KEY_UP, and EdLineKeyMap().

Referenced by LuaThread().

Here is the call graph for this function: