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