/* Also test for the top and bottom of the history file. */
+/* Display a buffer */
+void show(buf b)
+{
+ printf("%s", b.array);
+}
+
+
+
+/* Enter a char into a buffer and display the buffer array. */
+buf set(buf b, int i)
+{
+ if ( (b.index < 80) )
+ {
+ b.array[b.index] = i;
+ b.index += 1 ;
+ }
+ else
+ {
+ memset(b.array[0], 0, sizeof(b.array) );
+ b.array[0] = i;
+ b.index += 1 ;
+ }
+
+ show(b);
+ return b;
+
+}
+
+
+
/* Move up in history list. */
-buf up(int i)
+buf up(buf b)
{
/* Move down in history list. */
-buf down(int i)
+buf down(buf b)
{
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
+#include <malloc.h>
+#include "pd_readline.h"
/* Helper function, to let us see if a file */
/* Read the file into the array of strings. */
-/* TO DO - look at reading file into a series of structs. */
-
-void readhistory(char *fname)
+buf readhistory(char *fname)
{
+
+ /* Create a history buffer. */
+ buf h;
+
int retval = fexists(fname);
int i;
/* append new commands to it. */
FILE *fptr;
fptr = fopen(fname, "rw");
-
+
+ char line[80] ;
+
for(i=0; i<20; i++)
- {
- chop(fgets(hist[i], 80, fptr) );
+ {
+ fgets(line, 80, fptr);
+ chop(line) ;
+ memcpy(hist[i], line, 80) ;
+ puts(hist[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] ;
+ */
+
+ return h;
+
}
-
-/* TO DO - a function that reads in the 2d history array, and */
-/* returns a struct with one line from the array, and the index */
-/* of the line. */
+
/* 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 */
-/* TO DO: Use the helper function range */
-/* ( range(rstart, rend, val). ) */
-/* to handle entire ranges at once. */
-
-
void keyhandler(buf b)
{
- int i = getch();
+ int a = getch();
- int t = type(i);
+ int t = type(a);
switch(t)
{
case (1): break; /* Ctrl a */
case (2): break; /* Ctrl b */
case (3): getch(); spec(); break; /* Ctrl c */
- case (4): printf("%c", i); break; /* Printable chars. */
+ case (4): set(b, a); break; /* Printable chars. */
case (5): delch(b); break;
case (6): break;
default: break;
}
-
+
}
int main(void)
{
- /* Create a buffer for the text. */
+ /* Create a buffer for entered text. */
buf mybuf;
-
+ mybuf.index = 0;
+
/* Read in the command history file. */
- readhistory("test.txt");
-
+ buf histbuf = readhistory("test.txt");
+
while(1)
{
- keyhandler(mybuf);
-
+ keyhandler(mybuf);
+
}
return 0;
-/* Cursor movement funcs. */
-buf up(int i);
-buf down(int i);
+/* Buffer funcs. */
+buf set(buf b, int i);
+void show(buf b);
+buf up(buf b);
+buf down(buf b);
buf left(buf b);
buf right(buf b);
buf delch(buf b);
/* Other funcs. */
-void readhistory(char *fname);
+buf readhistory(char *fname);
void keyhandler(buf b);
printf("%d %d %d %d %d %d %d \n", a, b, c, d, e, f, g);
+int i = 65;
+char s[2];
+
+snprintf(s, 2, "%c", i);
+
+printf("%s \n" , s );
+
return 0;