Edinburgh Speech Tools  2.1-release
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
siod_main.cc
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1999 */
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 /* Authors: Alan W Black */
34 /* Date : February 1999 */
35 /*-----------------------------------------------------------------------*/
36 /* A simple wrap-around for SIOD giving a basic command line SIOD */
37 /* interpreter */
38 /* */
39 /*=======================================================================*/
40 #include <cstdlib>
41 #include "EST_cmd_line.h"
42 #include "EST_cutils.h"
43 #include "EST_Pathname.h"
44 #include "siod.h"
45 
46 static void siod_lisp_vars(void);
47 static void siod_load_default_files(void);
48 
49 void siod_server_init(void);
50 
51 int main(int argc, char **argv)
52 {
53  EST_Option al;
54  EST_StrList files;
55  EST_Litem *p;
56  int stdin_input,interactive;
57  int heap_size = DEFAULT_HEAP_SIZE;
58 
59  parse_command_line
60  (argc, argv,
61  EST_String("[options] [input files]\n")+
62  "Summary: Scheme in one Defun interpreter, file arguments are loaded\n"+
63  "-b Run in batch mode (no interaction)\n"+
64  "--batch Run in batch mode (no interaction)\n"+
65  "-i Run in interactive mode (default)\n"+
66  "--interactive\n"+
67  " Run in interactive mode (default)\n"+
68  "--pipe Run in pipe mode, reading commands from\n"+
69  " stdin, but no prompt or return values\n"+
70  " are printed (default if stdin not a tty)\n"+
71  "-heap <int> {512000}\n"+
72  " Initial size of heap\n",
73  files, al);
74 
75  if (al.present("-heap"))
76  heap_size = al.ival("-heap");
77 
78  // What to do about standard input and producing prompts etc.
79  if ((al.present("-i")) || (al.present("--interactive")))
80  {
81  interactive = TRUE;
82  stdin_input = TRUE;
83  }
84  else if ((al.present("--pipe")))
85  {
86  interactive=FALSE;
87  stdin_input = TRUE;
88  }
89  else if ((al.present("-b")) || (al.present("--batch")))
90  {
91  interactive=FALSE;
92  stdin_input=FALSE;
93  }
94  else if (isatty(0)) // if stdin is a terminal assume interactive
95  {
96  interactive = TRUE;
97  stdin_input = TRUE;
98  }
99  else // else assume pipe mode
100  {
101  interactive = FALSE;
102  stdin_input = TRUE;
103  }
104 
105  siod_init(heap_size);
106  siod_est_init();
107  siod_server_init();
108  siod_fringe_init();
109 
110  siod_prog_name = "siod";
111 
112  siod_lisp_vars();
113 
114  if (interactive)
115  siod_load_default_files();
116 
117  for (p=files.head(); p != 0; p=p->next())
118  {
119  if (files(p) == "-")
120  continue;
121  else if (files(p).matches(make_regex("^(.*")))
122  {
123  LISP l;
124  l = read_from_string(files(p));
125  leval(l,NIL);
126  }
127  else
128  vload(files(p),0);
129 
130  }
131 
132  if (stdin_input)
133  {
134  siod_print_welcome(EST_String::cat("Modified for ", est_name, " v", est_tools_version));
135  siod_repl(interactive); // expect input from stdin
136  }
137 
138  return 0;
139 }
140 
141 static void siod_load_default_files(void)
142 {
143  // Load in default files, init.scm. Users ~/.festivalrc
144  // (or whatever you wish to call it) is loaded by init.scm
145 
146  EST_Pathname initfile;
147 
148  // Load library init first
149  initfile = EST_Pathname(est_datadir).as_directory();
150  initfile += "siod";
151  initfile += "init.scm";
152 
153  if (access((const char *)initfile,R_OK) == 0)
154  vload(initfile,FALSE);
155  else
156  cerr << "Initialization file " << initfile << " not found" << endl;
157 }
158 
159 static void siod_lisp_vars(void)
160 {
161  // set up specific lisp variables
162  int major=0,minor=0,subminor=0;
163 
164  EST_Pathname lib;
165 
166  lib = EST_Pathname(est_libdir).as_directory();
167  lib += "siod";
168  siod_set_lval("libdir",strintern(lib));
169 
170  lib = EST_Pathname(est_datadir).as_directory();
171  lib += "siod";
172  siod_set_lval("datadir",strintern(lib));
173 
174  if (!strcmp(est_ostype,""))
175  siod_set_lval("*ostype*",rintern(est_ostype));
176  siod_set_lval("est_version",
177  strcons(strlen(est_tools_version),est_tools_version));
178 
179  EST_String bits[4];
180  EST_Regex sep = "[^0-9]+";
181  int nbits = split(est_tools_version, bits, 4, sep);
182 
183  if (nbits>0)
184  major = bits[0].Int();
185  if (nbits>1)
186  minor = bits[1].Int();
187  if (nbits>2)
188  subminor = bits[2].Int();
189 
190  siod_set_lval("est_version_number",
191  cons(flocons(major),
192  cons(flocons(minor),
193  cons(flocons(subminor),NIL))));
194 
195  // Modify my PATH to include these directories
196  EST_String path = getenv("PATH");
197 
198  path += ":" + EST_String(est_libdir);
199 
200  putenv(wstrdup("PATH=" + path));
201 
202  siod_set_lval("*modules*",NIL);
203 
204  return;
205 }
206 
int ival(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:76
A Regular expression class to go with the CSTR EST_String class.
Definition: EST_Regex.h:56
static EST_String cat(const EST_String s1, const EST_String s2=Empty, const EST_String s3=Empty, const EST_String s4=Empty, const EST_String s5=Empty, const EST_String s6=Empty, const EST_String s7=Empty, const EST_String s8=Empty, const EST_String s9=Empty)
Definition: EST_String.cc:1082
const int present(const K &rkey) const
Returns true if key is present.
Definition: EST_TKVL.cc:222