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