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