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" |
1
|
76 #include "procstream.h" |
1352
|
77 #include "sysdir.h" |
1
|
78 #include "tree-const.h" |
|
79 #include "unwind-prot.h" |
1352
|
80 #include "user-prefs.h" |
|
81 #include "utils.h" |
|
82 #include "variables.h" |
1
|
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 { |
1358
|
405 // Reallocate the array. Only copy pointers, not |
|
406 // the strings they point to, then only delete the |
|
407 // original array of pointers, and not the strings |
|
408 // they point to. |
244
|
409 |
1
|
410 num_max += 256; |
|
411 char **tmp = new char * [num_max]; |
|
412 for (int j = 0; j < i; j++) |
|
413 tmp[j] = retval[j]; |
|
414 |
244
|
415 delete [] retval; |
|
416 |
1
|
417 retval = tmp; |
|
418 } |
|
419 } |
|
420 } |
106
|
421 closedir (dirp); |
1
|
422 } |
|
423 |
526
|
424 retval[i] = 0; |
1
|
425 num = i; |
|
426 |
|
427 return retval; |
|
428 } |
|
429 |
|
430 char ** |
338
|
431 get_fcn_file_names (int& num, int no_suffix) |
1
|
432 { |
|
433 static int num_max = 1024; |
|
434 char **retval = new char * [num_max]; |
|
435 int i = 0; |
|
436 |
679
|
437 char *path_elt = kpse_path_element (user_pref.loadpath); |
1
|
438 |
679
|
439 while (path_elt) |
1
|
440 { |
679
|
441 str_llist_type *elt_dirs = kpse_element_dirs (path_elt); |
|
442 |
|
443 str_llist_elt_type *dir; |
|
444 for (dir = *elt_dirs; dir; dir = STR_LLIST_NEXT (*dir)) |
1
|
445 { |
679
|
446 char *elt_dir = STR_LLIST (*dir); |
1
|
447 |
679
|
448 if (elt_dir) |
1
|
449 { |
679
|
450 int tmp_num; |
|
451 char **names = get_fcn_file_names (tmp_num, elt_dir, no_suffix); |
|
452 |
|
453 if (i + tmp_num >= num_max - 1) |
|
454 { |
1358
|
455 // Reallocate the array. Only copy pointers, not |
|
456 // the strings they point to, then only delete the |
|
457 // original array of pointers, and not the strings |
|
458 // they point to. |
244
|
459 |
679
|
460 num_max += 1024; |
|
461 char **tmp = new char * [num_max]; |
|
462 for (int j = 0; j < i; j++) |
|
463 tmp[j] = retval[j]; |
1
|
464 |
679
|
465 delete [] retval; |
244
|
466 |
679
|
467 retval = tmp; |
|
468 } |
1
|
469 |
679
|
470 int k = 0; |
|
471 while (k < tmp_num) |
|
472 retval[i++] = names[k++]; |
|
473 } |
|
474 } |
1
|
475 |
679
|
476 path_elt = kpse_path_element (0); |
1
|
477 } |
|
478 |
526
|
479 retval[i] = 0; |
1
|
480 num = i; |
|
481 |
|
482 return retval; |
|
483 } |
|
484 |
1086
|
485 // Convert X to the nearest integer value. Should not pass NaN to |
|
486 // this function. |
|
487 |
1
|
488 int |
|
489 NINT (double x) |
|
490 { |
|
491 if (x > INT_MAX) |
|
492 return INT_MAX; |
|
493 else if (x < INT_MIN) |
|
494 return INT_MIN; |
|
495 else |
|
496 return (x > 0) ? ((int) (x + 0.5)) : ((int) (x - 0.5)); |
|
497 } |
|
498 |
|
499 double |
|
500 D_NINT (double x) |
|
501 { |
|
502 if (xisinf (x) || xisnan (x)) |
|
503 return x; |
|
504 else |
|
505 return floor (x + 0.5); |
|
506 } |
|
507 |
526
|
508 // XXX FIXME XXX -- put these in some file, and make them extern. |
|
509 |
|
510 static int |
|
511 all_strings (const Octave_object& args) |
|
512 { |
|
513 int n = args.length (); |
712
|
514 for (int i = 0; i < n; i++) |
610
|
515 if (! args(i).is_string ()) |
526
|
516 return 0; |
|
517 return 1; |
|
518 } |
|
519 |
|
520 char ** |
578
|
521 make_argv (const Octave_object& args, const char *fcn_name) |
526
|
522 { |
|
523 char **argv = 0; |
|
524 if (all_strings (args)) |
|
525 { |
|
526 int n = args.length (); |
|
527 argv = new char * [n + 1]; |
578
|
528 argv[0] = strsave (fcn_name); |
712
|
529 for (int i = 0; i < n; i++) |
|
530 argv[i+1] = strsave (args(i).string_value ()); |
526
|
531 } |
|
532 else |
578
|
533 error ("%s: expecting all arguments to be strings", fcn_name); |
526
|
534 |
|
535 return argv; |
|
536 } |
|
537 |
719
|
538 // Return non-zero if either NR or NC is zero. Return -1 if this |
|
539 // should be considered fatal; return 1 if this is ok. |
|
540 |
628
|
541 int |
|
542 empty_arg (const char *name, int nr, int nc) |
|
543 { |
|
544 int is_empty = 0; |
|
545 |
|
546 if (nr == 0 || nc == 0) |
|
547 { |
|
548 int flag = user_pref.propagate_empty_matrices; |
|
549 |
|
550 if (flag < 0) |
636
|
551 { |
|
552 gripe_empty_arg (name, 0); |
|
553 is_empty = 1; |
|
554 } |
673
|
555 else if (flag == 0) |
636
|
556 { |
|
557 gripe_empty_arg (name, 1); |
|
558 is_empty = -1; |
|
559 } |
719
|
560 else |
|
561 is_empty = 1; |
628
|
562 } |
|
563 |
|
564 return is_empty; |
|
565 } |
|
566 |
581
|
567 // Format a list in neat columns. Mostly stolen from GNU ls. This |
|
568 // should maybe be in utils.cc. |
|
569 |
526
|
570 ostrstream& |
|
571 list_in_columns (ostrstream& os, char **list) |
|
572 { |
1358
|
573 // Compute the maximum name length. |
526
|
574 |
|
575 int max_name_length = 0; |
|
576 int total_names = 0; |
1321
|
577 char **names = 0; |
|
578 for (names = list; *names; names++) |
526
|
579 { |
|
580 total_names++; |
|
581 int name_length = strlen (*names); |
|
582 if (name_length > max_name_length) |
|
583 max_name_length = name_length; |
|
584 } |
|
585 |
1358
|
586 // Allow at least two spaces between names. |
526
|
587 |
|
588 max_name_length += 2; |
|
589 |
1358
|
590 // Calculate the maximum number of columns that will fit. |
526
|
591 |
|
592 int line_length = terminal_columns (); |
|
593 int cols = line_length / max_name_length; |
|
594 if (cols == 0) |
|
595 cols = 1; |
|
596 |
1358
|
597 // Calculate the number of rows that will be in each column except |
|
598 // possibly for a short column on the right. |
526
|
599 |
|
600 int rows = total_names / cols + (total_names % cols != 0); |
|
601 |
1358
|
602 // Recalculate columns based on rows. |
526
|
603 |
|
604 cols = total_names / rows + (total_names % rows != 0); |
|
605 |
|
606 names = list; |
|
607 int count; |
|
608 for (int row = 0; row < rows; row++) |
|
609 { |
|
610 count = row; |
|
611 int pos = 0; |
|
612 |
1358
|
613 // Print the next row. |
526
|
614 |
|
615 while (1) |
|
616 { |
|
617 os << *(names + count); |
|
618 int name_length = strlen (*(names + count)); |
|
619 |
|
620 count += rows; |
|
621 if (count >= total_names) |
|
622 break; |
|
623 |
|
624 int spaces_to_pad = max_name_length - name_length; |
|
625 for (int i = 0; i < spaces_to_pad; i++) |
|
626 os << " "; |
|
627 pos += max_name_length; |
|
628 } |
|
629 os << "\n"; |
|
630 } |
|
631 |
|
632 return os; |
|
633 } |
|
634 |
581
|
635 // See if the given file is in the path. |
|
636 |
526
|
637 char * |
686
|
638 search_path_for_file (const char *path, const char *name) |
|
639 { |
|
640 char *retval = 0; |
|
641 |
|
642 char *tmp = kpse_path_search (path, name, kpathsea_true); |
|
643 |
|
644 if (tmp) |
|
645 { |
|
646 retval = make_absolute (tmp, the_current_working_directory); |
|
647 free (tmp); |
|
648 } |
|
649 |
|
650 return retval; |
|
651 } |
|
652 |
912
|
653 DEFUN ("file_in_path", Ffile_in_path, Sfile_in_path, 3, 1, |
686
|
654 "file_in_path (PATH, NAME)") |
|
655 { |
|
656 Octave_object retval; |
|
657 |
|
658 DEFINE_ARGV("file_in_path"); |
|
659 |
|
660 if (argc == 3) |
|
661 { |
|
662 char *fname = search_path_for_file (argv[1], argv[2]); |
|
663 |
|
664 if (fname) |
|
665 retval = fname; |
|
666 else |
|
667 retval = Matrix (); |
|
668 } |
|
669 else |
|
670 print_usage ("file_in_path"); |
|
671 |
|
672 DELETE_ARGV; |
|
673 |
|
674 return retval; |
|
675 } |
|
676 |
|
677 |
|
678 char * |
526
|
679 file_in_path (const char *name, const char *suffix) |
|
680 { |
|
681 char *retval = 0; |
|
682 |
1266
|
683 char *nm = 0; |
686
|
684 |
526
|
685 if (suffix) |
1266
|
686 nm = strconcat (name, suffix); |
|
687 else |
|
688 nm = strsave (name); |
526
|
689 |
|
690 if (! the_current_working_directory) |
|
691 get_working_directory ("file_in_path"); |
|
692 |
686
|
693 retval = search_path_for_file (user_pref.loadpath, nm); |
526
|
694 |
686
|
695 delete [] nm; |
526
|
696 |
|
697 return retval; |
|
698 } |
|
699 |
581
|
700 // See if there is an function file in the path. If so, return the |
|
701 // full path to the file. |
|
702 |
526
|
703 char * |
|
704 fcn_file_in_path (const char *name) |
|
705 { |
908
|
706 if (name) |
|
707 { |
|
708 int len = strlen (name); |
|
709 |
|
710 if (name [len - 2] == '.' && name [len - 1] == 'm') |
|
711 return file_in_path (name, ""); |
|
712 else |
|
713 return file_in_path (name, ".m"); |
|
714 } |
|
715 else |
|
716 return 0; |
526
|
717 } |
|
718 |
581
|
719 // See if there is an octave file in the path. If so, return the |
|
720 // full path to the file. |
|
721 |
572
|
722 char * |
|
723 oct_file_in_path (const char *name) |
|
724 { |
908
|
725 if (name) |
|
726 { |
|
727 int len = strlen (name); |
|
728 |
|
729 if (name [len - 4] == '.' && name [len - 3] == 'o' |
|
730 && name [len - 2] == 'c' && name [len - 1] == 't') |
|
731 return file_in_path (name, ""); |
|
732 else |
|
733 return file_in_path (name, ".oct"); |
|
734 } |
|
735 else |
|
736 return 0; |
572
|
737 } |
|
738 |
801
|
739 char * |
|
740 undo_string_escape (char c) |
|
741 { |
|
742 static char retval[2]; |
|
743 retval[1] = '\0'; |
|
744 |
|
745 if (! c) |
|
746 return 0; |
|
747 |
|
748 switch (c) |
|
749 { |
|
750 case '\a': |
|
751 return "\\a"; |
|
752 |
|
753 case '\b': // backspace |
|
754 return "\\b"; |
|
755 |
|
756 case '\f': // formfeed |
|
757 return "\\f"; |
|
758 |
|
759 case '\n': // newline |
|
760 return "\\n"; |
|
761 |
|
762 case '\r': // carriage return |
|
763 return "\\r"; |
|
764 |
|
765 case '\t': // horizontal tab |
|
766 return "\\t"; |
|
767 |
|
768 case '\v': // vertical tab |
|
769 return "\\v"; |
|
770 |
|
771 case '\\': // backslash |
|
772 return "\\\\"; |
|
773 |
|
774 case '"': // double quote |
|
775 return "\\\""; |
|
776 |
|
777 default: |
|
778 retval[0] = c; |
|
779 return retval; |
|
780 } |
|
781 } |
|
782 |
|
783 char * |
|
784 undo_string_escapes (char *s) |
|
785 { |
|
786 ostrstream buf; |
|
787 |
|
788 char *t; |
1215
|
789 while ((t = undo_string_escape (*s++))) |
801
|
790 buf << t; |
|
791 buf << ends; |
|
792 |
|
793 return buf.str (); |
|
794 } |
|
795 |
|
796 DEFUN ("undo_string_escapes", Fundo_string_escapes, |
|
797 Sundo_string_escapes, 1, 1, |
|
798 "undo_string_escapes (STRING)") |
|
799 { |
|
800 tree_constant retval; |
|
801 |
|
802 int nargin = args.length (); |
|
803 |
|
804 if (nargin == 1 && args(0).is_string ()) |
|
805 { |
|
806 char *str = undo_string_escapes (args(0).string_value ()); |
|
807 retval = str; |
|
808 delete [] str; |
|
809 } |
|
810 else |
1023
|
811 print_usage ("undo_string_escapes"); |
801
|
812 |
|
813 return retval; |
|
814 } |
|
815 |
572
|
816 /* |
1
|
817 ;;; Local Variables: *** |
|
818 ;;; mode: C++ *** |
|
819 ;;; page-delimiter: "^/\\*" *** |
|
820 ;;; End: *** |
|
821 */ |