getch() on Linux

ckm2k1

New member
getch() on Linux

Hello people, i've been searching and looking in google and all the possible man pages there is about the implemintations of getch()and getche() on Linux and i simply cant figure it out. All i want is for the Linux terminal to stop buffering the stdin. ex. even this simple code gives me errors: #include <curses.h> #include <stdio.h> int main() { char ch; if ( getch(ch) == 'y' ) printf("%c\n", ch ); else printf("%c\n", ch ); return 0; } error: undefined reference to 'stdscr' undefined reference to 'wgetch'
 

ckm2k1

New member
Segmenation fault...

very weird it compiles perfectly but when it runs it gives me "segmenation fault" any ideas?​
 

voguemaster

New member
נסה את GDB!

בשורת הפקודה תרשום:
ulimit -c unlimited​
זה יאפשר למערכת לעשות CORE DUMP במקרה ויש segfault (הסיגנל הוא SIGSEGV רק לידע כללי..). כשיש לך קובץ core שהמערכת כתבה, אפשר בעזרת GDB להגיע ישירות למיקום שבו קרתה התקלה (במידה וזה באג דטרמיניסטי כמו כאן..) בצורה הבאה:
gdb <progname> core​
תחליף את progname בשם של הקובץ שאתה מריץ (בין אם זה a.out או כל דבר אחר..) כש-GDB מורץ, הוא נותן כמה הודעות. מספיק בד"כ לרשום bt כדי לקבל backtrace של באיזה פונקציה אתה ומאיפה הגעת אליה, לרוב תוך פירוט הפרמטרים כך שתוכל לגלות. בשורה הרלוונטית תנסה לבדוק את הערכים של המשתנים ולראות אם הם תקינים.
 

voguemaster

New member
איך זה מתקמפל בכלל ? ../images/Emo12.gif

הפונקציה getch לא מקבלת משתנה char או אפילו לא int! התחביר הוא כזה:
char c; // using curses c = getch(); // using stdio c = getchar(); // getchar is defined as getc( stdin)​
 

ckm2k1

New member
Oops...

Sorrry you are right i made a little error in the code i posted it was supposed to be ch=getch(); . well tiredness is harmful :) . alright i will try gdb a bit and see what goes where.. thanks anyway​
 

ckm2k1

New member
Errors...

here is what gdb dumped at me (copyed & pasted) [ckm2k1@localhost ckm2k1]$ gdb -core core.1500 GNU gdb Red Hat Linux (5.2.1-4) 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 `./progy'. Program terminated with signal 11, Segmentation fault. #0 0x400318a9 in ?? () (gdb) bt #0 0x400318a9 in ?? () #1 0x080484b6 in ?? () #2 0x420158d4 in ?? () and i have no idea what that means. sorry for the stupid questions but if you can help please do..​
 

voguemaster

New member
אינעל תפוז, לא יכלתי להגיב..

אז ככה, הבעיה היא שאתה לא רואה שום נתון שימושי. זה בגלל שלא קימפלת את התוכנית שלך עם debug information. לכן, אני מציע לך להוסיף את הדגל g- לשורת הפקודה שבה אתה מקמפל (זה דגל של gcc שאומר לו להוסיף את המידע הרלוונטי כך שיהיה אפשר לראות שמות פונקציות ומשתנים ב-gdb). קמפל שוב עם debug info ותחזור על התרגיל...
 

ckm2k1

New member
Same thing...

Exactly the same thing. now the compilation was with this line: gcc -lcurses -g -o progy progy.c exactly the same results.​
 

choo

Active member
vingardium leviousa (the swish and

<p dir=ltr> to solve your original single-char reading problem: <br> http://www.erlenstar.demon.co.uk/unix/faq_4.html#SEC49 <br> look there. <br><br> regarding using gdb: <br> http://www.actcom.co.il/~choo/lupg/tutorials/debugging/debugging-with-gdb.html <br><br> good luck <br> </p>
 

voguemaster

New member
אני אבדוק את זה כשאני אגיע הביתה

אחד החסרונות באוניברסיטה הוא שאין לינוקס לסטודנטים
 

voguemaster

New member
עדכון קל..

כתבתי תוכנית פשוטה שעושה בדיוק את אותו דבר:
#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. תראה איזה נחמד אני לפעמים
 

ckm2k1

New member
Thanks ALOT

The sleepless hours are finished i can finaly relax and watch my program recieve unbuffered data. So thats all for now and thanks again for all the help aspecially to voguemaster who helped to no end. I am off to his great link...​
 
למעלה