Edinburgh Speech Tools  2.1-release
siodeditline.c
Go to the documentation of this file.
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1996,1997 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author : Alan W Black */
34 /* Date : December 1998 */
35 /*-----------------------------------------------------------------------*/
36 /* */
37 /* Due to incompatibility between the GPL for readline and Festival's */
38 /* current licence readline support was removed after 1.3.0 */
39 /* This uses a much simpler but much poorer command line editor called */
40 /* editline instead. */
41 /* */
42 /* Although this code is included in our distribution we still offer */
43 /* optional compilation as it may not work on all platforms */
44 /* */
45 /*=======================================================================*/
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include "EST_unix.h"
49 #include <string.h>
50 #include "EST_cutils.h"
51 #include "siodeditline.h"
52 
53 FILE *stddebug = NULL;
54 extern int el_pos;
55 extern char *repl_prompt;
56 
57 #ifndef SUPPORT_EDITLINE
58 
59 /* If for some reason you don't want editline the following */
60 /* functions are provided. They are functional but minimal. They are */
61 /* suitable when you are in an embedded environment and don't need */
62 /* command lin editing */
65 
66 int siod_el_getc(FILE *f)
67 {
68  (void) f;
69  int c;
70 
71  if (el_pos == -1)
72  {
73  fprintf(stdout,"%s",repl_prompt);
74  fflush(stdout);
75  el_pos = 0;
76  }
77 
78  c = getc(f);
79 
80  if (c == '\n')
81  el_pos = -1;
82 
83  return c;
84 }
85 
86 void siod_el_ungetc(int c, FILE *f)
87 {
88  ungetc(c,f);
89 }
90 
91 void siod_el_init(void)
92 {
93  return;
94 }
95 #else
96 #include "editline.h"
97 
98 static int possible_commandp(char *text, int start, int end);
99 static int possible_variablep(char *text, int start, int end);
100 static char **command_completion (char *text,int start,int end);
101 
102 static char *el_line = NULL;
103 
104 char *editline_history_file = ".editline_history";
105 static char *full_history_file = ".editline_history";
106 
107 static STATUS siod_display_doc ()
108 {
109  /* Find the current symbol and check for a documentation string */
110  char *symbol;
111  const char *docstring;
112  int i;
113 
114  symbol = el_current_sym();
115  putc('\n',stderr);
116  docstring = siod_docstring(symbol);
117  for (i=0; docstring[i] != '\0'; i++)
118  putc(docstring[i],stderr);
119  putc('\n',stderr);
120  fflush(stderr);
121  wfree(symbol);
122  el_redisplay();
123  return CSmove;
124 }
125 
126 static STATUS siod_say_doc ()
127 {
128  /* Find the current symbol and check for a documentation string */
129  /* Now this is what you call wasting your time. Here we get the */
130  /* synthesizer to say the documentation string */
131  char *symbol;
132 
133  symbol = el_current_sym();
134  fprintf(stderr,"\nsynthesizing doc string ...");
135  fflush(stderr);
136  siod_saydocstring(symbol);
137  putc('\n',stderr);
138  fflush(stderr);
139  wfree(symbol);
140  el_redisplay();
141  return CSmove;
142 }
143 
144 static STATUS siod_manual()
145 {
146  /* Find the current symbol and check for a documentation string */
147  /* Look for a "see " reference in its documentation string, if so */
148  /* access that section of the manual by sending a call to netscape */
149  char *symbol;
150  const char *infostring;
151 
152  symbol = el_current_sym();
153  infostring = siod_manual_sym(symbol);
154  putc('\n',stderr);
155  fprintf(stderr,"%s",infostring);
156  fflush(stderr);
157  putc('\n',stderr);
158  fflush(stderr);
159  el_redisplay();
160  wfree(symbol);
161  return CSmove;
162 }
163 
164 void siod_el_init(void)
165 {
166  /* Various initialization completion, history etc */
167  char *home;
168 
169  home = getenv("HOME");
170  if (home==NULL)
171  home="";
172  full_history_file =
173  walloc(char,strlen(home)+strlen(editline_history_file)+2);
174  sprintf(full_history_file,"%s/%s",home,editline_history_file);
175  read_history(full_history_file);
176  el_user_intr = TRUE; /* we want SIGINT to raise a signal */
177 
178  el_user_completion_function = command_completion;
179  el_bind_key_in_metamap('h',siod_display_doc);
180  el_bind_key_in_metamap('s',siod_say_doc);
181  el_bind_key_in_metamap('m',siod_manual);
182 }
183 
184 int siod_el_getc(FILE *f)
185 {
186  (void) f;
187  int c;
188 
189  if (el_pos == -1)
190  {
191  el_line=readline(repl_prompt);
192  if (el_line != NULL)
193  {
194  add_history(el_line);
195  write_history(full_history_file);
196  }
197  el_pos = 0;
198  }
199  if ((el_line==NULL) ||
200  ( el_pos >= 0 && strlen(el_line) <= (size_t) el_pos))
201  el_pos = -1;
202  if (el_line==NULL)
203  c = EOF;
204  else if (el_pos == -1)
205  c = '\n'; /* whitespace representing end of line */
206  else
207  {
208  c = el_line[el_pos];
209  el_pos++;
210  }
211 
212  return c;
213 }
214 
215 void siod_el_ungetc(int c, FILE *f)
216 {
217  (void) f;
218  (void) c;
219  if (el_pos > 0)
220  el_pos--;
221  else
222  {
223  fprintf(stderr,"fix ungetc when nothing is there");
224  }
225 }
226 
227 static int qsort_str_compare(const void *p1,const void *p2)
228 {
229  const char *s1;
230  const char *s2;
231 
232  s1 = *(const char **)p1;
233  s2 = *(const char **)p2;
234 
235  return strcmp(s1,s2);
236 }
237 
238 static char **command_completion (char *text,int start,int end)
239 {
240  char **matches = NULL;
241  int i;
242 
243  /* If preceding non-alphanum character is a left paren, */
244  /* look for a command else look for any variable */
245  if (possible_commandp(text,start,end))
246  matches = siod_command_generator(text+start,end-start);
247  else if (possible_variablep(text,start,end))
248  matches = siod_variable_generator(text+start,end-start);
249 
250  if (matches && matches[0] && matches[1])
251  {
252  /* If there are at least two, Sort them */
253  for (i=0; matches[i] != NULL; i++);
254  qsort(matches,i,sizeof(char *),qsort_str_compare);
255  }
256 
257  return matches;
258 }
259 
260 static int possible_commandp(char *text, int start, int end)
261 {
262  (void) end;
263  /* If non-white space previous to this is a left paren */
264  /* signal we are looking for a function name */
265  int t;
266 
267  for (t=start-1; t >= 0; t--)
268  if (strchr(" \t\n\r",text[t]) != NULL)
269  continue;
270  else if (text[t] == '(')
271  return TRUE;
272  else
273  return FALSE;
274 
275  return FALSE;
276 }
277 
278 static int possible_variablep(char *text, int start, int end)
279 {
280  (void) end;
281  /* Almost negative of above but if previous symbol is a quote */
282  /* let the file completion stuff do it */
283  int t;
284 
285  for (t=start-1; t >= 0; t--)
286  if (strchr(" \t\n",text[t]) != NULL)
287  continue;
288  else if (text[t] == '(')
289  return FALSE;
290  else if ((text[t] == '"') &&
291  (t == start-1))
292  return FALSE;
293  else
294  return TRUE;
295 
296  return TRUE;
297 }
298 
299 #endif /* SUPPORT_EDITLINE */
float end(const EST_Item &item)
Definition: EST_item_aux.cc:96
#define walloc(TYPE, SIZE)
Definition: EST_walloc.h:52
void read_history(const char *history_file)
Definition: editline.c:1313
void qsort(EST_TList< T > &a)
EL_USER_COMPLETION_FUNCTION_TYPE * el_user_completion_function
Definition: editline.c:79
char ** siod_command_generator(char *text, int length)
Definition: siod.cc:481
const char * siod_manual_sym(const char *symbol)
Definition: slib_doc.cc:223
void el_redisplay()
Definition: editline.c:1459
char * editline_history_file
Definition: editline.c:76
FILE * stddebug
Definition: siodeditline.c:53
char * repl_prompt
Definition: siod.cc:43
int el_pos
Definition: slib.cc:168
char * getenv()
void write_history(const char *history_file)
Definition: editline.c:1296
int siod_el_getc(FILE *f)
Definition: siodeditline.c:66
void el_bind_key_in_metamap(char c, Keymap_Function func)
Definition: editline.c:1967
int el_user_intr
Definition: el_sys_unix.c:53
const char * siod_docstring(const char *symbol)
Definition: slib_doc.cc:214
void siod_saydocstring(const char *symbol)
Definition: slib_doc.cc:234
#define FALSE
Definition: EST_bool.h:119
int el_no_echo
Definition: siodeditline.c:63
NULL
Definition: EST_WFST.cc:55
f
Definition: EST_item_aux.cc:48
STATUS
Definition: editline.h:60
void add_history(char *p)
Definition: editline.c:1395
void siod_el_ungetc(int c, FILE *f)
Definition: siodeditline.c:86
float start(const EST_Item &item)
Definition: EST_item_aux.cc:52
void siod_el_init(void)
Definition: siodeditline.c:91
int editline_histsize
Definition: siodeditline.c:64
char * readline(CONST char *prompt)
Definition: editline.c:1354
void wfree(void *p)
Definition: walloc.c:131
#define TRUE
Definition: EST_bool.h:118
char * el_current_sym()
Definition: editline.c:1464
char ** siod_variable_generator(char *text, int length)
Definition: siod.cc:441