1 /*
2 * parser.c
3 * rfc822 message parser
4 *
5 * Copyright (c) 2018 pkgconf authors (see AUTHORS).
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * This software is provided 'as is' and without any warranty, express or
12 * implied. In no event shall the authors be liable for any damages arising
13 * from the use of this software.
14 */
15
16 #include <libpkgconf/config.h>
17 #include <libpkgconf/stdinc.h>
18 #include <libpkgconf/libpkgconf.h>
19
20 /*
21 * !doc
22 *
23 * .. c:function:: pkgconf_pkg_t *pkgconf_pkg_new_from_file(const pkgconf_client_t *client, const char *filename, FILE *f)
24 *
25 * Parse a .pc file into a pkgconf_pkg_t object structure.
26 *
27 * :param pkgconf_client_t* client: The pkgconf client object to use for dependency resolution.
28 * :param char* filename: The filename of the package file (including full path).
29 * :param FILE* f: The file object to read from.
30 * :returns: A ``pkgconf_pkg_t`` object which contains the package data.
31 * :rtype: pkgconf_pkg_t *
32 */
33 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)34 pkgconf_parser_parse(FILE *f, void *data, const pkgconf_parser_operand_func_t *ops, const pkgconf_parser_warn_func_t warnfunc, const char *filename)
35 {
36 pkgconf_buffer_t readbuf = PKGCONF_BUFFER_INITIALIZER;
37 size_t lineno = 0;
38 bool continue_reading = true;
39
40 while (continue_reading)
41 {
42 char op, *p, *key, *value;
43 bool warned_key_whitespace = false, warned_value_whitespace = false;
44
45 continue_reading = pkgconf_fgetline(&readbuf, f);
46 lineno++;
47
48 p = readbuf.base;
49 if (p == NULL)
50 continue;
51 while (*p && isspace((unsigned char)*p))
52 p++;
53 if (*p && p != readbuf.base)
54 {
55 warnfunc(data, "%s:" SIZE_FMT_SPECIFIER ": warning: whitespace encountered while parsing key section\n",
56 filename, lineno);
57 warned_key_whitespace = true;
58 }
59 key = p;
60 while (*p && (isalpha((unsigned char)*p) || isdigit((unsigned char)*p) || *p == '_' || *p == '.'))
61 p++;
62
63 if (!isalpha((unsigned char)*key) &&
64 !isdigit((unsigned char)*p))
65 {
66 pkgconf_buffer_reset(&readbuf);
67 continue;
68 }
69
70 while (*p && isspace((unsigned char)*p))
71 {
72 if (!warned_key_whitespace)
73 {
74 warnfunc(data, "%s:" SIZE_FMT_SPECIFIER ": warning: whitespace encountered while parsing key section\n",
75 filename, lineno);
76 warned_key_whitespace = true;
77 }
78
79 /* set to null to avoid trailing spaces in key */
80 *p = '\0';
81 p++;
82 }
83
84 op = *p;
85 if (*p != '\0')
86 {
87 *p = '\0';
88 p++;
89 }
90
91 while (*p && isspace((unsigned char)*p))
92 p++;
93
94 value = p;
95 p = value + (strlen(value) - 1);
96 while (*p && isspace((unsigned char) *p) && p > value)
97 {
98 if (!warned_value_whitespace && op == '=')
99 {
100 warnfunc(data, "%s:" SIZE_FMT_SPECIFIER ": warning: trailing whitespace encountered while parsing value section\n",
101 filename, lineno);
102 warned_value_whitespace = true;
103 }
104
105 *p = '\0';
106 p--;
107 }
108 if (ops[(unsigned char) op])
109 ops[(unsigned char) op](data, lineno, key, value);
110
111 pkgconf_buffer_reset(&readbuf);
112 }
113
114 pkgconf_buffer_finalize(&readbuf);
115 }
116