Initial commit

This commit is contained in:
Julien 2011-08-28 21:48:33 +02:00
commit 0ba55ee2df
5 changed files with 628 additions and 0 deletions

29
Makefile Normal file
View File

@ -0,0 +1,29 @@
##
## Makefile for vt100
##
## Made by julien palard
## Login <vt100@mandark.fr>
##
NAME = vt100
SRC = vt100.c
OBJ = $(SRC:.c=.o)
CC = gcc
INCLUDE = .
DEFINE = _GNU_SOURCE
CFLAGS = -O3 -Wextra -Wall -ansi -pedantic -I$(INCLUDE)
RM = rm -f
$(NAME): $(OBJ)
$(CC) $(CFLAGS) -o $(NAME) $(OBJ) $(LIB)
all:
@make $(NAME)
.c.o:
$(CC) -D $(DEFINE) -c $(CFLAGS) $< -o $(<:.c=.o)
clean:
$(RM) $(NAME) *~ #*# *.o *.core
re: clean all

1
README Normal file
View File

@ -0,0 +1 @@
Trying to implement a library to emulate VT100.

201
test.c Normal file
View File

@ -0,0 +1,201 @@
#include <stdlib.h>
/*
DECSC Save Cursor (DEC Private)
ESC 7
This sequence causes the cursor position, graphic rendition, and
character set to be saved. (See DECRC).
*/
void DECSC(struct vt100 * vt100)
{
}
/*
DECRC Restore Cursor (DEC Private)
ESC 8
This sequence causes the previously saved cursor position, graphic
rendition, and character set to be restored.
*/
void DECRC(struct vt100 * vt100)
{
}
/*
IND Index
ESC D
This sequence causes the active position to move downward one line
without changing the column position. If the active position is at the
bottom margin, a scroll up is performed. Format Effector
*/
void IND(struct vt100 * vt100)
{
}
/*
NEL Next Line
ESC E
This sequence causes the active position to move to the first position
on the next line downward. If the active position is at the bottom
margin, a scroll up is performed. Format Effector
*/
void NEL(struct vt100 * vt100)
{
}
/*
CUU Cursor Up Host to VT100 and VT100 to Host
ESC [ Pn A default value: 1
Moves the active position upward without altering the column
position. The number of lines moved is determined by the parameter. A
parameter value of zero or one moves the active position one line
upward. A parameter value of n moves the active position n lines
upward. If an attempt is made to move the cursor above the top margin,
the cursor stops at the top margin. Editor Function
*/
void CUU(struct vt100 * vt100)
{
}
/*
CUD Cursor Down Host to VT100 and VT100 to Host
ESC [ Pn B default value: 1
The CUD sequence moves the active position downward without altering
the column position. The number of lines moved is determined by the
parameter. If the parameter value is zero or one, the active position
is moved one line downward. If the parameter value is n, the active
position is moved n lines downward. In an attempt is made to move the
cursor below the bottom margin, the cursor stops at the bottom
margin. Editor Function
*/
void CUD(struct vt100 * vt100)
{
}
/*
CUF Cursor Forward Host to VT100 and VT100 to Host
ESC [ Pn C default value: 1
The CUF sequence moves the active position to the right. The distance
moved is determined by the parameter. A parameter value of zero or one
moves the active position one position to the right. A parameter value
of n moves the active position n positions to the right. If an attempt
is made to move the cursor to the right of the right margin, the
cursor stops at the right margin. Editor Function
*/
void CUF(struct vt100 * vt100)
{
}
/*
CUB Cursor Backward Host to VT100 and VT100 to Host
ESC [ Pn D default value: 1
The CUB sequence moves the active position to the left. The distance
moved is determined by the parameter. If the parameter value is zero
or one, the active position is moved one position to the left. If the
parameter value is n, the active position is moved n positions to the
left. If an attempt is made to move the cursor to the left of the left
margin, the cursor stops at the left margin. Editor Function
*/
void CUB(struct vt100 * vt100)
{
}
/*
CUP Cursor Position
ESC [ Pn ; Pn H default value: 1
The CUP sequence moves the active position to the position specified
by the parameters. This sequence has two parameter values, the first
specifying the line position and the second specifying the column
position. A parameter value of zero or one for the first or second
parameter moves the active position to the first line or column in the
display, respectively. The default condition with no parameters
present is equivalent to a cursor to home action. In the VT100, this
control behaves identically with its format effector counterpart,
HVP. Editor Function
The numbering of lines depends on the state of the Origin Mode
(DECOM).
*/
void CUP(struct vt100 * vt100)
{
}
/*
ED Erase In Display
ESC [ Ps J default value: 0
This sequence erases some or all of the characters in the display
according to the parameter. Any complete line erased by this sequence
will return that line to single width mode. Editor Function
Parameter Parameter Meaning
0 Erase from the active position to the end of the screen,
inclusive (default)
1 Erase from start of the screen to the active position, inclusive
2 Erase all of the display all lines are erased, changed to
single-width, and the cursor does not move.
*/
void ED(struct vt100 * vt100)
{
}
/*
EL Erase In Line
ESC [ Ps K default value: 0
Erases some or all characters in the active line according to the
parameter. Editor Function
Parameter Parameter Meaning
0 Erase from the active position to the end of the line, inclusive
(default)
1 Erase from the start of the screen to the active position, inclusive
2 Erase all of the line, inclusive
*/
void EL(struct vt100 * vt100)
{
}
/*
HVP Horizontal and Vertical Position
ESC [ Pn ; Pn f default value: 1
Moves the active position to the position specified by the
parameters. This sequence has two parameter values, the first
specifying the line position and the second specifying the column. A
parameter value of either zero or one causes the active position to
move to the first line or column in the display, respectively. The
default condition with no parameters present moves the active position
to the home position. In the VT100, this control behaves identically
with its editor function counterpart, CUP. The numbering of lines and
columns depends on the reset or set state of the origin mode
(DECOM). Format Effector
*/
void HVP(struct vt100 * vt100)
{
}
int main(int ac, char **av)
{
return EXIT_SUCCESS;
}

160
vt100.c Normal file
View File

@ -0,0 +1,160 @@
#include <stdlib.h>
#include "vt100.h"
/*
* Source : http://vt100.net/docs/vt100-ug/chapter3.html#S3.3
* It's a vt100 implementation, that implements ANSI control function.
*/
/*
* Vocabulary
* CSI | Control Sequence Introducer | ESC [
* Pn | Numeric parameter | [0-9]+
* Ps | Selective parameter |
* | Parameter string | List of parameters separated by ';'
*/
/*
* Control sequences - VT100 to host ( write )
* CPR | Cursor Position Report | ESC [ Pn ; Pn R
*
*
* Control sequencs - Host to VT100 AND VT100 to host ( read write )
* CUB | Cursor Backward | ESC [ Pn D
*
*/
/*
* Naming convention :
* [rw]_shortname(struct vt100emul vt100, ...);
* So CPR is w_CPR(struct vt100emul vt100, int pn1, int pn2);
* And CUB is r_CUB(struct vt100emul vt100, int pn1, int pn2);
* AND w_CUB(struct vt100emul vt100, int pn1, int pn2);
*/
void vt100_push(struct vt100_emul *vt100, char c)
{
if (vt100->stack_ptr >= VT100_STACK_SIZE)
return ;
vt100->stack[vt100->stack_ptr++] = c;
}
void vt100_parse_params(struct vt100_emul *vt100)
{
unsigned int i;
vt100->argc = 0;
vt100->argv[0] = 0;
for (i = 0; i < vt100->stack_ptr; ++i)
{
if (vt100->stack[i] >= '0' && vt100->stack[i] <= '9')
{
vt100->argv[vt100->argc] = vt100->argv[vt100->argc] * 10
+ vt100->stack[i] - '0';
}
else if (vt100->stack[i] == ';')
{
vt100->argc += 1;
vt100->argv[vt100->argc] = 0;
}
}
}
void vt100_call_CSI(struct vt100_emul *vt100, char c)
{
if (c < 'A' || c > 'z')
return ;
if (((vt100_action *)vt100->csi_callbacks)[c - 'A'] == NULL)
return ;
vt100_parse_params(vt100);
((vt100_action *)vt100->csi_callbacks)[c - 'A'](vt100);
}
void vt100_call_ESC(struct vt100_emul *vt100, char c)
{
if (c < '0' || c > 'z')
return ;
if (((vt100_action *)vt100->esc_callbacks)[c - '0'] == NULL)
return ;
((vt100_action *)vt100->esc_callbacks)[c - '0'](vt100);
}
void vt100_call_HASH(struct vt100_emul *vt100, char c)
{
if (c < '0' || c > '9')
return ;
if (((vt100_action *)vt100->hash_callbacks)[c - '0'] == NULL)
return ;
((vt100_action *)vt100->hash_callbacks)[c - '0'](vt100);
}
void vt100_call_GSET(struct vt100_emul *vt100, char c)
{
if (c < '0' || c > 'B')
return ;
if (((vt100_action *)vt100->scs_callbacks)[c - '0'] == NULL)
return ;
((vt100_action *)vt100->scs_callbacks)[c - '0'](vt100);
}
void vt100_read(struct vt100_emul *vt100, char c)
{
if (vt100->state == INIT)
{
if (c != '\033')
vt100->write(vt100, c);
else
vt100->state = ESC;
}
else if (vt100->state == ESC)
{
if (c == '[')
vt100->state = CSI;
else if (c == '#')
vt100->state = HASH;
else if (c == '(')
vt100->state = G0SET;
else if (c == ')')
vt100->state = G1SET;
else
vt100_call_ESC(vt100, c);
}
else if (vt100->state == HASH)
{
vt100_call_HASH(vt100, c);
}
else if (vt100->state == G0SET || vt100->state == G1SET)
{
vt100_call_GSET(vt100, c);
}
else if (vt100->state == CSI)
{
if (c == ';' || (c >= '0' && c <= '9'))
vt100_push(vt100, c);
else
vt100_call_CSI(vt100, c);
}
}
struct vt100_emul *vt100_init(unsigned int width, unsigned int height,
struct vt100_ESC_callbacks *esc,
struct vt100_CSI_callbacks *csi,
struct vt100_HASH_callbacks *hash,
struct vt100_SCS_callbacks *scs)
{
struct vt100_emul *vt100;
vt100 = malloc(sizeof(*vt100));
vt100->width = width;
vt100->height = height;
vt100->cursor_pos_x = 0;
vt100->cursor_pos_y = 0;
vt100->stack_ptr = 0;
vt100->csi_callbacks = csi;
vt100->hash_callbacks = hash;
vt100->esc_callbacks = esc;
vt100->scs_callbacks = scs;
vt100->state = INIT;
return vt100;
}

237
vt100.h Normal file
View File

@ -0,0 +1,237 @@
#ifndef __VT100_H__
#define __VT100_H__
#define VT100_STACK_SIZE 1024
enum vt100_state
{
INIT,
ESC,
HASH,
G0SET,
G1SET,
CSI
};
struct vt100_emul;
typedef void (*vt100_action)(struct vt100_emul *emul);
/*
** [w] -> VT100 to Host
** [r] -> Host to VT100
** [rw] -> VT100 to Host and Host to VT100
** Please read http://vt100.net/docs/vt100-ug/chapter3.html#S3.3
*/
struct vt100_hash_callbacks
{
vt100_action hash_0;
vt100_action hash_1;
vt100_action hash_2;
vt100_action DECDHL_TOP; /* Double Height Line ESC # 3 */
vt100_action DECDHL_BOTTOM; /*Double Height Line ESC # 4 */
vt100_action DECSWL; /* Single-width Line ESC # 5 */
vt100_action DECDWL; /* Double-Width Line ESC # 6 */
vt100_action hash_7;
vt100_action DECALN; /* Screen Alignment Display ESC # 8 */
vt100_action hash_9;
};
struct vt100_SCS_callbacks
{
vt100_action SCS_0; /* Special Graphics */
vt100_action SCS_1; /* Alternate Character ROM Standard Character Set */
vt100_action SCS_2; /* Alternate Character ROM Special Graphics */
vt100_action SCS_3;
vt100_action SCS_4;
vt100_action SCS_5;
vt100_action SCS_6;
vt100_action SCS_7;
vt100_action SCS_8;
vt100_action SCS_9;
vt100_action SCS_3A;
vt100_action SCS_3B;
vt100_action SCS_3C;
vt100_action SCS_3D;
vt100_action SCS_3E;
vt100_action SCS_3F;
vt100_action SCS_40;
vt100_action SCS_A; /* United kingdom Set */
vt100_action SCS_B; /* ASCII Set*/
};
struct vt100_ESC_callbacks
{
vt100_action ESC_0;
vt100_action ESC_1;
vt100_action ESC_2;
vt100_action ESC_3;
vt100_action ESC_4;
vt100_action ESC_5;
vt100_action ESC_6;
vt100_action DECSC; /* Save Cursor ESC 7 */
vt100_action DECRC; /* Restore Cursor ESC 8 */
vt100_action ESC_9;
vt100_action ESC_3A;
vt100_action ESC_3B;
vt100_action ESC_3C;
vt100_action DECKPAM; /* Keypad Application Mode ESC = */
vt100_action DECKPNM; /* Keypad Numeric Mode ESC > */
vt100_action ESC_3F;
vt100_action ESC_40;
vt100_action ESC_A;
vt100_action ESC_B;
vt100_action ESC_C;
vt100_action IND; /* Index ESC D */
vt100_action NEL; /* Next Line ESC E */
vt100_action ESC_F;
vt100_action ESC_G;
vt100_action HTS; /* Horizontal Tabulation Set ESC H */
vt100_action ESC_I;
vt100_action ESC_J;
vt100_action ESC_K;
vt100_action ESC_L;
vt100_action RI; /* Reverse Index ESC M */
vt100_action ESC_N;
vt100_action ESC_O;
vt100_action ESC_P;
vt100_action ESC_Q;
vt100_action CPR; /* [w] Cursor Position Report ESC [ Pn ; Pn R */
vt100_action ESC_S;
vt100_action ESC_T;
vt100_action ESC_U;
vt100_action ESC_V;
vt100_action ESC_W;
vt100_action ESC_X;
vt100_action ESC_Y;
vt100_action DECID; /* Identify Terminal ESC Z */
vt100_action ESC_5B;
vt100_action ESC_5C;
vt100_action ESC_5D;
vt100_action ESC_5E;
vt100_action ESC_5F;
vt100_action ESC_60;
vt100_action ESC_a;
vt100_action ESC_b;
vt100_action RIS; /* Reset To Initial State ESC c */
vt100_action ESC_d;
vt100_action ESC_e;
vt100_action ESC_f;
vt100_action ESC_g;
vt100_action ESC_h;
vt100_action ESC_i;
vt100_action ESC_j;
vt100_action ESC_k;
vt100_action ESC_l;
vt100_action ESC_m;
vt100_action ESC_n;
vt100_action ESC_o;
vt100_action ESC_p;
vt100_action ESC_q;
vt100_action ESC_r;
vt100_action ESC_s;
vt100_action ESC_t;
vt100_action ESC_u;
vt100_action ESC_v;
vt100_action ESC_w;
vt100_action ESC_x;
vt100_action ESC_y;
vt100_action ESC_z;
};
struct vt100_CSI_callbacks
{
vt100_action CUU; /* [rw] Cursor Up ESC [ Pn A */
vt100_action CUD; /* [rw] Cursor Down ESC [ Pn B */
vt100_action CUF; /* [rw] Cursor Forward ESC [ Pn C */
vt100_action CUB; /* [rw] Cursor Backward ESC [ Pn D */
vt100_action CSI_E;
vt100_action CSI_F;
vt100_action CSI_G;
vt100_action CUP; /* [??] Cursor Position ESC [ Pn ; Pn H */
vt100_action CSI_I;
vt100_action ED; /* Erase In Display ESC [ Ps J */
vt100_action EL; /* Erase In Line ESC [ Ps K */
vt100_action CSI_L;
vt100_action CSI_M;
vt100_action CSI_N;
vt100_action CSI_O;
vt100_action CSI_P;
vt100_action CSI_Q;
vt100_action CPR; /* [w] Cursor Position Report ESC [ Pn ; Pn R */
vt100_action CSI_S;
vt100_action CSI_T;
vt100_action CSI_U;
vt100_action CSI_V;
vt100_action CSI_W;
vt100_action CSI_X;
vt100_action CSI_Y;
vt100_action CSI_Z;
vt100_action CSI_5B;
vt100_action CSI_5C;
vt100_action CSI_5D;
vt100_action CSI_5E;
vt100_action CSI_5F;
vt100_action CSI_60;
vt100_action CSI_a;
vt100_action CSI_b;
vt100_action DA; /* [??] Device Attributes ESC [ Pn c */
vt100_action CSI_d;
vt100_action CSI_e;
vt100_action HVP; /* Horiz. and Vert. Position ESC [ Pn ; Pn f */
vt100_action TBC; /* Tabulation Clear ESC [ Ps g */
vt100_action SM; /* Set Mode ESC [ Ps ; . . . ; Ps h */
vt100_action CSI_i;
vt100_action CSI_j;
vt100_action CSI_k;
vt100_action RM; /* Reset Mode ESC [ Ps ; Ps ; . . . ; Ps l */
vt100_action SGR; /* Select Graphic Rendition ESC [ Ps ; . . . ; Ps m */
vt100_action DSR; /* Device Status Report ESC [ Ps n */
vt100_action CSI_o;
vt100_action CSI_p;
vt100_action DECLL; /* Load LEDS ESC [ Ps q */
vt100_action DECSTBM; /* Set Top and Bottom Margins ESC [ Pn; Pn r */
vt100_action CSI_s;
vt100_action CSI_t;
vt100_action CSI_u;
vt100_action CSI_v;
vt100_action CSI_w;
vt100_action DECRE_TPARM; /* Re{port,quest} Terminal Parameters ESC [ . x */
vt100_action DECTST; /* Invoke Confidence Test ESC [ 2 ; Ps y */
vt100_action CSI_z;
};
struct vt100_emul
{
unsigned int width;
unsigned int height;
unsigned int cursor_pos_x;
unsigned int cursor_pos_y;
enum vt100_state state;
unsigned int argc;
unsigned int argv[VT100_STACK_SIZE];
void (*write)(struct vt100_emul *, char c);
char stack[VT100_STACK_SIZE];
unsigned int stack_ptr;
struct vt100_CSI_callbacks *csi_callbacks;
struct vt100_HASH_callbacks *hash_callbacks;
struct vt100_ESC_callbacks *esc_callbacks;
struct vt100_SCS_callbacks *scs_callbacks;
};
struct vt100_emul *vt100_init(unsigned int width, unsigned int height,
struct vt100_ESC_callbacks *esc,
struct vt100_CSI_callbacks *csi,
struct vt100_HASH_callbacks *hash,
struct vt100_SCS_callbacks *scs);
#endif