1
|
1 // utils.cc -*- C++ -*- |
|
2 /* |
|
3 |
296
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
|
28 #include <sys/types.h> |
|
29 #ifdef HAVE_UNISTD_H |
|
30 #include <unistd.h> |
|
31 #endif |
|
32 #include <sys/param.h> |
|
33 #include <string.h> |
|
34 #include <limits.h> |
|
35 #include <iostream.h> |
|
36 #include <strstream.h> |
|
37 #include <fstream.h> |
367
|
38 |
526
|
39 #include <Complex.h> |
287
|
40 |
|
41 extern "C" |
|
42 { |
636
|
43 #include <setjmp.h> |
|
44 |
|
45 #ifndef HAVE_STRNCASECMP |
287
|
46 extern int strncasecmp (const char*, const char*, size_t); |
636
|
47 #endif |
287
|
48 |
679
|
49 #define boolean kpathsea_boolean |
|
50 #define false kpathsea_false |
|
51 #define true kpathsea_true |
|
52 #include <kpathsea/pathsearch.h> |
|
53 |
138
|
54 #if defined (HAVE_TERMIOS_H) |
|
55 #include <termios.h> |
|
56 #elif defined (HAVE_TERMIO_H) |
1
|
57 #include <termio.h> |
138
|
58 #elif defined (HAVE_SGTTY_H) |
1
|
59 #include <sgtty.h> |
|
60 #else |
|
61 LOSE! LOSE! |
|
62 #endif |
|
63 |
581
|
64 #include <readline/tilde.h> |
1
|
65 } |
|
66 |
526
|
67 // This mess suggested by the autoconf manual. |
|
68 // unistd.h defines _POSIX_VERSION on POSIX.1 systems. |
|
69 #if defined(DIRENT) || defined(_POSIX_VERSION) |
|
70 #include <dirent.h> |
|
71 #define NLENGTH(dirent) (strlen((dirent)->d_name)) |
|
72 #else /* not (DIRENT or _POSIX_VERSION) */ |
|
73 #define dirent direct |
|
74 #define NLENGTH(dirent) ((dirent)->d_namlen) |
|
75 #ifdef SYSNDIR |
|
76 #include <sys/ndir.h> |
|
77 #endif /* SYSNDIR */ |
|
78 #ifdef SYSDIR |
|
79 #include <sys/dir.h> |
|
80 #endif /* SYSDIR */ |
|
81 #ifdef NDIR |
|
82 #include <ndir.h> |
|
83 #endif /* NDIR */ |
|
84 #endif /* not (DIRENT or _POSIX_VERSION) */ |
|
85 |
1
|
86 #include "SLStack.h" |
|
87 |
|
88 #include "procstream.h" |
|
89 #include "user-prefs.h" |
|
90 #include "variables.h" |
526
|
91 #include "dirfns.h" |
686
|
92 #include "defun.h" |
1
|
93 #include "error.h" |
686
|
94 #include "help.h" |
628
|
95 #include "gripes.h" |
526
|
96 #include "pager.h" |
1
|
97 #include "utils.h" |
175
|
98 #include "input.h" |
1
|
99 #include "octave.h" |
526
|
100 #include "oct-obj.h" |
1
|
101 #include "mappers.h" |
|
102 #include "tree-const.h" |
|
103 #include "unwind-prot.h" |
|
104 #include "octave-hist.h" |
|
105 |
|
106 // Top level context (?) |
|
107 extern jmp_buf toplevel; |
|
108 |
581
|
109 // Save a string. |
|
110 |
1
|
111 char * |
|
112 strsave (const char *s) |
|
113 { |
526
|
114 if (! s) |
|
115 return 0; |
1
|
116 |
|
117 int len = strlen (s); |
|
118 char *tmp = new char [len+1]; |
|
119 tmp = strcpy (tmp, s); |
|
120 return tmp; |
|
121 } |
|
122 |
581
|
123 // Concatenate two strings. |
|
124 |
1
|
125 char * |
|
126 strconcat (const char *s, const char *t) |
|
127 { |
|
128 int len = strlen (s) + strlen (t); |
|
129 char *tmp = new char [len+1]; |
|
130 strcpy (tmp, s); |
|
131 return strcat (tmp, t); |
|
132 } |
|
133 |
581
|
134 // Throw away input until a given character is read. |
|
135 |
1
|
136 void |
|
137 discard_until (istream& stream, char character) |
|
138 { |
|
139 int c; |
|
140 for (;;) |
|
141 { |
|
142 stream >> c; |
|
143 if (c == EOF || c == character) |
|
144 break; |
|
145 } |
|
146 if (c != EOF) |
|
147 stream.putback ((char) c); |
|
148 } |
|
149 |
607
|
150 #if 0 |
|
151 |
|
152 // XXX UNTESTED XXX |
|
153 |
|
154 // Read input until a given character is read. Returns characters |
|
155 // read in a new string that must be freed by the caller. |
|
156 |
|
157 char * |
|
158 read_until (istream& stream, char character) |
|
159 { |
|
160 int grow_size = 8; |
|
161 int limit = grow_size; |
|
162 char *buf = new char [limit]; |
|
163 char *bp = buf; |
|
164 |
|
165 get_more: |
|
166 is.getline (bp, limit, character); |
|
167 |
|
168 if (is.gcount () == 0) |
|
169 { |
|
170 delete [] buf; |
|
171 return 0; |
|
172 } |
|
173 |
|
174 if (is.gcount () == limit && buf[limit-1] != '\0') |
|
175 { |
|
176 char *tmp = new char [limit + grow_size]; |
|
177 strcpy (tmp, buf); |
|
178 delete [] buf; |
|
179 buf = tmp; |
|
180 bp = tmp + limit - 1; |
|
181 limit += grow_size; |
|
182 grow_size *= 2; |
|
183 goto get_more; |
|
184 } |
|
185 |
|
186 return buf; |
|
187 } |
|
188 #endif |
|
189 |
661
|
190 // Get a temporary file name. |
643
|
191 |
|
192 char * |
|
193 octave_tmp_file_name (void) |
|
194 { |
662
|
195 static char *retval = 0; |
|
196 |
|
197 if (retval) |
|
198 free (retval); |
|
199 |
|
200 retval = tempnam (0, "oct-"); |
|
201 |
661
|
202 if (! retval) |
|
203 error ("can't open temporary file!"); |
662
|
204 |
661
|
205 return retval; |
643
|
206 } |
|
207 |
801
|
208 DEFUN ("octave_tmp_file_name", Foctave_tmp_file_name, |
|
209 Soctave_tmp_file_name, 0, 1, |
|
210 "octave_tmp_file_name ()") |
|
211 { |
|
212 tree_constant retval; |
|
213 |
|
214 if (args.length () == 0) |
|
215 retval = octave_tmp_file_name (); |
|
216 else |
|
217 print_usage ("octave_tmp_file_name"); |
|
218 |
|
219 return retval; |
|
220 } |
|
221 |
1
|
222 char ** |
|
223 pathstring_to_vector (char *pathstring) |
|
224 { |
526
|
225 static char **path = 0; |
1
|
226 |
526
|
227 if (pathstring) |
1
|
228 { |
|
229 int nelem = 0; |
240
|
230 char *tmp_path = strsave (pathstring); |
|
231 if (*tmp_path != '\0') |
1
|
232 { |
|
233 nelem++; |
240
|
234 char *ptr = tmp_path; |
1
|
235 while (*ptr != '\0') |
|
236 { |
|
237 if (*ptr == ':') |
|
238 nelem++; |
|
239 ptr++; |
|
240 } |
|
241 } |
|
242 |
240
|
243 char **foo = path; |
|
244 while (foo && *foo) |
244
|
245 delete [] *foo++; |
1
|
246 delete [] path; |
244
|
247 |
1
|
248 path = new char * [nelem+1]; |
526
|
249 path[nelem] = 0; |
1
|
250 |
|
251 int i = 0; |
240
|
252 char *ptr = tmp_path; |
1
|
253 while (i < nelem) |
|
254 { |
|
255 char *end = strchr (ptr, ':'); |
526
|
256 if (end) |
1
|
257 *end = '\0'; |
|
258 char *result = tilde_expand (ptr); |
|
259 path[i] = strsave (result); |
|
260 free (result); |
|
261 ptr = end + 1; |
|
262 i++; |
|
263 } |
|
264 |
240
|
265 delete [] tmp_path; |
1
|
266 } |
|
267 |
|
268 return path; |
|
269 } |
|
270 |
581
|
271 // Return to the main command loop in octave.cc. |
|
272 |
189
|
273 void |
1
|
274 jump_to_top_level (void) |
|
275 { |
|
276 run_all_unwind_protects (); |
|
277 |
|
278 longjmp (toplevel, 1); |
|
279 } |
|
280 |
|
281 int |
526
|
282 almost_match (const char *std, const char *s, int min_match_len, |
|
283 int case_sens) |
1
|
284 { |
|
285 int stdlen = strlen (std); |
|
286 int slen = strlen (s); |
|
287 |
|
288 return (slen <= stdlen |
|
289 && slen >= min_match_len |
287
|
290 && (case_sens |
|
291 ? (strncmp (std, s, slen) == 0) |
|
292 : (strncasecmp (std, s, slen) == 0))); |
|
293 } |
|
294 |
581
|
295 // Ugh. |
|
296 |
287
|
297 int |
|
298 keyword_almost_match (const char **std, int *min_len, const char *s, |
|
299 int min_toks_to_match, int max_toks) |
|
300 { |
|
301 int status = 0; |
|
302 int tok_count = 0; |
|
303 int toks_matched = 0; |
|
304 |
526
|
305 if (! s || *s == '\0' || max_toks < 1) |
287
|
306 return status; |
|
307 |
|
308 char *kw = strsave (s); |
|
309 |
|
310 char *t = kw; |
|
311 while (*t != '\0') |
|
312 { |
|
313 if (*t == '\t') |
|
314 *t = ' '; |
|
315 t++; |
|
316 } |
|
317 |
|
318 char *beg = kw; |
|
319 while (*beg == ' ') |
|
320 beg++; |
|
321 |
|
322 if (*beg == '\0') |
|
323 return status; |
|
324 |
|
325 |
|
326 char **to_match = new char * [max_toks + 1]; |
526
|
327 const char **s1 = std; |
287
|
328 char **s2 = to_match; |
|
329 |
526
|
330 if (! s1 || ! s2) |
287
|
331 goto done; |
|
332 |
|
333 s2[tok_count] = beg; |
|
334 char *end; |
526
|
335 while ((end = strchr (beg, ' ')) != 0) |
287
|
336 { |
|
337 *end = '\0'; |
|
338 beg = end + 1; |
|
339 |
|
340 while (*beg == ' ') |
|
341 beg++; |
|
342 |
|
343 if (*beg == '\0') |
|
344 break; |
|
345 |
|
346 tok_count++; |
|
347 if (tok_count >= max_toks) |
|
348 goto done; |
|
349 |
|
350 s2[tok_count] = beg; |
|
351 } |
526
|
352 s2[tok_count+1] = 0; |
287
|
353 |
|
354 s2 = to_match; |
|
355 |
|
356 for (;;) |
|
357 { |
|
358 if (! almost_match (*s1, *s2, min_len[toks_matched], 0)) |
|
359 goto done; |
|
360 |
|
361 toks_matched++; |
|
362 |
|
363 s1++; |
|
364 s2++; |
|
365 |
|
366 if (! *s2) |
|
367 { |
|
368 status = (toks_matched >= min_toks_to_match); |
|
369 goto done; |
|
370 } |
|
371 |
|
372 if (! *s1) |
|
373 goto done; |
|
374 } |
|
375 |
|
376 done: |
|
377 |
|
378 delete [] kw; |
|
379 delete [] to_match; |
|
380 |
|
381 return status; |
1
|
382 } |
|
383 |
|
384 char ** |
338
|
385 get_fcn_file_names (int& num, const char *dir, int no_suffix) |
1
|
386 { |
|
387 static int num_max = 256; |
|
388 char **retval = new char * [num_max]; |
|
389 int i = 0; |
|
390 |
|
391 DIR *dirp = opendir (dir); |
526
|
392 if (dirp) |
1
|
393 { |
|
394 struct dirent *entry; |
526
|
395 while ((entry = readdir (dirp)) != 0) |
1
|
396 { |
|
397 int len = NLENGTH (entry); |
826
|
398 #ifdef WITH_DLD |
|
399 if ((len > 2 |
|
400 && entry->d_name[len-2] == '.' |
|
401 && entry->d_name[len-1] == 'm') |
|
402 || (len > 4 |
|
403 && entry->d_name[len-4] == '.' |
|
404 && entry->d_name[len-3] == 'o' |
|
405 && entry->d_name[len-2] == 'c' |
|
406 && entry->d_name[len-1] == 't')) |
|
407 #else |
1
|
408 if (len > 2 |
|
409 && entry->d_name[len-2] == '.' |
|
410 && entry->d_name[len-1] == 'm') |
826
|
411 #endif |
1
|
412 { |
|
413 retval[i] = strsave (entry->d_name); |
|
414 if (no_suffix) |
826
|
415 { |
|
416 if (retval[i][len-1] == 'm') |
|
417 retval[i][len-2] = '\0'; |
|
418 else |
|
419 retval[i][len-4] = '\0'; |
|
420 } |
1
|
421 |
|
422 i++; |
|
423 |
|
424 if (i == num_max - 1) |
|
425 { |
244
|
426 // Reallocate the array. Only copy pointers, not the strings they |
|
427 // point to, then only delete the original array of pointers, and not |
|
428 // the strings they point to. |
|
429 |
1
|
430 num_max += 256; |
|
431 char **tmp = new char * [num_max]; |
|
432 for (int j = 0; j < i; j++) |
|
433 tmp[j] = retval[j]; |
|
434 |
244
|
435 delete [] retval; |
|
436 |
1
|
437 retval = tmp; |
|
438 } |
|
439 } |
|
440 } |
106
|
441 closedir (dirp); |
1
|
442 } |
|
443 |
526
|
444 retval[i] = 0; |
1
|
445 num = i; |
|
446 |
|
447 return retval; |
|
448 } |
|
449 |
|
450 char ** |
338
|
451 get_fcn_file_names (int& num, int no_suffix) |
1
|
452 { |
|
453 static int num_max = 1024; |
|
454 char **retval = new char * [num_max]; |
|
455 int i = 0; |
|
456 |
679
|
457 char *path_elt = kpse_path_element (user_pref.loadpath); |
1
|
458 |
679
|
459 while (path_elt) |
1
|
460 { |
679
|
461 str_llist_type *elt_dirs = kpse_element_dirs (path_elt); |
|
462 |
|
463 str_llist_elt_type *dir; |
|
464 for (dir = *elt_dirs; dir; dir = STR_LLIST_NEXT (*dir)) |
1
|
465 { |
679
|
466 char *elt_dir = STR_LLIST (*dir); |
1
|
467 |
679
|
468 if (elt_dir) |
1
|
469 { |
679
|
470 int tmp_num; |
|
471 char **names = get_fcn_file_names (tmp_num, elt_dir, no_suffix); |
|
472 |
|
473 if (i + tmp_num >= num_max - 1) |
|
474 { |
244
|
475 // Reallocate the array. Only copy pointers, not the strings they |
|
476 // point to, then only delete the original array of pointers, and not |
|
477 // the strings they point to. |
|
478 |
679
|
479 num_max += 1024; |
|
480 char **tmp = new char * [num_max]; |
|
481 for (int j = 0; j < i; j++) |
|
482 tmp[j] = retval[j]; |
1
|
483 |
679
|
484 delete [] retval; |
244
|
485 |
679
|
486 retval = tmp; |
|
487 } |
1
|
488 |
679
|
489 int k = 0; |
|
490 while (k < tmp_num) |
|
491 retval[i++] = names[k++]; |
|
492 } |
|
493 } |
1
|
494 |
679
|
495 path_elt = kpse_path_element (0); |
1
|
496 } |
|
497 |
526
|
498 retval[i] = 0; |
1
|
499 num = i; |
|
500 |
|
501 return retval; |
|
502 } |
|
503 |
|
504 int |
|
505 NINT (double x) |
|
506 { |
|
507 if (x > INT_MAX) |
|
508 return INT_MAX; |
|
509 else if (x < INT_MIN) |
|
510 return INT_MIN; |
|
511 else |
|
512 return (x > 0) ? ((int) (x + 0.5)) : ((int) (x - 0.5)); |
|
513 } |
|
514 |
|
515 double |
|
516 D_NINT (double x) |
|
517 { |
|
518 if (xisinf (x) || xisnan (x)) |
|
519 return x; |
|
520 else |
|
521 return floor (x + 0.5); |
|
522 } |
|
523 |
526
|
524 // XXX FIXME XXX -- put these in some file, and make them extern. |
|
525 |
|
526 static int |
|
527 all_strings (const Octave_object& args) |
|
528 { |
|
529 int n = args.length (); |
712
|
530 for (int i = 0; i < n; i++) |
610
|
531 if (! args(i).is_string ()) |
526
|
532 return 0; |
|
533 return 1; |
|
534 } |
|
535 |
|
536 char ** |
578
|
537 make_argv (const Octave_object& args, const char *fcn_name) |
526
|
538 { |
|
539 char **argv = 0; |
|
540 if (all_strings (args)) |
|
541 { |
|
542 int n = args.length (); |
|
543 argv = new char * [n + 1]; |
578
|
544 argv[0] = strsave (fcn_name); |
712
|
545 for (int i = 0; i < n; i++) |
|
546 argv[i+1] = strsave (args(i).string_value ()); |
526
|
547 } |
|
548 else |
578
|
549 error ("%s: expecting all arguments to be strings", fcn_name); |
526
|
550 |
|
551 return argv; |
|
552 } |
|
553 |
719
|
554 // Return non-zero if either NR or NC is zero. Return -1 if this |
|
555 // should be considered fatal; return 1 if this is ok. |
|
556 |
628
|
557 int |
|
558 empty_arg (const char *name, int nr, int nc) |
|
559 { |
|
560 int is_empty = 0; |
|
561 |
|
562 if (nr == 0 || nc == 0) |
|
563 { |
|
564 int flag = user_pref.propagate_empty_matrices; |
|
565 |
|
566 if (flag < 0) |
636
|
567 { |
|
568 gripe_empty_arg (name, 0); |
|
569 is_empty = 1; |
|
570 } |
673
|
571 else if (flag == 0) |
636
|
572 { |
|
573 gripe_empty_arg (name, 1); |
|
574 is_empty = -1; |
|
575 } |
719
|
576 else |
|
577 is_empty = 1; |
628
|
578 } |
|
579 |
|
580 return is_empty; |
|
581 } |
|
582 |
581
|
583 // Format a list in neat columns. Mostly stolen from GNU ls. This |
|
584 // should maybe be in utils.cc. |
|
585 |
526
|
586 ostrstream& |
|
587 list_in_columns (ostrstream& os, char **list) |
|
588 { |
|
589 // Compute the maximum name length. |
|
590 |
|
591 int max_name_length = 0; |
|
592 int total_names = 0; |
|
593 for (char **names = list; *names; names++) |
|
594 { |
|
595 total_names++; |
|
596 int name_length = strlen (*names); |
|
597 if (name_length > max_name_length) |
|
598 max_name_length = name_length; |
|
599 } |
|
600 |
|
601 // Allow at least two spaces between names. |
|
602 |
|
603 max_name_length += 2; |
|
604 |
|
605 // Calculate the maximum number of columns that will fit. |
|
606 |
|
607 int line_length = terminal_columns (); |
|
608 int cols = line_length / max_name_length; |
|
609 if (cols == 0) |
|
610 cols = 1; |
|
611 |
|
612 // Calculate the number of rows that will be in each column except |
|
613 // possibly for a short column on the right. |
|
614 |
|
615 int rows = total_names / cols + (total_names % cols != 0); |
|
616 |
|
617 // Recalculate columns based on rows. |
|
618 |
|
619 cols = total_names / rows + (total_names % rows != 0); |
|
620 |
|
621 names = list; |
|
622 int count; |
|
623 for (int row = 0; row < rows; row++) |
|
624 { |
|
625 count = row; |
|
626 int pos = 0; |
|
627 |
|
628 // Print the next row. |
|
629 |
|
630 while (1) |
|
631 { |
|
632 os << *(names + count); |
|
633 int name_length = strlen (*(names + count)); |
|
634 |
|
635 count += rows; |
|
636 if (count >= total_names) |
|
637 break; |
|
638 |
|
639 int spaces_to_pad = max_name_length - name_length; |
|
640 for (int i = 0; i < spaces_to_pad; i++) |
|
641 os << " "; |
|
642 pos += max_name_length; |
|
643 } |
|
644 os << "\n"; |
|
645 } |
|
646 |
|
647 return os; |
|
648 } |
|
649 |
581
|
650 // See if the given file is in the path. |
|
651 |
526
|
652 char * |
686
|
653 search_path_for_file (const char *path, const char *name) |
|
654 { |
|
655 char *retval = 0; |
|
656 |
|
657 char *tmp = kpse_path_search (path, name, kpathsea_true); |
|
658 |
|
659 if (tmp) |
|
660 { |
|
661 retval = make_absolute (tmp, the_current_working_directory); |
|
662 free (tmp); |
|
663 } |
|
664 |
|
665 return retval; |
|
666 } |
|
667 |
|
668 DEFUN ("file_in_path", Ffile_in_pat, Sfile_in_path, 3, 1, |
|
669 "file_in_path (PATH, NAME)") |
|
670 { |
|
671 Octave_object retval; |
|
672 |
|
673 DEFINE_ARGV("file_in_path"); |
|
674 |
|
675 if (argc == 3) |
|
676 { |
|
677 char *fname = search_path_for_file (argv[1], argv[2]); |
|
678 |
|
679 if (fname) |
|
680 retval = fname; |
|
681 else |
|
682 retval = Matrix (); |
|
683 } |
|
684 else |
|
685 print_usage ("file_in_path"); |
|
686 |
|
687 DELETE_ARGV; |
|
688 |
|
689 return retval; |
|
690 } |
|
691 |
|
692 |
|
693 char * |
526
|
694 file_in_path (const char *name, const char *suffix) |
|
695 { |
|
696 char *retval = 0; |
|
697 |
679
|
698 char *nm = strsave (name); |
686
|
699 |
526
|
700 if (suffix) |
|
701 { |
|
702 char *tmp = nm; |
|
703 nm = strconcat (tmp, suffix); |
|
704 delete [] tmp; |
|
705 } |
|
706 |
|
707 if (! the_current_working_directory) |
|
708 get_working_directory ("file_in_path"); |
|
709 |
686
|
710 retval = search_path_for_file (user_pref.loadpath, nm); |
526
|
711 |
686
|
712 delete [] nm; |
526
|
713 |
|
714 return retval; |
|
715 } |
|
716 |
581
|
717 // See if there is an function file in the path. If so, return the |
|
718 // full path to the file. |
|
719 |
526
|
720 char * |
|
721 fcn_file_in_path (const char *name) |
|
722 { |
908
|
723 if (name) |
|
724 { |
|
725 int len = strlen (name); |
|
726 |
|
727 if (name [len - 2] == '.' && name [len - 1] == 'm') |
|
728 return file_in_path (name, ""); |
|
729 else |
|
730 return file_in_path (name, ".m"); |
|
731 } |
|
732 else |
|
733 return 0; |
526
|
734 } |
|
735 |
581
|
736 // See if there is an octave file in the path. If so, return the |
|
737 // full path to the file. |
|
738 |
572
|
739 char * |
|
740 oct_file_in_path (const char *name) |
|
741 { |
908
|
742 if (name) |
|
743 { |
|
744 int len = strlen (name); |
|
745 |
|
746 if (name [len - 4] == '.' && name [len - 3] == 'o' |
|
747 && name [len - 2] == 'c' && name [len - 1] == 't') |
|
748 return file_in_path (name, ""); |
|
749 else |
|
750 return file_in_path (name, ".oct"); |
|
751 } |
|
752 else |
|
753 return 0; |
572
|
754 } |
|
755 |
801
|
756 char * |
|
757 undo_string_escape (char c) |
|
758 { |
|
759 static char retval[2]; |
|
760 retval[1] = '\0'; |
|
761 |
|
762 if (! c) |
|
763 return 0; |
|
764 |
|
765 switch (c) |
|
766 { |
|
767 case '\a': |
|
768 return "\\a"; |
|
769 |
|
770 case '\b': // backspace |
|
771 return "\\b"; |
|
772 |
|
773 case '\f': // formfeed |
|
774 return "\\f"; |
|
775 |
|
776 case '\n': // newline |
|
777 return "\\n"; |
|
778 |
|
779 case '\r': // carriage return |
|
780 return "\\r"; |
|
781 |
|
782 case '\t': // horizontal tab |
|
783 return "\\t"; |
|
784 |
|
785 case '\v': // vertical tab |
|
786 return "\\v"; |
|
787 |
|
788 case '\\': // backslash |
|
789 return "\\\\"; |
|
790 |
|
791 case '"': // double quote |
|
792 return "\\\""; |
|
793 |
|
794 default: |
|
795 retval[0] = c; |
|
796 return retval; |
|
797 } |
|
798 } |
|
799 |
|
800 char * |
|
801 undo_string_escapes (char *s) |
|
802 { |
|
803 ostrstream buf; |
|
804 |
|
805 char *t; |
|
806 while (t = undo_string_escape (*s++)) |
|
807 buf << t; |
|
808 buf << ends; |
|
809 |
|
810 return buf.str (); |
|
811 } |
|
812 |
|
813 DEFUN ("undo_string_escapes", Fundo_string_escapes, |
|
814 Sundo_string_escapes, 1, 1, |
|
815 "undo_string_escapes (STRING)") |
|
816 { |
|
817 tree_constant retval; |
|
818 |
|
819 int nargin = args.length (); |
|
820 |
|
821 if (nargin == 1 && args(0).is_string ()) |
|
822 { |
|
823 char *str = undo_string_escapes (args(0).string_value ()); |
|
824 retval = str; |
|
825 delete [] str; |
|
826 } |
|
827 else |
|
828 print_usage ("undo_string_escapaes"); |
|
829 |
|
830 return retval; |
|
831 } |
|
832 |
572
|
833 /* |
1
|
834 ;;; Local Variables: *** |
|
835 ;;; mode: C++ *** |
|
836 ;;; page-delimiter: "^/\\*" *** |
|
837 ;;; End: *** |
|
838 */ |