[go: up one dir, main page]

Menu

[r34]: / trunk / fbsdport.c  Maximize  Restore  History

Download this file

318 lines (262 with data), 6.8 kB

  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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* Software License Agreement (BSD License)
*
* Copyright (c) 2008, David Forsythe
* All rights reserved.
*
* Redistribution and use of this software in source and binary
* forms, with or without modification, are permitted provided that
* the following conditions are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* * Neither the name of David Forsythe nor the names of its
* contributors may be used to endorse or promote products
* derived from this software without specific prior
* written permission of David Forsythe
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "fbsdport.h"
struct fbsd_port
{
int built;
int dep_t;
char *port_path;
char *port_opts;
int num_dep;
int num_par;
fbsd_port **parents;
fbsd_port **depends;
};
/* Create a new fbsd_port port. The ports know what they depend on, and are
* aware of what depends on them (as well as what kind of dependency they are,
* if they are a dependency at all. */
fbsd_port *
new_port(char *path, char *opts, int dep_type)
{
fbsd_port *port;
port = malloc(sizeof(fbsd_port));
if(!port)
return(NULL);
port->port_path = strdup(path);
if(!port->port_path)
{
free(port);
return(NULL);
}
port->port_opts = strdup(opts);
if(!port->port_opts)
{
free(port->port_path);
free(port);
return(NULL);
}
port->num_dep = 0;
port->num_par = 0;
port->depends = NULL;
port->parents = NULL;
port->built = INACTIVE;
port->dep_t = dep_type;
return(port);
}
/* Return the number of dependencies the port has */
unsigned int
num_depends(fbsd_port *port)
{
return(port->num_dep);
}
unsigned int
num_parents(fbsd_port *port)
{
return(port->num_par);
}
/* Add a dependency to port, and add a parent to dport */
fbsd_port *
add_depends(fbsd_port *port, fbsd_port *dport)
{
if(!port)
return(NULL);
int c;
fbsd_port **d_parents;
fbsd_port **p_depends;
p_depends = malloc(sizeof(fbsd_port *) * (port->num_dep + 1) + 1);
d_parents = malloc(sizeof(fbsd_port *) * (dport->num_par + 1) + 1);
if(!p_depends || !d_parents)
{
printf("Allocation error!\n");
if(p_depends) free(d_parents);
if(d_parents) free(p_depends);
return(port);
}
for(c = 0; c < port->num_dep; ++c)
p_depends[c] = port->depends[c];
p_depends[c] = dport;
for(c = 0; c < dport->num_par; ++c)
d_parents[c] = dport->parents[c];
d_parents[c] = port;
port->num_dep++;
dport->num_par++;
free(port->depends);
free(dport->parents);
port->depends = p_depends;
dport->parents = d_parents;
return(port);
}
/* Remove a dependency from port (based on index), and removed the port as a
* parent to the indexed port */
fbsd_port *
rem_depends(fbsd_port *port, int index)
{
if(!port)
return(NULL);
if(index < 0 || index > port->num_dep - 1)
{
printf("Bad index!\n");
return(port);
}
int c;
fbsd_port *dport;
fbsd_port **d_parents;
fbsd_port **p_depends;
dport = port->depends[index];
p_depends = malloc(sizeof(fbsd_port *) * (port->num_dep - 1) + 1);
d_parents = malloc(sizeof(fbsd_port *) * (dport->num_par - 1) + 1);
if(!p_depends || !d_parents)
{
printf("Allocation error!\n");
if(p_depends) free(d_parents);
if(d_parents) free(p_depends);
return(port);
}
for(c = 0; c < index; ++c)
p_depends[c] = port->depends[c];
for(; c < port->num_dep && port->num_dep > 1; ++c)
p_depends[c] = port->depends[c + 1];
for(c = 0; c < dport->num_par; ++c)
{
if(dport->parents[c] == port)
break;
d_parents[c] = dport->parents[c];
}
for(; c < dport->num_par && dport->num_par > 1; ++c)
d_parents[c] = dport->parents[c + 1];
port->num_dep--;
dport->num_par--;
free(port->depends);
free(dport->parents);
port->depends = p_depends;
dport->parents = d_parents;
return(port);
}
/* Return the port_path as a string */
char *
path_str(fbsd_port *port)
{
return(port->port_path);
}
/* Return the port_opts as a string */
char *
opts_str(fbsd_port *port)
{
return(port->port_opts);
}
/* Free a port safely and completely */
void
free_port(fbsd_port *port)
{
if(!port)
return;
if(port->depends) free(port->depends);
if(port->parents) free(port->parents);
free(port->port_path);
free(port->port_opts);
free(port);
}
/* Build status (getting), setting */
unsigned int
build_status(fbsd_port *port)
{
return(port->built);
}
unsigned int
build_set(fbsd_port *port, int status)
{
port->built = status;
return(port->built);
}
/* Get info on dependencies (by index) */
fbsd_port *
get_depends(fbsd_port *port, int index)
{
if(!port->depends) /* No dependencies */
return(NULL);
if(port->num_dep - 1 < index || 0 > index) /* Bad index */
return(NULL);
return(port->depends[index]);
}
fbsd_port *
get_parent(fbsd_port *port, int index)
{
if(!port->parents)
return(NULL);
if(port->num_par - 1 < index || 0 > index) /* Bad index */
return(NULL);
return(port->parents[index]);
}
/* Get the index of a dependency or parent. Return -1 if they aren't present in
* the respective stack */
int
index_depend(fbsd_port *parent, fbsd_port *child)
{
return(index_stack(child, parent->depends, parent->num_dep));
}
int
index_parent(fbsd_port *child, fbsd_port *parent)
{
return(index_stack(parent, child->parents, child->num_par));
}
int
index_stack(fbsd_port *port, fbsd_port **stack, int len)
{
int c;
for(c = 0; c < len; ++c)
if(port == stack[c])
return(c);
return(-1);
}
/* Dealing with dependnecy types */
unsigned int
depend_type(fbsd_port *port)
{
return(port->dep_t);
}
unsigned int
depend_set(fbsd_port *port, int dtype)
{
if(dtype < NOT_DEP || RUN_DEP < dtype)
return(0); /* Bad dtype */
port->dep_t = dtype;
return(port->dep_t);
}