#!/usr/bin/env python

"""
Copyright (c) 2006-2024 sqlmap developers (https://sqlmap.org/)
See the file 'LICENSE' for copying permission
"""

# 初始化readline模块变量为None
_readline = None
try:
    # 尝试导入系统自带的readline模块
    from readline import *
    import readline as _readline
except:
    try:
        # 如果系统readline导入失败,尝试导入pyreadline模块(Windows平台的readline实现)
        from pyreadline import *
        import pyreadline as _readline
    except:
        pass

# 导入所需的日志记录器和系统平台相关的设置
from lib.core.data import logger
from lib.core.settings import IS_WIN
from lib.core.settings import PLATFORM

# 在Windows平台下检查readline的输出文件
if IS_WIN and _readline:
    try:
        _outputfile = _readline.GetOutputFile()
    except AttributeError:
        # 如果获取输出文件失败,记录调试信息
        debugMsg = "Failed GetOutputFile when using platform's "
        debugMsg += "readline library"
        logger.debug(debugMsg)

        _readline = None

# 检测是否使用libedit替代GNU readline
# 感谢Boyd Waters提供这个补丁
uses_libedit = False

# 在Mac平台下检测是否使用libedit
if PLATFORM == "mac" and _readline:
    import commands

    # 使用otool命令检查readline库的依赖,查找是否包含libedit
    (status, result) = commands.getstatusoutput("otool -L %s | grep libedit" % _readline.__file__)

    if status == 0 and len(result) > 0:
        # 如果使用libedit(Leopard系统新特性),设置Tab键自动完成
        _readline.parse_and_bind("bind ^I rl_complete")

        # 记录检测到libedit的调试信息
        debugMsg = "Leopard libedit detected when using platform's "
        debugMsg += "readline library"
        logger.debug(debugMsg)

        uses_libedit = True

# clear_history()函数在Python 2.4中才引入
# 它在readline API中是可选的,所以需要显式检查其是否存在
# 某些平台可能没有这个函数
# 相关讨论见:http://mail.python.org/pipermail/python-dev/2003-August/037845.html
if _readline:
    if not hasattr(_readline, "clear_history"):
        # 如果没有clear_history函数,创建一个空实现
        def clear_history():
            pass

        # 将空实现添加到readline模块
        _readline.clear_history = clear_history
