File: libs_dep.c

package info (click to toggle)
librnd 4.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,812 kB
  • sloc: ansic: 126,990; sh: 2,602; makefile: 2,145; awk: 7
file content (221 lines) | stat: -rw-r--r-- 5,659 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
    puplug - portable micro plugin framework
    Copyright (C) 2017 Tibor 'Igor2' Palinkas

    libgpmi - General Package/Module Interface
    Copyright (C) 2005-2007 Patric 'TrueLight' Stout & Tibor 'Igor2' Palinkas

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 31 Milk Street, # 960789 Boston, MA 02196 USA.
*/

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "libs.h"
#include "error.h"


typedef struct {
	char *s, buff[PUP_PATH_MAX+64];
	int deps_alloced, conflict_alloced;
	int lineno;

	pup_context_t *pup;
	pup_plugin_t *lib;
	const char **dir_list;
	const char *pup_path;
} parse_dep_t;

#include "libs_dep_parse.h"


static void pup_conflict_announce(pup_context_t *pup, const char *who, const char *with_whom)
{
	char *tmp;
	tmp = malloc(strlen(who) + strlen(with_whom) + 40);
	sprintf(tmp, "package %s conflicts package %s", who, with_whom);
	pup_err_stack_push(pup, pup_err_conflict, pup_err_src_load_dep, tmp);
	free(tmp);
}

/* check if any of the existing plugins would conflict with lib */
int pup_conflict_loaded(pup_context_t *pup, const char *new_name)
{
	pup_plugin_t *p;
	for(p = pup->plugins; p != NULL; p = p->next) {
		int n;
		for(n = 0; n < p->conflict_len; n++)
			if (strcmp(p->conflict[n], new_name) == 0) {
				pup_conflict_announce(pup, p->name, new_name);
				return 1;
			}
	}
	return 0;
}

static int pup_conflict_loading(pup_context_t *pup, const char *oldn, const char *newn)
{
	if (pup_lookup(pup, oldn) != NULL) {
		pup_conflict_announce(pup, newn, oldn);
		return 1;
	}
	return 0;
}


static int pup_parse_dep(parse_dep_t *st, char *first, char *args)
{
	int veri = 0, state;
	pup_plugin_t *pkg;

	if (st->lib->flags & PUP_FLG_FIND_AUTO)
		return 0;

	if (args != NULL) {
		/* Atoi will stop at the next ';' - later we should use strtol here
		   if we are interested in flags after the 2nd separator */
		veri = atoi(args);
	}

	/* Try to load the package */
	pkg = pup_load_(st->pup, st->dir_list, NULL, first, veri, &state);

	/* Don't go in endless recursions becuase cyclic deps */
	if ((state == pup_err_already_loading) || (state == pup_err_cyclic_dep))
		return pup_err_stack_push(st->pup, pup_err_cyclic_dep, pup_err_src_load_dep, st->buff);

	/* Check for fatal errors */
	if (state < 0)
		return state;

	/* Append pkg to lib's dep list */
	if (st->lib->deps_len >= st->deps_alloced) {
		st->deps_alloced += 16;
		st->lib->deps = realloc(st->lib->deps, sizeof(pup_plugin_t *) * st->deps_alloced);
	}
	st->lib->deps[st->lib->deps_len] = pkg;
	st->lib->deps_len++;
	return 0;
}

static int pup_parse_conflict(parse_dep_t *st, char *first, char *args)
{
	if (st->lib->flags & PUP_FLG_FIND_AUTO)
		return 0;

	if (pup_conflict_loading(st->pup, first, st->lib->name))
		return 1;

	if (st->lib->conflict_len >= st->conflict_alloced) {
		st->conflict_alloced += 8;
		st->lib->conflict = realloc(st->lib->conflict, sizeof(char *) * st->conflict_alloced);
	}
	st->lib->conflict[st->lib->conflict_len] = pup_strdup(first);
	st->lib->conflict_len++;

	return 0;
}

static int pup_parse_autoload(parse_dep_t *st, char *first, char *args)
{
	st->lib->flags |= PUP_FLG_AUTOLOAD;
	return 0;
}

static int pup_parse_default(parse_dep_t *st, char *first, char *args) { return 0; }
static int pup_parse_tag(parse_dep_t *st, char *tag, char *args) { return 0; }


static int pup_parse_syntax_error(parse_dep_t *st, const char *msg)
{
	char tmp[PUP_PATH_MAX*2];
	sprintf(tmp, "syntax error in %s: %s\n", (st->pup_path == NULL ? "<buildin>" :st->pup_path), msg);
	pup_err_stack_push(st->pup, pup_err_parse_pup, pup_err_src_load_pup, tmp);
	return -1;
}

int pup_load_pup_file(pup_context_t *pup, const char **dir_list, pup_plugin_t *lib, const char *pup_file_name)
{
	FILE *f;
	int c, res = 0;
	parse_dep_t st;

	/* try to open the dep file of the library */
	f = fopen(pup_file_name, "r");
	if (f == NULL)
		return 0;

	memset(&st, 0, sizeof(st));
	pup_parse_pup_init(&st);
	st.pup = pup;
	st.lib = lib;
	st.dir_list = dir_list;
	st.pup_path = pup_file_name;

	/* Read the file char by char seeking words */
	do {
		c = fgetc(f);
		res = pup_parse_pup(&st, c);
	} while((res == 0) && (c != EOF));

	fclose(f);
	return res;
}

int pup_load_pup_str(pup_context_t *pup, const char **dir_list, pup_plugin_t *lib, const char *pup_script)
{
	int c, res = 0;
	parse_dep_t st;

	memset(&st, 0, sizeof(st));
	pup_parse_pup_init(&st);
	st.pup = pup;
	st.lib = lib;
	st.dir_list = dir_list;

	/* Read the file char by char seeking words */
	do {
		c = *pup_script;
		pup_script++;
		if (c == '\0')
			c = EOF;
		res = pup_parse_pup(&st, c);
	} while((res == 0) && (c != EOF));

	return res;
}

/**
 *  Unload dependency of a library recursively.
 */
void pup_unload_dep(pup_context_t *pup, pup_plugin_t *lib)
{
	int n;

	for(n = 0; n < lib->deps_len; n++)
		pup_unload_(pup, lib->deps[n], NULL);

	free(lib->deps);
	lib->deps = NULL;

	for(n = 0; n < lib->conflict_len; n++)
		free(lib->conflict[n]);

	free(lib->conflict);
	lib->conflict_len = 0;
}