<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import argparse
import functools

import gdb
import six

import pwndbg.chain
import pwndbg.color
import pwndbg.enhance
import pwndbg.exception
import pwndbg.hexdump
import pwndbg.memory
import pwndbg.regs
import pwndbg.symbol
import pwndbg.ui

commands = []


class Command(gdb.Command):
    """Generic command wrapper"""
    command_names = set()
    history = {}

    def __init__(self, function, prefix=False):
        command_name = function.__name__

        super(Command, self).__init__(command_name, gdb.COMMAND_USER, gdb.COMPLETE_EXPRESSION, prefix=prefix)
        self.function = function

        if command_name in self.command_names:
            raise Exception('Cannot add command %s: already exists.' % command_name)

        self.command_names.add(command_name)
        commands.append(self)

        functools.update_wrapper(self, function)
        self.__doc__ = function.__doc__

        self.repeat = False

    def split_args(self, argument):
        """Split a command-line string from the user into arguments.

        Returns:
            A ``(tuple, dict)``, in the form of ``*args, **kwargs``.
            The contents of the tuple/dict are undefined.
        """
        return gdb.string_to_argv(argument), {}
<commit_msg>Check if override built-in command (#543)<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import argparse
import functools

import gdb
import six

import pwndbg.chain
import pwndbg.color
import pwndbg.enhance
import pwndbg.exception
import pwndbg.hexdump
import pwndbg.memory
import pwndbg.regs
import pwndbg.symbol
import pwndbg.ui

commands = []

def list_current_commands():
    current_pagination = gdb.execute('show pagination', to_string=True)
    current_pagination = current_pagination.split()[-1].rstrip('.')  # Take last word and skip period

    gdb.execute('set pagination off')
    command_list = gdb.execute('help all', to_string=True).strip().split('\n')
    existing_commands = set()
    for line in command_list:
        line = line.strip()
        # Skip non-command entries
        if len(line) == 0 or line.startswith('Command class:') or line.startswith('Unclassified commands'):
            continue
        command = line.split()[0]
        existing_commands.add(command)
    gdb.execute('set pagination %s' % current_pagination) # Restore original setting
    return existing_commands

GDB_BUILTIN_COMMANDS = list_current_commands()

class Command(gdb.Command):
    """Generic command wrapper"""
    command_names = set()
    builtin_override_whitelist = {'up', 'down', 'search', 'pwd', 'start'}
    history = {}

    def __init__(self, function, prefix=False):
        command_name = function.__name__

        super(Command, self).__init__(command_name, gdb.COMMAND_USER, gdb.COMPLETE_EXPRESSION, prefix=prefix)
        self.function = function

        if command_name in self.command_names:
            raise Exception('Cannot add command %s: already exists.' % command_name)
        if command_name in GDB_BUILTIN_COMMANDS and command_name not in self.builtin_override_whitelist:
            raise Exception('Cannot override non-whitelisted built-in command "%s"' % command_name)

        self.command_names.add(command_name)
        commands.append(self)

        functools.update_wrapper(self, function)
        self.__doc__ = function.__doc__

        self.repeat = False

    def split_args(self, argument):
        """Split a command-line string from the user into arguments.

        Returns:
            A ``(tuple, dict)``, in the form of ``*args, **kwargs``.
            The contents of the tuple/dict are undefined.
        """
        return gdb.string_to_argv(argument), {}

{'fraction_non_alphanumeric': 0.09611097349516969, 'fraction_numerical': 0.0019816695566014365, 'mean_word_length': 3.3748645720476707, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5496179', 'n_tokens_mistral': 1208, 'n_tokens_neox': 1167, 'n_words': 322}