From f6d552766ba57f721dd617205e3b15496f2f757e Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Mon, 19 Jun 2023 13:19:20 +0200
Subject: g_ctx -> g
Because it's just plain noise. But I still enjoy the indicator.
---
hex.c | 410 +++++++++++++++++++++++++++++++++---------------------------------
1 file changed, 205 insertions(+), 205 deletions(-)
diff --git a/hex.c b/hex.c
index 3b6d4a9..a42459e 100644
--- a/hex.c
+++ b/hex.c
@@ -157,10 +157,10 @@ static struct app_context
struct attrs attrs[ATTRIBUTE_COUNT];
}
-g_ctx;
+g;
/// Shortcut to retrieve named terminal attributes
-#define APP_ATTR(name) g_ctx.attrs[ATTRIBUTE_ ## name].attrs
+#define APP_ATTR(name) g.attrs[ATTRIBUTE_ ## name].attrs
// --- Configuration -----------------------------------------------------------
@@ -197,7 +197,7 @@ load_config_colors (struct config_item *subtree, void *user_data)
const char *value;
#define XX(name, config, fg_, bg_, attrs_) \
if ((value = get_config_string (subtree, config))) \
- g_ctx.attrs[ATTRIBUTE_ ## name] = attrs_decode (value);
+ g.attrs[ATTRIBUTE_ ## name] = attrs_decode (value);
ATTRIBUTE_TABLE (XX)
#undef XX
}
@@ -205,7 +205,7 @@ load_config_colors (struct config_item *subtree, void *user_data)
static void
app_load_configuration (void)
{
- struct config *config = &g_ctx.config;
+ struct config *config = &g.config;
config_register_module (config, "colors", load_config_colors, NULL);
// Bootstrap configuration, so that we can access schema items at all
@@ -228,8 +228,8 @@ app_load_configuration (void)
}
if (root)
{
- config_load (&g_ctx.config, root);
- config_schema_call_changed (g_ctx.config.root);
+ config_load (&g.config, root);
+ config_schema_call_changed (g.config.root);
}
}
@@ -239,9 +239,9 @@ static void
app_init_attributes (void)
{
#define XX(name, config, fg_, bg_, attrs_) \
- g_ctx.attrs[ATTRIBUTE_ ## name].fg = fg_; \
- g_ctx.attrs[ATTRIBUTE_ ## name].bg = bg_; \
- g_ctx.attrs[ATTRIBUTE_ ## name].attrs = attrs_;
+ g.attrs[ATTRIBUTE_ ## name].fg = fg_; \
+ g.attrs[ATTRIBUTE_ ## name].bg = bg_; \
+ g.attrs[ATTRIBUTE_ ## name].attrs = attrs_;
ATTRIBUTE_TABLE (XX)
#undef XX
}
@@ -256,13 +256,13 @@ app_on_insufficient_color (void)
static void
app_init_context (void)
{
- poller_init (&g_ctx.poller);
- g_ctx.config = config_make ();
+ poller_init (&g.poller);
+ g.config = config_make ();
- ARRAY_INIT (g_ctx.marks);
- g_ctx.mark_strings = str_make ();
- ARRAY_INIT (g_ctx.marks_by_offset);
- ARRAY_INIT (g_ctx.offset_entries);
+ ARRAY_INIT (g.marks);
+ g.mark_strings = str_make ();
+ ARRAY_INIT (g.marks_by_offset);
+ ARRAY_INIT (g.offset_entries);
app_init_attributes ();
}
@@ -270,24 +270,24 @@ app_init_context (void)
static void
app_free_context (void)
{
- config_free (&g_ctx.config);
- poller_free (&g_ctx.poller);
+ config_free (&g.config);
+ poller_free (&g.poller);
- free (g_ctx.marks);
- str_free (&g_ctx.mark_strings);
- free (g_ctx.marks_by_offset);
- free (g_ctx.offset_entries);
+ free (g.marks);
+ str_free (&g.mark_strings);
+ free (g.marks_by_offset);
+ free (g.offset_entries);
- cstr_set (&g_ctx.message, NULL);
+ cstr_set (&g.message, NULL);
- cstr_set (&g_ctx.filename, NULL);
- free (g_ctx.data);
+ cstr_set (&g.filename, NULL);
+ free (g.data);
}
static void
app_quit (void)
{
- g_ctx.polling = false;
+ g.polling = false;
}
// --- Field marking -----------------------------------------------------------
@@ -296,11 +296,11 @@ app_quit (void)
static ssize_t
app_find_marks (int64_t offset)
{
- ssize_t min = 0, end = g_ctx.marks_by_offset_len;
+ ssize_t min = 0, end = g.marks_by_offset_len;
while (min < end)
{
ssize_t mid = min + (end - min) / 2;
- if (offset >= g_ctx.marks_by_offset[mid].offset)
+ if (offset >= g.marks_by_offset[mid].offset)
min = mid + 1;
else
end = mid;
@@ -312,10 +312,10 @@ static struct marks_by_offset *
app_marks_at_offset (int64_t offset)
{
ssize_t i = app_find_marks (offset);
- if (i < 0 || (size_t) i >= g_ctx.marks_by_offset_len)
+ if (i < 0 || (size_t) i >= g.marks_by_offset_len)
return NULL;
- struct marks_by_offset *marks = &g_ctx.marks_by_offset[i];
+ struct marks_by_offset *marks = &g.marks_by_offset[i];
if (marks->offset > offset)
return NULL;
return marks;
@@ -336,11 +336,11 @@ app_mark_cmp (const void *first, const void *second)
static size_t
app_store_marks (struct mark **entries, size_t len)
{
- size_t result = g_ctx.offset_entries_len;
- ARRAY_RESERVE (g_ctx.offset_entries, len);
- memcpy (g_ctx.offset_entries + g_ctx.offset_entries_len, entries,
+ size_t result = g.offset_entries_len;
+ ARRAY_RESERVE (g.offset_entries, len);
+ memcpy (g.offset_entries + g.offset_entries_len, entries,
sizeof *entries * len);
- g_ctx.offset_entries_len += len;
+ g.offset_entries_len += len;
return result;
}
@@ -356,8 +356,8 @@ app_store_marks (struct mark **entries, size_t len)
static void
app_flatten_marks (void)
{
- qsort (g_ctx.marks, g_ctx.marks_len, sizeof *g_ctx.marks, app_mark_cmp);
- if (!g_ctx.marks_len)
+ qsort (g.marks, g.marks_len, sizeof *g.marks, app_mark_cmp);
+ if (!g.marks_len)
return;
ARRAY (struct mark *, current)
@@ -365,14 +365,14 @@ app_flatten_marks (void)
int current_color = 0;
// Make offset zero actually point to an empty entry
- g_ctx.offset_entries[g_ctx.offset_entries_len++] = NULL;
+ g.offset_entries[g.offset_entries_len++] = NULL;
- struct mark *next = g_ctx.marks;
- struct mark *end = next + g_ctx.marks_len;
+ struct mark *next = g.marks;
+ struct mark *end = next + g.marks_len;
while (current_len || next < end)
{
// Find the closest offset at which marks change
- int64_t closest = g_ctx.data_offset + g_ctx.data_len;
+ int64_t closest = g.data_offset + g.data_len;
if (next < end)
closest = next->offset;
for (size_t i = 0; i < current_len; i++)
@@ -405,8 +405,8 @@ app_flatten_marks (void)
current_color %= 4;
}
- ARRAY_RESERVE (g_ctx.marks_by_offset, 1);
- g_ctx.marks_by_offset[g_ctx.marks_by_offset_len++] =
+ ARRAY_RESERVE (g.marks_by_offset, 1);
+ g.marks_by_offset[g.marks_by_offset_len++] =
(struct marks_by_offset) { closest, marks, color };
}
free (current);
@@ -441,7 +441,7 @@ static struct widget *
app_mono_padding (chtype attrs, float width, float height)
{
struct widget *w = g_xui.ui->padding (attrs, width, height);
- w->width = width * g_ctx.digitw;
+ w->width = width * g.digitw;
return w;
}
@@ -484,23 +484,23 @@ app_layout_cell (struct layout *hex, struct layout *ascii, int attrs,
struct marks_by_offset *marks = app_marks_at_offset (addr);
int attrs_mark = attrs;
if (marks && marks->color >= 0)
- attrs_mark = g_ctx.attrs[marks->color].attrs;
+ attrs_mark = g.attrs[marks->color].attrs;
- if (addr >= g_ctx.view_cursor
- && addr < g_ctx.view_cursor + 8)
+ if (addr >= g.view_cursor
+ && addr < g.view_cursor + 8)
{
attrs |= A_UNDERLINE;
attrs_mark |= A_UNDERLINE;
}
// TODO: leave it up to the user to decide what should be colored
- uint8_t cell = g_ctx.data[addr - g_ctx.data_offset];
- if (addr != g_ctx.view_cursor)
+ uint8_t cell = g.data[addr - g.data_offset];
+ if (addr != g.view_cursor)
{
char s[] = { hexa[cell >> 4], hexa[cell & 0xf], 0 };
app_push (hex, app_mono_label (attrs, s));
}
- else if (g_ctx.view_skip_nibble)
+ else if (g.view_skip_nibble)
{
char s1[] = { hexa[cell >> 4], 0 }, s2[] = { hexa[cell & 0xf], 0 };
app_push (hex, app_mono_label (attrs, s1));
@@ -530,14 +530,14 @@ app_layout_row (int64_t addr, int y, int attrs)
struct layout ascii = {};
app_push (&ascii, app_mono_padding (attrs, 2, 1));
- int64_t end_addr = g_ctx.data_offset + g_ctx.data_len;
+ int64_t end_addr = g.data_offset + g.data_len;
for (int x = 0; x < ROW_SIZE; x++)
{
if (x % 8 == 0) app_push (&hex, app_mono_padding (attrs, 1, 1));
if (x % 2 == 0) app_push (&hex, app_mono_padding (attrs, 1, 1));
int64_t cell_addr = addr + x;
- if (cell_addr < g_ctx.data_offset
+ if (cell_addr < g.data_offset
|| cell_addr >= end_addr)
{
app_push (&hex, app_mono_padding (attrs, 2, 1));
@@ -559,10 +559,10 @@ static struct widget *
app_layout_view (void)
{
struct layout l = {};
- int64_t end_addr = g_ctx.data_offset + g_ctx.data_len;
+ int64_t end_addr = g.data_offset + g.data_len;
for (int y = 0; y <= app_visible_rows (); y++)
{
- int64_t addr = g_ctx.view_top + y * ROW_SIZE;
+ int64_t addr = g.view_top + y * ROW_SIZE;
if (addr >= end_addr)
break;
@@ -579,10 +579,10 @@ app_layout_info (void)
{
const struct marks_by_offset *marks;
struct layout l = {};
- if (!(marks = app_marks_at_offset (g_ctx.view_cursor)))
+ if (!(marks = app_marks_at_offset (g.view_cursor)))
goto out;
- struct mark *mark, **iter = g_ctx.offset_entries + marks->marks;
+ struct mark *mark, **iter = g.offset_entries + marks->marks;
for (int y = 0; y <= app_visible_rows (); y++)
{
// TODO: we can use the field background
@@ -590,7 +590,7 @@ app_layout_info (void)
if (!(mark = *iter++))
break;
- const char *description = g_ctx.mark_strings.str + mark->description;
+ const char *description = g.mark_strings.str + mark->description;
app_push (&l, app_label (0, description));
}
out:
@@ -622,9 +622,9 @@ app_footer_field (char id, int size, const char *fmt, ...)
const char *coding = "";
if (size <= 1)
;
- else if (g_ctx.endianity == ENDIANITY_LE)
+ else if (g.endianity == ENDIANITY_LE)
coding = "le";
- else if (g_ctx.endianity == ENDIANITY_BE)
+ else if (g.endianity == ENDIANITY_BE)
coding = "be";
struct layout l = {};
@@ -655,7 +655,7 @@ app_footer_group (struct layout *out, int size, int64_t u, int64_t s, int align)
app_push (&l, app_footer_field ('u', size, "%" PRIu64, u));
app_push (&l, app_footer_field ('s', size, "%" PRId64, s));
l.head->width = MAX (l.head->width,
- g_ctx.digitw * align /* sign + ceil(log10(U/INT*_MAX)) */);
+ g.digitw * align /* sign + ceil(log10(U/INT*_MAX)) */);
if (out->head)
app_push (out, app_mono_padding (APP_ATTR (FOOTER), 2, 1));
app_push (out, xui_vbox (l.head));
@@ -668,11 +668,11 @@ app_layout_footer (void)
app_push (&statusl, app_label (APP_ATTR (BAR), APP_TITLE));
app_push (&statusl, g_xui.ui->padding (APP_ATTR (BAR), 1, 1));
- if (g_ctx.message)
- app_push (&statusl, app_label (APP_ATTR (BAR_HL), g_ctx.message));
- else if (g_ctx.filename)
+ if (g.message)
+ app_push (&statusl, app_label (APP_ATTR (BAR_HL), g.message));
+ else if (g.filename)
{
- char *filename = (char *) u8_strconv_from_locale (g_ctx.filename);
+ char *filename = (char *) u8_strconv_from_locale (g.filename);
app_push (&statusl, app_label (APP_ATTR (BAR_HL), filename));
free (filename);
app_push (&statusl, g_xui.ui->padding (APP_ATTR (BAR), 1, 1));
@@ -680,46 +680,46 @@ app_layout_footer (void)
app_push_hfill (&statusl, g_xui.ui->padding (APP_ATTR (BAR), 1, 1));
- char *address = xstrdup_printf ("%08" PRIx64, g_ctx.view_cursor);
+ char *address = xstrdup_printf ("%08" PRIx64, g.view_cursor);
app_push (&statusl, app_mono_label (APP_ATTR (BAR), address));
free (address);
app_push (&statusl, g_xui.ui->padding (APP_ATTR (BAR), 1, 1));
app_push (&statusl, app_mono_label (APP_ATTR (BAR),
- g_ctx.endianity == ENDIANITY_LE ? "LE" : "BE"))->id = WIDGET_ENDIANITY;
+ g.endianity == ENDIANITY_LE ? "LE" : "BE"))->id = WIDGET_ENDIANITY;
app_push (&statusl, g_xui.ui->padding (APP_ATTR (BAR), 1, 1));
- int64_t top = g_ctx.view_top;
- int64_t bot = g_ctx.view_top + app_visible_rows () * ROW_SIZE;
+ int64_t top = g.view_top;
+ int64_t bot = g.view_top + app_visible_rows () * ROW_SIZE;
struct str where = str_make ();
- if (top <= g_ctx.data_offset
- && bot >= g_ctx.data_offset + g_ctx.data_len)
+ if (top <= g.data_offset
+ && bot >= g.data_offset + g.data_len)
str_append (&where, "All");
- else if (top <= g_ctx.data_offset)
+ else if (top <= g.data_offset)
str_append (&where, "Top");
- else if (bot >= g_ctx.data_offset + g_ctx.data_len)
+ else if (bot >= g.data_offset + g.data_len)
str_append (&where, "Bot");
else
{
- int64_t end_addr = g_ctx.data_offset + g_ctx.data_len;
- int64_t cur = g_ctx.view_top / ROW_SIZE;
+ int64_t end_addr = g.data_offset + g.data_len;
+ int64_t cur = g.view_top / ROW_SIZE;
int64_t max = (end_addr - 1) / ROW_SIZE - app_visible_rows () + 1;
- cur -= g_ctx.data_offset / ROW_SIZE;
- max -= g_ctx.data_offset / ROW_SIZE;
+ cur -= g.data_offset / ROW_SIZE;
+ max -= g.data_offset / ROW_SIZE;
str_append_printf (&where, "%2d%%", (int) (100 * cur / max));
}
app_push (&statusl, app_mono_label (APP_ATTR (BAR), where.str));
str_free (&where);
- int64_t end_addr = g_ctx.data_offset + g_ctx.data_len;
- if (g_ctx.view_cursor < g_ctx.data_offset
- || g_ctx.view_cursor >= end_addr)
+ int64_t end_addr = g.data_offset + g.data_len;
+ if (g.view_cursor < g.data_offset
+ || g.view_cursor >= end_addr)
return xui_hbox (statusl.head);
- int64_t len = end_addr - g_ctx.view_cursor;
- uint8_t *p = g_ctx.data + (g_ctx.view_cursor - g_ctx.data_offset);
+ int64_t len = end_addr - g.view_cursor;
+ uint8_t *p = g.data + (g.view_cursor - g.data_offset);
// TODO: The entire bottom part perhaps should be pre-painted
// with APP_ATTR (FOOTER).
@@ -728,17 +728,17 @@ app_layout_footer (void)
app_footer_group (&groupl, 1, p[0], (int8_t) p[0], 3 + 4);
if (len >= 2)
{
- uint16_t value = app_decode (p, 2, g_ctx.endianity);
+ uint16_t value = app_decode (p, 2, g.endianity);
app_footer_group (&groupl, 2, value, (int16_t) value, 6 + 6);
}
if (len >= 4)
{
- uint32_t value = app_decode (p, 4, g_ctx.endianity);
+ uint32_t value = app_decode (p, 4, g.endianity);
app_footer_group (&groupl, 4, value, (int32_t) value, 6 + 11);
}
if (len >= 8)
{
- uint64_t value = app_decode (p, 8, g_ctx.endianity);
+ uint64_t value = app_decode (p, 8, g.endianity);
app_footer_group (&groupl, 8, value, (int64_t) value, 6 + 20);
}
@@ -828,8 +828,8 @@ static void
app_lua_coder_free (void *coder)
{
struct app_lua_coder *self = coder;
- luaL_unref (g_ctx.L, LUA_REGISTRYINDEX, self->ref_decode);
- luaL_unref (g_ctx.L, LUA_REGISTRYINDEX, self->ref_detect);
+ luaL_unref (g.L, LUA_REGISTRYINDEX, self->ref_decode);
+ luaL_unref (g.L, LUA_REGISTRYINDEX, self->ref_detect);
free (self);
}
@@ -840,7 +840,7 @@ app_lua_register (lua_State *L)
(void) app_lua_getfield (L, 1, "type", LUA_TSTRING, false);
const char *type = lua_tostring (L, -1);
- if (str_map_find (&g_ctx.coders, type))
+ if (str_map_find (&g.coders, type))
luaL_error (L, "a coder has already been registered for `%s'", type);
(void) app_lua_getfield (L, 1, "detect", LUA_TFUNCTION, true);
@@ -849,7 +849,7 @@ app_lua_register (lua_State *L)
struct app_lua_coder *coder = xcalloc (1, sizeof *coder);
coder->ref_decode = luaL_ref (L, LUA_REGISTRYINDEX);
coder->ref_detect = luaL_ref (L, LUA_REGISTRYINDEX);
- str_map_set (&g_ctx.coders, type, coder);
+ str_map_set (&g.coders, type, coder);
return 0;
}
@@ -973,12 +973,12 @@ app_lua_mark (int64_t offset, int64_t len, const char *desc)
if (len <= 0)
return;
- ARRAY_RESERVE (g_ctx.marks, 1);
- g_ctx.marks[g_ctx.marks_len++] =
- (struct mark) { offset, len, g_ctx.mark_strings.len };
+ ARRAY_RESERVE (g.marks, 1);
+ g.marks[g.marks_len++] =
+ (struct mark) { offset, len, g.mark_strings.len };
- str_append (&g_ctx.mark_strings, desc);
- str_append_c (&g_ctx.mark_strings, 0);
+ str_append (&g.mark_strings, desc);
+ str_append_c (&g.mark_strings, 0);
}
static int
@@ -986,7 +986,7 @@ app_lua_chunk_mark (lua_State *L)
{
struct app_lua_chunk *self = luaL_checkudata (L, 1, XLUA_CHUNK_METATABLE);
int n_args = lua_gettop (L);
- lua_rawgeti (L, LUA_REGISTRYINDEX, g_ctx.ref_format);
+ lua_rawgeti (L, LUA_REGISTRYINDEX, g.ref_format);
lua_insert (L, 2);
lua_call (L, n_args - 1, 1);
app_lua_mark (self->offset, self->len, luaL_checkstring (L, -1));
@@ -999,7 +999,7 @@ app_lua_chunk_identify (lua_State *L)
{
(void) luaL_checkudata (L, 1, XLUA_CHUNK_METATABLE);
- struct str_map_iter iter = str_map_iter_make (&g_ctx.coders);
+ struct str_map_iter iter = str_map_iter_make (&g.coders);
struct app_lua_coder *coder;
while ((coder = str_map_iter_next (&iter)))
{
@@ -1045,7 +1045,7 @@ app_lua_chunk_decode (lua_State *L)
// While we could call "detect" here, just to be sure, some kinds may not
// even be detectable and it's better to leave it up to the plugin
- struct app_lua_coder *coder = str_map_find (&g_ctx.coders, type);
+ struct app_lua_coder *coder = str_map_find (&g.coders, type);
if (!coder)
return luaL_error (L, "unknown type: %s", type);
@@ -1067,10 +1067,10 @@ app_lua_chunk_read (lua_State *L)
int64_t start = self->offset + self->position;
// XXX: or just return a shorter string in this case?
- if (start + len > g_ctx.data_offset + g_ctx.data_len)
+ if (start + len > g.data_offset + g.data_len)
return luaL_argerror (L, 2, "chunk is too short");
- lua_pushlstring (L, (char *) g_ctx.data + (start - g_ctx.data_offset), len);
+ lua_pushlstring (L, (char *) g.data + (start - g.data_offset), len);
self->position += len;
return 1;
}
@@ -1092,7 +1092,7 @@ app_lua_chunk_finish_read
}
// Prepare