cmdline: use SPDX tags
[dpdk.git] / lib / librte_cmdline / cmdline_vt100.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation.
3  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
4  * All rights reserved.
5  */
6
7 #include <stdlib.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdarg.h>
12 #include <ctype.h>
13 #include <termios.h>
14
15 #include "cmdline_vt100.h"
16
17 const char *cmdline_vt100_commands[] = {
18         vt100_up_arr,
19         vt100_down_arr,
20         vt100_right_arr,
21         vt100_left_arr,
22         "\177",
23         "\n",
24         "\001",
25         "\005",
26         "\013",
27         "\031",
28         "\003",
29         "\006",
30         "\002",
31         vt100_suppr,
32         vt100_tab,
33         "\004",
34         "\014",
35         "\r",
36         "\033\177",
37         vt100_word_left,
38         vt100_word_right,
39         "?",
40         "\027",
41         "\020",
42         "\016",
43         "\033\144",
44         vt100_bs,
45 };
46
47 void
48 vt100_init(struct cmdline_vt100 *vt)
49 {
50         if (!vt)
51                 return;
52         vt->state = CMDLINE_VT100_INIT;
53 }
54
55
56 static int
57 match_command(char *buf, unsigned int size)
58 {
59         const char *cmd;
60         size_t cmdlen;
61         unsigned int i = 0;
62
63         for (i=0 ; i<sizeof(cmdline_vt100_commands)/sizeof(const char *) ; i++) {
64                 cmd = *(cmdline_vt100_commands + i);
65
66                 cmdlen = strnlen(cmd, CMDLINE_VT100_BUF_SIZE);
67                 if (size == cmdlen &&
68                     !strncmp(buf, cmd, cmdlen)) {
69                         return i;
70                 }
71         }
72
73         return -1;
74 }
75
76 int
77 vt100_parser(struct cmdline_vt100 *vt, char ch)
78 {
79         unsigned int size;
80         uint8_t c = (uint8_t) ch;
81
82         if (!vt)
83                 return -1;
84
85         if (vt->bufpos >= CMDLINE_VT100_BUF_SIZE) {
86                 vt->state = CMDLINE_VT100_INIT;
87                 vt->bufpos = 0;
88         }
89
90         vt->buf[vt->bufpos++] = c;
91         size = vt->bufpos;
92
93         switch (vt->state) {
94         case CMDLINE_VT100_INIT:
95                 if (c == 033) {
96                         vt->state = CMDLINE_VT100_ESCAPE;
97                 }
98                 else {
99                         vt->bufpos = 0;
100                         goto match_command;
101                 }
102                 break;
103
104         case CMDLINE_VT100_ESCAPE:
105                 if (c == 0133) {
106                         vt->state = CMDLINE_VT100_ESCAPE_CSI;
107                 }
108                 else if (c >= 060 && c <= 0177) { /* XXX 0177 ? */
109                         vt->bufpos = 0;
110                         vt->state = CMDLINE_VT100_INIT;
111                         goto match_command;
112                 }
113                 break;
114
115         case CMDLINE_VT100_ESCAPE_CSI:
116                 if (c >= 0100 && c <= 0176) {
117                         vt->bufpos = 0;
118                         vt->state = CMDLINE_VT100_INIT;
119                         goto match_command;
120                 }
121                 break;
122
123         default:
124                 vt->bufpos = 0;
125                 break;
126         }
127
128         return -2;
129
130  match_command:
131         return match_command(vt->buf, size);
132 }