4 /* This code is aimed at helping those who would like to */
\r
5 /* create menus without using the curses library. */
\r
6 /* It creates a count variable (which could be used to */
\r
7 /* keep track of which item is highlighted). Using the */
\r
8 /* up and down arrow keys increments and decrements the */
\r
9 /* count variable. Finally, when you press Enter, the */
\r
10 /* value of the count variable is printed and the program */
\r
12 /* This code is released to the public domain. */
\r
13 /* "Share and enjoy...." ;) */
\r
16 #include <string.h>
\r
18 #include <termios.h> /* For getch() */
\r
20 /* This implementation of getch() is from here - */
\r
21 /* http://wesley.vidiqatch.org/ */
\r
22 /* Thanks, Wesley! */
\r
23 static struct termios old, new;
\r
25 /* Initialize new terminal i/o settings */
\r
26 void initTermios(int echo) {
\r
27 tcgetattr(0, &old); /* grab old terminal i/o settings */
\r
28 new = old; /* make new settings same as old settings */
\r
29 new.c_lflag &= ~ICANON; /* disable buffered i/o */
\r
30 new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
\r
31 tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */
\r
35 /* Restore old terminal i/o settings */
\r
36 void resetTermios(void) {
\r
37 tcsetattr(0, TCSANOW, &old);
\r
41 /* Read 1 character - echo defines echo mode */
\r
42 char getch_(int echo) {
\r
51 /* Read 1 character without echo */
\r
57 /* Read 1 character with echo */
\r
64 /* Helper function, to let us see if a .history file */
\r
65 /* exists in the current directory. */
\r
66 int fexists(char *fname)
\r
71 fptr = fopen(fname, "r") ;
\r
73 if ( !fptr ) return -1 ; /* File does not exist in dir. */
\r
76 return 0; /* File DOES exist in dir. */
\r
85 printf("Public Domain Menu Program \n");
\r
86 printf("\nUse the up and down arrow keys then press Enter\n");
\r
93 /* Buffer "pointer" */
\r
97 /* Test for existence of history file. */
\r
99 exists = fexists(".history");
\r
100 printf("Result: %d \n", exists);
\r
105 int key = getch();
\r
107 /* Printable chars. */
\r
108 if ( (key >= 32) && (key <= 126) )
\r
110 buffer[bufpnt] = key;
\r
114 /* Up arrow is 27, 91, 65. ( ESC [ A ) */
\r
115 /* Down arrow is 27, 91, 66. ( ESC [ B ) */
\r
116 /* Right arrow is 27, 91, 67. ( ESC [ C ) */
\r
117 /* Left arrow is 27, 91, 68. ( ESC [ D ) */
\r
118 else if(key == 27)
\r
123 { puts("You pressed up arrow! \n");
\r
125 printf("Menunum is now %d \n", menunum);
\r
127 else if(key == 66)
\r
128 { puts("You pressed down arrow! \n");
\r
130 printf("Menunum is now %d \n", menunum);
\r
132 else if(key == 67)
\r
133 { puts("You pressed right arrow! \n");
\r
135 else if(key == 68)
\r
136 { puts("You pressed left arrow! \n");
\r
140 /* A few other keys (for testing purposes ) */
\r
143 else if(key == 97)
\r
144 { puts("You pressed the \"a\" key! \n");
\r
146 else if(key == 98)
\r
147 { puts("You pressed the \"b\" key! \n");
\r
149 else if(key == 99)
\r
150 { puts("You pressed the \"c\" key! \n");
\r
152 else if(key == 100)
\r
153 { puts("You pressed the \"d\" key! \n");
\r
158 /* The Enter key exits. Enter is 10 decimal */
\r
159 else if(key == 10)
\r
160 { printf("You pressed ENTER! You chose item %d \n", menunum);
\r
161 printf("Buffer is %s \n", buffer) ;
\r