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