1
|
1 // utils.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993 John W. Eaton |
|
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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 /* |
|
25 |
|
26 The 11 functions listed below were adapted from a similar functions |
|
27 from GNU Bash, the Bourne Again SHell, copyright (C) 1987, 1989, 1991 |
|
28 Free Software Foundation, Inc. |
|
29 |
|
30 polite_directory_format absolute_pathname |
|
31 absolute_program base_pathname |
|
32 read_octal sub_append_string |
|
33 decode_prompt_string pathname_backup |
|
34 make_absolute get_working_directory |
|
35 change_to_directory |
|
36 |
|
37 */ |
|
38 |
|
39 #ifdef __GNUG__ |
|
40 #pragma implementation |
|
41 #endif |
|
42 |
|
43 #include <sys/types.h> |
|
44 #ifdef HAVE_UNISTD_H |
|
45 #include <unistd.h> |
|
46 #endif |
|
47 #include <sys/param.h> |
|
48 #include <setjmp.h> |
|
49 #include <string.h> |
|
50 #include <stdlib.h> |
|
51 #include <stdio.h> |
|
52 #include <time.h> |
|
53 #include <math.h> |
|
54 #include <limits.h> |
|
55 #include <iostream.h> |
|
56 #include <strstream.h> |
|
57 #include <fstream.h> |
|
58 #include <dirent.h> |
|
59 |
|
60 #define NLENGTH(dirent) (strlen((dirent)->d_name)) |
|
61 |
138
|
62 extern "C" |
|
63 { |
|
64 #if defined (HAVE_TERMIOS_H) |
|
65 #include <termios.h> |
|
66 #elif defined (HAVE_TERMIO_H) |
1
|
67 #include <termio.h> |
138
|
68 #elif defined (HAVE_SGTTY_H) |
1
|
69 #include <sgtty.h> |
|
70 #else |
|
71 LOSE! LOSE! |
|
72 #endif |
|
73 |
138
|
74 extern int ioctl (int, int, ...); |
|
75 char *tilde_expand (char *s); /* From readline's tilde.c */ |
1
|
76 } |
|
77 |
|
78 #include "SLStack.h" |
|
79 |
|
80 #include "statdefs.h" |
|
81 #include "procstream.h" |
|
82 #include "user-prefs.h" |
|
83 #include "variables.h" |
|
84 #include "error.h" |
|
85 #include "utils.h" |
175
|
86 #include "input.h" |
1
|
87 #include "octave.h" |
|
88 #include "mappers.h" |
|
89 #include "version.h" |
11
|
90 #include "defaults.h" |
1
|
91 #include "tree-const.h" |
|
92 #include "unwind-prot.h" |
|
93 #include "octave-hist.h" |
|
94 |
138
|
95 #ifndef STDIN_FILENO |
|
96 #define STDIN_FILENO 1 |
|
97 #endif |
|
98 |
1
|
99 // Top level context (?) |
|
100 extern jmp_buf toplevel; |
|
101 |
|
102 // Pipe to gnuplot. |
|
103 static oprocstream plot_stream; |
|
104 |
|
105 // Non-zero means follow symbolic links that point to directories just |
|
106 // as if they are real directories. |
|
107 static int follow_symbolic_links = 1; |
|
108 |
|
109 #ifndef MAXPATHLEN |
|
110 #define MAXPATHLEN 1024 |
|
111 #endif |
|
112 |
|
113 // The size that strings change by. |
|
114 #ifndef DEFAULT_ARRAY_SIZE |
|
115 #define DEFAULT_ARRAY_SIZE 512 |
|
116 #endif |
|
117 |
|
118 // The growth rate for the prompt string. |
|
119 #ifndef PROMPT_GROWTH |
|
120 #define PROMPT_GROWTH 50 |
|
121 #endif |
|
122 |
|
123 #ifndef MAX |
|
124 #define MAX(a,b) ((a) > (b) ? (a) : (b)) |
|
125 #endif |
|
126 |
|
127 #ifndef MIN |
|
128 #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
|
129 #endif |
|
130 |
|
131 // Where to find the site-wide configuration file |
|
132 #ifndef OCTAVE_HOME |
|
133 #define OCTAVE_HOME "/usr/local" |
|
134 #endif |
|
135 |
|
136 // Temp storage for a path. |
|
137 static char tdir[MAXPATHLEN]; |
|
138 |
|
139 // List of files to delete when we exit or crash. |
|
140 static SLStack <char *> tmp_files; |
|
141 |
|
142 /* |
|
143 * Save a string. |
|
144 */ |
|
145 char * |
|
146 strsave (const char *s) |
|
147 { |
|
148 if (s == (char *) NULL) |
|
149 return (char *) NULL; |
|
150 |
|
151 int len = strlen (s); |
|
152 char *tmp = new char [len+1]; |
|
153 tmp = strcpy (tmp, s); |
|
154 return tmp; |
|
155 } |
|
156 |
|
157 /* |
|
158 * Concatenate two strings. |
|
159 */ |
|
160 char * |
|
161 strconcat (const char *s, const char *t) |
|
162 { |
|
163 int len = strlen (s) + strlen (t); |
|
164 char *tmp = new char [len+1]; |
|
165 strcpy (tmp, s); |
|
166 return strcat (tmp, t); |
|
167 } |
|
168 |
|
169 /* |
|
170 * Throw away input until a given character is read. |
|
171 */ |
|
172 void |
|
173 discard_until (istream& stream, char character) |
|
174 { |
|
175 int c; |
|
176 for (;;) |
|
177 { |
|
178 stream >> c; |
|
179 if (c == EOF || c == character) |
|
180 break; |
|
181 } |
|
182 if (c != EOF) |
|
183 stream.putback ((char) c); |
|
184 } |
|
185 |
|
186 void |
164
|
187 check_dimensions (int& nr, int& nc, const char *warnfor) |
1
|
188 { |
|
189 if (nr < 0 || nc < 0) |
|
190 { |
|
191 if (user_pref.treat_neg_dim_as_zero) |
|
192 nr = nc = 0; |
|
193 else |
143
|
194 error ("%s: can't create a matrix with negative dimensions", |
|
195 warnfor); |
1
|
196 } |
|
197 } |
|
198 |
|
199 /* |
|
200 * Set terminal in raw mode. From less-177. |
|
201 * |
|
202 * Change terminal to "raw mode", or restore to "normal" mode. |
|
203 * "Raw mode" means |
|
204 * 1. An outstanding read will complete on receipt of a single keystroke. |
|
205 * 2. Input is not echoed. |
|
206 * 3. On output, \n is mapped to \r\n. |
|
207 * 4. \t is NOT expanded into spaces. |
|
208 * 5. Signal-causing characters such as ctrl-C (interrupt), |
|
209 * etc. are NOT disabled. |
|
210 * It doesn't matter whether an input \n is mapped to \r, or vice versa. |
|
211 */ |
|
212 void |
|
213 raw_mode (int on) |
|
214 { |
|
215 static int curr_on = 0; |
|
216 |
138
|
217 int tty_fd = STDIN_FILENO; |
178
|
218 if (! isatty (tty_fd)) |
138
|
219 { |
178
|
220 if (interactive || forced_interactive) |
|
221 error ("stdin is not a tty!"); |
138
|
222 return; |
|
223 } |
1
|
224 |
|
225 if (on == curr_on) |
|
226 return; |
|
227 |
138
|
228 #if defined (HAVE_TERMIOS_H) |
1
|
229 { |
138
|
230 struct termios s; |
|
231 static struct termios save_term; |
1
|
232 |
|
233 if (on) |
|
234 { |
|
235 // Get terminal modes. |
|
236 |
138
|
237 tcgetattr (tty_fd, &s); |
1
|
238 |
|
239 // Save modes and set certain variables dependent on modes. |
|
240 |
|
241 save_term = s; |
|
242 // ospeed = s.c_cflag & CBAUD; |
|
243 // erase_char = s.c_cc[VERASE]; |
|
244 // kill_char = s.c_cc[VKILL]; |
|
245 |
|
246 // Set the modes to the way we want them. |
|
247 |
|
248 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
169
|
249 s.c_oflag |= (OPOST|ONLCR); |
1
|
250 s.c_oflag &= ~(OCRNL|ONOCR|ONLRET); |
|
251 s.c_cc[VMIN] = 1; |
|
252 s.c_cc[VTIME] = 0; |
|
253 } |
|
254 else |
|
255 { |
|
256 // Restore saved modes. |
|
257 s = save_term; |
|
258 } |
138
|
259 tcsetattr (tty_fd, TCSAFLUSH, &s); |
1
|
260 } |
138
|
261 #elif defined (HAVE_TERMIO_H) |
|
262 { |
|
263 struct termio s; |
|
264 static struct termio save_term; |
|
265 |
|
266 if (on) |
|
267 { |
|
268 // Get terminal modes. |
|
269 |
|
270 ioctl (tty_fd, TCGETA, &s); |
|
271 |
|
272 // Save modes and set certain variables dependent on modes. |
|
273 |
|
274 save_term = s; |
|
275 // ospeed = s.c_cflag & CBAUD; |
|
276 // erase_char = s.c_cc[VERASE]; |
|
277 // kill_char = s.c_cc[VKILL]; |
|
278 |
|
279 // Set the modes to the way we want them. |
|
280 |
|
281 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
169
|
282 s.c_oflag |= (OPOST|ONLCR); |
138
|
283 s.c_oflag &= ~(OCRNL|ONOCR|ONLRET); |
|
284 s.c_cc[VMIN] = 1; |
|
285 s.c_cc[VTIME] = 0; |
|
286 } |
|
287 else |
|
288 { |
|
289 // Restore saved modes. |
|
290 s = save_term; |
|
291 } |
|
292 ioctl (tty_fd, TCSETAW, &s); |
|
293 } |
|
294 #elif defined (HAVE_SGTTY_H) |
1
|
295 { |
|
296 struct sgttyb s; |
|
297 static struct sgttyb save_term; |
|
298 |
|
299 if (on) |
|
300 { |
|
301 // Get terminal modes. |
|
302 |
138
|
303 ioctl (tty_fd, TIOCGETP, &s); |
1
|
304 |
|
305 // Save modes and set certain variables dependent on modes. |
|
306 |
|
307 save_term = s; |
|
308 // ospeed = s.sg_ospeed; |
|
309 // erase_char = s.sg_erase; |
|
310 // kill_char = s.sg_kill; |
|
311 |
|
312 // Set the modes to the way we want them. |
|
313 |
|
314 s.sg_flags |= CBREAK; |
169
|
315 s.sg_flags &= ~(ECHO); |
1
|
316 } |
|
317 else |
|
318 { |
|
319 // Restore saved modes. |
|
320 s = save_term; |
|
321 } |
138
|
322 ioctl (tty_fd, TIOCSETN, &s); |
1
|
323 } |
|
324 #else |
|
325 LOSE! LOSE! |
|
326 #endif |
|
327 |
|
328 curr_on = on; |
|
329 } |
|
330 |
|
331 /* |
|
332 * Read one character from the terminal. |
|
333 */ |
|
334 int |
|
335 kbhit (void) |
|
336 { |
|
337 int c; |
|
338 raw_mode (1); |
|
339 c = cin.get (); |
|
340 raw_mode (0); |
|
341 return c; |
|
342 } |
|
343 |
|
344 char ** |
|
345 pathstring_to_vector (char *pathstring) |
|
346 { |
|
347 static char **path = (char **) NULL; |
|
348 |
|
349 if (pathstring != (char *) NULL) |
|
350 { |
|
351 int nelem = 0; |
|
352 pathstring = strsave (pathstring); |
|
353 if (*pathstring != '\0') |
|
354 { |
|
355 nelem++; |
|
356 char *ptr = pathstring; |
|
357 while (*ptr != '\0') |
|
358 { |
|
359 if (*ptr == ':') |
|
360 nelem++; |
|
361 ptr++; |
|
362 } |
|
363 } |
|
364 |
|
365 delete [] path; |
|
366 path = new char * [nelem+1]; |
|
367 path[nelem] = (char *) NULL; |
|
368 |
|
369 int i = 0; |
|
370 char *ptr = pathstring; |
|
371 while (i < nelem) |
|
372 { |
|
373 char *end = strchr (ptr, ':'); |
|
374 if (end != (char *) NULL) |
|
375 *end = '\0'; |
|
376 char *result = tilde_expand (ptr); |
|
377 path[i] = strsave (result); |
|
378 free (result); |
|
379 ptr = end + 1; |
|
380 i++; |
|
381 } |
|
382 |
|
383 delete [] pathstring; |
|
384 } |
|
385 |
|
386 return path; |
|
387 } |
|
388 |
195
|
389 char * |
1
|
390 octave_home (void) |
|
391 { |
|
392 static char *home = (char *) NULL; |
|
393 delete [] home; |
|
394 char *oh = getenv ("OCTAVE_HOME"); |
|
395 if (oh != (char *) NULL) |
|
396 home = strsave (oh); |
|
397 else |
|
398 home = strsave (OCTAVE_HOME); |
|
399 return home; |
|
400 } |
|
401 |
195
|
402 char * |
1
|
403 octave_lib_dir (void) |
|
404 { |
|
405 static char *ol = (char *) NULL; |
|
406 delete [] ol; |
|
407 char *oh = octave_home (); |
|
408 char *tmp = strconcat (oh, "/lib/octave/"); |
|
409 ol = strconcat (tmp, version_string); |
224
|
410 delete [] tmp; |
1
|
411 return ol; |
|
412 } |
|
413 |
195
|
414 char * |
186
|
415 octave_info_dir (void) |
|
416 { |
|
417 static char *oi = (char *) NULL; |
|
418 delete [] oi; |
|
419 char *oh = octave_home (); |
|
420 oi = strconcat (oh, "/info/"); |
|
421 return oi; |
|
422 } |
|
423 |
224
|
424 /* |
|
425 * Handle OCTAVE_PATH from the environment like TeX handles TEXINPUTS. |
|
426 * If the path starts with `:', prepend the standard path. If it ends |
|
427 * with `:' append the standard path. If it begins and ends with |
|
428 * `:', do both (which is useless, but the luser asked for it...). |
|
429 * |
|
430 * This function may eventually be called more than once, so be |
|
431 * careful not to create memory leaks. |
|
432 */ |
1
|
433 char * |
|
434 default_path (void) |
|
435 { |
|
436 static char *pathstring = (char *) NULL; |
|
437 delete [] pathstring; |
224
|
438 |
|
439 static char *std_path = (char *) NULL; |
|
440 delete [] std_path; |
|
441 |
|
442 char *libdir = octave_lib_dir (); |
|
443 |
|
444 std_path = strconcat (".:", libdir); |
|
445 |
1
|
446 char *oct_path = getenv ("OCTAVE_PATH"); |
224
|
447 |
1
|
448 if (oct_path != (char *) NULL) |
224
|
449 { |
|
450 pathstring = strsave (oct_path); |
|
451 |
|
452 if (pathstring[0] == ':') |
|
453 { |
|
454 char *tmp = pathstring; |
|
455 pathstring = strconcat (std_path, pathstring); |
|
456 delete [] tmp; |
|
457 } |
|
458 |
|
459 int tmp_len = strlen (pathstring); |
|
460 if (pathstring[tmp_len-1] == ':') |
|
461 { |
|
462 char *tmp = pathstring; |
|
463 pathstring = strconcat (pathstring, std_path); |
|
464 delete [] tmp; |
|
465 } |
|
466 } |
1
|
467 else |
224
|
468 pathstring = strsave (std_path); |
|
469 |
1
|
470 return pathstring; |
|
471 } |
|
472 |
|
473 char * |
186
|
474 default_info_file (void) |
|
475 { |
|
476 static char *info_file_string = (char *) NULL; |
|
477 delete [] info_file_string; |
|
478 char *oct_info_file = getenv ("OCTAVE_INFO_FILE"); |
|
479 if (oct_info_file != (char *) NULL) |
|
480 info_file_string = strsave (oct_info_file); |
|
481 else |
|
482 { |
|
483 char *infodir = octave_info_dir (); |
|
484 info_file_string = strconcat (infodir, "octave.info"); |
|
485 } |
|
486 return info_file_string; |
|
487 } |
|
488 |
|
489 char * |
195
|
490 default_editor (void) |
|
491 { |
|
492 static char *editor_string = (char *) NULL; |
|
493 delete [] editor_string; |
|
494 char *env_editor = getenv ("EDITOR"); |
|
495 if (env_editor == (char *) NULL || *env_editor == '\0') |
|
496 editor_string = strsave ("vi"); |
|
497 else |
|
498 editor_string = strsave (env_editor); |
|
499 return editor_string; |
|
500 } |
|
501 |
|
502 char * |
1
|
503 get_site_defaults (void) |
|
504 { |
|
505 static char *sd = (char *) NULL; |
|
506 delete [] sd; |
|
507 char *libdir = octave_lib_dir (); |
|
508 sd = strconcat (libdir, "/octaverc"); |
|
509 return sd; |
|
510 } |
|
511 |
|
512 char * |
|
513 default_pager (void) |
|
514 { |
|
515 static char *pager_binary = (char *) NULL; |
|
516 delete [] pager_binary; |
|
517 char *pgr = getenv ("PAGER"); |
|
518 if (pgr != (char *) NULL) |
|
519 pager_binary = strsave (pgr); |
|
520 else |
|
521 #ifdef DEFAULT_PAGER |
|
522 pager_binary = strsave (DEFAULT_PAGER); |
|
523 #else |
|
524 pager_binary = strsave (""); |
|
525 #endif |
|
526 |
|
527 return pager_binary; |
|
528 } |
|
529 |
|
530 /* |
|
531 * See if the given file is in the path. |
|
532 */ |
|
533 char * |
164
|
534 file_in_path (const char *name, const char *suffix) |
1
|
535 { |
|
536 char *nm = strconcat ("/", name); |
|
537 char *tmp = nm; |
|
538 if (suffix != (char *) NULL) |
|
539 { |
|
540 nm = strconcat (tmp, suffix); |
|
541 delete [] tmp; |
|
542 } |
|
543 |
|
544 if (!the_current_working_directory) |
|
545 get_working_directory ("file_in_path"); |
|
546 |
|
547 char **path = pathstring_to_vector (user_pref.loadpath); |
|
548 |
|
549 char **ptr = path; |
|
550 if (ptr != (char **) NULL) |
|
551 { |
|
552 while (*ptr != (char *) NULL) |
|
553 { |
|
554 char *tmp_p = strconcat (*ptr, nm); |
|
555 char *p = make_absolute (tmp_p, the_current_working_directory); |
|
556 delete [] tmp_p; |
|
557 ifstream in_file (p); |
|
558 if (in_file) |
|
559 { |
|
560 in_file.close (); |
|
561 delete [] nm; |
|
562 return p; |
|
563 } |
|
564 delete [] p; |
|
565 ptr++; |
|
566 } |
|
567 } |
|
568 |
|
569 delete [] nm; |
|
570 return (char *) NULL; |
|
571 } |
|
572 |
|
573 /* |
|
574 * See if there is an M-file in the path. If so, return the full path |
|
575 * to the file. |
|
576 */ |
|
577 char * |
164
|
578 m_file_in_path (const char *name) |
1
|
579 { |
|
580 return file_in_path (name, ".m"); |
|
581 } |
|
582 |
|
583 /* |
|
584 * Return a pretty pathname. If the first part of the pathname is the |
|
585 * same as $HOME, then replace that with `~'. |
|
586 */ |
|
587 char * |
|
588 polite_directory_format (char *name) |
|
589 { |
|
590 int l = home_directory ? strlen (home_directory) : 0; |
|
591 |
|
592 if (l > 1 && strncmp (home_directory, name, l) == 0 |
|
593 && (!name[l] || name[l] == '/')) |
|
594 { |
|
595 strcpy (tdir + 1, name + l); |
|
596 tdir[0] = '~'; |
|
597 return (tdir); |
|
598 } |
|
599 else |
|
600 return name; |
|
601 } |
|
602 |
|
603 /* |
|
604 * Return 1 if STRING contains an absolute pathname, else 0. |
|
605 */ |
|
606 int |
164
|
607 absolute_pathname (const char *string) |
1
|
608 { |
|
609 if (!string || !*string) |
|
610 return 0; |
|
611 |
|
612 if (*string == '/') |
|
613 return 1; |
|
614 |
|
615 if (*string++ == '.') |
|
616 { |
|
617 if ((!*string) || *string == '/') |
|
618 return 1; |
|
619 |
|
620 if (*string++ == '.') |
|
621 if (!*string || *string == '/') |
|
622 return 1; |
|
623 } |
|
624 return 0; |
|
625 } |
|
626 |
|
627 /* |
|
628 * Return 1 if STRING is an absolute program name; it is absolute if |
|
629 * it contains any slashes. This is used to decide whether or not to |
|
630 * look up through $PATH. |
|
631 */ |
|
632 int |
164
|
633 absolute_program (const char *string) |
1
|
634 { |
|
635 return (strchr (string, '/') != (char *)NULL); |
|
636 } |
|
637 |
|
638 /* |
|
639 * Return the `basename' of the pathname in STRING (the stuff after |
|
640 * the last '/'). If STRING is not a full pathname, simply return it. |
|
641 */ |
|
642 char * |
|
643 base_pathname (char *string) |
|
644 { |
|
645 char *p = strrchr (string, '/'); |
|
646 |
|
647 if (!absolute_pathname (string)) |
|
648 return (string); |
|
649 |
|
650 if (p) |
|
651 return (++p); |
|
652 else |
|
653 return (string); |
|
654 } |
|
655 |
|
656 /* |
|
657 * Return the octal number parsed from STRING, or -1 to indicate that |
|
658 * the string contained a bad number. |
|
659 */ |
|
660 int |
164
|
661 read_octal (const char *string) |
1
|
662 { |
|
663 int result = 0; |
|
664 int digits = 0; |
|
665 |
|
666 while (*string && *string >= '0' && *string < '8') |
|
667 { |
|
668 digits++; |
|
669 result = (result * 8) + *string++ - '0'; |
|
670 } |
|
671 |
|
672 if (!digits || result > 0777 || *string) |
|
673 result = -1; |
|
674 |
|
675 return result; |
|
676 } |
|
677 |
|
678 /* |
|
679 * Append SOURCE to TARGET at INDEX. SIZE is the current amount of |
|
680 * space allocated to TARGET. SOURCE can be NULL, in which case |
|
681 * nothing happens. Gets rid of SOURCE by free ()ing it. Returns |
|
682 * TARGET in case the location has changed. |
|
683 */ |
|
684 char * |
|
685 sub_append_string (char *source, char *target, int *index, int *size) |
|
686 { |
|
687 if (source) |
|
688 { |
|
689 while ((int)strlen (source) >= (int)(*size - *index)) |
|
690 { |
|
691 char *tmp = new char [*size += DEFAULT_ARRAY_SIZE]; |
|
692 strcpy (tmp, target); |
|
693 delete [] target; |
|
694 target = tmp; |
|
695 } |
|
696 |
|
697 strcat (target, source); |
|
698 *index += strlen (source); |
|
699 |
|
700 delete [] source; |
|
701 } |
|
702 return target; |
|
703 } |
|
704 |
|
705 /* |
|
706 * Return a string which will be printed as a prompt. The string may |
|
707 * contain special characters which are decoded as follows: |
|
708 * |
|
709 * \t the time |
|
710 * \d the date |
|
711 * \n CRLF |
|
712 * \s the name of the shell (program) |
|
713 * \w the current working directory |
|
714 * \W the last element of PWD |
|
715 * \u your username |
|
716 * \h the hostname |
|
717 * \# the command number of this command |
|
718 * \! the history number of this command |
|
719 * \$ a $ or a # if you are root |
|
720 * \<octal> character code in octal |
|
721 * \\ a backslash |
|
722 */ |
|
723 char * |
164
|
724 decode_prompt_string (const char *string) |
1
|
725 { |
|
726 int result_size = PROMPT_GROWTH; |
|
727 int result_index = 0; |
|
728 char *result = new char [PROMPT_GROWTH]; |
|
729 int c; |
|
730 char *temp = (char *)NULL; |
|
731 |
|
732 result[0] = 0; |
|
733 while (c = *string++) |
|
734 { |
|
735 if (c == '\\') |
|
736 { |
|
737 c = *string; |
|
738 |
|
739 switch (c) |
|
740 { |
|
741 case '0': |
|
742 case '1': |
|
743 case '2': |
|
744 case '3': |
|
745 case '4': |
|
746 case '5': |
|
747 case '6': |
|
748 case '7': |
|
749 { |
|
750 char octal_string[4]; |
|
751 int n; |
|
752 |
|
753 strncpy (octal_string, string, 3); |
|
754 octal_string[3] = '\0'; |
|
755 |
|
756 n = read_octal (octal_string); |
|
757 |
|
758 temp = strsave ("\\"); |
|
759 if (n != -1) |
|
760 { |
|
761 string += 3; |
|
762 temp[0] = n; |
|
763 } |
|
764 |
|
765 c = 0; |
|
766 goto add_string; |
|
767 } |
|
768 |
|
769 case 't': |
|
770 case 'd': |
|
771 /* Make the current time/date into a string. */ |
|
772 { |
160
|
773 time_t the_time = time (0); |
1
|
774 char *ttemp = ctime (&the_time); |
|
775 temp = strsave (ttemp); |
|
776 |
|
777 if (c == 't') |
|
778 { |
|
779 strcpy (temp, temp + 11); |
|
780 temp[8] = '\0'; |
|
781 } |
|
782 else |
|
783 temp[10] = '\0'; |
|
784 |
|
785 goto add_string; |
|
786 } |
|
787 |
|
788 case 'n': |
|
789 if (!no_line_editing) |
|
790 temp = strsave ("\r\n"); |
|
791 else |
|
792 temp = strsave ("\n"); |
|
793 goto add_string; |
|
794 |
|
795 case 's': |
|
796 { |
|
797 temp = base_pathname (prog_name); |
|
798 temp = strsave (temp); |
|
799 goto add_string; |
|
800 } |
|
801 |
|
802 case 'w': |
|
803 case 'W': |
|
804 { |
|
805 char t_string[MAXPATHLEN]; |
|
806 #define EFFICIENT |
|
807 #ifdef EFFICIENT |
|
808 |
|
809 // Use the value of PWD because it is much more effecient. |
|
810 |
|
811 temp = user_pref.pwd; |
|
812 |
|
813 if (!temp) |
|
814 getcwd (t_string, MAXPATHLEN); |
|
815 else |
|
816 strcpy (t_string, temp); |
|
817 #else |
|
818 getcwd (t_string, MAXPATHLEN); |
|
819 #endif /* EFFICIENT */ |
|
820 |
|
821 if (c == 'W') |
|
822 { |
|
823 char *dir = strrchr (t_string, '/'); |
|
824 if (dir && dir != t_string) |
|
825 strcpy (t_string, dir + 1); |
|
826 temp = strsave (t_string); |
|
827 } |
|
828 else |
|
829 temp = strsave (polite_directory_format (t_string)); |
|
830 goto add_string; |
|
831 } |
|
832 |
|
833 case 'u': |
|
834 { |
|
835 temp = strsave (user_name); |
|
836 |
|
837 goto add_string; |
|
838 } |
|
839 |
|
840 case 'h': |
|
841 { |
|
842 char *t_string; |
|
843 |
|
844 temp = strsave (host_name); |
|
845 if (t_string = strchr (temp, '.')) |
|
846 *t_string = '\0'; |
|
847 |
|
848 goto add_string; |
|
849 } |
|
850 |
|
851 case '#': |
|
852 { |
|
853 char number_buffer[20]; |
|
854 sprintf (number_buffer, "%d", current_command_number); |
|
855 temp = strsave (number_buffer); |
|
856 goto add_string; |
|
857 } |
|
858 |
|
859 case '!': |
|
860 { |
|
861 char number_buffer[20]; |
|
862 int num = current_history_number (); |
|
863 if (num > 0) |
|
864 sprintf (number_buffer, "%d", num); |
|
865 else |
|
866 strcpy (number_buffer, "!"); |
|
867 temp = strsave (number_buffer); |
|
868 goto add_string; |
|
869 } |
|
870 |
|
871 case '$': |
|
872 temp = strsave (geteuid () == 0 ? "#" : "$"); |
|
873 goto add_string; |
|
874 |
|
875 case '\\': |
|
876 temp = strsave ("\\"); |
|
877 goto add_string; |
|
878 |
|
879 default: |
|
880 temp = strsave ("\\ "); |
|
881 temp[1] = c; |
|
882 |
|
883 add_string: |
|
884 if (c) |
|
885 string++; |
|
886 result = |
|
887 (char *)sub_append_string (temp, result, |
|
888 &result_index, &result_size); |
|
889 temp = (char *)NULL; /* Free ()'ed in sub_append_string (). */ |
|
890 result[result_index] = '\0'; |
|
891 break; |
|
892 } |
|
893 } |
|
894 else |
|
895 { |
|
896 while (3 + result_index > result_size) |
|
897 { |
|
898 char *tmp = new char [result_size += PROMPT_GROWTH]; |
|
899 strcpy (tmp, result); |
|
900 delete [] result; |
|
901 result = tmp; |
|
902 } |
|
903 result[result_index++] = c; |
|
904 result[result_index] = '\0'; |
|
905 } |
|
906 } |
|
907 |
|
908 #if 0 |
|
909 /* I don't really think that this is a good idea. Do you? */ |
|
910 if (!find_variable ("NO_PROMPT_VARS")) |
|
911 { |
|
912 WORD_LIST *expand_string (), *list; |
|
913 char *string_list (); |
|
914 |
|
915 list = expand_string (result, 1); |
|
916 free (result); |
|
917 result = string_list (list); |
|
918 dispose_words (list); |
|
919 } |
|
920 #endif |
|
921 |
|
922 return result; |
|
923 } |
|
924 |
|
925 /* |
|
926 * Remove the last N directories from PATH. Do not PATH blank. |
164
|
927 * PATH must contain enough space for MAXPATHLEN characters. |
1
|
928 */ |
|
929 void |
|
930 pathname_backup (char *path, int n) |
|
931 { |
|
932 register char *p; |
|
933 |
|
934 if (!*path) |
|
935 return; |
|
936 |
|
937 p = path + (strlen (path) - 1); |
|
938 |
|
939 while (n--) |
|
940 { |
|
941 while (*p == '/' && p != path) |
|
942 p--; |
|
943 |
|
944 while (*p != '/' && p != path) |
|
945 p--; |
|
946 |
|
947 *++p = '\0'; |
|
948 } |
|
949 } |
|
950 |
|
951 /* |
|
952 * Turn STRING (a pathname) into an absolute pathname, assuming that |
|
953 * DOT_PATH contains the symbolic location of '.'. This always |
|
954 * returns a new string, even if STRING was an absolute pathname to |
|
955 * begin with. |
|
956 */ |
|
957 char * |
164
|
958 make_absolute (const char *string, const char *dot_path) |
1
|
959 { |
|
960 static char current_path[MAXPATHLEN]; |
|
961 register char *cp; |
|
962 |
|
963 if (!dot_path || *string == '/') |
|
964 return strsave (string); |
|
965 |
|
966 strcpy (current_path, dot_path); |
|
967 |
|
968 if (!current_path[0]) |
|
969 strcpy (current_path, "./"); |
|
970 |
|
971 cp = current_path + (strlen (current_path) - 1); |
|
972 |
|
973 if (*cp++ != '/') |
|
974 *cp++ = '/'; |
|
975 |
|
976 *cp = '\0'; |
|
977 |
|
978 while (*string) |
|
979 { |
|
980 if (*string == '.') |
|
981 { |
|
982 if (!string[1]) |
|
983 return strsave (current_path); |
|
984 |
|
985 if (string[1] == '/') |
|
986 { |
|
987 string += 2; |
|
988 continue; |
|
989 } |
|
990 |
|
991 if (string[1] == '.' && (string[2] == '/' || !string[2])) |
|
992 { |
|
993 string += 2; |
|
994 |
|
995 if (*string) |
|
996 string++; |
|
997 |
|
998 pathname_backup (current_path, 1); |
|
999 cp = current_path + strlen (current_path); |
|
1000 continue; |
|
1001 } |
|
1002 } |
|
1003 |
|
1004 while (*string && *string != '/') |
|
1005 *cp++ = *string++; |
|
1006 |
|
1007 if (*string) |
|
1008 *cp++ = *string++; |
|
1009 |
|
1010 *cp = '\0'; |
|
1011 } |
|
1012 return strsave (current_path); |
|
1013 } |
|
1014 |
|
1015 /* |
|
1016 * Return a consed string which is the current working directory. |
|
1017 * FOR_WHOM is the name of the caller for error printing. |
|
1018 */ |
|
1019 char * |
164
|
1020 get_working_directory (const char *for_whom) |
1
|
1021 { |
|
1022 if (!follow_symbolic_links) |
|
1023 { |
|
1024 if (the_current_working_directory) |
|
1025 delete [] the_current_working_directory; |
|
1026 |
|
1027 the_current_working_directory = (char *)NULL; |
|
1028 } |
|
1029 |
|
1030 if (!the_current_working_directory) |
|
1031 { |
|
1032 char *directory; |
|
1033 |
|
1034 the_current_working_directory = new char [MAXPATHLEN]; |
|
1035 directory = getcwd (the_current_working_directory, MAXPATHLEN); |
|
1036 if (!directory) |
|
1037 { |
|
1038 message (for_whom, the_current_working_directory); |
|
1039 delete [] the_current_working_directory; |
|
1040 the_current_working_directory = (char *)NULL; |
|
1041 return (char *)NULL; |
|
1042 } |
|
1043 } |
|
1044 |
|
1045 return the_current_working_directory; |
|
1046 } |
|
1047 |
|
1048 /* |
|
1049 * Do the work of changing to the directory NEWDIR. Handle symbolic |
|
1050 * link following, etc. |
|
1051 */ |
|
1052 int |
164
|
1053 change_to_directory (const char *newdir) |
1
|
1054 { |
|
1055 char *t; |
|
1056 |
|
1057 if (follow_symbolic_links) |
|
1058 { |
|
1059 if (!the_current_working_directory) |
|
1060 get_working_directory ("cd_links"); |
|
1061 |
|
1062 if (the_current_working_directory) |
|
1063 t = make_absolute (newdir, the_current_working_directory); |
|
1064 else |
|
1065 t = strsave (newdir); |
|
1066 |
|
1067 /* Get rid of trailing `/'. */ |
|
1068 { |
|
1069 register int len_t = strlen (t); |
|
1070 if (len_t > 1) |
|
1071 { |
|
1072 --len_t; |
|
1073 if (t[len_t] == '/') |
|
1074 t[len_t] = '\0'; |
|
1075 } |
|
1076 } |
|
1077 |
|
1078 if (chdir (t) < 0) |
|
1079 { |
|
1080 delete [] t; |
|
1081 return 0; |
|
1082 } |
|
1083 |
|
1084 if (the_current_working_directory) |
|
1085 strcpy (the_current_working_directory, t); |
|
1086 |
|
1087 delete [] t; |
|
1088 return 1; |
|
1089 } |
|
1090 else |
|
1091 { |
|
1092 if (chdir (newdir) < 0) |
|
1093 return 0; |
|
1094 else |
|
1095 return 1; |
|
1096 } |
|
1097 } |
|
1098 |
|
1099 /* |
|
1100 * Has file `A' been modified after time `T'? |
|
1101 * |
|
1102 * case: |
|
1103 * |
|
1104 * a newer than t returns 1 |
|
1105 * a older than t returns 0 |
|
1106 * stat on a fails returns -1 |
|
1107 */ |
|
1108 int |
164
|
1109 is_newer (const char *fa, time_t t) |
1
|
1110 { |
|
1111 struct stat fa_sb; |
|
1112 register int fa_stat; |
|
1113 register int status = 0; |
|
1114 |
|
1115 fa_stat = stat (fa, &fa_sb); |
|
1116 if (fa_stat != 0) |
|
1117 status = -1; |
|
1118 |
|
1119 if (status != 0) |
|
1120 return status; |
|
1121 |
|
1122 return (fa_sb.st_mtime > t); |
|
1123 } |
|
1124 |
|
1125 /* |
|
1126 * Return to the main command loop in octave.cc. |
|
1127 */ |
189
|
1128 void |
1
|
1129 jump_to_top_level (void) |
|
1130 { |
|
1131 run_all_unwind_protects (); |
|
1132 |
|
1133 longjmp (toplevel, 1); |
|
1134 } |
|
1135 |
|
1136 /* |
|
1137 * Gag. |
|
1138 */ |
|
1139 char * |
|
1140 s_plural (int i) |
|
1141 { |
|
1142 static char *empty = ""; |
|
1143 static char *s = "s"; |
|
1144 return i == 1 ? empty : s; |
|
1145 } |
|
1146 |
|
1147 char * |
|
1148 es_plural (int i) |
|
1149 { |
|
1150 static char *empty = ""; |
|
1151 static char *es = "es"; |
|
1152 return i == 1 ? es : empty; |
|
1153 } |
|
1154 |
|
1155 char * |
|
1156 save_in_tmp_file (tree_constant& t, int ndim = 2, int parametric = 0) |
|
1157 { |
|
1158 char *name = strsave (tmpnam ((char *) NULL)); |
|
1159 if (name != (char *) NULL) |
|
1160 { |
|
1161 ofstream file (name); |
|
1162 if (file) |
|
1163 { |
|
1164 switch (ndim) |
|
1165 { |
|
1166 case 2: |
|
1167 t.save (file); |
|
1168 break; |
|
1169 case 3: |
|
1170 t.save_three_d (file, parametric); |
|
1171 break; |
|
1172 default: |
|
1173 panic_impossible (); |
|
1174 break; |
|
1175 } |
|
1176 } |
|
1177 else |
|
1178 { |
|
1179 error ("couldn't open temporary output file `%s'", name); |
|
1180 delete [] name; |
|
1181 name = (char *) NULL; |
|
1182 } |
|
1183 } |
|
1184 return name; |
|
1185 } |
|
1186 |
|
1187 void |
|
1188 mark_for_deletion (const char *filename) |
|
1189 { |
|
1190 char *tmp = strsave (filename); |
|
1191 tmp_files.push (tmp); |
|
1192 } |
|
1193 |
|
1194 void |
|
1195 cleanup_tmp_files (void) |
|
1196 { |
|
1197 while (! tmp_files.empty ()) |
|
1198 { |
|
1199 char *filename = tmp_files.pop (); |
|
1200 unlink (filename); |
|
1201 delete [] filename; |
|
1202 } |
|
1203 } |
|
1204 |
|
1205 int |
|
1206 send_to_plot_stream (const char *cmd) |
|
1207 { |
|
1208 // From sighandlers.cc: |
|
1209 extern int pipe_handler_error_count; |
|
1210 |
|
1211 static int initialized = 0; |
|
1212 |
|
1213 if (! plot_stream.is_open ()) |
|
1214 { |
|
1215 char *plot_prog = user_pref.gnuplot_binary; |
|
1216 if (plot_prog != (char *) NULL) |
|
1217 { |
|
1218 plot_stream.open (plot_prog); |
|
1219 if (! plot_stream.is_open ()) |
|
1220 { |
|
1221 warning ("plot: unable to open pipe to `%s'", |
|
1222 plot_prog); |
|
1223 |
|
1224 if (strcmp (plot_prog, "gnuplot") != 0) |
|
1225 { |
217
|
1226 warning ("having trouble finding plotting program."); |
|
1227 warning ("trying again with `gnuplot'"); |
1
|
1228 goto last_chance; |
|
1229 } |
|
1230 } |
|
1231 } |
|
1232 else |
|
1233 { |
|
1234 last_chance: |
|
1235 |
|
1236 plot_stream.open ("gnuplot"); |
|
1237 |
|
1238 if (! plot_stream.is_open ()) |
|
1239 { |
|
1240 error ("plot: unable to open pipe to `%s'", plot_prog); |
|
1241 return -1; |
|
1242 } |
|
1243 } |
|
1244 } |
|
1245 |
|
1246 if (! initialized) |
|
1247 { |
|
1248 initialized = 1; |
|
1249 plot_stream << "set data style lines\n"; |
|
1250 } |
|
1251 |
|
1252 plot_stream << cmd; |
|
1253 plot_stream.flush (); |
|
1254 pipe_handler_error_count = 0; |
|
1255 |
|
1256 return 0; |
|
1257 } |
|
1258 |
|
1259 void |
|
1260 close_plot_stream (void) |
|
1261 { |
|
1262 if (plot_stream.is_open ()) |
|
1263 plot_stream.close (); |
|
1264 } |
|
1265 |
|
1266 int |
164
|
1267 almost_match (const char *std, const char *s, int min_match_len = 1) |
1
|
1268 { |
|
1269 int stdlen = strlen (std); |
|
1270 int slen = strlen (s); |
|
1271 |
|
1272 return (slen <= stdlen |
|
1273 && slen >= min_match_len |
|
1274 && strncmp (std, s, slen) == 0); |
|
1275 } |
|
1276 |
|
1277 char ** |
164
|
1278 get_m_file_names (int& num, const char *dir, int no_suffix) |
1
|
1279 { |
|
1280 static int num_max = 256; |
|
1281 char **retval = new char * [num_max]; |
|
1282 int i = 0; |
|
1283 |
|
1284 DIR *dirp = opendir (dir); |
|
1285 if (dirp != (DIR *) NULL) |
|
1286 { |
|
1287 struct dirent *entry; |
|
1288 while ((entry = readdir (dirp)) != (struct dirent *) NULL) |
|
1289 { |
|
1290 int len = NLENGTH (entry); |
|
1291 if (len > 2 |
|
1292 && entry->d_name[len-2] == '.' |
|
1293 && entry->d_name[len-1] == 'm') |
|
1294 { |
|
1295 retval[i] = strsave (entry->d_name); |
|
1296 if (no_suffix) |
|
1297 retval[i][len-2] = '\0'; |
|
1298 |
|
1299 i++; |
|
1300 |
|
1301 if (i == num_max - 1) |
|
1302 { |
|
1303 num_max += 256; |
|
1304 char **tmp = new char * [num_max]; |
|
1305 for (int j = 0; j < i; j++) |
|
1306 tmp[j] = retval[j]; |
|
1307 |
|
1308 retval = tmp; |
|
1309 } |
|
1310 } |
|
1311 } |
106
|
1312 closedir (dirp); |
1
|
1313 } |
|
1314 |
|
1315 retval[i] = (char *) NULL; |
|
1316 num = i; |
|
1317 |
|
1318 return retval; |
|
1319 } |
|
1320 |
|
1321 char ** |
|
1322 get_m_file_names (int& num, int no_suffix) |
|
1323 { |
|
1324 static int num_max = 1024; |
|
1325 char **retval = new char * [num_max]; |
|
1326 int i = 0; |
|
1327 |
|
1328 char **path = pathstring_to_vector (user_pref.loadpath); |
|
1329 |
|
1330 char **ptr = path; |
|
1331 if (ptr != (char **) NULL) |
|
1332 { |
|
1333 while (*ptr != (char *) NULL) |
|
1334 { |
|
1335 int tmp_num; |
|
1336 char **names = get_m_file_names (tmp_num, *ptr, no_suffix); |
|
1337 |
|
1338 if (i + tmp_num >= num_max - 1) |
|
1339 { |
|
1340 num_max += 1024; |
|
1341 char **tmp = new char * [num_max]; |
|
1342 for (int j = 0; j < i; j++) |
|
1343 tmp[j] = retval[j]; |
|
1344 |
|
1345 retval = tmp; |
|
1346 } |
|
1347 |
|
1348 int k = 0; |
|
1349 while (k < tmp_num) |
|
1350 retval[i++] = names[k++]; |
|
1351 |
|
1352 ptr++; |
|
1353 } |
|
1354 } |
|
1355 |
|
1356 retval[i] = (char *) NULL; |
|
1357 num = i; |
|
1358 |
|
1359 return retval; |
|
1360 } |
|
1361 |
|
1362 int |
|
1363 NINT (double x) |
|
1364 { |
|
1365 if (x > INT_MAX) |
|
1366 return INT_MAX; |
|
1367 else if (x < INT_MIN) |
|
1368 return INT_MIN; |
|
1369 else |
|
1370 return (x > 0) ? ((int) (x + 0.5)) : ((int) (x - 0.5)); |
|
1371 } |
|
1372 |
|
1373 double |
|
1374 D_NINT (double x) |
|
1375 { |
|
1376 if (xisinf (x) || xisnan (x)) |
|
1377 return x; |
|
1378 else |
|
1379 return floor (x + 0.5); |
|
1380 } |
|
1381 |
|
1382 /* |
|
1383 ;;; Local Variables: *** |
|
1384 ;;; mode: C++ *** |
|
1385 ;;; page-delimiter: "^/\\*" *** |
|
1386 ;;; End: *** |
|
1387 */ |