Search code examples
cncurses

Why isn't my use of ncurses alternative charset working properly?


I'm trying to write a program that generates a crossword grid, so I'm using the ncurses library because I just need a simple interface to display the grid, the problem is when I use box() function with ACS_VLINE and ACS_HLINE, it doesn't work; it writes 'q' and 'x' instead of the box lines. It worked at the beginning but suddenly it stopped working; I don't know why. I'm simply initializing ncurses with initscr() and noecho(). Here's the part of the code where I draw the box:


int crossword(char ***grid, WINDOW **win, WINDOW ****c, int x, int y)
{
  int i;
  int j;
  int ch;
  t_word_list *wrdlist;

  clear();
  (void)x;
  (void)y;
  if (!(wrdlist = get_words("data/words.list")))
    return (-1);
  box(*win, ACS_VLINE, ACS_HLINE);
  i = -1;
  while ((*c)[++i])
  {
    j = -1;
    while ((*c)[i][++j])
      mvwaddch((*c)[i][j], 0, 0, (*grid)[i][j]);
  }
  wrefresh(stdscr);
  while ((ch = getch()))
    if (ch == 27)
    {
      nodelay(stdscr, TRUE);
      ch = getch();
      nodelay(stdscr, FALSE);
      if (ch < 0)
        break ;
      }
  return (endwin());
}

Output:

 lqqqqqqqqqqqqqqqqqqqqqk
 x 0 0 0 0 0 0 0 0 0 0 x
 x 0 0 0 0 0 0 0 0 0 0 x
 x 0 0 0 0 0 0 0 0 0 0 x
 x 0 0 0 0 0 0 0 0 0 0 x
 x 0 0 0 0 0 0 0 0 0 0 x
 x 0 0 0 0 0 0 0 0 0 0 x
 x 0 0 0 0 0 0 0 0 0 0 x
 x 0 0 0 0 0 0 0 0 0 0 x
 x 0 0 0 0 0 0 0 0 0 0 x
 x 0 0 0 0 0 0 0 0 0 0 x
 mqqqqqqqqqqqqqqqqqqqqqj

EDIT: I recreated the problem with minimal code:

#include <curses.h>

int     main(void)
{
  WINDOW *win;
  
  initscr();
  win = subwin(stdscr, 10, 10, 1, 1);
  box(win, ACS_VLINE, ACS_HLINE);
  wrefresh(stdscr);
  getch();
  return (0);
}

Output:

 lqqqqqqqqk
 x        x
 x        x
 x        x
 x        x
 x        x
 x        x
 x        x
 x        x
 mqqqqqqqqj

The flags I use for compilation: gcc main.c -lcurses


Solution

  • Converting parts of comments into an answer.

    What did you change between when it worked and when it stopped working? … Can you recreate the steps you would have used while creating the working version in a new MCVE (Minimal, Complete, Verifiable Example — or MRE or whatever name SO now uses) or an SSCCE (Short, Self-Contained, Correct Example). This would allow you to find out what breaks the working code with the bare minimum of code?

    … I just edited my atoi function that I just use in the main for the sizeX and sizeY; I didn't touch anything else and it suddenly stopped working. I tried to undo what I did after it wasn't working and it still doesn't work.

    So you changed something else as well, whether or not you realized it. It's possible that the terminal settings are screwed up — funnier things have been known. Have you tried creating a new terminal window and trying again in the new window?

    Oh yes! It was the terminal! It worked after a 'reset', thank you! I don't know why I didn't think about that earlier.

    Until curses (ncurses) programs have proved themselves reliable, always consider the possibility that a flawed version of the program under test messed up the terminal settings. Use stty -g to generate a string when the terminal is working properly (when first created, before you run your program). You can then use that string to reset the terminal to the same known state (assuming it is stty settings that are the problem). Sometimes, a new terminal window is necessary even so.

    good_stty=$(stty -g)
    
    …testing program under development…
    
    stty "$good_stty"
    

    Sometimes, you may need to type control-J and then stty "$good_stty" (or stty sane) and another control-J because the line ending settings have been modified and not restored correctly.