Merge tag 'v3.14.25' into backport/v3.14.24-ltsi-rc1+v3.14.25/snapshot-merge.wip
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / staging / ktap / runtime / lib_ansi.c
1 /*
2  * ansilib.c - ANSI escape sequences library
3  *
4  * http://en.wikipedia.org/wiki/ANSI_escape_code
5  *
6  * This file is part of ktap by Jovi Zhangwei.
7  *
8  * Copyright (C) 2012-2013 Jovi Zhangwei <jovi.zhangwei@gmail.com>.
9  *
10  * ktap is free software; you can redistribute it and/or modify it
11  * under the terms and conditions of the GNU General Public License,
12  * version 2, as published by the Free Software Foundation.
13  *
14  * ktap is distributed in the hope it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17  * more details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include "../include/ktap_types.h"
25 #include "ktap.h"
26 #include "kp_vm.h"
27
28 /**
29  * function ansi.clear_screen - Move cursor to top left and clear screen.
30  *
31  * Description: Sends ansi code for moving cursor to top left and then the
32  * ansi code for clearing the screen from the cursor position to the end.
33  */
34
35 static int ktap_lib_clear_screen(ktap_state *ks)
36 {
37         kp_printf(ks, "\033[1;1H\033[J");
38         return 0;
39 }
40
41 /**
42  * function ansi.set_color - Set the ansi Select Graphic Rendition mode.
43  * @fg: Foreground color to set.
44  *
45  * Description: Sends ansi code for Select Graphic Rendition mode for the
46  * given forground color. Black (30), Blue (34), Green (32), Cyan (36),
47  * Red (31), Purple (35), Brown (33), Light Gray (37).
48  */
49
50 static int ktap_lib_set_color(ktap_state *ks)
51 {
52         int fg;
53
54         kp_arg_check(ks, 1, KTAP_TNUMBER);
55
56         fg = nvalue(kp_arg(ks, 1));
57         kp_printf(ks, "\033[%dm", fg);
58         return 0;
59 }
60
61 /**
62  * function ansi.set_color2 - Set the ansi Select Graphic Rendition mode.
63  * @fg: Foreground color to set.
64  * @bg: Background color to set.
65  *
66  * Description: Sends ansi code for Select Graphic Rendition mode for the
67  * given forground color, Black (30), Blue (34), Green (32), Cyan (36),
68  * Red (31), Purple (35), Brown (33), Light Gray (37) and the given
69  * background color, Black (40), Red (41), Green (42), Yellow (43),
70  * Blue (44), Magenta (45), Cyan (46), White (47).
71  */
72 static int ktap_lib_set_color2(ktap_state *ks)
73 {
74         int fg, bg;
75
76         kp_arg_check(ks, 1, KTAP_TNUMBER);
77         kp_arg_check(ks, 2, KTAP_TNUMBER);
78
79         fg = nvalue(kp_arg(ks, 1));
80         bg = nvalue(kp_arg(ks, 2));
81         kp_printf(ks, "\033[%d;%dm", fg, bg);
82         return 0;
83 }
84
85 /**
86  * function ansi.set_color3 - Set the ansi Select Graphic Rendition mode.
87  * @fg: Foreground color to set.
88  * @bg: Background color to set.
89  * @attr: Color attribute to set.
90  *
91  * Description: Sends ansi code for Select Graphic Rendition mode for the
92  * given forground color, Black (30), Blue (34), Green (32), Cyan (36),
93  * Red (31), Purple (35), Brown (33), Light Gray (37), the given
94  * background color, Black (40), Red (41), Green (42), Yellow (43),
95  * Blue (44), Magenta (45), Cyan (46), White (47) and the color attribute
96  * All attributes off (0), Intensity Bold (1), Underline Single (4),
97  * Blink Slow (5), Blink Rapid (6), Image Negative (7).
98  */
99 static int ktap_lib_set_color3(ktap_state *ks)
100 {
101         int fg, bg, attr;
102
103         kp_arg_check(ks, 1, KTAP_TNUMBER);
104         kp_arg_check(ks, 2, KTAP_TNUMBER);
105         kp_arg_check(ks, 3, KTAP_TNUMBER);
106
107         fg = nvalue(kp_arg(ks, 1));
108         bg = nvalue(kp_arg(ks, 2));
109         attr = nvalue(kp_arg(ks, 3));
110
111         if (attr)
112                 kp_printf(ks, "\033[%d;%d;%dm", fg, bg, attr);
113         else
114                 kp_printf(ks, "\033[%d;%dm", fg, bg);
115
116         return 0;
117 }
118
119 /**
120  * function ansi.reset_color - Resets Select Graphic Rendition mode.
121  *
122  * Description: Sends ansi code to reset foreground, background and color
123  * attribute to default values.
124  */
125 static int ktap_lib_reset_color(ktap_state *ks)
126 {
127         kp_printf(ks, "\033[0;0m");
128         return 0;
129 }
130
131 /**
132  * function ansi.new_line - Move cursor to new line.
133  *
134  * Description: Sends ansi code new line.
135  */
136 static int ktap_lib_new_line (ktap_state *ks)
137 {
138         kp_printf(ks, "\12");
139         return 0;
140 }
141
142 static const ktap_Reg ansi_funcs[] = {
143         {"clear_screen", ktap_lib_clear_screen},
144         {"set_color", ktap_lib_set_color},
145         {"set_color2", ktap_lib_set_color2},
146         {"set_color3", ktap_lib_set_color3},
147         {"reset_color", ktap_lib_reset_color},
148         {"new_line", ktap_lib_new_line},
149         {NULL}
150 };
151
152 void kp_init_ansilib(ktap_state *ks)
153 {
154         kp_register_lib(ks, "ansi", ansi_funcs);
155 }