Edinburgh Speech Tools  2.1-release
siod_main.cc
Go to the documentation of this file.
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 using namespace std;
47 
48 static void siod_lisp_vars(void);
49 static void siod_load_default_files(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;
58 
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  {
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_libdir).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 
167  lib += "siod";
168 
169  siod_set_lval("libdir",strintern(lib));
170 
171  if (!strcmp(est_ostype,""))
172  siod_set_lval("*ostype*",rintern(est_ostype));
173  siod_set_lval("est_version",
175 
176  EST_String bits[4];
177  EST_Regex sep = "[^0-9]+";
178  int nbits = split(est_tools_version, bits, 4, sep);
179 
180  if (nbits>0)
181  major = bits[0].Int();
182  if (nbits>1)
183  minor = bits[1].Int();
184  if (nbits>2)
185  subminor = bits[2].Int();
186 
187  siod_set_lval("est_version_number",
188  cons(flocons(major),
189  cons(flocons(minor),
190  cons(flocons(subminor),NIL))));
191 
192  EST_Pathname etcdircommon = est_libdir;
193  etcdircommon += "etc";
194 
195  EST_Pathname etcdir = etcdircommon;
196  etcdir += est_ostype;
197 
198  // Modify my PATH to include these directories
199  siod_set_lval("etc-path",cons(rintern(etcdir),
200  cons(rintern(etcdircommon),NIL)));
201 
202  EST_String path = getenv("PATH");
203 
204  path += ":" + EST_String(etcdir) + ":" + EST_String(etcdircommon);
205 
206  putenv(wstrdup("PATH=" + path));
207 
208  siod_set_lval("*modules*",NIL);
209 
210  return;
211 }
212 
char * wstrdup(const char *s)
Definition: walloc.c:117
LISP siod_set_lval(const char *name, LISP val)
Definition: siod.cc:113
int ival(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:82
long heap_size
Definition: slib.cc:116
A Regular expression class to go with the CSTR EST_String class.
Definition: EST_Regex.h:56
#define NIL
Definition: siod_defs.h:92
void siod_est_init()
Definition: siod_est.cc:409
#define DEFAULT_HEAP_SIZE
Definition: siod_defs.h:29
LISP strintern(const char *data)
Definition: slib_str.cc:22
LISP strcons(long length, const char *data)
Definition: slib_str.cc:27
const char * siod_prog_name
Definition: slib.cc:171
int bits
Definition: rxp.c:20
char * getenv()
EST_UItem * next()
Definition: EST_UList.h:55
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:1084
LISP vload(const char *fname, long cflag)
Definition: slib_file.cc:632
EST_Regex & make_regex(const char *r)
Definition: siod.cc:361
LISP cons(LISP x, LISP y)
Definition: slib_list.cc:97
int siod_repl(int interactive)
Definition: slib_repl.cc:44
LISP read_from_string(const char *)
Definition: slib_str.cc:65
#define FALSE
Definition: EST_bool.h:119
void siod_print_welcome(EST_String extra_info)
Definition: slib.cc:239
const char *const est_tools_version
Definition: EST_cutils.c:49
LISP rintern(const char *name)
Definition: slib.cc:734
LISP leval(LISP x, LISP env)
Definition: slib.cc:1378
const char *const est_name
Definition: EST_cutils.c:52
int present(const K &rkey) const
Returns true if key is present.
Definition: EST_TKVL.cc:222
const char *const est_ostype
Definition: EST_cutils.c:64
LISP flocons(double x)
Definition: slib.cc:673
EST_UItem * head() const
Definition: EST_UList.h:97
int main(int argc, char **argv)
Definition: siod_main.cc:51
int siod_init(int heap_size=DEFAULT_HEAP_SIZE)
Definition: siod.cc:58
EST_String
EST_Pathname as_directory(void) const
#define TRUE
Definition: EST_bool.h:118
int parse_command_line(int argc, char *argv[], const EST_String &usage, EST_StrList &files, EST_Option &al, int make_stdio=1)
Definition: cmd_line.cc:101
const char *const est_libdir
Definition: EST_cutils.c:62