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