diff options
author | Peter D'Hoye <peter.dhoye@gmail.com> | 2009-05-24 21:28:16 +0000 |
---|---|---|
committer | Peter D'Hoye <peter.dhoye@gmail.com> | 2009-05-24 21:28:16 +0000 |
commit | 526b5580dabbfed7cfe5439dc3a90ec727f563c2 (patch) | |
tree | 22b1af92348785daad16714ee5e2b633017e0e48 /apps/plugins/pdbox/PDa/src/s_path.c | |
parent | 4f2dfcc01b260d946044ef2b6af5fe36cb772c8d (diff) | |
download | rockbox-526b5580dabbfed7cfe5439dc3a90ec727f563c2.tar.gz rockbox-526b5580dabbfed7cfe5439dc3a90ec727f563c2.zip |
Cut the files in half and it might work better (note to self: check your tree is really clean before patching)
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21070 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/pdbox/PDa/src/s_path.c')
-rw-r--r-- | apps/plugins/pdbox/PDa/src/s_path.c | 411 |
1 files changed, 0 insertions, 411 deletions
diff --git a/apps/plugins/pdbox/PDa/src/s_path.c b/apps/plugins/pdbox/PDa/src/s_path.c index 26b8dfda99..f40f1f6e49 100644 --- a/apps/plugins/pdbox/PDa/src/s_path.c +++ b/apps/plugins/pdbox/PDa/src/s_path.c | |||
@@ -407,414 +407,3 @@ void glob_path_dialog(t_pd *dummy, t_symbol *s, int argc, t_atom *argv) | |||
407 | } | 407 | } |
408 | } | 408 | } |
409 | 409 | ||
410 | |||
411 | /* Copyright (c) 1999 Guenter Geiger and others. | ||
412 | * For information on usage and redistribution, and for a DISCLAIMER OF ALL | ||
413 | * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */ | ||
414 | |||
415 | /* | ||
416 | * This file implements the loader for linux, which includes | ||
417 | * a little bit of path handling. | ||
418 | * | ||
419 | * Generalized by MSP to provide an open_via_path function | ||
420 | * and lists of files for all purposes. | ||
421 | */ | ||
422 | |||
423 | /* #define DEBUG(x) x */ | ||
424 | #define DEBUG(x) | ||
425 | void readsf_banana( void); /* debugging */ | ||
426 | |||
427 | #include <stdlib.h> | ||
428 | #ifdef UNIX | ||
429 | #include <unistd.h> | ||
430 | #include <sys/stat.h> | ||
431 | #endif | ||
432 | #ifdef MSW | ||
433 | #include <io.h> | ||
434 | #endif | ||
435 | |||
436 | #include <string.h> | ||
437 | #include "m_pd.h" | ||
438 | #include "m_imp.h" | ||
439 | #include "s_stuff.h" | ||
440 | #include <stdio.h> | ||
441 | #include <fcntl.h> | ||
442 | |||
443 | static t_namelist *pd_path, *pd_helppath; | ||
444 | |||
445 | /* Utility functions */ | ||
446 | |||
447 | /* copy until delimiter and return position after delimiter in string */ | ||
448 | /* if it was the last substring, return NULL */ | ||
449 | |||
450 | static const char* strtokcpy(char *to, const char *from, int delim) | ||
451 | { | ||
452 | int size = 0; | ||
453 | |||
454 | while (from[size] != (char)delim && from[size] != '\0') | ||
455 | size++; | ||
456 | |||
457 | strncpy(to,from,size); | ||
458 | to[size] = '\0'; | ||
459 | if (from[size] == '\0') return NULL; | ||
460 | if (size) return from+size+1; | ||
461 | else return NULL; | ||
462 | } | ||
463 | |||
464 | /* add a colon-separated list of names to a namelist */ | ||
465 | |||
466 | #ifdef MSW | ||
467 | #define SEPARATOR ';' | ||
468 | #else | ||
469 | #define SEPARATOR ':' | ||
470 | #endif | ||
471 | |||
472 | static t_namelist *namelist_doappend(t_namelist *listwas, const char *s) | ||
473 | { | ||
474 | t_namelist *nl = listwas, *rtn = listwas, *nl2; | ||
475 | nl2 = (t_namelist *)(getbytes(sizeof(*nl))); | ||
476 | nl2->nl_next = 0; | ||
477 | nl2->nl_string = (char *)getbytes(strlen(s) + 1); | ||
478 | strcpy(nl2->nl_string, s); | ||
479 | sys_unbashfilename(nl2->nl_string, nl2->nl_string); | ||
480 | if (!nl) | ||
481 | nl = rtn = nl2; | ||
482 | else | ||
483 | { | ||
484 | while (nl->nl_next) | ||
485 | nl = nl->nl_next; | ||
486 | nl->nl_next = nl2; | ||
487 | } | ||
488 | return (rtn); | ||
489 | |||
490 | } | ||
491 | |||
492 | t_namelist *namelist_append(t_namelist *listwas, const char *s) | ||
493 | { | ||
494 | const char *npos; | ||
495 | char temp[MAXPDSTRING]; | ||
496 | t_namelist *nl = listwas, *rtn = listwas; | ||
497 | |||
498 | npos = s; | ||
499 | do | ||
500 | { | ||
501 | npos = strtokcpy(temp, npos, SEPARATOR); | ||
502 | if (! *temp) continue; | ||
503 | nl = namelist_doappend(nl, temp); | ||
504 | } | ||
505 | while (npos); | ||
506 | return (nl); | ||
507 | } | ||
508 | |||
509 | void namelist_free(t_namelist *listwas) | ||
510 | { | ||
511 | t_namelist *nl, *nl2; | ||
512 | for (nl = listwas; nl; nl = nl2) | ||
513 | { | ||
514 | nl2 = nl->nl_next; | ||
515 | t_freebytes(nl->nl_string, strlen(nl->nl_string) + 1); | ||
516 | t_freebytes(nl, sizeof(*nl)); | ||
517 | } | ||
518 | } | ||
519 | |||
520 | void sys_addpath(const char *p) | ||
521 | { | ||
522 | pd_path = namelist_append(pd_path, p); | ||
523 | } | ||
524 | |||
525 | void sys_addhelppath(const char *p) | ||
526 | { | ||
527 | pd_helppath = namelist_append(pd_helppath, p); | ||
528 | } | ||
529 | |||
530 | #ifdef MSW | ||
531 | #define MSWOPENFLAG(bin) (bin ? _O_BINARY : _O_TEXT) | ||
532 | #else | ||
533 | #define MSWOPENFLAG(bin) 0 | ||
534 | #endif | ||
535 | |||
536 | /* search for a file in a specified directory, then along the globally | ||
537 | defined search path, using ext as filename extension. Exception: | ||
538 | if the 'name' starts with a slash or a letter, colon, and slash in MSW, | ||
539 | there is no search and instead we just try to open the file literally. The | ||
540 | fd is returned, the directory ends up in the "dirresult" which must be at | ||
541 | least "size" bytes. "nameresult" is set to point to the filename, which | ||
542 | ends up in the same buffer as dirresult. */ | ||
543 | |||
544 | int open_via_path(const char *dir, const char *name, const char* ext, | ||
545 | char *dirresult, char **nameresult, unsigned int size, int bin) | ||
546 | { | ||
547 | t_namelist *nl, thislist; | ||
548 | int fd = -1; | ||
549 | char listbuf[MAXPDSTRING]; | ||
550 | |||
551 | if (name[0] == '/' | ||
552 | #ifdef MSW | ||
553 | || (name[1] == ':' && name[2] == '/') | ||
554 | #endif | ||
555 | ) | ||
556 | { | ||
557 | thislist.nl_next = 0; | ||
558 | thislist.nl_string = listbuf; | ||
559 | listbuf[0] = 0; | ||
560 | } | ||
561 | else | ||
562 | { | ||
563 | thislist.nl_string = listbuf; | ||
564 | thislist.nl_next = pd_path; | ||
565 | strncpy(listbuf, dir, MAXPDSTRING); | ||
566 | listbuf[MAXPDSTRING-1] = 0; | ||
567 | sys_unbashfilename(listbuf, listbuf); | ||
568 | } | ||
569 | |||
570 | for (nl = &thislist; nl; nl = nl->nl_next) | ||
571 | { | ||
572 | if (strlen(nl->nl_string) + strlen(name) + strlen(ext) + 4 > | ||
573 | size) | ||
574 | continue; | ||
575 | strcpy(dirresult, nl->nl_string); | ||
576 | if (*dirresult && dirresult[strlen(dirresult)-1] != '/') | ||
577 | strcat(dirresult, "/"); | ||
578 | strcat(dirresult, name); | ||
579 | strcat(dirresult, ext); | ||
580 | sys_bashfilename(dirresult, dirresult); | ||
581 | |||
582 | DEBUG(post("looking for %s",dirresult)); | ||
583 | /* see if we can open the file for reading */ | ||
584 | if ((fd=open(dirresult,O_RDONLY | MSWOPENFLAG(bin))) >= 0) | ||
585 | { | ||
586 | /* in UNIX, further check that it's not a directory */ | ||
587 | #ifdef UNIX | ||
588 | struct stat statbuf; | ||
589 | int ok = ((fstat(fd, &statbuf) >= 0) && | ||
590 | !S_ISDIR(statbuf.st_mode)); | ||
591 | if (!ok) | ||
592 | { | ||
593 | if (sys_verbose) post("tried %s; stat failed or directory", | ||
594 | dirresult); | ||
595 | close (fd); | ||
596 | fd = -1; | ||
597 | } | ||
598 | else | ||
599 | #endif | ||
600 | { | ||
601 | char *slash; | ||
602 | if (sys_verbose) post("tried %s and succeeded", dirresult); | ||
603 | sys_unbashfilename(dirresult, dirresult); | ||
604 | slash = strrchr(dirresult, '/'); | ||
605 | if (slash) | ||
606 | { | ||
607 | *slash = 0; | ||
608 | *nameresult = slash + 1; | ||
609 | } | ||
610 | else *nameresult = dirresult; | ||
611 | |||
612 | return (fd); | ||
613 | } | ||
614 | } | ||
615 | else | ||
616 | { | ||
617 | if (sys_verbose) post("tried %s and failed", dirresult); | ||
618 | } | ||
619 | } | ||
620 | *dirresult = 0; | ||
621 | *nameresult = dirresult; | ||
622 | return (-1); | ||
623 | } | ||
624 | |||
625 | static int do_open_via_helppath(const char *realname, t_namelist *listp) | ||
626 | { | ||
627 | t_namelist *nl; | ||
628 | int fd = -1; | ||
629 | char dirresult[MAXPDSTRING], realdir[MAXPDSTRING]; | ||
630 | for (nl = listp; nl; nl = nl->nl_next) | ||
631 | { | ||
632 | strcpy(dirresult, nl->nl_string); | ||
633 | strcpy(realdir, dirresult); | ||
634 | if (*dirresult && dirresult[strlen(dirresult)-1] != '/') | ||
635 | strcat(dirresult, "/"); | ||
636 | strcat(dirresult, realname); | ||
637 | sys_bashfilename(dirresult, dirresult); | ||
638 | |||
639 | DEBUG(post("looking for %s",dirresult)); | ||
640 | /* see if we can open the file for reading */ | ||
641 | if ((fd=open(dirresult,O_RDONLY | MSWOPENFLAG(0))) >= 0) | ||
642 | { | ||
643 | /* in UNIX, further check that it's not a directory */ | ||
644 | #ifdef UNIX | ||
645 | struct stat statbuf; | ||
646 | int ok = ((fstat(fd, &statbuf) >= 0) && | ||
647 | !S_ISDIR(statbuf.st_mode)); | ||
648 | if (!ok) | ||
649 | { | ||
650 | if (sys_verbose) post("tried %s; stat failed or directory", | ||
651 | dirresult); | ||
652 | close (fd); | ||
653 | fd = -1; | ||
654 | } | ||
655 | else | ||
656 | #endif | ||
657 | { | ||
658 | char *slash; | ||
659 | if (sys_verbose) post("tried %s and succeeded", dirresult); | ||
660 | sys_unbashfilename(dirresult, dirresult); | ||
661 | close (fd); | ||
662 | glob_evalfile(0, gensym((char*)realname), gensym(realdir)); | ||
663 | return (1); | ||
664 | } | ||
665 | } | ||
666 | else | ||
667 | { | ||
668 | if (sys_verbose) post("tried %s and failed", dirresult); | ||
669 | } | ||
670 | } | ||
671 | return (0); | ||
672 | } | ||
673 | |||
674 | /* LATER make this use open_via_path above. We expect the ".pd" | ||
675 | suffix here, even though we have to tear it back off for one of the | ||
676 | search attempts. */ | ||
677 | void open_via_helppath(const char *name, const char *dir) | ||
678 | { | ||
679 | t_namelist *nl, thislist, *listp; | ||
680 | int fd = -1; | ||
681 | char dirbuf2[MAXPDSTRING], realname[MAXPDSTRING]; | ||
682 | |||
683 | /* if directory is supplied, put it at head of search list. */ | ||
684 | if (*dir) | ||
685 | { | ||
686 | thislist.nl_string = dirbuf2; | ||
687 | thislist.nl_next = pd_helppath; | ||
688 | strncpy(dirbuf2, dir, MAXPDSTRING); | ||
689 | dirbuf2[MAXPDSTRING-1] = 0; | ||
690 | sys_unbashfilename(dirbuf2, dirbuf2); | ||
691 | listp = &thislist; | ||
692 | } | ||
693 | else listp = pd_helppath; | ||
694 | /* 1. "objectname-help.pd" */ | ||
695 | strncpy(realname, name, MAXPDSTRING-10); | ||
696 | realname[MAXPDSTRING-10] = 0; | ||
697 | if (strlen(realname) > 3 && !strcmp(realname+strlen(realname)-3, ".pd")) | ||
698 | realname[strlen(realname)-3] = 0; | ||
699 | strcat(realname, "-help.pd"); | ||
700 | if (do_open_via_helppath(realname, listp)) | ||
701 | return; | ||
702 | /* 2. "help-objectname.pd" */ | ||
703 | strcpy(realname, "help-"); | ||
704 | strncat(realname, name, MAXPDSTRING-10); | ||
705 | realname[MAXPDSTRING-1] = 0; | ||
706 | if (do_open_via_helppath(realname, listp)) | ||
707 | return; | ||
708 | /* 3. "objectname.pd" */ | ||
709 | if (do_open_via_helppath(name, listp)) | ||
710 | return; | ||
711 | post("sorry, couldn't find help patch for \"%s\"", name); | ||
712 | return; | ||
713 | } | ||
714 | |||
715 | |||
716 | /* Startup file reading for linux and MACOSX. This should be replaced by | ||
717 | a better mechanism. This should be integrated with the audio, MIDI, and | ||
718 | path dialog system. */ | ||
719 | |||
720 | #ifdef UNIX | ||
721 | |||
722 | #define STARTUPNAME ".pdrc" | ||
723 | #define NUMARGS 1000 | ||
724 | |||
725 | int sys_argparse(int argc, char **argv); | ||
726 | |||
727 | int sys_rcfile(void) | ||
728 | { | ||
729 | FILE* file; | ||
730 | int i; | ||
731 | int k; | ||
732 | int rcargc; | ||
733 | char* rcargv[NUMARGS]; | ||
734 | char* buffer; | ||
735 | char fname[MAXPDSTRING], buf[1000], *home = getenv("HOME"); | ||
736 | |||
737 | /* parse a startup file */ | ||
738 | |||
739 | *fname = '\0'; | ||
740 | |||
741 | strncat(fname, home? home : ".", MAXPDSTRING-10); | ||
742 | strcat(fname, "/"); | ||
743 | |||
744 | strcat(fname, STARTUPNAME); | ||
745 | |||
746 | if (!(file = fopen(fname, "r"))) | ||
747 | return 1; | ||
748 | |||
749 | post("reading startup file: %s", fname); | ||
750 | |||
751 | rcargv[0] = "."; /* this no longer matters to sys_argparse() */ | ||
752 | |||
753 | for (i = 1; i < NUMARGS-1; i++) | ||
754 | { | ||
755 | if (fscanf(file, "%999s", buf) < 0) | ||
756 | break; | ||
757 | buf[1000] = 0; | ||
758 | if (!(rcargv[i] = malloc(strlen(buf) + 1))) | ||
759 | return (1); | ||
760 | strcpy(rcargv[i], buf); | ||
761 | } | ||
762 | if (i >= NUMARGS-1) | ||
763 | fprintf(stderr, "startup file too long; extra args dropped\n"); | ||
764 | rcargv[i] = 0; | ||
765 | |||
766 | rcargc = i; | ||
767 | |||
768 | /* parse the options */ | ||
769 | |||
770 | fclose(file); | ||
771 | if (sys_verbose) | ||
772 | { | ||
773 | if (rcargv) | ||
774 | { | ||
775 | post("startup args from RC file:"); | ||
776 | for (i = 1; i < rcargc; i++) | ||
777 | post("%s", rcargv[i]); | ||
778 | } | ||
779 | else post("no RC file arguments found"); | ||
780 | } | ||
781 | if (sys_argparse(rcargc, rcargv)) | ||
782 | { | ||
783 | post("error parsing RC arguments"); | ||
784 | return (1); | ||
785 | } | ||
786 | return (0); | ||
787 | } | ||
788 | #endif /* UNIX */ | ||
789 | |||
790 | /* start an audio settings dialog window */ | ||
791 | void glob_start_path_dialog(t_pd *dummy, t_floatarg flongform) | ||
792 | { | ||
793 | char buf[MAXPDSTRING]; | ||
794 | int i; | ||
795 | t_namelist *nl; | ||
796 | |||
797 | for (nl = pd_path, i = 0; nl && i < 10; nl = nl->nl_next, i++) | ||
798 | sys_vgui("pd_set pd_path%d \"%s\"\n", i, nl->nl_string); | ||
799 | for (; i < 10; i++) | ||
800 | sys_vgui("pd_set pd_path%d \"\"\n", i); | ||
801 | |||
802 | sprintf(buf, "pdtk_path_dialog %%s\n"); | ||
803 | gfxstub_new(&glob_pdobject, glob_start_path_dialog, buf); | ||
804 | } | ||
805 | |||
806 | /* new values from dialog window */ | ||
807 | void glob_path_dialog(t_pd *dummy, t_symbol *s, int argc, t_atom *argv) | ||
808 | { | ||
809 | int i; | ||
810 | namelist_free(pd_path); | ||
811 | pd_path = 0; | ||
812 | for (i = 0; i < argc; i++) | ||
813 | { | ||
814 | t_symbol *s = atom_getsymbolarg(i, argc, argv); | ||
815 | if (*s->s_name) | ||
816 | sys_addpath(s->s_name); | ||
817 | } | ||
818 | } | ||
819 | |||
820 | |||