Annotation of OpenXM_contrib2/windows/asir32gui/readline.c, Revision 1.1
1.1 ! noro 1: //#include <sys/file.h>
! 2: #include <stdlib.h>
! 3: #include <stdio.h>
! 4: #include <fcntl.h>
! 5: #include <ctype.h>
! 6: //#include <sys/types.h>
! 7: //#include <sys/stat.h>
! 8:
! 9: #define CTRL(c) ((c)&037)
! 10:
! 11: char *search_hist(),*next_hist(),*prev_hist();
! 12:
! 13: static char **hist_table;
! 14: static int hist_full, hist_current, hist_length;
! 15: static int hist_traverse, hist_traverse_index;
! 16:
! 17: #define CURRENT (hist_table[hist_current])
! 18:
! 19: init_hist(max)
! 20: int max;
! 21: {
! 22: hist_length = max;
! 23: hist_table = (char **)GC_malloc(hist_length*sizeof(char *));
! 24: hist_full = 0;
! 25: hist_current = -1;
! 26: }
! 27:
! 28: add_hist(p)
! 29: char *p;
! 30: {
! 31: if ( hist_current >= 0 && !strcmp(p,CURRENT) )
! 32: return;
! 33: hist_current++;
! 34: if ( hist_current == hist_length ) {
! 35: hist_current = 0;
! 36: hist_full = 1;
! 37: }
! 38: CURRENT = (char *)GC_malloc_atomic(strlen(p)+1);
! 39: strcpy(CURRENT,p);
! 40: }
! 41:
! 42: char *search_hist(p)
! 43: char *p;
! 44: {
! 45: int len,slen,index,i;
! 46:
! 47: if ( *p == '!' ) {
! 48: if ( hist_current < 0 )
! 49: return 0;
! 50: else
! 51: return CURRENT;
! 52: }
! 53: slen = strlen(p);
! 54: len = hist_full ? hist_length : hist_current+1;
! 55: index = hist_current;
! 56: for ( i = len; i; i--, index-- ) {
! 57: if ( index < 0 )
! 58: index += hist_length;
! 59: if ( !strncmp(p,hist_table[index],slen) )
! 60: return hist_table[index];
! 61: }
! 62: return 0;
! 63: }
! 64:
! 65: read_hist(fname)
! 66: char *fname;
! 67: {
! 68: int l;
! 69: char buf[BUFSIZ];
! 70: FILE *fp;
! 71:
! 72: if ( !(fp = fopen(fname,"rb")) )
! 73: return;
! 74: while ( fgets(buf,BUFSIZ-1,fp) ) {
! 75: l = strlen(buf);
! 76: if ( buf[l-1] == '\n' )
! 77: buf[l-1] = 0;
! 78: add_hist(buf);
! 79: }
! 80: fclose(fp);
! 81: }
! 82:
! 83: write_hist(fname)
! 84: char *fname;
! 85: {
! 86: int len,index,i;
! 87: FILE *fp;
! 88:
! 89: if ( !(fp = fopen(fname,"wb")) )
! 90: return;
! 91: len = hist_full ? hist_length : hist_current+1;
! 92: index = hist_full ? hist_current+1 : 0;
! 93: for ( i = 0; i < len; i++, index++ ) {
! 94: if ( index >= hist_length )
! 95: index -= hist_length;
! 96: fprintf(fp,"%s\n",hist_table[index]);
! 97: }
! 98: fclose(fp);
! 99: }
! 100:
! 101: char *next_hist() {
! 102: if ( !hist_traverse || hist_traverse_index == hist_current )
! 103: return 0;
! 104: else {
! 105: hist_traverse_index = (hist_traverse_index+1) % hist_length;
! 106: return hist_table[hist_traverse_index];
! 107: }
! 108: }
! 109:
! 110: char *prev_hist() {
! 111: if ( !hist_traverse ) {
! 112: if ( hist_current >= 0 ) {
! 113: hist_traverse = 1;
! 114: hist_traverse_index = hist_current;
! 115: return hist_table[hist_traverse_index];
! 116: } else
! 117: return 0;
! 118: } else {
! 119: hist_traverse_index--;
! 120: if ( hist_traverse_index < 0 )
! 121: if ( hist_full )
! 122: hist_traverse_index += hist_length;
! 123: else
! 124: hist_traverse_index = 0;
! 125: return hist_table[hist_traverse_index];
! 126: }
! 127: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>