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" |
1670
|
69 #include "toplev.h" |
1352
|
70 #include "pager.h" |
1155
|
71 #include "pathsearch.h" |
1690
|
72 #include "sysdep.h" |
1352
|
73 #include "sysdir.h" |
1742
|
74 #include "pt-const.h" |
|
75 #include "oct-obj.h" |
1
|
76 #include "unwind-prot.h" |
1352
|
77 #include "user-prefs.h" |
|
78 #include "utils.h" |
|
79 #include "variables.h" |
1
|
80 |
|
81 // Top level context (?) |
|
82 extern jmp_buf toplevel; |
|
83 |
581
|
84 // Save a string. |
|
85 |
1
|
86 char * |
|
87 strsave (const char *s) |
|
88 { |
526
|
89 if (! s) |
|
90 return 0; |
1
|
91 |
|
92 int len = strlen (s); |
|
93 char *tmp = new char [len+1]; |
|
94 tmp = strcpy (tmp, s); |
|
95 return tmp; |
|
96 } |
|
97 |
581
|
98 // Concatenate two strings. |
|
99 |
1
|
100 char * |
|
101 strconcat (const char *s, const char *t) |
|
102 { |
|
103 int len = strlen (s) + strlen (t); |
|
104 char *tmp = new char [len+1]; |
|
105 strcpy (tmp, s); |
1266
|
106 strcat (tmp, t); |
|
107 return tmp; |
1
|
108 } |
|
109 |
581
|
110 // Throw away input until a given character is read. |
|
111 |
1
|
112 void |
|
113 discard_until (istream& stream, char character) |
|
114 { |
|
115 int c; |
|
116 for (;;) |
|
117 { |
|
118 stream >> c; |
|
119 if (c == EOF || c == character) |
|
120 break; |
|
121 } |
|
122 if (c != EOF) |
|
123 stream.putback ((char) c); |
|
124 } |
|
125 |
607
|
126 #if 0 |
|
127 |
|
128 // XXX UNTESTED XXX |
|
129 |
|
130 // Read input until a given character is read. Returns characters |
|
131 // read in a new string that must be freed by the caller. |
|
132 |
|
133 char * |
|
134 read_until (istream& stream, char character) |
|
135 { |
|
136 int grow_size = 8; |
|
137 int limit = grow_size; |
|
138 char *buf = new char [limit]; |
|
139 char *bp = buf; |
|
140 |
|
141 get_more: |
|
142 is.getline (bp, limit, character); |
|
143 |
|
144 if (is.gcount () == 0) |
|
145 { |
|
146 delete [] buf; |
|
147 return 0; |
|
148 } |
|
149 |
|
150 if (is.gcount () == limit && buf[limit-1] != '\0') |
|
151 { |
|
152 char *tmp = new char [limit + grow_size]; |
|
153 strcpy (tmp, buf); |
|
154 delete [] buf; |
|
155 buf = tmp; |
|
156 bp = tmp + limit - 1; |
|
157 limit += grow_size; |
|
158 grow_size *= 2; |
|
159 goto get_more; |
|
160 } |
|
161 |
|
162 return buf; |
|
163 } |
|
164 #endif |
|
165 |
661
|
166 // Get a temporary file name. |
643
|
167 |
|
168 char * |
|
169 octave_tmp_file_name (void) |
|
170 { |
662
|
171 static char *retval = 0; |
|
172 |
|
173 if (retval) |
|
174 free (retval); |
|
175 |
|
176 retval = tempnam (0, "oct-"); |
|
177 |
661
|
178 if (! retval) |
|
179 error ("can't open temporary file!"); |
662
|
180 |
661
|
181 return retval; |
643
|
182 } |
|
183 |
801
|
184 DEFUN ("octave_tmp_file_name", Foctave_tmp_file_name, |
1488
|
185 Soctave_tmp_file_name, 10, |
801
|
186 "octave_tmp_file_name ()") |
|
187 { |
|
188 tree_constant retval; |
|
189 |
|
190 if (args.length () == 0) |
|
191 retval = octave_tmp_file_name (); |
|
192 else |
|
193 print_usage ("octave_tmp_file_name"); |
|
194 |
|
195 return retval; |
|
196 } |
|
197 |
1
|
198 char ** |
|
199 pathstring_to_vector (char *pathstring) |
|
200 { |
526
|
201 static char **path = 0; |
1
|
202 |
526
|
203 if (pathstring) |
1
|
204 { |
|
205 int nelem = 0; |
240
|
206 char *tmp_path = strsave (pathstring); |
|
207 if (*tmp_path != '\0') |
1
|
208 { |
|
209 nelem++; |
240
|
210 char *ptr = tmp_path; |
1
|
211 while (*ptr != '\0') |
|
212 { |
1104
|
213 if (*ptr == SEPCHAR) |
1
|
214 nelem++; |
|
215 ptr++; |
|
216 } |
|
217 } |
|
218 |
240
|
219 char **foo = path; |
|
220 while (foo && *foo) |
244
|
221 delete [] *foo++; |
1
|
222 delete [] path; |
244
|
223 |
1
|
224 path = new char * [nelem+1]; |
526
|
225 path[nelem] = 0; |
1
|
226 |
|
227 int i = 0; |
240
|
228 char *ptr = tmp_path; |
1
|
229 while (i < nelem) |
|
230 { |
1104
|
231 char *end = strchr (ptr, SEPCHAR); |
526
|
232 if (end) |
1
|
233 *end = '\0'; |
|
234 char *result = tilde_expand (ptr); |
|
235 path[i] = strsave (result); |
|
236 free (result); |
|
237 ptr = end + 1; |
|
238 i++; |
|
239 } |
|
240 |
240
|
241 delete [] tmp_path; |
1
|
242 } |
|
243 |
|
244 return path; |
|
245 } |
|
246 |
581
|
247 // Return to the main command loop in octave.cc. |
|
248 |
1618
|
249 extern "C" void |
1
|
250 jump_to_top_level (void) |
|
251 { |
|
252 run_all_unwind_protects (); |
|
253 |
|
254 longjmp (toplevel, 1); |
|
255 } |
|
256 |
|
257 int |
526
|
258 almost_match (const char *std, const char *s, int min_match_len, |
|
259 int case_sens) |
1
|
260 { |
|
261 int stdlen = strlen (std); |
|
262 int slen = strlen (s); |
|
263 |
|
264 return (slen <= stdlen |
|
265 && slen >= min_match_len |
287
|
266 && (case_sens |
|
267 ? (strncmp (std, s, slen) == 0) |
|
268 : (strncasecmp (std, s, slen) == 0))); |
|
269 } |
|
270 |
581
|
271 // Ugh. |
|
272 |
287
|
273 int |
|
274 keyword_almost_match (const char **std, int *min_len, const char *s, |
|
275 int min_toks_to_match, int max_toks) |
|
276 { |
|
277 int status = 0; |
|
278 int tok_count = 0; |
|
279 int toks_matched = 0; |
|
280 |
526
|
281 if (! s || *s == '\0' || max_toks < 1) |
287
|
282 return status; |
|
283 |
|
284 char *kw = strsave (s); |
|
285 |
|
286 char *t = kw; |
|
287 while (*t != '\0') |
|
288 { |
|
289 if (*t == '\t') |
|
290 *t = ' '; |
|
291 t++; |
|
292 } |
|
293 |
|
294 char *beg = kw; |
|
295 while (*beg == ' ') |
|
296 beg++; |
|
297 |
|
298 if (*beg == '\0') |
|
299 return status; |
|
300 |
|
301 |
|
302 char **to_match = new char * [max_toks + 1]; |
526
|
303 const char **s1 = std; |
287
|
304 char **s2 = to_match; |
|
305 |
526
|
306 if (! s1 || ! s2) |
287
|
307 goto done; |
|
308 |
|
309 s2[tok_count] = beg; |
|
310 char *end; |
526
|
311 while ((end = strchr (beg, ' ')) != 0) |
287
|
312 { |
|
313 *end = '\0'; |
|
314 beg = end + 1; |
|
315 |
|
316 while (*beg == ' ') |
|
317 beg++; |
|
318 |
|
319 if (*beg == '\0') |
|
320 break; |
|
321 |
|
322 tok_count++; |
|
323 if (tok_count >= max_toks) |
|
324 goto done; |
|
325 |
|
326 s2[tok_count] = beg; |
|
327 } |
526
|
328 s2[tok_count+1] = 0; |
287
|
329 |
|
330 s2 = to_match; |
|
331 |
|
332 for (;;) |
|
333 { |
|
334 if (! almost_match (*s1, *s2, min_len[toks_matched], 0)) |
|
335 goto done; |
|
336 |
|
337 toks_matched++; |
|
338 |
|
339 s1++; |
|
340 s2++; |
|
341 |
|
342 if (! *s2) |
|
343 { |
|
344 status = (toks_matched >= min_toks_to_match); |
|
345 goto done; |
|
346 } |
|
347 |
|
348 if (! *s1) |
|
349 goto done; |
|
350 } |
|
351 |
|
352 done: |
|
353 |
|
354 delete [] kw; |
|
355 delete [] to_match; |
|
356 |
|
357 return status; |
1
|
358 } |
|
359 |
|
360 char ** |
338
|
361 get_fcn_file_names (int& num, const char *dir, int no_suffix) |
1
|
362 { |
|
363 static int num_max = 256; |
|
364 char **retval = new char * [num_max]; |
|
365 int i = 0; |
|
366 |
|
367 DIR *dirp = opendir (dir); |
526
|
368 if (dirp) |
1
|
369 { |
|
370 struct dirent *entry; |
526
|
371 while ((entry = readdir (dirp)) != 0) |
1
|
372 { |
|
373 int len = NLENGTH (entry); |
1664
|
374 #if defined (WITH_DYNAMIC_LINKING) |
826
|
375 if ((len > 2 |
|
376 && entry->d_name[len-2] == '.' |
|
377 && entry->d_name[len-1] == 'm') |
|
378 || (len > 4 |
|
379 && entry->d_name[len-4] == '.' |
|
380 && entry->d_name[len-3] == 'o' |
|
381 && entry->d_name[len-2] == 'c' |
|
382 && entry->d_name[len-1] == 't')) |
|
383 #else |
1
|
384 if (len > 2 |
|
385 && entry->d_name[len-2] == '.' |
|
386 && entry->d_name[len-1] == 'm') |
826
|
387 #endif |
1
|
388 { |
|
389 retval[i] = strsave (entry->d_name); |
|
390 if (no_suffix) |
826
|
391 { |
|
392 if (retval[i][len-1] == 'm') |
|
393 retval[i][len-2] = '\0'; |
|
394 else |
|
395 retval[i][len-4] = '\0'; |
|
396 } |
1
|
397 |
|
398 i++; |
|
399 |
|
400 if (i == num_max - 1) |
|
401 { |
1358
|
402 // Reallocate the array. Only copy pointers, not |
|
403 // the strings they point to, then only delete the |
|
404 // original array of pointers, and not the strings |
|
405 // they point to. |
244
|
406 |
1
|
407 num_max += 256; |
|
408 char **tmp = new char * [num_max]; |
|
409 for (int j = 0; j < i; j++) |
|
410 tmp[j] = retval[j]; |
|
411 |
244
|
412 delete [] retval; |
|
413 |
1
|
414 retval = tmp; |
|
415 } |
|
416 } |
|
417 } |
106
|
418 closedir (dirp); |
1
|
419 } |
|
420 |
526
|
421 retval[i] = 0; |
1
|
422 num = i; |
|
423 |
|
424 return retval; |
|
425 } |
|
426 |
|
427 char ** |
338
|
428 get_fcn_file_names (int& num, int no_suffix) |
1
|
429 { |
|
430 static int num_max = 1024; |
|
431 char **retval = new char * [num_max]; |
|
432 int i = 0; |
|
433 |
679
|
434 char *path_elt = kpse_path_element (user_pref.loadpath); |
1
|
435 |
679
|
436 while (path_elt) |
1
|
437 { |
679
|
438 str_llist_type *elt_dirs = kpse_element_dirs (path_elt); |
|
439 |
|
440 str_llist_elt_type *dir; |
|
441 for (dir = *elt_dirs; dir; dir = STR_LLIST_NEXT (*dir)) |
1
|
442 { |
679
|
443 char *elt_dir = STR_LLIST (*dir); |
1
|
444 |
679
|
445 if (elt_dir) |
1
|
446 { |
679
|
447 int tmp_num; |
|
448 char **names = get_fcn_file_names (tmp_num, elt_dir, no_suffix); |
|
449 |
|
450 if (i + tmp_num >= num_max - 1) |
|
451 { |
1358
|
452 // Reallocate the array. Only copy pointers, not |
|
453 // the strings they point to, then only delete the |
|
454 // original array of pointers, and not the strings |
|
455 // they point to. |
244
|
456 |
679
|
457 num_max += 1024; |
|
458 char **tmp = new char * [num_max]; |
|
459 for (int j = 0; j < i; j++) |
|
460 tmp[j] = retval[j]; |
1
|
461 |
679
|
462 delete [] retval; |
244
|
463 |
679
|
464 retval = tmp; |
|
465 } |
1
|
466 |
679
|
467 int k = 0; |
|
468 while (k < tmp_num) |
|
469 retval[i++] = names[k++]; |
|
470 } |
|
471 } |
1
|
472 |
679
|
473 path_elt = kpse_path_element (0); |
1
|
474 } |
|
475 |
526
|
476 retval[i] = 0; |
1
|
477 num = i; |
|
478 |
|
479 return retval; |
|
480 } |
|
481 |
1086
|
482 // Convert X to the nearest integer value. Should not pass NaN to |
|
483 // this function. |
|
484 |
1
|
485 int |
|
486 NINT (double x) |
|
487 { |
|
488 if (x > INT_MAX) |
|
489 return INT_MAX; |
|
490 else if (x < INT_MIN) |
|
491 return INT_MIN; |
|
492 else |
|
493 return (x > 0) ? ((int) (x + 0.5)) : ((int) (x - 0.5)); |
|
494 } |
|
495 |
|
496 double |
|
497 D_NINT (double x) |
|
498 { |
|
499 if (xisinf (x) || xisnan (x)) |
|
500 return x; |
|
501 else |
|
502 return floor (x + 0.5); |
|
503 } |
|
504 |
526
|
505 // XXX FIXME XXX -- put these in some file, and make them extern. |
|
506 |
|
507 static int |
|
508 all_strings (const Octave_object& args) |
|
509 { |
|
510 int n = args.length (); |
712
|
511 for (int i = 0; i < n; i++) |
610
|
512 if (! args(i).is_string ()) |
526
|
513 return 0; |
|
514 return 1; |
|
515 } |
|
516 |
|
517 char ** |
578
|
518 make_argv (const Octave_object& args, const char *fcn_name) |
526
|
519 { |
|
520 char **argv = 0; |
|
521 if (all_strings (args)) |
|
522 { |
|
523 int n = args.length (); |
|
524 argv = new char * [n + 1]; |
578
|
525 argv[0] = strsave (fcn_name); |
712
|
526 for (int i = 0; i < n; i++) |
1728
|
527 { |
|
528 string tstr = args(i).string_value (); |
|
529 argv[i+1] = strsave (tstr.c_str ()); |
|
530 } |
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 |
1488
|
653 DEFUN ("file_in_path", Ffile_in_path, Sfile_in_path, 10, |
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 * |
1363
|
784 undo_string_escapes (const char *s) |
801
|
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, |
1488
|
797 Sundo_string_escapes, 10, |
801
|
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 { |
1728
|
806 string tstr = args(0).string_value (); |
|
807 char *str = undo_string_escapes (tstr.c_str ()); |
801
|
808 retval = str; |
|
809 delete [] str; |
|
810 } |
|
811 else |
1023
|
812 print_usage ("undo_string_escapes"); |
801
|
813 |
|
814 return retval; |
|
815 } |
|
816 |
1711
|
817 // This function was adapted from xputenv from Karl Berry's kpathsearch |
|
818 // library. |
|
819 |
|
820 void |
|
821 oct_putenv (const char *var_name, const char *value) |
|
822 { |
|
823 static const char **saved_env_items = 0; |
|
824 static unsigned saved_len; |
|
825 char *old_item = 0; |
|
826 |
|
827 int new_len = strlen (var_name) + strlen (value) + 2; |
|
828 |
|
829 char *new_item = new char [new_len]; |
|
830 |
|
831 sprintf (new_item, "%s=%s", var_name, value); |
|
832 |
|
833 #ifndef SMART_PUTENV |
|
834 |
|
835 // Check if we have saved anything yet. |
|
836 |
|
837 if (! saved_env_items) |
|
838 { |
|
839 saved_env_items = new const char * [1]; |
|
840 saved_env_items[0] = var_name; |
|
841 saved_len = 1; |
|
842 } |
|
843 else |
|
844 { |
|
845 // Check if we've assigned VAR_NAME before. |
|
846 |
|
847 unsigned len = strlen (var_name); |
|
848 |
|
849 for (unsigned i = 0; i < saved_len && ! old_item; i++) |
|
850 { |
|
851 if (strcmp (saved_env_items[i], var_name) == 0) |
|
852 { |
|
853 old_item = getenv (var_name); |
|
854 |
|
855 assert (old_item); |
|
856 |
|
857 // Back up to the `NAME=' in the environment before the |
|
858 // value that getenv returns. |
|
859 |
|
860 old_item -= (len + 1); |
|
861 } |
|
862 } |
|
863 |
|
864 if (! old_item) |
|
865 { |
|
866 // If we haven't seen VAR_NAME before, save it. Assume it |
|
867 // is in safe storage. |
|
868 |
|
869 saved_len++; |
|
870 |
|
871 const char **tmp = new const char * [saved_len]; |
|
872 |
|
873 for (unsigned i = 0; i < saved_len - 1; i++) |
|
874 tmp[i] = saved_env_items[i]; |
|
875 |
|
876 tmp[saved_len - 1] = var_name; |
|
877 |
|
878 delete [] saved_env_items; |
|
879 |
|
880 saved_env_items = tmp; |
|
881 } |
|
882 } |
|
883 |
|
884 #endif |
|
885 |
|
886 // As far as I can see there's no way to distinguish between the |
|
887 // various errors; putenv doesn't have errno values. |
|
888 |
|
889 if (putenv (new_item) < 0) |
|
890 error ("putenv (%s) failed", new_item); |
|
891 |
|
892 #ifndef SMART_PUTENV |
|
893 |
|
894 // Can't free `new_item' because its contained value is now in |
|
895 // `environ', but we can free `old_item', since it's been replaced. |
|
896 |
|
897 delete [] old_item; |
|
898 |
|
899 #endif |
|
900 } |
|
901 |
572
|
902 /* |
1
|
903 ;;; Local Variables: *** |
|
904 ;;; mode: C++ *** |
|
905 ;;; page-delimiter: "^/\\*" *** |
|
906 ;;; End: *** |
|
907 */ |