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
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <stdio.h>
28#include <errno.h>
29#include <assert.h>
30#include <ipxe/ansiesc.h>
31#include <ipxe/ansicol.h>
32#include <config/colour.h>
33
34/** @file
35 *
36 * ANSI colours
37 *
38 */
39
40/** ANSI colour pair definitions */
51
52/**
53 * Set ANSI colour (when no colour definition support is present)
54 *
55 * @v colour Colour index
56 * @v which Foreground/background selector
57 */
58__weak void ansicol_set ( unsigned int colour, unsigned int which ) {
59
60 /* Colour indices are hardcoded and should never be out of range */
61 assert ( colour < 10 );
62
63 /* Set basic colour */
64 printf ( CSI "%c%dm", which, colour );
65}
66
67/**
68 * Set ANSI foreground colour
69 *
70 * @v colour Colour index
71 */
72static void ansicol_foreground ( unsigned int colour ) {
73 ansicol_set ( colour, '3' );
74}
75
76/**
77 * Set ANSI background colour
78 *
79 * @v colour Colour index
80 */
81static void ansicol_background ( unsigned int colour ) {
82 ansicol_set ( colour, '4' );
83}
84
85/**
86 * Set ANSI foreground and background colour
87 *
88 * @v cpair Colour pair index
89 */
90void ansicol_set_pair ( unsigned int cpair ) {
91 struct ansicol_pair *pair;
92
93 /* Colour pair indices are hardcoded and should never be out of range */
94 assert ( cpair < ( sizeof ( ansicol_pairs ) /
95 sizeof ( ansicol_pairs[0] ) ) );
96
97 /* Set both foreground and background colours */
98 pair = &ansicol_pairs[cpair];
101}
102
103/**
104 * Define ANSI colour pair
105 *
106 * @v cpair Colour pair index
107 * @v foreground Foreground colour index
108 * @v background Background colour index
109 * @ret rc Return status code
110 */
111int ansicol_define_pair ( unsigned int cpair, unsigned int foreground,
112 unsigned int background ) {
113 struct ansicol_pair *pair;
114
115 /* Fail if colour index is out of range */
116 if ( cpair >= ( sizeof ( ansicol_pairs ) / sizeof ( ansicol_pairs[0] )))
117 return -EINVAL;
118
119 /* Update colour pair definition */
120 pair = &ansicol_pairs[cpair];
121 pair->foreground = foreground;
122 pair->background = background;
123 DBGC ( &ansicol_pairs[0], "ANSICOL redefined colour pair %d as "
124 "foreground %d background %d\n", cpair, foreground, background );
125
126 return 0;
127}
#define colour
Colour for debug messages.
Definition acpi.c:42
void ansicol_set_pair(unsigned int cpair)
Set ANSI foreground and background colour.
Definition ansicol.c:90
__weak void ansicol_set(unsigned int colour, unsigned int which)
Set ANSI colour (when no colour definition support is present)
Definition ansicol.c:58
static void ansicol_background(unsigned int colour)
Set ANSI background colour.
Definition ansicol.c:81
int ansicol_define_pair(unsigned int cpair, unsigned int foreground, unsigned int background)
Define ANSI colour pair.
Definition ansicol.c:111
static void ansicol_foreground(unsigned int colour)
Set ANSI foreground colour.
Definition ansicol.c:72
static struct ansicol_pair ansicol_pairs[]
ANSI colour pair definitions.
Definition ansicol.c:41
ANSI colours.
#define COLOR_DEFAULT
Definition ansicol.h:18
ANSI escape sequences.
#define CSI
Control Sequence Introducer.
Definition ansiesc.h:96
Assertions.
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
Display colour configuration.
#define COLOR_ALERT_BG
Definition colour.h:26
#define COLOR_EDIT_FG
Definition colour.h:22
#define COLOR_SEPARATOR_BG
Definition colour.h:20
#define COLOR_ALERT_FG
Definition colour.h:25
#define COLOR_URL_FG
Definition colour.h:28
#define COLOR_EDIT_BG
Definition colour.h:23
#define COLOR_PXE_FG
Definition colour.h:31
#define COLOR_NORMAL_BG
Definition colour.h:14
#define COLOR_NORMAL_FG
Definition colour.h:13
#define COLOR_SEPARATOR_FG
Definition colour.h:19
#define COLOR_PXE_BG
Definition colour.h:32
#define COLOR_SELECT_FG
Definition colour.h:16
#define COLOR_SELECT_BG
Definition colour.h:17
#define COLOR_URL_BG
Definition colour.h:29
Error codes.
#define CPAIR_SELECT
Highlighted text.
Definition ansicol.h:44
#define CPAIR_EDIT
Editable text.
Definition ansicol.h:50
#define CPAIR_NORMAL
Normal text.
Definition ansicol.h:41
#define CPAIR_DEFAULT
Default colour pair.
Definition ansicol.h:38
#define CPAIR_SEPARATOR
Unselectable text (e.g.
Definition ansicol.h:47
#define CPAIR_ALERT
Error text.
Definition ansicol.h:53
#define CPAIR_PXE
PXE selected menu entry.
Definition ansicol.h:59
#define CPAIR_URL
URL text.
Definition ansicol.h:56
#define DBGC(...)
Definition compiler.h:505
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define EINVAL
Invalid argument.
Definition errno.h:429
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
#define __weak
Declare a function as weak (use before the definition)
Definition compiler.h:219
An ANSI colour pair definition.
Definition ansicol.h:64
uint8_t foreground
Foreground colour index.
Definition ansicol.h:66
uint8_t background
Background colour index.
Definition ansicol.h:68
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition vsprintf.c:465