2012-11-14 15 views
0

我想學習如何使用curses來處理我即將到來的任務的輸入。 我得到交流程序調用測試curses.c其中載有以下代碼...如何正確使用curses /調試一個簡單的c程序

#include <curses.h> 
#include <stdio.h> 
#include <stdlib.h> 

// brief example of using curses. 
// man 3 ncurses for introductory man page, and man 3 function name 
// for more information on that function. 

void setup_curses(); 
void unset_curses(); 

int main() 
{ 

setup_curses(); 

move(5, 10); 
printw("Press any key to start."); 
refresh(); 
int c = getch(); 

nodelay(stdscr, true); 
erase(); 

move(5, 10); 
printw("Press arrow keys, 'q' to quit."); 
refresh(); 

c = getch(); 

while(1) 
{ 
    if (c != ERR) 
    { 
    // in asn3, won't need to do any printing to screen. 
    // instead, will rotate figure on left or right arrow keys, and 
    // initiate thrust when space bar is pressed. 
    erase(); 
    move(5,10); 
    printw("Press arrow keys, 'q' to quit."); 
    move(6, 10); 
    if (c == KEY_DOWN) 
    printw("down key pressed"); 
    else if (c == KEY_LEFT) 
    printw("left key pressed"); 
    else if (c == KEY_RIGHT) 
    printw("right key pressed"); 
    else if (c == KEY_UP) 
    printw("up key pressed"); 
    else if (c == 'q') 
    break; 
    refresh(); 

    } 

    c = getch(); 

} 

// must do this or else Terminal will be unusable 
// (if there are problems, it's not that big a deal though ... just 
// close the Terminal, and open a new one.) 
unset_curses(); 

exit(EXIT_SUCCESS); 
} 

void setup_curses() 
{ 
    // use return values. see man pages. likely just useful for error 
    // checking (NULL or non-NULL, at least for init_scr) 
    initscr(); 
    cbreak(); 
    noecho(); 
    // needed for cursor keys (even though says keypad) 
    keypad(stdscr, true); 
} 

void unset_curses() 
{ 
    keypad(stdscr, false); 
    nodelay(stdscr, false); 
    nocbreak(); 
    echo(); 
    endwin(); 
} 

我運行通過鍵入以下到端子 GCC此程序-std = C99 -Wall -o測​​試儀試驗-curses.c -lcurses ./tester

和一切都很好,花花公子。但是,當我修改代碼來移動畫板中的簡單線條圖以響應箭頭鍵時,我得到了重大錯誤。 這是我修改的程序。

#include <curses.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <assert.h> 

// brief example of using curses. 
// man 3 ncurses for introductory man page, and man 3 function name 
// for more information on that function. 
typedef struct { 
    long x, y, x1, y1; 
    long deltax, deltay, deltax1, deltay1; 
} line_item; 

line_item *line; 
FILE * popen(const char*, const char*); 
int pclose(FILE*); 
void setup_curses(); 
void unset_curses(); 
void draw(line_item * line, FILE * executable); 
void translate(line_item * line, double trans_x, double trans_y); 

int main() 
{ 
    FILE * executable; 
    executable = popen("java -jar Sketchpad.jar", "w"); 
    assert(executable != NULL); 

    setup_curses(); 

    move(5, 10); 
    printw("Press any key to start."); 
    refresh(); 
    int c = getch(); 

    nodelay(stdscr, true); 
    erase(); 

    move(5, 10); 
    printw("Press arrow keys, 'q' to quit."); 
    refresh(); 

    c = getch(); 

    while(1) 
    { 
    if (c != ERR) 
    { 
     // in asn3, won't need to do any printing to screen. 
     // instead, will rotate figure on left or right arrow keys, and 
     // initiate thrust when space bar is pressed. 
     erase(); 
     move(5,10); 
     printw("Press arrow keys, 'q' to quit."); 
     move(6, 10); 
     if (c == KEY_DOWN){ 
     printw("down key pressed"); 
     translate(line, 0, -10); 
     draw(line, executable); 
     } 
     else if (c == KEY_LEFT){ 
     printw("left key pressed"); 
     translate(line, -10, 0); 
     draw(line, executable); 
     } 
     else if (c == KEY_RIGHT){ 
     printw("right key pressed"); 
     translate(line, 10, 0); 
     draw(line, executable); 
     } 
     else if (c == KEY_UP){ 
     printw("up key pressed"); 
     translate(line, 10, 0); 
     draw(line, executable); 
     } 
     else if (c == 'q') 
     break; 
     refresh(); 

    } 

    c = getch(); 
    pclose(executable); 
    } 

    // must do this or else Terminal will be unusable 
    // (if there are problems, it's not that big a deal though ... just 
    // close the Terminal, and open a new one.) 
    unset_curses(); 

    exit(EXIT_SUCCESS); 
} 

void draw(line_item * line, FILE * executable) 
{ 
    fprintf(executable, "eraseSegment %ld %ld %ld %ld", line->x, line->y, line->x1, line->y1); 
    fprintf(executable, "drawSegment %ld %ld %ld %ld", line->deltax, line->deltay, line->deltax1, line->deltay1); 
    fflush(executable); 
} 
void translate(line_item * line, double trans_x, double trans_y) 
{ 
    line->x = line->deltax; 
    line->y = line->deltay; 
    line->x1 = line->deltax1; 
    line->y1 = line->deltay1; 

    line->deltax = line->x + trans_x; 
    line->deltax1 = line->x1 + trans_x; 
    line->deltay = line->y + trans_y; 
    line->deltay1 = line->y1 + trans_y; 
} 

void setup_curses() 
{ 
    // use return values. see man pages. likely just useful for error 
    // checking (NULL or non-NULL, at least for init_scr) 
    initscr(); 
    cbreak(); 
    noecho(); 
    // needed for cursor keys (even though says keypad) 
    keypad(stdscr, true); 
} 

void unset_curses() 
{ 
    keypad(stdscr, false); 
    nodelay(stdscr, false); 
    nocbreak(); 
    echo(); 
    endwin(); 
} 

當前目錄包含這兩個程序和Sketchpad.jar。的錯誤,當我運行第二個看起來像這樣,我得到....

ses.so.5.9 
      7f100a23a000-7f100a23b000 r--p 0001f000 08:01 2882225     /lib/x86_64-linux-gnu/libncurses.so.5.9 
      7f100a23b000-7f100a23c000 rw-p 00020000 08:01 2882225     /lib/x86_64-linux-gnu/libncurses.so.5.9 
      7f100a23c000-7f100a25e000 r-xp 00000000 08:01 2877975     /lib/x86_64-linux-gnu/ld-2.15.so 
      7f100a428000-7f100a42c000 rw-p 00000000 00:00 0 
      7f100a45b000-7f100a45e000 rw-p 00000000 00:00 0 
      7f100a45e000-7f100a45f000 r--p 00022000 08:01 2877975     /lib/x86_64-linux-gnu/ld-2.15.so 
      7f100a45f000-7f100a461000 rw-p 00023000 08:01 2877975     /lib/x86_64-linux-gnu/ld-2.15.so 
      7ffffa0be000-7ffffa0df000 rw-p 00000000 00:00 0       [stack] 
     7ffffa19e000-7ffffa19f000 r-xp 00000000 00:00 0       [vdso] 
    ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0     [vsyscall] 
     Aborted (core dumped) 

我不明白這是什麼告訴我,什麼文件之間的差異造成了這個爛攤子。請幫忙。

我應該告訴你一些關於發送到畫板 drawSegment x和y X1 Y1以下命令,從(X,Y)繪製一條線 - >(X1,Y1)在畫板窗口 eraseSegment x和y X1 Y1刪除該行。

+0

這聽起來像你想運行GDB的內部測試程序。這樣,當你得到核心轉儲時,你可以1)得到一個回溯,2)可能在崩潰時確定相關變量的值。 – paulsm4

+0

我用gdb運行它,它給了我下面的附加行程序接收到的信號SIGABRT,中止。 0x00007ffff7609425 in __GI_raise(sig = <優化出>) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64 ../nptl/sysdeps/unix/sysv/linux/raise。 c:沒有這樣的文件或目錄。(gdb) – wenincode

+0

當GDB發生崩潰時,可以使用'bt'(backtrace)命令查看調用鏈。使用'up'命令轉到你的函數。然後,您可以檢查變量以查看可能出錯的內容。請閱讀[GDB文檔](http://www.gnu.org/software/gdb/documentation/)。 –

回答

0

對於初學者來說,你的行指針永遠不會指向任何真實的東西。這可能會導致核心轉儲。

最小編輯修復你可以這樣做: -

line_item _line; 
line_item *line; 

然後在主,在循環之前做: -

line = &_line; 
+0

啊是的,我的壞 - 但是當我得到它指向line_item的一個元素我仍然得到相同的錯誤。 – wenincode

相關問題