bug-ncurses
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Unknown malfunction of ncurses


From: Anthony Marshall
Subject: Unknown malfunction of ncurses
Date: Tue, 11 Sep 2012 12:01:39 +0200

Greetings,

Whilst trying
Example 9. A Simple Color example in the NCURSES-Programming-HOWTO.html, I extended the program slightly, thus:

/*
 * colr_ex.c    color example
 *
 * $Revision: 1.46 $
 */

#include <ncurses.h>
#include <string.h>

static char *msg[] = {
        "Viola !!! In color ...","A slightly longer string here ...",
        "Short string ..."
};

int prev_x=40, prev_l=0;

void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, int cp);

int main(int argc, char *argv[])
{
        char fin[]="Example Completed";
        register int x=0;

        initscr();                      /* Start curses mode            */
        noecho();
        if(has_colors() == FALSE)
        {
                endwin();
                printf("Your terminal does not support color\n");
                exit(1);
        }
        start_color();                  /* Start color                  */
        init_pair(1, COLOR_RED, COLOR_BLACK);
        init_pair(2, COLOR_BLUE, COLOR_GREEN);
        init_pair(3, COLOR_BLACK, COLOR_WHITE);

/*      for(;x<4;x++)
        {                                                               */
                attron(COLOR_PAIR(x+1));
                print_in_middle(stdscr, LINES / 2, 0, 0, msg[x], x+1);
                attroff(COLOR_PAIR(x+1));
                mvwprintw(stdscr, LINES-2, 0, "%d iteration completed.", x+1);
                wgetch(stdscr);
/*      }                                                               */
        mvwprintw(stdscr, LINES-2, (COLS-strlen(fin))-1, "%s", fin);
        wrefresh(stdscr);
        getch();
        endwin();
}

void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, int cp)
{
        register int c;
        int length, x, y;
        float temp;

        if(win == NULL) win = stdscr;
        getyx(win, y, x);
        if(startx != 0) x = startx;
        if(starty != 0) y = starty;
        if(width == 0) width = COLS;

        length = strlen(string);
        temp = (width - length)/ 2;
        x = startx + (int)temp;
        if(length<prev_l)
        {
                move(y,prev_x);
                standend();
                for(c=0;c<prev_l;c++) addch(' ');
                attron(COLOR_PAIR(cp));
        }
        mvwprintw(win, y, x, "%s", string);
        refresh();
        prev_x=x;prev_l=length;
}

As you may notice in the main, I have commented out the for() loop because, uncommented, it seems to generate a segmentation error each time it is compiled in and run, and I cannot seem to work out why.

Could you please advise me why this is the case and is there a solution?

reply via email to

[Prev in Thread] Current Thread [Next in Thread]