aboutsummaryrefslogtreecommitdiff
path: root/pd/src/s_path.c
blob: a61956f1b6940ea16be99c4f0bbcfeeb7db25eb4 (plain)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* Copyright (c) 1999 Guenter Geiger and others.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

/*
 * This file implements the loader for linux, which includes
 * a little bit of path handling.
 *
 * Generalized by MSP to provide an open_via_path function
 * and lists of files for all purposes.
 */ 

/* #define DEBUG(x) x */
#define DEBUG(x)
void readsf_banana( void);    /* debugging */

#include <stdlib.h>
#ifdef UNIX
#include <unistd.h>
#include <sys/stat.h>
#endif
#ifdef NT
#include <io.h>
#endif

#include <string.h>
#include "m_imp.h"
#include <stdio.h>
#include <fcntl.h>

static t_namelist *pd_path;

/* Utility functions */

/* copy until delimiter and return position after delimiter in string */
/* if it was the last substring, return NULL */

static const char* strtokcpy(char *to, const char *from, int delim)
{
    int size = 0;

    while (from[size] != (char)delim && from[size] != '\0')
    	size++;

    strncpy(to,from,size);
    to[size] = '\0';
    if (from[size] == '\0') return NULL;
    if (size) return from+size+1;
    else return NULL;
}

/* add a colon-separated list of names to a namelist */

#ifdef NT
#define SEPARATOR ';'
#else
#define SEPARATOR ':'
#endif

static t_namelist *namelist_doappend(t_namelist *listwas, const char *s)
{
    t_namelist *nl = listwas, *rtn = listwas, *nl2;
    nl2 = (t_namelist *)(getbytes(sizeof(*nl)));
    nl2->nl_next = 0;
    nl2->nl_string = (char *)getbytes(strlen(s) + 1);
    strcpy(nl2->nl_string, s);
    sys_unbashfilename(nl2->nl_string, nl2->nl_string);
    if (!nl)
        nl = rtn = nl2;
    else
    {
        while (nl->nl_next)
    	    nl = nl->nl_next;
        nl->nl_next = nl2;
    }
    return (rtn);

}

t_namelist *namelist_append(t_namelist *listwas, const char *s)
{
    const char *npos;
    char temp[MAXPDSTRING];
    t_namelist *nl = listwas, *rtn = listwas;
    
    npos = s;
    do
    {
	npos = strtokcpy(temp, npos, SEPARATOR);
	if (! *temp) continue;
	nl = namelist_doappend(nl, temp);
    }
	while (npos);
    return (nl);
}

void namelist_free(t_namelist *listwas)
{
    t_namelist *nl, *nl2;
    for (nl = listwas; nl; nl = nl2)
    {
    	nl2 = nl->nl_next;
	t_freebytes(nl->nl_string, strlen(nl->nl_string) + 1);
	t_freebytes(nl, sizeof(*nl));
    }
}

void sys_addpath(const char *p)
{
     pd_path = namelist_append(pd_path, p);
}

#ifdef NT
#define NTOPENFLAG (bin ? _O_BINARY : _O_TEXT)
#else
#define NTOPENFLAG 0
#endif

/* search for a file in a specified directory, then along the globally
defined search path, using ext as filename extension.  Exception:
if the 'name' starts with a slash or a letter, colon, and slash in NT,
there is no search and instead we just try to open the file literally.  The
fd is returned, the directory ends up in the "dirresult" which must be at
least "size" bytes.  "nameresult" is set to point to the filename, which
ends up in the same buffer as dirresult. */

int open_via_path(const char *dir, const char *name, const char* ext,
    char *dirresult, char **nameresult, unsigned int size, int bin)
{
    t_namelist *nl, thislist;
    int fd = -1;
    char listbuf[MAXPDSTRING];

    if (name[0] == '/' 
#ifdef NT
    	|| (name[1] == ':' && name[2] == '/')
#endif
    	    )
    {
    	thislist.nl_next = 0;
    	thislist.nl_string = listbuf;
    	listbuf[0] = 0;
    }
    else
    {
    	thislist.nl_string = listbuf;
	thislist.nl_next = pd_path;
	strncpy(listbuf, dir, MAXPDSTRING);
	listbuf[MAXPDSTRING-1] = 0;
	sys_unbashfilename(listbuf, listbuf);
    }
    for (nl = &thislist; nl; nl = nl->nl_next)
    {
    	if (strlen(nl->nl_string) + strlen(name) + strlen(ext) + 4 >
	    size)
	    	continue;
	strcpy(dirresult, nl->nl_string);
	if (*dirresult && dirresult[strlen(dirresult)-1] != '/')
	       strcat(dirresult, "/");
	strcat(dirresult, name);
	strcat(dirresult, ext);
	sys_bashfilename(dirresult, dirresult);

	DEBUG(post("looking for %s",dirresult));
	    /* see if we can open the file for reading */
	if ((fd=open(dirresult,O_RDONLY | NTOPENFLAG)) >= 0)
	{
	    	/* in UNIX, further check that it's not a directory */
#ifdef UNIX
    	    struct stat statbuf;
	    int ok =  ((fstat(fd, &statbuf) >= 0) &&
	    	!S_ISDIR(statbuf.st_mode));
	    if (!ok)
	    {
	    	if (sys_verbose) post("tried %s; stat failed or directory",
		    dirresult);
	    	close (fd);
		fd = -1;
    	    }
	    else
#endif
    	    {
	    	char *slash;
		if (sys_verbose) post("tried %s and succeeded", dirresult);
		sys_unbashfilename(dirresult, dirresult);
		slash = strrchr(dirresult, '/');
		if (slash)
		{
		    *slash = 0;
		    *nameresult = slash + 1;
		}
		else *nameresult = dirresult;
		
	    	return (fd);  
	    }
	}
	else
	{
	    if (sys_verbose) post("tried %s and failed", dirresult);
	}
    }
    *dirresult = 0;
    *nameresult = dirresult;
    return (-1);
}

/* Startup file reading for linux */

#ifdef __linux__

#define STARTUPNAME ".pdrc"
#define NUMARGS 1000

int sys_argparse(int argc, char **argv);

int sys_rcfile(void)
{
    FILE* file;
    int i;
    int k;
    int rcargc;
    char* rcargv[NUMARGS];
    char* buffer;
    char  fname[MAXPDSTRING], buf[1000];

    /* parse a startup file */
    
    *fname = '\0'; 

    strncat(fname, getenv("HOME"), MAXPDSTRING-10);
    strcat(fname, "/");

    strcat(fname, STARTUPNAME);

    if (!(file = fopen(fname, "r")))
    	return 1;

    post("reading startup file: %s", fname);

    rcargv[0] = ".";	/* this no longer matters to sys_argparse() */

    for (i = 1; i < NUMARGS-1; i++)
    {
    	if (fscanf(file, "%999s", buf) < 0)
	    break;
	buf[1000] = 0;
	if (!(rcargv[i] = malloc(strlen(buf) + 1)))
	    return (1);
	strcpy(rcargv[i], buf);
    }
    if (i >= NUMARGS-1)
    	fprintf(stderr, "startup file too long; extra args dropped\n");
    rcargv[i] = 0;

    rcargc = i;

    /* parse the options */

    fclose(file);
    if (sys_verbose)
    {
    	if (rcargv)
	{
    	    post("startup args from RC file:");
	    for (i = 1; i < rcargc; i++)
	    	post("%s", rcargv[i]);
    	}
	else post("no RC file arguments found");
    }
    if (sys_argparse(rcargc, rcargv))
    {
    	post("error parsing RC arguments");
	return (1);
    }
    return (0);
}
#endif /* __linux__ */