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