File: path.c

package info (click to toggle)
audacity 1.2.4b-2.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 24,136 kB
  • ctags: 20,445
  • sloc: ansic: 139,567; cpp: 55,998; sh: 24,963; lisp: 3,772; makefile: 1,683; python: 272
file content (195 lines) | stat: -rw-r--r-- 4,912 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * New xlisp_path code by Dominic Mazzoni
 *
 * There is now a function provided to set the xlisp_path.
 * This is particularly useful for external programs
 * (e.g. Audacity, or a Nyquist GUI) that have their own
 * mechanism of setting/finding the path.  If xlisp_path
 * is NULL, the old platform-specific methods are still
 * used.
 */

#include <string.h>

#include "switches.h"
#include "xlisp.h"

static char *g_xlisp_path = NULL;

void set_xlisp_path(const char *p)
{
    if (g_xlisp_path) {
        free(g_xlisp_path);
        g_xlisp_path = NULL;
    }

    if (p) {
        g_xlisp_path = malloc(strlen(p)+1);
        strcpy(g_xlisp_path, p);
    }
}

#ifdef UNIX
const char *unix_return_xlisp_path()
{
    char *paths = getenv("XLISPPATH");
    if (!paths || !*paths) {
        char msg[512];
        sprintf(msg, "\n%s\n%s\n%s\n%s\n%s\n",
                "Warning: XLISP failed to find XLISPPATH in the environment.",
                "If you are using Nyquist, probably you should cd to the",
                "nyquist directory and type:",
                "    setenv XLISPPATH `pwd`/runtime:`pwd`/lib",
                "or set XLISPPATH in your .login or .cshrc file.");
        errputstr(msg);
    }
    return paths;
}
#endif

#ifdef WINDOWS
#include "winfun.h"

const char *windows_return_xlisp_path()
{
    #define paths_max 1024
    static char paths[paths_max];
    get_xlisp_path(paths, paths_max);
    /* make sure we got paths, and the list is not empty */
    if (!paths[0]) {
        sprintf(paths, "\n%s\n%s\n%s\n",
           "Warning: XLISP failed to find XLISPPATH in the Registry.",
           "You should follow the installation instructions. Enter an",
           "empty string if you really want no search path.");
        errputstr(paths);
    }

    return paths;
}
#endif

#ifdef MACINTOSH
const char *mac_return_xlisp_path()
{
    #define paths_max 1024
    static char paths[paths_max];
    int prefs_found = false;
    get_xlisp_path(paths, paths_max, &prefs_found);
    if (!paths[0]) {
        if (prefs_found) {
            sprintf(paths, "\n%s\n%s\n%s\n",
             "Warning: XLISP failed to find XLISPPATH in XLisp Preferences.",
             "You should probably delete XLisp Preferences and let XLisp",
             "create a new one for you.");
        }
        else {
           sprintf(paths, "\n%s\n%s\n%s\n%s\n%s\n",
            "Warning: XLISP failed to find XLisp Preferences.",
            "You should manually locate and load the file runtime:init.lsp",
            "Nyquist will create an XLisp Preferences file to automatically",
            "find the file next time. You may edit XLisp Preferences to add",
            "additional search paths, using a comma as separator.");
        }
        errputstr(paths);
    }

    return paths;
}
#endif

const char *return_xlisp_path()
{
    if (g_xlisp_path)
        return g_xlisp_path;

#ifdef WINDOWS
    return windows_return_xlisp_path();
#endif
#ifdef MACINTOSH
    return mac_return_xlisp_path();
#endif
#ifdef UNIX
    return unix_return_xlisp_path();
#endif
}

#ifdef UNIX
static const char pathchar = '/';
static const char sepchar = ':';
#endif

#ifdef WINDOWS
static const char pathchar = '\\';
static const char sepchar = ',';
#endif

#ifdef MACINTOSH
static const char pathchar = ':';
static const char sepchar = ',';
#endif

char *g_xlptemp = NULL;

const char *find_in_xlisp_path(const char *fname)
{
    const char *paths = return_xlisp_path();
    if (!paths)
        return NULL;

    while (paths && *paths) {
        FILE *fp;
        const char *start;
        int len;

        /* skip over separator */
        while (*paths == sepchar) paths++;

        /* find next directory */
        start = paths;
        while (*paths && (*paths != sepchar))
            paths++;

        if (g_xlptemp) {
           free(g_xlptemp);
           g_xlptemp = NULL;
        }

        len = paths - start;
        g_xlptemp = malloc(len + strlen(fname) + 10);
        memcpy(g_xlptemp, start, len);

        if (len == 0)
           continue;

        /* add "/" if needed */
        if (g_xlptemp[len-1] != pathchar)
           g_xlptemp[len++] = pathchar;
        
        /* append the file name */
        memcpy(&g_xlptemp[len], fname, strlen(fname));
        len += strlen(fname);
        g_xlptemp[len] = 0;

        /* append the .lsp extension */
        if (needsextension(g_xlptemp))
           strcat(g_xlptemp, ".lsp");

        fp = osaopen(g_xlptemp, "r");
        if (fp) {
           fclose(fp);

           #ifdef MACINTOSH
           /* We found the file ok, call setup_preferences to create
            * XLisp Preferences file (this only happens if previous
            * attempt to find the file failed
            */
           setup_preferences(g_xlptemp);
           #endif

           return g_xlptemp;
        }
    }

    /* It wasn't found */
    return NULL;
}