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