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