Edinburgh Speech Tools  2.1-release
slib_sys.cc
Go to the documentation of this file.
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 #endif
19 
20 static LISP lgetenv(LISP name)
21 {
22  return rintern(getenv(get_c_string(name)));
23 }
24 
25 static LISP lsetenv(LISP name,LISP value)
26 {
27  char *entry=walloc(char,strlen(get_c_string(name))+
28  strlen(get_c_string(value))+16);
29  sprintf(entry,"%s=%s",get_c_string(name),get_c_string(value));
30  putenv(entry);
31  return name;
32 }
33 
34 static LISP lsystem(LISP name)
35 {
36  return flocons((double)system(get_c_string(name)));
37 }
38 
39 static LISP lpwd(void)
40 {
41  char *cwd;
42 
43  cwd = getcwd(NULL,1024);
44 
45  return cintern(cwd);
46 }
47 
48 static LISP lchdir(LISP args, LISP env)
49 {
50  (void)env;
51  char *home;
52  int chdir_result;
53  if (siod_llength(args) == 0)
54  {
55  home = getenv("HOME");
56  chdir_result = chdir(home);
57  if (chdir_result == 0)
58  return rintern(home);
59  else
60  return NIL;
61  }
62  else
63  {
64  chdir_result = chdir(get_c_string(leval(car(args),env)));
65  if (chdir_result == 0)
66  return (car(args));
67  else
68  return NIL;
69  }
70 }
71 
72 static LISP lgetpid(void)
73 {
74  return flocons((float)getpid());
75 }
76 
77 static long siod_time_base;
78 
79 LISP siod_time()
80 {
81 #ifdef __unix__
82  struct timeval tv;
83  struct timezone tz;
84 
85  gettimeofday(&tv,&tz);
86 
87  return flocons(((double)(tv.tv_sec-siod_time_base))+
88  ((double)tv.tv_usec/1000000));
89 #else
90  return flocons(0);
91 #endif
92 }
93 
94 void init_subrs_sys(void)
95 {
96 
97 #ifdef __unix__
98  struct timeval tv;
99  struct timezone tz;
100 
101  gettimeofday(&tv,&tz);
102 
103  siod_time_base = tv.tv_sec;
104 #endif
105 
106  init_subr_0("getpid",lgetpid,
107  "(getpid)\n\
108  Return process id.");
109  init_fsubr("cd",lchdir,
110  "(cd DIRNAME)\n\
111  Change directory to DIRNAME, if DIRNAME is nil or not specified \n\
112  change directory to user's HOME directory.");
113  init_subr_0("pwd",lpwd,
114  "(pwd)\n\
115  Returns current directory as a string.");
116  init_subr_1("getenv",lgetenv,
117  "(getenv VARNAME)\n\
118  Returns value of UNIX environment variable VARNAME, or nil if VARNAME\n\
119  is unset.");
120  init_subr_2("setenv",lsetenv,
121  "(setenv VARNAME VALUE)\n\
122  Set the UNIX environment variable VARNAME to VALUE.");
123  init_subr_1("system",lsystem,
124  "(system COMMAND)\n\
125  Execute COMMAND (a string) with the UNIX shell.");
126  init_subr_0("time", siod_time,
127  "(time)\n\
128  Returns number of seconds since start of epoch (if OS permits it\n\
129  countable).");
130 
131 }
void init_subrs_sys(void)
Definition: slib_sys.cc:94
#define walloc(TYPE, SIZE)
Definition: EST_walloc.h:52
#define NIL
Definition: siod_defs.h:92
int siod_llength(LISP list)
Definition: siod.cc:202
void init_subr_2(const char *name, LISP(*fcn)(LISP, LISP), const char *doc)
Definition: slib.cc:900
void init_fsubr(const char *name, LISP(*fcn)(LISP, LISP), const char *doc)
Definition: slib.cc:908
char * getenv()
const char * get_c_string(LISP x)
Definition: slib.cc:638
LISP cintern(const char *name)
Definition: slib.cc:728
NULL
Definition: EST_WFST.cc:55
void init_subr_1(const char *name, LISP(*fcn)(LISP), const char *doc)
Definition: slib.cc:898
LISP siod_time()
Definition: slib_sys.cc:79
LISP rintern(const char *name)
Definition: slib.cc:734
LISP leval(LISP x, LISP env)
Definition: slib.cc:1378
void init_subr_0(const char *name, LISP(*fcn)(void), const char *doc)
Definition: slib.cc:896
LISP flocons(double x)
Definition: slib.cc:673
LISP car(LISP x)
Definition: slib_list.cc:115