static void readline_completion(ReadLineState *rs)

{

    int len, i, j, max_width, nb_cols, max_prefix;

    char *cmdline;



    rs->nb_completions = 0;



    cmdline = g_malloc(rs->cmd_buf_index + 1);

    memcpy(cmdline, rs->cmd_buf, rs->cmd_buf_index);

    cmdline[rs->cmd_buf_index] = '\0';

    rs->completion_finder(rs->mon, cmdline);

    g_free(cmdline);



    /* no completion found */

    if (rs->nb_completions <= 0)

        return;

    if (rs->nb_completions == 1) {

        len = strlen(rs->completions[0]);

        for(i = rs->completion_index; i < len; i++) {

            readline_insert_char(rs, rs->completions[0][i]);

        }

        /* extra space for next argument. XXX: make it more generic */

        if (len > 0 && rs->completions[0][len - 1] != '/')

            readline_insert_char(rs, ' ');

    } else {

        monitor_printf(rs->mon, "\n");

        max_width = 0;

        max_prefix = 0;	

        for(i = 0; i < rs->nb_completions; i++) {

            len = strlen(rs->completions[i]);

            if (i==0) {

                max_prefix = len;

            } else {

                if (len < max_prefix)

                    max_prefix = len;

                for(j=0; j<max_prefix; j++) {

                    if (rs->completions[i][j] != rs->completions[0][j])

                        max_prefix = j;

                }

            }

            if (len > max_width)

                max_width = len;

        }

        if (max_prefix > 0) 

            for(i = rs->completion_index; i < max_prefix; i++) {

                readline_insert_char(rs, rs->completions[0][i]);

            }

        max_width += 2;

        if (max_width < 10)

            max_width = 10;

        else if (max_width > 80)

            max_width = 80;

        nb_cols = 80 / max_width;

        j = 0;

        for(i = 0; i < rs->nb_completions; i++) {

            monitor_printf(rs->mon, "%-*s", max_width, rs->completions[i]);

            if (++j == nb_cols || i == (rs->nb_completions - 1)) {

                monitor_printf(rs->mon, "\n");

                j = 0;

            }

        }

        readline_show_prompt(rs);

    }

    for (i = 0; i < rs->nb_completions; i++) {

        g_free(rs->completions[i]);

    }

}

static void readline_completion(ReadLineState *rs)

{

    int len, i, j, max_width, nb_cols, max_prefix;

    char *cmdline;



    rs->nb_completions = 0;



    cmdline = g_malloc(rs->cmd_buf_index + 1);

    memcpy(cmdline, rs->cmd_buf, rs->cmd_buf_index);

    cmdline[rs->cmd_buf_index] = '\0';

    rs->completion_finder(rs->mon, cmdline);

    g_free(cmdline);



    

    if (rs->nb_completions <= 0)

        return;

    if (rs->nb_completions == 1) {

        len = strlen(rs->completions[0]);

        for(i = rs->completion_index; i < len; i++) {

            readline_insert_char(rs, rs->completions[0][i]);

        }

        

        if (len > 0 && rs->completions[0][len - 1] != '/')

            readline_insert_char(rs, ' ');

    } else {

        monitor_printf(rs->mon, "\n");

        max_width = 0;

        max_prefix = 0;	

        for(i = 0; i < rs->nb_completions; i++) {

            len = strlen(rs->completions[i]);

            if (i==0) {

                max_prefix = len;

            } else {

                if (len < max_prefix)

                    max_prefix = len;

                for(j=0; j<max_prefix; j++) {

                    if (rs->completions[i][j] != rs->completions[0][j])

                        max_prefix = j;

                }

            }

            if (len > max_width)

                max_width = len;

        }

        if (max_prefix > 0) 

            for(i = rs->completion_index; i < max_prefix; i++) {

                readline_insert_char(rs, rs->completions[0][i]);

            }

        max_width += 2;

        if (max_width < 10)

            max_width = 10;

        else if (max_width > 80)

            max_width = 80;

        nb_cols = 80 / max_width;

        j = 0;

        for(i = 0; i < rs->nb_completions; i++) {

            monitor_printf(rs->mon, "%-*s", max_width, rs->completions[i]);

            if (++j == nb_cols || i == (rs->nb_completions - 1)) {

                monitor_printf(rs->mon, "\n");

                j = 0;

            }

        }

        readline_show_prompt(rs);

    }

    for (i = 0; i < rs->nb_completions; i++) {

        g_free(rs->completions[i]);

    }

}

static void FUNC_0(ReadLineState *VAR_0)

{

    int VAR_1, VAR_2, VAR_3, VAR_4, VAR_5, VAR_6;

    char *VAR_7;



    VAR_0->nb_completions = 0;



    VAR_7 = g_malloc(VAR_0->cmd_buf_index + 1);

    memcpy(VAR_7, VAR_0->cmd_buf, VAR_0->cmd_buf_index);

    VAR_7[VAR_0->cmd_buf_index] = '\0';

    VAR_0->completion_finder(VAR_0->mon, VAR_7);

    g_free(VAR_7);



    

    if (VAR_0->nb_completions <= 0)

        return;

    if (VAR_0->nb_completions == 1) {

        VAR_1 = strlen(VAR_0->completions[0]);

        for(VAR_2 = VAR_0->completion_index; VAR_2 < VAR_1; VAR_2++) {

            readline_insert_char(VAR_0, VAR_0->completions[0][VAR_2]);

        }

        

        if (VAR_1 > 0 && VAR_0->completions[0][VAR_1 - 1] != '/')

            readline_insert_char(VAR_0, ' ');

    } else {

        monitor_printf(VAR_0->mon, "\n");

        VAR_4 = 0;

        VAR_6 = 0;	

        for(VAR_2 = 0; VAR_2 < VAR_0->nb_completions; VAR_2++) {

            VAR_1 = strlen(VAR_0->completions[VAR_2]);

            if (VAR_2==0) {

                VAR_6 = VAR_1;

            } else {

                if (VAR_1 < VAR_6)

                    VAR_6 = VAR_1;

                for(VAR_3=0; VAR_3<VAR_6; VAR_3++) {

                    if (VAR_0->completions[VAR_2][VAR_3] != VAR_0->completions[0][VAR_3])

                        VAR_6 = VAR_3;

                }

            }

            if (VAR_1 > VAR_4)

                VAR_4 = VAR_1;

        }

        if (VAR_6 > 0) 

            for(VAR_2 = VAR_0->completion_index; VAR_2 < VAR_6; VAR_2++) {

                readline_insert_char(VAR_0, VAR_0->completions[0][VAR_2]);

            }

        VAR_4 += 2;

        if (VAR_4 < 10)

            VAR_4 = 10;

        else if (VAR_4 > 80)

            VAR_4 = 80;

        VAR_5 = 80 / VAR_4;

        VAR_3 = 0;

        for(VAR_2 = 0; VAR_2 < VAR_0->nb_completions; VAR_2++) {

            monitor_printf(VAR_0->mon, "%-*s", VAR_4, VAR_0->completions[VAR_2]);

            if (++VAR_3 == VAR_5 || VAR_2 == (VAR_0->nb_completions - 1)) {

                monitor_printf(VAR_0->mon, "\n");

                VAR_3 = 0;

            }

        }

        readline_show_prompt(VAR_0);

    }

    for (VAR_2 = 0; VAR_2 < VAR_0->nb_completions; VAR_2++) {

        g_free(VAR_0->completions[VAR_2]);

    }

}

static void FUNC_0(ReadLineState *VAR_0)
{ int VAR_1, VAR_2, VAR_3, VAR_4, VAR_5, VAR_6; char *VAR_7; VAR_0->nb_completions = 0; VAR_7 = g_malloc(VAR_0->cmd_buf_index + 1); memcpy(VAR_7, VAR_0->cmd_buf, VAR_0->cmd_buf_index); VAR_7[VAR_0->cmd_buf_index] = '\0'; VAR_0->completion_finder(VAR_0->mon, VAR_7); g_free(VAR_7); if (VAR_0->nb_completions <= 0)
return; if (VAR_0->nb_completions == 1) { VAR_1 = strlen(VAR_0->completions[0]); for(VAR_2 = VAR_0->completion_index; VAR_2 < VAR_1; VAR_2++) { readline_insert_char(VAR_0, VAR_0->completions[0][VAR_2]); } if (VAR_1 > 0 && VAR_0->completions[0][VAR_1 - 1] != '/')
readline_insert_char(VAR_0, ' '); } else { monitor_printf(VAR_0->mon, "\n"); VAR_4 = 0; VAR_6 = 0; for(VAR_2 = 0; VAR_2 < VAR_0->nb_completions; VAR_2++) { VAR_1 = strlen(VAR_0->completions[VAR_2]); if (VAR_2==0) { VAR_6 = VAR_1; } else { if (VAR_1 < VAR_6)
VAR_6 = VAR_1; for(VAR_3=0; VAR_3<VAR_6; VAR_3++) { if (VAR_0->completions[VAR_2][VAR_3] != VAR_0->completions[0][VAR_3])
VAR_6 = VAR_3; } } if (VAR_1 > VAR_4)
VAR_4 = VAR_1; } if (VAR_6 > 0)
for(VAR_2 = VAR_0->completion_index; VAR_2 < VAR_6; VAR_2++) { readline_insert_char(VAR_0, VAR_0->completions[0][VAR_2]); } VAR_4 += 2; if (VAR_4 < 10)
VAR_4 = 10; else if (VAR_4 > 80)
VAR_4 = 80; VAR_5 = 80 / VAR_4; VAR_3 = 0; for(VAR_2 = 0; VAR_2 < VAR_0->nb_completions; VAR_2++) { monitor_printf(VAR_0->mon, "%-*s", VAR_4, VAR_0->completions[VAR_2]); if (++VAR_3 == VAR_5 || VAR_2 == (VAR_0->nb_completions - 1)) { monitor_printf(VAR_0->mon, "\n"); VAR_3 = 0; } } readline_show_prompt(VAR_0); } for (VAR_2 = 0; VAR_2 < VAR_0->nb_completions; VAR_2++) { g_free(VAR_0->completions[VAR_2]); } }