Do some error checking in puthead()
[s-mailx.git] / colour.c
blob13766e78ce301ebff9c3fde58145796f43ffd86a
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ `(un)?colour' commands, and anything working with it.
4 * Copyright (c) 2014 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #undef n_FILE
19 #define n_FILE colour
21 #ifndef HAVE_AMALGAMATION
22 # include "nail.h"
23 #endif
25 EMPTY_FILE()
26 #ifdef HAVE_COLOUR
28 /* Not needed publically, but extends a set from nail.h */
29 #define n_COLOUR_TAG_ERR ((char*)-1)
30 #define a_COLOUR_TAG_IS_SPECIAL(P) (PTR2SIZE(P) >= PTR2SIZE(-3))
32 enum a_colour_type{
33 a_COLOUR_T_256,
34 a_COLOUR_T_8,
35 a_COLOUR_T_1,
36 a_COLOUR_T_NONE, /* EQ largest real colour + 1! */
37 a_COLOUR_T_UNKNOWN /* Initial value: real one queried before 1st use */
40 enum a_colour_tag_type{
41 a_COLOUR_TT_NONE,
42 a_COLOUR_TT_DOT = 1<<0, /* "dot" */
43 a_COLOUR_TT_OLDER = 1<<1, /* "older" */
44 a_COLOUR_TT_HEADERS = 1<<2, /* Comma-separated list of headers allowed */
46 a_COLOUR_TT_SUM = a_COLOUR_TT_DOT | a_COLOUR_TT_OLDER,
47 a_COLOUR_TT_VIEW = a_COLOUR_TT_HEADERS
50 struct a_colour_type_map{
51 ui8_t ctm_type; /* a_colour_type */
52 char ctm_name[7];
55 struct a_colour_map_id{
56 ui8_t cmi_ctx; /* enum n_colour_ctx */
57 ui8_t cmi_id; /* enum n_colour_id */
58 ui8_t cmi_tt; /* enum a_colour_tag_type */
59 char const cmi_name[13];
61 n_CTA(n__COLOUR_IDS <= UI8_MAX, "Enumeration exceeds storage datatype");
63 struct n_colour_pen{
64 struct str cp_dat; /* Pre-prepared ISO 6429 escape sequence */
67 struct a_colour_map /* : public n_colour_pen */{
68 struct n_colour_pen cm_pen; /* Points into .cm_buf */
69 struct a_colour_map *cm_next;
70 char const *cm_tag; /* Colour tag or NULL for default (last) */
71 struct a_colour_map_id const *cm_cmi;
72 #ifdef HAVE_REGEX
73 regex_t *cm_regex;
74 #endif
75 ui32_t cm_refcnt; /* Beware of reference drops in recursions */
76 ui32_t cm_user_off; /* User input offset in .cm_buf */
77 char cm_buf[n_VFIELD_SIZE(0)];
80 struct a_colour_g{
81 ui8_t cg_type; /* a_colour_type */
82 /* TODO cg_has_env not used, we have to go for PS_COLOUR_ACTIVE */
83 bool_t cg_has_env;
84 ui8_t cg_ctx; /* If .cg_has_env, enum n_colour_ctx */
85 ui8_t __cg_pad[5];
86 struct a_colour_map *cg_active; /* The currently active colour */
87 struct n_colour_pen cg_reset; /* The reset sequence */
88 struct a_colour_map
89 *cg_maps[a_COLOUR_T_NONE][n__COLOUR_CTX_MAX][n__COLOUR_IDS];
90 char cg_reset_buf[n_ALIGN_SMALL(sizeof("\033[0m"))];
93 /* TODO The colour environment simply should be a pointer into an
94 * TODO carrier structure in equal spirit to the fio.c stack, which gets
95 * TODO created for each execute() cycle (long in TODO), and carries along
96 * TODO all the information, memory allocations and also output (filter)
97 * TODO chains, so that we could actually decide whether we could simply
98 * TODO suspend output for a chain, need to place reset sequences, etc.
99 * TODO For now, since we have no such carrier to know where colour
100 * TODO sequences have to be written, creating a colour environment requires
101 * TODO that the current colour state is "reset", because we wouldn't know
102 * TODO where to place reset sequences and ditto, reestablish colour.
103 * TODO This should be no problem in practice, however */
104 struct a_colour_env{
105 struct a_colour_env *ce_last;
106 ui8_t ce_ctx; /* enum n_colour_ctx active upon switch */
107 bool_t ce_is_active; /* Was colour active in outer level? */
108 ui8_t __ce_pad[6];
111 /* C99: use [INDEX]={} */
112 /* */
113 n_CTA(a_COLOUR_T_256 == 0, "Unexpected value of constant");
114 n_CTA(a_COLOUR_T_8 == 1, "Unexpected value of constant");
115 n_CTA(a_COLOUR_T_1 == 2, "Unexpected value of constant");
116 static char const a_colour_types[][8] = {"256", "iso", "mono"};
118 static struct a_colour_type_map const a_colour_type_maps[] = {
119 {a_COLOUR_T_256, "256"},
120 {a_COLOUR_T_8, "8"}, {a_COLOUR_T_8, "iso"}, {a_COLOUR_T_8, "ansi"},
121 {a_COLOUR_T_1, "1"}, {a_COLOUR_T_1, "mono"}
124 n_CTA(n_COLOUR_CTX_SUM == 0, "Unexpected value of constant");
125 n_CTA(n_COLOUR_CTX_VIEW == 1, "Unexpected value of constant");
126 n_CTA(n_COLOUR_CTX_MLE == 2, "Unexpected value of constant");
127 static char const a_colour_ctx_prefixes[n__COLOUR_CTX_MAX][8] = {
128 "sum-", "view-", "mle-"
131 static struct a_colour_map_id const
132 a_colour_map_ids[n__COLOUR_CTX_MAX][n__COLOUR_IDS] = {{
133 {n_COLOUR_CTX_SUM, n_COLOUR_ID_SUM_DOTMARK, a_COLOUR_TT_SUM, "dotmark"},
134 {n_COLOUR_CTX_SUM, n_COLOUR_ID_SUM_HEADER, a_COLOUR_TT_SUM, "header"},
135 {n_COLOUR_CTX_SUM, n_COLOUR_ID_SUM_THREAD, a_COLOUR_TT_SUM, "thread"},
136 }, {
137 {n_COLOUR_CTX_VIEW, n_COLOUR_ID_VIEW_FROM_, a_COLOUR_TT_NONE, "from_"},
138 {n_COLOUR_CTX_VIEW, n_COLOUR_ID_VIEW_HEADER, a_COLOUR_TT_VIEW, "header"},
139 {n_COLOUR_CTX_VIEW, n_COLOUR_ID_VIEW_MSGINFO, a_COLOUR_TT_NONE, "msginfo"},
140 {n_COLOUR_CTX_VIEW, n_COLOUR_ID_VIEW_PARTINFO, a_COLOUR_TT_NONE, "partinfo"},
141 }, {
142 {n_COLOUR_CTX_MLE, n_COLOUR_ID_MLE_POSITION, a_COLOUR_TT_NONE, "position"},
143 {n_COLOUR_CTX_MLE, n_COLOUR_ID_MLE_PROMPT, a_COLOUR_TT_NONE, "prompt"},
145 #define a_COLOUR_MAP_SHOW_FIELDWIDTH \
146 (int)(sizeof("view-")-1 + sizeof("partinfo")-1)
148 static struct a_colour_g *a_colour_g;
149 static struct a_colour_env *a_colour_env;
151 static void a_colour_init(void);
152 DBG( static void a_colour_atexit(void); )
154 /* Find the type or -1 */
155 static enum a_colour_type a_colour_type_find(char const *name);
157 /* `(un)?colour' implementations */
158 static bool_t a_colour_mux(char **argv);
159 static bool_t a_colour_unmux(char **argv);
161 static bool_t a_colour__show(enum a_colour_type ct);
162 /* (regexpp may be NULL) */
163 static char const *a_colour__tag_identify(struct a_colour_map_id const *cmip,
164 char const *ctag, void **regexpp);
166 /* Try to find a mapping identity for user given slotname */
167 static struct a_colour_map_id const *a_colour_map_id_find(char const *slotname);
169 /* Find an existing mapping for the given combination */
170 static struct a_colour_map *a_colour_map_find(enum n_colour_id cid,
171 char const *ctag);
173 /* In-/Decrement reference counter, destroy if counts gets zero */
174 #define a_colour_map_ref(SELF) do{ ++(SELF)->cm_refcnt; }while(0)
175 static void a_colour_map_unref(struct a_colour_map *self);
177 /* Create an ISO 6429 (ECMA-48/ANSI) terminal control escape sequence from user
178 * input spec, store it or on error message in *store */
179 static bool_t a_colour_iso6429(enum a_colour_type ct, char **store,
180 char const *spec);
182 static void
183 a_colour_init(void){
184 NYD2_ENTER;
186 a_colour_g = scalloc(1, sizeof *a_colour_g);
188 memcpy(a_colour_g->cg_reset.cp_dat.s = a_colour_g->cg_reset_buf, "\033[0m",
189 a_colour_g->cg_reset.cp_dat.l = sizeof("\033[0m") -1); /* (calloc) */
190 a_colour_g->cg_type = a_COLOUR_T_UNKNOWN;
191 DBG( atexit(&a_colour_atexit); ) /* TODO prog-global atexit event */
192 NYD2_LEAVE;
195 #ifdef HAVE_DEBUG
196 static void
197 a_colour_atexit(void){
198 NYD_ENTER;
199 if(a_colour_env != NULL)
200 n_colour_env_pop(TRU1);
201 free(a_colour_g);
202 a_colour_g = NULL;
203 NYD_LEAVE;
205 #endif
207 static enum a_colour_type
208 a_colour_type_find(char const *name){
209 struct a_colour_type_map const *ctmp;
210 enum a_colour_type rv;
211 NYD2_ENTER;
213 ctmp = a_colour_type_maps;
214 do if(!asccasecmp(ctmp->ctm_name, name)){
215 rv = ctmp->ctm_type;
216 goto jleave;
217 }while(PTRCMP(++ctmp, !=, a_colour_type_maps + n_NELEM(a_colour_type_maps)));
219 rv = (enum a_colour_type)-1;
220 jleave:
221 NYD2_LEAVE;
222 return rv;
225 static bool_t
226 a_colour_mux(char **argv){
227 void *regexp;
228 char const *mapname, *ctag;
229 struct a_colour_map **cmap, *blcmp, *lcmp, *cmp;
230 struct a_colour_map_id const *cmip;
231 bool_t rv;
232 enum a_colour_type ct;
233 NYD2_ENTER;
235 if((ct = a_colour_type_find(*argv++)) == (enum a_colour_type)-1 &&
236 (*argv != NULL || !n_is_all_or_aster(argv[-1]))){
237 n_err(_("`colour': invalid colour type %s\n"),
238 n_shexp_quote_cp(argv[-1], FAL0));
239 rv = FAL0;
240 goto jleave;
243 if(a_colour_g == NULL)
244 a_colour_init();
246 if(*argv == NULL){
247 rv = a_colour__show(ct);
248 goto jleave;
251 rv = FAL0;
252 regexp = NULL;
254 if((cmip = a_colour_map_id_find(mapname = argv[0])) == NULL){
255 n_err(_("`colour': non-existing mapping: %s\n"),
256 n_shexp_quote_cp(mapname, FAL0));
257 goto jleave;
260 if(argv[1] == NULL){
261 n_err(_("`colour': %s: missing attribute argument\n"),
262 n_shexp_quote_cp(mapname, FAL0));
263 goto jleave;
266 /* Check whether preconditions are at all allowed, verify them as far as
267 * possible as necessary. For shell_quote() simplicity let's just ignore an
268 * empty precondition */
269 if((ctag = argv[2]) != NULL && *ctag != '\0'){
270 char const *xtag;
272 if(cmip->cmi_tt == a_COLOUR_TT_NONE){
273 n_err(_("`colour': %s doesn't support preconditions\n"),
274 n_shexp_quote_cp(mapname, FAL0));
275 goto jleave;
276 }else if((xtag = a_colour__tag_identify(cmip, ctag, &regexp)) ==
277 n_COLOUR_TAG_ERR){
278 /* I18N: ..of colour mapping */
279 n_err(_("`colour': %s: invalid precondition: %s\n"),
280 n_shexp_quote_cp(mapname, FAL0), n_shexp_quote_cp(ctag, FAL0));
281 goto jleave;
283 ctag = xtag;
286 /* At this time we have all the information to be able to query whether such
287 * a mapping is yet established. If so, destroy it */
288 for(blcmp = lcmp = NULL,
289 cmp = *(cmap =
290 &a_colour_g->cg_maps[ct][cmip->cmi_ctx][cmip->cmi_id]);
291 cmp != NULL; blcmp = lcmp, lcmp = cmp, cmp = cmp->cm_next){
292 char const *xctag = cmp->cm_tag;
294 if(xctag == ctag ||
295 (ctag != NULL && !a_COLOUR_TAG_IS_SPECIAL(ctag) &&
296 xctag != NULL && !a_COLOUR_TAG_IS_SPECIAL(xctag) &&
297 !strcmp(xctag, ctag))){
298 if(lcmp == NULL)
299 *cmap = cmp->cm_next;
300 else
301 lcmp->cm_next = cmp->cm_next;
302 a_colour_map_unref(cmp);
303 break;
307 /* Create mapping */
308 /* C99 */{
309 size_t tl, ul, cl;
310 char *bp, *cp;
312 if(!a_colour_iso6429(ct, &cp, argv[1])){
313 /* I18N: colour command: mapping: error message: user argument */
314 n_err(_("`colour': %s: %s: %s\n"), n_shexp_quote_cp(mapname, FAL0),
315 cp, n_shexp_quote_cp(argv[1], FAL0));
316 goto jleave;
319 tl = (ctag != NULL && !a_COLOUR_TAG_IS_SPECIAL(ctag)) ? strlen(ctag) : 0;
320 cmp = smalloc(sizeof(*cmp) -
321 n_VFIELD_SIZEOF(struct a_colour_map, cm_buf) +
322 tl +1 + (ul = strlen(argv[1])) +1 + (cl = strlen(cp)) +1);
324 /* .cm_buf stuff */
325 cmp->cm_pen.cp_dat.s = bp = cmp->cm_buf;
326 cmp->cm_pen.cp_dat.l = cl;
327 memcpy(bp, cp, ++cl);
328 bp += cl;
330 cmp->cm_user_off = (ui32_t)PTR2SIZE(bp - cmp->cm_buf);
331 memcpy(bp, argv[1], ++ul);
332 bp += ul;
334 if(tl > 0){
335 cmp->cm_tag = bp;
336 memcpy(bp, ctag, ++tl);
337 /*bp += tl;*/
338 }else
339 cmp->cm_tag = ctag;
341 /* Non-buf stuff; default mapping */
342 if(lcmp != NULL){
343 /* Default mappings must be last */
344 if(ctag == NULL){
345 while(lcmp->cm_next != NULL)
346 lcmp = lcmp->cm_next;
347 }else if(lcmp->cm_next == NULL && lcmp->cm_tag == NULL){
348 if((lcmp = blcmp) == NULL)
349 goto jlinkhead;
351 cmp->cm_next = lcmp->cm_next;
352 lcmp->cm_next = cmp;
353 }else{
354 jlinkhead:
355 cmp->cm_next = *cmap;
356 *cmap = cmp;
358 cmp->cm_cmi = cmip;
359 #ifdef HAVE_REGEX
360 cmp->cm_regex = regexp;
361 #endif
362 cmp->cm_refcnt = 0;
363 a_colour_map_ref(cmp);
365 rv = TRU1;
366 jleave:
367 NYD2_LEAVE;
368 return rv;
371 static bool_t
372 a_colour_unmux(char **argv){
373 char const *mapname, *ctag, *xtag;
374 struct a_colour_map **cmap, *lcmp, *cmp;
375 struct a_colour_map_id const *cmip;
376 enum a_colour_type ct;
377 bool_t aster, rv;
378 NYD2_ENTER;
380 rv = TRU1;
381 aster = FAL0;
383 if((ct = a_colour_type_find(*argv++)) == (enum a_colour_type)-1){
384 if(!n_is_all_or_aster(argv[-1])){
385 n_err(_("`uncolour': invalid colour type %s\n"),
386 n_shexp_quote_cp(argv[-1], FAL0));
387 rv = FAL0;
388 goto j_leave;
390 aster = TRU1;
391 ct = 0;
394 mapname = argv[0];
395 ctag = (mapname != NULL) ? argv[1] : mapname;
397 if(a_colour_g == NULL)
398 goto jemap;
400 /* Delete anything? */
401 jredo:
402 if(ctag == NULL && mapname[0] == '*' && mapname[1] == '\0'){
403 size_t i1, i2;
404 struct a_colour_map *tmp;
406 for(i1 = 0; i1 < n__COLOUR_CTX_MAX; ++i1)
407 for(i2 = 0; i2 < n__COLOUR_IDS; ++i2)
408 for(cmp = *(cmap = &a_colour_g->cg_maps[ct][i1][i2]), *cmap = NULL;
409 cmp != NULL;){
410 tmp = cmp;
411 cmp = cmp->cm_next;
412 a_colour_map_unref(tmp);
414 }else{
415 if(a_colour_g == NULL){
416 rv = FAL0;
417 jemap:
418 /* I18N: colour command, mapping and precondition (option in quotes) */
419 n_err(_("`uncolour': non-existing mapping: %s%s%s\n"),
420 n_shexp_quote_cp(mapname, FAL0), (ctag == NULL ? n_empty : " "),
421 (ctag == NULL ? n_empty : n_shexp_quote_cp(ctag, FAL0)));
422 goto jleave;
425 if((cmip = a_colour_map_id_find(mapname)) == NULL){
426 rv = FAL0;
427 goto jemap;
430 if((xtag = ctag) != NULL){
431 if(cmip->cmi_tt == a_COLOUR_TT_NONE){
432 n_err(_("`uncolour': %s doesn't support preconditions\n"),
433 n_shexp_quote_cp(mapname, FAL0));
434 rv = FAL0;
435 goto jleave;
436 }else if((xtag = a_colour__tag_identify(cmip, ctag, NULL)) ==
437 n_COLOUR_TAG_ERR){
438 n_err(_("`uncolour': %s: invalid precondition: %s\n"),
439 n_shexp_quote_cp(mapname, FAL0), n_shexp_quote_cp(ctag, FAL0));
440 rv = FAL0;
441 goto jleave;
443 /* (Improve user experience) */
444 if(xtag != NULL && !a_COLOUR_TAG_IS_SPECIAL(xtag))
445 ctag = xtag;
448 lcmp = NULL;
449 cmp = *(cmap = &a_colour_g->cg_maps[ct][cmip->cmi_ctx][cmip->cmi_id]);
450 for(;;){
451 char const *xctag;
453 if(cmp == NULL){
454 rv = FAL0;
455 goto jemap;
457 if((xctag = cmp->cm_tag) == ctag)
458 break;
459 if(ctag != NULL && !a_COLOUR_TAG_IS_SPECIAL(ctag) &&
460 xctag != NULL && !a_COLOUR_TAG_IS_SPECIAL(xctag) &&
461 !strcmp(xctag, ctag))
462 break;
463 lcmp = cmp;
464 cmp = cmp->cm_next;
467 if(lcmp == NULL)
468 *cmap = cmp->cm_next;
469 else
470 lcmp->cm_next = cmp->cm_next;
471 a_colour_map_unref(cmp);
474 jleave:
475 if(aster && ++ct != a_COLOUR_T_NONE)
476 goto jredo;
477 j_leave:
478 NYD2_LEAVE;
479 return rv;
482 static bool_t
483 a_colour__show(enum a_colour_type ct){
484 struct a_colour_map *cmp;
485 size_t i1, i2;
486 bool_t rv;
487 NYD2_ENTER;
489 /* Show all possible types? */
490 if((rv = (ct == (enum a_colour_type)-1 ? TRU1 : FAL0)))
491 ct = 0;
492 jredo:
493 for(i1 = 0; i1 < n__COLOUR_CTX_MAX; ++i1)
494 for(i2 = 0; i2 < n__COLOUR_IDS; ++i2){
495 if((cmp = a_colour_g->cg_maps[ct][i1][i2]) == NULL)
496 continue;
498 while(cmp != NULL){
499 char const *tagann, *tag;
501 tagann = n_empty;
502 if((tag = cmp->cm_tag) == NULL)
503 tag = n_empty;
504 else if(tag == n_COLOUR_TAG_SUM_DOT)
505 tag = "dot";
506 else if(tag == n_COLOUR_TAG_SUM_OLDER)
507 tag = "older";
508 #ifdef HAVE_REGEX
509 else if(cmp->cm_regex != NULL)
510 tagann = "[rx] ";
511 #endif
512 printf("colour %s %-*s %s %s%s\n",
513 a_colour_types[ct], a_COLOUR_MAP_SHOW_FIELDWIDTH,
514 savecat(a_colour_ctx_prefixes[i1],
515 a_colour_map_ids[i1][i2].cmi_name),
516 (char const*)cmp->cm_buf + cmp->cm_user_off,
517 tagann, n_shexp_quote_cp(tag, TRU1));
518 cmp = cmp->cm_next;
522 if(rv && ++ct != a_COLOUR_T_NONE)
523 goto jredo;
524 rv = TRU1;
525 NYD2_LEAVE;
526 return rv;
529 static char const *
530 a_colour__tag_identify(struct a_colour_map_id const *cmip, char const *ctag,
531 void **regexpp){
532 NYD2_ENTER;
533 n_UNUSED(regexpp);
535 if((cmip->cmi_tt & a_COLOUR_TT_DOT) && !asccasecmp(ctag, "dot"))
536 ctag = n_COLOUR_TAG_SUM_DOT;
537 else if((cmip->cmi_tt & a_COLOUR_TT_OLDER) && !asccasecmp(ctag, "older"))
538 ctag = n_COLOUR_TAG_SUM_OLDER;
539 else if(cmip->cmi_tt & a_COLOUR_TT_HEADERS){
540 char *cp, c;
541 size_t i;
543 /* Can this be a valid list of headers? However, with regular expressions
544 * simply use the input as such if it appears to be a regex */
545 #ifdef HAVE_REGEX
546 if(is_maybe_regex(ctag)){
547 if(regexpp != NULL && regcomp(*regexpp = smalloc(sizeof(regex_t)),
548 ctag, REG_EXTENDED | REG_ICASE | REG_NOSUB)){
549 free(*regexpp);
550 goto jetag;
552 }else
553 #endif
555 /* Normalize to lowercase and strip any whitespace before use */
556 i = strlen(ctag);
557 cp = salloc(i +1);
559 for(i = 0; (c = *ctag++) != '\0';){
560 bool_t isblspc = blankspacechar(c);
562 if(!isblspc && !alnumchar(c) && c != '-' && c != ',')
563 goto jetag;
564 /* Since we compare header names as they come from the message this
565 * lowercasing is however redundant: we need to asccasecmp() them */
566 if(!isblspc)
567 cp[i++] = lowerconv(c);
569 cp[i] = '\0';
570 ctag = cp;
572 }else
573 jetag:
574 ctag = n_COLOUR_TAG_ERR;
575 NYD2_LEAVE;
576 return ctag;
579 static struct a_colour_map_id const *
580 a_colour_map_id_find(char const *cp){
581 size_t i;
582 struct a_colour_map_id const (*cmip)[n__COLOUR_IDS], *rv;
583 NYD2_ENTER;
585 rv = NULL;
587 for(i = 0;; ++i){
588 if(i == n__COLOUR_IDS)
589 goto jleave;
590 else{
591 size_t j = strlen(a_colour_ctx_prefixes[i]);
592 if(!ascncasecmp(cp, a_colour_ctx_prefixes[i], j)){
593 cp += j;
594 break;
598 cmip = &a_colour_map_ids[i];
600 for(i = 0;; ++i){
601 if(i == n__COLOUR_IDS || (rv = &(*cmip)[i])->cmi_name[0] == '\0'){
602 rv = NULL;
603 break;
605 if(!asccasecmp(cp, rv->cmi_name))
606 break;
608 jleave:
609 NYD2_LEAVE;
610 return rv;
613 static struct a_colour_map *
614 a_colour_map_find(enum n_colour_id cid, char const *ctag){
615 struct a_colour_map *cmp;
616 NYD2_ENTER;
618 cmp = a_colour_g->cg_maps[a_colour_g->cg_type][a_colour_g->cg_ctx][cid];
619 for(; cmp != NULL; cmp = cmp->cm_next){
620 char const *xtag = cmp->cm_tag;
622 if(xtag == ctag)
623 break;
624 if(xtag == NULL)
625 break;
626 if(ctag == NULL || a_COLOUR_TAG_IS_SPECIAL(ctag))
627 continue;
628 #ifdef HAVE_REGEX
629 if(cmp->cm_regex != NULL){
630 if(regexec(cmp->cm_regex, ctag, 0,NULL, 0) != REG_NOMATCH)
631 break;
632 }else
633 #endif
634 if(cmp->cm_cmi->cmi_tt & a_COLOUR_TT_HEADERS){
635 char *hlist = savestr(xtag), *cp;
637 while((cp = n_strsep(&hlist, ',', TRU1)) != NULL){
638 if(!asccasecmp(cp, ctag))
639 break;
641 if(cp != NULL)
642 break;
645 NYD2_LEAVE;
646 return cmp;
649 static void
650 a_colour_map_unref(struct a_colour_map *self){
651 NYD2_ENTER;
652 if(--self->cm_refcnt == 0){
653 #ifdef HAVE_REGEX
654 if(self->cm_regex != NULL){
655 regfree(self->cm_regex);
656 free(self->cm_regex);
658 #endif
659 free(self);
661 NYD2_LEAVE;
664 static bool_t
665 a_colour_iso6429(enum a_colour_type ct, char **store, char const *spec){
666 struct isodesc{
667 char id_name[15];
668 char id_modc;
669 } const fta[] = {
670 {"bold", '1'}, {"underline", '4'}, {"reverse", '7'}
671 }, ca[] = {
672 {"black", '0'}, {"red", '1'}, {"green", '2'}, {"brown", '3'},
673 {"blue", '4'}, {"magenta", '5'}, {"cyan", '6'}, {"white", '7'}
674 }, *idp;
675 char *xspec, *cp, fg[3], cfg[2 + 2*sizeof("255")];
676 ui8_t ftno_base, ftno;
677 bool_t rv;
678 NYD_ENTER;
680 rv = FAL0;
681 /* 0/1 indicate usage, thereafter possibly 256 color sequences */
682 cfg[0] = cfg[1] = 0;
684 /* Since we use salloc(), reuse the n_strsep() buffer also for the return
685 * value, ensure we have enough room for that */
686 /* C99 */{
687 size_t i = strlen(spec) +1;
688 xspec = salloc(n_MAX(i, sizeof("\033[1;4;7;38;5;255;48;5;255m")));
689 memcpy(xspec, spec, i);
690 spec = xspec;
693 /* Iterate over the colour spec */
694 ftno = 0;
695 while((cp = n_strsep(&xspec, ',', TRU1)) != NULL){
696 char *y, *x = strchr(cp, '=');
697 if(x == NULL){
698 jbail:
699 *store = n_UNCONST(_("invalid attribute list"));
700 goto jleave;
702 *x++ = '\0';
704 if(!asccasecmp(cp, "ft")){
705 if(!asccasecmp(x, "inverse")){
706 OBSOLETE(_("please use reverse for ft= fonts, not inverse"));
707 x = n_UNCONST("reverse");
709 for(idp = fta;; ++idp)
710 if(idp == fta + n_NELEM(fta)){
711 *store = n_UNCONST(_("invalid font attribute"));
712 goto jleave;
713 }else if(!asccasecmp(x, idp->id_name)){
714 if(ftno < n_NELEM(fg))
715 fg[ftno++] = idp->id_modc;
716 else{
717 *store = n_UNCONST(_("too many font attributes"));
718 goto jleave;
720 break;
722 }else if(!asccasecmp(cp, "fg")){
723 y = cfg + 0;
724 goto jiter_colour;
725 }else if(!asccasecmp(cp, "bg")){
726 y = cfg + 1;
727 jiter_colour:
728 if(ct == a_COLOUR_T_1){
729 *store = n_UNCONST(_("colours are not allowed"));
730 goto jleave;
732 /* Maybe 256 color spec */
733 if(digitchar(x[0])){
734 sl_i xv;
736 if(ct == a_COLOUR_T_8){
737 *store = n_UNCONST(_("invalid colour for 8-colour mode"));
738 goto jleave;
741 xv = strtol(x, &cp, 10);
742 if(xv < 0 || xv > 255 || *cp != '\0' || PTRCMP(&x[3], <, cp)){
743 *store = n_UNCONST(_("invalid 256-colour specification"));
744 goto jleave;
746 y[0] = 5;
747 memcpy((y == &cfg[0] ? y + 2 : y + 1 + sizeof("255")), x,
748 (x[1] == '\0' ? 2 : (x[2] == '\0' ? 3 : 4)));
749 }else for(idp = ca;; ++idp)
750 if(idp == ca + n_NELEM(ca)){
751 *store = n_UNCONST(_("invalid colour attribute"));
752 goto jleave;
753 }else if(!asccasecmp(x, idp->id_name)){
754 y[0] = 1;
755 y[2] = idp->id_modc;
756 break;
758 }else
759 goto jbail;
762 /* Restore our salloc() buffer, create return value */
763 xspec = n_UNCONST(spec);
764 if(ftno > 0 || cfg[0] || cfg[1]){ /* TODO unite/share colour setters */
765 xspec[0] = '\033';
766 xspec[1] = '[';
767 xspec += 2;
769 for(ftno_base = ftno; ftno > 0;){
770 if(ftno-- != ftno_base)
771 *xspec++ = ';';
772 *xspec++ = fg[ftno];
775 if(cfg[0]){
776 if(ftno_base > 0)
777 *xspec++ = ';';
778 xspec[0] = '3';
779 if(cfg[0] == 1){
780 xspec[1] = cfg[2];
781 xspec += 2;
782 }else{
783 memcpy(xspec + 1, "8;5;", 4);
784 xspec += 5;
785 for(ftno = 2; cfg[ftno] != '\0'; ++ftno)
786 *xspec++ = cfg[ftno];
790 if(cfg[1]){
791 if(ftno_base > 0 || cfg[0])
792 *xspec++ = ';';
793 xspec[0] = '4';
794 if(cfg[1] == 1){
795 xspec[1] = cfg[3];
796 xspec += 2;
797 }else{
798 memcpy(xspec + 1, "8;5;", 4);
799 xspec += 5;
800 for(ftno = 2 + sizeof("255"); cfg[ftno] != '\0'; ++ftno)
801 *xspec++ = cfg[ftno];
805 *xspec++ = 'm';
807 *xspec = '\0';
808 *store = n_UNCONST(spec);
809 rv = TRU1;
810 jleave:
811 NYD_LEAVE;
812 return rv;
815 FL int
816 c_colour(void *v){
817 int rv;
818 NYD_ENTER;
820 rv = !a_colour_mux(v);
821 NYD_LEAVE;
822 return rv;
825 FL int
826 c_uncolour(void *v){
827 int rv;
828 NYD_ENTER;
830 rv = !a_colour_unmux(v);
831 NYD_LEAVE;
832 return rv;
835 FL void
836 n_colour_env_create(enum n_colour_ctx cctx, bool_t pager_used){
837 NYD_ENTER;
838 if(!(options & OPT_INTERACTIVE))
839 goto jleave;
841 if (ok_blook(colour_disable) || (pager_used && !ok_blook(colour_pager))){
842 n_colour_env_push(); /* FIXME lex.c only pops (always env */
843 goto jleave;
846 if(n_UNLIKELY(a_colour_g == NULL))
847 a_colour_init();
849 if(n_UNLIKELY(a_colour_g->cg_type == a_COLOUR_T_UNKNOWN)){
850 struct n_termcap_value tv;
852 if(!n_termcap_query(n_TERMCAP_QUERY_colors, &tv)){
853 a_colour_g->cg_type = a_COLOUR_T_NONE;
854 goto jleave;
855 }else
856 switch(tv.tv_data.tvd_numeric){
857 case 256: a_colour_g->cg_type = a_COLOUR_T_256; break;
858 case 8: a_colour_g->cg_type = a_COLOUR_T_8; break;
859 case 1: a_colour_g->cg_type = a_COLOUR_T_1; break;
860 default:
861 if(options & OPT_D_V)
862 n_err(_("Ignoring unsupported termcap entry for Co(lors)\n"));
863 /* FALLTHRU */
864 case 0:
865 a_colour_g->cg_type = a_COLOUR_T_NONE;
866 goto jleave;
870 if(a_colour_g->cg_type == a_COLOUR_T_NONE)
871 goto jleave;
873 a_colour_g->cg_ctx = cctx;
874 a_colour_g->cg_active = NULL;
875 pstate |= PS_COLOUR_ACTIVE;
876 jleave:
877 NYD_LEAVE;
880 FL void
881 n_colour_env_push(void){
882 struct a_colour_env *cep;
883 NYD_ENTER;
885 if(!(options & OPT_INTERACTIVE))
886 goto jleave;
888 cep = smalloc(sizeof *cep);
889 cep->ce_last = a_colour_env;
890 if(a_colour_g != NULL){
891 cep->ce_ctx = a_colour_g->cg_ctx;
892 a_colour_g->cg_active = NULL;
894 cep->ce_is_active = ((pstate & PS_COLOUR_ACTIVE) != 0);
895 a_colour_env = cep;
897 pstate &= ~PS_COLOUR_ACTIVE;
898 jleave:
899 NYD_LEAVE;
902 FL void
903 n_colour_env_pop(bool_t any_env_till_root){
904 NYD_ENTER;
905 if(!(options & OPT_INTERACTIVE))
906 goto jleave;
908 while(a_colour_env != NULL){
909 struct a_colour_env *cep;
911 if((cep = a_colour_env)->ce_is_active)
912 pstate |= PS_COLOUR_ACTIVE;
913 else
914 pstate &= ~PS_COLOUR_ACTIVE;
916 if(a_colour_g != NULL){
917 a_colour_g->cg_active = NULL;
918 a_colour_g->cg_ctx = cep->ce_ctx;
920 a_colour_env = cep->ce_last;
922 free(cep);
923 if(!any_env_till_root)
924 break;
927 if(any_env_till_root && a_colour_g != NULL && (pstate & PS_COLOUR_ACTIVE)){
928 pstate &= ~PS_COLOUR_ACTIVE;
929 a_colour_g->cg_active = NULL;
931 jleave:
932 NYD_LEAVE;
935 FL void
936 n_colour_env_gut(FILE *fp){
937 NYD_ENTER;
938 if((options & OPT_INTERACTIVE) && (pstate & PS_COLOUR_ACTIVE)){
939 pstate &= ~PS_COLOUR_ACTIVE;
941 if(a_colour_g->cg_active != NULL){
942 a_colour_g->cg_active = NULL;
943 if(fp != NULL)
944 fwrite(a_colour_g->cg_reset.cp_dat.s, a_colour_g->cg_reset.cp_dat.l,
945 1, fp);
948 NYD_LEAVE;
951 FL void
952 n_colour_put(FILE *fp, enum n_colour_id cid, char const *ctag){
953 NYD_ENTER;
954 if(pstate & PS_COLOUR_ACTIVE){
955 if(a_colour_g->cg_active != NULL)
956 fwrite(a_colour_g->cg_reset.cp_dat.s, a_colour_g->cg_reset.cp_dat.l, 1,
957 fp);
958 if((a_colour_g->cg_active = a_colour_map_find(cid, ctag)) != NULL)
959 fwrite(a_colour_g->cg_active->cm_pen.cp_dat.s,
960 a_colour_g->cg_active->cm_pen.cp_dat.l, 1, fp);
962 NYD_LEAVE;
965 FL void
966 n_colour_reset(FILE *fp){
967 NYD_ENTER;
968 if((pstate & PS_COLOUR_ACTIVE) && a_colour_g->cg_active != NULL){
969 a_colour_g->cg_active = NULL;
970 fwrite(a_colour_g->cg_reset.cp_dat.s, a_colour_g->cg_reset.cp_dat.l, 1,
971 fp);
973 NYD_LEAVE;
976 FL struct str const *
977 n_colour_reset_to_str(void){
978 struct str *rv;
979 NYD_ENTER;
981 if(pstate & PS_COLOUR_ACTIVE)
982 rv = &a_colour_g->cg_reset.cp_dat;
983 else
984 rv = NULL;
985 NYD_LEAVE;
986 return rv;
989 FL struct n_colour_pen *
990 n_colour_pen_create(enum n_colour_id cid, char const *ctag){
991 struct a_colour_map *cmp;
992 struct n_colour_pen *rv;
993 NYD_ENTER;
995 if((pstate & PS_COLOUR_ACTIVE) &&
996 (cmp = a_colour_map_find(cid, ctag)) != NULL){
997 union {void *vp; char *cp; struct n_colour_pen *cpp;} u;
998 u.vp = cmp;
999 rv = u.cpp;
1000 }else
1001 rv = NULL;
1002 NYD_LEAVE;
1003 return rv;
1006 FL void
1007 n_colour_pen_put(struct n_colour_pen *self, FILE *fp){
1008 NYD_ENTER;
1009 if(pstate & PS_COLOUR_ACTIVE){
1010 union {void *vp; char *cp; struct a_colour_map *cmp;} u;
1012 u.vp = self;
1013 if(u.cmp != a_colour_g->cg_active){
1014 if(a_colour_g->cg_active != NULL)
1015 fwrite(a_colour_g->cg_reset.cp_dat.s, a_colour_g->cg_reset.cp_dat.l,
1016 1, fp);
1017 if(u.cmp != NULL)
1018 fwrite(self->cp_dat.s, self->cp_dat.l, 1, fp);
1019 a_colour_g->cg_active = u.cmp;
1022 NYD_LEAVE;
1025 FL struct str const *
1026 n_colour_pen_to_str(struct n_colour_pen *self){
1027 struct str *rv;
1028 NYD_ENTER;
1030 if((pstate & PS_COLOUR_ACTIVE) && self != NULL)
1031 rv = &self->cp_dat;
1032 else
1033 rv = NULL;
1034 NYD_LEAVE;
1035 return rv;
1037 #endif /* HAVE_COLOUR */
1039 /* s-it-mode */