summaryrefslogtreecommitdiff
path: root/libpkgconf/parser.c
diff options
context:
space:
mode:
Diffstat (limited to 'libpkgconf/parser.c')
-rw-r--r--libpkgconf/parser.c183
1 files changed, 110 insertions, 73 deletions
diff --git a/libpkgconf/parser.c b/libpkgconf/parser.c
index 3a66b8af2a8d..11f023267388 100644
--- a/libpkgconf/parser.c
+++ b/libpkgconf/parser.c
@@ -2,7 +2,9 @@
* parser.c
* rfc822 message parser
*
- * Copyright (c) 2018 pkgconf authors (see AUTHORS).
+ * SPDX-License-Identifier: pkgconf
+ *
+ * Copyright (c) 2018, 2025 pkgconf authors (see AUTHORS).
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -17,97 +19,132 @@
#include <libpkgconf/stdinc.h>
#include <libpkgconf/libpkgconf.h>
-/*
- * !doc
- *
- * .. c:function:: pkgconf_pkg_t *pkgconf_pkg_new_from_file(const pkgconf_client_t *client, const char *filename, FILE *f)
- *
- * Parse a .pc file into a pkgconf_pkg_t object structure.
- *
- * :param pkgconf_client_t* client: The pkgconf client object to use for dependency resolution.
- * :param char* filename: The filename of the package file (including full path).
- * :param FILE* f: The file object to read from.
- * :returns: A ``pkgconf_pkg_t`` object which contains the package data.
- * :rtype: pkgconf_pkg_t *
- */
-void
-pkgconf_parser_parse(FILE *f, void *data, const pkgconf_parser_operand_func_t *ops, const pkgconf_parser_warn_func_t warnfunc, const char *filename)
+static void
+pkgconf_parser_canonicalize_line(pkgconf_buffer_t *buffer)
{
- pkgconf_buffer_t readbuf = PKGCONF_BUFFER_INITIALIZER;
- size_t lineno = 0;
- bool continue_reading = true;
-
- while (continue_reading)
- {
- char op, *p, *key, *value;
- bool warned_key_whitespace = false, warned_value_whitespace = false;
-
- continue_reading = pkgconf_fgetline(&readbuf, f);
- lineno++;
+ bool escaped = false;
+ char *buf = buffer->base;
+ char *src = buf, *dst = buf;
- p = readbuf.base;
- if (p == NULL)
- continue;
- while (*p && isspace((unsigned char)*p))
- p++;
- if (*p && p != readbuf.base)
- {
- warnfunc(data, "%s:" SIZE_FMT_SPECIFIER ": warning: whitespace encountered while parsing key section\n",
- filename, lineno);
- warned_key_whitespace = true;
- }
- key = p;
- while (*p && (isalpha((unsigned char)*p) || isdigit((unsigned char)*p) || *p == '_' || *p == '.'))
- p++;
+ if (buf == NULL)
+ return;
- if (!isalpha((unsigned char)*key) &&
- !isdigit((unsigned char)*p))
+ for (; *src != '\0'; src++)
+ {
+ if (*src == '\\' && !escaped)
{
- pkgconf_buffer_reset(&readbuf);
+ escaped = true;
continue;
}
- while (*p && isspace((unsigned char)*p))
+ if (*src == '#' && !escaped)
+ break;
+
+ if (escaped)
{
- if (!warned_key_whitespace)
+ if (*src == '#')
{
- warnfunc(data, "%s:" SIZE_FMT_SPECIFIER ": warning: whitespace encountered while parsing key section\n",
- filename, lineno);
- warned_key_whitespace = true;
+ *dst++ = '#';
+ escaped = false;
+ continue;
}
- /* set to null to avoid trailing spaces in key */
- *p = '\0';
- p++;
+ *dst++ = '\\';
+ escaped = false;
}
- op = *p;
- if (*p != '\0')
+ *dst++ = *src;
+ }
+
+ if (escaped)
+ *dst++ = '\\';
+
+ *dst = '\0';
+ buffer->end = dst;
+}
+
+void
+pkgconf_parser_parse_buffer(void *data, const pkgconf_parser_operand_func_t *ops, const pkgconf_parser_warn_func_t warnfunc, pkgconf_buffer_t *buffer, const char *warnprefix)
+{
+ char op, *p, *key, *value;
+ size_t vallen;
+
+ pkgconf_parser_canonicalize_line(buffer);
+
+ p = buffer->base;
+ if (p == NULL)
+ return;
+ while (*p && isspace((unsigned char)*p))
+ p++;
+ if (*p && p != buffer->base)
+ {
+ warnfunc(data, "%s: warning: whitespace encountered while parsing key section\n",
+ warnprefix);
+ }
+ key = p;
+ while (*p && (isalpha((unsigned char)*p) || isdigit((unsigned char)*p) || *p == '_' || *p == '.'))
+ p++;
+
+ if (!isalpha((unsigned char)*key) && !isdigit((unsigned char)*key))
+ return;
+
+ while (*p && isspace((unsigned char)*p))
+ {
+ warnfunc(data, "%s: warning: whitespace encountered while parsing key section\n",
+ warnprefix);
+
+ /* set to null to avoid trailing spaces in key */
+ *p = '\0';
+ p++;
+ }
+
+ op = *p;
+ if (*p != '\0')
+ {
+ *p = '\0';
+ p++;
+ }
+
+ while (*p && isspace((unsigned char)*p))
+ p++;
+
+ value = p;
+ vallen = strlen(value);
+ if (vallen)
+ p = value + (vallen - 1);
+
+ while (*p && isspace((unsigned char) *p) && p > value)
+ {
+ if (op == '=')
{
- *p = '\0';
- p++;
+ warnfunc(data, "%s: warning: trailing whitespace encountered while parsing value section\n",
+ warnprefix);
}
- while (*p && isspace((unsigned char)*p))
- p++;
+ *p = '\0';
+ p--;
+ }
- value = p;
- p = value + (strlen(value) - 1);
- while (*p && isspace((unsigned char) *p) && p > value)
- {
- if (!warned_value_whitespace && op == '=')
- {
- warnfunc(data, "%s:" SIZE_FMT_SPECIFIER ": warning: trailing whitespace encountered while parsing value section\n",
- filename, lineno);
- warned_value_whitespace = true;
- }
+ if (ops[(unsigned char) op])
+ ops[(unsigned char) op](data, warnprefix, key, value);
+}
- *p = '\0';
- p--;
- }
- if (ops[(unsigned char) op])
- ops[(unsigned char) op](data, lineno, key, value);
+void
+pkgconf_parser_parse(FILE *f, void *data, const pkgconf_parser_operand_func_t *ops, const pkgconf_parser_warn_func_t warnfunc, const char *filename)
+{
+ pkgconf_buffer_t readbuf = PKGCONF_BUFFER_INITIALIZER;
+ size_t lineno = 0;
+ bool continue_reading = true;
+
+ while (continue_reading)
+ {
+ char warnprefix[PKGCONF_ITEM_SIZE];
+
+ continue_reading = pkgconf_fgetline(&readbuf, f);
+ lineno++;
+ snprintf(warnprefix, sizeof warnprefix, "%s:" SIZE_FMT_SPECIFIER, filename, lineno);
+ pkgconf_parser_parse_buffer(data, ops, warnfunc, &readbuf, warnprefix);
pkgconf_buffer_reset(&readbuf);
}