chiark / gitweb /
*.[ch]: Remove unnecessary header files.
[mLib] / ui / ttycolour.c
1 /* -*-c-*-
2  *
3  * Configurable terminal colour support
4  *
5  * (c) 2024 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the mLib utilities library.
11  *
12  * mLib is free software: you can redistribute it and/or modify it under
13  * the terms of the GNU Library General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or (at
15  * your option) any later version.
16  *
17  * mLib is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
20  * License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with mLib.  If not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25  * USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <ctype.h>
31 #include <string.h>
32
33 #include "macros.h"
34 #include "report.h"
35 #include "ttycolour.h"
36
37 /*----- Main code ---------------------------------------------------------*/
38
39 int ttycolour_config(ttycolour_attr *attr, const char *user, unsigned f,
40                      const struct ttycolour_style *tab)
41 {
42   const char *p, *q;
43   size_t n;
44   unsigned i, arg, a;
45   int rc = 0;
46
47   for (i = 0; tab[i].tok; i++) attr[i] = tab[i].dflt;
48
49   if (f&TCIF_GETENV) p = getenv(user);
50   else p = user;
51   if (p)
52     while (*p) {
53       n = strcspn(p, "=:"); q = p + n;
54       if (!*q || *q == ':') {
55         if (f&TCIF_REPORT) moan("missing colour token in `%s'", user);
56         rc = -1; p = *q ? q + 1 : q; continue;
57       }
58       for (i = 0; tab[i].tok; i++)
59         if (STRNCMP(p, ==, tab[i].tok, n) && !tab[i].tok[n])
60           goto found_tok;
61       if (f&TCIF_REPORT)
62         moan("unknown colour token `%.*s' in `%s'", (int)n, p, user);
63       rc = -1; q = p + strcspn(p, ":"); p = *q ? q + 1 : q; continue;
64
65     found_tok:
66       a = 0; arg = 0; q++;
67       for (;;) {
68
69         if (ISDIGIT(*q))
70           { arg = 10*arg + (*q++ - '0'); continue; }
71
72         switch (arg) {
73           case 0: a = 0; break;
74           case 1: a |= TCAF_BOLD; break;
75           case 39: a &= ~(TCAF_FG | TCAF_FGMASK); break;
76           case 49: a &= ~(TCAF_BG | TCAF_BGMASK); break;
77           case 30: case 31: case 32: case 33:
78           case 34: case 35: case 36: case 37:
79             a = (a&~TCAF_FGMASK) | TCAF_FG | ((arg - 30) << TCAF_FGSHIFT);
80             break;
81           case 40: case 41: case 42: case 43:
82           case 44: case 45: case 46: case 47:
83             a = (a&~TCAF_BGMASK) | TCAF_BG | ((arg - 40) << TCAF_BGSHIFT);
84             break;
85           case 90: case 91: case 92: case 93:
86           case 94: case 95: case 96: case 97:
87             a = (a&~TCAF_FGMASK) | TCAF_FG |
88                 (((arg - 90) | TCCF_BRIGHT) << TCAF_FGSHIFT);
89             break;
90           case 100: case 101: case 102: case 103:
91           case 104: case 105: case 106: case 107:
92             a = (a&~TCAF_BGMASK) | TCAF_BG |
93                 (((arg - 100) | TCCF_BRIGHT) << TCAF_BGSHIFT);
94             break;
95           default:
96             if (f&TCIF_REPORT)
97               moan("unknown colour code %u in `%.*s' string in `%s'",
98                    arg, (int)n, p, user);
99             rc = -1; break;
100         }
101         arg = 0;
102         if (!*q || *q == ':') break;
103         else if (*q != ';') {
104           if (f&TCIF_REPORT)
105             moan("expected `;' but found `%c' in `%.*s' string in `%s'",
106                  *q, (int)n, p, user);
107           rc = -1; q += strcspn(q, ":;"); if (!*q || *q == ':') break;
108         }
109         q++;
110       }
111       attr[i] = a; p = *q ? q + 1 : q;
112     }
113
114   return (rc);
115 }
116
117 void ttycolour_init(struct ttycolour_state *tc)
118   { tc->attr = 0; }
119
120 /* --- @ttycolour_setattr@ --- *
121  *
122  * Arguments:   @const struct gprintf_ops *ops@ = formatting operations
123  *              @void *go@ = output sink
124  *              @struct ttycolour_state *tc
125  *              @ttycolour_attr attr@ = attribute code to set
126  *
127  * Returns:     Zero on success, %$-1$% on error.
128  *
129  * Use:         Send a control sequence to the output stream so that
130  *              subsequent text is printed with the given attributes.
131  *
132  *              Some effort is taken to avoid unnecessary control sequences.
133  *              In particular, if @attr@ matches the current terminal
134  *              settings already, then nothing is written.
135  */
136
137 int ttycolour_setattr(const struct gprintf_ops *gops, void *go,
138                       struct ttycolour_state *tc, ttycolour_attr attr)
139 {
140   unsigned diff = tc->attr ^ attr;
141   unsigned f = 0;
142
143 #define f_semi 1u
144
145 #define PUT_SEMI do {                                                   \
146   if (!(f&f_semi)) f |= f_semi;                                         \
147   else if (gops->putch(go, ';') < 0) return (-1);                       \
148 } while (0)
149
150 #define SET_COLOUR(norm, bright, colour) do {                           \
151   unsigned char _col = (colour);                                        \
152                                                                         \
153   PUT_SEMI;                                                             \
154   gprintf(gops, go, "%d",                                               \
155           (_col&TCCF_BRIGHT ? (bright) : (norm)) + (_col&TCCF_RGBMASK)); \
156 } while (0)
157
158   /* If there's nothing to do, we might as well stop now. */
159   if (!diff) return (0);
160
161   /* Start on the control command. */
162   if (gops->putm(go, "\x1b[", 2) < 0) return (-1);
163
164   /* Change the boldness if necessary. */
165   if (diff&TCAF_BOLD) {
166     PUT_SEMI;
167     if (attr&TCAF_BOLD) { if (gops->putch(go, '1') < 0) return (-1); }
168     else { diff = tc->attr; if (gops->putch(go, '0') < 0) return (-1); }
169   }
170
171   /* Change the foreground colour if necessary. */
172   if (diff&(TCAF_FG | TCAF_FGMASK)) {
173     if (attr&TCAF_FG)
174       SET_COLOUR(30, 90, (attr&TCAF_FGMASK) >> TCAF_FGSHIFT);
175     else
176       { PUT_SEMI; if (gops->putm(go, "39", 2) < 0) return (-1); }
177   }
178
179   /* Change the background colour if necessary. */
180   if (diff&(TCAF_BG | TCAF_BGMASK)) {
181     if (attr&TCAF_BG)
182       SET_COLOUR(40, 100, (attr&TCAF_BGMASK) >> TCAF_BGSHIFT);
183     else
184       { PUT_SEMI; if (gops->putm(go, "49", 2) < 0) return (-1); }
185   }
186
187   /* Terminate the control command and save the new attributes. */
188   if (gops->putch(go, 'm') < 0) return (-1);
189   tc->attr = attr; return (0);
190
191 #undef f_semi
192 #undef PUT_SEMI
193 #undef SET_COLOUR
194 }
195
196 /*----- That's all, folks -------------------------------------------------*/