]> pd.if.org Git - pd_readline/commitdiff
More work
authorAndy Elvey <andy@obsidian.(none)>
Sat, 24 Nov 2012 01:50:18 +0000 (14:50 +1300)
committerAndy Elvey <andy@obsidian.(none)>
Sat, 24 Nov 2012 01:50:18 +0000 (14:50 +1300)
funcs.c
history.c
keyhandler.c
pd_readline [new file with mode: 0755]
pd_readline.c
pd_readline.h
range [new file with mode: 0755]

diff --git a/funcs.c b/funcs.c
index 36241e5cb97ddfd6adac38d3e1caca31de25c72b..3623220228d841bc52735d64f52f04e18f665323 100644 (file)
--- a/funcs.c
+++ b/funcs.c
 /* array member of the buffer.                            */ 
 /* Also test for the top and bottom of the history file.  */     
 
+/*  Error function.  */ 
+void error(void)
+{ 
+   printf("Error \n");         
+}      
+
+
 
 /*  Display a buffer  */ 
 void show(buf b)
@@ -37,7 +44,7 @@ buf set(buf b, int i)
     } 
   else
     { 
-          memset(b.array[0], 0, sizeof(b.array) ); 
+          memset(&b.array[0], 0, sizeof(b.array) ); 
           b.array[0] = i;
           b.index += 1 ;               
     }          
@@ -48,21 +55,53 @@ buf set(buf b, int i)
 } 
 
 
+/*  Return a line from hist.  */ 
+buf get(hist h)
+{ 
+   buf b; 
+   memcpy(&b.array[0], h.array[h.curindex], 80); 
+   return b;           
+}       
+
+
 
 /* Move up in history list. */ 
-buf up(buf b
+hist up(hist h
 { 
+       
+  buf b;         
+       
+  if ( (h.curindex > 0) )  
+    { 
+               h.curindex -= 1; 
+               memset(&b.array[0], 0, sizeof(b.array) ); 
+               memcpy(&b.array[0], h.array[h.curindex], 80);   
+               show(b);                
+        return h;              
+    }    
+     
+  else error();  
   
-
-
 } 
 
 
 /* Move down in history list. */ 
-buf down(buf b
+hist down(hist h
 { 
-
-
+       
+  buf b;         
+       
+  if ( (h.curindex < 19) )  
+    { 
+               h.curindex += 1; 
+               memset(&b.array[0], 0, sizeof(b.array) ); 
+               memcpy(&b.array[0], h.array[h.curindex], 80); 
+               show(b);    
+        return h;                      
+    }  
+    
+  else error();     
+  
 } 
 
 
@@ -133,13 +172,13 @@ int type(int i)
 
 /*  Function for special key combinations  */ 
 /*  (Ctrl, Alt, function keys.             */ 
-void spec(void)
+void spec(hist h)
 { 
        
   int j = getch();  
-   
-       if ( ( j == 65 )        )  printf("Up ");  
-  else if ( ( j == 66 )        )  printf("Down "); 
+    
+       if ( ( j == 65 )        )  up(h); 
+  else if ( ( j == 66 )        )  down(h); 
   else if ( ( j == 67 )        )  printf("Right "); 
   else if ( ( j == 68 )        )  printf("Left "); 
                
index dd70ed5797bfeb7196526333eb9e18b433708633..498cbe8e86af1a9267a7b4c9b89374fe1fa4df50 100644 (file)
--- a/history.c
+++ b/history.c
@@ -35,18 +35,13 @@ char *chop(char *s)
 }
 
 
-/* An array to store the command-history file in. */ 
-/* Only 20 lines are read. */ 
-char hist[20][80];  
 
-
-
-/* Read the file into the array of strings.                */ 
-buf readhistory(char *fname) 
+/* Read the file into the array of strings.   */ 
+hist readhistory(char *fname) 
 { 
        
    /*  Create a history buffer.  */ 
-   buf h;      
+   hist h;     
        
    int retval = fexists(fname); 
         
@@ -63,22 +58,20 @@ buf readhistory(char *fname)
          for(i=0; i<20; i++)  
          { 
                fgets(line, 80, fptr);                   
-               chop(line) ;   
-               memcpy(hist[i], line, 80) ;   
-               puts(hist[i]); 
+               chop(line) ;  
+               
+               /* TO DO: fix the "too few arguments" bug here.... */  
+               memcpy(&h.array[i], line, 80) ;   
+               puts(h.array[i]); 
          }
          
    }  /* retval == 0  */          
        
        else puts("Error! File does not exist. \n");  
-       
-       /* Read the most recent command into histbuf */ 
-       /* and set the index to 19.                  */
-       /*  
-           h.index = 19; 
-        h.array = hist[h.index] ;  
-    */
-       
+               
+       /*  Set the curindex to 19.  */     
+           h.curindex = 19;       
+               
        return h;
        
 }      
index 0ba3c6a368bcd9503e01ecf77369fe98408351c7..f4ca5baa146f729885ca2d639f37cb52bde323d6 100644 (file)
@@ -65,7 +65,7 @@ char getche(void) {
 /*  Alt keys are just esc then key (e.g. Alt-g is esc g ).  */   
 /*  Ctrl (then letter) keys are just Dec 1 to Dec 26        */ 
 
-void keyhandler(buf b) 
+void keyhandler(buf b, hist h
 { 
     
   int a = getch();    
@@ -77,7 +77,7 @@ void keyhandler(buf b)
          
        case (1):   break;     /*  Ctrl a  */   
        case (2):   break;     /*  Ctrl b  */   
-       case (3):   getch();  spec(); break;   /*  Ctrl c  */     
+       case (3):   getch();  spec(h); break;   /*  Ctrl c  */     
        case (4):   set(b, a);   break;    /*  Printable chars.  */    
        case (5):   delch(b);  break;  
        case (6):   break;  
diff --git a/pd_readline b/pd_readline
new file mode 100755 (executable)
index 0000000..a54280c
Binary files /dev/null and b/pd_readline differ
index 8649fc1c036553e33069d19e13ecfce3a86bca96..044d6efb04e274667dba0eda24c6f1d6f61efe34 100644 (file)
@@ -35,13 +35,13 @@ int main(void)
    
   
   /* Read in the command history file. */ 
-  buf histbuf = readhistory("test.txt");  
+  hist myhist = readhistory("test.txt");  
     
   
   while(1) 
   {  
        
-    keyhandler(mybuf);  
+    keyhandler(mybuf, myhist);  
          
   }         
                                       
index ea5efbe4354f724e496b9535810a2c31e02308cb..8be5d8f27f5789a75b05207e455355ed5a25f68b 100644 (file)
@@ -14,12 +14,31 @@ typedef struct {
 } buf;  
 
 
+/*  History. */ 
+typedef struct { 
+       int curindex; 
+       char array[20][80]; 
+} hist;    
+
+
+
+
+/*  Termios funcs.  */ 
+void initTermios(int echo) ;
+void resetTermios(void) ;
+char getch_(int echo);
+char getch(void);
+char getche(void);
+
 
 /*  Buffer funcs. */ 
-buf set(buf b, int i);
+void error(void); 
 void show(buf b);
-buf up(buf b); 
-buf down(buf b); 
+buf set(buf b, int i);
+buf get(hist h); 
+
+hist up(hist h); 
+hist down(hist h); 
 buf left(buf b); 
 buf right(buf b); 
 buf delch(buf b); 
@@ -30,12 +49,12 @@ int type(int i);
 
 
 /*  Special key handling.  */ 
-void spec(void); 
+void spec(hist h); 
 
 
 /*  Other funcs.  */  
-buf readhistory(char *fname); 
-void keyhandler(buf b);  
+hist readhistory(char *fname); 
+void keyhandler(buf b, hist h);  
 
 
 
diff --git a/range b/range
new file mode 100755 (executable)
index 0000000..709d9e0
Binary files /dev/null and b/range differ