5832
|
1 /* |
|
2 |
|
3 Copyright (C) 2006 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <algorithm> |
|
29 |
|
30 #include "dir-ops.h" |
|
31 #include "file-ops.h" |
|
32 #include "file-stat.h" |
|
33 #include "oct-env.h" |
|
34 #include "pathsearch.h" |
|
35 |
|
36 #include "defaults.h" |
|
37 #include "defun.h" |
|
38 #include "input.h" |
|
39 #include "load-path.h" |
|
40 #include "pager.h" |
|
41 #include "parse.h" |
|
42 #include "toplev.h" |
|
43 #include "unwind-prot.h" |
|
44 #include "utils.h" |
|
45 |
|
46 load_path *load_path::instance = 0; |
|
47 load_path::hook_function_ptr load_path::add_hook = execute_pkg_add; |
|
48 load_path::hook_function_ptr load_path::remove_hook = execute_pkg_del; |
|
49 std::string load_path::command_line_path; |
|
50 |
|
51 static std::string Vsystem_path; |
|
52 |
|
53 void |
|
54 load_path::dir_info::update (void) |
|
55 { |
|
56 if (is_relative) |
|
57 initialize (); |
|
58 else |
|
59 { |
|
60 file_stat fs (dir_name); |
|
61 |
|
62 if (fs) |
|
63 { |
|
64 if (fs.mtime () != dir_mtime) |
|
65 initialize (); |
|
66 } |
|
67 else |
|
68 { |
|
69 std::string msg = fs.error (); |
|
70 warning ("load_path: %s: %s", dir_name.c_str (), msg.c_str ()); |
|
71 } |
|
72 } |
|
73 } |
|
74 |
|
75 void |
|
76 load_path::dir_info::initialize (void) |
|
77 { |
|
78 is_relative = ! octave_env::absolute_pathname (dir_name); |
|
79 |
|
80 file_stat fs (dir_name); |
|
81 |
|
82 if (fs) |
|
83 { |
|
84 dir_mtime = fs.mtime (); |
|
85 |
|
86 bool has_private_subdir = get_file_list (dir_name); |
|
87 |
|
88 if (! error_state) |
|
89 { |
|
90 if (has_private_subdir) |
|
91 { |
|
92 std::string pdn = dir_name + file_ops::dir_sep_str + "private"; |
|
93 |
|
94 get_private_function_map (pdn); |
|
95 } |
|
96 } |
|
97 } |
|
98 else |
|
99 { |
|
100 std::string msg = fs.error (); |
|
101 warning ("load_path: %s: %s", dir_name.c_str (), msg.c_str ()); |
|
102 } |
|
103 } |
|
104 |
|
105 bool |
|
106 load_path::dir_info::get_file_list (const std::string& d) |
|
107 { |
|
108 bool has_private_subdir = false; |
|
109 |
|
110 dir_entry dir (d); |
|
111 |
|
112 if (dir) |
|
113 { |
|
114 string_vector flist = dir.read (); |
|
115 |
|
116 octave_idx_type len = flist.length (); |
|
117 |
|
118 all_files.resize (len); |
|
119 fcn_files.resize (len); |
|
120 |
|
121 octave_idx_type all_files_count = 0; |
|
122 octave_idx_type fcn_files_count = 0; |
|
123 |
|
124 for (octave_idx_type i = 0; i < len; i++) |
|
125 { |
|
126 std::string fname = flist[i]; |
|
127 |
|
128 std::string full_name = d + file_ops::dir_sep_str + fname; |
|
129 |
|
130 file_stat fs (full_name); |
|
131 |
|
132 if (fs) |
|
133 { |
|
134 if (fs.is_dir ()) |
|
135 { |
|
136 if (! has_private_subdir && fname == "private") |
|
137 has_private_subdir = true; |
|
138 } |
|
139 else |
|
140 { |
|
141 all_files[all_files_count++] = fname; |
|
142 |
|
143 size_t pos = fname.rfind ('.'); |
|
144 |
|
145 if (pos != NPOS) |
|
146 { |
|
147 std::string ext = fname.substr (pos); |
|
148 |
|
149 if (ext == ".m" || ext == ".oct") |
|
150 { |
|
151 std::string base = fname.substr (0, pos); |
|
152 |
|
153 if (valid_identifier (base)) |
|
154 fcn_files[fcn_files_count++] = fname; |
|
155 } |
|
156 } |
|
157 } |
|
158 } |
|
159 } |
|
160 |
|
161 all_files.resize (all_files_count); |
|
162 fcn_files.resize (fcn_files_count); |
|
163 } |
|
164 else |
|
165 { |
|
166 std::string msg = dir.error (); |
|
167 warning ("load_path: %s: %s", d.c_str (), msg.c_str ()); |
|
168 } |
|
169 |
|
170 return has_private_subdir; |
|
171 } |
|
172 |
|
173 void |
|
174 load_path::dir_info::get_private_function_map (const std::string& d) |
|
175 { |
|
176 dir_entry dir (d); |
|
177 |
|
178 if (dir) |
|
179 { |
|
180 string_vector flist = dir.read (); |
|
181 |
|
182 octave_idx_type len = flist.length (); |
|
183 |
|
184 for (octave_idx_type i = 0; i < len; i++) |
|
185 { |
|
186 std::string fname = flist[i]; |
|
187 |
|
188 std::string ext; |
|
189 std::string base = fname; |
|
190 |
|
191 size_t pos = fname.rfind ('.'); |
|
192 |
|
193 if (pos != NPOS) |
|
194 { |
|
195 base = fname.substr (0, pos); |
|
196 ext = fname.substr (pos); |
|
197 |
|
198 if (valid_identifier (base)) |
|
199 { |
|
200 int t = 0; |
|
201 |
|
202 if (ext == ".m") |
|
203 t = load_path::M_FILE; |
|
204 else if (ext == ".oct") |
|
205 t = load_path::OCT_FILE; |
|
206 |
|
207 private_function_map[base] |= t; |
|
208 } |
|
209 } |
|
210 } |
|
211 } |
|
212 else |
|
213 { |
|
214 std::string msg = dir.error (); |
|
215 warning ("load_path: %s: %s", d.c_str (), msg.c_str ()); |
|
216 } |
|
217 } |
|
218 |
|
219 bool |
|
220 load_path::instance_ok (void) |
|
221 { |
|
222 bool retval = true; |
|
223 |
|
224 if (! instance) |
|
225 instance = new load_path (); |
|
226 |
|
227 if (! instance) |
|
228 { |
|
229 ::error ("unable to create load path object!"); |
|
230 |
|
231 retval = false; |
|
232 } |
|
233 |
|
234 return retval; |
|
235 } |
|
236 |
|
237 load_path::const_dir_info_list_iterator |
|
238 load_path::find_dir_info (const std::string& dir) const |
|
239 { |
|
240 const_dir_info_list_iterator retval = dir_info_list.begin (); |
|
241 |
|
242 while (retval != dir_info_list.end ()) |
|
243 { |
|
244 if (retval->dir_name == dir) |
|
245 break; |
|
246 |
|
247 retval++; |
|
248 } |
|
249 |
|
250 return retval; |
|
251 } |
|
252 |
|
253 load_path::dir_info_list_iterator |
|
254 load_path::find_dir_info (const std::string& dir) |
|
255 { |
|
256 dir_info_list_iterator retval = dir_info_list.begin (); |
|
257 |
|
258 while (retval != dir_info_list.end ()) |
|
259 { |
|
260 if (retval->dir_name == dir) |
|
261 break; |
|
262 |
|
263 retval++; |
|
264 } |
|
265 |
|
266 return retval; |
|
267 } |
|
268 |
|
269 bool |
|
270 load_path::contains (const std::string& dir) const |
|
271 { |
|
272 return find_dir_info (dir) != dir_info_list.end (); |
|
273 } |
|
274 |
|
275 void |
|
276 load_path::move (dir_info_list_iterator i, bool at_end) |
|
277 { |
|
278 if (dir_info_list.size () > 1) |
|
279 { |
|
280 dir_info di = *i; |
|
281 |
|
282 dir_info_list.erase (i); |
|
283 |
|
284 if (at_end) |
|
285 dir_info_list.push_back (di); |
|
286 else |
|
287 dir_info_list.push_front (di); |
|
288 |
|
289 std::string dir = di.dir_name; |
|
290 |
|
291 string_vector fcn_files = di.fcn_files; |
|
292 |
|
293 octave_idx_type len = fcn_files.length (); |
|
294 |
|
295 for (octave_idx_type k = 0; k < len; k++) |
|
296 { |
|
297 std::string fname = fcn_files[k]; |
|
298 |
|
299 std::string ext; |
|
300 std::string base = fname; |
|
301 |
|
302 size_t pos = fname.rfind ('.'); |
|
303 |
|
304 if (pos != NPOS) |
|
305 { |
|
306 base = fname.substr (0, pos); |
|
307 ext = fname.substr (pos); |
|
308 } |
|
309 |
|
310 std::list<file_info>& file_info_list = fcn_map[base]; |
|
311 |
|
312 if (file_info_list.size () == 1) |
|
313 continue; |
|
314 else |
|
315 { |
|
316 for (std::list<file_info>::iterator p = file_info_list.begin (); |
|
317 p != file_info_list.end (); |
|
318 p++) |
|
319 { |
|
320 if (p->dir_name == dir) |
|
321 { |
|
322 file_info& fi = *p; |
|
323 |
|
324 file_info_list.erase (p); |
|
325 |
|
326 if (at_end) |
|
327 file_info_list.push_back (fi); |
|
328 else |
|
329 file_info_list.push_front (fi); |
|
330 |
|
331 break; |
|
332 } |
|
333 } |
|
334 } |
|
335 } |
|
336 } |
|
337 } |
|
338 |
|
339 static void |
|
340 maybe_add_path_elts (std::string& path, const std::string& dir) |
|
341 { |
|
342 std::string tpath = genpath (dir); |
|
343 |
|
344 if (! tpath.empty ()) |
|
345 path += dir_path::path_sep_str + tpath; |
|
346 } |
|
347 |
|
348 void |
|
349 load_path::do_initialize (void) |
|
350 { |
|
351 Vsystem_path = ":"; |
|
352 |
|
353 maybe_add_path_elts (Vsystem_path, Vlocal_ver_oct_file_dir); |
|
354 maybe_add_path_elts (Vsystem_path, Vlocal_api_oct_file_dir); |
|
355 maybe_add_path_elts (Vsystem_path, Vlocal_oct_file_dir); |
|
356 maybe_add_path_elts (Vsystem_path, Vlocal_ver_fcn_file_dir); |
|
357 maybe_add_path_elts (Vsystem_path, Vlocal_api_fcn_file_dir); |
|
358 maybe_add_path_elts (Vsystem_path, Vlocal_fcn_file_dir); |
|
359 maybe_add_path_elts (Vsystem_path, Voct_file_dir); |
|
360 maybe_add_path_elts (Vsystem_path, Vfcn_file_dir); |
|
361 |
|
362 std::string tpath = load_path::command_line_path; |
|
363 |
|
364 if (tpath.empty ()) |
|
365 tpath = octave_env::getenv ("OCTAVE_LOADPATH"); |
|
366 |
|
367 std::string xpath = "."; |
|
368 |
|
369 if (! tpath.empty ()) |
|
370 xpath += dir_path::path_sep_str + tpath; |
|
371 |
|
372 if (Vsystem_path != ":") |
|
373 xpath += Vsystem_path; |
|
374 |
|
375 do_set (xpath + ":::"); |
|
376 } |
|
377 |
|
378 void |
|
379 load_path::do_clear (void) |
|
380 { |
|
381 dir_info_list.clear (); |
|
382 fcn_map.clear (); |
|
383 } |
|
384 |
|
385 static std::list<std::string> |
|
386 split_path (const std::string& p) |
|
387 { |
|
388 std::list<std::string> retval; |
|
389 |
|
390 size_t beg = 0; |
|
391 size_t end = p.find (dir_path::path_sep_char); |
|
392 |
|
393 size_t len = p.length (); |
|
394 |
|
395 while (end != NPOS) |
|
396 { |
|
397 std::string elt = p.substr (beg, end-beg); |
|
398 |
|
399 if (! elt.empty ()) |
|
400 retval.push_back (elt); |
|
401 |
|
402 beg = end + 1; |
|
403 |
|
404 if (beg == len) |
|
405 break; |
|
406 |
|
407 end = p.find (dir_path::path_sep_char, beg); |
|
408 } |
|
409 |
|
410 std::string elt = p.substr (beg); |
|
411 |
|
412 if (! elt.empty ()) |
|
413 retval.push_back (elt); |
|
414 |
|
415 return retval; |
|
416 } |
|
417 |
|
418 void |
|
419 load_path::do_set (const std::string& p) |
|
420 { |
|
421 do_clear (); |
|
422 |
|
423 std::list<std::string> elts = split_path (p); |
|
424 |
|
425 // Temporarily disable add hook. |
|
426 |
|
427 unwind_protect_ptr (add_hook); |
|
428 |
|
429 add_hook = 0; |
|
430 |
|
431 for (std::list<std::string>::const_iterator i = elts.begin (); |
|
432 i != elts.end (); |
|
433 i++) |
|
434 do_append (*i); |
|
435 |
|
436 // Restore add hook and execute for all newly added directories. |
|
437 |
|
438 unwind_protect::run (); |
|
439 |
|
440 for (dir_info_list_iterator i = dir_info_list.begin (); |
|
441 i != dir_info_list.end (); |
|
442 i++) |
|
443 { |
|
444 if (add_hook) |
|
445 add_hook (i->dir_name); |
|
446 } |
|
447 } |
|
448 |
|
449 void |
|
450 load_path::do_append (const std::string& dir) |
|
451 { |
|
452 if (! dir.empty ()) |
|
453 { |
|
454 dir_info_list_iterator i = find_dir_info (dir); |
|
455 |
|
456 if (i != dir_info_list.end ()) |
|
457 move (i, true); |
|
458 else |
|
459 { |
|
460 dir_info di (dir); |
|
461 |
|
462 if (! error_state) |
|
463 { |
|
464 dir_info_list.push_back (di); |
|
465 |
|
466 add_to_fcn_map (di, true); |
|
467 |
|
468 if (add_hook) |
|
469 add_hook (dir); |
|
470 } |
|
471 } |
|
472 } |
|
473 } |
|
474 |
|
475 void |
|
476 load_path::do_prepend (const std::string& dir) |
|
477 { |
|
478 if (! dir.empty ()) |
|
479 { |
|
480 dir_info_list_iterator i = find_dir_info (dir); |
|
481 |
|
482 if (i != dir_info_list.end ()) |
|
483 move (i, false); |
|
484 else |
|
485 { |
|
486 dir_info di (dir); |
|
487 |
|
488 if (! error_state) |
|
489 { |
|
490 dir_info_list.push_front (di); |
|
491 |
|
492 add_to_fcn_map (di, false); |
|
493 |
|
494 if (add_hook) |
|
495 add_hook (dir); |
|
496 } |
|
497 } |
|
498 |
|
499 // FIXME -- is there a better way to do this? |
|
500 |
|
501 i = find_dir_info ("."); |
|
502 |
|
503 if (i != dir_info_list.end ()) |
|
504 move (i, false); |
|
505 else |
|
506 panic_impossible (); |
|
507 } |
|
508 } |
|
509 |
|
510 bool |
|
511 load_path::do_remove (const std::string& dir) |
|
512 { |
|
513 bool retval = false; |
|
514 |
|
515 if (! dir.empty ()) |
|
516 { |
|
517 if (dir == ".") |
|
518 warning ("rmpath: can't remove \".\" from path"); |
|
519 |
|
520 dir_info_list_iterator i = find_dir_info (dir); |
|
521 |
|
522 if (i != dir_info_list.end ()) |
|
523 { |
|
524 retval = true; |
|
525 |
|
526 string_vector fcn_files = i->fcn_files; |
|
527 |
|
528 dir_info_list.erase (i); |
|
529 |
|
530 octave_idx_type len = fcn_files.length (); |
|
531 |
|
532 for (octave_idx_type k = 0; k < len; k++) |
|
533 { |
|
534 std::string fname = fcn_files[k]; |
|
535 |
|
536 std::string ext; |
|
537 std::string base = fname; |
|
538 |
|
539 size_t pos = fname.rfind ('.'); |
|
540 |
|
541 if (pos != NPOS) |
|
542 { |
|
543 base = fname.substr (0, pos); |
|
544 ext = fname.substr (pos); |
|
545 } |
|
546 |
|
547 std::list<file_info>& file_info_list = fcn_map[base]; |
|
548 |
|
549 for (std::list<file_info>::iterator p = file_info_list.begin (); |
|
550 p != file_info_list.end (); |
|
551 p++) |
|
552 { |
|
553 if (p->dir_name == dir) |
|
554 { |
|
555 file_info_list.erase (p); |
|
556 |
|
557 if (file_info_list.empty ()) |
|
558 fcn_map.erase (fname); |
|
559 |
|
560 break; |
|
561 } |
|
562 } |
|
563 } |
|
564 |
|
565 if (remove_hook) |
|
566 remove_hook (dir); |
|
567 } |
|
568 } |
|
569 |
|
570 return retval; |
|
571 } |
|
572 |
|
573 void |
|
574 load_path::do_update (void) const |
|
575 { |
|
576 // I don't see a better way to do this because we need to |
|
577 // preserve the correct directory ordering for new files that |
|
578 // have appeared. |
|
579 |
|
580 fcn_map.clear (); |
|
581 |
|
582 for (dir_info_list_iterator p = dir_info_list.begin (); |
|
583 p != dir_info_list.end (); |
|
584 p++) |
|
585 { |
|
586 dir_info& di = *p; |
|
587 |
|
588 di.update (); |
|
589 |
|
590 add_to_fcn_map (di, true); |
|
591 } |
|
592 } |
|
593 |
|
594 std::string |
|
595 load_path::do_find_fcn (const std::string& fcn, int type) const |
|
596 { |
|
597 std::string retval; |
|
598 |
|
599 update (); |
|
600 |
|
601 const_fcn_map_iterator p = fcn_map.find (fcn); |
|
602 |
|
603 if (p != fcn_map.end ()) |
|
604 { |
|
605 const std::list<file_info>& file_info_list = p->second; |
|
606 |
|
607 for (const_file_info_list_iterator i = file_info_list.begin (); |
|
608 i != file_info_list.end (); |
|
609 i++) |
|
610 { |
|
611 const file_info& fi = *i; |
|
612 |
|
613 int t = fi.types; |
|
614 |
|
615 retval = fi.dir_name + file_ops::dir_sep_str + fcn; |
|
616 |
|
617 if (type == load_path::OCT_FILE) |
|
618 { |
|
619 if ((type & t) == load_path::OCT_FILE) |
|
620 { |
|
621 retval += ".oct"; |
|
622 break; |
|
623 } |
|
624 } |
|
625 else if (type == load_path::M_FILE) |
|
626 { |
|
627 if ((type & t) == load_path::M_FILE) |
|
628 { |
|
629 retval += ".m"; |
|
630 break; |
|
631 } |
|
632 } |
|
633 else if (type == (load_path::M_FILE | load_path::OCT_FILE)) |
|
634 { |
|
635 if (t & load_path::OCT_FILE) |
|
636 { |
|
637 retval += ".oct"; |
|
638 break; |
|
639 } |
|
640 else if (t & load_path::M_FILE) |
|
641 { |
|
642 retval += ".m"; |
|
643 break; |
|
644 } |
|
645 } |
|
646 else |
|
647 error ("load_path::do_find_fcn: %s: invalid type code = %d", |
|
648 fcn.c_str (), type); |
|
649 } |
|
650 } |
|
651 |
|
652 return retval; |
|
653 } |
|
654 |
|
655 std::string |
|
656 load_path::do_find_file (const std::string& file) const |
|
657 { |
|
658 std::string retval; |
|
659 |
|
660 if (octave_env::absolute_pathname (file)) |
|
661 { |
|
662 file_stat fs (file); |
|
663 |
|
664 if (fs.exists ()) |
|
665 return file; |
|
666 } |
|
667 |
|
668 std::string dir_name; |
|
669 |
|
670 for (const_dir_info_list_iterator p = dir_info_list.begin (); |
|
671 p != dir_info_list.end (); |
|
672 p++) |
|
673 { |
|
674 string_vector all_files = p->all_files; |
|
675 |
|
676 octave_idx_type len = all_files.length (); |
|
677 |
|
678 for (octave_idx_type i = 0; i < len; i++) |
|
679 { |
|
680 if (all_files[i] == file) |
|
681 { |
|
682 dir_name = p->dir_name; |
|
683 break; |
|
684 } |
|
685 } |
|
686 } |
|
687 |
|
688 if (! dir_name.empty ()) |
|
689 retval = dir_name + file_ops::dir_sep_str + file; |
|
690 |
|
691 return retval; |
|
692 } |
|
693 |
|
694 std::string |
|
695 load_path::do_find_first_of (const string_vector& flist) const |
|
696 { |
|
697 std::string retval; |
|
698 |
|
699 std::string dir_name; |
|
700 std::string file_name; |
|
701 |
|
702 octave_idx_type flen = flist.length (); |
|
703 octave_idx_type rel_flen = 0; |
|
704 |
|
705 string_vector rel_flist (flen); |
|
706 |
|
707 for (octave_idx_type i = 0; i < flen; i++) |
|
708 { |
|
709 if (octave_env::absolute_pathname (flist[i])) |
|
710 { |
|
711 file_stat fs (flist[i]); |
|
712 |
|
713 if (fs.exists ()) |
|
714 return flist[i]; |
|
715 } |
|
716 else |
|
717 rel_flist[rel_flen++] = flist[i]; |
|
718 } |
|
719 |
|
720 rel_flist.resize (rel_flen); |
|
721 |
|
722 for (const_dir_info_list_iterator p = dir_info_list.begin (); |
|
723 p != dir_info_list.end (); |
|
724 p++) |
|
725 { |
|
726 string_vector all_files = p->all_files; |
|
727 |
|
728 octave_idx_type len = all_files.length (); |
|
729 |
|
730 for (octave_idx_type i = 0; i < len; i++) |
|
731 { |
|
732 |
|
733 for (octave_idx_type j = 0; j < rel_flen; j++) |
|
734 { |
|
735 if (all_files[i] == rel_flist[j]) |
|
736 { |
|
737 dir_name = p->dir_name; |
|
738 file_name = rel_flist[j]; |
|
739 break; |
|
740 } |
|
741 } |
|
742 } |
|
743 } |
|
744 |
|
745 if (! dir_name.empty ()) |
|
746 retval = dir_name + file_ops::dir_sep_str + file_name; |
|
747 |
|
748 return retval; |
|
749 } |
|
750 |
|
751 string_vector |
|
752 load_path::do_find_all_first_of (const string_vector& flist) const |
|
753 { |
|
754 std::list<std::string> retlist; |
|
755 |
|
756 std::string dir_name; |
|
757 std::string file_name; |
|
758 |
|
759 octave_idx_type flen = flist.length (); |
|
760 octave_idx_type rel_flen = 0; |
|
761 |
|
762 string_vector rel_flist (flen); |
|
763 |
|
764 for (octave_idx_type i = 0; i < flen; i++) |
|
765 { |
|
766 if (octave_env::absolute_pathname (flist[i])) |
|
767 { |
|
768 file_stat fs (flist[i]); |
|
769 |
|
770 if (fs.exists ()) |
|
771 retlist.push_back (flist[i]); |
|
772 } |
|
773 else |
|
774 rel_flist[rel_flen++] = flist[i]; |
|
775 } |
|
776 |
|
777 rel_flist.resize (rel_flen); |
|
778 |
|
779 for (const_dir_info_list_iterator p = dir_info_list.begin (); |
|
780 p != dir_info_list.end (); |
|
781 p++) |
|
782 { |
|
783 string_vector all_files = p->all_files; |
|
784 |
|
785 octave_idx_type len = all_files.length (); |
|
786 |
|
787 for (octave_idx_type i = 0; i < len; i++) |
|
788 { |
|
789 for (octave_idx_type j = 0; j < rel_flen; j++) |
|
790 { |
|
791 if (all_files[i] == rel_flist[j]) |
|
792 retlist.push_back |
|
793 (p->dir_name + file_ops::dir_sep_str + rel_flist[j]); |
|
794 } |
|
795 } |
|
796 } |
|
797 |
|
798 size_t retsize = retlist.size (); |
|
799 |
|
800 string_vector retval (retsize); |
|
801 |
|
802 for (size_t i = 0; i < retsize; i++) |
|
803 { |
|
804 retval[i] = retlist.front (); |
|
805 |
|
806 retlist.pop_front (); |
|
807 } |
|
808 |
|
809 return retval; |
|
810 } |
|
811 |
|
812 string_vector |
|
813 load_path::do_dirs (void) const |
|
814 { |
|
815 size_t len = dir_info_list.size (); |
|
816 |
|
817 string_vector retval (len); |
|
818 |
|
819 octave_idx_type k = 0; |
|
820 |
|
821 for (const_dir_info_list_iterator i = dir_info_list.begin (); |
|
822 i != dir_info_list.end (); |
|
823 i++) |
|
824 retval[k++] = i->dir_name; |
|
825 |
|
826 return retval; |
|
827 } |
|
828 |
|
829 std::list<std::string> |
|
830 load_path::do_dir_list (void) const |
|
831 { |
|
832 std::list<std::string> retval; |
|
833 |
|
834 for (const_dir_info_list_iterator i = dir_info_list.begin (); |
|
835 i != dir_info_list.end (); |
|
836 i++) |
|
837 retval.push_back (i->dir_name); |
|
838 |
|
839 return retval; |
|
840 } |
|
841 |
|
842 string_vector |
|
843 load_path::do_files (const std::string& dir) const |
|
844 { |
|
845 string_vector retval; |
|
846 |
|
847 const_dir_info_list_iterator i = find_dir_info (dir); |
|
848 |
|
849 if (i != dir_info_list.end ()) |
|
850 retval = i->fcn_files; |
|
851 |
|
852 return retval; |
|
853 } |
|
854 |
|
855 string_vector |
|
856 load_path::do_fcn_names (void) const |
|
857 { |
|
858 size_t len = fcn_map.size (); |
|
859 |
|
860 string_vector retval (len); |
|
861 |
|
862 octave_idx_type count = 0; |
|
863 |
|
864 for (const_fcn_map_iterator p = fcn_map.begin (); |
|
865 p != fcn_map.end (); |
|
866 p++) |
|
867 retval[count++] = p->first; |
|
868 |
|
869 return retval; |
|
870 } |
|
871 |
|
872 std::string |
|
873 load_path::do_path (void) const |
|
874 { |
|
875 std::string xpath; |
|
876 |
|
877 string_vector xdirs = load_path::dirs (); |
|
878 |
|
879 octave_idx_type len = xdirs.length (); |
|
880 |
|
881 if (len > 0) |
|
882 xpath = xdirs[0]; |
|
883 |
|
884 for (octave_idx_type i = 1; i < len; i++) |
|
885 xpath += dir_path::path_sep_str + xdirs[i]; |
|
886 |
|
887 return xpath; |
|
888 } |
|
889 |
|
890 void |
|
891 load_path::do_display (std::ostream& os) const |
|
892 { |
|
893 for (const_dir_info_list_iterator i = dir_info_list.begin (); |
|
894 i != dir_info_list.end (); |
|
895 i++) |
|
896 { |
|
897 string_vector fcn_files = i->fcn_files; |
|
898 |
|
899 if (! fcn_files.empty ()) |
|
900 { |
|
901 os << "\n*** function files in " << i->dir_name << ":\n\n"; |
|
902 |
|
903 fcn_files.list_in_columns (os); |
|
904 } |
|
905 |
|
906 #if defined (DEBUG_LOAD_PATH) |
|
907 |
|
908 const std::map<std::string, int>& private_function_map |
|
909 = i->private_function_map; |
|
910 |
|
911 if (private_function_map.size () > 0) |
|
912 { |
|
913 os << "private:\n"; |
|
914 |
|
915 for (std::map<std::string, int>::const_iterator p = private_function_map.begin (); |
|
916 p != private_function_map.end (); |
|
917 p++) |
|
918 { |
|
919 os << " " << p->first << " ("; |
|
920 |
|
921 bool printed_type = false; |
|
922 |
|
923 int types = p->second; |
|
924 |
|
925 if (types & load_path::OCT_FILE) |
|
926 { |
|
927 os << "oct"; |
|
928 printed_type = true; |
|
929 } |
|
930 |
|
931 if (types & load_path::M_FILE) |
|
932 { |
|
933 if (printed_type) |
|
934 os << "|"; |
|
935 os << "m"; |
|
936 printed_type = true; |
|
937 } |
|
938 |
|
939 os << ")\n"; |
|
940 } |
|
941 |
|
942 os << "\n"; |
|
943 } |
|
944 #endif |
|
945 } |
|
946 |
|
947 #if defined (DEBUG_LOAD_PATH) |
|
948 |
|
949 for (const_fcn_map_iterator i = fcn_map.begin (); |
|
950 i != fcn_map.end (); |
|
951 i++) |
|
952 { |
|
953 os << i->first << ":\n"; |
|
954 |
|
955 const std::list<file_info>& file_info_list = i->second; |
|
956 |
|
957 for (const_file_info_list_iterator p = file_info_list.begin (); |
|
958 p != file_info_list.end (); |
|
959 p++) |
|
960 { |
|
961 os << " " << p->dir_name << " ("; |
|
962 |
|
963 bool printed_type = false; |
|
964 |
|
965 if (p->types & load_path::OCT_FILE) |
|
966 { |
|
967 os << "oct"; |
|
968 printed_type = true; |
|
969 } |
|
970 |
|
971 if (p->types & load_path::M_FILE) |
|
972 { |
|
973 if (printed_type) |
|
974 os << "|"; |
|
975 os << "m"; |
|
976 printed_type = true; |
|
977 } |
|
978 |
|
979 os << ")\n"; |
|
980 } |
|
981 } |
|
982 |
|
983 os << "\n"; |
|
984 |
|
985 #endif |
|
986 } |
|
987 |
|
988 void |
|
989 load_path::add_to_fcn_map (const dir_info& di, bool at_end) const |
|
990 { |
|
991 std::string dir_name = di.dir_name; |
|
992 |
|
993 string_vector fcn_files = di.fcn_files; |
|
994 |
|
995 octave_idx_type len = fcn_files.length (); |
|
996 |
|
997 for (octave_idx_type i = 0; i < len; i++) |
|
998 { |
|
999 std::string fname = fcn_files[i]; |
|
1000 |
|
1001 std::string ext; |
|
1002 std::string base = fname; |
|
1003 |
|
1004 size_t pos = fname.rfind ('.'); |
|
1005 |
|
1006 if (pos != NPOS) |
|
1007 { |
|
1008 base = fname.substr (0, pos); |
|
1009 ext = fname.substr (pos); |
|
1010 } |
|
1011 |
|
1012 std::list<file_info>& file_info_list = fcn_map[base]; |
|
1013 |
|
1014 file_info_list_iterator p = file_info_list.begin (); |
|
1015 |
|
1016 while (p != file_info_list.end ()) |
|
1017 { |
|
1018 if (p->dir_name == dir_name) |
|
1019 break; |
|
1020 |
|
1021 p++; |
|
1022 } |
|
1023 |
|
1024 int t = 0; |
|
1025 if (ext == ".m") |
|
1026 t = load_path::M_FILE; |
|
1027 else if (ext == ".oct") |
|
1028 t = load_path::OCT_FILE; |
|
1029 |
|
1030 if (p == file_info_list.end ()) |
|
1031 { |
|
1032 file_info fi (dir_name, t); |
|
1033 |
|
1034 if (at_end) |
|
1035 file_info_list.push_back (fi); |
|
1036 else |
|
1037 file_info_list.push_front (fi); |
|
1038 } |
|
1039 else |
|
1040 { |
|
1041 file_info& fi = *p; |
|
1042 |
|
1043 fi.types |= t; |
|
1044 } |
|
1045 } |
|
1046 } |
|
1047 |
|
1048 std::string |
|
1049 genpath (const std::string& dirname, const string_vector& skip) |
|
1050 { |
|
1051 std::string retval; |
|
1052 |
|
1053 std::string full_dirname = file_ops::tilde_expand (dirname); |
|
1054 |
|
1055 dir_entry dir (full_dirname); |
|
1056 |
|
1057 if (dir) |
|
1058 { |
|
1059 retval = dirname; |
|
1060 |
|
1061 string_vector dirlist = dir.read (); |
|
1062 |
|
1063 octave_idx_type len = dirlist.length (); |
|
1064 |
|
1065 for (octave_idx_type i = 0; i < len; i++) |
|
1066 { |
|
1067 std::string elt = dirlist[i]; |
|
1068 |
|
1069 // FIXME -- the caller should be able to specify the list of |
|
1070 // directories to skip in addition to "." and "..". |
|
1071 |
|
1072 bool skip_p = (elt == "." || elt == ".."); |
|
1073 |
|
1074 if (! skip_p) |
|
1075 { |
|
1076 for (octave_idx_type j = 0; j < skip.length (); j++) |
|
1077 { |
|
1078 skip_p = (elt == skip[j]); |
|
1079 if (skip_p) |
|
1080 break; |
|
1081 } |
|
1082 |
|
1083 if (! skip_p) |
|
1084 { |
|
1085 std::string nm = full_dirname + file_ops::dir_sep_str + elt; |
|
1086 |
|
1087 file_stat fs (nm); |
|
1088 |
|
1089 if (fs && fs.is_dir ()) |
|
1090 retval += dir_path::path_sep_str + genpath (nm); |
|
1091 } |
|
1092 } |
|
1093 } |
|
1094 } |
|
1095 |
|
1096 return retval; |
|
1097 } |
|
1098 |
|
1099 static void |
|
1100 execute_pkg_add_or_del (const std::string& dir, |
|
1101 const std::string& script_file) |
|
1102 { |
|
1103 if (! octave_interpreter_ready) |
|
1104 return; |
|
1105 |
|
1106 unwind_protect::begin_frame ("execute_pkg_add_or_del"); |
|
1107 |
|
1108 unwind_protect_bool (input_from_startup_file); |
|
1109 |
|
1110 input_from_startup_file = true; |
|
1111 |
|
1112 std::string file = dir + file_ops::dir_sep_str + script_file; |
|
1113 |
|
1114 file_stat fs = file_stat (file); |
|
1115 |
|
1116 if (fs.exists ()) |
|
1117 source_file (file); |
|
1118 |
|
1119 unwind_protect::run_frame ("execute_pkg_add_or_del"); |
|
1120 } |
|
1121 |
|
1122 void |
|
1123 execute_pkg_add (const std::string& dir) |
|
1124 { |
|
1125 execute_pkg_add_or_del (dir, "PKG_ADD"); |
|
1126 } |
|
1127 |
|
1128 void |
|
1129 execute_pkg_del (const std::string& dir) |
|
1130 { |
|
1131 execute_pkg_add_or_del (dir, "PKG_DEL"); |
|
1132 } |
|
1133 |
|
1134 DEFUN (genpath, args, , |
|
1135 "-*- texinfo -*-\n\ |
|
1136 @deftypefn {Built-in Function} {} genpath (@var{dir})\n\ |
|
1137 Return a path constructed from @var{dir} and all its subdiretories.\n\ |
|
1138 @end deftypefn") |
|
1139 { |
|
1140 octave_value retval; |
|
1141 |
|
1142 if (args.length () == 1) |
|
1143 { |
|
1144 std::string dirname = args(0).string_value (); |
|
1145 |
|
1146 if (! error_state) |
|
1147 retval = genpath (dirname); |
|
1148 else |
|
1149 error ("genpath: expecting argument to be a character string"); |
|
1150 } |
|
1151 else |
|
1152 print_usage (); |
|
1153 |
|
1154 return retval; |
|
1155 } |
|
1156 |
|
1157 DEFUN (rehash, , , |
|
1158 "-*- texinfo -*-\n\ |
|
1159 @deftypefn {Built-in Function} {} rehash ()\n\ |
|
1160 Reinitialize Octave's @code{LOADPATH} directory cache.\n\ |
|
1161 @end deftypefn") |
|
1162 { |
|
1163 octave_value_list retval; |
|
1164 |
|
1165 load_path::update (); |
|
1166 |
|
1167 // FIXME -- maybe we should rename this variable since it is being |
|
1168 // used for more than keeping track of the prompt time. |
|
1169 |
|
1170 // This will force updated functions to be found. |
|
1171 Vlast_prompt_time.stamp (); |
|
1172 |
|
1173 return retval; |
|
1174 } |
|
1175 |
|
1176 DEFUN (pathdef, , , |
|
1177 "-*- texinfo -*-\n\ |
|
1178 @deftypefn {Built-in Function} {@var{val} =} pathdef ()\n\ |
|
1179 Return the default list of directories in which to search for function\n\ |
|
1180 files.\n\ |
|
1181 @seealso{path, addpath, rmpath, genpath, savepath, pathsep}\n\ |
|
1182 @end deftypefn") |
|
1183 { |
|
1184 return octave_value (Vsystem_path); |
|
1185 } |
|
1186 |
|
1187 DEFUN (path, args, nargout, |
|
1188 "-*- texinfo -*-\n\ |
|
1189 @deftypefn {Function File} {} path (@dots{})\n\ |
|
1190 Modify or display Octave's @code{LOADPATH}.\n\ |
|
1191 \n\ |
|
1192 If @var{nargin} and @var{nargout} are zero, display the elements of\n\ |
|
1193 Octave's @code{LOADPATH} in an easy to read format.\n\ |
|
1194 \n\ |
|
1195 If @var{nargin} is zero and nargout is greater than zero, return the\n\ |
|
1196 current value of @code{LOADPATH}.\n\ |
|
1197 \n\ |
|
1198 If @var{nargin} is greater than zero, concatenate the arguments,\n\ |
|
1199 separating them with @code{pathsep()}. Set the internal search path\n\ |
|
1200 to the result and return it.\n\ |
|
1201 \n\ |
|
1202 No checks are made for duplicate elements.\n\ |
|
1203 @seealso{addpath, rmpath, genpath, pathdef, savepath, pathsep}\n\ |
|
1204 @end deftypefn") |
|
1205 { |
|
1206 octave_value retval; |
|
1207 |
|
1208 int argc = args.length () + 1; |
|
1209 |
|
1210 string_vector argv = args.make_argv ("path"); |
|
1211 |
|
1212 if (! error_state) |
|
1213 { |
|
1214 if (argc > 1) |
|
1215 { |
|
1216 std::string path = argv[1]; |
|
1217 |
|
1218 for (int i = 2; i < argc; i++) |
|
1219 path += dir_path::path_sep_str; |
|
1220 |
|
1221 size_t plen = path.length (); |
|
1222 |
|
1223 if (! ((plen == 1 && path[0] == ':') |
|
1224 || (plen > 1 |
|
1225 && path.substr (0, 2) == ("." + dir_path::path_sep_str)))) |
|
1226 path = "." + dir_path::path_sep_str + path; |
|
1227 |
|
1228 load_path::set (path); |
|
1229 } |
|
1230 |
|
1231 if (nargout > 0) |
|
1232 retval = load_path::path (); |
|
1233 else if (argc == 1 && nargout == 0) |
|
1234 { |
|
1235 octave_stdout << "\nOctave's search path contains the following directories:\n\n"; |
|
1236 |
|
1237 string_vector dirs = load_path::dirs (); |
|
1238 |
|
1239 dirs.list_in_columns (octave_stdout); |
|
1240 |
|
1241 octave_stdout << "\n"; |
|
1242 } |
|
1243 } |
|
1244 |
|
1245 return retval; |
|
1246 } |
|
1247 |
|
1248 DEFCMD (addpath, args, nargout, |
|
1249 "-*- texinfo -*-\n\ |
|
1250 @deftypefn {Function File} {} addpath (@var{dir1}, @dots{})\n\ |
|
1251 @deftypefnx {Function File} {} addpath (@var{dir1}, @dots{}, @var{option})\n\ |
|
1252 Add @var{dir1}, @dots{} to the current function search path. If\n\ |
|
1253 @var{option} is @samp{\"-begin\"} or 0 (the default), prepend the\n\ |
|
1254 directory name to the current path. If @var{option} is @samp{\"-end\"}\n\ |
|
1255 or 1, append the directory name to the current path.\n\ |
|
1256 Directories added to the path must exist.\n\ |
|
1257 @seealso{path, rmpath, genpath, pathdef, savepath, pathsep}\n\ |
|
1258 @end deftypefn") |
|
1259 { |
|
1260 octave_value retval; |
|
1261 |
|
1262 // Originally written by Bill Denney and Etienne Grossman. Heavily |
|
1263 // modified and translated to C++ by jwe. |
|
1264 |
|
1265 if (nargout > 0) |
|
1266 retval = load_path::path (); |
|
1267 |
|
1268 int nargin = args.length (); |
|
1269 |
|
1270 if (nargin > 0) |
|
1271 { |
|
1272 bool append = false; |
|
1273 |
|
1274 octave_value option_arg = args(nargin-1); |
|
1275 |
|
1276 if (option_arg.is_string ()) |
|
1277 { |
|
1278 std::string option = option_arg.string_value (); |
|
1279 |
|
1280 if (option == "-end") |
|
1281 { |
|
1282 append = true; |
|
1283 nargin--; |
|
1284 } |
|
1285 else if (option == "-begin") |
|
1286 nargin--; |
|
1287 } |
|
1288 else if (option_arg.is_numeric_type ()) |
|
1289 { |
|
1290 int val = option_arg.int_value (); |
|
1291 |
|
1292 if (! error_state) |
|
1293 { |
|
1294 if (val == 0) |
|
1295 append = false; |
|
1296 else if (val == 1) |
|
1297 append = true; |
|
1298 else |
|
1299 { |
|
1300 error ("addpath: expecting final argument to be 1 or 0"); |
|
1301 return retval; |
|
1302 } |
|
1303 } |
|
1304 else |
|
1305 { |
|
1306 error ("addpath: expecting final argument to be 1 or 0"); |
|
1307 return retval; |
|
1308 } |
|
1309 } |
|
1310 |
|
1311 std::list<std::string> xpath = load_path::dir_list (); |
|
1312 |
|
1313 // Strip "." for now. Calling path to set the path will restore it. |
|
1314 |
|
1315 xpath.remove ("."); |
|
1316 |
|
1317 for (int i = 0; i < nargin; i++) |
|
1318 { |
|
1319 std::string arg = args(i).string_value (); |
|
1320 |
|
1321 if (! error_state) |
|
1322 { |
|
1323 std::list<std::string> dir_elts = split_path (arg); |
|
1324 |
|
1325 for (std::list<std::string>::const_iterator p = dir_elts.begin (); |
|
1326 p != dir_elts.end (); |
|
1327 p++) |
|
1328 { |
|
1329 std::string dir = *p; |
|
1330 |
|
1331 //dir = regexprep (dir_elts{j}, "//+", "/"); |
|
1332 //dir = regexprep (dir, "/$", ""); |
|
1333 |
|
1334 if (dir == "." && append) |
|
1335 warning ("addpath: \".\" is always first in the path"); |
|
1336 |
|
1337 file_stat fs (dir); |
|
1338 |
|
1339 if (fs) |
|
1340 { |
|
1341 if (fs.is_dir ()) |
|
1342 { |
|
1343 if (append) |
|
1344 load_path::append (dir); |
|
1345 else |
|
1346 load_path::prepend (dir); |
|
1347 } |
|
1348 else |
|
1349 warning ("addpath: %s: not a directory", dir.c_str ()); |
|
1350 } |
|
1351 else |
|
1352 { |
|
1353 std::string msg = fs.error (); |
|
1354 warning ("addpath: %s: %s", dir.c_str (), msg.c_str ()); |
|
1355 } |
|
1356 } |
|
1357 } |
|
1358 else |
|
1359 error ("addpath: expecting all args to be character strings"); |
|
1360 } |
|
1361 } |
|
1362 else |
|
1363 print_usage (); |
|
1364 |
|
1365 return retval; |
|
1366 } |
|
1367 |
|
1368 DEFCMD (rmpath, args, nargout, |
|
1369 "-*- texinfo -*-\n\ |
|
1370 @deftypefn {Function File} {} rmpath (@var{dir1}, @dots{})\n\ |
|
1371 Remove @var{dir1}, @dots{} from the current function search path.\n\ |
|
1372 \n\ |
|
1373 @seealso{path, addpath, genpath, pathdef, savepath, pathsep}\n\ |
|
1374 @end deftypefn") |
|
1375 { |
|
1376 // Originally by Etienne Grossmann. Heavily modified and translated |
|
1377 // to C++ by jwe. |
|
1378 |
|
1379 octave_value retval; |
|
1380 |
|
1381 if (nargout > 0) |
|
1382 retval = load_path::path (); |
|
1383 |
|
1384 int nargin = args.length (); |
|
1385 |
|
1386 if (nargin > 0) |
|
1387 { |
|
1388 std::list<std::string> xpath = load_path::dir_list (); |
|
1389 |
|
1390 for (int i = 0; i < nargin; i++) |
|
1391 { |
|
1392 std::string arg = args(i).string_value (); |
|
1393 |
|
1394 if (! error_state) |
|
1395 { |
|
1396 std::list<std::string> dir_elts = split_path (arg); |
|
1397 |
|
1398 for (std::list<std::string>::const_iterator p = dir_elts.begin (); |
|
1399 p != dir_elts.end (); |
|
1400 p++) |
|
1401 { |
|
1402 std::string dir = *p; |
|
1403 |
|
1404 //dir = regexprep (dir_elts{j}, "//+", "/"); |
|
1405 //dir = regexprep (dir, "/$", ""); |
|
1406 |
|
1407 if (! load_path::remove (dir)) |
|
1408 warning ("rmpath: %s: not found", dir.c_str ()); |
|
1409 } |
|
1410 } |
|
1411 else |
|
1412 error ("addpath: expecting all args to be character strings"); |
|
1413 } |
|
1414 } |
|
1415 else |
|
1416 print_usage (); |
|
1417 |
|
1418 return retval; |
|
1419 } |
|
1420 |
|
1421 /* |
|
1422 ;;; Local Variables: *** |
|
1423 ;;; mode: C++ *** |
|
1424 ;;; End: *** |
|
1425 */ |