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 |
2926
|
60 #include "file-stat.h" |
1651
|
61 #include "oct-cmplx.h" |
2926
|
62 #include "oct-env.h" |
1755
|
63 #include "str-vec.h" |
1651
|
64 |
2492
|
65 #include <defaults.h> |
1352
|
66 #include "defun.h" |
1781
|
67 #include "dir-ops.h" |
1352
|
68 #include "dirfns.h" |
|
69 #include "error.h" |
|
70 #include "gripes.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 // Return to the main command loop in octave.cc. |
|
86 |
1618
|
87 extern "C" void |
1
|
88 jump_to_top_level (void) |
|
89 { |
2985
|
90 unwind_protect::run_all (); |
1
|
91 |
|
92 longjmp (toplevel, 1); |
|
93 } |
|
94 |
|
95 int |
1755
|
96 almost_match (const string& std, const string& s, int min_match_len, |
526
|
97 int case_sens) |
1
|
98 { |
1755
|
99 int stdlen = std.length (); |
|
100 int slen = s.length (); |
1
|
101 |
|
102 return (slen <= stdlen |
|
103 && slen >= min_match_len |
287
|
104 && (case_sens |
1755
|
105 ? (strncmp (std.c_str (), s.c_str (), slen) == 0) |
|
106 : (strncasecmp (std.c_str (), s.c_str (), slen) == 0))); |
287
|
107 } |
|
108 |
581
|
109 // Ugh. |
|
110 |
287
|
111 int |
1755
|
112 keyword_almost_match (const char **std, int *min_len, const string& s, |
287
|
113 int min_toks_to_match, int max_toks) |
|
114 { |
|
115 int status = 0; |
|
116 int tok_count = 0; |
|
117 int toks_matched = 0; |
|
118 |
1755
|
119 if (s.empty () || max_toks < 1) |
287
|
120 return status; |
|
121 |
1755
|
122 char *kw = strsave (s.c_str ()); |
287
|
123 |
|
124 char *t = kw; |
|
125 while (*t != '\0') |
|
126 { |
|
127 if (*t == '\t') |
|
128 *t = ' '; |
|
129 t++; |
|
130 } |
|
131 |
|
132 char *beg = kw; |
|
133 while (*beg == ' ') |
|
134 beg++; |
|
135 |
|
136 if (*beg == '\0') |
|
137 return status; |
|
138 |
|
139 |
|
140 char **to_match = new char * [max_toks + 1]; |
526
|
141 const char **s1 = std; |
287
|
142 char **s2 = to_match; |
|
143 |
526
|
144 if (! s1 || ! s2) |
287
|
145 goto done; |
|
146 |
|
147 s2[tok_count] = beg; |
|
148 char *end; |
526
|
149 while ((end = strchr (beg, ' ')) != 0) |
287
|
150 { |
|
151 *end = '\0'; |
|
152 beg = end + 1; |
|
153 |
|
154 while (*beg == ' ') |
|
155 beg++; |
|
156 |
|
157 if (*beg == '\0') |
|
158 break; |
|
159 |
|
160 tok_count++; |
|
161 if (tok_count >= max_toks) |
|
162 goto done; |
|
163 |
|
164 s2[tok_count] = beg; |
|
165 } |
526
|
166 s2[tok_count+1] = 0; |
287
|
167 |
|
168 s2 = to_match; |
|
169 |
|
170 for (;;) |
|
171 { |
|
172 if (! almost_match (*s1, *s2, min_len[toks_matched], 0)) |
|
173 goto done; |
|
174 |
|
175 toks_matched++; |
|
176 |
|
177 s1++; |
|
178 s2++; |
|
179 |
|
180 if (! *s2) |
|
181 { |
|
182 status = (toks_matched >= min_toks_to_match); |
|
183 goto done; |
|
184 } |
|
185 |
|
186 if (! *s1) |
|
187 goto done; |
|
188 } |
|
189 |
|
190 done: |
|
191 |
|
192 delete [] kw; |
|
193 delete [] to_match; |
|
194 |
|
195 return status; |
1
|
196 } |
|
197 |
2234
|
198 // Return non-zero if either NR or NC is zero. Return -1 if this |
|
199 // should be considered fatal; return 1 if this is ok. |
|
200 |
|
201 int |
|
202 empty_arg (const char *name, int nr, int nc) |
|
203 { |
|
204 int is_empty = 0; |
|
205 |
|
206 if (nr == 0 || nc == 0) |
|
207 { |
|
208 int flag = Vpropagate_empty_matrices; |
|
209 |
|
210 if (flag < 0) |
|
211 { |
|
212 gripe_empty_arg (name, 0); |
|
213 is_empty = 1; |
|
214 } |
|
215 else if (flag == 0) |
|
216 { |
|
217 gripe_empty_arg (name, 1); |
|
218 is_empty = -1; |
|
219 } |
|
220 else |
|
221 is_empty = 1; |
|
222 } |
|
223 |
|
224 return is_empty; |
|
225 } |
|
226 |
581
|
227 // See if the given file is in the path. |
|
228 |
1755
|
229 string |
|
230 search_path_for_file (const string& path, const string& name) |
686
|
231 { |
1787
|
232 dir_path p (path); |
686
|
233 |
2926
|
234 return octave_env::make_absolute (p.find (name), octave_env::getcwd ()); |
686
|
235 } |
|
236 |
1957
|
237 DEFUN (file_in_path, args, , |
686
|
238 "file_in_path (PATH, NAME)") |
|
239 { |
2086
|
240 octave_value_list retval; |
686
|
241 |
1755
|
242 int argc = args.length () + 1; |
|
243 |
1968
|
244 string_vector argv = args.make_argv ("file_in_path"); |
1755
|
245 |
|
246 if (error_state) |
|
247 return retval; |
686
|
248 |
|
249 if (argc == 3) |
|
250 { |
1755
|
251 string fname = search_path_for_file (argv[1], argv[2]); |
686
|
252 |
1755
|
253 if (fname.empty ()) |
|
254 retval = Matrix (); |
|
255 else |
686
|
256 retval = fname; |
|
257 } |
|
258 else |
|
259 print_usage ("file_in_path"); |
|
260 |
|
261 return retval; |
|
262 } |
|
263 |
1755
|
264 string |
|
265 file_in_path (const string& name, const string& suffix) |
526
|
266 { |
1755
|
267 string nm = name; |
526
|
268 |
1755
|
269 if (! suffix.empty ()) |
|
270 nm.append (suffix); |
686
|
271 |
2204
|
272 return search_path_for_file (Vload_path, nm); |
526
|
273 } |
|
274 |
581
|
275 // See if there is an function file in the path. If so, return the |
|
276 // full path to the file. |
|
277 |
1755
|
278 string |
|
279 fcn_file_in_path (const string& name) |
526
|
280 { |
1755
|
281 string retval; |
908
|
282 |
1755
|
283 int len = name.length (); |
|
284 |
|
285 if (len > 0) |
|
286 { |
|
287 if (len > 2 && name [len - 2] == '.' && name [len - 1] == 'm') |
|
288 retval = file_in_path (name, ""); |
908
|
289 else |
1755
|
290 retval = file_in_path (name, ".m"); |
908
|
291 } |
1755
|
292 |
|
293 return retval; |
526
|
294 } |
|
295 |
581
|
296 // See if there is an octave file in the path. If so, return the |
|
297 // full path to the file. |
|
298 |
1755
|
299 string |
|
300 oct_file_in_path (const string& name) |
572
|
301 { |
1755
|
302 string retval; |
908
|
303 |
1755
|
304 int len = name.length (); |
|
305 |
|
306 if (len > 0) |
|
307 { |
|
308 if (len > 2 && name [len - 4] == '.' && name [len - 3] == 'o' |
908
|
309 && name [len - 2] == 'c' && name [len - 1] == 't') |
1755
|
310 retval = file_in_path (name, ""); |
908
|
311 else |
1755
|
312 retval = file_in_path (name, ".oct"); |
908
|
313 } |
1755
|
314 |
|
315 return retval; |
572
|
316 } |
|
317 |
1755
|
318 const char * |
801
|
319 undo_string_escape (char c) |
|
320 { |
|
321 if (! c) |
1755
|
322 return ""; |
801
|
323 |
|
324 switch (c) |
|
325 { |
|
326 case '\a': |
|
327 return "\\a"; |
|
328 |
|
329 case '\b': // backspace |
|
330 return "\\b"; |
|
331 |
|
332 case '\f': // formfeed |
|
333 return "\\f"; |
|
334 |
|
335 case '\n': // newline |
|
336 return "\\n"; |
|
337 |
|
338 case '\r': // carriage return |
|
339 return "\\r"; |
|
340 |
|
341 case '\t': // horizontal tab |
|
342 return "\\t"; |
|
343 |
|
344 case '\v': // vertical tab |
|
345 return "\\v"; |
|
346 |
|
347 case '\\': // backslash |
|
348 return "\\\\"; |
|
349 |
|
350 case '"': // double quote |
|
351 return "\\\""; |
|
352 |
|
353 default: |
1755
|
354 { |
|
355 static char retval[2]; |
|
356 retval[0] = c; |
|
357 retval[1] = '\0'; |
|
358 return retval; |
|
359 } |
801
|
360 } |
|
361 } |
|
362 |
1755
|
363 string |
|
364 undo_string_escapes (const string& s) |
801
|
365 { |
1755
|
366 string retval; |
801
|
367 |
1755
|
368 for (size_t i = 0; i < s.length (); i++) |
|
369 retval.append (undo_string_escape (s[i])); |
801
|
370 |
1755
|
371 return retval; |
801
|
372 } |
|
373 |
1957
|
374 DEFUN (undo_string_escapes, args, , |
801
|
375 "undo_string_escapes (STRING)") |
|
376 { |
2086
|
377 octave_value retval; |
801
|
378 |
|
379 int nargin = args.length (); |
|
380 |
|
381 if (nargin == 1 && args(0).is_string ()) |
1755
|
382 retval = undo_string_escapes (args(0).string_value ()); |
801
|
383 else |
1023
|
384 print_usage ("undo_string_escapes"); |
801
|
385 |
|
386 return retval; |
|
387 } |
|
388 |
2285
|
389 static void |
|
390 warn_old_style_preference (bool val, const string& sval) |
|
391 { |
|
392 warning |
|
393 ("preference of \"%s\" is obsolete -- use numeric value of %d instead", |
|
394 sval.c_str (), (val ? 1 : 0)); |
|
395 } |
|
396 |
2204
|
397 // Check the value of a string variable to see if it it's ok to do |
|
398 // something. |
|
399 // |
|
400 // return of 1 => always ok. |
|
401 // return of 0 => never ok. |
|
402 // return of -1 => ok, but give me warning (default). |
|
403 |
|
404 int |
|
405 check_preference (const string& var) |
|
406 { |
|
407 int pref = -1; |
|
408 |
|
409 string val = builtin_string_variable (var); |
|
410 |
|
411 if (val.empty ()) |
|
412 { |
|
413 double dval = 0; |
|
414 if (builtin_real_scalar_variable (var, dval)) |
|
415 pref = NINT (dval); |
|
416 } |
|
417 else |
|
418 { |
|
419 if (val.compare ("yes", 0, 3) == 0 |
|
420 || val.compare ("true", 0, 4) == 0) |
2285
|
421 { |
|
422 warn_old_style_preference (true, val); |
|
423 pref = 1; |
|
424 } |
2204
|
425 else if (val.compare ("never", 0, 5) == 0 |
|
426 || val.compare ("no", 0, 2) == 0 |
|
427 || val.compare ("false", 0, 5) == 0) |
2285
|
428 { |
|
429 warn_old_style_preference (false, val); |
|
430 pref = 0; |
|
431 } |
2204
|
432 } |
|
433 |
|
434 return pref; |
|
435 } |
|
436 |
572
|
437 /* |
1
|
438 ;;; Local Variables: *** |
|
439 ;;; mode: C++ *** |
|
440 ;;; End: *** |
|
441 */ |