cmdline (merge-intel): fix whitespaces
[libcmdline.git] / src / lib / cmdline_vt100.c
1 /*
2  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the University of California, Berkeley nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdarg.h>
33 #include <ctype.h>
34
35 #include "cmdline_vt100.h"
36
37 const char *cmdline_vt100_commands[] = {
38         vt100_up_arr,
39         vt100_down_arr,
40         vt100_right_arr,
41         vt100_left_arr,
42         "\177",
43         "\n",
44         "\001",
45         "\005",
46         "\013",
47         "\031",
48         "\003",
49         "\006",
50         "\002",
51         vt100_suppr,
52         vt100_tab,
53         "\004",
54         "\014",
55         "\r",
56         "\033\177",
57         vt100_word_left,
58         vt100_word_right,
59         "?",
60 };
61
62 void
63 vt100_init(struct cmdline_vt100 *vt)
64 {
65         vt->state = CMDLINE_VT100_INIT;
66 }
67
68
69 static int
70 match_command(char *buf, unsigned int size)
71 {
72         const char *cmd;
73         unsigned int i = 0;
74
75         for (i=0 ; i<sizeof(cmdline_vt100_commands)/sizeof(const char *) ; i++) {
76                 cmd = *(cmdline_vt100_commands + i);
77
78                 if (size == strlen(cmd) &&
79                     !strncmp(buf, cmd, strlen(cmd))) {
80                         return i;
81                 }
82         }
83
84         return -1;
85 }
86
87 int
88 vt100_parser(struct cmdline_vt100 *vt, char ch)
89 {
90         unsigned int size;
91         uint8_t c = (uint8_t) ch;
92
93         if (vt->bufpos > CMDLINE_VT100_BUF_SIZE) {
94                 vt->state = CMDLINE_VT100_INIT;
95                 vt->bufpos = 0;
96         }
97
98         vt->buf[vt->bufpos++] = c;
99         size = vt->bufpos;
100
101         switch (vt->state) {
102         case CMDLINE_VT100_INIT:
103                 if (c == 033) {
104                         vt->state = CMDLINE_VT100_ESCAPE;
105                 }
106                 else {
107                         vt->bufpos = 0;
108                         goto match_command;
109                 }
110                 break;
111
112         case CMDLINE_VT100_ESCAPE:
113                 if (c == 0133) {
114                         vt->state = CMDLINE_VT100_ESCAPE_CSI;
115                 }
116                 else if (c >= 060 && c <= 0177) { /* XXX 0177 ? */
117                         vt->bufpos = 0;
118                         vt->state = CMDLINE_VT100_INIT;
119                         goto match_command;
120                 }
121                 break;
122
123         case CMDLINE_VT100_ESCAPE_CSI:
124                 if (c >= 0100 && c <= 0176) {
125                         vt->bufpos = 0;
126                         vt->state = CMDLINE_VT100_INIT;
127                         goto match_command;
128                 }
129                 break;
130
131         default:
132                 vt->bufpos = 0;
133                 break;
134         }
135
136         return -2;
137
138  match_command:
139         return match_command(vt->buf, size);
140 }