xref: /freebsd/sbin/veriexec/manifest_lexer.l (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1 %{
2 /*-
3  * Copyright (c) 2004-2018, Juniper Networks, Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <stdio.h>
30 #include <string.h>
31 #include "veriexec.h"
32 #include "manifest_parser.h"
33 
34 #define YY_NO_UNPUT
35 
36 int lineno = 1;
37 int bol = 1;
38 extern int parser_version;
39 
40 void yyerror(const char *message);
41 void warning(const char *message);
42 int yylex(void);
43 
44 %}
45 
46 %%
47 
48 \n {
49 	lineno++;
50 	bol=1;
51 	return EOL;
52 }
53 
54 [/a-zA-Z0-9\.][^ \t\n=]*  {
55 	yylval.string = strdup(yytext);
56 	if (bol) {
57 		bol=0;
58 		return PATH;
59 	} else
60 		return STRING;
61 }
62 
63 = {
64 	return EQ;
65 }
66 
67 
68 [ \t\r] ;  /* eat white ones */
69 
70 #>[0-9]+ {
71         /*
72          * If we are older than the specified version
73          * ignore rest of line, otherwise just discard this token.
74          */
75         int skip = atoi(&yytext[2]);
76 
77         VERBOSE(3, ("%s: skip if %d <= %d\n", yytext, parser_version, skip));
78         if (parser_version <= skip) {
79 		/* treat as a comment, yyless() is cheaper than yyunput() */
80 		yytext[yyleng - 1] = '#';
81 		yyless(2);
82         }
83 }
84 
85 #[^>\n].* ;      /* comment */
86 
87 .    yyerror("invalid character");
88 
89 %%
90 
91 static char *manifest_file = NULL;
92 
93 struct string_buf {
94 	const char *buf;
95 	size_t pos, size;
96 };
97 
98 static int
99 read_string_buf (void *token, char *dest, int count)
100 {
101 	struct string_buf *str_buf_p = (struct string_buf *)token;
102 	ssize_t n;
103 
104 	if (count < 0)
105 		return 0;
106 
107 	n = str_buf_p->size - str_buf_p->pos;
108 	if (count < n)
109 		n = count;
110 
111 	memcpy(dest, str_buf_p->buf + str_buf_p->pos, n);
112 	str_buf_p->pos += n;
113 
114 	return n;
115 }
116 
117 FILE *
118 manifest_open (const char *file, const char *file_content)
119 {
120 	static struct string_buf str_buf;
121 
122 	if (manifest_file) {
123 		free(manifest_file);
124 		fclose(yyin);
125 	}
126 
127 	str_buf.buf  = file_content;
128 	str_buf.pos  = 0;
129 	str_buf.size = strlen(file_content);
130 	yyin = fropen(&str_buf, read_string_buf);
131 	if (yyin) {
132 		manifest_file = strdup(file);
133 		lineno = 1;
134 		manifest_parser_init();
135 	} else
136 		manifest_file = NULL;
137 	return yyin;
138 }
139 
140 void
141 yyerror(const char *string)
142 {
143 	fprintf(stderr, "%s: %d: %s at %s\n",
144 	    manifest_file, lineno, string, yytext);
145 }
146 
147 int
148 yywrap(void)
149 {
150 	return (1);
151 }
152