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