Annotation of OpenXM_contrib2/asir2000/parse/history.c, Revision 1.4
1.2 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.3 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.2 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.4 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/parse/history.c,v 1.3 2000/08/22 05:04:27 noro Exp $
1.2 noro 49: */
1.1 noro 50: #include <stdio.h>
51: #include <string.h>
52: #include <malloc.h>
53:
54: char *prev_hist(void), *next_hist(void), *search_hist(char *p);
55: void write_hist(char *fname), read_hist(char *fname);
56: void add_hist(char *p), init_hist(int max);
57: void reset_traverse(void);
58:
59: static char **hist_table;
60: static int hist_full, hist_current, hist_length;
61: static int hist_traverse, hist_traverse_index;
62: #define CURRENT (hist_table[hist_current])
63:
64: void init_hist(int max)
65: {
1.4 ! noro 66: hist_length = max;
! 67: hist_table = (char **)calloc(hist_length,sizeof(char *));
! 68: hist_full = 0;
! 69: hist_current = -1;
1.1 noro 70: }
71:
72: void reset_traverse(void)
73: {
1.4 ! noro 74: hist_traverse = 0;
1.1 noro 75: }
76:
77: void add_hist(char *p)
78: {
1.4 ! noro 79: if ( hist_current >= 0 && CURRENT && !strcmp(p,CURRENT) )
! 80: return;
! 81: hist_current++;
! 82: if ( hist_current == hist_length ) {
! 83: hist_current = 0;
! 84: hist_full = 1;
! 85: }
! 86: CURRENT = (char *)realloc(CURRENT,strlen(p)+1);
! 87: strcpy(CURRENT,p);
1.1 noro 88: }
89:
90: char *search_hist(char *p)
91: {
1.4 ! noro 92: int len,slen,index,i;
1.1 noro 93:
1.4 ! noro 94: if ( *p == '!' ) {
! 95: if ( hist_current < 0 )
! 96: return 0;
! 97: else
! 98: return CURRENT;
! 99: }
! 100: slen = strlen(p);
! 101: len = hist_full ? hist_length : hist_current+1;
! 102: index = hist_current;
! 103: for ( i = len; i; i--, index-- ) {
! 104: if ( index < 0 )
! 105: index += hist_length;
! 106: if ( !strncmp(p,hist_table[index],slen) )
! 107: return hist_table[index];
! 108: }
! 109: return 0;
1.1 noro 110: }
111:
112: void read_hist(char *fname)
113: {
1.4 ! noro 114: int l;
! 115: char buf[BUFSIZ];
! 116: FILE *fp;
! 117:
! 118: if ( !(fp = fopen(fname,"rb")) )
! 119: return;
! 120: while ( fgets(buf,BUFSIZ-1,fp) ) {
! 121: l = strlen(buf);
! 122: if ( buf[l-1] == '\n' )
! 123: buf[l-1] = 0;
! 124: add_hist(buf);
! 125: }
! 126: fclose(fp);
1.1 noro 127: }
128:
129: void write_hist(char *fname)
130: {
1.4 ! noro 131: int len,index,i;
! 132: FILE *fp;
1.1 noro 133:
1.4 ! noro 134: if ( !(fp = fopen(fname,"wb")) )
! 135: return;
! 136: len = hist_full ? hist_length : hist_current+1;
! 137: index = hist_full ? hist_current+1 : 0;
! 138: for ( i = 0; i < len; i++, index++ ) {
! 139: if ( index >= hist_length )
! 140: index -= hist_length;
! 141: fprintf(fp,"%s\n",hist_table[index]);
! 142: }
! 143: fclose(fp);
1.1 noro 144: }
145:
146: char *next_hist(void) {
1.4 ! noro 147: if ( !hist_traverse || hist_traverse_index == hist_current )
! 148: return 0;
! 149: else {
! 150: hist_traverse_index = (hist_traverse_index+1) % hist_length;
! 151: return hist_table[hist_traverse_index];
! 152: }
1.1 noro 153: }
154:
155: char *prev_hist(void) {
1.4 ! noro 156: if ( !hist_traverse ) {
! 157: if ( hist_current >= 0 ) {
! 158: hist_traverse = 1;
! 159: hist_traverse_index = hist_current;
! 160: return hist_table[hist_traverse_index];
! 161: } else
! 162: return 0;
! 163: } else {
! 164: hist_traverse_index--;
! 165: if ( hist_traverse_index < 0 )
! 166: if ( hist_full )
! 167: hist_traverse_index += hist_length;
! 168: else
! 169: hist_traverse_index = 0;
! 170: return hist_table[hist_traverse_index];
! 171: }
1.1 noro 172: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>