Skip to content

Commit

Permalink
Added "setenv" for bools, to export values to the environment.
Browse files Browse the repository at this point in the history
  • Loading branch information
megastep committed Mar 31, 2006
1 parent 7bb7ceb commit 69c4d0f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 20 deletions.
5 changes: 5 additions & 0 deletions README.xml
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,11 @@ Additionally, one of the following attributes must be used, unless "if" below is
The value must be a non-zero number to map to be TRUE. If the variable is not
set in the environment, the boolean will likewise be set to FALSE.

setenv The name of an environment variable that will be set with the value of the boolean
when running user scripts throughout the installer. The value of that variable will
be 0 or 1. This can be used for advanced scripting querying the internal state of
the installer.

It also recognizes two optional attributes :

later If set to "yes", the script will be executed at the time that the condition
Expand Down
30 changes: 18 additions & 12 deletions bools.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $Id: bools.c,v 1.1 2006-03-29 23:38:28 megastep Exp $ */
/* $Id: bools.c,v 1.2 2006-03-31 01:29:02 megastep Exp $ */

/*
Manage global installer booleans.
Expand Down Expand Up @@ -32,15 +32,18 @@ struct _setup_expression
struct _setup_expression *list, *next;
};

setup_bool *setup_booleans = NULL;

static install_info *cur_info = NULL;
static setup_bool *booleans = NULL, *setup_false = NULL, *setup_true = NULL;
static setup_bool *setup_false = NULL, *setup_true = NULL;

/* Fill up with standard booleans */
void setup_init_bools(install_info *info)
{
char buf[80];

cur_info = info;
setup_booleans = NULL;

/* Basic bools */
setup_true = setup_add_bool("true", 1);
Expand Down Expand Up @@ -81,8 +84,8 @@ setup_bool *setup_new_bool(const char *name)
if ( ret ) {
memset(ret, 0, sizeof(*ret));
ret->name = strdup(name);
ret->next = booleans;
booleans = ret;
ret->next = setup_booleans;
setup_booleans = ret;
}
return ret;
}
Expand All @@ -92,6 +95,7 @@ void setup_free_bool(setup_bool *b)
if (b) {
free(b->name);
free(b->script);
free(b->envvar);
free(b);
}
}
Expand All @@ -110,21 +114,23 @@ setup_bool *setup_find_bool(const char *name)
{
setup_bool *ret;

for ( ret = booleans; ret; ret = ret->next ) {
for ( ret = setup_booleans; ret; ret = ret->next ) {
if ( !strcmp(ret->name, name) ) {
return ret;
}
}
return NULL;
}

int setup_get_bool(const setup_bool *b)
int setup_get_bool(setup_bool *b)
{
if ( b ) {
if ( b->once ) {
return b->inited ? b->value : 0;
} else if (b->script) { /* Run the script to determine the value */
return (run_script(cur_info, b->script, 0, 0) == 0);
b->value = (run_script(cur_info, b->script, 0, 0) == 0);
b->inited = 1; /* Keep track of the last value */
return b->value;
}
}
return 0;
Expand Down Expand Up @@ -348,11 +354,11 @@ int match_condition(const char *expr)
void setup_exit_bools(void)
{
setup_bool *b;
while ( booleans ) {
b = booleans->next;
setup_free_bool(booleans);
booleans = b;
while ( setup_booleans ) {
b = setup_booleans->next;
setup_free_bool(setup_booleans);
setup_booleans = b;
}
cur_info = NULL;
booleans = NULL;
setup_booleans = NULL;
}
8 changes: 6 additions & 2 deletions bools.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $Id: bools.h,v 1.1 2006-03-29 23:38:28 megastep Exp $ */
/* $Id: bools.h,v 1.2 2006-03-31 01:29:02 megastep Exp $ */

/*
Manage global installer booleans.
Expand All @@ -13,6 +13,7 @@
typedef struct _setup_bool {
char *name; /* Symbolic name */
char *script; /* A script to be run to determine the value, or NULL */
char *envvar; /* An environment variable to set with the value, if any */
unsigned value : 1; /* Boolean value */
unsigned once : 1; /* Whether the script has to be run every time the bool is evaluated, or just upon init */
unsigned inited : 1; /* Value was filled in */
Expand All @@ -24,6 +25,9 @@ typedef struct _setup_bool {
struct _setup_expression;
typedef struct _setup_expression setup_expression;

/* Exported list of booleans */

extern setup_bool *setup_booleans;

/**** API functions *******/

Expand All @@ -39,7 +43,7 @@ setup_bool *setup_add_bool(const char *name, unsigned value);

/* Manipulate booleans */
setup_bool *setup_find_bool(const char *name);
int setup_get_bool(const setup_bool *b);
int setup_get_bool(setup_bool *b);
void setup_set_bool(setup_bool *b, unsigned value);


Expand Down
26 changes: 20 additions & 6 deletions install.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $Id: install.c,v 1.171 2006-03-30 01:53:52 megastep Exp $ */
/* $Id: install.c,v 1.172 2006-03-31 01:29:02 megastep Exp $ */

/* Modifications by Borland/Inprise Corp.:
04/10/2000: Added code to expand ~ in a default path immediately after
Expand Down Expand Up @@ -616,20 +616,22 @@ const char *GetProductREADME(install_info *info, int *keepdirs)
int GetProductBooleans(install_info *info)
{
xmlNodePtr node;
char *name, *script, *later, *envvar, *cond;
char *name, *script, *later, *envvar, *envto, *cond;
int count = 0;
setup_bool *b = NULL;

for(node = XML_CHILDREN(XML_ROOT(info->config)); node; node = node->next) {
if(! strcmp((char *)node->name, "bool") ) {
name = (char *)xmlGetProp(node, BAD_CAST "name");
script = (char *)xmlGetProp(node, BAD_CAST "script");
later = (char *)xmlGetProp(node, BAD_CAST "later");
envvar = (char *)xmlGetProp(node, BAD_CAST "env");
envto = (char *)xmlGetProp(node, BAD_CAST "setenv");
cond = (char *)xmlGetProp(node, BAD_CAST "if");

if ( match_condition(cond) ) {
if ( script ) {
setup_bool *b = setup_new_bool(name);
b = setup_new_bool(name);
if ( b ) {
count ++;
b->script = strdup(script);
Expand All @@ -645,7 +647,7 @@ int GetProductBooleans(install_info *info)
log_fatal(_("Failed to allocate new bool"));
}
} else if ( envvar ) {
setup_bool *b = setup_new_bool(name);
b = setup_new_bool(name);
if ( b ) {
char *env = getenv(envvar);

Expand All @@ -659,18 +661,21 @@ int GetProductBooleans(install_info *info)
log_debug("New environment bool: %s = %s (from %s)", name, b->value ? "TRUE" : "FALSE", envvar);
}
} else if ( cond ) { /* We matched the condition - if all else fails, create the bool */
setup_add_bool(name, 1);
b = setup_add_bool(name, 1);
} else {
log_warning(_("Must have at least 'script' or 'env' attribute for <bool>"));
}
} else {
setup_add_bool(name, 0);
b = setup_add_bool(name, 0);
log_debug("New bool '%s' set to FALSE because of condition '%s'", name, cond);
}
if ( b && envto )
b->envvar = strdup(envto);
xmlFree(name);
xmlFree(script);
xmlFree(later);
xmlFree(envvar);
xmlFree(envto);
xmlFree(cond);
}
}
Expand Down Expand Up @@ -2939,6 +2944,8 @@ int run_script(install_info *info, const char *script, int arg, int include_tags

fp = fdopen(fd, "w");
if ( fp ) {
setup_bool *b;

fprintf(fp, /* Create script file, setting environment variables */
"#!/bin/sh\n"
"SETUP_PRODUCTNAME=\"%s\"\n"
Expand All @@ -2961,6 +2968,13 @@ int run_script(install_info *info, const char *script, int arg, int include_tags
"SETUP_OPTIONTAGS=\"%s\"\n"
"export SETUP_OPTIONTAGS\n",
get_optiontags_string(info));
/* Set boolean environment variables */
for ( b = setup_booleans; b; b = b->next ) {
if (b->envvar && b->inited) { /* We are NOT running scripts at this point */
fprintf(fp,"%s=%d; export %s\n", b->envvar, b->value, b->envvar);
}
}

/* Append script itself */
fprintf(fp, "%s%s\n",
working_dir, script);
Expand Down

0 comments on commit 69c4d0f

Please sign in to comment.