Skip to content

Commit

Permalink
highlight_table --> gef.ui.highlight_table
Browse files Browse the repository at this point in the history
  • Loading branch information
hugsy committed Jan 7, 2022
1 parent eb7312b commit d83b239
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions gef.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ def update_gef(argv):
sys.exit(0)

gef = None
__functions__ = []
__aliases__ = []
__watches__ = {}
__gef_convenience_vars_index__ = 0
__context_messages__ = []
Expand All @@ -164,11 +162,8 @@ def update_gef(argv):

libc_args_definitions = {}

highlight_table = {}
ANSI_SPLIT_RE = r"(\033\[[\d;]*m)"

# idea: lru_cache wrapper that register back callback items for cache clearing

def reset_all_caches():
"""Free all caches. If an object is cached, it will have a callable attribute `cache_clear`
which will be invoked to purge the function cache."""
Expand Down Expand Up @@ -200,26 +195,27 @@ def reset():

def highlight_text(text):
"""
Highlight text using highlight_table { match -> color } settings.
Highlight text using gef.ui.highlight_table { match -> color } settings.
If RegEx is enabled it will create a match group around all items in the
highlight_table and wrap the specified color in the highlight_table
gef.ui.highlight_table and wrap the specified color in the gef.ui.highlight_table
around those matches.
If RegEx is disabled, split by ANSI codes and 'colorify' each match found
within the specified string.
"""
if not highlight_table:
global gef
if not gef.ui.highlight_table:
return text

if gef.config["highlight.regex"]:
for match, color in highlight_table.items():
for match, color in gef.ui.highlight_table.items():
text = re.sub("(" + match + ")", Color.colorify("\\1", color), text)
return text

ansiSplit = re.split(ANSI_SPLIT_RE, text)

for match, color in highlight_table.items():
for match, color in gef.ui.highlight_table.items():
for index, val in enumerate(ansiSplit):
found = val.find(match)
if found > -1:
Expand Down Expand Up @@ -4428,8 +4424,8 @@ def register_priority_command(cls):

def register_function(cls):
"""Decorator for registering a new convenience function to GDB."""
global __functions__
__functions__.append(cls)
global gef
gef.session.functions.append(cls)
return cls


Expand Down Expand Up @@ -9999,11 +9995,11 @@ class HighlightListCommand(GenericCommand):
_syntax_ = _cmdline_

def print_highlight_table(self):
if not highlight_table:
if not gef.ui.highlight_table:
return err("no matches found")

left_pad = max(map(len, highlight_table.keys()))
for match, color in sorted(highlight_table.items()):
left_pad = max(map(len, gef.ui.highlight_table.keys()))
for match, color in sorted(gef.ui.highlight_table.items()):
print("{} {} {}".format(Color.colorify(match.ljust(left_pad), color), VERTICAL_LINE, Color.colorify(color, color)))
return

Expand All @@ -10019,7 +10015,7 @@ class HighlightClearCommand(GenericCommand):
_syntax_ = _cmdline_

def do_invoke(self, argv):
return highlight_table.clear()
return gef.ui.highlight_table.clear()


@register_command
Expand All @@ -10035,7 +10031,7 @@ def do_invoke(self, argv):
return self.usage()

match, color = argv
highlight_table[match] = color
gef.ui.highlight_table[match] = color
return


Expand All @@ -10057,7 +10053,7 @@ def do_invoke(self, argv):
if not argv:
return self.usage()

highlight_table.pop(argv[0], None)
gef.ui.highlight_table.pop(argv[0], None)
return


Expand Down Expand Up @@ -10558,7 +10554,7 @@ def load(self, initial=False):
self.commands = [(x._cmdline_, x) for x in gef.session.commands]

# load all of the functions
for function_class_name in __functions__:
for function_class_name in gef.session.functions:
self.loaded_functions.append(function_class_name())

def is_loaded(x):
Expand Down Expand Up @@ -10795,7 +10791,7 @@ def invoke(self, args, from_tty):

# save the aliases
cfg.add_section("aliases")
for alias in __aliases__:
for alias in gef.session.aliases:
cfg.set("aliases", alias._alias, alias._command)

with open(GEF_RC, "w") as fd:
Expand Down Expand Up @@ -10935,7 +10931,7 @@ def __init__(self, alias, command, completer_class=gdb.COMPLETE_NONE, command_cl
if not p:
return

if any(x for x in __aliases__ if x._alias == alias):
if any(x for x in gef.session.aliases if x._alias == alias):
return

self._command = command
Expand All @@ -10951,7 +10947,7 @@ def __init__(self, alias, command, completer_class=gdb.COMPLETE_NONE, command_cl
self.complete = _instance.complete

super().__init__(alias, command_class, completer_class=completer_class)
__aliases__.append(self)
gef.session.aliases.append(self)
return

def invoke(self, args, from_tty):
Expand Down Expand Up @@ -11012,13 +11008,13 @@ def __init__(self):
return

def do_invoke(self, argv):
global __aliases__
global gef
if len(argv) != 1:
self.usage()
return
try:
alias_to_remove = next(filter(lambda x: x._alias == argv[0], __aliases__))
__aliases__.remove(alias_to_remove)
alias_to_remove = next(filter(lambda x: x._alias == argv[0], gef.session.aliases))
gef.session.aliases.remove(alias_to_remove)
except (ValueError, StopIteration):
err("{0} not found in aliases.".format(argv[0]))
return
Expand All @@ -11038,7 +11034,7 @@ def __init__(self):

def do_invoke(self, argv):
ok("Aliases defined:")
for a in __aliases__:
for a in gef.session.aliases:
gef_print("{:30s} {} {}".format(a._alias, RIGHT_ARROW, a._command))
return

Expand Down Expand Up @@ -11384,6 +11380,8 @@ def __init__(self):
self.remote = None
self.qemu_mode = False
self.commands = []
self.functions = []
self.aliases = []
return

def reset_caches(self):
Expand Down Expand Up @@ -11474,6 +11472,7 @@ def __init__(self):
self.output_fd = None
self.context_hidden = False
self.stream_buffer = None
self.highlight_table = {}
return

class Gef:
Expand Down

0 comments on commit d83b239

Please sign in to comment.