1 /*
2  * $Id: history.c,v 1.2 2001/06/14 18:16:15 ura Exp $
3  */
4 
5 /*
6  * FreeWnn is a network-extensible Kana-to-Kanji conversion system.
7  * This file is part of FreeWnn.
8  *
9  * Copyright Kyoto University Research Institute for Mathematical Sciences
10  *                 1987, 1988, 1989, 1990, 1991, 1992
11  * Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1999
12  * Copyright ASTEC, Inc. 1987, 1988, 1989, 1990, 1991, 1992
13  * Copyright 1991 by Massachusetts Institute of Technology
14  *
15  * Author: OMRON SOFTWARE Co., Ltd. <freewnn@rd.kyoto.omronsoft.co.jp>
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 2, or (at your option)
20  * any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with GNU Emacs; see the file COPYING.  If not, write to the
29  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30  *
31  * Commentary:
32  *
33  * Change log:
34  *
35  * Last modified date: 8,Feb.1999
36  *
37  * Code:
38  *
39  */
40 /*      Version 4.0
41  */
42 #include <stdio.h>
43 #include "commonhd.h"
44 #include "sdefine.h"
45 #include "xim.h"
46 #include "sheader.h"
47 #include "jslib.h"
48 #include "ext.h"
49 
50 int
init_history()51 init_history ()
52 {
53   int size1;
54   char *area_pter;
55 
56   register History *hist, *pre, *end;
57 
58   if ((history_cunt = max_history) < 1)
59     {
60       history_cunt = 0;
61       return (0);
62     }
63   size1 = history_cunt * sizeof (History);
64   if ((area_pter = Malloc ((unsigned) size1)) == NULL)
65     {
66       history_cunt = 0;
67       wnn_errorno = WNN_MALLOC_ERR;
68       return (-1);
69     }
70   hist = beginning_of_history = end_of_history = add_history = (History *) area_pter;
71   c_c->save_hist = (History *) area_pter;
72   pre = end = hist + (history_cunt - 1);
73   do
74     {
75       hist->hbuffer = NULL;
76       pre->next = hist;
77       hist->previous = pre;
78       hist->num = 0;
79       pre = hist++;
80     }
81   while (hist <= end);
82   return (0);
83 }
84 
85 static void
set_up_history()86 set_up_history ()
87 {
88   if (add_history == beginning_of_history && end_of_history != beginning_of_history)
89     beginning_of_history = beginning_of_history->next;
90   end_of_history = add_history;
91   add_history = add_history->next;
92   current_history = NULL;
93 }
94 
95 int
make_history(wstr,n)96 make_history (wstr, n)
97      register w_char *wstr;
98      register int n;
99 {
100   register char *p;
101 
102   if (!history_cunt)
103     return (0);
104   if (n <= 0 || n > maxchg || *wstr == 0)
105     return (-1);
106 
107   if (n == 1 && NORMAL_CHAR (*wstr))
108     {
109       if (!wchar_holding)
110         {
111           wchar_holding = 1;
112           set_up_history ();
113           end_of_history->num = 0;
114         }
115       if (!(p = Malloc ((unsigned) ((end_of_history->num + 1) * sizeof (w_char)))))
116         {
117           malloc_error ("allocation of data for history");
118           return (-1);
119         }
120       if (end_of_history->hbuffer)
121         Free (end_of_history->hbuffer);
122       end_of_history->hbuffer = (w_char *) p;
123       Strncpy (end_of_history->hbuffer + end_of_history->num, wstr, 1);
124       if (++(end_of_history->num) >= maxchg)
125         wchar_holding = 0;
126       return (0);
127     }
128   if (wchar_holding)
129     wchar_holding = 0;
130   if (n == 1 && !(KANJI_CHAR (*wstr)))
131     /* don't insert cntrol code in history buffer */
132     return (0);
133   if (end_of_history->num == n && Strncmp (end_of_history->hbuffer, wstr, n) == 0)
134     {
135       current_history = NULL;
136       return (0);
137     }
138   if (!(p = Malloc ((unsigned) (n * sizeof (w_char)))))
139     {
140       malloc_error ("allocation of data for history");
141       return (-1);
142     }
143   set_up_history ();
144   if (end_of_history->hbuffer != NULL)
145     Free ((char *) end_of_history->hbuffer);
146   end_of_history->hbuffer = (w_char *) p;
147   Strncpy (end_of_history->hbuffer, wstr, n);
148   end_of_history->num = n;
149 
150   return (0);
151 }
152 
153 static int
get_current_history(wbuf)154 get_current_history (wbuf)
155      register w_char *wbuf;
156 {
157   if (!history_cunt)
158     return (0);
159   if (wchar_holding)
160     wchar_holding = 0;
161   if (current_history == NULL)
162     current_history = end_of_history;
163   Strncpy (wbuf, current_history->hbuffer, current_history->num);
164   return (current_history->num);
165 }
166 
167 void
get_end_of_history(wbuf)168 get_end_of_history (wbuf)
169      register w_char *wbuf;
170 {
171   if (!history_cunt)
172     {
173       *wbuf = 0;
174     }
175   else
176     {
177       if (wchar_holding)
178         wchar_holding = 0;
179       Strncpy (wbuf, end_of_history->hbuffer, end_of_history->num);
180       *(wbuf + end_of_history->num) = 0;
181     }
182 }
183 
184 int
previous_history1(buffer)185 previous_history1 (buffer)
186      register w_char *buffer;
187 {
188   if (!history_cunt || current_history == beginning_of_history)
189     return (0);                 /* do nothing */
190 
191   if (current_history != NULL)
192     current_history = current_history->previous;
193   return (get_current_history (buffer));
194 }
195 
196 int
next_history1(buffer)197 next_history1 (buffer)
198      register w_char *buffer;
199 {
200   if (!history_cunt || current_history == end_of_history || current_history == NULL)
201     return (0);
202 
203   current_history = current_history->next;
204   return (get_current_history (buffer));
205 }
206 
207 void
destroy_history()208 destroy_history ()
209 {
210   register History *p;
211   register int i;
212 
213   if (c_c == NULL || c_c->save_hist == NULL)
214     return;
215   for (i = 0, p = c_c->save_hist; i < history_cunt; i++, p++)
216     {
217       if (p->hbuffer)
218         Free ((char *) p->hbuffer);
219     }
220   Free ((char *) c_c->save_hist);
221 }
222