iPXE
ansicol.c
Go to the documentation of this file.
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[] = {
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];
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 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
#define COLOR_DEFAULT
Definition: ansicol.h:17
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
#define COLOR_ALERT_FG
Definition: colour.h:24
static void ansicol_background(unsigned int colour)
Set ANSI background colour.
Definition: ansicol.c:80
Error codes.
#define COLOR_SEPARATOR_BG
Definition: colour.h:19
#define CPAIR_EDIT
Editable text.
Definition: ansicol.h:49
#define DBGC(...)
Definition: compiler.h:505
uint8_t foreground
Foreground colour index.
Definition: ansicol.h:65
#define COLOR_EDIT_BG
Definition: colour.h:22
Display colour configuration.
void ansicol_set_pair(unsigned int cpair)
Set ANSI foreground and background colour.
Definition: ansicol.c:89
#define CPAIR_NORMAL
Normal text.
Definition: ansicol.h:40
#define COLOR_URL_BG
Definition: colour.h:28
#define COLOR_ALERT_BG
Definition: colour.h:25
#define CSI
Control Sequence Introducer.
Definition: ansiesc.h:95
#define colour
Colour for debug messages.
Definition: init.c:40
An ANSI colour pair definition.
Definition: ansicol.h:63
#define COLOR_NORMAL_BG
Definition: colour.h:13
#define CPAIR_PXE
PXE selected menu entry.
Definition: ansicol.h:58
Assertions.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define COLOR_NORMAL_FG
Definition: colour.h:12
#define COLOR_SEPARATOR_FG
Definition: colour.h:18
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
int ansicol_define_pair(unsigned int cpair, unsigned int foreground, unsigned int background)
Define ANSI colour pair.
Definition: ansicol.c:110
ANSI escape sequences.
#define CPAIR_ALERT
Error text.
Definition: ansicol.h:52
__weak void ansicol_set(unsigned int colour, unsigned int which)
Set ANSI colour (when no colour definition support is present)
Definition: ansicol.c:57
#define CPAIR_URL
URL text.
Definition: ansicol.h:55
#define COLOR_SELECT_BG
Definition: colour.h:16
#define COLOR_EDIT_FG
Definition: colour.h:21
#define COLOR_PXE_BG
Definition: colour.h:31
#define CPAIR_SEPARATOR
Unselectable text (e.g.
Definition: ansicol.h:46
static struct ansicol_pair ansicol_pairs[]
ANSI colour pair definitions.
Definition: ansicol.c:40
#define CPAIR_DEFAULT
Default colour pair.
Definition: ansicol.h:37
static void ansicol_foreground(unsigned int colour)
Set ANSI foreground colour.
Definition: ansicol.c:71
#define COLOR_SELECT_FG
Definition: colour.h:15
#define CPAIR_SELECT
Highlighted text.
Definition: ansicol.h:43
#define __weak
Declare a function as weak (use before the definition)
Definition: compiler.h:219
#define COLOR_URL_FG
Definition: colour.h:27
uint8_t background
Background colour index.
Definition: ansicol.h:67
ANSI colours.
#define COLOR_PXE_FG
Definition: colour.h:30