Ejemplo n.º 1
0
/*
 * Reads a list of articles from the specified FILE.
 * This function always returns a valid List.
 *
 * This function is used to read datasheets in from
 * stdin during product registration.
 */
static List *
read_articles(FILE *in)
{
	List *result = _wsreg_list_create();

	if (in != NULL) {
		/*
		 * Set up a file reader to read the articles
		 */
		char *end_tokens[] = {
			"--",
			"\5",
			"\255",
			NULL};
		Ds_article_input_stream *ais = NULL;
		File_reader *fr = _wsreg_freader_create(in,
		    end_tokens);
		fr->set_echo_function(fr, input_text);

		/*
		 * Set up the datasheet article input stream.
		 */
		ais = _wsreg_dsais_open(fr);

		if (ais != NULL) {
			/*
			 * Read the articles into a list.
			 */
			while (ais->has_more_articles(ais)) {
				Article *a = ais->get_next_article(ais);
				if (a != NULL) {
					/*
					 * Be sure the new Article has a valid
					 * id.
					 */
					a->generate_id(a);

					log_message("DEBUG",
					    " < adding article %s [id=%s]>\n",
					    2, a->get_mnemonic(a),
					    a->get_id(a));
					result->add_element(result, a);
				}
			}
			ais->close(ais);
		}
		fr->free(fr);
	}

	return (result);
}
Ejemplo n.º 2
0
/*
 * Registers articles.  The arguments in the specified
 * list are (in order):
 *
 * install location
 * datasheet filename
 * parent mnemonic
 * parent id
 *
 * All of these arguments are optional.
 *
 * The install location refers to the directory in which the software
 * has been installed.
 *
 * The datasheet filename specifies the name of the file that contains
 * the data representing articles to be registered.  If the datasheet
 * filename is not provided, the datasheet information will be read
 * from stdin.
 *
 * The parent mnemonic specifies the name of the article which is the
 * parent of the article(s) being registered with this call.
 *
 * The parent id specifies the instance of the article which is the
 * parent of the article(s) being registered with this call.
 *
 * This function returns the id of the article being registered.
 */
static char *
register_articles(List *arg_list)
{
	/*
	 * The default datasheet file is stdin.
	 */
	FILE *in = stdin;
	char *result = NULL;
	char *location = NULL;
	char *parent_mnemonic = NULL;
	char *parent_id = NULL;
	Wsreg_component *parent_component = NULL;
	List *matches = NULL;
	List *article_list = NULL;
	Conversion *conversion;

	if (arg_list->size(arg_list) > 0) {
		/*
		 * Install location, datasheet filename.
		 */
		char *path;
		location = (char *)arg_list->element_at(arg_list, 0);
		if (strcmp(location, "-") == 0) {
			location = NULL;
		}
		path = (char *)arg_list->element_at(arg_list, 1);
		in = NULL;
		if (path != NULL) {
			in = fopen(path, "r");
		}
		if (in == NULL) {
			(void) fprintf(stderr, PRODREG_CANT_READ_FILE,
			    path);
			(void) fprintf(stderr, "\n");
			return ("");
		}

		if (arg_list->size(arg_list) > 2) {
			/*
			 * Parent mnemonic, parent id.
			 */
			parent_mnemonic =
			    (char *)arg_list->element_at(arg_list, 2);
			parent_id = (char *)arg_list->element_at(arg_list, 3);
			if (parent_mnemonic != NULL &&
			    parent_id != NULL) {
				matches =
				    get_matching_components(parent_mnemonic,
					parent_id);
				if (matches != NULL &&
				    matches->size(matches) == 1) {
					parent_component =
					    (Wsreg_component *)
					    matches->element_at(matches, 0);
				} else {
					(void) fprintf(stderr,
					    PRODREG_NO_SUCH_COMPONENT,
					    parent_mnemonic, parent_id);
					(void) fprintf(stderr, "\n");

				}
			}
		}
	}

	article_list = read_articles(in);
	conversion = _wsreg_conversion_create(NULL);

	/*
	 * Creates associations between parent Article and
	 * child Article.
	 */
	conversion->create_associations(article_list);

	/*
	 * Convert the articles to Wsreg_component structures
	 * and register.
	 */
	article_list->reset_iterator(article_list);
	while (article_list->has_more_elements(article_list)) {
		Article *a =
		    (Article *)article_list->next_element(article_list);
		/*
		 * The install location passed in overrides that in the
		 * datasheet.  I am not sure where this would be applicable.
		 * Is it really that the datasheet is incorrect and the
		 * user knows best here?
		 */
		if (location != NULL) {
			a->set_property(a, "installlocation", location);
		}
		conversion->add_article(conversion, a);
		result = a->get_id(a);
	}
	conversion->register_components(conversion, parent_component, TRUE);
	conversion->free(conversion);

	if (matches != NULL) {
		matches->free(matches, (Free)wsreg_free_component);
	}
	return (result);
}