/* 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);
}