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