Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
slib_sys.cc
1 /*
2  * COPYRIGHT (c) 1988-1994 BY *
3  * PARADIGM ASSOCIATES INCORPORATED, CAMBRIDGE, MASSACHUSETTS. *
4  * See the source file SLIB.C for more information. *
5 
6  * Reorganization of files (Mar 1999) by Alan W Black <awb@cstr.ed.ac.uk>
7 
8  * System functions
9 
10 */
11 #include <cstdio>
12 #include "siod.h"
13 #include "siodp.h"
14 
15 #ifdef unix
16 #include <sys/time.h>
17 #include <unistd.h>
18 static long siod_time_base;
19 #endif
20 
21 static LISP lgetenv(LISP name)
22 {
23  return rintern(getenv(get_c_string(name)));
24 }
25 
26 static LISP lsetenv(LISP name,LISP value)
27 {
28  char *entry=walloc(char,strlen(get_c_string(name))+
29  strlen(get_c_string(value))+16);
30  sprintf(entry,"%s=%s",get_c_string(name),get_c_string(value));
31  putenv(entry);
32  return name;
33 }
34 
35 static LISP lsystem(LISP name)
36 {
37  (void)system(get_c_string(name));
38  return NIL;
39 }
40 
41 static LISP lpwd(void)
42 {
43  char *cwd;
44 
45  cwd = getcwd(NULL,1024);
46 
47  return cintern(cwd);
48 }
49 
50 static LISP lchdir(LISP args, LISP env)
51 {
52  (void)env;
53  char *home;
54 
55  if (siod_llength(args) == 0)
56  {
57  home = getenv("HOME");
58  chdir(home);
59  return rintern(home);
60  }
61  else
62  {
63  chdir(get_c_string(leval(car(args),env)));
64  return (car(args));
65  }
66 }
67 
68 static LISP lgetpid(void)
69 {
70  return flocons((float)getpid());
71 }
72 
73 LISP siod_time()
74 {
75 #ifdef unix
76  struct timeval tv;
77  struct timezone tz;
78 
79  gettimeofday(&tv,&tz);
80 
81  return flocons(((double)(tv.tv_sec-siod_time_base))+
82  ((double)tv.tv_usec/1000000));
83 #else
84  return flocons(0);
85 #endif
86 }
87 
88 void init_subrs_sys(void)
89 {
90 
91 #ifdef unix
92  struct timeval tv;
93  struct timezone tz;
94 
95  gettimeofday(&tv,&tz);
96 
97  siod_time_base = tv.tv_sec;
98 #endif
99 
100  init_subr_0("getpid",lgetpid,
101  "(getpid)\n\
102  Return process id.");
103  init_fsubr("cd",lchdir,
104  "(cd DIRNAME)\n\
105  Change directory to DIRNAME, if DIRNAME is nil or not specified \n\
106  change directory to user's HOME directory.");
107  init_subr_0("pwd",lpwd,
108  "(pwd)\n\
109  Returns current directory as a string.");
110  init_subr_1("getenv",lgetenv,
111  "(getenv VARNAME)\n\
112  Returns value of UNIX environment variable VARNAME, or nil if VARNAME\n\
113  is unset.");
114  init_subr_2("setenv",lsetenv,
115  "(setenv VARNAME VALUE)\n\
116  Set the UNIX environment variable VARNAME to VALUE.");
117  init_subr_1("system",lsystem,
118  "(system COMMAND)\n\
119  Execute COMMAND (a string) with the UNIX shell.");
120  init_subr_0("time", siod_time,
121  "(time)\n\
122  Returns number of seconds since start of epoch (if OS permits it\n\
123  countable).");
124 
125 }