[process] Include process name in debug messages
[ipxe.git] / 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 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdio.h>
27 #include <errno.h>
28 #include <assert.h>
29 #include <ipxe/ansiesc.h>
30 #include <ipxe/ansicol.h>
31 #include <config/colour.h>
32
33 /** @file
34 *
35 * ANSI colours
36 *
37 */
38
39 /** ANSI colour pair definitions */
40 static struct ansicol_pair ansicol_pairs[] = {
41 [CPAIR_DEFAULT] = { COLOR_DEFAULT, COLOR_DEFAULT },
42 [CPAIR_NORMAL] = { COLOR_NORMAL_FG, COLOR_NORMAL_BG },
43 [CPAIR_SELECT] = { COLOR_SELECT_FG, COLOR_SELECT_BG },
44 [CPAIR_SEPARATOR] = { COLOR_SEPARATOR_FG, COLOR_SEPARATOR_BG },
45 [CPAIR_EDIT] = { COLOR_EDIT_FG, COLOR_EDIT_BG },
46 [CPAIR_ALERT] = { COLOR_ALERT_FG, COLOR_ALERT_BG },
47 [CPAIR_URL] = { COLOR_URL_FG, COLOR_URL_BG },
48 [CPAIR_PXE] = { COLOR_PXE_FG, COLOR_PXE_BG },
49 };
50
51 /**
52 * Set ANSI colour (when no colour definition support is present)
53 *
54 * @v colour Colour index
55 * @v which Foreground/background selector
56 */
57 __weak void ansicol_set ( unsigned int colour, unsigned int which ) {
58
59 /* Colour indices are hardcoded and should never be out of range */
60 assert ( colour < 10 );
61
62 /* Set basic colour */
63 printf ( CSI "%c%dm", which, colour );
64 }
65
66 /**
67 * Set ANSI foreground colour
68 *
69 * @v colour Colour index
70 */
71 static void ansicol_foreground ( unsigned int colour ) {
72 ansicol_set ( colour, '3' );
73 }
74
75 /**
76 * Set ANSI background colour
77 *
78 * @v colour Colour index
79 */
80 static void ansicol_background ( unsigned int colour ) {
81 ansicol_set ( colour, '4' );
82 }
83
84 /**
85 * Set ANSI foreground and background colour
86 *
87 * @v cpair Colour pair index
88 */
89 void ansicol_set_pair ( unsigned int cpair ) {
90 struct ansicol_pair *pair;
91
92 /* Colour pair indices are hardcoded and should never be out of range */
93 assert ( cpair < ( sizeof ( ansicol_pairs ) /
94 sizeof ( ansicol_pairs[0] ) ) );
95
96 /* Set both foreground and background colours */
97 pair = &ansicol_pairs[cpair];
98 ansicol_foreground ( pair->foreground );
99 ansicol_background ( pair->background );
100 }
101
102 /**
103 * Define ANSI colour pair
104 *
105 * @v cpair Colour pair index
106 * @v foreground Foreground colour index
107 * @v background Background colour index
108 * @ret rc Return status code
109 */
110 int ansicol_define_pair ( unsigned int cpair, unsigned int foreground,
111 unsigned int background ) {
112 struct ansicol_pair *pair;
113
114 /* Fail if colour index is out of range */
115 if ( cpair >= ( sizeof ( ansicol_pairs ) / sizeof ( ansicol_pairs[0] )))
116 return -EINVAL;
117
118 /* Update colour pair definition */
119 pair = &ansicol_pairs[cpair];
120 pair->foreground = foreground;
121 pair->background = background;
122 DBGC ( &ansicol_pairs[0], "ANSICOL redefined colour pair %d as "
123 "foreground %d background %d\n", cpair, foreground, background );
124
125 return 0;
126 }