setup/setup.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Feb 11 19:47:47 2010 +0000 (2010-02-11)
changeset 8 f9a7fa531563
parent 0 49fc8f60e4a5
child 13 b0a35bae4961
permissions -rw-r--r--
Start 0.3
     1 /*
     2  * Copyright (C) 2009  J. Ali Harlow <ali@juiblex.co.uk>
     3  *
     4  * This program is free software; you can redistribute it and/or modify
     5  * it under the terms of the GNU General Public License as published by
     6  * the Free Software Foundation; either version 2 of the License, or
     7  * (at your option) any later version.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License along
    15  * with this program; if not, write to the Free Software Foundation, Inc.,
    16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    17  */
    18 
    19 #include <stdlib.h>
    20 #include <stdio.h>
    21 #include <string.h>
    22 #include <limits.h>
    23 #ifdef WIN32
    24 #include <windows.h>
    25 #include <shlobj.h>
    26 #endif
    27 #include <lua.h>
    28 #include "config.h"
    29 #include "plover/plover.h"
    30 #include "whelk/whelk.h"
    31 
    32 LUALIB_API int luaopen_posix(lua_State *L);
    33 
    34 struct vector {
    35     int len,alloc;
    36     char **strings;
    37 };
    38 
    39 struct vector *vector_new(void)
    40 {
    41     struct vector *vector;
    42     vector=malloc(sizeof(*vector));
    43     vector->len=0;
    44     vector->alloc=16;
    45     vector->strings=calloc(vector->alloc,sizeof(char *));
    46     return vector;
    47 }
    48 
    49 void vector_append(struct vector *vector,const char *str)
    50 {
    51     if (++(vector->len)>=vector->alloc)
    52     {
    53 	vector->alloc*=2;
    54 	vector->strings=realloc(vector->strings,vector->alloc*sizeof(char *));
    55     }
    56     vector->strings[vector->len-1]=strdup(str);
    57     vector->strings[vector->len]=NULL;
    58 }
    59 
    60 int vector_contains(struct vector *vector,const char *str)
    61 {
    62     int i;
    63     for(i=0;i<vector->len;i++)
    64 	if (!strcmp(vector->strings[i],str))
    65 	    return 1;
    66     return 0;
    67 }
    68 
    69 void vector_free(struct vector *vector)
    70 {
    71     int i;
    72     for(i=0;i<vector->len;i++)
    73 	free(vector->strings[i]);
    74     free(vector->strings);
    75     free(vector);
    76 }
    77 
    78 static void *alloc_lua(void *user_data,void *ptr,size_t osize,size_t nsize)
    79 {
    80     if (!nsize)
    81     {
    82 	free(ptr);
    83 	return NULL;
    84     }
    85     else
    86 	return realloc(ptr,nsize);
    87 }
    88 
    89 void setup(const char *argv0)
    90 {
    91     char *path,*s,*prefix;
    92     int changed;
    93     struct comps *comps;
    94     struct comps_group *group;
    95     struct comps_requirement *pkg;
    96     struct vector *packages=NULL;
    97     path=plover_get_program_directory(argv0);
    98     s=plover_strconcat(path,"/repodata/comps.xml",NULL);
    99     comps=plover_comps_new_from_file(s);
   100     if (!comps)
   101     {
   102 	perror(s);
   103 	exit(1);
   104     }
   105     free(s);
   106     prefix=plover_default_prefix_for_vendor(comps->vendor);
   107     group=plover_comps_lookup_group(comps,"base");
   108     if (!group)
   109     {
   110 	fprintf(stderr,"No base group found in comps.xml\n");
   111 	exit(1);
   112     }
   113     packages=vector_new();
   114     do
   115     {
   116 	changed=0;
   117 	for(pkg=group->packages;pkg;pkg=pkg->next)
   118 	{
   119 	    if (vector_contains(packages,pkg->name))
   120 		continue;
   121 	    if (pkg->type==COMPS_REQUIREMENT_DEFAULT ||
   122 	      pkg->type==COMPS_REQUIREMENT_MANDATORY ||
   123 	      pkg->type==COMPS_REQUIREMENT_CONDITIONAL &&
   124 	      vector_contains(packages,pkg->requires))
   125 	    {
   126 		changed++;
   127 		vector_append(packages,pkg->name);
   128 	    }
   129 	}
   130     } while(changed);
   131     plover_comps_free(comps);
   132     if (!packages->len)
   133     {
   134 	fprintf(stderr,"No packages to install\n");
   135 	exit(1);
   136     }
   137     plover_install(path,prefix,packages->strings);
   138     vector_free(packages);
   139     free(prefix);
   140     free(path);
   141 }
   142 
   143 int main(int argc,char **argv)
   144 {
   145     razor_set_lua_loader("posix",luaopen_posix);
   146     razor_set_lua_loader("whelk",luaopen_whelk);
   147     if (argc>1 && !strcmp(argv[1],"-u"))
   148 	plover_remove(NULL);
   149     else
   150 	setup(argv[0]);
   151 }