guesscomponent.c 3.31 KB
Newer Older
Bernhard Link's avatar
Bernhard Link committed
1
/*  This file is part of "reprepro"
2 3
 *  Copyright (C) 2003 Bernhard R. Link
 *  This program is free software; you can redistribute it and/or modify
4
 *  it under the terms of the GNU General Public License version 2 as
Bernhard Link's avatar
Bernhard Link committed
5
 *  published by the Free Software Foundation.
6 7 8 9 10 11 12 13
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
Bernhard Link's avatar
Bernhard Link committed
14
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02111-1301  USA
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 */
#include <config.h>

#include <errno.h>
#include <assert.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include "error.h"
#include "guesscomponent.h"

/* Guess which component to use:
 * - if the user gave one, use that one.
 * - if the section is a componentname, use this one
 * - if the section starts with a componentname/, use this one
 * - if the section ends with a /componentname, use this one
 * - if the section/ is the start of a componentname, use this one
 * - use the first component in the list
 */

35
retvalue guess_component(const char *codename, const struct atomlist *components, const char *package, const char *section, component_t givencomponent, component_t *guess) {
36 37 38
	int i;
	size_t section_len;

39 40
	if (atom_defined(givencomponent)) {
		if (!atomlist_in(components, givencomponent)) {
41 42 43 44 45 46 47
			(void)fprintf(stderr,
"Could not find '%s' in components of '%s': ",
					atoms_components[givencomponent],
					codename);
			(void)atomlist_fprint(stderr,
					at_component, components);
			(void)fputs("'\n", stderr);
48 49
			return RET_ERROR;
		}
50 51
		*guess = givencomponent;
		return RET_OK;
52
	}
53 54 55 56
	if (section == NULL) {
		fprintf(stderr,
"Found no section for '%s', so I cannot guess the component to put it in!\n",
				package);
57 58
		return RET_ERROR;
	}
59 60 61 62
	if (components->count <= 0) {
		fprintf(stderr,
"I do not find any components in '%s', so there is no chance I cannot even take one by guessing!\n",
				codename);
63 64 65 66
		return RET_ERROR;
	}
	section_len = strlen(section);

67
	for (i = 0 ; i < components->count ; i++) {
68
		const char *component = atoms_components[components->atoms[i]];
69

70
		if (strcmp(section, component) == 0) {
71 72 73
			*guess = components->atoms[i];
			return RET_OK;
		}
74
	}
75
	for (i = 0 ; i < components->count ; i++) {
76
		const char *component = atoms_components[components->atoms[i]];
77 78
		size_t len = strlen(component);

79 80
		if (len<section_len && section[len] == '/' &&
				strncmp(section, component, len) == 0) {
81 82 83
			*guess = components->atoms[i];
			return RET_OK;
		}
84
	}
85
	for (i = 0 ; i < components->count ; i++) {
86
		const char *component = atoms_components[components->atoms[i]];
87 88
		size_t len = strlen(component);

89
		if (len<section_len && section[section_len-len-1] == '/' &&
90
				strncmp(section+section_len-len, component, len)
91
				== 0) {
92 93 94
			*guess = components->atoms[i];
			return RET_OK;
		}
95
	}
96
	for (i = 0 ; i < components->count ; i++) {
97 98
		const char *component = atoms_components[components->atoms[i]];

99 100
		if (strncmp(section, component, section_len) == 0 &&
				component[section_len] == '/') {
101 102 103
			*guess = components->atoms[i];
			return RET_OK;
		}
104 105

	}
106 107
	*guess = components->atoms[0];
	return RET_OK;
108
}