From 36403b46c9eec27a672b0cfbfad9a79c7d153dca Mon Sep 17 00:00:00 2001 From: andy Date: Mon, 22 Oct 2012 12:56:44 +1300 Subject: [PATCH] Starting cleanup of code. --- Makefile | 41 ++++++++++ README | 11 +++ funcs.c | 81 ++++++++++++++++++ history.c | 65 +++++++++++++++ keyhandler.c | 77 ++++++++++++++++++ pd_readline.c | 221 ++------------------------------------------------ 6 files changed, 284 insertions(+), 212 deletions(-) create mode 100644 Makefile create mode 100644 funcs.c create mode 100644 history.c create mode 100644 keyhandler.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..be2e21a --- /dev/null +++ b/Makefile @@ -0,0 +1,41 @@ + + +# Makefile for pd_readline + + +CC = gcc + +CFLAGS = -O2 -Wall -g $(DEFINES) + + +# INCPATH = -I. +INCPATH = . + +LDFLAGS = $(SYSLDFLAGS) $(MYLDFLAGS) + +LIBS = -l$(SYSLIBS) $(MYLIBS) + +RM = rm -f + +#HEADERS= foo.h bar.h baz.h + +OBJECTS = pd_readline.o keyhandler.o funcs.o history.o + +%.o: %.c + $(CC) $(CFLAGS) $< -o $@ + +pd_readline: $(OBJECTS) + $(CC) $(LDFLAGS) $(OBJECTS) -o $@ $(LIBS) + +#pd_readline.o: $(HEADERS) +#keyhandler.o: $(HEADERS) +#funcs.o: $(HEADERS) +#history.o: $(HEADERS) + + +.PHONY: clean +clean: + rm *.o pd_readline + + + diff --git a/README b/README index c826c97..6835e88 100644 --- a/README +++ b/README @@ -4,6 +4,17 @@ This repo is for the storage of a public-domain readline-and-command-history implementation. +FILES: + +pd_readline.c - This is a file to test the +implementation. Contains the main() function. + +keyhandler.c - Reads the keystrokes and calls +functions to handle them. + +funcs.c - Functions to handle the keystrokes. + + Update - 6th Sep 2012 - Almost there! diff --git a/funcs.c b/funcs.c new file mode 100644 index 0000000..7444e66 --- /dev/null +++ b/funcs.c @@ -0,0 +1,81 @@ + + +/* funcs.c */ +/* Functions to handle cursor movement for */ +/* pd_readline. */ +/* This code is released to the public domain. */ +/* "Share and enjoy...." ;) */ +/* See the UNLICENSE file for details. */ + + +#include +#include +#include + + + +/* Move up in history list. */ +int up(int i) +{ + + + +} + + +/* Move down in history list. */ +int down(int i) +{ + + +} + + +/* Move cursor to left. */ +buf left(buf b) +{ + + +} + + +/* Move cursor to right. */ +buf right(buf b) +{ + + +} + + +/* Delete a char. */ +buf delch(buf b) +{ + + +} + + +/* Insert a char. */ +buf insch(buf b) +{ + + +} + + + +/* Function to handle escape sequences. */ +int esc(int i) +{ + + + + +} + + + + + + + diff --git a/history.c b/history.c new file mode 100644 index 0000000..f8a0d25 --- /dev/null +++ b/history.c @@ -0,0 +1,65 @@ + + +/* history.c */ +/* Command history. */ +/* This code is released to the public domain. */ +/* "Share and enjoy...." ;) */ +/* See the UNLICENSE file for details. */ + + + +/* Helper function, to let us see if a file */ +/* exists in the current directory. */ +int fexists(char *fname) +{ + FILE *fptr; + fptr = fopen(fname, "r") ; + if ( !fptr ) return -1 ; /* File does not exist in dir. */ + fclose(fptr); + return 0; /* File DOES exist in dir. */ +} + + +/* Helper function to chop newlines off the lines read in. */ +/* Without this being done, an extra newline is inserted */ +/* (which is usually not what is wanted). */ +char *chop(char *s) +{ + s[strcspn(s,"\n")] = '\0'; + return 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. */ +void readhistory(char *fname) +{ + int retval = fexists(fname); + + int i; + if (retval == 0) { + /* File exists, so open it. */ + /* We open it in read-write mode so we can */ + /* append new commands to it. */ + FILE *fptr; + fptr = fopen(fname, "rw"); + + for(i=0; i<20; i++) + { + chop(fgets(hist[i], 80, fptr) ); + } + + } /* retval == 0 */ + + else puts("Error! File does not exist. \n"); + +} + + + + diff --git a/keyhandler.c b/keyhandler.c new file mode 100644 index 0000000..0130e9b --- /dev/null +++ b/keyhandler.c @@ -0,0 +1,77 @@ + + + +/* keyhandler.c */ +/* Handle keystrokes for pd_readline. */ +/* This code is released to the public domain. */ +/* "Share and enjoy...." ;) */ +/* See the UNLICENSE file for details. */ + + +#include +#include +#include +#include + + +/* This implementation of getch() is from here - */ +/* http://wesley.vidiqatch.org/ */ +/* Thanks, Wesley! */ +static struct termios old, new; + +/* Initialize new terminal i/o settings */ +void initTermios(int echo) { + tcgetattr(0, &old); /* grab old terminal i/o settings */ + new = old; /* make new settings same as old settings */ + new.c_lflag &= ~ICANON; /* disable buffered i/o */ + new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */ + tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */ +} + + +/* Restore old terminal i/o settings */ +void resetTermios(void) { + tcsetattr(0, TCSANOW, &old); +} + + +/* Read 1 character - echo defines echo mode */ +char getch_(int echo) { + char ch; + initTermios(echo); + ch = getchar(); + resetTermios(); + return ch; +} + + +/* Read 1 character without echo */ +char getch(void) { + return getch_(0); +} + + +/* Read 1 character with echo */ +char getche(void) { + return getch_(1); +} + + + +int keyhandler(void) +{ + int i = getch(); + + switch(i); + { + case (27): escape() ; + case (33): dosomething(); + case (42): something(); + default: stuff(): + } + +} + + + + diff --git a/pd_readline.c b/pd_readline.c index b4b886e..5f000bd 100644 --- a/pd_readline.c +++ b/pd_readline.c @@ -23,228 +23,25 @@ /* Ctrl-C and Ctrl-V. */ -#include -#include -#include - -/* This implementation of getch() is from here - */ -/* http://wesley.vidiqatch.org/ */ -/* Thanks, Wesley! */ -static struct termios old, new; - -/* Initialize new terminal i/o settings */ -void initTermios(int echo) { - tcgetattr(0, &old); /* grab old terminal i/o settings */ - new = old; /* make new settings same as old settings */ - new.c_lflag &= ~ICANON; /* disable buffered i/o */ - new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */ - tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */ -} - - -/* Restore old terminal i/o settings */ -void resetTermios(void) { - tcsetattr(0, TCSANOW, &old); -} - - -/* Read 1 character - echo defines echo mode */ -char getch_(int echo) { - char ch; - initTermios(echo); - ch = getchar(); - resetTermios(); - return ch; -} - - -/* Read 1 character without echo */ -char getch(void) { - return getch_(0); -} - - -/* Read 1 character with echo */ -char getche(void) { - return getch_(1); -} - - - -/* Helper function, to let us see if a file */ -/* exists in the current directory. */ -int fexists(char *fname) -{ - FILE *fptr; - fptr = fopen(fname, "r") ; - if ( !fptr ) return -1 ; /* File does not exist in dir. */ - fclose(fptr); - return 0; /* File DOES exist in dir. */ -} - - -/* Helper function to chop newlines off the lines read in. */ -/* Without this being done, an extra newline is inserted */ -/* (which is usually not what is wanted). */ -char *chop(char *s) -{ - s[strcspn(s,"\n")] = '\0'; - return 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. */ -void readfile(char *fname) -{ - int retval = fexists(fname); - - int i; - if (retval == 0) { - /* File exists, so open it. */ - /* We open it in read-write mode so we can */ - /* append new commands to it. */ - FILE *fptr; - fptr = fopen(fname, "rw"); - - for(i=0; i<20; i++) - { - chop(fgets(hist[i], 80, fptr) ); - } - - } /* retval == 0 */ - - else puts("Error! File does not exist. \n"); - -} - - -/* Helper function to print the command-history file. */ -void printfile(void) -{ - int j; - - for(j=0; j<20; j++) - { - puts(hist[j]); - } -} - - - - -/* Helper function. Print a previous command-line WITHOUT */ -/* a newline. */ -void putline(char *str) -{ - char *line = chop(str); - printf("%s", line); -} - int main(void) { - -/* Our "command-history" file. */ -readfile("test.txt"); - - -/* "Pointer" for the history file. */ -int histpnt = 20; - + + /* Read in the command history file. */ + readhistory(); + while(1) - { - - int key = getch(); + { - /* Printable chars. */ - if ( (key >= 32) && (key <= 126) ) - { - /* We have a printable key so print it. */ - putchar(key); - } - - /* Up arrow is 27, 91, 65. ( ESC [ A ) */ - /* Down arrow is 27, 91, 66. ( ESC [ B ) */ - /* Right arrow is 27, 91, 67. ( ESC [ C ) */ - /* Left arrow is 27, 91, 68. ( ESC [ D ) */ - /* Function keys. */ - /* F2 is 27, 79, 81. */ - /* F3 is 27, 79, 82. */ - /* F4 is 27, 79, 83. */ - - /* Backspace */ - else if(key == 127) - { - /* Move left 1 char and delete that char */ - printf("\033[1D"); - printf("\040"); - printf("\033[1D"); - } + keyhandler(); - /* We have an escape key-sequence */ - else if(key == 27) - { - key = getch(); - if(key == 91) - key = getch(); - - if (key == 65) /* Up Arrow */ - { - /* Move one command "back" in history. */ - histpnt -= 1; - /* Clear to end of line. */ - printf("\033[80D"); - printf("\033[K"); - /* Print the pointed-at command-sequence. */ - putline(hist[histpnt]); - } - - if(key == 66) /* Down Arrow */ - { - /* Move one command "forward" in history. */ - histpnt += 1; - /* Clear to end of line. */ - printf("\033[80D"); - printf("\033[K"); - /* Print the pointed-at command-sequence. */ - putline(hist[histpnt]); - } - - if(key == 67) /* Left arrow */ - { - /* Move one character to the left. */ - printf("\033[1C"); - } - - if(key == 68) /* Right arrow */ - { - /* Move one character to the right. */ - printf("\033[1D"); - } - - } /* End of key=27 key sequence. */ - - - /* The Enter key exits. Enter is 10 decimal */ - else if(key == 10) - { - puts("\n"); - puts("Exiting... \n"); - break; - } /* Key = Enter */ - } + } - return 0; - + return 0; + } -- 2.40.0