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