From bbccbe0c85887bfc15c57a0c5eb97e59f7cb9fa7 Mon Sep 17 00:00:00 2001
From: Peter Stephenson
Date: Tue, 9 Oct 2018 14:38:26 +0100
Subject: 43660: extend 43653 when final exit is implicit.
Combine logic for case after committed to exit (shell_exiting) with
case where exit occurred in a function we nee to unwind (exit_pending).
Add sarky note for future generations to be confused at.
---
Src/init.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
(limited to 'Src/init.c')
diff --git a/Src/init.c b/Src/init.c
index e9e6be9b4..838c2c2d1 100644
--- a/Src/init.c
+++ b/Src/init.c
@@ -157,7 +157,7 @@ loop(int toplevel, int justonce)
* Handle that now.
*/
stopmsg = 1;
- zexit(exit_pending >> 1, 0);
+ zexit(exit_val, 0);
}
if (tok == LEXERR && !lastval)
lastval = 1;
@@ -215,14 +215,14 @@ loop(int toplevel, int justonce)
clearerr(stderr);
}
if (subsh) /* how'd we get this far in a subshell? */
- exit(lastval);
+ realexit();
if (((!interact || sourcelevel) && errflag) || retflag)
break;
if (isset(SINGLECOMMAND) && toplevel) {
dont_queue_signals();
if (sigtrapped[SIGEXIT])
dotrap(SIGEXIT);
- exit(lastval);
+ realexit();
}
if (justonce)
break;
@@ -1358,7 +1358,7 @@ init_misc(char *cmd, char *zsh_name)
bshin = fdopen(SHIN, "r");
execstring(cmd, 0, 1, "cmdarg");
stopmsg = 1;
- zexit(lastval, 0);
+ zexit(exit_val ? exit_val : lastval, 0);
}
if (interact && isset(RCS))
--
cgit v1.2.3
From 0d3a786b7cff90868a586e5e3a491b82589a236d Mon Sep 17 00:00:00 2001
From: Peter Stephenson
Date: Wed, 10 Oct 2018 11:17:58 +0100
Subject: 43669: ensure explicit exit status is used over implicit
---
ChangeLog | 5 +++++
Src/builtin.c | 4 ++--
Src/init.c | 2 +-
Test/C03traps.ztst | 3 +++
4 files changed, 11 insertions(+), 3 deletions(-)
(limited to 'Src/init.c')
diff --git a/ChangeLog b/ChangeLog
index ed01701f6..12cf3ebf2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2018-10-10 Peter Stephenson
+
+ * 43669: Src/builtin.c, Src/init.c, Test/C03traps.ztst: ensure
+ explicit exit status is used rather than implicit.
+
2018-10-09 Peter Stephenson
* 43660: Src/builtin.c, Src/exec.c, Src/init.c,
diff --git a/Src/builtin.c b/Src/builtin.c
index e01e035cc..8dcdcc024 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -5723,7 +5723,7 @@ int exit_val;
void
realexit(void)
{
- exit(exit_val ? exit_val : lastval);
+ exit((shell_exiting || exit_pending) ? exit_val : lastval);
}
/* As realexit(), but call _exit instead */
@@ -5732,7 +5732,7 @@ realexit(void)
void
_realexit(void)
{
- _exit(exit_val ? exit_val : lastval);
+ _exit((shell_exiting || exit_pending) ? exit_val : lastval);
}
/* exit the shell. val is the return value of the shell. *
diff --git a/Src/init.c b/Src/init.c
index 838c2c2d1..cec914329 100644
--- a/Src/init.c
+++ b/Src/init.c
@@ -1358,7 +1358,7 @@ init_misc(char *cmd, char *zsh_name)
bshin = fdopen(SHIN, "r");
execstring(cmd, 0, 1, "cmdarg");
stopmsg = 1;
- zexit(exit_val ? exit_val : lastval, 0);
+ zexit((exit_pending || shell_exiting) ? exit_val : lastval, 0);
}
if (interact && isset(RCS))
diff --git a/Test/C03traps.ztst b/Test/C03traps.ztst
index bab0b0a0c..57daf8ddf 100644
--- a/Test/C03traps.ztst
+++ b/Test/C03traps.ztst
@@ -872,6 +872,9 @@ F:Must be tested with a top-level script rather than source or function
$ZTST_testdir/../Src/zsh -fc 'fn() { exit 13; }; trap fn EXIT'
13:Explicit exit in exit trap overrides implicit exit status
+ $ZTST_testdir/../Src/zsh -fc 'fn() { exit 0; }; trap fn EXIT; false'
+0:Explicit exit status 0 in exit trap overrides implicit non-zero status
+
%clean
rm -f TRAPEXIT
--
cgit v1.2.3
From 37d0005a9e651f366076c930019464d162506a57 Mon Sep 17 00:00:00 2001
From: Oliver Kiddle
Date: Mon, 5 Nov 2018 22:24:05 +0100
Subject: 43747: new module to map colours from hex triplets to the nearest
matching colour
---
ChangeLog | 6 ++
Src/Modules/nearcolor.c | 178 ++++++++++++++++++++++++++++++++++++++++++++++
Src/Modules/nearcolor.mdd | 5 ++
Src/init.c | 1 +
Src/prompt.c | 19 ++++-
Src/zsh.h | 7 ++
6 files changed, 215 insertions(+), 1 deletion(-)
create mode 100644 Src/Modules/nearcolor.c
create mode 100644 Src/Modules/nearcolor.mdd
(limited to 'Src/init.c')
diff --git a/ChangeLog b/ChangeLog
index 1dfcb58f9..d224f7840 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2018-11-05 Oliver Kiddle
+
+ * 43747: Src/Modules/nearcolor.c, Src/Modules/nearcolor.mdd,
+ Src/init.c, Src/prompt.c, Src/zsh.h: new module to map
+ colours from hex triplets to the nearest matching colour
+
2018-11-03 Peter Stephenson
* 43752: Completion/Base/Completer/_expand: Fix quoting if
diff --git a/Src/Modules/nearcolor.c b/Src/Modules/nearcolor.c
new file mode 100644
index 000000000..2a763d470
--- /dev/null
+++ b/Src/Modules/nearcolor.c
@@ -0,0 +1,178 @@
+#include "nearcolor.mdh"
+#include "nearcolor.pro"
+
+#include
+
+struct cielab {
+ float L, a, b;
+};
+typedef struct cielab *Cielab;
+
+static float
+deltae(Cielab lab1, Cielab lab2)
+{
+ /* taking square root unnecessary as we're just comparing values */
+ return powf(lab1->L - lab2->L, 2) +
+ powf(lab1->a - lab2->a, 2) +
+ powf(lab1->b - lab2->b, 2);
+}
+
+static void
+RGBtoLAB(int red, int green, int blue, Cielab lab)
+{
+ float R = (float)red / 255.0;
+ float G = (float)green / 255.0;
+ float B = (float)blue / 255.0;
+ R = 100.0 * (R > 0.04045 ? powf((R + 0.055) / 1.055, 2.4) : R / 12.92);
+ G = 100.0 * (G > 0.04045 ? powf((G + 0.055) / 1.055, 2.4) : G / 12.92);
+ B = 100.0 * (B > 0.04045 ? powf((B + 0.055) / 1.055, 2.4) : B / 12.92);
+
+ /* Observer. = 2 degrees, Illuminant = D65 */
+ float X = (R * 0.4124 + G * 0.3576 + B * 0.1805) / 95.047;
+ float Y = (R * 0.2126 + G * 0.7152 + B * 0.0722) / 100.0;
+ float Z = (R * 0.0193 + G * 0.1192 + B * 0.9505) / 108.883;
+
+ X = (X > 0.008856) ? powf(X, 1.0/3.0) : (7.787 * X) + (16.0 / 116.0);
+ Y = (Y > 0.008856) ? powf(Y, 1.0/3.0) : (7.787 * Y) + (16.0 / 116.0);
+ Z = (Z > 0.008856) ? powf(Z, 1.0/3.0) : (7.787 * Z) + (16.0 / 116.0);
+
+ lab->L = (116.0 * Y) - 16.0;
+ lab->a = 500.0 * (X - Y);
+ lab->b = 200.0 * (Y - Z);
+}
+
+static int
+mapRGBto88(int red, int green, int blue)
+{
+ int component[] = { 0, 0x8b, 0xcd, 0xff, 0x2e, 0x5c, 0x8b, 0xa2, 0xb9, 0xd0, 0xe7 };
+ struct cielab orig, next;
+ float nextl, bestl = -1;
+ int r, g, b;
+ int comp_r = 0, comp_g = 0, comp_b = 0;
+
+ /* Get original value */
+ RGBtoLAB(red, green, blue, &orig);
+
+ /* try every one of the 72 colours */
+ for (r = 0; r < 11; r++) {
+ for (g = 0; g <= 3; g++) {
+ for (b = 0; b <= 3; b++) {
+ if (r > 3) g = b = r; /* advance inner loops to the block of greys */
+ RGBtoLAB(component[r], component[g], component[b], &next);
+ nextl = deltae(&orig, &next);
+ if (nextl < bestl || bestl < 0) {
+ bestl = nextl;
+ comp_r = r;
+ comp_g = g;
+ comp_b = b;
+ }
+ }
+ }
+ }
+
+ return (comp_r > 3) ? 77 + comp_r :
+ 16 + (comp_r * 16) + (comp_g * 4) + comp_b;
+}
+
+/*
+ * Convert RGB to nearest colour in the 256 colour range
+ */
+static int
+mapRGBto256(int red, int green, int blue)
+{
+ int component[] = {
+ 0, 0x5f, 0x87, 0xaf, 0xd7, 0xff,
+ 0x8, 0x12, 0x1c, 0x26, 0x30, 0x3a, 0x44, 0x4e,
+ 0x58, 0x62, 0x6c, 0x76, 0x80, 0x8a, 0x94, 0x9e,
+ 0xa8, 0xb2, 0xbc, 0xc6, 0xd0, 0xda, 0xe4, 0xee
+ };
+ struct cielab orig, next;
+ float nextl, bestl = -1;
+ int r, g, b;
+ int comp_r = 0, comp_g = 0, comp_b = 0;
+
+ /* Get original value */
+ RGBtoLAB(red, green, blue, &orig);
+
+ for (r = 0; r < sizeof(component)/sizeof(*component); r++) {
+ for (g = 0; g <= 5; g++) {
+ for (b = 0; b <= 5; b++) {
+ if (r > 5) g = b = r; /* advance inner loops to the block of greys */
+ RGBtoLAB(component[r], component[g], component[b], &next);
+ nextl = deltae(&orig, &next);
+ if (nextl < bestl || bestl < 0) {
+ bestl = nextl;
+ comp_r = r;
+ comp_g = g;
+ comp_b = b;
+ }
+ }
+ }
+ }
+
+ return (comp_r > 5) ? 226 + comp_r :
+ 16 + (comp_r * 36) + (comp_g * 6) + comp_b;
+}
+
+static int
+getnearestcolor(UNUSED(Hookdef dummy), Color_rgb col)
+{
+ if (tccolours == 256)
+ return mapRGBto256(col->red, col->green, col->blue);
+ if (tccolours == 88)
+ return mapRGBto88(col->red, col->green, col->blue);
+ return 0;
+}
+
+static struct features module_features = {
+ NULL, 0,
+ NULL, 0,
+ NULL, 0,
+ NULL, 0,
+ 0
+};
+
+/**/
+int
+setup_(UNUSED(Module m))
+{
+ return 0;
+}
+
+/**/
+int
+features_(Module m, char ***features)
+{
+ *features = featuresarray(m, &module_features);
+ return 0;
+}
+
+/**/
+int
+enables_(Module m, int **enables)
+{
+ return handlefeatures(m, &module_features, enables);
+}
+
+/**/
+int
+boot_(Module m)
+{
+ addhookfunc("get_color_attr", (Hookfn) getnearestcolor);
+ return 0;
+}
+
+/**/
+int
+cleanup_(Module m)
+{
+ deletehookfunc("get_color_attr", (Hookfn) getnearestcolor);
+ return setfeatureenables(m, &module_features, NULL);
+}
+
+/**/
+int
+finish_(UNUSED(Module m))
+{
+ return 0;
+}
diff --git a/Src/Modules/nearcolor.mdd b/Src/Modules/nearcolor.mdd
new file mode 100644
index 000000000..2fcdaf04e
--- /dev/null
+++ b/Src/Modules/nearcolor.mdd
@@ -0,0 +1,5 @@
+name=zsh/nearcolor
+link=dynamic
+load=no
+
+objects="nearcolor.o"
diff --git a/Src/init.c b/Src/init.c
index cec914329..e7e62e2f7 100644
--- a/Src/init.c
+++ b/Src/init.c
@@ -94,6 +94,7 @@ mod_export struct hookdef zshhooks[] = {
HOOKDEF("exit", NULL, HOOKF_ALL),
HOOKDEF("before_trap", NULL, HOOKF_ALL),
HOOKDEF("after_trap", NULL, HOOKF_ALL),
+ HOOKDEF("get_color_attr", NULL, HOOKF_ALL),
};
/* keep executing lists until EOF found */
diff --git a/Src/prompt.c b/Src/prompt.c
index 959ed8e3d..39edbdb2b 100644
--- a/Src/prompt.c
+++ b/Src/prompt.c
@@ -1621,7 +1621,24 @@ match_colour(const char **teststrp, int is_fg, int colour)
int shft, on, named = 0, tc;
if (teststrp) {
- if ((named = ialpha(**teststrp))) {
+ if (**teststrp == '#' && isxdigit((*teststrp)[1])) {
+ struct color_rgb color;
+ char *end;
+ zlong col = zstrtol(*teststrp+1, &end, 16);
+ if (end - *teststrp == 4) {
+ color.red = col >> 8 | ((col >> 8) << 4);
+ color.green = (col & 0xf0) >> 4;
+ color.green |= color.green << 4;
+ color.blue = col & 0xf;
+ color.blue |= color.blue << 4;
+ } else if (end - *teststrp == 7) {
+ color.red = col >> 16;
+ color.green = (col & 0xff00) >> 8;
+ color.blue = col & 0xff;
+ }
+ *teststrp = end;
+ colour = runhookdef(GETCOLORATTR, &color);
+ } else if ((named = ialpha(**teststrp))) {
colour = match_named_colour(teststrp);
if (colour == 8) {
/* default */
diff --git a/Src/zsh.h b/Src/zsh.h
index 894158818..68731e226 100644
--- a/Src/zsh.h
+++ b/Src/zsh.h
@@ -2707,6 +2707,12 @@ struct ttyinfo {
#define COL_SEQ_BG (1)
#define COL_SEQ_COUNT (2)
+struct color_rgb {
+ unsigned int red, green, blue;
+};
+
+typedef struct color_rgb *Color_rgb;
+
/*
* Flags to testcap() and set_colour_attribute (which currently only
* handles TSC_PROMPT).
@@ -3203,6 +3209,7 @@ enum {
#define EXITHOOK (zshhooks + 0)
#define BEFORETRAPHOOK (zshhooks + 1)
#define AFTERTRAPHOOK (zshhooks + 2)
+#define GETCOLORATTR (zshhooks + 3)
#ifdef MULTIBYTE_SUPPORT
/* Final argument to mb_niceformat() */
--
cgit v1.2.3