00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <stdlib.h>
00025 #include <stdio.h>
00026 #include <string.h>
00027 #include <stdarg.h>
00028 #include <ctype.h>
00029
00030 #include <aversive/pgmspace.h>
00031
00032 #include "vt100.h"
00033
00034 static const prog_char cmd0[] = vt100_up_arr;
00035 static const prog_char cmd1[] = vt100_down_arr;
00036 static const prog_char cmd2[] = vt100_right_arr;
00037 static const prog_char cmd3[] = vt100_left_arr;
00038 static const prog_char cmd4[] = "\177";
00039 static const prog_char cmd5[] = "\n";
00040 static const prog_char cmd6[] = "\001";
00041 static const prog_char cmd7[] = "\005";
00042 static const prog_char cmd8[] = "\013";
00043 static const prog_char cmd9[] = "\031";
00044 static const prog_char cmd10[] = "\003";
00045 static const prog_char cmd11[] = "\006";
00046 static const prog_char cmd12[] = "\002";
00047 static const prog_char cmd13[] = vt100_suppr;
00048 static const prog_char cmd14[] = vt100_tab;
00049 static const prog_char cmd15[] = "\004";
00050 static const prog_char cmd16[] = "\014";
00051 static const prog_char cmd17[] = "\r";
00052 static const prog_char cmd18[] = "\033\177";
00053 static const prog_char cmd19[] = vt100_word_left;
00054 static const prog_char cmd20[] = vt100_word_right;
00055 static const prog_char cmd21[] = "?";
00056
00057 const prog_char * vt100_commands[] PROGMEM = {
00058 cmd0, cmd1, cmd2, cmd3, cmd4, cmd5, cmd6, cmd7,
00059 cmd8, cmd9, cmd10, cmd11, cmd12, cmd13, cmd14,
00060 cmd15, cmd16, cmd17, cmd18, cmd19, cmd20,
00061 cmd21,
00062 };
00063
00064 void
00065 vt100_init(struct vt100 * vt)
00066 {
00067 vt->state = VT100_INIT;
00068 }
00069
00070
00071 static int8_t
00072 match_command(char * buf, uint8_t size)
00073 {
00074 const prog_char * cmd;
00075 uint8_t i = 0;
00076
00077 for (i=0 ; i<sizeof(vt100_commands)/sizeof(const prog_char *) ; i++) {
00078 #ifdef HOST_VERSION
00079 cmd = *(vt100_commands + i);
00080 #else
00081 cmd = (const prog_char *) pgm_read_word (vt100_commands + i);
00082 #endif
00083
00084 if (size == strlen_P(cmd) &&
00085 !strncmp_P(buf, cmd, strlen_P(cmd))) {
00086 return i;
00087 }
00088 }
00089
00090 return -1;
00091 }
00092
00093 int8_t
00094 vt100_parser(struct vt100 *vt, char ch)
00095 {
00096 uint8_t size;
00097 uint8_t c = (uint8_t) ch;
00098
00099 if (vt->bufpos > VT100_BUF_SIZE) {
00100 vt->state = VT100_INIT;
00101 vt->bufpos = 0;
00102 }
00103
00104 vt->buf[vt->bufpos++] = c;
00105 size = vt->bufpos;
00106
00107 switch (vt->state) {
00108 case VT100_INIT:
00109 if (c == 033) {
00110 vt->state = VT100_ESCAPE;
00111 }
00112 else {
00113 vt->bufpos = 0;
00114 goto match_command;
00115 }
00116 break;
00117
00118 case VT100_ESCAPE:
00119 if (c == 0133) {
00120 vt->state = VT100_ESCAPE_CSI;
00121 }
00122 else if (c >= 060 && c <= 0177) {
00123 vt->bufpos = 0;
00124 vt->state = VT100_INIT;
00125 goto match_command;
00126 }
00127 break;
00128
00129 case VT100_ESCAPE_CSI:
00130 if (c >= 0100 && c <= 0176) {
00131 vt->bufpos = 0;
00132 vt->state = VT100_INIT;
00133 goto match_command;
00134 }
00135 break;
00136
00137 default:
00138 vt->bufpos = 0;
00139 break;
00140 }
00141
00142 return -2;
00143
00144 match_command:
00145 return match_command(vt->buf, size);
00146 }