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> |
|
30 |
1
|
31 #include <sys/types.h> |
|
32 #ifdef HAVE_UNISTD_H |
|
33 #include <unistd.h> |
|
34 #endif |
|
35 #include <sys/param.h> |
|
36 #include <iostream.h> |
|
37 #include <strstream.h> |
|
38 #include <fstream.h> |
367
|
39 |
526
|
40 #include <Complex.h> |
287
|
41 |
|
42 extern "C" |
|
43 { |
636
|
44 #include <setjmp.h> |
|
45 |
|
46 #ifndef HAVE_STRNCASECMP |
287
|
47 extern int strncasecmp (const char*, const char*, size_t); |
636
|
48 #endif |
287
|
49 |
138
|
50 #if defined (HAVE_TERMIOS_H) |
|
51 #include <termios.h> |
|
52 #elif defined (HAVE_TERMIO_H) |
1
|
53 #include <termio.h> |
138
|
54 #elif defined (HAVE_SGTTY_H) |
1
|
55 #include <sgtty.h> |
|
56 #else |
|
57 LOSE! LOSE! |
|
58 #endif |
|
59 |
581
|
60 #include <readline/tilde.h> |
1
|
61 } |
|
62 |
526
|
63 // This mess suggested by the autoconf manual. |
|
64 // unistd.h defines _POSIX_VERSION on POSIX.1 systems. |
958
|
65 #if defined (HAVE_DIRENT_H) || defined (_POSIX_VERSION) |
526
|
66 #include <dirent.h> |
|
67 #define NLENGTH(dirent) (strlen((dirent)->d_name)) |
958
|
68 #else |
526
|
69 #define dirent direct |
|
70 #define NLENGTH(dirent) ((dirent)->d_namlen) |
958
|
71 #if defined (HAVE_SYS_NDIR_H) |
526
|
72 #include <sys/ndir.h> |
958
|
73 #endif |
|
74 #if defined (HAVE_SYS_DIR_H) |
526
|
75 #include <sys/dir.h> |
958
|
76 #endif |
|
77 #if defined (HAVE_NDIR_H) |
526
|
78 #include <ndir.h> |
958
|
79 #endif |
|
80 #endif |
526
|
81 |
1
|
82 #include "SLStack.h" |
|
83 |
1155
|
84 #include "pathsearch.h" |
1
|
85 #include "procstream.h" |
|
86 #include "user-prefs.h" |
|
87 #include "variables.h" |
526
|
88 #include "dirfns.h" |
686
|
89 #include "defun.h" |
1
|
90 #include "error.h" |
686
|
91 #include "help.h" |
628
|
92 #include "gripes.h" |
526
|
93 #include "pager.h" |
1
|
94 #include "utils.h" |
175
|
95 #include "input.h" |
1
|
96 #include "octave.h" |
526
|
97 #include "oct-obj.h" |
1
|
98 #include "mappers.h" |
|
99 #include "tree-const.h" |
|
100 #include "unwind-prot.h" |
|
101 #include "octave-hist.h" |
|
102 |
|
103 // Top level context (?) |
|
104 extern jmp_buf toplevel; |
|
105 |
581
|
106 // Save a string. |
|
107 |
1
|
108 char * |
|
109 strsave (const char *s) |
|
110 { |
526
|
111 if (! s) |
|
112 return 0; |
1
|
113 |
|
114 int len = strlen (s); |
|
115 char *tmp = new char [len+1]; |
|
116 tmp = strcpy (tmp, s); |
|
117 return tmp; |
|
118 } |
|
119 |
581
|
120 // Concatenate two strings. |
|
121 |
1
|
122 char * |
|
123 strconcat (const char *s, const char *t) |
|
124 { |
|
125 int len = strlen (s) + strlen (t); |
|
126 char *tmp = new char [len+1]; |
|
127 strcpy (tmp, s); |
1266
|
128 strcat (tmp, t); |
|
129 return tmp; |
1
|
130 } |
|
131 |
581
|
132 // Throw away input until a given character is read. |
|
133 |
1
|
134 void |
|
135 discard_until (istream& stream, char character) |
|
136 { |
|
137 int c; |
|
138 for (;;) |
|
139 { |
|
140 stream >> c; |
|
141 if (c == EOF || c == character) |
|
142 break; |
|
143 } |
|
144 if (c != EOF) |
|
145 stream.putback ((char) c); |
|
146 } |
|
147 |
607
|
148 #if 0 |
|
149 |
|
150 // XXX UNTESTED XXX |
|
151 |
|
152 // Read input until a given character is read. Returns characters |
|
153 // read in a new string that must be freed by the caller. |
|
154 |
|
155 char * |
|
156 read_until (istream& stream, char character) |
|
157 { |
|
158 int grow_size = 8; |
|
159 int limit = grow_size; |
|
160 char *buf = new char [limit]; |
|
161 char *bp = buf; |
|
162 |
|
163 get_more: |
|
164 is.getline (bp, limit, character); |
|
165 |
|
166 if (is.gcount () == 0) |
|
167 { |
|
168 delete [] buf; |
|
169 return 0; |
|
170 } |
|
171 |
|
172 if (is.gcount () == limit && buf[limit-1] != '\0') |
|
173 { |
|
174 char *tmp = new char [limit + grow_size]; |
|
175 strcpy (tmp, buf); |
|
176 delete [] buf; |
|
177 buf = tmp; |
|
178 bp = tmp + limit - 1; |
|
179 limit += grow_size; |
|
180 grow_size *= 2; |
|
181 goto get_more; |
|
182 } |
|
183 |
|
184 return buf; |
|
185 } |
|
186 #endif |
|
187 |
661
|
188 // Get a temporary file name. |
643
|
189 |
|
190 char * |
|
191 octave_tmp_file_name (void) |
|
192 { |
662
|
193 static char *retval = 0; |
|
194 |
|
195 if (retval) |
|
196 free (retval); |
|
197 |
|
198 retval = tempnam (0, "oct-"); |
|
199 |
661
|
200 if (! retval) |
|
201 error ("can't open temporary file!"); |
662
|
202 |
661
|
203 return retval; |
643
|
204 } |
|
205 |
801
|
206 DEFUN ("octave_tmp_file_name", Foctave_tmp_file_name, |
|
207 Soctave_tmp_file_name, 0, 1, |
|
208 "octave_tmp_file_name ()") |
|
209 { |
|
210 tree_constant retval; |
|
211 |
|
212 if (args.length () == 0) |
|
213 retval = octave_tmp_file_name (); |
|
214 else |
|
215 print_usage ("octave_tmp_file_name"); |
|
216 |
|
217 return retval; |
|
218 } |
|
219 |
1
|
220 char ** |
|
221 pathstring_to_vector (char *pathstring) |
|
222 { |
526
|
223 static char **path = 0; |
1
|
224 |
526
|
225 if (pathstring) |
1
|
226 { |
|
227 int nelem = 0; |
240
|
228 char *tmp_path = strsave (pathstring); |
|
229 if (*tmp_path != '\0') |
1
|
230 { |
|
231 nelem++; |
240
|
232 char *ptr = tmp_path; |
1
|
233 while (*ptr != '\0') |
|
234 { |
1104
|
235 if (*ptr == SEPCHAR) |
1
|
236 nelem++; |
|
237 ptr++; |
|
238 } |
|
239 } |
|
240 |
240
|
241 char **foo = path; |
|
242 while (foo && *foo) |
244
|
243 delete [] *foo++; |
1
|
244 delete [] path; |
244
|
245 |
1
|
246 path = new char * [nelem+1]; |
526
|
247 path[nelem] = 0; |
1
|
248 |
|
249 int i = 0; |
240
|
250 char *ptr = tmp_path; |
1
|
251 while (i < nelem) |
|
252 { |
1104
|
253 char *end = strchr (ptr, SEPCHAR); |
526
|
254 if (end) |
1
|
255 *end = '\0'; |
|
256 char *result = tilde_expand (ptr); |
|
257 path[i] = strsave (result); |
|
258 free (result); |
|
259 ptr = end + 1; |
|
260 i++; |
|
261 } |
|
262 |
240
|
263 delete [] tmp_path; |
1
|
264 } |
|
265 |
|
266 return path; |
|
267 } |
|
268 |
581
|
269 // Return to the main command loop in octave.cc. |
|
270 |
189
|
271 void |
1
|
272 jump_to_top_level (void) |
|
273 { |
|
274 run_all_unwind_protects (); |
|
275 |
|
276 longjmp (toplevel, 1); |
|
277 } |
|
278 |
|
279 int |
526
|
280 almost_match (const char *std, const char *s, int min_match_len, |
|
281 int case_sens) |
1
|
282 { |
|
283 int stdlen = strlen (std); |
|
284 int slen = strlen (s); |
|
285 |
|
286 return (slen <= stdlen |
|
287 && slen >= min_match_len |
287
|
288 && (case_sens |
|
289 ? (strncmp (std, s, slen) == 0) |
|
290 : (strncasecmp (std, s, slen) == 0))); |
|
291 } |
|
292 |
581
|
293 // Ugh. |
|
294 |
287
|
295 int |
|
296 keyword_almost_match (const char **std, int *min_len, const char *s, |
|
297 int min_toks_to_match, int max_toks) |
|
298 { |
|
299 int status = 0; |
|
300 int tok_count = 0; |
|
301 int toks_matched = 0; |
|
302 |
526
|
303 if (! s || *s == '\0' || max_toks < 1) |
287
|
304 return status; |
|
305 |
|
306 char *kw = strsave (s); |
|
307 |
|
308 char *t = kw; |
|
309 while (*t != '\0') |
|
310 { |
|
311 if (*t == '\t') |
|
312 *t = ' '; |
|
313 t++; |
|
314 } |
|
315 |
|
316 char *beg = kw; |
|
317 while (*beg == ' ') |
|
318 beg++; |
|
319 |
|
320 if (*beg == '\0') |
|
321 return status; |
|
322 |
|
323 |
|
324 char **to_match = new char * [max_toks + 1]; |
526
|
325 const char **s1 = std; |
287
|
326 char **s2 = to_match; |
|
327 |
526
|
328 if (! s1 || ! s2) |
287
|
329 goto done; |
|
330 |
|
331 s2[tok_count] = beg; |
|
332 char *end; |
526
|
333 while ((end = strchr (beg, ' ')) != 0) |
287
|
334 { |
|
335 *end = '\0'; |
|
336 beg = end + 1; |
|
337 |
|
338 while (*beg == ' ') |
|
339 beg++; |
|
340 |
|
341 if (*beg == '\0') |
|
342 break; |
|
343 |
|
344 tok_count++; |
|
345 if (tok_count >= max_toks) |
|
346 goto done; |
|
347 |
|
348 s2[tok_count] = beg; |
|
349 } |
526
|
350 s2[tok_count+1] = 0; |
287
|
351 |
|
352 s2 = to_match; |
|
353 |
|
354 for (;;) |
|
355 { |
|
356 if (! almost_match (*s1, *s2, min_len[toks_matched], 0)) |
|
357 goto done; |
|
358 |
|
359 toks_matched++; |
|
360 |
|
361 s1++; |
|
362 s2++; |
|
363 |
|
364 if (! *s2) |
|
365 { |
|
366 status = (toks_matched >= min_toks_to_match); |
|
367 goto done; |
|
368 } |
|
369 |
|
370 if (! *s1) |
|
371 goto done; |
|
372 } |
|
373 |
|
374 done: |
|
375 |
|
376 delete [] kw; |
|
377 delete [] to_match; |
|
378 |
|
379 return status; |
1
|
380 } |
|
381 |
|
382 char ** |
338
|
383 get_fcn_file_names (int& num, const char *dir, int no_suffix) |
1
|
384 { |
|
385 static int num_max = 256; |
|
386 char **retval = new char * [num_max]; |
|
387 int i = 0; |
|
388 |
|
389 DIR *dirp = opendir (dir); |
526
|
390 if (dirp) |
1
|
391 { |
|
392 struct dirent *entry; |
526
|
393 while ((entry = readdir (dirp)) != 0) |
1
|
394 { |
|
395 int len = NLENGTH (entry); |
826
|
396 #ifdef WITH_DLD |
|
397 if ((len > 2 |
|
398 && entry->d_name[len-2] == '.' |
|
399 && entry->d_name[len-1] == 'm') |
|
400 || (len > 4 |
|
401 && entry->d_name[len-4] == '.' |
|
402 && entry->d_name[len-3] == 'o' |
|
403 && entry->d_name[len-2] == 'c' |
|
404 && entry->d_name[len-1] == 't')) |
|
405 #else |
1
|
406 if (len > 2 |
|
407 && entry->d_name[len-2] == '.' |
|
408 && entry->d_name[len-1] == 'm') |
826
|
409 #endif |
1
|
410 { |
|
411 retval[i] = strsave (entry->d_name); |
|
412 if (no_suffix) |
826
|
413 { |
|
414 if (retval[i][len-1] == 'm') |
|
415 retval[i][len-2] = '\0'; |
|
416 else |
|
417 retval[i][len-4] = '\0'; |
|
418 } |
1
|
419 |
|
420 i++; |
|
421 |
|
422 if (i == num_max - 1) |
|
423 { |
244
|
424 // Reallocate the array. Only copy pointers, not the strings they |
|
425 // point to, then only delete the original array of pointers, and not |
|
426 // the strings they point to. |
|
427 |
1
|
428 num_max += 256; |
|
429 char **tmp = new char * [num_max]; |
|
430 for (int j = 0; j < i; j++) |
|
431 tmp[j] = retval[j]; |
|
432 |
244
|
433 delete [] retval; |
|
434 |
1
|
435 retval = tmp; |
|
436 } |
|
437 } |
|
438 } |
106
|
439 closedir (dirp); |
1
|
440 } |
|
441 |
526
|
442 retval[i] = 0; |
1
|
443 num = i; |
|
444 |
|
445 return retval; |
|
446 } |
|
447 |
|
448 char ** |
338
|
449 get_fcn_file_names (int& num, int no_suffix) |
1
|
450 { |
|
451 static int num_max = 1024; |
|
452 char **retval = new char * [num_max]; |
|
453 int i = 0; |
|
454 |
679
|
455 char *path_elt = kpse_path_element (user_pref.loadpath); |
1
|
456 |
679
|
457 while (path_elt) |
1
|
458 { |
679
|
459 str_llist_type *elt_dirs = kpse_element_dirs (path_elt); |
|
460 |
|
461 str_llist_elt_type *dir; |
|
462 for (dir = *elt_dirs; dir; dir = STR_LLIST_NEXT (*dir)) |
1
|
463 { |
679
|
464 char *elt_dir = STR_LLIST (*dir); |
1
|
465 |
679
|
466 if (elt_dir) |
1
|
467 { |
679
|
468 int tmp_num; |
|
469 char **names = get_fcn_file_names (tmp_num, elt_dir, no_suffix); |
|
470 |
|
471 if (i + tmp_num >= num_max - 1) |
|
472 { |
244
|
473 // Reallocate the array. Only copy pointers, not the strings they |
|
474 // point to, then only delete the original array of pointers, and not |
|
475 // the strings they point to. |
|
476 |
679
|
477 num_max += 1024; |
|
478 char **tmp = new char * [num_max]; |
|
479 for (int j = 0; j < i; j++) |
|
480 tmp[j] = retval[j]; |
1
|
481 |
679
|
482 delete [] retval; |
244
|
483 |
679
|
484 retval = tmp; |
|
485 } |
1
|
486 |
679
|
487 int k = 0; |
|
488 while (k < tmp_num) |
|
489 retval[i++] = names[k++]; |
|
490 } |
|
491 } |
1
|
492 |
679
|
493 path_elt = kpse_path_element (0); |
1
|
494 } |
|
495 |
526
|
496 retval[i] = 0; |
1
|
497 num = i; |
|
498 |
|
499 return retval; |
|
500 } |
|
501 |
1086
|
502 // Convert X to the nearest integer value. Should not pass NaN to |
|
503 // this function. |
|
504 |
1
|
505 int |
|
506 NINT (double x) |
|
507 { |
|
508 if (x > INT_MAX) |
|
509 return INT_MAX; |
|
510 else if (x < INT_MIN) |
|
511 return INT_MIN; |
|
512 else |
|
513 return (x > 0) ? ((int) (x + 0.5)) : ((int) (x - 0.5)); |
|
514 } |
|
515 |
|
516 double |
|
517 D_NINT (double x) |
|
518 { |
|
519 if (xisinf (x) || xisnan (x)) |
|
520 return x; |
|
521 else |
|
522 return floor (x + 0.5); |
|
523 } |
|
524 |
526
|
525 // XXX FIXME XXX -- put these in some file, and make them extern. |
|
526 |
|
527 static int |
|
528 all_strings (const Octave_object& args) |
|
529 { |
|
530 int n = args.length (); |
712
|
531 for (int i = 0; i < n; i++) |
610
|
532 if (! args(i).is_string ()) |
526
|
533 return 0; |
|
534 return 1; |
|
535 } |
|
536 |
|
537 char ** |
578
|
538 make_argv (const Octave_object& args, const char *fcn_name) |
526
|
539 { |
|
540 char **argv = 0; |
|
541 if (all_strings (args)) |
|
542 { |
|
543 int n = args.length (); |
|
544 argv = new char * [n + 1]; |
578
|
545 argv[0] = strsave (fcn_name); |
712
|
546 for (int i = 0; i < n; i++) |
|
547 argv[i+1] = strsave (args(i).string_value ()); |
526
|
548 } |
|
549 else |
578
|
550 error ("%s: expecting all arguments to be strings", fcn_name); |
526
|
551 |
|
552 return argv; |
|
553 } |
|
554 |
719
|
555 // Return non-zero if either NR or NC is zero. Return -1 if this |
|
556 // should be considered fatal; return 1 if this is ok. |
|
557 |
628
|
558 int |
|
559 empty_arg (const char *name, int nr, int nc) |
|
560 { |
|
561 int is_empty = 0; |
|
562 |
|
563 if (nr == 0 || nc == 0) |
|
564 { |
|
565 int flag = user_pref.propagate_empty_matrices; |
|
566 |
|
567 if (flag < 0) |
636
|
568 { |
|
569 gripe_empty_arg (name, 0); |
|
570 is_empty = 1; |
|
571 } |
673
|
572 else if (flag == 0) |
636
|
573 { |
|
574 gripe_empty_arg (name, 1); |
|
575 is_empty = -1; |
|
576 } |
719
|
577 else |
|
578 is_empty = 1; |
628
|
579 } |
|
580 |
|
581 return is_empty; |
|
582 } |
|
583 |
581
|
584 // Format a list in neat columns. Mostly stolen from GNU ls. This |
|
585 // should maybe be in utils.cc. |
|
586 |
526
|
587 ostrstream& |
|
588 list_in_columns (ostrstream& os, char **list) |
|
589 { |
|
590 // Compute the maximum name length. |
|
591 |
|
592 int max_name_length = 0; |
|
593 int total_names = 0; |
1321
|
594 char **names = 0; |
|
595 for (names = list; *names; names++) |
526
|
596 { |
|
597 total_names++; |
|
598 int name_length = strlen (*names); |
|
599 if (name_length > max_name_length) |
|
600 max_name_length = name_length; |
|
601 } |
|
602 |
|
603 // Allow at least two spaces between names. |
|
604 |
|
605 max_name_length += 2; |
|
606 |
|
607 // Calculate the maximum number of columns that will fit. |
|
608 |
|
609 int line_length = terminal_columns (); |
|
610 int cols = line_length / max_name_length; |
|
611 if (cols == 0) |
|
612 cols = 1; |
|
613 |
|
614 // Calculate the number of rows that will be in each column except |
|
615 // possibly for a short column on the right. |
|
616 |
|
617 int rows = total_names / cols + (total_names % cols != 0); |
|
618 |
|
619 // Recalculate columns based on rows. |
|
620 |
|
621 cols = total_names / rows + (total_names % rows != 0); |
|
622 |
|
623 names = list; |
|
624 int count; |
|
625 for (int row = 0; row < rows; row++) |
|
626 { |
|
627 count = row; |
|
628 int pos = 0; |
|
629 |
|
630 // Print the next row. |
|
631 |
|
632 while (1) |
|
633 { |
|
634 os << *(names + count); |
|
635 int name_length = strlen (*(names + count)); |
|
636 |
|
637 count += rows; |
|
638 if (count >= total_names) |
|
639 break; |
|
640 |
|
641 int spaces_to_pad = max_name_length - name_length; |
|
642 for (int i = 0; i < spaces_to_pad; i++) |
|
643 os << " "; |
|
644 pos += max_name_length; |
|
645 } |
|
646 os << "\n"; |
|
647 } |
|
648 |
|
649 return os; |
|
650 } |
|
651 |
581
|
652 // See if the given file is in the path. |
|
653 |
526
|
654 char * |
686
|
655 search_path_for_file (const char *path, const char *name) |
|
656 { |
|
657 char *retval = 0; |
|
658 |
|
659 char *tmp = kpse_path_search (path, name, kpathsea_true); |
|
660 |
|
661 if (tmp) |
|
662 { |
|
663 retval = make_absolute (tmp, the_current_working_directory); |
|
664 free (tmp); |
|
665 } |
|
666 |
|
667 return retval; |
|
668 } |
|
669 |
912
|
670 DEFUN ("file_in_path", Ffile_in_path, Sfile_in_path, 3, 1, |
686
|
671 "file_in_path (PATH, NAME)") |
|
672 { |
|
673 Octave_object retval; |
|
674 |
|
675 DEFINE_ARGV("file_in_path"); |
|
676 |
|
677 if (argc == 3) |
|
678 { |
|
679 char *fname = search_path_for_file (argv[1], argv[2]); |
|
680 |
|
681 if (fname) |
|
682 retval = fname; |
|
683 else |
|
684 retval = Matrix (); |
|
685 } |
|
686 else |
|
687 print_usage ("file_in_path"); |
|
688 |
|
689 DELETE_ARGV; |
|
690 |
|
691 return retval; |
|
692 } |
|
693 |
|
694 |
|
695 char * |
526
|
696 file_in_path (const char *name, const char *suffix) |
|
697 { |
|
698 char *retval = 0; |
|
699 |
1266
|
700 char *nm = 0; |
686
|
701 |
526
|
702 if (suffix) |
1266
|
703 nm = strconcat (name, suffix); |
|
704 else |
|
705 nm = strsave (name); |
526
|
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; |
1215
|
806 while ((t = undo_string_escape (*s++))) |
801
|
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 |
1023
|
828 print_usage ("undo_string_escapes"); |
801
|
829 |
|
830 return retval; |
|
831 } |
|
832 |
572
|
833 /* |
1
|
834 ;;; Local Variables: *** |
|
835 ;;; mode: C++ *** |
|
836 ;;; page-delimiter: "^/\\*" *** |
|
837 ;;; End: *** |
|
838 */ |