On Sat, Nov 02, 2019 at 02:24:26PM +0100, Claudio Jeker wrote:
> Refactor the TAL loading code a bit (move the file parsing back into
> tal.c) and adjust the regress test to use this new function. This fixes
> the regress test and makes the code a bit nicer.
>
> OK?
> --
> :wq Claudio
>
> Index: usr.sbin/rpki-client/extern.h
> ===
> RCS file: /cvs/src/usr.sbin/rpki-client/extern.h,v
> retrieving revision 1.10
> diff -u -p -r1.10 extern.h
> --- usr.sbin/rpki-client/extern.h 31 Oct 2019 08:36:43 - 1.10
> +++ usr.sbin/rpki-client/extern.h 1 Nov 2019 19:59:50 -
> @@ -230,6 +230,7 @@ extern int verbose;
> void tal_buffer(char **, size_t *, size_t *, const struct tal *);
> void tal_free(struct tal *);
> struct tal *tal_parse(const char *, char *);
> +char *tal_read_file(const char *);
> struct tal *tal_read(int);
>
> void cert_buffer(char **, size_t *, size_t *, const struct cert *);
> Index: usr.sbin/rpki-client/main.c
> ===
> RCS file: /cvs/src/usr.sbin/rpki-client/main.c,v
> retrieving revision 1.21
> diff -u -p -r1.21 main.c
> --- usr.sbin/rpki-client/main.c 31 Oct 2019 08:36:43 - 1.21
> +++ usr.sbin/rpki-client/main.c 1 Nov 2019 20:01:35 -
> @@ -22,7 +22,6 @@
> #include
>
> #include
> -#include
> #include
> #include
> #include
> @@ -463,59 +462,11 @@ queue_add_from_mft_set(int fd, struct en
> static void
> queue_add_tal(int fd, struct entityq *q, const char *file, size_t *eid)
> {
> - char*nfile, *nbuf, *line = NULL, *buf = NULL;
> - FILE*in;
> - ssize_t n, i;
> - size_t sz = 0, bsz = 0;
> - int optcomment = 1;
> -
> - if ((in = fopen(file, "r")) == NULL)
> - err(EXIT_FAILURE, "fopen: %s", file);
> -
> - while ((n = getline(, , in)) != -1) {
> - /* replace CRLF with just LF */
> - if (n > 1 && line[n - 1] == '\n' && line[n - 2] == '\r') {
> - line[n - 2] = '\n';
> - line[n - 1] = '\0';
> - n--;
> - }
> - if (optcomment) {
> - /* if this is comment, just eat the line */
> - if (line[0] == '#')
> - continue;
> - optcomment = 0;
> - /*
> - * Empty line is end of section and needs
> - * to be eaten as well.
> - */
> - if (line[0] == '\n')
> - continue;
> - }
> -
> - /* make sure every line is valid ascii */
> - for (i = 0; i < n; i++)
> - if (!isprint(line[i]) && !isspace(line[i]))
> - errx(EXIT_FAILURE, "getline: %s: "
> - "invalid content", file);
> -
> - /* concat line to buf */
> - if ((nbuf = realloc(buf, bsz + n + 1)) == NULL)
> - err(EXIT_FAILURE, NULL);
> - buf = nbuf;
> - bsz += n + 1;
> - strlcat(buf, line, bsz);
> - /* limit the buffer size */
> - if (bsz > 4096)
> - errx(EXIT_FAILURE, "%s: file too big", file);
> - }
> -
> - free(line);
> - if (ferror(in))
> - err(EXIT_FAILURE, "getline: %s", file);
> - fclose(in);
> + char*nfile, *buf;
>
> if ((nfile = strdup(file)) == NULL)
> err(EXIT_FAILURE, "strdup");
> + buf = tal_read_file(file);
>
> /* Not in a repository, so directly add to queue. */
> entityq_add(fd, q, nfile, RTYPE_TAL, NULL, NULL, NULL, 0, buf, eid);
> Index: usr.sbin/rpki-client/tal.c
> ===
> RCS file: /cvs/src/usr.sbin/rpki-client/tal.c,v
> retrieving revision 1.8
> diff -u -p -r1.8 tal.c
> --- usr.sbin/rpki-client/tal.c31 Oct 2019 08:36:43 - 1.8
> +++ usr.sbin/rpki-client/tal.c1 Nov 2019 20:01:27 -
> @@ -17,6 +17,7 @@
>
> #include
> #include
> +#include
> #include
> #include
> #include
> @@ -154,6 +155,63 @@ tal_parse(const char *fn, char *buf)
> p->descr[dlen] = 0;
>
> return p;
> +}
> +
> +char *
> +tal_read_file(const char *file)
> +{
> + char*nbuf, *line = NULL, *buf = NULL;
> + FILE*in;
> + ssize_t n, i;
> + size_t sz = 0, bsz = 0;
> + int optcomment = 1;
> +
> + if ((in = fopen(file, "r")) == NULL)
> + err(EXIT_FAILURE, "fopen: %s", file);
> +
> + while ((n = getline(, , in)) != -1) {
> + /* replace CRLF with just LF */
> + if (n > 1 && line[n - 1] == '\n' && line[n - 2] ==