Example #1
0
char *
gui_color_decode (const char *string, const char *replacement)
{
    const unsigned char *ptr_string;
    unsigned char *out;
    int out_length, out_pos, length;
    
    if (!string)
        return NULL;
    
    out_length = (strlen ((char *)string) * 2) + 1;
    out = malloc (out_length);
    if (!out)
        return NULL;
    
    ptr_string = (unsigned char *)string;
    out_pos = 0;
    while (ptr_string && ptr_string[0] && (out_pos < out_length - 1))
    {
        switch (ptr_string[0])
        {
            case GUI_COLOR_COLOR_CHAR:
                ptr_string++;
                if (replacement && replacement[0])
                {
                    out[out_pos] = replacement[0];
                    out_pos++;
                }
                else
                {
                    switch (ptr_string[0])
                    {
                        case GUI_COLOR_FG_CHAR:
                        case GUI_COLOR_BG_CHAR:
                            if (ptr_string[1] && ptr_string[2])
                                ptr_string += 3;
                            break;
                        case GUI_COLOR_FG_BG_CHAR:
                            if (ptr_string[1] && ptr_string[2] && (ptr_string[3] == ',')
                                && ptr_string[4] && ptr_string[5])
                                ptr_string += 6;
                            break;
                        case GUI_COLOR_BAR_CHAR:
                            ptr_string++;
                            switch (ptr_string[0])
                            {
                                case GUI_COLOR_BAR_FG_CHAR:
                                case GUI_COLOR_BAR_BG_CHAR:
                                case GUI_COLOR_BAR_DELIM_CHAR:
                                case GUI_COLOR_BAR_START_INPUT_CHAR:
                                case GUI_COLOR_BAR_MOVE_CURSOR_CHAR:
                                    ptr_string++;
                                    break;
                            }
                            break;
                        default:
                            if (isdigit (ptr_string[0]) && isdigit (ptr_string[1]))
                                ptr_string += 2;
                            break;
                    }
                }
                break;
            case GUI_COLOR_SET_WEECHAT_CHAR:
            case GUI_COLOR_REMOVE_WEECHAT_CHAR:
                ptr_string++;
                if (replacement && replacement[0])
                {
                    out[out_pos] = replacement[0];
                    out_pos++;
                }
                else
                {
                    if (ptr_string[0])
                        ptr_string++;
                }
                break;
            case GUI_COLOR_RESET_CHAR:
                ptr_string++;
                if (replacement && replacement[0])
                {
                    out[out_pos] = replacement[0];
                    out_pos++;
                }
                break;
            default:
                length = utf8_char_size ((char *)ptr_string);
                if (length == 0)
                    length = 1;
                memcpy (out + out_pos, ptr_string, length);
                out_pos += length;
                ptr_string += length;
                break;
        }
    }
    out[out_pos] = '\0';
    
    return (char *)out;
}
Example #2
0
char *
gui_color_decode (const char *string, const char *replacement)
{
    const unsigned char *ptr_string;
    unsigned char *out;
    int out_length, out_pos, length;

    if (!string)
        return NULL;

    out_length = (strlen ((char *)string) * 2) + 1;
    out = malloc (out_length);
    if (!out)
        return NULL;

    ptr_string = (unsigned char *)string;
    out_pos = 0;
    while (ptr_string && ptr_string[0] && (out_pos < out_length - 1))
    {
        switch (ptr_string[0])
        {
            case GUI_COLOR_COLOR_CHAR:
                ptr_string++;
                switch (ptr_string[0])
                {
                    case GUI_COLOR_FG_CHAR:
                        ptr_string++;
                        if (ptr_string[0] == GUI_COLOR_EXTENDED_CHAR)
                        {
                            ptr_string++;
                            while (gui_color_attr_get_flag (ptr_string[0]) > 0)
                            {
                                ptr_string++;
                            }
                            if (ptr_string[0] && ptr_string[1] && ptr_string[2]
                                && ptr_string[3] && ptr_string[4])
                            {
                                ptr_string += 5;
                            }
                        }
                        else
                        {
                            while (gui_color_attr_get_flag (ptr_string[0]) > 0)
                            {
                                ptr_string++;
                            }
                            if (ptr_string[0] && ptr_string[1])
                                ptr_string += 2;
                        }
                        break;
                    case GUI_COLOR_BG_CHAR:
                        ptr_string++;
                        if (ptr_string[0] == GUI_COLOR_EXTENDED_CHAR)
                        {
                            ptr_string++;
                            if (ptr_string[0] && ptr_string[1] && ptr_string[2]
                                && ptr_string[3] && ptr_string[4])
                            {
                                ptr_string += 5;
                            }
                        }
                        else
                        {
                            if (ptr_string[0] && ptr_string[1])
                                ptr_string += 2;
                        }
                        break;
                    case GUI_COLOR_FG_BG_CHAR:
                        ptr_string++;
                        if (ptr_string[0] == GUI_COLOR_EXTENDED_CHAR)
                        {
                            ptr_string++;
                            while (gui_color_attr_get_flag (ptr_string[0]) > 0)
                            {
                                ptr_string++;
                            }
                            if (ptr_string[0] && ptr_string[1] && ptr_string[2]
                                && ptr_string[3] && ptr_string[4])
                            {
                                ptr_string += 5;
                            }
                        }
                        else
                        {
                            while (gui_color_attr_get_flag (ptr_string[0]) > 0)
                            {
                                ptr_string++;
                            }
                            if (ptr_string[0] && ptr_string[1])
                                ptr_string += 2;
                        }
                        if (ptr_string[0] == ',')
                        {
                            if (ptr_string[1] == GUI_COLOR_EXTENDED_CHAR)
                            {
                                if (ptr_string[2] && ptr_string[3]
                                    && ptr_string[4] && ptr_string[5]
                                    && ptr_string[6])
                                {
                                    ptr_string += 7;
                                }
                            }
                            else
                            {
                                if (ptr_string[1] && ptr_string[2])
                                    ptr_string += 3;
                            }
                        }
                        break;
                    case GUI_COLOR_EXTENDED_CHAR:
                        if ((isdigit (ptr_string[1])) && (isdigit (ptr_string[2]))
                            && (isdigit (ptr_string[3])) && (isdigit (ptr_string[4]))
                            && (isdigit (ptr_string[5])))
                            ptr_string += 6;
                        break;
                    case GUI_COLOR_EMPHASIS_CHAR:
                        ptr_string++;
                        break;
                    case GUI_COLOR_BAR_CHAR:
                        ptr_string++;
                        switch (ptr_string[0])
                        {
                            case GUI_COLOR_BAR_FG_CHAR:
                            case GUI_COLOR_BAR_BG_CHAR:
                            case GUI_COLOR_BAR_DELIM_CHAR:
                            case GUI_COLOR_BAR_START_INPUT_CHAR:
                            case GUI_COLOR_BAR_START_INPUT_HIDDEN_CHAR:
                            case GUI_COLOR_BAR_MOVE_CURSOR_CHAR:
                            case GUI_COLOR_BAR_START_ITEM:
                            case GUI_COLOR_BAR_START_LINE_ITEM:
                                ptr_string++;
                                break;
                        }
                        break;
                    case GUI_COLOR_RESET_CHAR:
                        ptr_string++;
                        break;
                    default:
                        if (isdigit (ptr_string[0]) && isdigit (ptr_string[1]))
                            ptr_string += 2;
                        break;
                }
                if (replacement && replacement[0])
                {
                    out[out_pos] = replacement[0];
                    out_pos++;
                }
                break;
            case GUI_COLOR_SET_ATTR_CHAR:
            case GUI_COLOR_REMOVE_ATTR_CHAR:
                ptr_string++;
                if (ptr_string[0])
                    ptr_string++;
                if (replacement && replacement[0])
                {
                    out[out_pos] = replacement[0];
                    out_pos++;
                }
                break;
            case GUI_COLOR_RESET_CHAR:
                ptr_string++;
                if (replacement && replacement[0])
                {
                    out[out_pos] = replacement[0];
                    out_pos++;
                }
                break;
            default:
                length = utf8_char_size ((char *)ptr_string);
                if (length == 0)
                    length = 1;
                memcpy (out + out_pos, ptr_string, length);
                out_pos += length;
                ptr_string += length;
                break;
        }
    }
    out[out_pos] = '\0';

    return (char *)out;
}
Example #3
0
int
input_data (struct t_gui_buffer *buffer, const char *data)
{
    char *pos, *buf, str_buffer[128], *new_data, *buffer_full_name;
    const char *ptr_data, *ptr_data_for_buffer;
    int length, char_size, first_command, rc;

    rc = WEECHAT_RC_OK;

    if (!buffer || !gui_buffer_valid (buffer)
        || !data || !data[0] || (data[0] == '\r') || (data[0] == '\n'))
    {
        return WEECHAT_RC_ERROR;
    }

    buffer_full_name = strdup (buffer->full_name);
    if (!buffer_full_name)
        return WEECHAT_RC_ERROR;

    /* execute modifier "input_text_for_buffer" */
    snprintf (str_buffer, sizeof (str_buffer),
              "0x%lx", (long unsigned int)buffer);
    new_data = hook_modifier_exec (NULL,
                                   "input_text_for_buffer",
                                   str_buffer,
                                   data);

    /* data was dropped? */
    if (new_data && !new_data[0])
        goto end;

    first_command = 1;
    ptr_data = (new_data) ? new_data : data;
    while (ptr_data && ptr_data[0])
    {
        /*
         * if the buffer pointer is not valid any more (or if it's another
         * buffer), use the current buffer for the next command
         */
        if (!first_command
            && (!gui_buffer_valid (buffer)
                || (strcmp (buffer->full_name, buffer_full_name) != 0)))
        {
            if (!gui_current_window || !gui_current_window->buffer)
                break;
            buffer = gui_current_window->buffer;
            free (buffer_full_name);
            buffer_full_name = strdup (buffer->full_name);
            if (!buffer_full_name)
                break;
        }

        pos = strchr (ptr_data, '\n');
        if (pos)
            pos[0] = '\0';

        ptr_data_for_buffer = string_input_for_buffer (ptr_data);
        if (ptr_data_for_buffer)
        {
            /*
             * input string is NOT a command, send it to buffer input
             * callback
             */
            if (string_is_command_char (ptr_data_for_buffer))
            {
                char_size = utf8_char_size (ptr_data_for_buffer);
                length = strlen (ptr_data_for_buffer) + char_size + 1;
                buf = malloc (length);
                if (buf)
                {
                    memcpy (buf, ptr_data_for_buffer, char_size);
                    snprintf (buf + char_size, length - char_size,
                              "%s", ptr_data_for_buffer);
                    input_exec_data (buffer, buf);
                    free (buf);
                }
            }
            else
                input_exec_data (buffer, ptr_data_for_buffer);
        }
        else
        {
            /* input string is a command */
            rc = input_exec_command (buffer, 1, buffer->plugin, ptr_data);
        }

        if (pos)
        {
            pos[0] = '\n';
            ptr_data = pos + 1;
        }
        else
            ptr_data = NULL;

        first_command = 0;
    }

end:
    if (new_data)
        free (new_data);
    if (buffer_full_name)
        free (buffer_full_name);

    return rc;
}
Example #4
0
void
input_data (struct t_gui_buffer *buffer, const char *data)
{
    char *pos, *buf, str_buffer[128], *new_data;
    const char *ptr_data, *ptr_data_for_buffer;
    int length, char_size;

    if (!buffer || !data || !data[0] || (data[0] == '\r') || (data[0] == '\n'))
        return;

    /* execute modifier "input_text_for_buffer" */
    snprintf (str_buffer, sizeof (str_buffer),
              "0x%lx", (long unsigned int)buffer);
    new_data = hook_modifier_exec (NULL,
                                   "input_text_for_buffer",
                                   str_buffer,
                                   data);

    /* data not dropped? */
    if (!new_data || new_data[0])
    {
        ptr_data = (new_data) ? new_data : data;
        while (ptr_data && ptr_data[0])
        {
            pos = strchr (ptr_data, '\n');
            if (pos)
                pos[0] = '\0';

            ptr_data_for_buffer = string_input_for_buffer (ptr_data);
            if (ptr_data_for_buffer)
            {
                /*
                 * input string is NOT a command, send it to buffer input
                 * callback
                 */
                if (string_is_command_char (ptr_data_for_buffer))
                {
                    char_size = utf8_char_size (ptr_data_for_buffer);
                    length = strlen (ptr_data_for_buffer) + char_size + 1;
                    buf = malloc (length);
                    if (buf)
                    {
                        memcpy (buf, ptr_data_for_buffer, char_size);
                        snprintf (buf + char_size, length - char_size,
                                  "%s", ptr_data_for_buffer);
                        input_exec_data (buffer, buf);
                        free (buf);
                    }
                }
                else
                    input_exec_data (buffer, ptr_data_for_buffer);
            }
            else
            {
                /* input string is a command */
                input_exec_command (buffer, 1, buffer->plugin, ptr_data);
            }

            if (pos)
            {
                pos[0] = '\n';
                ptr_data = pos + 1;
            }
            else
                ptr_data = NULL;
        }
    }

    if (new_data)
        free (new_data);
}