עדכון קל..
כתבתי תוכנית פשוטה שעושה בדיוק את אותו דבר:
#include <ncurses.h> #include <stdio.h> int main() { char ch; int rc; rc = getch(); if ( rc != ERR) { ch = (char)rc; printf("\nchar read is: %c\n", ch); if ( ch == 'y') printf("Yes\n"); else if ( ch == 'n') printf("No\n"); } else printf("\nError returned, exitin..\n"); return 0; }
עכשיו, קימפלתי:
gcc -g test.c -lcurses
הרצתי (אחרי שכמובן עשיתי ulimit -c unlimited) וקיבלתי core dump, הנה הפלט עם gdb:
[vogue@seawolf Projects]$ a.out Segmentation fault (core dumped) [vogue@seawolf Projects]$ ls a.out core.10247 icqconv test.c test.c~ [vogue@seawolf Projects]$ gdb a.out core.10247 GNU gdb Red Hat Linux (5.1.90CVS-5) Copyright 2002 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-redhat-linux"... Core was generated by `a.out'. Program terminated with signal 11, Segmentation fault. Reading symbols from /usr/lib/libncurses.so.5...done. Loaded symbols for /usr/lib/libncurses.so.5 Reading symbols from /lib/i686/libc.so.6...done. Loaded symbols for /lib/i686/libc.so.6 Reading symbols from /lib/ld-linux.so.2...done. Loaded symbols for /lib/ld-linux.so.2 #0 0x4003cdcd in wgetch () from /usr/lib/libncurses.so.5 (gdb) bt #0 0x4003cdcd in wgetch () from /usr/lib/libncurses.so.5 #1 0x08048564 in main () at test.c:9 #2 0x42017499 in __libc_start_main () from /lib/i686/libc.so.6 (gdb)
כמו שאתה רואה, יש לי debug info אבל אין בעצם מה להראות. אני חושב שהספריות לא מקומפלות עם debug info אז הוא לא מראה את מספר השורה בתוך הספריה שבה קרה הדבר הבלתי חוקי שגרם לאפליקציה לקבל SIGSEGV. בכל מקרה, אני מעריך שב- 99 אחוז זה קשור לעובדה שהפונקציה getch מקבלת תו מחלון curses. היות ואין לנו חלון אני מעריך שיש פה בעיה להשתמש בפונקציה הזאת. בדיקה מהירה בקובץ ה-header של ncurses מגלה לי את הבעיה, מצורפת הפקודה והפלט:
[vogue@seawolf Projects]$ cat /usr/include/ncurses.h | grep getch extern NCURSES_EXPORT(int) getch (void); /* generated */ extern NCURSES_EXPORT(int) mvgetch (int, int); /* generated */ extern NCURSES_EXPORT(int) mvwgetch (WINDOW *, int, int); /* generated */ extern NCURSES_EXPORT(int) ungetch (int); /* implemented */ extern NCURSES_EXPORT(int) wgetch (WINDOW *); /* implemented */ #define getch() wgetch(stdscr) #define mvwgetch(win,y,x) (wmove(win,y,x) == ERR ? ERR : wgetch(win)) #define mvgetch(y,x) mvwgetch(stdscr,y,x) * Pseudo-character tokens outside ASCII range. The curses wgetch() function
כמו שאתה יכול לראות, הפונקציה getch מוגדרת בתור מאקרו. המאקרו הזה הוא בעצם קריאה לפונקציה wgetch שתפקידה (להערכתי) הוא לקלוט תו מחלון כלשהו (שים לב שהיא מקבלת מצביע לחלון curses, זה טיפוס שמוגדר בקובץ H המדובר). עכשיו, מן הסתם יש חלון שנקרא stdscr שהוא איזושהי הקבלה של stdin עבור חלונות ncurses. חיפוש קצר בגוגל מעלה שצריך לאתחל את ספרית ה-curses ורק אז אפשר לעבוד. זאת הבעיה בעצם של הקוד וזה היה החשד שלי. החלון הוירטואלי stdscr לא היה מוקצה ולכן המאקרו העביר ל-wgetch מצביע NULL. אני מצרף
לינק שהוא מבוא ל-ncurses. תראה איזה נחמד אני לפעמים