]> pd.if.org Git - pd_readline/blobdiff - pdmenu5.c
More good progress.
[pd_readline] / pdmenu5.c
diff --git a/pdmenu5.c b/pdmenu5.c
new file mode 100644 (file)
index 0000000..9c8721f
--- /dev/null
+++ b/pdmenu5.c
@@ -0,0 +1,139 @@
+\r
+\r
+/*  pd_readline.c                                           */  \r
+/*  Status (as at 26th Aug 2012) : useful progress.         */ \r
+/*  Keystroke sequences (along with the special flags       */ \r
+/*  like Esc, Ctrl, Alt etc are now stored in a buffer      */ \r
+/*  ( an array of structs ).                                */    \r
+/*  It will still be some time before this is a REAL        */ \r
+/*  readline, but we are "on the way"......                 */     \r
+/*  This code is released to the public domain.             */ \r
+/*  "Share and enjoy...."  ;)                               */  \r
+\r
+\r
+#include <string.h>   \r
+#include <stdio.h> \r
+\r
+\r
+/*  Helper function, to let us see if a .history file */ \r
+/*  exists in the current directory.  */ \r
+int fexists(char *fname)\r
+{  \r
+\r
+   FILE *fptr ;        \r
+       \r
+   fptr = fopen(fname, "r") ;  \r
+   \r
+   if ( !fptr )  return -1 ;  /* File does not exist in dir. */        \r
+        \r
+   fclose(fptr);  \r
+   return 0;    /* File DOES exist in dir.  */         \r
+\r
+} \r
+\r
+\r
+ /* Struct to store key sequences */ \r
+typedef struct { \r
+          int fnkey; \r
+          int ctrl; \r
+          int alt ; \r
+       int shf ; \r
+       int esc ; \r
+       int lbr ;  /* For left-bracket ([) of escape sequences */ \r
+       int key; \r
+   } keyseq ;         \r
+\r
+\r
+\r
+\r
+\r
+int main(void)\r
+{\r
+       \r
+  printf("Public Domain Readline \n");              \r
+  printf("NOTE! - at the moment, we are using \n");  \r
+  printf("NON-echoing reads, storing the keystrokes \n");  \r
+  printf("in a buffer \n");   \r
+    \r
+  \r
+  /* Buffer - an array of keyseq structs.                     */  \r
+  /* Note - now that we store the keystrokes in here,         */ \r
+  /* we can look at the various flags and decide whether to   */ \r
+  /* "echo" the key (as normal) or suppress it (as with an    */ \r
+  /* arrow key).                                              */    \r
+  keyseq buffer[80] ;   \r
+  \r
+  /* Buffer "pointer"  */ \r
+  int bufpnt = 0; \r
+  \r
+  \r
+  /* Test for existence of history file. */  \r
+  int exists;  \r
+  exists = fexists(".history"); \r
+  printf("Result: %d \n", exists);  \r
+  \r
+  while(1) \r
+    {  \r
+                   \r
+      int key = getchar(); \r
+       \r
+      /* Printable chars. */  \r
+      if ( (key >= 32)  && (key <= 126) ) \r
+      { \r
+               putchar(key);                   \r
+        buffer[bufpnt].key = key;  \r
+        bufpnt += 1; \r
+      }  \r
+                                                                       \r
+      /* Up arrow is 27, 91, 65.    ( ESC [ A )   */   \r
+      /* Down arrow is 27, 91, 66.  ( ESC [ B )   */ \r
+      /* Right arrow is 27, 91, 67. ( ESC [ C )   */ \r
+      /* Left arrow is 27, 91, 68.  ( ESC [ D )   */    \r
+      /* Function keys.     */ \r
+      /* F2 is 27, 79, 81.  */  \r
+      /* F3 is 27, 79, 82.  */  \r
+      /* F4 is 27, 79, 83.  */  \r
+           \r
+                \r
+      else if(key == 27)        \r
+          {                      \r
+                         buffer[bufpnt].esc = 1;                        \r
+                         key = getchar(); \r
+              if(key == 91)               \r
+              buffer[bufpnt].lbr = 1;                \r
+              key = getchar(); \r
+              if( (key >= 65) && (key <= 68) )  \r
+               {                                \r
+                                buffer[bufpnt].key = key;                  \r
+               }                                 \r
+            bufpnt += 1;           \r
+          } \r
+              \r
+                                          \r
+                                                 \r
+    /* The Enter key exits. Enter is 10 decimal */       \r
+        else if(key == 10)  \r
+               { \r
+                        int j ; \r
+                  /* Print the array of structs. */ \r
+                    for (j=0; j<10; j++)       \r
+                    { \r
+                           printf("Fnkey: %d ", buffer[j].fnkey  ) ; \r
+                           printf("Ctrl:  %d ", buffer[j].ctrl   ) ;   \r
+                           printf("Alt:   %d ", buffer[j].alt    ) ; \r
+                           printf("Shf:   %d ", buffer[j].shf    ) ; \r
+                           printf("Esc:   %d ", buffer[j].esc    ) ; \r
+                           printf("Lbr:   %d ", buffer[j].lbr    ) ;  \r
+                   printf("Key:   %d \n", buffer[j].key  ) ; \r
+                }                      \r
+                                         \r
+             break;           \r
+         }  /* Key = Enter */   \r
+    }                \r
+                                      \r
+       return 0;\r
+}  \r
+\r
+\r
+\r
+\r