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 |
2234
|
211 // Return non-zero if either NR or NC is zero. Return -1 if this |
|
212 // should be considered fatal; return 1 if this is ok. |
|
213 |
|
214 int |
|
215 empty_arg (const char *name, int nr, int nc) |
|
216 { |
|
217 int is_empty = 0; |
|
218 |
|
219 if (nr == 0 || nc == 0) |
|
220 { |
|
221 int flag = Vpropagate_empty_matrices; |
|
222 |
|
223 if (flag < 0) |
|
224 { |
|
225 gripe_empty_arg (name, 0); |
|
226 is_empty = 1; |
|
227 } |
|
228 else if (flag == 0) |
|
229 { |
|
230 gripe_empty_arg (name, 1); |
|
231 is_empty = -1; |
|
232 } |
|
233 else |
|
234 is_empty = 1; |
|
235 } |
|
236 |
|
237 return is_empty; |
|
238 } |
|
239 |
581
|
240 // See if the given file is in the path. |
|
241 |
1755
|
242 string |
|
243 search_path_for_file (const string& path, const string& name) |
686
|
244 { |
1787
|
245 dir_path p (path); |
686
|
246 |
2204
|
247 return make_absolute (p.find (name), Vcurrent_directory); |
686
|
248 } |
|
249 |
1957
|
250 DEFUN (file_in_path, args, , |
686
|
251 "file_in_path (PATH, NAME)") |
|
252 { |
2086
|
253 octave_value_list retval; |
686
|
254 |
1755
|
255 int argc = args.length () + 1; |
|
256 |
1968
|
257 string_vector argv = args.make_argv ("file_in_path"); |
1755
|
258 |
|
259 if (error_state) |
|
260 return retval; |
686
|
261 |
|
262 if (argc == 3) |
|
263 { |
1755
|
264 string fname = search_path_for_file (argv[1], argv[2]); |
686
|
265 |
1755
|
266 if (fname.empty ()) |
|
267 retval = Matrix (); |
|
268 else |
686
|
269 retval = fname; |
|
270 } |
|
271 else |
|
272 print_usage ("file_in_path"); |
|
273 |
|
274 return retval; |
|
275 } |
|
276 |
1755
|
277 string |
|
278 file_in_path (const string& name, const string& suffix) |
526
|
279 { |
1755
|
280 string nm = name; |
526
|
281 |
1755
|
282 if (! suffix.empty ()) |
|
283 nm.append (suffix); |
686
|
284 |
2204
|
285 if (Vcurrent_directory.empty ()) |
526
|
286 get_working_directory ("file_in_path"); |
|
287 |
2204
|
288 return search_path_for_file (Vload_path, nm); |
526
|
289 } |
|
290 |
581
|
291 // See if there is an function file in the path. If so, return the |
|
292 // full path to the file. |
|
293 |
1755
|
294 string |
|
295 fcn_file_in_path (const string& name) |
526
|
296 { |
1755
|
297 string retval; |
908
|
298 |
1755
|
299 int len = name.length (); |
|
300 |
|
301 if (len > 0) |
|
302 { |
|
303 if (len > 2 && name [len - 2] == '.' && name [len - 1] == 'm') |
|
304 retval = file_in_path (name, ""); |
908
|
305 else |
1755
|
306 retval = file_in_path (name, ".m"); |
908
|
307 } |
1755
|
308 |
|
309 return retval; |
526
|
310 } |
|
311 |
581
|
312 // See if there is an octave file in the path. If so, return the |
|
313 // full path to the file. |
|
314 |
1755
|
315 string |
|
316 oct_file_in_path (const string& name) |
572
|
317 { |
1755
|
318 string retval; |
908
|
319 |
1755
|
320 int len = name.length (); |
|
321 |
|
322 if (len > 0) |
|
323 { |
|
324 if (len > 2 && name [len - 4] == '.' && name [len - 3] == 'o' |
908
|
325 && name [len - 2] == 'c' && name [len - 1] == 't') |
1755
|
326 retval = file_in_path (name, ""); |
908
|
327 else |
1755
|
328 retval = file_in_path (name, ".oct"); |
908
|
329 } |
1755
|
330 |
|
331 return retval; |
572
|
332 } |
|
333 |
1755
|
334 const char * |
801
|
335 undo_string_escape (char c) |
|
336 { |
|
337 if (! c) |
1755
|
338 return ""; |
801
|
339 |
|
340 switch (c) |
|
341 { |
|
342 case '\a': |
|
343 return "\\a"; |
|
344 |
|
345 case '\b': // backspace |
|
346 return "\\b"; |
|
347 |
|
348 case '\f': // formfeed |
|
349 return "\\f"; |
|
350 |
|
351 case '\n': // newline |
|
352 return "\\n"; |
|
353 |
|
354 case '\r': // carriage return |
|
355 return "\\r"; |
|
356 |
|
357 case '\t': // horizontal tab |
|
358 return "\\t"; |
|
359 |
|
360 case '\v': // vertical tab |
|
361 return "\\v"; |
|
362 |
|
363 case '\\': // backslash |
|
364 return "\\\\"; |
|
365 |
|
366 case '"': // double quote |
|
367 return "\\\""; |
|
368 |
|
369 default: |
1755
|
370 { |
|
371 static char retval[2]; |
|
372 retval[0] = c; |
|
373 retval[1] = '\0'; |
|
374 return retval; |
|
375 } |
801
|
376 } |
|
377 } |
|
378 |
1755
|
379 string |
|
380 undo_string_escapes (const string& s) |
801
|
381 { |
1755
|
382 string retval; |
801
|
383 |
1755
|
384 for (size_t i = 0; i < s.length (); i++) |
|
385 retval.append (undo_string_escape (s[i])); |
801
|
386 |
1755
|
387 return retval; |
801
|
388 } |
|
389 |
1957
|
390 DEFUN (undo_string_escapes, args, , |
801
|
391 "undo_string_escapes (STRING)") |
|
392 { |
2086
|
393 octave_value retval; |
801
|
394 |
|
395 int nargin = args.length (); |
|
396 |
|
397 if (nargin == 1 && args(0).is_string ()) |
1755
|
398 retval = undo_string_escapes (args(0).string_value ()); |
801
|
399 else |
1023
|
400 print_usage ("undo_string_escapes"); |
801
|
401 |
|
402 return retval; |
|
403 } |
|
404 |
1711
|
405 // This function was adapted from xputenv from Karl Berry's kpathsearch |
|
406 // library. |
|
407 |
|
408 void |
|
409 oct_putenv (const char *var_name, const char *value) |
|
410 { |
|
411 static const char **saved_env_items = 0; |
|
412 static unsigned saved_len; |
|
413 char *old_item = 0; |
|
414 |
|
415 int new_len = strlen (var_name) + strlen (value) + 2; |
|
416 |
|
417 char *new_item = new char [new_len]; |
|
418 |
|
419 sprintf (new_item, "%s=%s", var_name, value); |
|
420 |
|
421 #ifndef SMART_PUTENV |
|
422 |
|
423 // Check if we have saved anything yet. |
|
424 |
|
425 if (! saved_env_items) |
|
426 { |
|
427 saved_env_items = new const char * [1]; |
|
428 saved_env_items[0] = var_name; |
|
429 saved_len = 1; |
|
430 } |
|
431 else |
|
432 { |
|
433 // Check if we've assigned VAR_NAME before. |
|
434 |
|
435 unsigned len = strlen (var_name); |
|
436 |
|
437 for (unsigned i = 0; i < saved_len && ! old_item; i++) |
|
438 { |
|
439 if (strcmp (saved_env_items[i], var_name) == 0) |
|
440 { |
|
441 old_item = getenv (var_name); |
|
442 |
|
443 assert (old_item); |
|
444 |
|
445 // Back up to the `NAME=' in the environment before the |
|
446 // value that getenv returns. |
|
447 |
|
448 old_item -= (len + 1); |
|
449 } |
|
450 } |
|
451 |
|
452 if (! old_item) |
|
453 { |
|
454 // If we haven't seen VAR_NAME before, save it. Assume it |
|
455 // is in safe storage. |
|
456 |
|
457 saved_len++; |
|
458 |
|
459 const char **tmp = new const char * [saved_len]; |
|
460 |
|
461 for (unsigned i = 0; i < saved_len - 1; i++) |
|
462 tmp[i] = saved_env_items[i]; |
|
463 |
|
464 tmp[saved_len - 1] = var_name; |
|
465 |
|
466 delete [] saved_env_items; |
|
467 |
|
468 saved_env_items = tmp; |
|
469 } |
|
470 } |
|
471 |
|
472 #endif |
|
473 |
|
474 // As far as I can see there's no way to distinguish between the |
|
475 // various errors; putenv doesn't have errno values. |
|
476 |
|
477 if (putenv (new_item) < 0) |
|
478 error ("putenv (%s) failed", new_item); |
|
479 |
|
480 #ifndef SMART_PUTENV |
|
481 |
|
482 // Can't free `new_item' because its contained value is now in |
|
483 // `environ', but we can free `old_item', since it's been replaced. |
|
484 |
|
485 delete [] old_item; |
|
486 |
|
487 #endif |
|
488 } |
|
489 |
2204
|
490 // Check the value of a string variable to see if it it's ok to do |
|
491 // something. |
|
492 // |
|
493 // return of 1 => always ok. |
|
494 // return of 0 => never ok. |
|
495 // return of -1 => ok, but give me warning (default). |
|
496 |
|
497 int |
|
498 check_preference (const string& var) |
|
499 { |
|
500 int pref = -1; |
|
501 |
|
502 string val = builtin_string_variable (var); |
|
503 |
|
504 if (val.empty ()) |
|
505 { |
|
506 double dval = 0; |
|
507 if (builtin_real_scalar_variable (var, dval)) |
|
508 pref = NINT (dval); |
|
509 } |
|
510 else |
|
511 { |
|
512 if (val.compare ("yes", 0, 3) == 0 |
|
513 || val.compare ("true", 0, 4) == 0) |
|
514 pref = 1; |
|
515 else if (val.compare ("never", 0, 5) == 0 |
|
516 || val.compare ("no", 0, 2) == 0 |
|
517 || val.compare ("false", 0, 5) == 0) |
|
518 pref = 0; |
|
519 } |
|
520 |
|
521 return pref; |
|
522 } |
|
523 |
572
|
524 /* |
1
|
525 ;;; Local Variables: *** |
|
526 ;;; mode: C++ *** |
|
527 ;;; End: *** |
|
528 */ |