n_shell_quote(): rewrite, tweak user-at-a-glance experience
[s-mailx.git] / colour.c
blob371738383aefb4c6d4a2c7a8a808669966b71065
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. For shell_quote() simplicity let's just ignore an
265 * empty precondition */
266 if((ctag = argv[2]) != NULL && *ctag != '\0'){
267 char const *xtag;
269 if(cmip->cmi_tt == a_COLOUR_TT_NONE){
270 n_err(_("`colour': \"%s\" doesn't support preconditions\n"), mapname);
271 goto jleave;
272 }else if((xtag = a_colour__tag_identify(cmip, ctag, &regexp)) ==
273 n_COLOUR_TAG_ERR){
274 /* I18N: ..of colour mapping */
275 n_err(_("`colour': \"%s\": invalid precondition: \"%s\"\n"),
276 mapname, ctag);
277 goto jleave;
279 ctag = xtag;
282 /* At this time we have all the information to be able to query wether such
283 * a mapping is yet established. If so, destroy it */
284 for(blcmp = lcmp = NULL,
285 cmp = *(cmap =
286 &a_colour_g->cg_maps[ct][cmip->cmi_ctx][cmip->cmi_id]);
287 cmp != NULL; blcmp = lcmp, lcmp = cmp, cmp = cmp->cm_next){
288 char const *xctag = cmp->cm_tag;
290 if(xctag == ctag ||
291 (ctag != NULL && !a_COLOUR_TAG_IS_SPECIAL(ctag) &&
292 xctag != NULL && !a_COLOUR_TAG_IS_SPECIAL(xctag) &&
293 !strcmp(xctag, ctag))){
294 if(lcmp == NULL)
295 *cmap = cmp->cm_next;
296 else
297 lcmp->cm_next = cmp->cm_next;
298 a_colour_map_unref(cmp);
299 break;
303 /* Create mapping */
304 /* C99 */{
305 size_t tl, ul, cl;
306 char *bp, *cp;
308 if(!a_colour_iso6429(ct, &cp, argv[1])){
309 /* I18N: colour command: mapping: error message: user argument */
310 n_err(_("`colour': \"%s\": %s: \"%s\"\n"), mapname, cp, argv[1]);
311 goto jleave;
314 tl = (ctag != NULL && !a_COLOUR_TAG_IS_SPECIAL(ctag)) ? strlen(ctag) : 0;
315 cmp = smalloc(sizeof(*cmp) - VFIELD_SIZEOF(struct a_colour_map, cm_buf) +
316 tl +1 + (ul = strlen(argv[1])) +1 + (cl = strlen(cp)) +1);
318 /* .cm_buf stuff */
319 cmp->cm_pen.cp_dat.s = bp = cmp->cm_buf;
320 cmp->cm_pen.cp_dat.l = cl;
321 memcpy(bp, cp, ++cl);
322 bp += cl;
324 cmp->cm_user_off = (ui32_t)PTR2SIZE(bp - cmp->cm_buf);
325 memcpy(bp, argv[1], ++ul);
326 bp += ul;
328 if(tl > 0){
329 cmp->cm_tag = bp;
330 memcpy(bp, ctag, ++tl);
331 bp += tl;
332 }else
333 cmp->cm_tag = ctag;
335 /* Non-buf stuff; default mapping */
336 if(lcmp != NULL){
337 /* Default mappings must be last */
338 if(ctag == NULL){
339 while(lcmp->cm_next != NULL)
340 lcmp = lcmp->cm_next;
341 }else if(lcmp->cm_next == NULL && lcmp->cm_tag == NULL){
342 if((lcmp = blcmp) == NULL)
343 goto jlinkhead;
345 cmp->cm_next = lcmp->cm_next;
346 lcmp->cm_next = cmp;
347 }else{
348 jlinkhead:
349 cmp->cm_next = *cmap;
350 *cmap = cmp;
352 cmp->cm_cmi = cmip;
353 #ifdef HAVE_REGEX
354 cmp->cm_regex = regexp;
355 #endif
356 cmp->cm_refcnt = 0;
357 a_colour_map_ref(cmp);
359 rv = TRU1;
360 jleave:
361 NYD2_LEAVE;
362 return rv;
365 static bool_t
366 a_colour_unmux(char **argv){
367 char const *mapname, *ctag, *xtag;
368 struct a_colour_map **cmap, *lcmp, *cmp;
369 struct a_colour_map_id const *cmip;
370 enum a_colour_type ct;
371 bool_t aster, rv;
372 NYD2_ENTER;
374 rv = TRU1;
375 aster = FAL0;
377 if((ct = a_colour_type_find(*argv++)) == (enum a_colour_type)-1){
378 if(!n_is_all_or_aster(argv[-1])){
379 n_err(_("`uncolour': invalid colour type \"%s\"\n"), argv[-1]);
380 rv = FAL0;
381 goto jleave;
383 aster = TRU1;
384 ct = 0;
387 mapname = argv[0];
388 ctag = (mapname != NULL) ? argv[1] : mapname;
390 if(a_colour_g == NULL)
391 goto jemap;
393 /* Delete anything? */
394 jredo:
395 if(ctag == NULL && mapname[0] == '*' && mapname[1] == '\0'){
396 size_t i1, i2;
397 struct a_colour_map *tmp;
399 for(i1 = 0; i1 < n__COLOUR_CTX_MAX; ++i1)
400 for(i2 = 0; i2 < n__COLOUR_IDS; ++i2)
401 for(cmp = *(cmap = &a_colour_g->cg_maps[ct][i1][i2]), *cmap = NULL;
402 cmp != NULL;){
403 tmp = cmp;
404 cmp = cmp->cm_next;
405 a_colour_map_unref(tmp);
407 }else{
408 if(a_colour_g == NULL){
409 rv = FAL0;
410 jemap:
411 /* I18N: colour command, mapping and precondition (option in quotes) */
412 n_err(_("`uncolour': non-existing mapping: \"%s\"%s%s%s\n"),
413 mapname, (ctag == NULL ? "" : " \""),
414 (ctag == NULL ? "" : ctag), (ctag == NULL ? "" : "\""));
415 goto jleave;
418 if((cmip = a_colour_map_id_find(mapname)) == NULL){
419 rv = FAL0;
420 goto jemap;
423 if((xtag = ctag) != NULL){
424 if(cmip->cmi_tt == a_COLOUR_TT_NONE){
425 n_err(_("`uncolour': \"%s\" doesn't support preconditions\n"),
426 mapname);
427 rv = FAL0;
428 goto jleave;
429 }else if((xtag = a_colour__tag_identify(cmip, ctag, NULL)) ==
430 n_COLOUR_TAG_ERR){
431 n_err(_("`uncolour': \"%s\": invalid precondition: \"%s\"\n"),
432 mapname, ctag);
433 rv = FAL0;
434 goto jleave;
436 /* (Improve user experience) */
437 if(xtag != NULL && !a_COLOUR_TAG_IS_SPECIAL(xtag))
438 ctag = xtag;
441 lcmp = NULL;
442 cmp = *(cmap = &a_colour_g->cg_maps[ct][cmip->cmi_ctx][cmip->cmi_id]);
443 for(;;){
444 char const *xctag;
446 if(cmp == NULL){
447 rv = FAL0;
448 goto jemap;
450 if((xctag = cmp->cm_tag) == ctag)
451 break;
452 if(ctag != NULL && !a_COLOUR_TAG_IS_SPECIAL(ctag) &&
453 xctag != NULL && !a_COLOUR_TAG_IS_SPECIAL(xctag) &&
454 !strcmp(xctag, ctag))
455 break;
456 lcmp = cmp;
457 cmp = cmp->cm_next;
460 if(lcmp == NULL)
461 *cmap = cmp->cm_next;
462 else
463 lcmp->cm_next = cmp->cm_next;
464 a_colour_map_unref(cmp);
467 jleave:
468 if(aster && ++ct != a_COLOUR_T_NONE)
469 goto jredo;
470 NYD2_LEAVE;
471 return rv;
474 static bool_t
475 a_colour__show(enum a_colour_type ct){
476 struct a_colour_map *cmp;
477 size_t i1, i2;
478 bool_t rv;
479 NYD2_ENTER;
481 /* Show all possible types? */
482 if((rv = (ct == (enum a_colour_type)-1 ? TRU1 : FAL0)))
483 ct = 0;
484 jredo:
485 for(i1 = 0; i1 < n__COLOUR_CTX_MAX; ++i1)
486 for(i2 = 0; i2 < n__COLOUR_IDS; ++i2){
487 if((cmp = a_colour_g->cg_maps[ct][i1][i2]) == NULL)
488 continue;
490 while(cmp != NULL){
491 char const *tagann, *tag;
493 tagann = "";
494 if((tag = cmp->cm_tag) == NULL)
495 tag = "";
496 else if(tag == n_COLOUR_TAG_SUM_DOT)
497 tag = "dot";
498 else if(tag == n_COLOUR_TAG_SUM_OLDER)
499 tag = "older";
500 #ifdef HAVE_REGEX
501 else if(cmp->cm_regex != NULL)
502 tagann = "[rx] ";
503 #endif
504 printf("colour %s %-*s %s %s%s\n",
505 a_colour_types[ct], a_COLOUR_MAP_SHOW_FIELDWIDTH,
506 savecat(a_colour_ctx_prefixes[i1],
507 a_colour_map_ids[i1][i2].cmi_name),
508 (char const*)cmp->cm_buf + cmp->cm_user_off,
509 tagann, n_shell_quote_cp(tag, TRU1));
510 cmp = cmp->cm_next;
514 if(rv && ++ct != a_COLOUR_T_NONE)
515 goto jredo;
516 rv = TRU1;
517 NYD2_LEAVE;
518 return rv;
521 static char const *
522 a_colour__tag_identify(struct a_colour_map_id const *cmip, char const *ctag,
523 void **regexpp){
524 NYD2_ENTER;
525 UNUSED(regexpp);
527 if((cmip->cmi_tt & a_COLOUR_TT_DOT) && !asccasecmp(ctag, "dot"))
528 ctag = n_COLOUR_TAG_SUM_DOT;
529 else if((cmip->cmi_tt & a_COLOUR_TT_OLDER) && !asccasecmp(ctag, "older"))
530 ctag = n_COLOUR_TAG_SUM_OLDER;
531 else if(cmip->cmi_tt & a_COLOUR_TT_HEADERS){
532 char *cp, c;
533 size_t i;
535 /* Can this be a valid list of headers? However, with regular expressions
536 * simply use the input as such if it appears to be a regex */
537 #ifdef HAVE_REGEX
538 if(is_maybe_regex(ctag)){
539 if(regexpp != NULL && regcomp(*regexpp = smalloc(sizeof(regex_t)),
540 ctag, REG_EXTENDED | REG_ICASE | REG_NOSUB)){
541 free(*regexpp);
542 goto jetag;
544 }else
545 #endif
547 /* Normalize to lowercase and strip any whitespace before use */
548 i = strlen(ctag);
549 cp = salloc(i +1);
551 for(i = 0; (c = *ctag++) != '\0';){
552 bool_t isblspc = blankspacechar(c);
554 if(!isblspc && !alnumchar(c) && c != '-' && c != ',')
555 goto jetag;
556 /* Since we compare header names as they come from the message this
557 * lowercasing is however redundant: we need to asccasecmp() them */
558 if(!isblspc)
559 cp[i++] = lowerconv(c);
561 cp[i] = '\0';
562 ctag = cp;
564 }else
565 jetag:
566 ctag = n_COLOUR_TAG_ERR;
567 NYD2_LEAVE;
568 return ctag;
571 static struct a_colour_map_id const *
572 a_colour_map_id_find(char const *cp){
573 size_t i;
574 struct a_colour_map_id const (*cmip)[n__COLOUR_IDS], *rv;
575 NYD2_ENTER;
577 rv = NULL;
579 for(i = 0;; ++i){
580 if(i == n__COLOUR_IDS)
581 goto jleave;
582 else{
583 size_t j = strlen(a_colour_ctx_prefixes[i]);
584 if(!ascncasecmp(cp, a_colour_ctx_prefixes[i], j)){
585 cp += j;
586 break;
590 cmip = &a_colour_map_ids[i];
592 for(i = 0;; ++i){
593 if(i == n__COLOUR_IDS || (rv = &(*cmip)[i])->cmi_name[0] == '\0'){
594 rv = NULL;
595 break;
597 if(!asccasecmp(cp, rv->cmi_name))
598 break;
600 jleave:
601 NYD2_LEAVE;
602 return rv;
605 static struct a_colour_map *
606 a_colour_map_find(enum n_colour_id cid, char const *ctag){
607 struct a_colour_map *cmp;
608 NYD2_ENTER;
610 cmp = a_colour_g->cg_maps[a_colour_g->cg_type][a_colour_g->cg_ctx][cid];
611 for(; cmp != NULL; cmp = cmp->cm_next){
612 char const *xtag = cmp->cm_tag;
614 if(xtag == ctag)
615 break;
616 if(xtag == NULL)
617 break;
618 if(ctag == NULL || a_COLOUR_TAG_IS_SPECIAL(ctag))
619 continue;
620 #ifdef HAVE_REGEX
621 if(cmp->cm_regex != NULL){
622 if(regexec(cmp->cm_regex, ctag, 0,NULL, 0) != REG_NOMATCH)
623 break;
624 }else
625 #endif
626 if(cmp->cm_cmi->cmi_tt & a_COLOUR_TT_HEADERS){
627 char *hlist = savestr(xtag), *cp;
629 while((cp = n_strsep(&hlist, ',', TRU1)) != NULL){
630 if(!asccasecmp(cp, ctag))
631 break;
633 if(cp != NULL)
634 break;
637 NYD2_LEAVE;
638 return cmp;
641 static void
642 a_colour_map_unref(struct a_colour_map *self){
643 NYD2_ENTER;
644 if(--self->cm_refcnt == 0){
645 #ifdef HAVE_REGEX
646 if(self->cm_regex != NULL){
647 regfree(self->cm_regex);
648 free(self->cm_regex);
650 #endif
651 free(self);
653 NYD2_LEAVE;
656 static bool_t
657 a_colour_iso6429(enum a_colour_type ct, char **store, char const *spec){
658 struct isodesc{
659 char id_name[15];
660 char id_modc;
661 } const fta[] = {
662 {"bold", '1'}, {"underline", '4'}, {"reverse", '7'}
663 }, ca[] = {
664 {"black", '0'}, {"red", '1'}, {"green", '2'}, {"brown", '3'},
665 {"blue", '4'}, {"magenta", '5'}, {"cyan", '6'}, {"white", '7'}
666 }, *idp;
667 char *xspec, *cp, fg[3], cfg[2 + 2*sizeof("255")];
668 ui8_t ftno_base, ftno;
669 bool_t rv;
670 NYD_ENTER;
672 rv = FAL0;
673 /* 0/1 indicate usage, thereafter possibly 256 color sequences */
674 cfg[0] = cfg[1] = 0;
676 /* Since we use salloc(), reuse the n_strsep() buffer also for the return
677 * value, ensure we have enough room for that */
678 /* C99 */{
679 size_t i = strlen(spec) +1;
680 xspec = salloc(MAX(i, sizeof("\033[1;4;7;38;5;255;48;5;255m")));
681 memcpy(xspec, spec, i);
682 spec = xspec;
685 /* Iterate over the colour spec */
686 ftno = 0;
687 while((cp = n_strsep(&xspec, ',', TRU1)) != NULL){
688 char *y, *x = strchr(cp, '=');
689 if(x == NULL){
690 jbail:
691 *store = UNCONST(_("invalid attribute list"));
692 goto jleave;
694 *x++ = '\0';
696 if(!asccasecmp(cp, "ft")){
697 if(!asccasecmp(x, "inverse")){
698 OBSOLETE(_("please use \"reverse\" not \"inverse\" for ft= fonts"));
699 x = UNCONST("reverse");
701 for(idp = fta;; ++idp)
702 if(idp == fta + NELEM(fta)){
703 *store = UNCONST(_("invalid font attribute"));
704 goto jleave;
705 }else if(!asccasecmp(x, idp->id_name)){
706 if(ftno < NELEM(fg))
707 fg[ftno++] = idp->id_modc;
708 else{
709 *store = UNCONST(_("too many font attributes"));
710 goto jleave;
712 break;
714 }else if(!asccasecmp(cp, "fg")){
715 y = cfg + 0;
716 goto jiter_colour;
717 }else if(!asccasecmp(cp, "bg")){
718 y = cfg + 1;
719 jiter_colour:
720 if(ct == a_COLOUR_T_1){
721 *store = UNCONST(_("colours are not allowed"));
722 goto jleave;
724 /* Maybe 256 color spec */
725 if(digitchar(x[0])){
726 sl_i xv;
728 if(ct == a_COLOUR_T_8){
729 *store = UNCONST(_("invalid colour for 8-colour mode"));
730 goto jleave;
733 xv = strtol(x, &cp, 10);
734 if(xv < 0 || xv > 255 || *cp != '\0' || PTRCMP(&x[3], <, cp)){
735 *store = UNCONST(_("invalid 256-colour specification"));
736 goto jleave;
738 y[0] = 5;
739 memcpy((y == &cfg[0] ? y + 2 : y + 1 + sizeof("255")), x,
740 (x[1] == '\0' ? 2 : (x[2] == '\0' ? 3 : 4)));
741 }else for(idp = ca;; ++idp)
742 if(idp == ca + NELEM(ca)){
743 *store = UNCONST(_("invalid colour attribute"));
744 goto jleave;
745 }else if(!asccasecmp(x, idp->id_name)){
746 y[0] = 1;
747 y[2] = idp->id_modc;
748 break;
750 }else
751 goto jbail;
754 /* Restore our salloc() buffer, create return value */
755 xspec = UNCONST(spec);
756 if(ftno > 0 || cfg[0] || cfg[1]){ /* TODO unite/share colour setters */
757 xspec[0] = '\033';
758 xspec[1] = '[';
759 xspec += 2;
761 for(ftno_base = ftno; ftno > 0;){
762 if(ftno-- != ftno_base)
763 *xspec++ = ';';
764 *xspec++ = fg[ftno];
767 if(cfg[0]){
768 if(ftno_base > 0)
769 *xspec++ = ';';
770 xspec[0] = '3';
771 if(cfg[0] == 1){
772 xspec[1] = cfg[2];
773 xspec += 2;
774 }else{
775 memcpy(xspec + 1, "8;5;", 4);
776 xspec += 5;
777 for(ftno = 2; cfg[ftno] != '\0'; ++ftno)
778 *xspec++ = cfg[ftno];
782 if(cfg[1]){
783 if(ftno_base > 0 || cfg[0])
784 *xspec++ = ';';
785 xspec[0] = '4';
786 if(cfg[1] == 1){
787 xspec[1] = cfg[3];
788 xspec += 2;
789 }else{
790 memcpy(xspec + 1, "8;5;", 4);
791 xspec += 5;
792 for(ftno = 2 + sizeof("255"); cfg[ftno] != '\0'; ++ftno)
793 *xspec++ = cfg[ftno];
797 *xspec++ = 'm';
799 *xspec = '\0';
800 *store = UNCONST(spec);
801 rv = TRU1;
802 jleave:
803 NYD_LEAVE;
804 return rv;
807 FL int
808 c_colour(void *v){
809 int rv;
810 NYD_ENTER;
812 rv = !a_colour_mux(v);
813 NYD_LEAVE;
814 return rv;
817 FL int
818 c_uncolour(void *v){
819 int rv;
820 NYD_ENTER;
822 rv = !a_colour_unmux(v);
823 NYD_LEAVE;
824 return rv;
827 FL void
828 n_colour_env_create(enum n_colour_ctx cctx, bool_t pager_used){
829 NYD_ENTER;
830 if(!(options & OPT_INTERACTIVE))
831 goto jleave;
833 if (ok_blook(colour_disable) || (pager_used && !ok_blook(colour_pager))){
834 n_colour_env_push(); /* FIXME lex.c only pops (always env */
835 goto jleave;
838 if(UNLIKELY(a_colour_g == NULL))
839 a_colour_init();
841 if(UNLIKELY(a_colour_g->cg_type == a_COLOUR_T_UNKNOWN)){
842 struct n_termcap_value tv;
844 if(!n_termcap_query(n_TERMCAP_QUERY_colors, &tv)){
845 a_colour_g->cg_type = a_COLOUR_T_NONE;
846 goto jleave;
847 }else
848 switch(tv.tv_data.tvd_numeric){
849 case 256: a_colour_g->cg_type = a_COLOUR_T_256; break;
850 case 8: a_colour_g->cg_type = a_COLOUR_T_8; break;
851 case 1: a_colour_g->cg_type = a_COLOUR_T_1; break;
852 default:
853 if(options & OPT_D_V)
854 n_err(_("Ignoring unsupported termcap entry for Co(lors)\n"));
855 /* FALLTHRU */
856 case 0:
857 a_colour_g->cg_type = a_COLOUR_T_NONE;
858 goto jleave;
862 if(a_colour_g->cg_type == a_COLOUR_T_NONE)
863 goto jleave;
865 a_colour_g->cg_ctx = cctx;
866 a_colour_g->cg_active = NULL;
867 pstate |= PS_COLOUR_ACTIVE;
868 jleave:
869 NYD_LEAVE;
872 FL void
873 n_colour_env_push(void){
874 struct a_colour_env *cep;
875 NYD_ENTER;
877 if(!(options & OPT_INTERACTIVE))
878 goto jleave;
880 cep = smalloc(sizeof *cep);
881 cep->ce_last = a_colour_env;
882 if(a_colour_g != NULL){
883 cep->ce_ctx = a_colour_g->cg_ctx;
884 a_colour_g->cg_active = NULL;
886 cep->ce_is_active = ((pstate & PS_COLOUR_ACTIVE) != 0);
887 a_colour_env = cep;
889 pstate &= ~PS_COLOUR_ACTIVE;
890 jleave:
891 NYD_LEAVE;
894 FL void
895 n_colour_env_pop(bool_t any_env_till_root){
896 NYD_ENTER;
897 if(!(options & OPT_INTERACTIVE))
898 goto jleave;
900 while(a_colour_env != NULL){
901 struct a_colour_env *cep;
903 if((cep = a_colour_env)->ce_is_active)
904 pstate |= PS_COLOUR_ACTIVE;
905 else
906 pstate &= ~PS_COLOUR_ACTIVE;
908 if(a_colour_g != NULL){
909 a_colour_g->cg_active = NULL;
910 a_colour_g->cg_ctx = cep->ce_ctx;
912 a_colour_env = cep->ce_last;
914 free(cep);
915 if(!any_env_till_root)
916 break;
919 if(any_env_till_root && a_colour_g != NULL && (pstate & PS_COLOUR_ACTIVE)){
920 pstate &= ~PS_COLOUR_ACTIVE;
921 a_colour_g->cg_active = NULL;
923 jleave:
924 NYD_LEAVE;
927 FL void
928 n_colour_env_gut(FILE *fp){
929 NYD_ENTER;
930 if((options & OPT_INTERACTIVE) && (pstate & PS_COLOUR_ACTIVE)){
931 pstate &= ~PS_COLOUR_ACTIVE;
933 if(a_colour_g->cg_active != NULL){
934 a_colour_g->cg_active = NULL;
935 if(fp != NULL)
936 fwrite(a_colour_g->cg_reset.cp_dat.s, a_colour_g->cg_reset.cp_dat.l,
937 1, fp);
940 NYD_LEAVE;
943 FL void
944 n_colour_put(FILE *fp, enum n_colour_id cid, char const *ctag){
945 NYD_ENTER;
946 if(pstate & PS_COLOUR_ACTIVE){
947 if(a_colour_g->cg_active != NULL)
948 fwrite(a_colour_g->cg_reset.cp_dat.s, a_colour_g->cg_reset.cp_dat.l, 1,
949 fp);
950 if((a_colour_g->cg_active = a_colour_map_find(cid, ctag)) != NULL)
951 fwrite(a_colour_g->cg_active->cm_pen.cp_dat.s,
952 a_colour_g->cg_active->cm_pen.cp_dat.l, 1, fp);
954 NYD_LEAVE;
957 FL void
958 n_colour_reset(FILE *fp){
959 NYD_ENTER;
960 if((pstate & PS_COLOUR_ACTIVE) && a_colour_g->cg_active != NULL){
961 a_colour_g->cg_active = NULL;
962 fwrite(a_colour_g->cg_reset.cp_dat.s, a_colour_g->cg_reset.cp_dat.l, 1,
963 fp);
965 NYD_LEAVE;
968 FL struct str const *
969 n_colour_reset_to_str(void){
970 struct str *rv;
971 NYD_ENTER;
973 if(pstate & PS_COLOUR_ACTIVE)
974 rv = &a_colour_g->cg_reset.cp_dat;
975 else
976 rv = NULL;
977 NYD_LEAVE;
978 return rv;
981 FL struct n_colour_pen *
982 n_colour_pen_create(enum n_colour_id cid, char const *ctag){
983 struct a_colour_map *cmp;
984 struct n_colour_pen *rv;
985 NYD_ENTER;
987 if((pstate & PS_COLOUR_ACTIVE) &&
988 (cmp = a_colour_map_find(cid, ctag)) != NULL){
989 union {void *vp; char *cp; struct n_colour_pen *cpp;} u;
990 u.vp = cmp;
991 rv = u.cpp;
992 }else
993 rv = NULL;
994 NYD_LEAVE;
995 return rv;
998 FL void
999 n_colour_pen_put(struct n_colour_pen *self, FILE *fp){
1000 NYD_ENTER;
1001 if(pstate & PS_COLOUR_ACTIVE){
1002 union {void *vp; char *cp; struct a_colour_map *cmp;} u;
1004 u.vp = self;
1005 if(u.cmp != a_colour_g->cg_active){
1006 if(a_colour_g->cg_active != NULL)
1007 fwrite(a_colour_g->cg_reset.cp_dat.s, a_colour_g->cg_reset.cp_dat.l,
1008 1, fp);
1009 if(u.cmp != NULL)
1010 fwrite(self->cp_dat.s, self->cp_dat.l, 1, fp);
1011 a_colour_g->cg_active = u.cmp;
1014 NYD_LEAVE;
1017 FL struct str const *
1018 n_colour_pen_to_str(struct n_colour_pen *self){
1019 struct str *rv;
1020 NYD_ENTER;
1022 if((pstate & PS_COLOUR_ACTIVE) && self != NULL)
1023 rv = &self->cp_dat;
1024 else
1025 rv = NULL;
1026 NYD_LEAVE;
1027 return rv;
1029 #endif /* HAVE_COLOUR */
1031 /* s-it-mode */