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 |
240
|
39 #ifdef HAVE_CONFIG_H |
|
40 #include "config.h" |
1
|
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; |
240
|
352 char *tmp_path = strsave (pathstring); |
|
353 if (*tmp_path != '\0') |
1
|
354 { |
|
355 nelem++; |
240
|
356 char *ptr = tmp_path; |
1
|
357 while (*ptr != '\0') |
|
358 { |
|
359 if (*ptr == ':') |
|
360 nelem++; |
|
361 ptr++; |
|
362 } |
|
363 } |
|
364 |
240
|
365 // Why do I have to do this? |
|
366 |
|
367 char **foo = path; |
|
368 while (foo && *foo) |
|
369 { |
|
370 delete [] *foo; |
|
371 *foo = (char *) NULL; |
|
372 foo++; |
|
373 } |
|
374 |
1
|
375 delete [] path; |
|
376 path = new char * [nelem+1]; |
|
377 path[nelem] = (char *) NULL; |
|
378 |
|
379 int i = 0; |
240
|
380 char *ptr = tmp_path; |
1
|
381 while (i < nelem) |
|
382 { |
|
383 char *end = strchr (ptr, ':'); |
|
384 if (end != (char *) NULL) |
|
385 *end = '\0'; |
|
386 char *result = tilde_expand (ptr); |
|
387 path[i] = strsave (result); |
|
388 free (result); |
|
389 ptr = end + 1; |
|
390 i++; |
|
391 } |
|
392 |
240
|
393 delete [] tmp_path; |
1
|
394 } |
|
395 |
|
396 return path; |
|
397 } |
|
398 |
195
|
399 char * |
1
|
400 octave_home (void) |
|
401 { |
|
402 static char *home = (char *) NULL; |
|
403 delete [] home; |
|
404 char *oh = getenv ("OCTAVE_HOME"); |
|
405 if (oh != (char *) NULL) |
|
406 home = strsave (oh); |
|
407 else |
|
408 home = strsave (OCTAVE_HOME); |
|
409 return home; |
|
410 } |
|
411 |
195
|
412 char * |
1
|
413 octave_lib_dir (void) |
|
414 { |
|
415 static char *ol = (char *) NULL; |
|
416 delete [] ol; |
|
417 char *oh = octave_home (); |
|
418 char *tmp = strconcat (oh, "/lib/octave/"); |
|
419 ol = strconcat (tmp, version_string); |
224
|
420 delete [] tmp; |
1
|
421 return ol; |
|
422 } |
|
423 |
195
|
424 char * |
186
|
425 octave_info_dir (void) |
|
426 { |
|
427 static char *oi = (char *) NULL; |
|
428 delete [] oi; |
|
429 char *oh = octave_home (); |
|
430 oi = strconcat (oh, "/info/"); |
|
431 return oi; |
|
432 } |
|
433 |
224
|
434 /* |
|
435 * Handle OCTAVE_PATH from the environment like TeX handles TEXINPUTS. |
|
436 * If the path starts with `:', prepend the standard path. If it ends |
|
437 * with `:' append the standard path. If it begins and ends with |
|
438 * `:', do both (which is useless, but the luser asked for it...). |
|
439 * |
|
440 * This function may eventually be called more than once, so be |
|
441 * careful not to create memory leaks. |
|
442 */ |
1
|
443 char * |
|
444 default_path (void) |
|
445 { |
|
446 static char *pathstring = (char *) NULL; |
|
447 delete [] pathstring; |
224
|
448 |
|
449 static char *std_path = (char *) NULL; |
|
450 delete [] std_path; |
|
451 |
|
452 char *libdir = octave_lib_dir (); |
|
453 |
|
454 std_path = strconcat (".:", libdir); |
|
455 |
1
|
456 char *oct_path = getenv ("OCTAVE_PATH"); |
224
|
457 |
1
|
458 if (oct_path != (char *) NULL) |
224
|
459 { |
|
460 pathstring = strsave (oct_path); |
|
461 |
|
462 if (pathstring[0] == ':') |
|
463 { |
|
464 char *tmp = pathstring; |
|
465 pathstring = strconcat (std_path, pathstring); |
|
466 delete [] tmp; |
|
467 } |
|
468 |
|
469 int tmp_len = strlen (pathstring); |
|
470 if (pathstring[tmp_len-1] == ':') |
|
471 { |
|
472 char *tmp = pathstring; |
|
473 pathstring = strconcat (pathstring, std_path); |
|
474 delete [] tmp; |
|
475 } |
|
476 } |
1
|
477 else |
224
|
478 pathstring = strsave (std_path); |
|
479 |
1
|
480 return pathstring; |
|
481 } |
|
482 |
|
483 char * |
186
|
484 default_info_file (void) |
|
485 { |
|
486 static char *info_file_string = (char *) NULL; |
|
487 delete [] info_file_string; |
|
488 char *oct_info_file = getenv ("OCTAVE_INFO_FILE"); |
|
489 if (oct_info_file != (char *) NULL) |
|
490 info_file_string = strsave (oct_info_file); |
|
491 else |
|
492 { |
|
493 char *infodir = octave_info_dir (); |
|
494 info_file_string = strconcat (infodir, "octave.info"); |
|
495 } |
|
496 return info_file_string; |
|
497 } |
|
498 |
|
499 char * |
195
|
500 default_editor (void) |
|
501 { |
|
502 static char *editor_string = (char *) NULL; |
|
503 delete [] editor_string; |
|
504 char *env_editor = getenv ("EDITOR"); |
|
505 if (env_editor == (char *) NULL || *env_editor == '\0') |
|
506 editor_string = strsave ("vi"); |
|
507 else |
|
508 editor_string = strsave (env_editor); |
|
509 return editor_string; |
|
510 } |
|
511 |
|
512 char * |
1
|
513 get_site_defaults (void) |
|
514 { |
|
515 static char *sd = (char *) NULL; |
|
516 delete [] sd; |
|
517 char *libdir = octave_lib_dir (); |
|
518 sd = strconcat (libdir, "/octaverc"); |
|
519 return sd; |
|
520 } |
|
521 |
|
522 char * |
|
523 default_pager (void) |
|
524 { |
|
525 static char *pager_binary = (char *) NULL; |
|
526 delete [] pager_binary; |
|
527 char *pgr = getenv ("PAGER"); |
|
528 if (pgr != (char *) NULL) |
|
529 pager_binary = strsave (pgr); |
|
530 else |
|
531 #ifdef DEFAULT_PAGER |
|
532 pager_binary = strsave (DEFAULT_PAGER); |
|
533 #else |
|
534 pager_binary = strsave (""); |
|
535 #endif |
|
536 |
|
537 return pager_binary; |
|
538 } |
|
539 |
|
540 /* |
|
541 * See if the given file is in the path. |
|
542 */ |
|
543 char * |
164
|
544 file_in_path (const char *name, const char *suffix) |
1
|
545 { |
240
|
546 char *retval = (char *) NULL; |
|
547 |
1
|
548 char *nm = strconcat ("/", name); |
|
549 if (suffix != (char *) NULL) |
|
550 { |
240
|
551 char *tmp = nm; |
1
|
552 nm = strconcat (tmp, suffix); |
|
553 delete [] tmp; |
|
554 } |
|
555 |
240
|
556 if (! the_current_working_directory) |
1
|
557 get_working_directory ("file_in_path"); |
|
558 |
|
559 char **path = pathstring_to_vector (user_pref.loadpath); |
|
560 |
|
561 char **ptr = path; |
|
562 if (ptr != (char **) NULL) |
|
563 { |
|
564 while (*ptr != (char *) NULL) |
|
565 { |
240
|
566 char *tmp = strconcat (*ptr, nm); |
|
567 char *p = make_absolute (tmp, the_current_working_directory); |
|
568 delete [] tmp; |
1
|
569 ifstream in_file (p); |
|
570 if (in_file) |
|
571 { |
|
572 in_file.close (); |
240
|
573 retval = p; |
|
574 goto done; |
1
|
575 } |
|
576 delete [] p; |
|
577 ptr++; |
|
578 } |
|
579 } |
|
580 |
240
|
581 done: |
1
|
582 delete [] nm; |
240
|
583 return retval; |
1
|
584 } |
|
585 |
|
586 /* |
|
587 * See if there is an M-file in the path. If so, return the full path |
|
588 * to the file. |
|
589 */ |
|
590 char * |
164
|
591 m_file_in_path (const char *name) |
1
|
592 { |
|
593 return file_in_path (name, ".m"); |
|
594 } |
|
595 |
|
596 /* |
|
597 * Return a pretty pathname. If the first part of the pathname is the |
|
598 * same as $HOME, then replace that with `~'. |
|
599 */ |
|
600 char * |
|
601 polite_directory_format (char *name) |
|
602 { |
|
603 int l = home_directory ? strlen (home_directory) : 0; |
|
604 |
|
605 if (l > 1 && strncmp (home_directory, name, l) == 0 |
240
|
606 && (! name[l] || name[l] == '/')) |
1
|
607 { |
|
608 strcpy (tdir + 1, name + l); |
|
609 tdir[0] = '~'; |
|
610 return (tdir); |
|
611 } |
|
612 else |
|
613 return name; |
|
614 } |
|
615 |
|
616 /* |
|
617 * Return 1 if STRING contains an absolute pathname, else 0. |
|
618 */ |
|
619 int |
164
|
620 absolute_pathname (const char *string) |
1
|
621 { |
240
|
622 if (! string || ! *string) |
1
|
623 return 0; |
|
624 |
|
625 if (*string == '/') |
|
626 return 1; |
|
627 |
|
628 if (*string++ == '.') |
|
629 { |
240
|
630 if ((! *string) || *string == '/') |
1
|
631 return 1; |
|
632 |
|
633 if (*string++ == '.') |
240
|
634 if (! *string || *string == '/') |
1
|
635 return 1; |
|
636 } |
|
637 return 0; |
|
638 } |
|
639 |
|
640 /* |
|
641 * Return 1 if STRING is an absolute program name; it is absolute if |
|
642 * it contains any slashes. This is used to decide whether or not to |
|
643 * look up through $PATH. |
|
644 */ |
|
645 int |
164
|
646 absolute_program (const char *string) |
1
|
647 { |
|
648 return (strchr (string, '/') != (char *)NULL); |
|
649 } |
|
650 |
|
651 /* |
|
652 * Return the `basename' of the pathname in STRING (the stuff after |
|
653 * the last '/'). If STRING is not a full pathname, simply return it. |
|
654 */ |
|
655 char * |
|
656 base_pathname (char *string) |
|
657 { |
|
658 char *p = strrchr (string, '/'); |
|
659 |
240
|
660 if (! absolute_pathname (string)) |
1
|
661 return (string); |
|
662 |
|
663 if (p) |
|
664 return (++p); |
|
665 else |
|
666 return (string); |
|
667 } |
|
668 |
|
669 /* |
|
670 * Return the octal number parsed from STRING, or -1 to indicate that |
|
671 * the string contained a bad number. |
|
672 */ |
|
673 int |
164
|
674 read_octal (const char *string) |
1
|
675 { |
|
676 int result = 0; |
|
677 int digits = 0; |
|
678 |
|
679 while (*string && *string >= '0' && *string < '8') |
|
680 { |
|
681 digits++; |
|
682 result = (result * 8) + *string++ - '0'; |
|
683 } |
|
684 |
240
|
685 if (! digits || result > 0777 || *string) |
1
|
686 result = -1; |
|
687 |
|
688 return result; |
|
689 } |
|
690 |
|
691 /* |
|
692 * Append SOURCE to TARGET at INDEX. SIZE is the current amount of |
|
693 * space allocated to TARGET. SOURCE can be NULL, in which case |
|
694 * nothing happens. Gets rid of SOURCE by free ()ing it. Returns |
|
695 * TARGET in case the location has changed. |
|
696 */ |
|
697 char * |
|
698 sub_append_string (char *source, char *target, int *index, int *size) |
|
699 { |
|
700 if (source) |
|
701 { |
|
702 while ((int)strlen (source) >= (int)(*size - *index)) |
|
703 { |
|
704 char *tmp = new char [*size += DEFAULT_ARRAY_SIZE]; |
|
705 strcpy (tmp, target); |
|
706 delete [] target; |
|
707 target = tmp; |
|
708 } |
|
709 |
|
710 strcat (target, source); |
|
711 *index += strlen (source); |
|
712 |
|
713 delete [] source; |
|
714 } |
|
715 return target; |
|
716 } |
|
717 |
|
718 /* |
|
719 * Return a string which will be printed as a prompt. The string may |
|
720 * contain special characters which are decoded as follows: |
|
721 * |
|
722 * \t the time |
|
723 * \d the date |
|
724 * \n CRLF |
|
725 * \s the name of the shell (program) |
|
726 * \w the current working directory |
|
727 * \W the last element of PWD |
|
728 * \u your username |
|
729 * \h the hostname |
|
730 * \# the command number of this command |
|
731 * \! the history number of this command |
|
732 * \$ a $ or a # if you are root |
|
733 * \<octal> character code in octal |
|
734 * \\ a backslash |
|
735 */ |
|
736 char * |
164
|
737 decode_prompt_string (const char *string) |
1
|
738 { |
|
739 int result_size = PROMPT_GROWTH; |
|
740 int result_index = 0; |
|
741 char *result = new char [PROMPT_GROWTH]; |
|
742 int c; |
|
743 char *temp = (char *)NULL; |
|
744 |
|
745 result[0] = 0; |
|
746 while (c = *string++) |
|
747 { |
|
748 if (c == '\\') |
|
749 { |
|
750 c = *string; |
|
751 |
|
752 switch (c) |
|
753 { |
|
754 case '0': |
|
755 case '1': |
|
756 case '2': |
|
757 case '3': |
|
758 case '4': |
|
759 case '5': |
|
760 case '6': |
|
761 case '7': |
|
762 { |
|
763 char octal_string[4]; |
|
764 int n; |
|
765 |
|
766 strncpy (octal_string, string, 3); |
|
767 octal_string[3] = '\0'; |
|
768 |
|
769 n = read_octal (octal_string); |
|
770 |
|
771 temp = strsave ("\\"); |
|
772 if (n != -1) |
|
773 { |
|
774 string += 3; |
|
775 temp[0] = n; |
|
776 } |
|
777 |
|
778 c = 0; |
|
779 goto add_string; |
|
780 } |
|
781 |
|
782 case 't': |
|
783 case 'd': |
|
784 /* Make the current time/date into a string. */ |
|
785 { |
160
|
786 time_t the_time = time (0); |
1
|
787 char *ttemp = ctime (&the_time); |
|
788 temp = strsave (ttemp); |
|
789 |
|
790 if (c == 't') |
|
791 { |
|
792 strcpy (temp, temp + 11); |
|
793 temp[8] = '\0'; |
|
794 } |
|
795 else |
|
796 temp[10] = '\0'; |
|
797 |
|
798 goto add_string; |
|
799 } |
|
800 |
|
801 case 'n': |
240
|
802 if (! no_line_editing) |
1
|
803 temp = strsave ("\r\n"); |
|
804 else |
|
805 temp = strsave ("\n"); |
|
806 goto add_string; |
|
807 |
|
808 case 's': |
|
809 { |
|
810 temp = base_pathname (prog_name); |
|
811 temp = strsave (temp); |
|
812 goto add_string; |
|
813 } |
|
814 |
|
815 case 'w': |
|
816 case 'W': |
|
817 { |
|
818 char t_string[MAXPATHLEN]; |
|
819 #define EFFICIENT |
|
820 #ifdef EFFICIENT |
|
821 |
|
822 // Use the value of PWD because it is much more effecient. |
|
823 |
|
824 temp = user_pref.pwd; |
|
825 |
240
|
826 if (! temp) |
1
|
827 getcwd (t_string, MAXPATHLEN); |
|
828 else |
|
829 strcpy (t_string, temp); |
|
830 #else |
|
831 getcwd (t_string, MAXPATHLEN); |
|
832 #endif /* EFFICIENT */ |
|
833 |
|
834 if (c == 'W') |
|
835 { |
|
836 char *dir = strrchr (t_string, '/'); |
|
837 if (dir && dir != t_string) |
|
838 strcpy (t_string, dir + 1); |
|
839 temp = strsave (t_string); |
|
840 } |
|
841 else |
|
842 temp = strsave (polite_directory_format (t_string)); |
|
843 goto add_string; |
|
844 } |
|
845 |
|
846 case 'u': |
|
847 { |
|
848 temp = strsave (user_name); |
|
849 |
|
850 goto add_string; |
|
851 } |
|
852 |
|
853 case 'h': |
|
854 { |
|
855 char *t_string; |
|
856 |
|
857 temp = strsave (host_name); |
|
858 if (t_string = strchr (temp, '.')) |
|
859 *t_string = '\0'; |
|
860 |
|
861 goto add_string; |
|
862 } |
|
863 |
|
864 case '#': |
|
865 { |
|
866 char number_buffer[20]; |
|
867 sprintf (number_buffer, "%d", current_command_number); |
|
868 temp = strsave (number_buffer); |
|
869 goto add_string; |
|
870 } |
|
871 |
|
872 case '!': |
|
873 { |
|
874 char number_buffer[20]; |
|
875 int num = current_history_number (); |
|
876 if (num > 0) |
|
877 sprintf (number_buffer, "%d", num); |
|
878 else |
|
879 strcpy (number_buffer, "!"); |
|
880 temp = strsave (number_buffer); |
|
881 goto add_string; |
|
882 } |
|
883 |
|
884 case '$': |
|
885 temp = strsave (geteuid () == 0 ? "#" : "$"); |
|
886 goto add_string; |
|
887 |
|
888 case '\\': |
|
889 temp = strsave ("\\"); |
|
890 goto add_string; |
|
891 |
|
892 default: |
|
893 temp = strsave ("\\ "); |
|
894 temp[1] = c; |
|
895 |
|
896 add_string: |
|
897 if (c) |
|
898 string++; |
|
899 result = |
|
900 (char *)sub_append_string (temp, result, |
|
901 &result_index, &result_size); |
|
902 temp = (char *)NULL; /* Free ()'ed in sub_append_string (). */ |
|
903 result[result_index] = '\0'; |
|
904 break; |
|
905 } |
|
906 } |
|
907 else |
|
908 { |
|
909 while (3 + result_index > result_size) |
|
910 { |
|
911 char *tmp = new char [result_size += PROMPT_GROWTH]; |
|
912 strcpy (tmp, result); |
|
913 delete [] result; |
|
914 result = tmp; |
|
915 } |
|
916 result[result_index++] = c; |
|
917 result[result_index] = '\0'; |
|
918 } |
|
919 } |
|
920 |
|
921 #if 0 |
|
922 /* I don't really think that this is a good idea. Do you? */ |
240
|
923 if (! find_variable ("NO_PROMPT_VARS")) |
1
|
924 { |
|
925 WORD_LIST *expand_string (), *list; |
|
926 char *string_list (); |
|
927 |
|
928 list = expand_string (result, 1); |
|
929 free (result); |
|
930 result = string_list (list); |
|
931 dispose_words (list); |
|
932 } |
|
933 #endif |
|
934 |
|
935 return result; |
|
936 } |
|
937 |
|
938 /* |
|
939 * Remove the last N directories from PATH. Do not PATH blank. |
164
|
940 * PATH must contain enough space for MAXPATHLEN characters. |
1
|
941 */ |
|
942 void |
|
943 pathname_backup (char *path, int n) |
|
944 { |
|
945 register char *p; |
|
946 |
240
|
947 if (! *path) |
1
|
948 return; |
|
949 |
|
950 p = path + (strlen (path) - 1); |
|
951 |
|
952 while (n--) |
|
953 { |
|
954 while (*p == '/' && p != path) |
|
955 p--; |
|
956 |
|
957 while (*p != '/' && p != path) |
|
958 p--; |
|
959 |
|
960 *++p = '\0'; |
|
961 } |
|
962 } |
|
963 |
|
964 /* |
|
965 * Turn STRING (a pathname) into an absolute pathname, assuming that |
|
966 * DOT_PATH contains the symbolic location of '.'. This always |
|
967 * returns a new string, even if STRING was an absolute pathname to |
|
968 * begin with. |
|
969 */ |
|
970 char * |
164
|
971 make_absolute (const char *string, const char *dot_path) |
1
|
972 { |
|
973 static char current_path[MAXPATHLEN]; |
|
974 register char *cp; |
|
975 |
240
|
976 if (! dot_path || *string == '/') |
1
|
977 return strsave (string); |
|
978 |
|
979 strcpy (current_path, dot_path); |
|
980 |
240
|
981 if (! current_path[0]) |
1
|
982 strcpy (current_path, "./"); |
|
983 |
|
984 cp = current_path + (strlen (current_path) - 1); |
|
985 |
|
986 if (*cp++ != '/') |
|
987 *cp++ = '/'; |
|
988 |
|
989 *cp = '\0'; |
|
990 |
|
991 while (*string) |
|
992 { |
|
993 if (*string == '.') |
|
994 { |
240
|
995 if (! string[1]) |
1
|
996 return strsave (current_path); |
|
997 |
|
998 if (string[1] == '/') |
|
999 { |
|
1000 string += 2; |
|
1001 continue; |
|
1002 } |
|
1003 |
240
|
1004 if (string[1] == '.' && (string[2] == '/' || ! string[2])) |
1
|
1005 { |
|
1006 string += 2; |
|
1007 |
|
1008 if (*string) |
|
1009 string++; |
|
1010 |
|
1011 pathname_backup (current_path, 1); |
|
1012 cp = current_path + strlen (current_path); |
|
1013 continue; |
|
1014 } |
|
1015 } |
|
1016 |
|
1017 while (*string && *string != '/') |
|
1018 *cp++ = *string++; |
|
1019 |
|
1020 if (*string) |
|
1021 *cp++ = *string++; |
|
1022 |
|
1023 *cp = '\0'; |
|
1024 } |
|
1025 return strsave (current_path); |
|
1026 } |
|
1027 |
|
1028 /* |
|
1029 * Return a consed string which is the current working directory. |
|
1030 * FOR_WHOM is the name of the caller for error printing. |
|
1031 */ |
|
1032 char * |
164
|
1033 get_working_directory (const char *for_whom) |
1
|
1034 { |
240
|
1035 if (! follow_symbolic_links) |
1
|
1036 { |
|
1037 if (the_current_working_directory) |
|
1038 delete [] the_current_working_directory; |
|
1039 |
|
1040 the_current_working_directory = (char *)NULL; |
|
1041 } |
|
1042 |
240
|
1043 if (! the_current_working_directory) |
1
|
1044 { |
|
1045 char *directory; |
|
1046 |
|
1047 the_current_working_directory = new char [MAXPATHLEN]; |
|
1048 directory = getcwd (the_current_working_directory, MAXPATHLEN); |
240
|
1049 if (! directory) |
1
|
1050 { |
|
1051 message (for_whom, the_current_working_directory); |
|
1052 delete [] the_current_working_directory; |
|
1053 the_current_working_directory = (char *)NULL; |
|
1054 return (char *)NULL; |
|
1055 } |
|
1056 } |
|
1057 |
|
1058 return the_current_working_directory; |
|
1059 } |
|
1060 |
|
1061 /* |
|
1062 * Do the work of changing to the directory NEWDIR. Handle symbolic |
|
1063 * link following, etc. |
|
1064 */ |
|
1065 int |
164
|
1066 change_to_directory (const char *newdir) |
1
|
1067 { |
|
1068 char *t; |
|
1069 |
|
1070 if (follow_symbolic_links) |
|
1071 { |
240
|
1072 if (! the_current_working_directory) |
1
|
1073 get_working_directory ("cd_links"); |
|
1074 |
|
1075 if (the_current_working_directory) |
|
1076 t = make_absolute (newdir, the_current_working_directory); |
|
1077 else |
|
1078 t = strsave (newdir); |
|
1079 |
|
1080 /* Get rid of trailing `/'. */ |
|
1081 { |
|
1082 register int len_t = strlen (t); |
|
1083 if (len_t > 1) |
|
1084 { |
|
1085 --len_t; |
|
1086 if (t[len_t] == '/') |
|
1087 t[len_t] = '\0'; |
|
1088 } |
|
1089 } |
|
1090 |
|
1091 if (chdir (t) < 0) |
|
1092 { |
|
1093 delete [] t; |
|
1094 return 0; |
|
1095 } |
|
1096 |
|
1097 if (the_current_working_directory) |
|
1098 strcpy (the_current_working_directory, t); |
|
1099 |
|
1100 delete [] t; |
|
1101 return 1; |
|
1102 } |
|
1103 else |
|
1104 { |
|
1105 if (chdir (newdir) < 0) |
|
1106 return 0; |
|
1107 else |
|
1108 return 1; |
|
1109 } |
|
1110 } |
|
1111 |
|
1112 /* |
|
1113 * Has file `A' been modified after time `T'? |
|
1114 * |
|
1115 * case: |
|
1116 * |
|
1117 * a newer than t returns 1 |
|
1118 * a older than t returns 0 |
|
1119 * stat on a fails returns -1 |
|
1120 */ |
|
1121 int |
164
|
1122 is_newer (const char *fa, time_t t) |
1
|
1123 { |
|
1124 struct stat fa_sb; |
|
1125 register int fa_stat; |
|
1126 register int status = 0; |
|
1127 |
|
1128 fa_stat = stat (fa, &fa_sb); |
|
1129 if (fa_stat != 0) |
|
1130 status = -1; |
|
1131 |
|
1132 if (status != 0) |
|
1133 return status; |
|
1134 |
|
1135 return (fa_sb.st_mtime > t); |
|
1136 } |
|
1137 |
|
1138 /* |
|
1139 * Return to the main command loop in octave.cc. |
|
1140 */ |
189
|
1141 void |
1
|
1142 jump_to_top_level (void) |
|
1143 { |
|
1144 run_all_unwind_protects (); |
|
1145 |
|
1146 longjmp (toplevel, 1); |
|
1147 } |
|
1148 |
|
1149 /* |
|
1150 * Gag. |
|
1151 */ |
|
1152 char * |
|
1153 s_plural (int i) |
|
1154 { |
|
1155 static char *empty = ""; |
|
1156 static char *s = "s"; |
|
1157 return i == 1 ? empty : s; |
|
1158 } |
|
1159 |
|
1160 char * |
|
1161 es_plural (int i) |
|
1162 { |
|
1163 static char *empty = ""; |
|
1164 static char *es = "es"; |
|
1165 return i == 1 ? es : empty; |
|
1166 } |
|
1167 |
|
1168 char * |
|
1169 save_in_tmp_file (tree_constant& t, int ndim = 2, int parametric = 0) |
|
1170 { |
|
1171 char *name = strsave (tmpnam ((char *) NULL)); |
|
1172 if (name != (char *) NULL) |
|
1173 { |
|
1174 ofstream file (name); |
|
1175 if (file) |
|
1176 { |
|
1177 switch (ndim) |
|
1178 { |
|
1179 case 2: |
|
1180 t.save (file); |
|
1181 break; |
|
1182 case 3: |
|
1183 t.save_three_d (file, parametric); |
|
1184 break; |
|
1185 default: |
|
1186 panic_impossible (); |
|
1187 break; |
|
1188 } |
|
1189 } |
|
1190 else |
|
1191 { |
|
1192 error ("couldn't open temporary output file `%s'", name); |
|
1193 delete [] name; |
|
1194 name = (char *) NULL; |
|
1195 } |
|
1196 } |
|
1197 return name; |
|
1198 } |
|
1199 |
|
1200 void |
|
1201 mark_for_deletion (const char *filename) |
|
1202 { |
|
1203 char *tmp = strsave (filename); |
|
1204 tmp_files.push (tmp); |
|
1205 } |
|
1206 |
|
1207 void |
|
1208 cleanup_tmp_files (void) |
|
1209 { |
|
1210 while (! tmp_files.empty ()) |
|
1211 { |
|
1212 char *filename = tmp_files.pop (); |
|
1213 unlink (filename); |
|
1214 delete [] filename; |
|
1215 } |
|
1216 } |
|
1217 |
|
1218 int |
|
1219 send_to_plot_stream (const char *cmd) |
|
1220 { |
|
1221 // From sighandlers.cc: |
|
1222 extern int pipe_handler_error_count; |
|
1223 |
|
1224 static int initialized = 0; |
|
1225 |
|
1226 if (! plot_stream.is_open ()) |
|
1227 { |
|
1228 char *plot_prog = user_pref.gnuplot_binary; |
|
1229 if (plot_prog != (char *) NULL) |
|
1230 { |
|
1231 plot_stream.open (plot_prog); |
|
1232 if (! plot_stream.is_open ()) |
|
1233 { |
|
1234 warning ("plot: unable to open pipe to `%s'", |
|
1235 plot_prog); |
|
1236 |
|
1237 if (strcmp (plot_prog, "gnuplot") != 0) |
|
1238 { |
217
|
1239 warning ("having trouble finding plotting program."); |
|
1240 warning ("trying again with `gnuplot'"); |
1
|
1241 goto last_chance; |
|
1242 } |
|
1243 } |
|
1244 } |
|
1245 else |
|
1246 { |
|
1247 last_chance: |
|
1248 |
|
1249 plot_stream.open ("gnuplot"); |
|
1250 |
|
1251 if (! plot_stream.is_open ()) |
|
1252 { |
|
1253 error ("plot: unable to open pipe to `%s'", plot_prog); |
|
1254 return -1; |
|
1255 } |
|
1256 } |
|
1257 } |
|
1258 |
|
1259 if (! initialized) |
|
1260 { |
|
1261 initialized = 1; |
|
1262 plot_stream << "set data style lines\n"; |
|
1263 } |
|
1264 |
|
1265 plot_stream << cmd; |
|
1266 plot_stream.flush (); |
|
1267 pipe_handler_error_count = 0; |
|
1268 |
|
1269 return 0; |
|
1270 } |
|
1271 |
|
1272 void |
|
1273 close_plot_stream (void) |
|
1274 { |
|
1275 if (plot_stream.is_open ()) |
|
1276 plot_stream.close (); |
|
1277 } |
|
1278 |
|
1279 int |
164
|
1280 almost_match (const char *std, const char *s, int min_match_len = 1) |
1
|
1281 { |
|
1282 int stdlen = strlen (std); |
|
1283 int slen = strlen (s); |
|
1284 |
|
1285 return (slen <= stdlen |
|
1286 && slen >= min_match_len |
|
1287 && strncmp (std, s, slen) == 0); |
|
1288 } |
|
1289 |
|
1290 char ** |
164
|
1291 get_m_file_names (int& num, const char *dir, int no_suffix) |
1
|
1292 { |
|
1293 static int num_max = 256; |
|
1294 char **retval = new char * [num_max]; |
|
1295 int i = 0; |
|
1296 |
|
1297 DIR *dirp = opendir (dir); |
|
1298 if (dirp != (DIR *) NULL) |
|
1299 { |
|
1300 struct dirent *entry; |
|
1301 while ((entry = readdir (dirp)) != (struct dirent *) NULL) |
|
1302 { |
|
1303 int len = NLENGTH (entry); |
|
1304 if (len > 2 |
|
1305 && entry->d_name[len-2] == '.' |
|
1306 && entry->d_name[len-1] == 'm') |
|
1307 { |
|
1308 retval[i] = strsave (entry->d_name); |
|
1309 if (no_suffix) |
|
1310 retval[i][len-2] = '\0'; |
|
1311 |
|
1312 i++; |
|
1313 |
|
1314 if (i == num_max - 1) |
|
1315 { |
|
1316 num_max += 256; |
|
1317 char **tmp = new char * [num_max]; |
|
1318 for (int j = 0; j < i; j++) |
|
1319 tmp[j] = retval[j]; |
|
1320 |
|
1321 retval = tmp; |
|
1322 } |
|
1323 } |
|
1324 } |
106
|
1325 closedir (dirp); |
1
|
1326 } |
|
1327 |
|
1328 retval[i] = (char *) NULL; |
|
1329 num = i; |
|
1330 |
|
1331 return retval; |
|
1332 } |
|
1333 |
|
1334 char ** |
|
1335 get_m_file_names (int& num, int no_suffix) |
|
1336 { |
|
1337 static int num_max = 1024; |
|
1338 char **retval = new char * [num_max]; |
|
1339 int i = 0; |
|
1340 |
|
1341 char **path = pathstring_to_vector (user_pref.loadpath); |
|
1342 |
|
1343 char **ptr = path; |
|
1344 if (ptr != (char **) NULL) |
|
1345 { |
|
1346 while (*ptr != (char *) NULL) |
|
1347 { |
|
1348 int tmp_num; |
|
1349 char **names = get_m_file_names (tmp_num, *ptr, no_suffix); |
|
1350 |
|
1351 if (i + tmp_num >= num_max - 1) |
|
1352 { |
|
1353 num_max += 1024; |
|
1354 char **tmp = new char * [num_max]; |
|
1355 for (int j = 0; j < i; j++) |
|
1356 tmp[j] = retval[j]; |
|
1357 |
|
1358 retval = tmp; |
|
1359 } |
|
1360 |
|
1361 int k = 0; |
|
1362 while (k < tmp_num) |
|
1363 retval[i++] = names[k++]; |
|
1364 |
|
1365 ptr++; |
|
1366 } |
|
1367 } |
|
1368 |
|
1369 retval[i] = (char *) NULL; |
|
1370 num = i; |
|
1371 |
|
1372 return retval; |
|
1373 } |
|
1374 |
|
1375 int |
|
1376 NINT (double x) |
|
1377 { |
|
1378 if (x > INT_MAX) |
|
1379 return INT_MAX; |
|
1380 else if (x < INT_MIN) |
|
1381 return INT_MIN; |
|
1382 else |
|
1383 return (x > 0) ? ((int) (x + 0.5)) : ((int) (x - 0.5)); |
|
1384 } |
|
1385 |
|
1386 double |
|
1387 D_NINT (double x) |
|
1388 { |
|
1389 if (xisinf (x) || xisnan (x)) |
|
1390 return x; |
|
1391 else |
|
1392 return floor (x + 0.5); |
|
1393 } |
|
1394 |
|
1395 /* |
|
1396 ;;; Local Variables: *** |
|
1397 ;;; mode: C++ *** |
|
1398 ;;; page-delimiter: "^/\\*" *** |
|
1399 ;;; End: *** |
|
1400 */ |