(BWDIC!) Allow `source' in `call'ed macros..
[s-mailx.git] / colour.c
blob21fab79eec1a5d63d4b4cf049cf8a250830b0d97
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ `(un)?colour' commands, and anything working with it.
4 * Copyright (c) 2014 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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[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[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 wether 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 + 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"), argv[-1]);
238 rv = FAL0;
239 goto jleave;
242 if(a_colour_g == NULL)
243 a_colour_init();
245 if(*argv == NULL){
246 rv = a_colour__show(ct);
247 goto jleave;
250 rv = FAL0;
251 regexp = NULL;
253 if((cmip = a_colour_map_id_find(mapname = argv[0])) == NULL){
254 n_err(_("`colour': non-existing mapping: \"%s\"\n"), mapname);
255 goto jleave;
258 if(argv[1] == NULL){
259 n_err(_("`colour': \"%s\": missing attribute argument\n"), mapname);
260 goto jleave;
263 /* Check wether preconditions are at all allowed, verify them as far as
264 * possible as necessary */
265 if((ctag = argv[2]) != NULL){
266 char const *xtag;
268 if(cmip->cmi_tt == a_COLOUR_TT_NONE){
269 n_err(_("`colour': \"%s\" doesn't support preconditions\n"), mapname);
270 goto jleave;
271 }else if((xtag = a_colour__tag_identify(cmip, ctag, &regexp)) ==
272 n_COLOUR_TAG_ERR){
273 /* I18N: ..of colour mapping */
274 n_err(_("`colour': \"%s\": invalid precondition: \"%s\"\n"),
275 mapname, ctag);
276 goto jleave;
278 ctag = xtag;
281 /* At this time we have all the information to be able to query wether such
282 * a mapping is yet established. If so, destroy it */
283 for(blcmp = lcmp = NULL,
284 cmp = *(cmap =
285 &a_colour_g->cg_maps[ct][cmip->cmi_ctx][cmip->cmi_id]);
286 cmp != NULL; blcmp = lcmp, lcmp = cmp, cmp = cmp->cm_next){
287 char const *xctag = cmp->cm_tag;
289 if(xctag == ctag ||
290 (ctag != NULL && !a_COLOUR_TAG_IS_SPECIAL(ctag) &&
291 xctag != NULL && !a_COLOUR_TAG_IS_SPECIAL(xctag) &&
292 !strcmp(xctag, ctag))){
293 if(lcmp == NULL)
294 *cmap = cmp->cm_next;
295 else
296 lcmp->cm_next = cmp->cm_next;
297 a_colour_map_unref(cmp);
298 break;
302 /* Create mapping */
303 /* C99 */{
304 size_t tl, ul, cl;
305 char *bp, *cp;
307 if(!a_colour_iso6429(ct, &cp, argv[1])){
308 /* I18N: colour command: mapping: error message: user argument */
309 n_err(_("`colour': \"%s\": %s: \"%s\"\n"), mapname, cp, argv[1]);
310 goto jleave;
313 tl = (ctag != NULL && !a_COLOUR_TAG_IS_SPECIAL(ctag)) ? strlen(ctag) : 0;
314 cmp = smalloc(sizeof(*cmp) - VFIELD_SIZEOF(struct a_colour_map, cm_buf) +
315 tl +1 + (ul = strlen(argv[1])) +1 + (cl = strlen(cp)) +1);
317 /* .cm_buf stuff */
318 cmp->cm_pen.cp_dat.s = bp = cmp->cm_buf;
319 cmp->cm_pen.cp_dat.l = cl;
320 memcpy(bp, cp, ++cl);
321 bp += cl;
323 cmp->cm_user_off = (ui32_t)PTR2SIZE(bp - cmp->cm_buf);
324 memcpy(bp, argv[1], ++ul);
325 bp += ul;
327 if(tl > 0){
328 cmp->cm_tag = bp;
329 memcpy(bp, ctag, ++tl);
330 bp += tl;
331 }else
332 cmp->cm_tag = ctag;
334 /* Non-buf stuff; default mapping */
335 if(lcmp != NULL){
336 /* Default mappings must be last */
337 if(ctag == NULL){
338 while(lcmp->cm_next != NULL)
339 lcmp = lcmp->cm_next;
340 }else if(lcmp->cm_next == NULL && lcmp->cm_tag == NULL){
341 if((lcmp = blcmp) == NULL)
342 goto jlinkhead;
344 cmp->cm_next = lcmp->cm_next;
345 lcmp->cm_next = cmp;
346 }else{
347 jlinkhead:
348 cmp->cm_next = *cmap;
349 *cmap = cmp;
351 cmp->cm_cmi = cmip;
352 #ifdef HAVE_REGEX
353 cmp->cm_regex = regexp;
354 #endif
355 cmp->cm_refcnt = 0;
356 a_colour_map_ref(cmp);
358 rv = TRU1;
359 jleave:
360 NYD2_LEAVE;
361 return rv;
364 static bool_t
365 a_colour_unmux(char **argv){
366 char const *mapname, *ctag, *xtag;
367 struct a_colour_map **cmap, *lcmp, *cmp;
368 struct a_colour_map_id const *cmip;
369 enum a_colour_type ct;
370 bool_t aster, rv;
371 NYD2_ENTER;
373 rv = TRU1;
374 aster = FAL0;
376 if((ct = a_colour_type_find(*argv++)) == (enum a_colour_type)-1){
377 if(!n_is_all_or_aster(argv[-1])){
378 n_err(_("`uncolour': invalid colour type \"%s\"\n"), argv[-1]);
379 rv = FAL0;
380 goto jleave;
382 aster = TRU1;
383 ct = 0;
386 mapname = argv[0];
387 ctag = (mapname != NULL) ? argv[1] : mapname;
389 if(a_colour_g == NULL)
390 goto jemap;
392 /* Delete anything? */
393 jredo:
394 if(ctag == NULL && mapname[0] == '*' && mapname[1] == '\0'){
395 size_t i1, i2;
396 struct a_colour_map *tmp;
398 for(i1 = 0; i1 < n__COLOUR_CTX_MAX; ++i1)
399 for(i2 = 0; i2 < n__COLOUR_IDS; ++i2)
400 for(cmp = *(cmap = &a_colour_g->cg_maps[ct][i1][i2]), *cmap = NULL;
401 cmp != NULL;){
402 tmp = cmp;
403 cmp = cmp->cm_next;
404 a_colour_map_unref(tmp);
406 }else{
407 if(a_colour_g == NULL){
408 rv = FAL0;
409 jemap:
410 /* I18N: colour command, mapping and precondition (option in quotes) */
411 n_err(_("`uncolour': non-existing mapping: \"%s\"%s%s%s\n"),
412 mapname, (ctag == NULL ? "" : " \""),
413 (ctag == NULL ? "" : ctag), (ctag == NULL ? "" : "\""));
414 goto jleave;
417 if((cmip = a_colour_map_id_find(mapname)) == NULL){
418 rv = FAL0;
419 goto jemap;
422 if((xtag = ctag) != NULL){
423 if(cmip->cmi_tt == a_COLOUR_TT_NONE){
424 n_err(_("`uncolour': \"%s\" doesn't support preconditions\n"),
425 mapname);
426 rv = FAL0;
427 goto jleave;
428 }else if((xtag = a_colour__tag_identify(cmip, ctag, NULL)) ==
429 n_COLOUR_TAG_ERR){
430 n_err(_("`uncolour': \"%s\": invalid precondition: \"%s\"\n"),
431 mapname, ctag);
432 rv = FAL0;
433 goto jleave;
435 /* (Improve user experience) */
436 if(xtag != NULL && !a_COLOUR_TAG_IS_SPECIAL(xtag))
437 ctag = xtag;
440 lcmp = NULL;
441 cmp = *(cmap = &a_colour_g->cg_maps[ct][cmip->cmi_ctx][cmip->cmi_id]);
442 for(;;){
443 char const *xctag;
445 if(cmp == NULL){
446 rv = FAL0;
447 goto jemap;
449 if((xctag = cmp->cm_tag) == ctag)
450 break;
451 if(ctag != NULL && !a_COLOUR_TAG_IS_SPECIAL(ctag) &&
452 xctag != NULL && !a_COLOUR_TAG_IS_SPECIAL(xctag) &&
453 !strcmp(xctag, ctag))
454 break;
455 lcmp = cmp;
456 cmp = cmp->cm_next;
459 if(lcmp == NULL)
460 *cmap = cmp->cm_next;
461 else
462 lcmp->cm_next = cmp->cm_next;
463 a_colour_map_unref(cmp);
466 jleave:
467 if(aster && ++ct != a_COLOUR_T_NONE)
468 goto jredo;
469 NYD2_LEAVE;
470 return rv;
473 static bool_t
474 a_colour__show(enum a_colour_type ct){
475 struct a_colour_map *cmp;
476 size_t i1, i2;
477 bool_t rv;
478 NYD2_ENTER;
480 /* Show all possible types? */
481 if((rv = (ct == (enum a_colour_type)-1 ? TRU1 : FAL0)))
482 ct = 0;
483 jredo:
484 for(i1 = 0; i1 < n__COLOUR_CTX_MAX; ++i1)
485 for(i2 = 0; i2 < n__COLOUR_IDS; ++i2){
486 if((cmp = a_colour_g->cg_maps[ct][i1][i2]) == NULL)
487 continue;
489 while(cmp != NULL){
490 char const *tagann, *tag;
492 tagann = "";
493 if((tag = cmp->cm_tag) == NULL)
494 tag = "";
495 else if(tag == n_COLOUR_TAG_SUM_DOT)
496 tag = "dot";
497 else if(tag == n_COLOUR_TAG_SUM_OLDER)
498 tag = "older";
499 #ifdef HAVE_REGEX
500 else if(cmp->cm_regex != NULL)
501 tagann = "[rx] ";
502 #endif
503 printf("colour %s %-*s %s %s%s\n",
504 a_colour_types[ct], a_COLOUR_MAP_SHOW_FIELDWIDTH,
505 savecat(a_colour_ctx_prefixes[i1],
506 a_colour_map_ids[i1][i2].cmi_name),
507 (char const*)cmp->cm_buf + cmp->cm_user_off,
508 tagann, tag);
509 cmp = cmp->cm_next;
513 if(rv && ++ct != a_COLOUR_T_NONE)
514 goto jredo;
515 rv = TRU1;
516 NYD2_LEAVE;
517 return rv;
520 static char const *
521 a_colour__tag_identify(struct a_colour_map_id const *cmip, char const *ctag,
522 void **regexpp){
523 NYD2_ENTER;
524 UNUSED(regexpp);
526 if((cmip->cmi_tt & a_COLOUR_TT_DOT) && !asccasecmp(ctag, "dot"))
527 ctag = n_COLOUR_TAG_SUM_DOT;
528 else if((cmip->cmi_tt & a_COLOUR_TT_OLDER) && !asccasecmp(ctag, "older"))
529 ctag = n_COLOUR_TAG_SUM_OLDER;
530 else if(cmip->cmi_tt & a_COLOUR_TT_HEADERS){
531 char *cp, c;
532 size_t i;
534 /* Can this be a valid list of headers? However, with regular expressions
535 * simply use the input as such if it appears to be a regex */
536 #ifdef HAVE_REGEX
537 if(is_maybe_regex(ctag)){
538 if(regexpp != NULL && regcomp(*regexpp = smalloc(sizeof(regex_t)),
539 ctag, REG_EXTENDED | REG_ICASE | REG_NOSUB)){
540 free(*regexpp);
541 goto jetag;
543 }else
544 #endif
546 /* Normalize to lowercase and strip any whitespace before use */
547 i = strlen(ctag);
548 cp = salloc(i +1);
550 for(i = 0; (c = *ctag++) != '\0';){
551 bool_t isblspc = blankspacechar(c);
553 if(!isblspc && !alnumchar(c) && c != '-' && c != ',')
554 goto jetag;
555 /* Since we compare header names as they come from the message this
556 * lowercasing is however redundant: we need to asccasecmp() them */
557 if(!isblspc)
558 cp[i++] = lowerconv(c);
560 cp[i] = '\0';
561 ctag = cp;
563 }else
564 jetag:
565 ctag = n_COLOUR_TAG_ERR;
566 NYD2_LEAVE;
567 return ctag;
570 static struct a_colour_map_id const *
571 a_colour_map_id_find(char const *cp){
572 size_t i;
573 struct a_colour_map_id const (*cmip)[n__COLOUR_IDS], *rv;
574 NYD2_ENTER;
576 rv = NULL;
578 for(i = 0;; ++i){
579 if(i == n__COLOUR_IDS)
580 goto jleave;
581 else{
582 size_t j = strlen(a_colour_ctx_prefixes[i]);
583 if(!ascncasecmp(cp, a_colour_ctx_prefixes[i], j)){
584 cp += j;
585 break;
589 cmip = &a_colour_map_ids[i];
591 for(i = 0;; ++i){
592 if(i == n__COLOUR_IDS || (rv = &(*cmip)[i])->cmi_name[0] == '\0'){
593 rv = NULL;
594 break;
596 if(!asccasecmp(cp, rv->cmi_name))
597 break;
599 jleave:
600 NYD2_LEAVE;
601 return rv;
604 static struct a_colour_map *
605 a_colour_map_find(enum n_colour_id cid, char const *ctag){
606 struct a_colour_map *cmp;
607 NYD2_ENTER;
609 cmp = a_colour_g->cg_maps[a_colour_g->cg_type][a_colour_g->cg_ctx][cid];
610 for(; cmp != NULL; cmp = cmp->cm_next){
611 char const *xtag = cmp->cm_tag;
613 if(xtag == ctag)
614 break;
615 if(xtag == NULL)
616 break;
617 if(ctag == NULL || a_COLOUR_TAG_IS_SPECIAL(ctag))
618 continue;
619 #ifdef HAVE_REGEX
620 if(cmp->cm_regex != NULL){
621 if(regexec(cmp->cm_regex, ctag, 0,NULL, 0) != REG_NOMATCH)
622 break;
623 }else
624 #endif
625 if(cmp->cm_cmi->cmi_tt & a_COLOUR_TT_HEADERS){
626 char *hlist = savestr(xtag), *cp;
628 while((cp = n_strsep(&hlist, ',', TRU1)) != NULL){
629 if(!asccasecmp(cp, ctag))
630 break;
632 if(cp != NULL)
633 break;
636 NYD2_LEAVE;
637 return cmp;
640 static void
641 a_colour_map_unref(struct a_colour_map *self){
642 NYD2_ENTER;
643 if(--self->cm_refcnt == 0){
644 #ifdef HAVE_REGEX
645 if(self->cm_regex != NULL){
646 regfree(self->cm_regex);
647 free(self->cm_regex);
649 #endif
650 free(self);
652 NYD2_LEAVE;
655 static bool_t
656 a_colour_iso6429(enum a_colour_type ct, char **store, char const *spec){
657 struct isodesc{
658 char id_name[15];
659 char id_modc;
660 } const fta[] = {
661 {"bold", '1'}, {"underline", '4'}, {"reverse", '7'}
662 }, ca[] = {
663 {"black", '0'}, {"red", '1'}, {"green", '2'}, {"brown", '3'},
664 {"blue", '4'}, {"magenta", '5'}, {"cyan", '6'}, {"white", '7'}
665 }, *idp;
666 char *xspec, *cp, fg[3], cfg[2 + 2*sizeof("255")];
667 ui8_t ftno_base, ftno;
668 bool_t rv;
669 NYD_ENTER;
671 rv = FAL0;
672 /* 0/1 indicate usage, thereafter possibly 256 color sequences */
673 cfg[0] = cfg[1] = 0;
675 /* Since we use salloc(), reuse the n_strsep() buffer also for the return
676 * value, ensure we have enough room for that */
677 /* C99 */{
678 size_t i = strlen(spec) +1;
679 xspec = salloc(MAX(i, sizeof("\033[1;4;7;38;5;255;48;5;255m")));
680 memcpy(xspec, spec, i);
681 spec = xspec;
684 /* Iterate over the colour spec */
685 ftno = 0;
686 while((cp = n_strsep(&xspec, ',', TRU1)) != NULL){
687 char *y, *x = strchr(cp, '=');
688 if(x == NULL){
689 jbail:
690 *store = UNCONST(_("invalid attribute list"));
691 goto jleave;
693 *x++ = '\0';
695 if(!asccasecmp(cp, "ft")){
696 if(!asccasecmp(x, "inverse")){
697 OBSOLETE(_("please use \"reverse\" not \"inverse\" for ft= fonts"));
698 x = UNCONST("reverse");
700 for(idp = fta;; ++idp)
701 if(idp == fta + NELEM(fta)){
702 *store = UNCONST(_("invalid font attribute"));
703 goto jleave;
704 }else if(!asccasecmp(x, idp->id_name)){
705 if(ftno < NELEM(fg))
706 fg[ftno++] = idp->id_modc;
707 else{
708 *store = UNCONST(_("too many font attributes"));
709 goto jleave;
711 break;
713 }else if(!asccasecmp(cp, "fg")){
714 y = cfg + 0;
715 goto jiter_colour;
716 }else if(!asccasecmp(cp, "bg")){
717 y = cfg + 1;
718 jiter_colour:
719 if(ct == a_COLOUR_T_1){
720 *store = UNCONST(_("colours are not allowed"));
721 goto jleave;
723 /* Maybe 256 color spec */
724 if(digitchar(x[0])){
725 sl_i xv;
727 if(ct == a_COLOUR_T_8){
728 *store = UNCONST(_("invalid colour for 8-colour mode"));
729 goto jleave;
732 xv = strtol(x, &cp, 10);
733 if(xv < 0 || xv > 255 || *cp != '\0' || PTRCMP(&x[3], <, cp)){
734 *store = UNCONST(_("invalid 256-colour specification"));
735 goto jleave;
737 y[0] = 5;
738 memcpy((y == &cfg[0] ? y + 2 : y + 1 + sizeof("255")), x,
739 (x[1] == '\0' ? 2 : (x[2] == '\0' ? 3 : 4)));
740 }else for(idp = ca;; ++idp)
741 if(idp == ca + NELEM(ca)){
742 *store = UNCONST(_("invalid colour attribute"));
743 goto jleave;
744 }else if(!asccasecmp(x, idp->id_name)){
745 y[0] = 1;
746 y[2] = idp->id_modc;
747 break;
749 }else
750 goto jbail;
753 /* Restore our salloc() buffer, create return value */
754 xspec = UNCONST(spec);
755 if(ftno > 0 || cfg[0] || cfg[1]){ /* TODO unite/share colour setters */
756 xspec[0] = '\033';
757 xspec[1] = '[';
758 xspec += 2;
760 for(ftno_base = ftno; ftno > 0;){
761 if(ftno-- != ftno_base)
762 *xspec++ = ';';
763 *xspec++ = fg[ftno];
766 if(cfg[0]){
767 if(ftno_base > 0)
768 *xspec++ = ';';
769 xspec[0] = '3';
770 if(cfg[0] == 1){
771 xspec[1] = cfg[2];
772 xspec += 2;
773 }else{
774 memcpy(xspec + 1, "8;5;", 4);
775 xspec += 5;
776 for(ftno = 2; cfg[ftno] != '\0'; ++ftno)
777 *xspec++ = cfg[ftno];
781 if(cfg[1]){
782 if(ftno_base > 0 || cfg[0])
783 *xspec++ = ';';
784 xspec[0] = '4';
785 if(cfg[1] == 1){
786 xspec[1] = cfg[3];
787 xspec += 2;
788 }else{
789 memcpy(xspec + 1, "8;5;", 4);
790 xspec += 5;
791 for(ftno = 2 + sizeof("255"); cfg[ftno] != '\0'; ++ftno)
792 *xspec++ = cfg[ftno];
796 *xspec++ = 'm';
798 *xspec = '\0';
799 *store = UNCONST(spec);
800 rv = TRU1;
801 jleave:
802 NYD_LEAVE;
803 return rv;
806 FL int
807 c_colour(void *v){
808 int rv;
809 NYD_ENTER;
811 rv = !a_colour_mux(v);
812 NYD_LEAVE;
813 return rv;
816 FL int
817 c_uncolour(void *v){
818 int rv;
819 NYD_ENTER;
821 rv = !a_colour_unmux(v);
822 NYD_LEAVE;
823 return rv;
826 FL void
827 n_colour_env_create(enum n_colour_ctx cctx, bool_t pager_used){
828 NYD_ENTER;
829 if(!(options & OPT_INTERACTIVE))
830 goto jleave;
832 if (ok_blook(colour_disable) || (pager_used && !ok_blook(colour_pager))){
833 n_colour_env_push(); /* FIXME lex.c only pops (always env */
834 goto jleave;
837 if(UNLIKELY(a_colour_g == NULL))
838 a_colour_init();
840 if(UNLIKELY(a_colour_g->cg_type == a_COLOUR_T_UNKNOWN)){
841 struct n_termcap_value tv;
843 if(!n_termcap_query(n_TERMCAP_QUERY_colors, &tv)){
844 a_colour_g->cg_type = a_COLOUR_T_NONE;
845 goto jleave;
846 }else
847 switch(tv.tv_data.tvd_numeric){
848 case 256: a_colour_g->cg_type = a_COLOUR_T_256; break;
849 case 8: a_colour_g->cg_type = a_COLOUR_T_8; break;
850 case 1: a_colour_g->cg_type = a_COLOUR_T_1; break;
851 default:
852 if(options & OPT_D_V)
853 n_err(_("Ignoring unsupported termcap entry for Co(lors)\n"));
854 /* FALLTHRU */
855 case 0:
856 a_colour_g->cg_type = a_COLOUR_T_NONE;
857 goto jleave;
861 if(a_colour_g->cg_type == a_COLOUR_T_NONE)
862 goto jleave;
864 a_colour_g->cg_ctx = cctx;
865 a_colour_g->cg_active = NULL;
866 pstate |= PS_COLOUR_ACTIVE;
867 jleave:
868 NYD_LEAVE;
871 FL void
872 n_colour_env_push(void){
873 struct a_colour_env *cep;
874 NYD_ENTER;
876 if(!(options & OPT_INTERACTIVE))
877 goto jleave;
879 cep = smalloc(sizeof *cep);
880 cep->ce_last = a_colour_env;
881 if(a_colour_g != NULL){
882 cep->ce_ctx = a_colour_g->cg_ctx;
883 a_colour_g->cg_active = NULL;
885 cep->ce_is_active = ((pstate & PS_COLOUR_ACTIVE) != 0);
886 a_colour_env = cep;
888 pstate &= ~PS_COLOUR_ACTIVE;
889 jleave:
890 NYD_LEAVE;
893 FL void
894 n_colour_env_pop(bool_t any_env_till_root){
895 NYD_ENTER;
896 if(!(options & OPT_INTERACTIVE))
897 goto jleave;
899 while(a_colour_env != NULL){
900 struct a_colour_env *cep;
902 if((cep = a_colour_env)->ce_is_active)
903 pstate |= PS_COLOUR_ACTIVE;
904 else
905 pstate &= ~PS_COLOUR_ACTIVE;
907 if(a_colour_g != NULL){
908 a_colour_g->cg_active = NULL;
909 a_colour_g->cg_ctx = cep->ce_ctx;
911 a_colour_env = cep->ce_last;
913 free(cep);
914 if(!any_env_till_root)
915 break;
918 if(any_env_till_root && a_colour_g != NULL && (pstate & PS_COLOUR_ACTIVE)){
919 pstate &= ~PS_COLOUR_ACTIVE;
920 a_colour_g->cg_active = NULL;
922 jleave:
923 NYD_LEAVE;
926 FL void
927 n_colour_env_gut(FILE *fp){
928 NYD_ENTER;
929 if((options & OPT_INTERACTIVE) && (pstate & PS_COLOUR_ACTIVE)){
930 pstate &= ~PS_COLOUR_ACTIVE;
932 if(a_colour_g->cg_active != NULL){
933 a_colour_g->cg_active = NULL;
934 if(fp != NULL)
935 fwrite(a_colour_g->cg_reset.cp_dat.s, a_colour_g->cg_reset.cp_dat.l,
936 1, fp);
939 NYD_LEAVE;
942 FL void
943 n_colour_put(FILE *fp, enum n_colour_id cid, char const *ctag){
944 NYD_ENTER;
945 if(pstate & PS_COLOUR_ACTIVE){
946 if(a_colour_g->cg_active != NULL)
947 fwrite(a_colour_g->cg_reset.cp_dat.s, a_colour_g->cg_reset.cp_dat.l, 1,
948 fp);
949 if((a_colour_g->cg_active = a_colour_map_find(cid, ctag)) != NULL)
950 fwrite(a_colour_g->cg_active->cm_pen.cp_dat.s,
951 a_colour_g->cg_active->cm_pen.cp_dat.l, 1, fp);
953 NYD_LEAVE;
956 FL void
957 n_colour_reset(FILE *fp){
958 NYD_ENTER;
959 if((pstate & PS_COLOUR_ACTIVE) && a_colour_g->cg_active != NULL){
960 a_colour_g->cg_active = NULL;
961 fwrite(a_colour_g->cg_reset.cp_dat.s, a_colour_g->cg_reset.cp_dat.l, 1,
962 fp);
964 NYD_LEAVE;
967 FL struct str const *
968 n_colour_reset_to_str(void){
969 struct str *rv;
970 NYD_ENTER;
972 if(pstate & PS_COLOUR_ACTIVE)
973 rv = &a_colour_g->cg_reset.cp_dat;
974 else
975 rv = NULL;
976 NYD_LEAVE;
977 return rv;
980 FL struct n_colour_pen *
981 n_colour_pen_create(enum n_colour_id cid, char const *ctag){
982 struct a_colour_map *cmp;
983 struct n_colour_pen *rv;
984 NYD_ENTER;
986 if((pstate & PS_COLOUR_ACTIVE) &&
987 (cmp = a_colour_map_find(cid, ctag)) != NULL){
988 union {void *vp; char *cp; struct n_colour_pen *cpp;} u;
989 u.vp = cmp;
990 rv = u.cpp;
991 }else
992 rv = NULL;
993 NYD_LEAVE;
994 return rv;
997 FL void
998 n_colour_pen_put(struct n_colour_pen *self, FILE *fp){
999 NYD_ENTER;
1000 if(pstate & PS_COLOUR_ACTIVE){
1001 union {void *vp; char *cp; struct a_colour_map *cmp;} u;
1003 u.vp = self;
1004 if(u.cmp != a_colour_g->cg_active){
1005 if(a_colour_g->cg_active != NULL)
1006 fwrite(a_colour_g->cg_reset.cp_dat.s, a_colour_g->cg_reset.cp_dat.l,
1007 1, fp);
1008 if(u.cmp != NULL)
1009 fwrite(self->cp_dat.s, self->cp_dat.l, 1, fp);
1010 a_colour_g->cg_active = u.cmp;
1013 NYD_LEAVE;
1016 FL struct str const *
1017 n_colour_pen_to_str(struct n_colour_pen *self){
1018 struct str *rv;
1019 NYD_ENTER;
1021 if((pstate & PS_COLOUR_ACTIVE) && self != NULL)
1022 rv = &self->cp_dat;
1023 else
1024 rv = NULL;
1025 NYD_LEAVE;
1026 return rv;
1028 #endif /* HAVE_COLOUR */
1030 /* s-it-mode */