1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ `(un)?colour' commands, and anything working with it.
3 *@ TODO n_colour_env should be objects, n_COLOUR_IS_ACTIVE() should take
4 *@ TODO such an object! We still should work together with n_go_data,
5 *@ TODO but only for cleanup purposes. No stack at all, that is to say!
6 *@ TODO (Note we yet use autorec memory, so with JUMPS this needs care!)
8 * Copyright (c) 2014 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
10 * Permission to use, copy, modify, and/or distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 #ifndef HAVE_AMALGAMATION
32 /* Not needed publically, but extends a set from nail.h */
33 #define n_COLOUR_TAG_ERR ((char*)-1)
34 #define a_COLOUR_TAG_IS_SPECIAL(P) (PTR2SIZE(P) >= PTR2SIZE(-3))
40 a_COLOUR_T_NONE
, /* EQ largest real colour + 1! */
41 a_COLOUR_T_UNKNOWN
/* Initial value: real one queried before 1st use */
44 enum a_colour_tag_type
{
46 a_COLOUR_TT_DOT
= 1<<0, /* "dot" */
47 a_COLOUR_TT_OLDER
= 1<<1, /* "older" */
48 a_COLOUR_TT_HEADERS
= 1<<2, /* Comma-separated list of headers allowed */
50 a_COLOUR_TT_SUM
= a_COLOUR_TT_DOT
| a_COLOUR_TT_OLDER
,
51 a_COLOUR_TT_VIEW
= a_COLOUR_TT_HEADERS
54 struct a_colour_type_map
{
55 ui8_t ctm_type
; /* a_colour_type */
59 struct a_colour_map_id
{
60 ui8_t cmi_ctx
; /* enum n_colour_ctx */
61 ui8_t cmi_id
; /* enum n_colour_id */
62 ui8_t cmi_tt
; /* enum a_colour_tag_type */
63 char const cmi_name
[13];
65 n_CTA(n__COLOUR_IDS
<= UI8_MAX
, "Enumeration exceeds storage datatype");
68 struct str cp_dat
; /* Pre-prepared ISO 6429 escape sequence */
71 struct a_colour_map
/* : public n_colour_pen */{
72 struct n_colour_pen cm_pen
; /* Points into .cm_buf */
73 struct a_colour_map
*cm_next
;
74 char const *cm_tag
; /* Colour tag or NULL for default (last) */
75 struct a_colour_map_id
const *cm_cmi
;
79 ui32_t cm_refcnt
; /* Beware of reference drops in recursions */
80 ui32_t cm_user_off
; /* User input offset in .cm_buf */
81 char cm_buf
[n_VFIELD_SIZE(0)];
86 ui8_t cg_type
; /* a_colour_type */
88 struct n_colour_pen cg_reset
; /* The reset sequence */
90 *cg_maps
[a_COLOUR_T_NONE
][n__COLOUR_CTX_MAX1
][n__COLOUR_IDS
];
91 char cg__reset_buf
[n_ALIGN_SMALL(sizeof("\033[0m"))];
94 /* C99: use [INDEX]={} */
96 n_CTA(a_COLOUR_T_256
== 0, "Unexpected value of constant");
97 n_CTA(a_COLOUR_T_8
== 1, "Unexpected value of constant");
98 n_CTA(a_COLOUR_T_1
== 2, "Unexpected value of constant");
99 static char const a_colour_types
[][8] = {"256", "iso", "mono"};
101 static struct a_colour_type_map
const a_colour_type_maps
[] = {
102 {a_COLOUR_T_256
, "256"},
103 {a_COLOUR_T_8
, "8"}, {a_COLOUR_T_8
, "iso"}, {a_COLOUR_T_8
, "ansi"},
104 {a_COLOUR_T_1
, "1"}, {a_COLOUR_T_1
, "mono"}
107 n_CTA(n_COLOUR_CTX_SUM
== 0, "Unexpected value of constant");
108 n_CTA(n_COLOUR_CTX_VIEW
== 1, "Unexpected value of constant");
109 n_CTA(n_COLOUR_CTX_MLE
== 2, "Unexpected value of constant");
110 static char const a_colour_ctx_prefixes
[n__COLOUR_CTX_MAX1
][8] = {
111 "sum-", "view-", "mle-"
114 static struct a_colour_map_id
const
115 a_colour_map_ids
[n__COLOUR_CTX_MAX1
][n__COLOUR_IDS
] = {{
116 {n_COLOUR_CTX_SUM
, n_COLOUR_ID_SUM_DOTMARK
, a_COLOUR_TT_SUM
, "dotmark"},
117 {n_COLOUR_CTX_SUM
, n_COLOUR_ID_SUM_HEADER
, a_COLOUR_TT_SUM
, "header"},
118 {n_COLOUR_CTX_SUM
, n_COLOUR_ID_SUM_THREAD
, a_COLOUR_TT_SUM
, "thread"},
120 {n_COLOUR_CTX_VIEW
, n_COLOUR_ID_VIEW_FROM_
, a_COLOUR_TT_NONE
, "from_"},
121 {n_COLOUR_CTX_VIEW
, n_COLOUR_ID_VIEW_HEADER
, a_COLOUR_TT_VIEW
, "header"},
122 {n_COLOUR_CTX_VIEW
, n_COLOUR_ID_VIEW_MSGINFO
, a_COLOUR_TT_NONE
, "msginfo"},
123 {n_COLOUR_CTX_VIEW
, n_COLOUR_ID_VIEW_PARTINFO
, a_COLOUR_TT_NONE
, "partinfo"},
125 {n_COLOUR_CTX_MLE
, n_COLOUR_ID_MLE_POSITION
, a_COLOUR_TT_NONE
, "position"},
126 {n_COLOUR_CTX_MLE
, n_COLOUR_ID_MLE_PROMPT
, a_COLOUR_TT_NONE
, "prompt"},
128 #define a_COLOUR_MAP_SHOW_FIELDWIDTH \
129 (int)(sizeof("view-")-1 + sizeof("partinfo")-1)
131 static struct a_colour_g a_colour_g
;
134 static void a_colour_init(void);
136 /* Find the type or -1 */
137 static enum a_colour_type
a_colour_type_find(char const *name
);
139 /* `(un)?colour' implementations */
140 static bool_t
a_colour_mux(char **argv
);
141 static bool_t
a_colour_unmux(char **argv
);
143 static bool_t
a_colour__show(enum a_colour_type ct
);
144 /* (regexpp may be NULL) */
145 static char const *a_colour__tag_identify(struct a_colour_map_id
const *cmip
,
146 char const *ctag
, void **regexpp
);
148 /* Try to find a mapping identity for user given slotname */
149 static struct a_colour_map_id
const *a_colour_map_id_find(char const *slotname
);
151 /* Find an existing mapping for the given combination */
152 static struct a_colour_map
*a_colour_map_find(enum n_colour_id cid
,
153 enum n_colour_ctx cctx
, char const *ctag
);
155 /* In-/Decrement reference counter, destroy if counts gets zero */
156 #define a_colour_map_ref(SELF) do{ ++(SELF)->cm_refcnt; }while(0)
157 static void a_colour_map_unref(struct a_colour_map
*self
);
159 /* Create an ISO 6429 (ECMA-48/ANSI) terminal control escape sequence from user
160 * input spec, store it or on error message in *store */
161 static bool_t
a_colour_iso6429(enum a_colour_type ct
, char **store
,
167 a_colour_g
.cg_is_init
= TRU1
;
168 memcpy(a_colour_g
.cg_reset
.cp_dat
.s
= a_colour_g
.cg__reset_buf
, "\033[0m",
169 a_colour_g
.cg_reset
.cp_dat
.l
= sizeof("\033[0m") -1); /* (calloc) */
170 a_colour_g
.cg_type
= a_COLOUR_T_UNKNOWN
;
174 static enum a_colour_type
175 a_colour_type_find(char const *name
){
176 struct a_colour_type_map
const *ctmp
;
177 enum a_colour_type rv
;
180 ctmp
= a_colour_type_maps
;
181 do if(!asccasecmp(ctmp
->ctm_name
, name
)){
184 }while(PTRCMP(++ctmp
, !=, a_colour_type_maps
+ n_NELEM(a_colour_type_maps
)));
186 rv
= (enum a_colour_type
)-1;
193 a_colour_mux(char **argv
){
195 char const *mapname
, *ctag
;
196 struct a_colour_map
**cmap
, *blcmp
, *lcmp
, *cmp
;
197 struct a_colour_map_id
const *cmip
;
199 enum a_colour_type ct
;
202 if((ct
= a_colour_type_find(*argv
++)) == (enum a_colour_type
)-1 &&
203 (*argv
!= NULL
|| !n_is_all_or_aster(argv
[-1]))){
204 n_err(_("`colour': invalid colour type %s\n"),
205 n_shexp_quote_cp(argv
[-1], FAL0
));
210 if(!a_colour_g
.cg_is_init
)
214 rv
= a_colour__show(ct
);
221 if((cmip
= a_colour_map_id_find(mapname
= argv
[0])) == NULL
){
222 n_err(_("`colour': non-existing mapping: %s\n"),
223 n_shexp_quote_cp(mapname
, FAL0
));
228 n_err(_("`colour': %s: missing attribute argument\n"),
229 n_shexp_quote_cp(mapname
, FAL0
));
233 /* Check whether preconditions are at all allowed, verify them as far as
234 * possible as necessary. For shell_quote() simplicity let's just ignore an
235 * empty precondition */
236 if((ctag
= argv
[2]) != NULL
&& *ctag
!= '\0'){
239 if(cmip
->cmi_tt
== a_COLOUR_TT_NONE
){
240 n_err(_("`colour': %s does not support preconditions\n"),
241 n_shexp_quote_cp(mapname
, FAL0
));
243 }else if((xtag
= a_colour__tag_identify(cmip
, ctag
, ®exp
)) ==
245 /* I18N: ..of colour mapping */
246 n_err(_("`colour': %s: invalid precondition: %s\n"),
247 n_shexp_quote_cp(mapname
, FAL0
), n_shexp_quote_cp(ctag
, FAL0
));
253 /* At this time we have all the information to be able to query whether such
254 * a mapping is yet established. If so, destroy it */
255 for(blcmp
= lcmp
= NULL
,
257 &a_colour_g
.cg_maps
[ct
][cmip
->cmi_ctx
][cmip
->cmi_id
]);
258 cmp
!= NULL
; blcmp
= lcmp
, lcmp
= cmp
, cmp
= cmp
->cm_next
){
259 char const *xctag
= cmp
->cm_tag
;
262 (ctag
!= NULL
&& !a_COLOUR_TAG_IS_SPECIAL(ctag
) &&
263 xctag
!= NULL
&& !a_COLOUR_TAG_IS_SPECIAL(xctag
) &&
264 !strcmp(xctag
, ctag
))){
266 *cmap
= cmp
->cm_next
;
268 lcmp
->cm_next
= cmp
->cm_next
;
269 a_colour_map_unref(cmp
);
279 if(!a_colour_iso6429(ct
, &cp
, argv
[1])){
280 /* I18N: colour command: mapping: error message: user argument */
281 n_err(_("`colour': %s: %s: %s\n"), n_shexp_quote_cp(mapname
, FAL0
),
282 cp
, n_shexp_quote_cp(argv
[1], FAL0
));
286 tl
= (ctag
!= NULL
&& !a_COLOUR_TAG_IS_SPECIAL(ctag
)) ? strlen(ctag
) : 0;
287 cmp
= smalloc(n_VSTRUCT_SIZEOF(struct a_colour_map
, cm_buf
) +
288 tl
+1 + (ul
= strlen(argv
[1])) +1 + (cl
= strlen(cp
)) +1);
291 cmp
->cm_pen
.cp_dat
.s
= bp
= cmp
->cm_buf
;
292 cmp
->cm_pen
.cp_dat
.l
= cl
;
293 memcpy(bp
, cp
, ++cl
);
296 cmp
->cm_user_off
= (ui32_t
)PTR2SIZE(bp
- cmp
->cm_buf
);
297 memcpy(bp
, argv
[1], ++ul
);
302 memcpy(bp
, ctag
, ++tl
);
307 /* Non-buf stuff; default mapping */
309 /* Default mappings must be last */
311 while(lcmp
->cm_next
!= NULL
)
312 lcmp
= lcmp
->cm_next
;
313 }else if(lcmp
->cm_next
== NULL
&& lcmp
->cm_tag
== NULL
){
314 if((lcmp
= blcmp
) == NULL
)
317 cmp
->cm_next
= lcmp
->cm_next
;
321 cmp
->cm_next
= *cmap
;
326 cmp
->cm_regex
= regexp
;
329 a_colour_map_ref(cmp
);
338 a_colour_unmux(char **argv
){
339 char const *mapname
, *ctag
, *xtag
;
340 struct a_colour_map
**cmap
, *lcmp
, *cmp
;
341 struct a_colour_map_id
const *cmip
;
342 enum a_colour_type ct
;
349 if((ct
= a_colour_type_find(*argv
++)) == (enum a_colour_type
)-1){
350 if(!n_is_all_or_aster(argv
[-1])){
351 n_err(_("`uncolour': invalid colour type %s\n"),
352 n_shexp_quote_cp(argv
[-1], FAL0
));
363 if(!a_colour_g
.cg_is_init
)
366 /* Delete anything? */
368 if(ctag
== NULL
&& mapname
[0] == '*' && mapname
[1] == '\0'){
370 struct a_colour_map
*tmp
;
372 for(i1
= 0; i1
< n__COLOUR_CTX_MAX1
; ++i1
)
373 for(i2
= 0; i2
< n__COLOUR_IDS
; ++i2
)
374 for(cmp
= *(cmap
= &a_colour_g
.cg_maps
[ct
][i1
][i2
]), *cmap
= NULL
;
378 a_colour_map_unref(tmp
);
381 if((cmip
= a_colour_map_id_find(mapname
)) == NULL
){
384 /* I18N: colour command, mapping and precondition (option in quotes) */
385 n_err(_("`uncolour': non-existing mapping: %s%s%s\n"),
386 n_shexp_quote_cp(mapname
, FAL0
), (ctag
== NULL
? n_empty
: " "),
387 (ctag
== NULL
? n_empty
: n_shexp_quote_cp(ctag
, FAL0
)));
391 if((xtag
= ctag
) != NULL
){
392 if(cmip
->cmi_tt
== a_COLOUR_TT_NONE
){
393 n_err(_("`uncolour': %s does not support preconditions\n"),
394 n_shexp_quote_cp(mapname
, FAL0
));
397 }else if((xtag
= a_colour__tag_identify(cmip
, ctag
, NULL
)) ==
399 n_err(_("`uncolour': %s: invalid precondition: %s\n"),
400 n_shexp_quote_cp(mapname
, FAL0
), n_shexp_quote_cp(ctag
, FAL0
));
404 /* (Improve user experience) */
405 if(xtag
!= NULL
&& !a_COLOUR_TAG_IS_SPECIAL(xtag
))
410 cmp
= *(cmap
= &a_colour_g
.cg_maps
[ct
][cmip
->cmi_ctx
][cmip
->cmi_id
]);
418 if((xctag
= cmp
->cm_tag
) == ctag
)
420 if(ctag
!= NULL
&& !a_COLOUR_TAG_IS_SPECIAL(ctag
) &&
421 xctag
!= NULL
&& !a_COLOUR_TAG_IS_SPECIAL(xctag
) &&
422 !strcmp(xctag
, ctag
))
429 *cmap
= cmp
->cm_next
;
431 lcmp
->cm_next
= cmp
->cm_next
;
432 a_colour_map_unref(cmp
);
436 if(aster
&& ++ct
!= a_COLOUR_T_NONE
)
444 a_colour__show(enum a_colour_type ct
){
445 struct a_colour_map
*cmp
;
450 /* Show all possible types? */
451 if((rv
= (ct
== (enum a_colour_type
)-1 ? TRU1
: FAL0
)))
454 for(i1
= 0; i1
< n__COLOUR_CTX_MAX1
; ++i1
)
455 for(i2
= 0; i2
< n__COLOUR_IDS
; ++i2
){
456 if((cmp
= a_colour_g
.cg_maps
[ct
][i1
][i2
]) == NULL
)
460 char const *tagann
, *tag
;
463 if((tag
= cmp
->cm_tag
) == NULL
)
465 else if(tag
== n_COLOUR_TAG_SUM_DOT
)
467 else if(tag
== n_COLOUR_TAG_SUM_OLDER
)
470 else if(cmp
->cm_regex
!= NULL
)
473 fprintf(n_stdout
, "colour %s %-*s %s %s%s\n",
474 a_colour_types
[ct
], a_COLOUR_MAP_SHOW_FIELDWIDTH
,
475 savecat(a_colour_ctx_prefixes
[i1
],
476 a_colour_map_ids
[i1
][i2
].cmi_name
),
477 (char const*)cmp
->cm_buf
+ cmp
->cm_user_off
,
478 tagann
, n_shexp_quote_cp(tag
, TRU1
));
483 if(rv
&& ++ct
!= a_COLOUR_T_NONE
)
491 a_colour__tag_identify(struct a_colour_map_id
const *cmip
, char const *ctag
,
496 if((cmip
->cmi_tt
& a_COLOUR_TT_DOT
) && !asccasecmp(ctag
, "dot"))
497 ctag
= n_COLOUR_TAG_SUM_DOT
;
498 else if((cmip
->cmi_tt
& a_COLOUR_TT_OLDER
) && !asccasecmp(ctag
, "older"))
499 ctag
= n_COLOUR_TAG_SUM_OLDER
;
500 else if(cmip
->cmi_tt
& a_COLOUR_TT_HEADERS
){
504 /* Can this be a valid list of headers? However, with regular expressions
505 * simply use the input as such if it appears to be a regex */
507 if(n_is_maybe_regex(ctag
)){
510 if(regexpp
!= NULL
&&
511 (s
= regcomp(*regexpp
= smalloc(sizeof(regex_t
)), ctag
,
512 REG_EXTENDED
| REG_ICASE
| REG_NOSUB
)) != 0){
513 n_err(_("`colour': invalid regular expression: %s: %s\n"),
514 n_shexp_quote_cp(ctag
, FAL0
), n_regex_err_to_doc(*regexpp
, s
));
521 /* Normalize to lowercase and strip any whitespace before use */
525 for(i
= 0; (c
= *ctag
++) != '\0';){
526 bool_t isblspc
= blankspacechar(c
);
528 if(!isblspc
&& !alnumchar(c
) && c
!= '-' && c
!= ',')
530 /* Since we compare header names as they come from the message this
531 * lowercasing is however redundant: we need to asccasecmp() them */
533 cp
[i
++] = lowerconv(c
);
540 ctag
= n_COLOUR_TAG_ERR
;
545 static struct a_colour_map_id
const *
546 a_colour_map_id_find(char const *cp
){
548 struct a_colour_map_id
const (*cmip
)[n__COLOUR_IDS
], *rv
;
554 if(i
== n__COLOUR_IDS
)
557 size_t j
= strlen(a_colour_ctx_prefixes
[i
]);
558 if(!ascncasecmp(cp
, a_colour_ctx_prefixes
[i
], j
)){
564 cmip
= &a_colour_map_ids
[i
];
567 if(i
== n__COLOUR_IDS
|| (rv
= &(*cmip
)[i
])->cmi_name
[0] == '\0'){
571 if(!asccasecmp(cp
, rv
->cmi_name
))
579 static struct a_colour_map
*
580 a_colour_map_find(enum n_colour_id cid
, enum n_colour_ctx cctx
,
582 struct a_colour_map
*cmp
;
585 cmp
= a_colour_g
.cg_maps
[a_colour_g
.cg_type
][cctx
][cid
];
586 for(; cmp
!= NULL
; cmp
= cmp
->cm_next
){
587 char const *xtag
= cmp
->cm_tag
;
593 if(ctag
== NULL
|| a_COLOUR_TAG_IS_SPECIAL(ctag
))
596 if(cmp
->cm_regex
!= NULL
){
597 if(regexec(cmp
->cm_regex
, ctag
, 0,NULL
, 0) != REG_NOMATCH
)
601 if(cmp
->cm_cmi
->cmi_tt
& a_COLOUR_TT_HEADERS
){
602 char *hlist
= savestr(xtag
), *cp
;
604 while((cp
= n_strsep(&hlist
, ',', TRU1
)) != NULL
){
605 if(!asccasecmp(cp
, ctag
))
617 a_colour_map_unref(struct a_colour_map
*self
){
619 if(--self
->cm_refcnt
== 0){
621 if(self
->cm_regex
!= NULL
){
622 regfree(self
->cm_regex
);
623 free(self
->cm_regex
);
632 a_colour_iso6429(enum a_colour_type ct
, char **store
, char const *spec
){
637 {"bold", '1'}, {"underline", '4'}, {"reverse", '7'}
639 {"black", '0'}, {"red", '1'}, {"green", '2'}, {"brown", '3'},
640 {"blue", '4'}, {"magenta", '5'}, {"cyan", '6'}, {"white", '7'}
642 char *xspec
, *cp
, fg
[3], cfg
[2 + 2*sizeof("255")];
643 ui8_t ftno_base
, ftno
;
648 /* 0/1 indicate usage, thereafter possibly 256 color sequences */
651 /* Since we use salloc(), reuse the n_strsep() buffer also for the return
652 * value, ensure we have enough room for that */
654 size_t i
= strlen(spec
) +1;
655 xspec
= salloc(n_MAX(i
, sizeof("\033[1;4;7;38;5;255;48;5;255m")));
656 memcpy(xspec
, spec
, i
);
660 /* Iterate over the colour spec */
662 while((cp
= n_strsep(&xspec
, ',', TRU1
)) != NULL
){
663 char *y
, *x
= strchr(cp
, '=');
666 *store
= n_UNCONST(_("invalid attribute list"));
671 if(!asccasecmp(cp
, "ft")){
672 if(!asccasecmp(x
, "inverse")){
673 n_OBSOLETE(_("please use reverse for ft= fonts, not inverse"));
674 x
= n_UNCONST("reverse");
676 for(idp
= fta
;; ++idp
)
677 if(idp
== fta
+ n_NELEM(fta
)){
678 *store
= n_UNCONST(_("invalid font attribute"));
680 }else if(!asccasecmp(x
, idp
->id_name
)){
681 if(ftno
< n_NELEM(fg
))
682 fg
[ftno
++] = idp
->id_modc
;
684 *store
= n_UNCONST(_("too many font attributes"));
689 }else if(!asccasecmp(cp
, "fg")){
692 }else if(!asccasecmp(cp
, "bg")){
695 if(ct
== a_COLOUR_T_1
){
696 *store
= n_UNCONST(_("colours are not allowed"));
699 /* Maybe 256 color spec */
703 if(ct
== a_COLOUR_T_8
){
704 *store
= n_UNCONST(_("invalid colour for 8-colour mode"));
708 if((n_idec_ui8_cp(&xv
, x
, 10, NULL
709 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
710 ) != n_IDEC_STATE_CONSUMED
){
711 *store
= n_UNCONST(_("invalid 256-colour specification"));
715 memcpy((y
== &cfg
[0] ? y
+ 2 : y
+ 1 + sizeof("255")), x
,
716 (x
[1] == '\0' ? 2 : (x
[2] == '\0' ? 3 : 4)));
717 }else for(idp
= ca
;; ++idp
)
718 if(idp
== ca
+ n_NELEM(ca
)){
719 *store
= n_UNCONST(_("invalid colour attribute"));
721 }else if(!asccasecmp(x
, idp
->id_name
)){
730 /* Restore our salloc() buffer, create return value */
731 xspec
= n_UNCONST(spec
);
732 if(ftno
> 0 || cfg
[0] || cfg
[1]){ /* TODO unite/share colour setters */
737 for(ftno_base
= ftno
; ftno
> 0;){
738 if(ftno
-- != ftno_base
)
751 memcpy(xspec
+ 1, "8;5;", 4);
753 for(ftno
= 2; cfg
[ftno
] != '\0'; ++ftno
)
754 *xspec
++ = cfg
[ftno
];
759 if(ftno_base
> 0 || cfg
[0])
766 memcpy(xspec
+ 1, "8;5;", 4);
768 for(ftno
= 2 + sizeof("255"); cfg
[ftno
] != '\0'; ++ftno
)
769 *xspec
++ = cfg
[ftno
];
776 *store
= n_UNCONST(spec
);
788 rv
= !a_colour_mux(v
);
798 rv
= !a_colour_unmux(v
);
804 n_colour_stack_del(struct n_go_data_ctx
*gdcp
){
805 struct n_colour_env
*vp
, *cep
;
808 vp
= gdcp
->gdc_colour
;
809 gdcp
->gdc_colour
= NULL
;
810 gdcp
->gdc_colour_active
= FAL0
;
812 while((cep
= vp
) != NULL
){
815 if(cep
->ce_current
!= NULL
&& cep
->ce_outfp
== n_stdout
){
818 hdl
= n_signal(SIGPIPE
, SIG_IGN
);
819 fwrite(a_colour_g
.cg_reset
.cp_dat
.s
, a_colour_g
.cg_reset
.cp_dat
.l
, 1,
821 fflush(cep
->ce_outfp
);
822 n_signal(SIGPIPE
, hdl
);
829 n_colour_env_create(enum n_colour_ctx cctx
, FILE *fp
, bool_t pager_used
){
830 struct n_colour_env
*cep
;
833 if(!(n_psonce
& n_PSO_INTERACTIVE
))
836 if(!a_colour_g
.cg_is_init
)
839 /* TODO reset the outer level? Iff ce_outfp==fp? */
840 cep
= salloc(sizeof *cep
);
841 cep
->ce_last
= n_go_data
->gdc_colour
;
842 cep
->ce_enabled
= FAL0
;
844 cep
->ce_ispipe
= pager_used
;
846 cep
->ce_current
= NULL
;
847 n_go_data
->gdc_colour_active
= FAL0
;
848 n_go_data
->gdc_colour
= cep
;
850 if(ok_blook(colour_disable
) || (pager_used
&& !ok_blook(colour_pager
)))
853 if(n_UNLIKELY(a_colour_g
.cg_type
== a_COLOUR_T_UNKNOWN
)){
854 struct n_termcap_value tv
;
856 if(!n_termcap_query(n_TERMCAP_QUERY_colors
, &tv
)){
857 a_colour_g
.cg_type
= a_COLOUR_T_NONE
;
860 switch(tv
.tv_data
.tvd_numeric
){
861 case 256: a_colour_g
.cg_type
= a_COLOUR_T_256
; break;
862 case 8: a_colour_g
.cg_type
= a_COLOUR_T_8
; break;
863 case 1: a_colour_g
.cg_type
= a_COLOUR_T_1
; break;
865 if(n_poption
& n_PO_D_V
)
866 n_err(_("Ignoring unsupported termcap entry for Co(lors)\n"));
869 a_colour_g
.cg_type
= a_COLOUR_T_NONE
;
874 if(a_colour_g
.cg_type
== a_COLOUR_T_NONE
)
877 n_go_data
->gdc_colour_active
= cep
->ce_enabled
= TRU1
;
883 n_colour_env_gut(void){
884 struct n_colour_env
*cep
;
887 if(!(n_psonce
& n_PSO_INTERACTIVE
))
890 /* TODO v15: Could happen because of jump, causing _stack_del().. */
891 if((cep
= n_go_data
->gdc_colour
) == NULL
)
893 n_go_data
->gdc_colour_active
= ((n_go_data
->gdc_colour
= cep
->ce_last
894 ) != NULL
&& cep
->ce_last
->ce_enabled
);
896 if(cep
->ce_current
!= NULL
){
899 hdl
= n_signal(SIGPIPE
, SIG_IGN
);
900 fwrite(a_colour_g
.cg_reset
.cp_dat
.s
, a_colour_g
.cg_reset
.cp_dat
.l
, 1,
902 n_signal(SIGPIPE
, hdl
);
909 n_colour_put(enum n_colour_id cid
, char const *ctag
){
911 if(n_COLOUR_IS_ACTIVE()){
912 struct n_colour_env
*cep
;
914 cep
= n_go_data
->gdc_colour
;
916 if(cep
->ce_current
!= NULL
)
917 fwrite(a_colour_g
.cg_reset
.cp_dat
.s
, a_colour_g
.cg_reset
.cp_dat
.l
, 1,
920 if((cep
->ce_current
= a_colour_map_find(cid
, cep
->ce_ctx
, ctag
)) != NULL
)
921 fwrite(cep
->ce_current
->cm_pen
.cp_dat
.s
,
922 cep
->ce_current
->cm_pen
.cp_dat
.l
, 1, cep
->ce_outfp
);
928 n_colour_reset(void){
930 if(n_COLOUR_IS_ACTIVE()){
931 struct n_colour_env
*cep
;
933 cep
= n_go_data
->gdc_colour
;
935 if(cep
->ce_current
!= NULL
){
936 cep
->ce_current
= NULL
;
937 fwrite(a_colour_g
.cg_reset
.cp_dat
.s
, a_colour_g
.cg_reset
.cp_dat
.l
, 1,
944 FL
struct str
const *
945 n_colour_reset_to_str(void){
949 if(n_COLOUR_IS_ACTIVE())
950 rv
= &a_colour_g
.cg_reset
.cp_dat
;
957 FL
struct n_colour_pen
*
958 n_colour_pen_create(enum n_colour_id cid
, char const *ctag
){
959 struct a_colour_map
*cmp
;
960 struct n_colour_pen
*rv
;
963 if(n_COLOUR_IS_ACTIVE() &&
964 (cmp
= a_colour_map_find(cid
, n_go_data
->gdc_colour
->ce_ctx
, ctag
)
966 union {void *vp
; char *cp
; struct n_colour_pen
*cpp
;} u
;
977 n_colour_pen_put(struct n_colour_pen
*self
){
979 if(n_COLOUR_IS_ACTIVE()){
980 union {void *vp
; char *cp
; struct a_colour_map
*cmp
;} u
;
981 struct n_colour_env
*cep
;
983 cep
= n_go_data
->gdc_colour
;
986 if(u
.cmp
!= cep
->ce_current
){
987 if(cep
->ce_current
!= NULL
)
988 fwrite(a_colour_g
.cg_reset
.cp_dat
.s
, a_colour_g
.cg_reset
.cp_dat
.l
,
992 fwrite(self
->cp_dat
.s
, self
->cp_dat
.l
, 1, cep
->ce_outfp
);
993 cep
->ce_current
= u
.cmp
;
999 FL
struct str
const *
1000 n_colour_pen_to_str(struct n_colour_pen
*self
){
1004 if(n_COLOUR_IS_ACTIVE() && self
!= NULL
)
1011 #endif /* HAVE_COLOUR */