]> git.ozlabs.org Git - petitboot/blob - test/ui/console-sequence.c
discover: Reimplement native-parser as a Bison parser
[petitboot] / test / ui / console-sequence.c
1 /*
2  *  Copyright (C) 2017 IBM Corporation
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; version 2 of the License.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  */
14
15 #include <stdlib.h>
16 #include <string.h>
17 #include <assert.h>
18 #include <stdbool.h>
19 #include <stdio.h>
20
21 #include "talloc/talloc.h"
22
23 #define ERR     (-1)
24 #define pb_log(...)     printf(__VA_ARGS__)
25 #define pb_debug(...)   printf(__VA_ARGS__)
26
27 /*
28  * Several example terminal commands, see:
29  * https://vt100.net/docs/vt100-ug/chapter3.html
30  */
31 static char identify_rsp[] = {033, 0133, 077, 061, 073, 060, 0143, '\0'};
32 static char decdwl[] = {033, 043, 066, '\0'};
33 static char attrs[] = {033, 0133, 060, 073, 064, 073, 065, 0155, '\0'};
34 static char cursor[] = {033, 070, '\0'};
35 static char conf_test[] = {033, 0133, 062, 073, 061, 0171, '\0'};
36 static char status[] = {033, 0133, 060, 0156, '\0'};
37 static char erase_screen[] = {033, 0133, 062, 0112, '\0'};
38 static char status_ok_rsp[] = {033, 0133, 064, 0156, '\0'};
39 static char garbage[] = {001, 002, 003, 004, '\0'};
40 static char esc_garbage[] = {033, 002, 003, 004, '\0'};
41 static char *ptr;
42
43 static signed char getch(void)
44 {
45         if (!ptr || *ptr == '\0')
46                 return -ERR;
47         return *ptr++;
48 }
49
50 #include "ui/ncurses/console-codes.c"
51
52 int main(void)
53 {
54         void *ctx;
55         char *seq, *confused;
56
57         ctx = talloc_new(NULL);
58
59         ptr = &identify_rsp[1];
60         printf("Identity response\n");
61         seq = handle_control_sequence(ctx, identify_rsp[0]);
62         assert(strncmp(seq, identify_rsp, strlen(identify_rsp)) == 0);
63
64         printf("DECDWL\n");
65         ptr = &decdwl[1];
66         seq = handle_control_sequence(ctx, decdwl[0]);
67         assert(strncmp(seq, decdwl, strlen(decdwl)) == 0);
68
69         printf("Attributes\n");
70         ptr = &attrs[1];
71         seq = handle_control_sequence(ctx, attrs[0]);
72         assert(strncmp(seq, attrs, strlen(attrs)) == 0);
73
74         printf("Reset Cursor\n");
75         ptr = &cursor[1];
76         seq = handle_control_sequence(ctx, cursor[0]);
77         assert(strncmp(seq, cursor, strlen(cursor)) == 0);
78
79         printf("Confidence Test\n");
80         ptr = &conf_test[1];
81         seq = handle_control_sequence(ctx, conf_test[0]);
82         assert(strncmp(seq, conf_test, strlen(conf_test)) == 0);
83
84         printf("Status\n");
85         ptr = &status[1];
86         seq = handle_control_sequence(ctx, status[0]);
87         assert(strncmp(seq, status, strlen(status)) == 0);
88
89         printf("Erase Screen\n");
90         ptr = &erase_screen[1];
91         seq = handle_control_sequence(ctx, erase_screen[0]);
92         assert(strncmp(seq, erase_screen, strlen(erase_screen)) == 0);
93
94         printf("Status Response\n");
95         ptr = &status_ok_rsp[1];
96         seq = handle_control_sequence(ctx, status_ok_rsp[0]);
97         assert(strncmp(seq, status_ok_rsp, strlen(status_ok_rsp)) == 0);
98
99         printf("Garbage\n");
100         ptr = &garbage[1];
101         seq = handle_control_sequence(ctx, garbage[0]);
102         assert(seq == NULL);
103
104         printf("ESC then Garbage\n");
105         ptr = &esc_garbage[1];
106         confused = talloc_asprintf(ctx, "%c%c...", 033, 002);
107         seq = handle_control_sequence(ctx, esc_garbage[0]);
108         assert(strncmp(seq, confused, strlen(confused)) == 0);
109
110         talloc_free(ctx);
111         return EXIT_SUCCESS;
112 }