Ejemplo n.º 1
0
result_t console_base::readLine(exlib::string msg, exlib::string &retVal,
                                AsyncEvent *ac)
{
    if (!ac)
        return CHECK_ERROR(CALL_E_LONGSYNC);

    flushLog();

#ifndef _WIN32
    static bool _init = false;
    static char *(*_readline)(const char *);
    static void (*_add_history)(char *);

#ifdef DEBUG
#ifdef __clang__
    static void (*_free)(void*);

    if (_free == 0)
        _free = (void (*)(void*))dlsym(RTLD_NEXT, "free");
#else
#define _free __real_free
#endif
#else
#define _free free
#endif

    if (!_init)
    {
        _init = true;

#ifdef __clang__
        void *handle = dlopen("libreadline.dylib", RTLD_LAZY);
#else
        const char *readline_dylib_names[] =
        {
            "libreadline.so.6",
            "libreadline.so.5",
            "libreadline.so"
        };
        const size_t readline_dylib_names_size = ARRAYSIZE(readline_dylib_names);
        void *handle = 0;

        for (size_t i = 0; i < readline_dylib_names_size; i++)
        {
            handle = dlopen(readline_dylib_names[i], RTLD_LAZY);
            if (handle) break;
        }
#endif

        if (handle)
        {
            _readline = (char *(*)(const char *))dlsym(handle, "readline");
            _add_history = (void (*)(char *))dlsym(handle, "add_history");
        } else
        {
            _readline = readline;
            _add_history = add_history;
        }
    }

    if (isatty(fileno(stdin)))
    {
        char *line;
        const char* lfptr = qstrrchr(msg.c_str(), '\n');

        if (lfptr != NULL)
        {
            puts(msg.substr(0, lfptr - msg.c_str()).c_str());
            line = _readline(lfptr + 1);
        }
        else
            line = _readline(msg.c_str());

        if (!line)
            return CHECK_ERROR(LastError());

        if (*line)
        {
            _add_history(line);
            retVal = line;
        }
        _free(line);
    } else
#endif
    {
        std_logger::out(msg);
        char *line = read_line();

        if (!line)
            return CHECK_ERROR(LastError());

        retVal = line;
        free(line);
    }

    return 0;
}
Ejemplo n.º 2
0
void Url::trimUrl(exlib::string url, exlib::string& retVal)
{
    exlib::string rest;
    int32_t start = -1;
    int32_t end = -1;
    int32_t lastPos = 0;
    int32_t i;
    bool isWs;

    bool inWs = false;

    for (i = 0; i < (int32_t)url.length(); i++) {
        isWs = url[i] == 32 || url[i] == 9 || url[i] == 13 || url[i] == 10 || url[i] == 12;

        if (*(unsigned char*)&url[i] == 0xc2 && *(unsigned char*)&url[i + 1] == 0xa0) {
            isWs = true;
            i++;
        }
        if (*(unsigned char*)&url[i] == 239 && *(unsigned char*)&url[i + 1] == 187 && *(unsigned char*)&url[i + 2] == 191) {
            isWs = true;
            i += 2;
        }

        if (start == -1) {
            if (isWs)
                continue;
            lastPos = start = i;
        } else {
            if (inWs) {
                if (!isWs) {
                    end = -1;
                    inWs = false;
                }
            } else if (isWs) {
                end = i;
                inWs = true;
            }
        }
        if (url[i] == 92 && i - lastPos > 0)
            url[i] = '/';
    }

    if (start != -1) {
        if (lastPos == start) {
            if (end == -1) {
                if (start == 0)
                    rest = url;
                else
                    rest = url.substr(start);
            } else {
                rest = url.substr(start, end - start);
            }
        } else if (end == -1 && lastPos < (int32_t)url.length()) {
            // We converted some backslashes and have only part of the entire string
            rest = url.substr(lastPos);
        } else if (end != -1 && lastPos < end) {
            // We converted some backslashes and have only part of the entire string
            rest = url.substr(lastPos, end);
        }
    }
    retVal = rest;
}