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 |
1728
|
32 #include <string> |
|
33 |
1349
|
34 #include <fstream.h> |
|
35 #include <iostream.h> |
|
36 #include <strstream.h> |
|
37 |
1350
|
38 #ifdef HAVE_UNISTD_H |
1
|
39 #include <sys/types.h> |
|
40 #include <unistd.h> |
|
41 #endif |
367
|
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 |
1465
|
53 #ifndef HAVE_STRNCASECMP |
|
54 extern "C" int strncasecmp (const char*, const char*, size_t); |
|
55 #endif |
1
|
56 |
|
57 #include "SLStack.h" |
|
58 |
1651
|
59 #include "oct-cmplx.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" |
1742
|
68 #include "oct-hist.h" |
1750
|
69 #include "oct-obj.h" |
1352
|
70 #include "pager.h" |
1155
|
71 #include "pathsearch.h" |
1690
|
72 #include "sysdep.h" |
1352
|
73 #include "sysdir.h" |
1750
|
74 #include "toplev.h" |
1
|
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, |
1488
|
184 Soctave_tmp_file_name, 10, |
801
|
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'; |
1750
|
233 string result = oct_tilde_expand (ptr); |
|
234 path[i] = strsave (result.c_str ()); |
1
|
235 ptr = end + 1; |
|
236 i++; |
|
237 } |
|
238 |
240
|
239 delete [] tmp_path; |
1
|
240 } |
|
241 |
|
242 return path; |
|
243 } |
|
244 |
581
|
245 // Return to the main command loop in octave.cc. |
|
246 |
1618
|
247 extern "C" void |
1
|
248 jump_to_top_level (void) |
|
249 { |
|
250 run_all_unwind_protects (); |
|
251 |
|
252 longjmp (toplevel, 1); |
|
253 } |
|
254 |
|
255 int |
526
|
256 almost_match (const char *std, const char *s, int min_match_len, |
|
257 int case_sens) |
1
|
258 { |
|
259 int stdlen = strlen (std); |
|
260 int slen = strlen (s); |
|
261 |
|
262 return (slen <= stdlen |
|
263 && slen >= min_match_len |
287
|
264 && (case_sens |
|
265 ? (strncmp (std, s, slen) == 0) |
|
266 : (strncasecmp (std, s, slen) == 0))); |
|
267 } |
|
268 |
581
|
269 // Ugh. |
|
270 |
287
|
271 int |
|
272 keyword_almost_match (const char **std, int *min_len, const char *s, |
|
273 int min_toks_to_match, int max_toks) |
|
274 { |
|
275 int status = 0; |
|
276 int tok_count = 0; |
|
277 int toks_matched = 0; |
|
278 |
526
|
279 if (! s || *s == '\0' || max_toks < 1) |
287
|
280 return status; |
|
281 |
|
282 char *kw = strsave (s); |
|
283 |
|
284 char *t = kw; |
|
285 while (*t != '\0') |
|
286 { |
|
287 if (*t == '\t') |
|
288 *t = ' '; |
|
289 t++; |
|
290 } |
|
291 |
|
292 char *beg = kw; |
|
293 while (*beg == ' ') |
|
294 beg++; |
|
295 |
|
296 if (*beg == '\0') |
|
297 return status; |
|
298 |
|
299 |
|
300 char **to_match = new char * [max_toks + 1]; |
526
|
301 const char **s1 = std; |
287
|
302 char **s2 = to_match; |
|
303 |
526
|
304 if (! s1 || ! s2) |
287
|
305 goto done; |
|
306 |
|
307 s2[tok_count] = beg; |
|
308 char *end; |
526
|
309 while ((end = strchr (beg, ' ')) != 0) |
287
|
310 { |
|
311 *end = '\0'; |
|
312 beg = end + 1; |
|
313 |
|
314 while (*beg == ' ') |
|
315 beg++; |
|
316 |
|
317 if (*beg == '\0') |
|
318 break; |
|
319 |
|
320 tok_count++; |
|
321 if (tok_count >= max_toks) |
|
322 goto done; |
|
323 |
|
324 s2[tok_count] = beg; |
|
325 } |
526
|
326 s2[tok_count+1] = 0; |
287
|
327 |
|
328 s2 = to_match; |
|
329 |
|
330 for (;;) |
|
331 { |
|
332 if (! almost_match (*s1, *s2, min_len[toks_matched], 0)) |
|
333 goto done; |
|
334 |
|
335 toks_matched++; |
|
336 |
|
337 s1++; |
|
338 s2++; |
|
339 |
|
340 if (! *s2) |
|
341 { |
|
342 status = (toks_matched >= min_toks_to_match); |
|
343 goto done; |
|
344 } |
|
345 |
|
346 if (! *s1) |
|
347 goto done; |
|
348 } |
|
349 |
|
350 done: |
|
351 |
|
352 delete [] kw; |
|
353 delete [] to_match; |
|
354 |
|
355 return status; |
1
|
356 } |
|
357 |
|
358 char ** |
338
|
359 get_fcn_file_names (int& num, const char *dir, int no_suffix) |
1
|
360 { |
|
361 static int num_max = 256; |
|
362 char **retval = new char * [num_max]; |
|
363 int i = 0; |
|
364 |
|
365 DIR *dirp = opendir (dir); |
526
|
366 if (dirp) |
1
|
367 { |
|
368 struct dirent *entry; |
526
|
369 while ((entry = readdir (dirp)) != 0) |
1
|
370 { |
|
371 int len = NLENGTH (entry); |
1664
|
372 #if defined (WITH_DYNAMIC_LINKING) |
826
|
373 if ((len > 2 |
|
374 && entry->d_name[len-2] == '.' |
|
375 && entry->d_name[len-1] == 'm') |
|
376 || (len > 4 |
|
377 && entry->d_name[len-4] == '.' |
|
378 && entry->d_name[len-3] == 'o' |
|
379 && entry->d_name[len-2] == 'c' |
|
380 && entry->d_name[len-1] == 't')) |
|
381 #else |
1
|
382 if (len > 2 |
|
383 && entry->d_name[len-2] == '.' |
|
384 && entry->d_name[len-1] == 'm') |
826
|
385 #endif |
1
|
386 { |
|
387 retval[i] = strsave (entry->d_name); |
|
388 if (no_suffix) |
826
|
389 { |
|
390 if (retval[i][len-1] == 'm') |
|
391 retval[i][len-2] = '\0'; |
|
392 else |
|
393 retval[i][len-4] = '\0'; |
|
394 } |
1
|
395 |
|
396 i++; |
|
397 |
|
398 if (i == num_max - 1) |
|
399 { |
1358
|
400 // Reallocate the array. Only copy pointers, not |
|
401 // the strings they point to, then only delete the |
|
402 // original array of pointers, and not the strings |
|
403 // they point to. |
244
|
404 |
1
|
405 num_max += 256; |
|
406 char **tmp = new char * [num_max]; |
|
407 for (int j = 0; j < i; j++) |
|
408 tmp[j] = retval[j]; |
|
409 |
244
|
410 delete [] retval; |
|
411 |
1
|
412 retval = tmp; |
|
413 } |
|
414 } |
|
415 } |
106
|
416 closedir (dirp); |
1
|
417 } |
|
418 |
526
|
419 retval[i] = 0; |
1
|
420 num = i; |
|
421 |
|
422 return retval; |
|
423 } |
|
424 |
|
425 char ** |
338
|
426 get_fcn_file_names (int& num, int no_suffix) |
1
|
427 { |
|
428 static int num_max = 1024; |
|
429 char **retval = new char * [num_max]; |
|
430 int i = 0; |
|
431 |
679
|
432 char *path_elt = kpse_path_element (user_pref.loadpath); |
1
|
433 |
679
|
434 while (path_elt) |
1
|
435 { |
679
|
436 str_llist_type *elt_dirs = kpse_element_dirs (path_elt); |
|
437 |
|
438 str_llist_elt_type *dir; |
|
439 for (dir = *elt_dirs; dir; dir = STR_LLIST_NEXT (*dir)) |
1
|
440 { |
679
|
441 char *elt_dir = STR_LLIST (*dir); |
1
|
442 |
679
|
443 if (elt_dir) |
1
|
444 { |
679
|
445 int tmp_num; |
|
446 char **names = get_fcn_file_names (tmp_num, elt_dir, no_suffix); |
|
447 |
|
448 if (i + tmp_num >= num_max - 1) |
|
449 { |
1358
|
450 // Reallocate the array. Only copy pointers, not |
|
451 // the strings they point to, then only delete the |
|
452 // original array of pointers, and not the strings |
|
453 // they point to. |
244
|
454 |
679
|
455 num_max += 1024; |
|
456 char **tmp = new char * [num_max]; |
|
457 for (int j = 0; j < i; j++) |
|
458 tmp[j] = retval[j]; |
1
|
459 |
679
|
460 delete [] retval; |
244
|
461 |
679
|
462 retval = tmp; |
|
463 } |
1
|
464 |
679
|
465 int k = 0; |
|
466 while (k < tmp_num) |
|
467 retval[i++] = names[k++]; |
|
468 } |
|
469 } |
1
|
470 |
679
|
471 path_elt = kpse_path_element (0); |
1
|
472 } |
|
473 |
526
|
474 retval[i] = 0; |
1
|
475 num = i; |
|
476 |
|
477 return retval; |
|
478 } |
|
479 |
1086
|
480 // Convert X to the nearest integer value. Should not pass NaN to |
|
481 // this function. |
|
482 |
1
|
483 int |
|
484 NINT (double x) |
|
485 { |
|
486 if (x > INT_MAX) |
|
487 return INT_MAX; |
|
488 else if (x < INT_MIN) |
|
489 return INT_MIN; |
|
490 else |
|
491 return (x > 0) ? ((int) (x + 0.5)) : ((int) (x - 0.5)); |
|
492 } |
|
493 |
|
494 double |
|
495 D_NINT (double x) |
|
496 { |
|
497 if (xisinf (x) || xisnan (x)) |
|
498 return x; |
|
499 else |
|
500 return floor (x + 0.5); |
|
501 } |
|
502 |
526
|
503 // XXX FIXME XXX -- put these in some file, and make them extern. |
|
504 |
|
505 static int |
|
506 all_strings (const Octave_object& args) |
|
507 { |
|
508 int n = args.length (); |
712
|
509 for (int i = 0; i < n; i++) |
610
|
510 if (! args(i).is_string ()) |
526
|
511 return 0; |
|
512 return 1; |
|
513 } |
|
514 |
|
515 char ** |
578
|
516 make_argv (const Octave_object& args, const char *fcn_name) |
526
|
517 { |
|
518 char **argv = 0; |
|
519 if (all_strings (args)) |
|
520 { |
|
521 int n = args.length (); |
|
522 argv = new char * [n + 1]; |
578
|
523 argv[0] = strsave (fcn_name); |
712
|
524 for (int i = 0; i < n; i++) |
1728
|
525 { |
|
526 string tstr = args(i).string_value (); |
|
527 argv[i+1] = strsave (tstr.c_str ()); |
|
528 } |
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 { |
1358
|
571 // Compute the maximum name length. |
526
|
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 |
1358
|
584 // Allow at least two spaces between names. |
526
|
585 |
|
586 max_name_length += 2; |
|
587 |
1358
|
588 // Calculate the maximum number of columns that will fit. |
526
|
589 |
|
590 int line_length = terminal_columns (); |
|
591 int cols = line_length / max_name_length; |
|
592 if (cols == 0) |
|
593 cols = 1; |
|
594 |
1358
|
595 // Calculate the number of rows that will be in each column except |
|
596 // possibly for a short column on the right. |
526
|
597 |
|
598 int rows = total_names / cols + (total_names % cols != 0); |
|
599 |
1358
|
600 // Recalculate columns based on rows. |
526
|
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 |
1358
|
611 // Print the next row. |
526
|
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 |
1488
|
651 DEFUN ("file_in_path", Ffile_in_path, Sfile_in_path, 10, |
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 * |
1363
|
782 undo_string_escapes (const char *s) |
801
|
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, |
1488
|
795 Sundo_string_escapes, 10, |
801
|
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 { |
1728
|
804 string tstr = args(0).string_value (); |
|
805 char *str = undo_string_escapes (tstr.c_str ()); |
801
|
806 retval = str; |
|
807 delete [] str; |
|
808 } |
|
809 else |
1023
|
810 print_usage ("undo_string_escapes"); |
801
|
811 |
|
812 return retval; |
|
813 } |
|
814 |
1711
|
815 // This function was adapted from xputenv from Karl Berry's kpathsearch |
|
816 // library. |
|
817 |
|
818 void |
|
819 oct_putenv (const char *var_name, const char *value) |
|
820 { |
|
821 static const char **saved_env_items = 0; |
|
822 static unsigned saved_len; |
|
823 char *old_item = 0; |
|
824 |
|
825 int new_len = strlen (var_name) + strlen (value) + 2; |
|
826 |
|
827 char *new_item = new char [new_len]; |
|
828 |
|
829 sprintf (new_item, "%s=%s", var_name, value); |
|
830 |
|
831 #ifndef SMART_PUTENV |
|
832 |
|
833 // Check if we have saved anything yet. |
|
834 |
|
835 if (! saved_env_items) |
|
836 { |
|
837 saved_env_items = new const char * [1]; |
|
838 saved_env_items[0] = var_name; |
|
839 saved_len = 1; |
|
840 } |
|
841 else |
|
842 { |
|
843 // Check if we've assigned VAR_NAME before. |
|
844 |
|
845 unsigned len = strlen (var_name); |
|
846 |
|
847 for (unsigned i = 0; i < saved_len && ! old_item; i++) |
|
848 { |
|
849 if (strcmp (saved_env_items[i], var_name) == 0) |
|
850 { |
|
851 old_item = getenv (var_name); |
|
852 |
|
853 assert (old_item); |
|
854 |
|
855 // Back up to the `NAME=' in the environment before the |
|
856 // value that getenv returns. |
|
857 |
|
858 old_item -= (len + 1); |
|
859 } |
|
860 } |
|
861 |
|
862 if (! old_item) |
|
863 { |
|
864 // If we haven't seen VAR_NAME before, save it. Assume it |
|
865 // is in safe storage. |
|
866 |
|
867 saved_len++; |
|
868 |
|
869 const char **tmp = new const char * [saved_len]; |
|
870 |
|
871 for (unsigned i = 0; i < saved_len - 1; i++) |
|
872 tmp[i] = saved_env_items[i]; |
|
873 |
|
874 tmp[saved_len - 1] = var_name; |
|
875 |
|
876 delete [] saved_env_items; |
|
877 |
|
878 saved_env_items = tmp; |
|
879 } |
|
880 } |
|
881 |
|
882 #endif |
|
883 |
|
884 // As far as I can see there's no way to distinguish between the |
|
885 // various errors; putenv doesn't have errno values. |
|
886 |
|
887 if (putenv (new_item) < 0) |
|
888 error ("putenv (%s) failed", new_item); |
|
889 |
|
890 #ifndef SMART_PUTENV |
|
891 |
|
892 // Can't free `new_item' because its contained value is now in |
|
893 // `environ', but we can free `old_item', since it's been replaced. |
|
894 |
|
895 delete [] old_item; |
|
896 |
|
897 #endif |
|
898 } |
|
899 |
572
|
900 /* |
1
|
901 ;;; Local Variables: *** |
|
902 ;;; mode: C++ *** |
|
903 ;;; page-delimiter: "^/\\*" *** |
|
904 ;;; End: *** |
|
905 */ |