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