1
|
1 /* |
|
2 |
1884
|
3 Copyright (C) 1996 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
1343
|
27 #include <climits> |
1345
|
28 #include <csetjmp> |
1346
|
29 #include <cstring> |
1343
|
30 |
1728
|
31 #include <string> |
|
32 |
1349
|
33 #include <fstream.h> |
|
34 #include <iostream.h> |
|
35 #include <strstream.h> |
|
36 |
1350
|
37 #ifdef HAVE_UNISTD_H |
1
|
38 #include <sys/types.h> |
|
39 #include <unistd.h> |
|
40 #endif |
367
|
41 |
138
|
42 #if defined (HAVE_TERMIOS_H) |
|
43 #include <termios.h> |
|
44 #elif defined (HAVE_TERMIO_H) |
1
|
45 #include <termio.h> |
138
|
46 #elif defined (HAVE_SGTTY_H) |
1
|
47 #include <sgtty.h> |
|
48 #else |
|
49 LOSE! LOSE! |
|
50 #endif |
|
51 |
1465
|
52 #ifndef HAVE_STRNCASECMP |
|
53 extern "C" int strncasecmp (const char*, const char*, size_t); |
|
54 #endif |
1
|
55 |
|
56 #include "SLStack.h" |
|
57 |
2233
|
58 #include "file-ops.h" |
1651
|
59 #include "oct-cmplx.h" |
1755
|
60 #include "str-vec.h" |
1651
|
61 |
2204
|
62 #include "defaults.h" |
1352
|
63 #include "defun.h" |
1781
|
64 #include "dir-ops.h" |
1352
|
65 #include "dirfns.h" |
|
66 #include "error.h" |
|
67 #include "gripes.h" |
|
68 #include "help.h" |
|
69 #include "input.h" |
|
70 #include "mappers.h" |
1742
|
71 #include "oct-hist.h" |
1750
|
72 #include "oct-obj.h" |
1352
|
73 #include "pager.h" |
1155
|
74 #include "pathsearch.h" |
1690
|
75 #include "sysdep.h" |
1750
|
76 #include "toplev.h" |
1
|
77 #include "unwind-prot.h" |
1352
|
78 #include "utils.h" |
|
79 #include "variables.h" |
1
|
80 |
|
81 // Top level context (?) |
|
82 extern jmp_buf toplevel; |
|
83 |
581
|
84 // Save a string. |
|
85 |
1
|
86 char * |
|
87 strsave (const char *s) |
|
88 { |
526
|
89 if (! s) |
|
90 return 0; |
1
|
91 |
|
92 int len = strlen (s); |
|
93 char *tmp = new char [len+1]; |
|
94 tmp = strcpy (tmp, s); |
|
95 return tmp; |
|
96 } |
|
97 |
581
|
98 // Return to the main command loop in octave.cc. |
|
99 |
1618
|
100 extern "C" void |
1
|
101 jump_to_top_level (void) |
|
102 { |
|
103 run_all_unwind_protects (); |
|
104 |
|
105 longjmp (toplevel, 1); |
|
106 } |
|
107 |
|
108 int |
1755
|
109 almost_match (const string& std, const string& s, int min_match_len, |
526
|
110 int case_sens) |
1
|
111 { |
1755
|
112 int stdlen = std.length (); |
|
113 int slen = s.length (); |
1
|
114 |
|
115 return (slen <= stdlen |
|
116 && slen >= min_match_len |
287
|
117 && (case_sens |
1755
|
118 ? (strncmp (std.c_str (), s.c_str (), slen) == 0) |
|
119 : (strncasecmp (std.c_str (), s.c_str (), slen) == 0))); |
287
|
120 } |
|
121 |
581
|
122 // Ugh. |
|
123 |
287
|
124 int |
1755
|
125 keyword_almost_match (const char **std, int *min_len, const string& s, |
287
|
126 int min_toks_to_match, int max_toks) |
|
127 { |
|
128 int status = 0; |
|
129 int tok_count = 0; |
|
130 int toks_matched = 0; |
|
131 |
1755
|
132 if (s.empty () || max_toks < 1) |
287
|
133 return status; |
|
134 |
1755
|
135 char *kw = strsave (s.c_str ()); |
287
|
136 |
|
137 char *t = kw; |
|
138 while (*t != '\0') |
|
139 { |
|
140 if (*t == '\t') |
|
141 *t = ' '; |
|
142 t++; |
|
143 } |
|
144 |
|
145 char *beg = kw; |
|
146 while (*beg == ' ') |
|
147 beg++; |
|
148 |
|
149 if (*beg == '\0') |
|
150 return status; |
|
151 |
|
152 |
|
153 char **to_match = new char * [max_toks + 1]; |
526
|
154 const char **s1 = std; |
287
|
155 char **s2 = to_match; |
|
156 |
526
|
157 if (! s1 || ! s2) |
287
|
158 goto done; |
|
159 |
|
160 s2[tok_count] = beg; |
|
161 char *end; |
526
|
162 while ((end = strchr (beg, ' ')) != 0) |
287
|
163 { |
|
164 *end = '\0'; |
|
165 beg = end + 1; |
|
166 |
|
167 while (*beg == ' ') |
|
168 beg++; |
|
169 |
|
170 if (*beg == '\0') |
|
171 break; |
|
172 |
|
173 tok_count++; |
|
174 if (tok_count >= max_toks) |
|
175 goto done; |
|
176 |
|
177 s2[tok_count] = beg; |
|
178 } |
526
|
179 s2[tok_count+1] = 0; |
287
|
180 |
|
181 s2 = to_match; |
|
182 |
|
183 for (;;) |
|
184 { |
|
185 if (! almost_match (*s1, *s2, min_len[toks_matched], 0)) |
|
186 goto done; |
|
187 |
|
188 toks_matched++; |
|
189 |
|
190 s1++; |
|
191 s2++; |
|
192 |
|
193 if (! *s2) |
|
194 { |
|
195 status = (toks_matched >= min_toks_to_match); |
|
196 goto done; |
|
197 } |
|
198 |
|
199 if (! *s1) |
|
200 goto done; |
|
201 } |
|
202 |
|
203 done: |
|
204 |
|
205 delete [] kw; |
|
206 delete [] to_match; |
|
207 |
|
208 return status; |
1
|
209 } |
|
210 |
581
|
211 // See if the given file is in the path. |
|
212 |
1755
|
213 string |
|
214 search_path_for_file (const string& path, const string& name) |
686
|
215 { |
1787
|
216 dir_path p (path); |
686
|
217 |
2204
|
218 return make_absolute (p.find (name), Vcurrent_directory); |
686
|
219 } |
|
220 |
1957
|
221 DEFUN (file_in_path, args, , |
686
|
222 "file_in_path (PATH, NAME)") |
|
223 { |
2086
|
224 octave_value_list retval; |
686
|
225 |
1755
|
226 int argc = args.length () + 1; |
|
227 |
1968
|
228 string_vector argv = args.make_argv ("file_in_path"); |
1755
|
229 |
|
230 if (error_state) |
|
231 return retval; |
686
|
232 |
|
233 if (argc == 3) |
|
234 { |
1755
|
235 string fname = search_path_for_file (argv[1], argv[2]); |
686
|
236 |
1755
|
237 if (fname.empty ()) |
|
238 retval = Matrix (); |
|
239 else |
686
|
240 retval = fname; |
|
241 } |
|
242 else |
|
243 print_usage ("file_in_path"); |
|
244 |
|
245 return retval; |
|
246 } |
|
247 |
1755
|
248 string |
|
249 file_in_path (const string& name, const string& suffix) |
526
|
250 { |
1755
|
251 string nm = name; |
526
|
252 |
1755
|
253 if (! suffix.empty ()) |
|
254 nm.append (suffix); |
686
|
255 |
2204
|
256 if (Vcurrent_directory.empty ()) |
526
|
257 get_working_directory ("file_in_path"); |
|
258 |
2204
|
259 return search_path_for_file (Vload_path, nm); |
526
|
260 } |
|
261 |
581
|
262 // See if there is an function file in the path. If so, return the |
|
263 // full path to the file. |
|
264 |
1755
|
265 string |
|
266 fcn_file_in_path (const string& name) |
526
|
267 { |
1755
|
268 string retval; |
908
|
269 |
1755
|
270 int len = name.length (); |
|
271 |
|
272 if (len > 0) |
|
273 { |
|
274 if (len > 2 && name [len - 2] == '.' && name [len - 1] == 'm') |
|
275 retval = file_in_path (name, ""); |
908
|
276 else |
1755
|
277 retval = file_in_path (name, ".m"); |
908
|
278 } |
1755
|
279 |
|
280 return retval; |
526
|
281 } |
|
282 |
581
|
283 // See if there is an octave file in the path. If so, return the |
|
284 // full path to the file. |
|
285 |
1755
|
286 string |
|
287 oct_file_in_path (const string& name) |
572
|
288 { |
1755
|
289 string retval; |
908
|
290 |
1755
|
291 int len = name.length (); |
|
292 |
|
293 if (len > 0) |
|
294 { |
|
295 if (len > 2 && name [len - 4] == '.' && name [len - 3] == 'o' |
908
|
296 && name [len - 2] == 'c' && name [len - 1] == 't') |
1755
|
297 retval = file_in_path (name, ""); |
908
|
298 else |
1755
|
299 retval = file_in_path (name, ".oct"); |
908
|
300 } |
1755
|
301 |
|
302 return retval; |
572
|
303 } |
|
304 |
1755
|
305 const char * |
801
|
306 undo_string_escape (char c) |
|
307 { |
|
308 if (! c) |
1755
|
309 return ""; |
801
|
310 |
|
311 switch (c) |
|
312 { |
|
313 case '\a': |
|
314 return "\\a"; |
|
315 |
|
316 case '\b': // backspace |
|
317 return "\\b"; |
|
318 |
|
319 case '\f': // formfeed |
|
320 return "\\f"; |
|
321 |
|
322 case '\n': // newline |
|
323 return "\\n"; |
|
324 |
|
325 case '\r': // carriage return |
|
326 return "\\r"; |
|
327 |
|
328 case '\t': // horizontal tab |
|
329 return "\\t"; |
|
330 |
|
331 case '\v': // vertical tab |
|
332 return "\\v"; |
|
333 |
|
334 case '\\': // backslash |
|
335 return "\\\\"; |
|
336 |
|
337 case '"': // double quote |
|
338 return "\\\""; |
|
339 |
|
340 default: |
1755
|
341 { |
|
342 static char retval[2]; |
|
343 retval[0] = c; |
|
344 retval[1] = '\0'; |
|
345 return retval; |
|
346 } |
801
|
347 } |
|
348 } |
|
349 |
1755
|
350 string |
|
351 undo_string_escapes (const string& s) |
801
|
352 { |
1755
|
353 string retval; |
801
|
354 |
1755
|
355 for (size_t i = 0; i < s.length (); i++) |
|
356 retval.append (undo_string_escape (s[i])); |
801
|
357 |
1755
|
358 return retval; |
801
|
359 } |
|
360 |
1957
|
361 DEFUN (undo_string_escapes, args, , |
801
|
362 "undo_string_escapes (STRING)") |
|
363 { |
2086
|
364 octave_value retval; |
801
|
365 |
|
366 int nargin = args.length (); |
|
367 |
|
368 if (nargin == 1 && args(0).is_string ()) |
1755
|
369 retval = undo_string_escapes (args(0).string_value ()); |
801
|
370 else |
1023
|
371 print_usage ("undo_string_escapes"); |
801
|
372 |
|
373 return retval; |
|
374 } |
|
375 |
1711
|
376 // This function was adapted from xputenv from Karl Berry's kpathsearch |
|
377 // library. |
|
378 |
|
379 void |
|
380 oct_putenv (const char *var_name, const char *value) |
|
381 { |
|
382 static const char **saved_env_items = 0; |
|
383 static unsigned saved_len; |
|
384 char *old_item = 0; |
|
385 |
|
386 int new_len = strlen (var_name) + strlen (value) + 2; |
|
387 |
|
388 char *new_item = new char [new_len]; |
|
389 |
|
390 sprintf (new_item, "%s=%s", var_name, value); |
|
391 |
|
392 #ifndef SMART_PUTENV |
|
393 |
|
394 // Check if we have saved anything yet. |
|
395 |
|
396 if (! saved_env_items) |
|
397 { |
|
398 saved_env_items = new const char * [1]; |
|
399 saved_env_items[0] = var_name; |
|
400 saved_len = 1; |
|
401 } |
|
402 else |
|
403 { |
|
404 // Check if we've assigned VAR_NAME before. |
|
405 |
|
406 unsigned len = strlen (var_name); |
|
407 |
|
408 for (unsigned i = 0; i < saved_len && ! old_item; i++) |
|
409 { |
|
410 if (strcmp (saved_env_items[i], var_name) == 0) |
|
411 { |
|
412 old_item = getenv (var_name); |
|
413 |
|
414 assert (old_item); |
|
415 |
|
416 // Back up to the `NAME=' in the environment before the |
|
417 // value that getenv returns. |
|
418 |
|
419 old_item -= (len + 1); |
|
420 } |
|
421 } |
|
422 |
|
423 if (! old_item) |
|
424 { |
|
425 // If we haven't seen VAR_NAME before, save it. Assume it |
|
426 // is in safe storage. |
|
427 |
|
428 saved_len++; |
|
429 |
|
430 const char **tmp = new const char * [saved_len]; |
|
431 |
|
432 for (unsigned i = 0; i < saved_len - 1; i++) |
|
433 tmp[i] = saved_env_items[i]; |
|
434 |
|
435 tmp[saved_len - 1] = var_name; |
|
436 |
|
437 delete [] saved_env_items; |
|
438 |
|
439 saved_env_items = tmp; |
|
440 } |
|
441 } |
|
442 |
|
443 #endif |
|
444 |
|
445 // As far as I can see there's no way to distinguish between the |
|
446 // various errors; putenv doesn't have errno values. |
|
447 |
|
448 if (putenv (new_item) < 0) |
|
449 error ("putenv (%s) failed", new_item); |
|
450 |
|
451 #ifndef SMART_PUTENV |
|
452 |
|
453 // Can't free `new_item' because its contained value is now in |
|
454 // `environ', but we can free `old_item', since it's been replaced. |
|
455 |
|
456 delete [] old_item; |
|
457 |
|
458 #endif |
|
459 } |
|
460 |
2204
|
461 // Check the value of a string variable to see if it it's ok to do |
|
462 // something. |
|
463 // |
|
464 // return of 1 => always ok. |
|
465 // return of 0 => never ok. |
|
466 // return of -1 => ok, but give me warning (default). |
|
467 |
|
468 int |
|
469 check_preference (const string& var) |
|
470 { |
|
471 int pref = -1; |
|
472 |
|
473 string val = builtin_string_variable (var); |
|
474 |
|
475 if (val.empty ()) |
|
476 { |
|
477 double dval = 0; |
|
478 if (builtin_real_scalar_variable (var, dval)) |
|
479 pref = NINT (dval); |
|
480 } |
|
481 else |
|
482 { |
|
483 if (val.compare ("yes", 0, 3) == 0 |
|
484 || val.compare ("true", 0, 4) == 0) |
|
485 pref = 1; |
|
486 else if (val.compare ("never", 0, 5) == 0 |
|
487 || val.compare ("no", 0, 2) == 0 |
|
488 || val.compare ("false", 0, 5) == 0) |
|
489 pref = 0; |
|
490 } |
|
491 |
|
492 return pref; |
|
493 } |
|
494 |
572
|
495 /* |
1
|
496 ;;; Local Variables: *** |
|
497 ;;; mode: C++ *** |
|
498 ;;; End: *** |
|
499 */ |