1
0
Fork 0
mirror of https://github.com/pygos/pkg-utils.git synced 2024-11-15 01:47:11 +01:00
pkg-utils/main/cmd/pack/desc.c
David Oberhollenzer 04f19bac2f pack: read package name from description
This commit changes the pack command to read the output package name from
the description file instead of the command line. The command line argument
is replaced with a package repository directory to which the package file
is then written.

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-01-30 10:40:02 +01:00

140 lines
2.4 KiB
C

#include "pack.h"
static int handle_requires(input_file_t *f, pkg_desc_t *desc)
{
char *ptr = f->line, *end;
dependency_t *dep;
size_t len;
while (*ptr != '\0') {
while (isspace(*ptr))
++ptr;
end = ptr;
while (*end != '\0' && !isspace(*end))
++end;
len = end - ptr;
if (len == 0)
break;
if (len > 0xFF) {
fprintf(stderr, "%s: %zu: dependency name too long\n",
f->filename, f->linenum);
return -1;
}
dep = calloc(1, sizeof(*dep) + len + 1);
if (dep == NULL) {
fprintf(stderr, "%s: %zu: out of memory\n",
f->filename, f->linenum);
return -1;
}
memcpy(dep->name, ptr, len);
dep->type = PKG_DEPENDENCY_REQUIRES;
dep->next = desc->deps;
desc->deps = dep;
ptr = end;
}
return 0;
}
static int handle_name(input_file_t *f, pkg_desc_t *desc)
{
if (desc->name != NULL) {
fprintf(stderr, "%s: %zu: name redefined\n",
f->filename, f->linenum);
return -1;
}
desc->name = strdup(f->line);
if (desc->name == NULL) {
fputs("out of memory\n", stderr);
return -1;
}
return 0;
}
static const struct {
const char *name;
int (*handle)(input_file_t *f, pkg_desc_t *desc);
} line_hooks[] = {
{ "requires", handle_requires },
{ "name", handle_name },
};
#define NUM_LINE_HOOKS (sizeof(line_hooks) / sizeof(line_hooks[0]))
int desc_read(const char *path, pkg_desc_t *desc)
{
input_file_t f;
size_t i, len;
char *ptr;
int ret;
memset(desc, 0, sizeof(*desc));
if (open_file(&f, path))
return -1;
for (;;) {
ret = prefetch_line(&f);
if (ret < 0)
goto fail;
if (ret > 0)
break;
ptr = f.line;
for (i = 0; i < NUM_LINE_HOOKS; ++i) {
len = strlen(line_hooks[i].name);
if (strncmp(ptr, line_hooks[i].name, len) != 0)
continue;
if (!isspace(ptr[len]) && ptr[len] != '\0')
continue;
for (ptr += len; isspace(*ptr); ++ptr)
;
memmove(f.line, ptr, strlen(ptr) + 1);
break;
}
if (i == NUM_LINE_HOOKS) {
fprintf(stderr, "%s: %zu: unknown description field\n",
f.filename, f.linenum);
goto fail;
}
ret = line_hooks[i].handle(&f, desc);
if (ret)
goto fail;
}
if (desc->name == NULL) {
fprintf(stderr, "%s: no name given in package description\n",
f.filename);
}
cleanup_file(&f);
return 0;
fail:
cleanup_file(&f);
return -1;
}
void desc_free(pkg_desc_t *desc)
{
dependency_t *dep;
while (desc->deps != NULL) {
dep = desc->deps;
desc->deps = dep->next;
free(dep);
}
free(desc->name);
}