Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / core / ansicol.c
1 /*
2  * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdio.h>
23 #include <errno.h>
24 #include <assert.h>
25 #include <ipxe/ansiesc.h>
26 #include <ipxe/ansicol.h>
27 #include <config/colour.h>
28
29 /** @file
30  *
31  * ANSI colours
32  *
33  */
34
35 /** ANSI colour pair definitions */
36 static struct ansicol_pair ansicol_pairs[] = {
37         [CPAIR_DEFAULT] = { COLOR_DEFAULT, COLOR_DEFAULT },
38         [CPAIR_NORMAL] = { COLOR_NORMAL_FG, COLOR_NORMAL_BG },
39         [CPAIR_SELECT] = { COLOR_SELECT_FG, COLOR_SELECT_BG },
40         [CPAIR_SEPARATOR] = { COLOR_SEPARATOR_FG, COLOR_SEPARATOR_BG },
41         [CPAIR_EDIT] = { COLOR_EDIT_FG, COLOR_EDIT_BG },
42         [CPAIR_ALERT] = { COLOR_ALERT_FG, COLOR_ALERT_BG },
43         [CPAIR_URL] = { COLOR_URL_FG, COLOR_URL_BG },
44         [CPAIR_PXE] = { COLOR_PXE_FG, COLOR_PXE_BG },
45 };
46
47 /**
48  * Set ANSI colour (when no colour definition support is present)
49  *
50  * @v colour            Colour index
51  * @v which             Foreground/background selector
52  */
53 __weak void ansicol_set ( unsigned int colour, unsigned int which ) {
54
55         /* Colour indices are hardcoded and should never be out of range */
56         assert ( colour < 10 );
57
58         /* Set basic colour */
59         printf ( CSI "%c%dm", which, colour );
60 }
61
62 /**
63  * Set ANSI foreground colour
64  *
65  * @v colour            Colour index
66  */
67 static void ansicol_foreground ( unsigned int colour ) {
68         ansicol_set ( colour, '3' );
69 }
70
71 /**
72  * Set ANSI background colour
73  *
74  * @v colour            Colour index
75  */
76 static void ansicol_background ( unsigned int colour ) {
77         ansicol_set ( colour, '4' );
78 }
79
80 /**
81  * Set ANSI foreground and background colour
82  *
83  * @v cpair             Colour pair index
84  */
85 void ansicol_set_pair ( unsigned int cpair ) {
86         struct ansicol_pair *pair;
87
88         /* Colour pair indices are hardcoded and should never be out of range */
89         assert ( cpair < ( sizeof ( ansicol_pairs ) /
90                            sizeof ( ansicol_pairs[0] ) ) );
91
92         /* Set both foreground and background colours */
93         pair = &ansicol_pairs[cpair];
94         ansicol_foreground ( pair->foreground );
95         ansicol_background ( pair->background );
96 }
97
98 /**
99  * Define ANSI colour pair
100  *
101  * @v cpair             Colour pair index
102  * @v foreground        Foreground colour index
103  * @v background        Background colour index
104  * @ret rc              Return status code
105  */
106 int ansicol_define_pair ( unsigned int cpair, unsigned int foreground,
107                           unsigned int background ) {
108         struct ansicol_pair *pair;
109
110         /* Fail if colour index is out of range */
111         if ( cpair >= ( sizeof ( ansicol_pairs ) / sizeof ( ansicol_pairs[0] )))
112                 return -EINVAL;
113
114         /* Update colour pair definition */
115         pair = &ansicol_pairs[cpair];
116         pair->foreground = foreground;
117         pair->background = background;
118         DBGC ( &ansicol_pairs[0], "ANSICOL redefined colour pair %d as "
119                "foreground %d background %d\n", cpair, foreground, background );
120
121         return 0;
122 }