forked from specialunderwear/Hosts.prefpane
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhostsparser.y
More file actions
128 lines (103 loc) · 4.38 KB
/
Copy pathhostsparser.y
File metadata and controls
128 lines (103 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
%{
// Hosts, a system preference pane to manage your hosts file.
// Copyright (C) 2011 PermanentMarkers
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// contact maintainer at hosts@permanentmarkers.nl
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "hosts.h"
#include "hostsparser.h"
#ifndef YYSTYPE
#define YYSTYPE NLPERMANENTMARKERSHOSTS_YYSTYPE
#define YYSTYPE_IS_TRIVIAL 1
#define yystype YYSTYPE
#define YYSTYPE_IS_DECLARED 1
typedef union YYSTYPE
{
NLPERMANENTMARKERSHOSTS_CHostEntry * hostentry;
char * string;
} YYSTYPE;
#endif
// predeclare bison functions
void NLPERMANENTMARKERSHOSTSerror (void * scanner, char const * error);
// predeclare lex functions.
FILE *NLPERMANENTMARKERSHOSTSget_in (void * scanner);
void NLPERMANENTMARKERSHOSTSset_in (FILE * in_str , void * scanner);
YYSTYPE * NLPERMANENTMARKERSHOSTSget_lval (void * scanner );
// callback for parser results.
void (*NLPERMANENTMARKERSHOSTS_callback)(NLPERMANENTMARKERSHOSTS_CHostEntry *) = NULL;
// callback for parser errors.
void (*NLPERMANENTMARKERSHOSTS_error_callback)(NLPERMANENTMARKERSHOSTS_CHostEntryError *) = NULL;
void NLPERMANENTMARKERSHOSTSparsefile(char * filename, void (*callback)(NLPERMANENTMARKERSHOSTS_CHostEntry *), void (*error_handler)(NLPERMANENTMARKERSHOSTS_CHostEntryError *) ) {
void * scanner;
NLPERMANENTMARKERSHOSTS_callback = callback;
NLPERMANENTMARKERSHOSTS_error_callback = error_handler;
NLPERMANENTMARKERSHOSTSlex_init (&scanner);
FILE *file_handle = fopen(filename, "r");
NLPERMANENTMARKERSHOSTSset_in(file_handle, scanner);
NLPERMANENTMARKERSHOSTSparse(scanner);
NLPERMANENTMARKERSHOSTSlex_destroy(scanner);
}
%}
%define api.pure
%error-verbose
%name-prefix="NLPERMANENTMARKERSHOSTS"
%expect 2
%token COMMENT ADDRESS HOSTNAME NEWLINE
%token END 0
%union {
NLPERMANENTMARKERSHOSTS_CHostEntry * hostentry;
char * string;
}
%type <string> ADDRESS COMMENT HOSTNAME
%type <hostentry> hostnames
%type <hostentry> rule
%destructor { free($$); } ADDRESS COMMENT HOSTNAME
%destructor { NLPERMANENTMARKERSHOSTS_chostentry_destroy($$);} hostnames rule
%parse-param {void *scanner}
%lex-param {yyscan_t *scanner}
%%
input:
| input line
;
hostnames: HOSTNAME { $$ = NLPERMANENTMARKERSHOSTS_chostentry_create(NULL, $1, NULL); }
| hostnames HOSTNAME { $$ = NLPERMANENTMARKERSHOSTS_chostentry_add_hostname($1, $2); }
;
line: rule NEWLINE { NLPERMANENTMARKERSHOSTS_callback($1); }
| rule { NLPERMANENTMARKERSHOSTS_callback($1);}
| NEWLINE { NLPERMANENTMARKERSHOSTS_callback(NLPERMANENTMARKERSHOSTS_chostentry_create(NULL, NULL, NULL)); }
| error NEWLINE {yyerrok;}
;
rule: ADDRESS hostnames COMMENT { $2->address = $1; $2->comment = $3; $$ = $2;}
| ADDRESS hostnames { $2->address = $1; $$ = $2;}
| COMMENT { $$ = NLPERMANENTMARKERSHOSTS_chostentry_create(NULL, NULL, $1);}
;
%%
void NLPERMANENTMARKERSHOSTSerror (void * scanner, char const * error) {
YYSTYPE * lval = NLPERMANENTMARKERSHOSTSget_lval(scanner);
if (NLPERMANENTMARKERSHOSTS_error_callback != NULL) {
NLPERMANENTMARKERSHOSTS_CHostEntryError error_obj;
error_obj.linenumber = NLPERMANENTMARKERSHOSTSget_lineno(scanner);
error_obj.error = strdup(error);
error_obj.token = strdup(lval->string);
NLPERMANENTMARKERSHOSTS_error_callback(&error_obj);
free(error_obj.error);
free(error_obj.token);
} else {
printf("Error parsing /etc/hosts line:%i %s\n%s\n", NLPERMANENTMARKERSHOSTSget_lineno(scanner), lval->string, error);
}
}