Watt-32 tcp/ip  2.2 dev-rel.10
highc.c
Go to the documentation of this file.
1 
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include <io.h>
41 
42 #include "wattcp.h"
43 #include "strings.h"
44 #include "pcconfig.h"
45 
46 #if defined(__HIGHC__) && (DOSX & PHARLAP)
47 
48 /*
49  * HighC's putenv() doesn't modify the inherited environment.
50  * We must do it ourself.
51  */
52 static char *setup_environment (void)
53 {
54  char *copy;
55  char *start;
56  char **env = _environ; /* list of env.var + putenv() vars */
57  UINT size = 0;
58 
59  if (!env || !*env)
60  return (NULL);
61 
62  while (env && *env) /* find how many bytes we need to malloc */
63  size += strlen (*env++) + 1;
64 
65  size++;
66  copy = malloc (size);
67  if (!copy)
68  return (NULL);
69 
70  start = copy;
71  env = _environ;
72  while (env && *env) /* make a new continuous environment block */
73  {
74  int len = strlen (*env) + 1;
75 
76  memcpy (copy, *env++, len);
77  copy += len;
78  }
79  *copy = '\0'; /* final termination */
80  return (start);
81 }
82 
83 static int execute (const char *path, const char *args, const char *env)
84 {
85  union REGS reg;
86  EXEC_BLK blk;
87  char buf[256];
88  int len;
89  ULONG minPages, maxPages;
90 
91  len = strlen (args);
92  if (len > sizeof(buf)-2)
93  return (0);
94 
95  _dx_cmem_usage (0, FALSE, &minPages, &maxPages);
96 
97  strcpy (buf+1, args);
98  buf [0] = len++;
99  buf [len] = '\r';
100 
101  /* if `env' is NULL we'll inherit the environment without any
102  * variables added by `putenv()'
103  */
104  blk.ex_eoff = (UCHAR*)env; /* pass new environment */
105  blk.ex_eseg = SS_DATA;
106  blk.ex_coff = (UCHAR*) buf; /* command line address */
107  blk.ex_cseg = SS_DATA;
108  reg.x.ax = 0x4B00;
109  reg.x.bx = (UINT) &blk;
110  reg.x.dx = (UINT) path;
111  fflush (stdout); /* flush buffers now */
112  fflush (stderr);
113 
114  intdos (&reg, &reg);
115  if (reg.x.cflag & 1) /* carry set, return -1 */
116  return (-1);
117 
118  reg.x.ax = 0x4D00;
119  intdos (&reg, &reg);
120  return (reg.h.al); /* return child exit code */
121 }
122 
123 /*
124  * This replaces system() in hc386.lib
125  * Note: this is aliased in <tcp.h>
126  */
127 int _mw_watt_system (const char *cmd)
128 {
129  char buf[150];
130  const char *env;
131  const char *comspec = getenv ("COMSPEC");
132  int rc;
133 
134  if (!comspec || access(comspec,0))
135  {
136  (*_printf) ("Bad COMSPEC variable\n");
137  return (0);
138  }
139 
140  if (cmd && *cmd)
141  _bprintf (buf, sizeof(buf), " /c %s", cmd);
142  else _bprintf (buf, sizeof(buf), "%s", cmd);
143 
144  env = setup_environment();
145  rc = execute (comspec, buf, env);
146 
147  if (env)
148  free (env);
149  return (rc);
150 }
151 
152 /*
153  * The following functions are called when Call_trace, Prolog_trace
154  * and Epilog_trace pragmas are in effect.
155  * Library must be compiled with: -Hon=Call_trace
156  * -Hon=Prolog_trace
157  * and -Hon=Epilog_trace
158  */
159 
160 #pragma Off(Call_trace) /* no trace of trace itself */
161 #pragma Off(Prolog_trace)
162 #pragma Off(Epilog_trace)
163 #pragma Global_aliasing_convention("%r")
164 
165 #if defined(USE_DEBUG)
166 
167 #define MAX_LEVEL 1000
168 
169 
170 static int level = 0;
171 static const char *caller [MAX_LEVEL];
172 
173 static BOOL is_watt32_func (const char *func) /* incomplete */
174 {
175  if (!strcmp(func,"watt_sock_init"))
176  return (TRUE);
177  if (!strcmp(func,"sock_init"))
178  return (TRUE);
179  if (!strcmp(func,"_w32_init_misc"))
180  return (TRUE);
181  if (!strcmp(func,"setup_dos_xfer_buf"))
182  return (TRUE);
183  if (!strcmp(func,"RDTSC_enabled"))
184  return (TRUE);
185  if (!strcmp(func,"_w32_init_timers"))
186  return (TRUE);
187  if (!strcmp(func,"set_utc_offset"))
188  return (TRUE);
189  if (!strcmp(func,"get_zone"))
190  return (TRUE);
191  if (!strcmp(func,"tcp_init"))
192  return (TRUE);
193  if (!strcmp(func,"_eth_init"))
194  return (TRUE);
195  return (FALSE);
196 }
197 
198 void _call_trace (const char *from, const char *module, unsigned line,
199  const char *to)
200 {
201  static const char *last_module = NULL;
202  char where[30];
203 
204  if (!ctrace_on || level >= DIM(caller)-1)
205  return;
206 
207  _bprintf (where, sizeof(where), "%s(%u)", module, line);
208 
209  (*_printf) ("%-16s:%*s %s() -> %s()\n", where, level, "", from, to);
210 
211  if (caller[level] == NULL ||
212  (strcmp(from,caller[level])) && is_watt32_func(to))
213  caller[level++] = from;
214 
215  else if (last_module && !strcmp(module,last_module) && level > 0)
216  level--;
217 
218  last_module = module;
219 }
220 
221 void _prolog_trace (const char *func)
222 {
223 #if 0
224  if (ctrace_on)
225  (*_printf) ("%s()\n", func);
226 #endif
227  ARGSUSED (func);
228 }
229 
230 void _epilog_trace (const char *func)
231 {
232  if (level > 0 && ctrace_on)
233  --level;
234 }
235 #else /* !USE_DEBUG */
236 
237 void _call_trace (const char *from, const char *module, unsigned line, const char *to)
238 {
239  ARGSUSED (from);
240  ARGSUSED (module);
241  ARGSUSED (line);
242  ARGSUSED (to);
243 }
244 
245 void _prolog_trace (const char *func)
246 {
247  ARGSUSED (func);
248 }
249 
250 void _epilog_trace (const char *func)
251 {
252  ARGSUSED (func);
253 }
254 #endif /* USE_DEBUG */
255 #endif /* __HIGHC__ && (DOSX & PHARLAP) */
Core definitions.
static BOOL is_watt32_func(const char *func)
Definition: highc.c:173