| 1 | # Bind8 Conf parser | ||
| 2 | # | ||
| 3 | # Copyright Karthik Krishnamurthy <karthik.k@extremix.net> | ||
| 4 | |||
| 5 | package Unix::Conf::Bind8::Conf; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | use Unix::Conf::Bind8::Conf::Lib; | ||
| 11 | |||
| 12 | use constant EOF => 0; | ||
| 13 | use constant NEWLINE => 1; | ||
| 14 | use constant COMMENT => 2; | ||
| 15 | use constant OPENCCOMMENT => 3; | ||
| 16 | use constant CLOSECCOMMENT => 4; | ||
| 17 | use constant SEMICOLON => 5; | ||
| 18 | use constant OPENBRACE => 6; | ||
| 19 | use constant CLOSEBRACE => 7; | ||
| 20 | use constant EXCLAMATION => 8; | ||
| 21 | use constant ASTERISK => 9; | ||
| 22 | use constant NUMBER => 10; | ||
| 23 | use constant STRING => 11; | ||
| 24 | use constant WORD => 12; | ||
| 25 | use constant IPv4 => 13; | ||
| 26 | use constant IP_PREFIX => 14; | ||
| 27 | use constant _WHITESPACE => 15; | ||
| 28 | use constant UNRECOGNISED => 16; | ||
| 29 | |||
| 30 | my @TokenType = ( | ||
| 31 | "EOF", | ||
| 32 | "NEWLINE", | ||
| 33 | "COMMENT", | ||
| 34 | "OPENCCOMMENT", | ||
| 35 | "CLOSECCOMMENT", | ||
| 36 | "SEMICOLON", | ||
| 37 | "OPENBRACE", | ||
| 38 | "CLOSEBRACE", | ||
| 39 | "EXCLAMATION", | ||
| 40 | "ASTERISK", | ||
| 41 | "NUMBER", | ||
| 42 | "STRING", | ||
| 43 | "WORD", | ||
| 44 | "IPv4", | ||
| 45 | "IP_PREFIX", | ||
| 46 | "_WHITESPACE", | ||
| 47 | "UNRECOGNISED", | ||
| 48 | ); | ||
| 49 | |||
| 50 | my ($Conf, $FH, $Line, $Token, $ConfName, $Lineno); | ||
| 51 | my $Err = []; | ||
| 52 | # the following are treated as references, so that we can | ||
| 53 | # avoid the overhead of passing these string by copy to _rstring, _tws | ||
| 54 | # methods. | ||
| 55 | # my ($RString, $TWS); | ||
| 56 | our ($RString, $TWS); | ||
| 57 | # store positional info on where the last successfully accepted | ||
| 58 | # token ends. R_Offset contains the offset into RString, where | ||
| 59 | # the last token ends, _Lineno, the lineno, and L_Offset the pos () | ||
| 60 | # where the last token match ended. | ||
| 61 | my ($R_Offset, $_Lineno, $L_Offset); | ||
| 62 | |||
| 63 | ################## PARSER STATE DECLARATIONS ######################## | ||
| 64 | |||
| 65 | use constant P_NORMAL => 1; | ||
| 66 | use constant P_INDIRECTIVE => 2; | ||
| 67 | |||
| 68 | my $ParserState; | ||
| 69 | |||
| 70 | ##################################################################### | ||
| 71 | |||
| 72 | ################## LEXER STATE DECLARATIONS ######################## | ||
| 73 | |||
| 74 | use constant L_NORMAL => 1; | ||
| 75 | use constant L_INCOMMENT => 2; | ||
| 76 | my $LexerState; | ||
| 77 | |||
| 78 | ##################################################################### | ||
| 79 | |||
| 80 | |||
| 81 | # store the above data here in case we call _parse_conf recursively | ||
| 82 | # in case of an include directive. | ||
| 83 | my (@ParseStack); | ||
| 84 | |||
| 85 | sub __pushfile () | ||
| 86 | { | ||
| 87 | push ( | ||
| 88 | @ParseStack, | ||
| 89 | [ | ||
| 90 | $Conf, $FH, | ||
| 91 | $Line, $Token, | ||
| 92 | $ConfName, $Lineno, | ||
| 93 | $RString, $TWS, | ||
| 94 | $R_Offset, $_Lineno, | ||
| 95 | $L_Offset, $ParserState, | ||
| 96 | $LexerState, pos ($Line), | ||
| 97 | ] | ||
| 98 | ); | ||
| 99 | ( | ||
| 100 | $Conf, $FH, | ||
| 101 | $Line, $Token, | ||
| 102 | $ConfName, $Lineno, | ||
| 103 | $RString, $TWS, | ||
| 104 | $R_Offset, $_Lineno, | ||
| 105 | $L_Offset, $ParserState, | ||
| 106 | $LexerState, | ||
| 107 | ) = (undef) x 13; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub __popfile () | ||
| 111 | { | ||
| 112 | my $pos; | ||
| 113 | ( | ||
| 114 | $Conf, $FH, | ||
| 115 | $Line, $Token, | ||
| 116 | $ConfName, $Lineno, | ||
| 117 | $RString, $TWS, | ||
| 118 | $R_Offset, $_Lineno, | ||
| 119 | $L_Offset, $ParserState, | ||
| 120 | $LexerState, $pos, | ||
| 121 | ) = @{pop (@ParseStack)}; | ||
| 122 | pos ($Line) = $pos; | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | # Change to add this to the Conf object itself later using $Conf->__add_err () | ||
| 127 | sub __die ($;$) | ||
| 128 | { | ||
| 129 | die ($_[0]) if (@_ == 1); | ||
| 130 | die (Unix::Conf->_err ($_[0]." ($ConfName:$Lineno)", $_[1]."\n" )); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub __warn ($;$) | ||
| 134 | { | ||
| 135 | if (@_ == 1) { | ||
| 136 | $Conf->__add_err ($_[0]); | ||
| 137 | return (); | ||
| 138 | } | ||
| 139 | $Conf->__add_err (Unix::Conf->_err ($_[0]." ($ConfName:$Lineno)", $_[1]."\n" )); | ||
| 140 | } | ||
| 141 | |||
| 142 | use constant T_TOKENTYPE => 0; | ||
| 143 | use constant T_TOKEN => 1; | ||
| 144 | use constant T_ROFFSET => 2; | ||
| 145 | use constant T_LINENO => 3; | ||
| 146 | use constant T_LOFFSET => 4; | ||
| 147 | |||
| 148 | sub tokentype ($) { return $TokenType[$_[0]->[T_TOKENTYPE]]; } | ||
| 149 | |||
| 150 | my %Dispatch = ( | ||
| 151 | 'acl' => \&__parse_named_acl, | ||
| 152 | 'controls' => \&__parse_controls, | ||
| 153 | 'include' => \&__parse_include, | ||
| 154 | 'key' => \&__parse_key, | ||
| 155 | 'logging' => \&__parse_logging, | ||
| 156 | 'options' => \&__parse_options, | ||
| 157 | 'server' => \&__parse_server, | ||
| 158 | 'trusted-keys' => \&__parse_trustedkeys, | ||
| 159 | 'zone' => \&__parse_zone, | ||
| 160 | ); | ||
| 161 | |||
| 162 | sub __parse_conf ($$) | ||
| 163 | { | ||
| 164 | __pushfile (); | ||
| 165 | |||
| 166 | ($Conf, $FH) = ($_[0], $_[0]->fh ()); | ||
| 167 | $ConfName = "$FH"; | ||
| 168 | ($Lineno, $L_Offset, $R_Offset) = (0, 0, 0); | ||
| 169 | my ($token, $handler, $obj); | ||
| 170 | |||
| 171 | |||
| 172 | $ParserState = P_NORMAL; $LexerState = L_NORMAL; | ||
| 173 | $RString = undef; | ||
| 174 | |||
| 175 | while (($token = __gettoken ())->[T_TOKENTYPE] != EOF) { | ||
| 176 | # set the TWS for the last parsed object. | ||
| 177 | # print "TWS\n[->$$TWS<-]\n" if (defined ($TWS)); | ||
| 178 | $obj->_tws ($TWS) if ($obj); | ||
| 179 | $TWS = undef; | ||
| 180 | if ($token->[T_TOKENTYPE] == COMMENT) { | ||
| 181 | $obj = $Conf->new_comment (); | ||
| 182 | } | ||
| 183 | elsif ($token->[T_TOKENTYPE] == WORD) { | ||
| 184 | # at this point it is still an assumption. | ||
| 185 | $ParserState = P_INDIRECTIVE; | ||
| 186 | __die ("_parse_conf", "illegal/unsupported directive `$token->[T_TOKEN]', aborting") | ||
| 187 | unless (defined ($handler = $Dispatch{$token->[T_TOKEN]})); | ||
| 188 | $obj = &$handler(); | ||
| 189 | } | ||
| 190 | # the file could be emtpy or the return an error | ||
| 191 | if ($obj) { | ||
| 192 | $ParserState = P_NORMAL; | ||
| 193 | $obj->_rstring ($RString); | ||
| 194 | # print "DIRECTIVE \n[->$$RString<-]\n" if (defined ($RString)); | ||
| 195 | $RString = undef; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | $Conf->dirty (0); | ||
| 199 | __popfile (); | ||
| 200 | return (1); | ||
| 201 | } | ||
| 202 | |||
| 203 | ############################## Utility functions ########################################### | ||
| 204 | |||
| 205 | sub __slurp_semicolon () | ||
| 206 | { | ||
| 207 | my $token; | ||
| 208 | if (($token = __gettoken ())->[T_TOKENTYPE] != SEMICOLON) { | ||
| 209 | __ungettoken ($token); | ||
| 210 | __warn ( | ||
| 211 | 'slurp_semicolon', | ||
| 212 | "expected token `SEMICOLON', got `$TokenType[$token->[T_TOKENTYPE]]', ignored" | ||
| 213 | ); | ||
| 214 | return; | ||
| 215 | } | ||
| 216 | return (1); | ||
| 217 | } | ||
| 218 | |||
| 219 | sub __slurp_openbr () | ||
| 220 | { | ||
| 221 | my $token; | ||
| 222 | if (($token = __gettoken ())->[T_TOKENTYPE] != OPENBRACE) { | ||
| 223 | __ungettoken ($token); | ||
| 224 | __warn ( | ||
| 225 | 'slurp_openbr', | ||
| 226 | "expected token `OPENBRACE', got `$TokenType[$token->[T_TOKENTYPE]]', ignored" | ||
| 227 | ); | ||
| 228 | return; | ||
| 229 | } | ||
| 230 | return (1); | ||
| 231 | } | ||
| 232 | |||
| 233 | sub __slurp_closebr () | ||
| 234 | { | ||
| 235 | my $token; | ||
| 236 | if (($token = __gettoken ())->[T_TOKENTYPE] != CLOSEBRACE) { | ||
| 237 | __ungettoken ($token); | ||
| 238 | __warn ( | ||
| 239 | 'slurp_closebr', | ||
| 240 | "expected token `CLOSEBRACE', got `$TokenType[$token->[T_TOKENTYPE]]', ignored" | ||
| 241 | ); | ||
| 242 | return; | ||
| 243 | } | ||
| 244 | return (1); | ||
| 245 | } | ||
| 246 | |||
| 247 | |||
| 248 | ################################## Parse functions ############################################ | ||
| 249 | |||
| 250 | # subroutine to eat up a directive. for unsupported directives. | ||
| 251 | # returns an instance of Unix::Conf::Bind8::Conf::Dummy. | ||
| 252 | sub __eat_directive () | ||
| 253 | { | ||
| 254 | my (@stack, $token); | ||
| 255 | |||
| 256 | until (($token = __gettoken ())->[T_TOKENTYPE] == OPENBRACE) {;} | ||
| 257 | |||
| 258 | while (($token = __gettoken ())->[T_TOKENTYPE] != EOF) { | ||
| 259 | if ($token->[T_TOKENTYPE] == OPENBRACE) { | ||
| 260 | push (@stack, OPENBRACE); | ||
| 261 | } | ||
| 262 | elsif ($token->[T_TOKENTYPE] == CLOSEBRACE) { | ||
| 263 | last unless (defined (pop (@stack))); | ||
| 264 | } | ||
| 265 | } | ||
| 266 | __slurp_semicolon (); | ||
| 267 | return ($Conf->new_dummy ()); | ||
| 268 | } | ||
| 269 | |||
| 270 | sub __parse_include () | ||
| 271 | { | ||
| 272 | my $token = __gettoken ()->[T_TOKEN]; | ||
| 273 | my ($include, %args); | ||
| 274 | |||
| 275 | $token =~ s/"(.+)"/$1/; | ||
| 276 | $args{FILE} = $token; | ||
| 277 | $args{SECURE_OPEN} = $Conf->fh ()->secure_open (); | ||
| 278 | $include = $Conf->new_include (%args) or die ($include); | ||
| 279 | __slurp_semicolon (); | ||
| 280 | return ($include); | ||
| 281 | } | ||
| 282 | |||
| 283 | # parse list of ipaddresses. used with the masters/forwarders/also-notify | ||
| 284 | # statements in the zone directive and the forwarders statement in the | ||
| 285 | # options directive. Returns an anon array of addresses if defined, else | ||
| 286 | # an undef. | ||
| 287 | sub __parse_ipaddress_list () | ||
| 288 | { | ||
| 289 | my ($token, $ret); | ||
| 290 | |||
| 291 | __slurp_openbr (); | ||
| 292 | while (($token = __gettoken ())->[T_TOKENTYPE] != CLOSEBRACE) { | ||
| 293 | push (@$ret, "$token->[T_TOKEN]"); | ||
| 294 | __slurp_semicolon (); | ||
| 295 | } | ||
| 296 | return ($ret); | ||
| 297 | } | ||
| 298 | |||
| 299 | my @zone_dir1 = qw ( | ||
| 300 | type | ||
| 301 | file | ||
| 302 | notify | ||
| 303 | forward | ||
| 304 | transfer-source | ||
| 305 | check-names | ||
| 306 | max-transfer-time-in | ||
| 307 | ); | ||
| 308 | |||
| 309 | sub __parse_zone () | ||
| 310 | { | ||
| 311 | my ($token, $_token, $zone, $name, $ret); | ||
| 312 | |||
| 313 | local $" = "|"; | ||
| 314 | $zone = $Conf->new_zone ( NAME => ($token = __gettoken ())->[T_TOKEN] ) | ||
| 315 | or __die ($zone); | ||
| 316 | |||
| 317 | if (($token = __gettoken ())->[T_TOKENTYPE] != OPENBRACE) { | ||
| 318 | $ret = $zone->class ($token->[T_TOKEN]) | ||
| 319 | or __warn ($ret); | ||
| 320 | __slurp_openbr (); | ||
| 321 | } | ||
| 322 | |||
| 323 | # now parse zone directives | ||
| 324 | while (($token = __gettoken ())->[T_TOKENTYPE] != CLOSEBRACE) { | ||
| 325 | $_token = $token->[T_TOKEN]; | ||
| 326 | ($_token =~ /^(@zone_dir1)$/) && do { | ||
| 327 | $_token =~ s/-/_/g; | ||
| 328 | $ret = $zone->$_token (__gettoken ()->[T_TOKEN]) | ||
| 329 | or __warn ($ret); | ||
| 330 | goto SEMICOLON; | ||
| 331 | }; | ||
| 332 | ($_token =~ /^allow-(transfer|query|update)$/) && do { | ||
| 333 | my $acl; | ||
| 334 | $_token =~ tr/-/_/; | ||
| 335 | __slurp_openbr (); | ||
| 336 | ($acl = __parse_acl ()) && | ||
| 337 | ($ret = $zone->$_token ($acl) or __warn ($ret)); | ||
| 338 | next; | ||
| 339 | }; | ||
| 340 | ($_token eq 'masters') && do { | ||
| 341 | my ($port, $address); | ||
| 342 | if (($token = __gettoken ())->[T_TOKEN] eq 'port') { | ||
| 343 | $port = __gettoken ()->[T_TOKEN]; | ||
| 344 | } | ||
| 345 | else { | ||
| 346 | __ungettoken ($token); | ||
| 347 | } | ||
| 348 | $address = __parse_ipaddress_list (); | ||
| 349 | unless ($address) { | ||
| 350 | __warn ("__parse_zone", "masters address list cannot be empty"); | ||
| 351 | goto SEMICOLON; | ||
| 352 | } | ||
| 353 | $ret = $zone->masters (PORT => $port, ADDRESS => $address) | ||
| 354 | or __warn ($ret); | ||
| 355 | goto SEMICOLON; | ||
| 356 | }; | ||
| 357 | ($_token =~ /^(also-notify|forwarders)$/) && do { | ||
| 358 | $_token =~ tr/-/_/; | ||
| 359 | my $list = __parse_ipaddress_list (); | ||
| 360 | $list = [] unless (defined ($list)); | ||
| 361 | $ret = $zone->$_token ($list) or __warn ($list); | ||
| 362 | goto SEMICOLON; | ||
| 363 | }; | ||
| 364 | ($_token eq 'pubkey') && do { | ||
| 365 | my $ret; | ||
| 366 | |||
| 367 | __warn ($ret) unless ( | ||
| 368 | $ret = $zone->pubkey ( | ||
| 369 | __gettoken ()->[T_TOKEN], __gettoken ()->[T_TOKEN], | ||
| 370 | __gettoken ()->[T_TOKEN], __gettoken ()->[T_TOKEN], | ||
| 371 | ) | ||
| 372 | ); | ||
| 373 | __slurp_semicolon (); | ||
| 374 | next; | ||
| 375 | }; | ||
| 376 | # unhandled as the syntax does not define this zone directive | ||
| 377 | ($_token eq 'ixfr-base') && do { | ||
| 378 | __gettoken (); | ||
| 379 | __slurp_semicolon (); | ||
| 380 | next; | ||
| 381 | }; | ||
| 382 | __die ('__parse_zone', "`$_token' illegal/unsupported zone directive"); | ||
| 383 | |||
| 384 | SEMICOLON: | ||
| 385 | __slurp_semicolon (); | ||
| 386 | } | ||
| 387 | |||
| 388 | # end of the zone statement | ||
| 389 | __slurp_semicolon (); | ||
| 390 | return ($zone); | ||
| 391 | } | ||
| 392 | |||
| 393 | |||
| 394 | sub __parse_named_acl () { return __parse_acl (1); } | ||
| 395 | |||
| 396 | sub __parse_acl (;$); | ||
| 397 | |||
| 398 | # Arguments: '1' (indicating true) if it is a named acl (acl 'aclname' { ..) | ||
| 399 | # the same subroutine is used to parse acls in front of directives like | ||
| 400 | # allow-query. | ||
| 401 | sub __parse_acl (;$) | ||
| 402 | { | ||
| 403 | my ($named) = @_; | ||
| 404 | my ($token, $_token, $acl, $ret, $element); | ||
| 405 | |||
| 406 | if ($named) { | ||
| 407 | $acl = $Conf->new_acl (NAME => (__gettoken ()->[T_TOKEN])) | ||
| 408 | or __die ($acl); | ||
| 409 | __slurp_openbr (); | ||
| 410 | } | ||
| 411 | else { | ||
| 412 | $acl = $Conf->new_acl () or die ($acl); | ||
| 413 | } | ||
| 414 | |||
| 415 | while (($token = __gettoken ())->[T_TOKENTYPE] != CLOSEBRACE) { | ||
| 416 | $element = ''; | ||
| 417 | if ($token->[T_TOKEN] eq '!') { | ||
| 418 | $element = "!"; | ||
| 419 | $token = __gettoken (); | ||
| 420 | } | ||
| 421 | |||
| 422 | # need to do something to handle when the whole | ||
| 423 | # block is negated like in !{ ...; }; | ||
| 424 | if ($token->[T_TOKENTYPE] == OPENBRACE) { | ||
| 425 | my $pacl; | ||
| 426 | if ($pacl = __parse_acl ()) { | ||
| 427 | $ret = $acl->add_elements ($pacl) | ||
| 428 | or __warn ($ret); | ||
| 429 | } | ||
| 430 | else { | ||
| 431 | __warn ($pacl); | ||
| 432 | } | ||
| 433 | next; | ||
| 434 | } | ||
| 435 | elsif ($token->[T_TOKEN] eq 'key') { | ||
| 436 | $element .= sprintf ("key %s", __gettoken ()->[T_TOKEN]); | ||
| 437 | } | ||
| 438 | else { | ||
| 439 | $element .= $token->[T_TOKEN] | ||
| 440 | } | ||
| 441 | $ret = $acl->add_elements ($element) or __warn ($ret); | ||
| 442 | __slurp_semicolon (); | ||
| 443 | } | ||
| 444 | __slurp_semicolon (); | ||
| 445 | return ($acl); | ||
| 446 | } | ||
| 447 | |||
| 448 | |||
| 449 | sub __parse_options () | ||
| 450 | { | ||
| 451 | my ($token, $_token, $ret); | ||
| 452 | my $options; | ||
| 453 | $options = $Conf->new_options () or __die ($options); | ||
| 454 | |||
| 455 | __slurp_openbr (); | ||
| 456 | while (($token = __gettoken ())->[T_TOKENTYPE] != CLOSEBRACE) { | ||
| 457 | $_token = $token->[T_TOKEN]; | ||
| 458 | ($_token eq 'rrset-order') && do { | ||
| 459 | __slurp_openbr (); | ||
| 460 | while (($token = __gettoken ())->[T_TOKENTYPE] != CLOSEBRACE) { | ||
| 461 | my %args; | ||
| 462 | if ($token->[T_TOKEN] eq 'class') { | ||
| 463 | $args{CLASS} = __gettoken ()->[T_TOKEN]; | ||
| 464 | $token = __gettoken (); | ||
| 465 | } | ||
| 466 | if ($token->[T_TOKEN] eq 'type') { | ||
| 467 | $args{TYPE} = __gettoken ()->[T_TOKEN]; | ||
| 468 | $token = __gettoken (); | ||
| 469 | } | ||
| 470 | if ($token->[T_TOKEN] eq 'name') { | ||
| 471 | $args{NAME} = __gettoken ()->[T_TOKEN]; | ||
| 472 | $token = __gettoken (); | ||
| 473 | } | ||
| 474 | __die ('__parse_options', "expected `order' got `$token->[T_TOKEN]'") | ||
| 475 | unless ($token->[T_TOKEN] eq 'order'); | ||
| 476 | $args{ORDER} = __gettoken ()->[T_TOKEN]; | ||
| 477 | $ret = $options->add_to_rrset_order (%args) | ||
| 478 | or __warn ($ret); | ||
| 479 | __slurp_semicolon (); | ||
| 480 | } | ||
| 481 | goto SEMICOLON; | ||
| 482 | }; | ||
| 483 | ($_token eq 'forwarders') && do { | ||
| 484 | my $list = __parse_ipaddress_list (); | ||
| 485 | $list = [] unless (defined ($list)); | ||
| 486 | $ret = $options->forwarders ($list) | ||
| 487 | or __warn ($ret); | ||
| 488 | goto SEMICOLON; | ||
| 489 | }; | ||
| 490 | ($_token eq 'check-names') && do { | ||
| 491 | $ret = $options->add_to_check_names ( | ||
| 492 | __gettoken ()->[T_TOKEN], __gettoken ()->[T_TOKEN] | ||
| 493 | ) or __warn ($ret); | ||
| 494 | goto SEMICOLON; | ||
| 495 | }; | ||
| 496 | ($_token eq 'listen-on') && do { | ||
| 497 | my ($port, $acl); | ||
| 498 | if (($token = __gettoken ())->[T_TOKEN] eq 'port') { | ||
| 499 | $port = __gettoken ()->[T_TOKEN]; | ||
| 500 | } | ||
| 501 | else { | ||
| 502 | __ungettoken ($token); | ||
| 503 | $port = ''; | ||
| 504 | } | ||
| 505 | __slurp_openbr (); | ||
| 506 | $acl = __parse_acl () or __die ($acl); | ||
| 507 | $ret = $options->add_to_listen_on ($port => $acl->elements ()) | ||
| 508 | or __warn ($ret); | ||
| 509 | next; | ||
| 510 | }; | ||
| 511 | ($_token eq 'query-source') && do { | ||
| 512 | my %args; | ||
| 513 | while (($token = __gettoken ())->[T_TOKENTYPE] != SEMICOLON) { | ||
| 514 | $args{PORT} = __gettoken ()->[T_TOKEN] if ($token->[T_TOKEN] eq 'port'); | ||
| 515 | $args{ADDRESS} = __gettoken ()->[T_TOKEN] if ($token->[T_TOKEN] eq 'address'); | ||
| 516 | } | ||
| 517 | $ret = $options->query_source (%args) or __warn ($ret); | ||
| 518 | next; | ||
| 519 | }; | ||
| 520 | ($_token =~ /^(topology|sortlist|blackhole)$/) && do { | ||
| 521 | my $acl; | ||
| 522 | __slurp_openbr (); | ||
| 523 | ($acl = __parse_acl ()) && | ||
| 524 | ($ret = $options->$_token ($acl) or __warn ($acl)); | ||
| 525 | next; | ||
| 526 | }; | ||
| 527 | ($_token =~ /^allow-(query|recursion|transfer)$/) && do { | ||
| 528 | my $acl; | ||
| 529 | $_token =~ s/-/_/g; | ||
| 530 | __slurp_openbr (); | ||
| 531 | ($acl = __parse_acl ()) && | ||
| 532 | ($ret = $options->$_token ($acl) or __warn ($acl)); | ||
| 533 | next; | ||
| 534 | }; | ||
| 535 | |||
| 536 | # Handle the general case | ||
| 537 | do { | ||
| 538 | __die ('__parse_options', "`$_token' illegal/unsupported option") | ||
| 539 | unless ($options->__valid_option ($_token)); | ||
| 540 | $_token =~ s/-/_/g; | ||
| 541 | $ret = $options->$_token (__gettoken ()->[T_TOKEN]) or __warn ($ret); | ||
| 542 | goto SEMICOLON; | ||
| 543 | }; | ||
| 544 | |||
| 545 | SEMICOLON: __slurp_semicolon (); | ||
| 546 | } | ||
| 547 | |||
| 548 | __slurp_semicolon (); | ||
| 549 | return ($options); | ||
| 550 | } | ||
| 551 | |||
| 552 | |||
| 553 | sub __parse_logging () | ||
| 554 | { | ||
| 555 | my ($token, $logging); | ||
| 556 | |||
| 557 | $logging = $Conf->new_logging () or die ($logging); | ||
| 558 | __slurp_openbr (); | ||
| 559 | while (($token = __gettoken ())->[T_TOKENTYPE] != CLOSEBRACE) { | ||
| 560 | ($token->[T_TOKEN] eq 'channel') && do { __parse_channel ($logging); }; | ||
| 561 | ($token->[T_TOKEN] eq 'category') && do { __parse_category ($logging); }; | ||
| 562 | } | ||
| 563 | __slurp_semicolon (); | ||
| 564 | return ($logging); | ||
| 565 | } | ||
| 566 | |||
| 567 | sub __parse_channel ($) | ||
| 568 | { | ||
| 569 | my $logging = $_[0]; | ||
| 570 | my ($ret, $channel, $token, $_token, $path, $versions, $size); | ||
| 571 | |||
| 572 | $channel = $logging->new_channel (NAME => __gettoken ()->[T_TOKEN]) | ||
| 573 | or __die ($channel); | ||
| 574 | __slurp_openbr (); | ||
| 575 | |||
| 576 | until (($token = __gettoken ())->[T_TOKENTYPE] == CLOSEBRACE) { | ||
| 577 | $_token = $token->[T_TOKEN]; | ||
| 578 | ($_token eq 'file') && do { | ||
| 579 | my @args; | ||
| 580 | $ret = $channel->output ($_token) or __warn ($ret); | ||
| 581 | push (@args, ( PATH => __gettoken ()->[T_TOKEN])); | ||
| 582 | while (($token = __gettoken ())->[T_TOKENTYPE] != SEMICOLON) { | ||
| 583 | ($token->[T_TOKEN] eq 'versions') and | ||
| 584 | push (@args, ( VERSIONS => __gettoken ()->[T_TOKEN])); | ||
| 585 | ($token->[T_TOKEN] eq 'size') and | ||
| 586 | push (@args, ( SIZE => __gettoken ()->[T_TOKEN])); | ||
| 587 | } | ||
| 588 | |||
| 589 | $ret = $channel->file ( @args ) or __warn ($ret); | ||
| 590 | next; | ||
| 591 | }; | ||
| 592 | # we slurp the semicolon in every case instead of doing in at the bottom | ||
| 593 | # is so that we can check for the illegal channel directive. | ||
| 594 | ($_token eq 'syslog') && do { | ||
| 595 | $ret = $channel->output ($_token) or __warn ($ret); | ||
| 596 | |||
| 597 | # channel boo { syslog; } is legal syntax. refer to the named.conf | ||
| 598 | # in src/bin/named (channel no_info_messages). however the | ||
| 599 | # syntax in the named.conf man page doesnt suggest that | ||
| 600 | if (($token = __gettoken ())->[T_TOKENTYPE] != SEMICOLON) { | ||
| 601 | $ret = $channel->syslog ($token->[T_TOKEN]) or __warn ($ret); | ||
| 602 | __slurp_semicolon (); | ||
| 603 | } | ||
| 604 | next; | ||
| 605 | }; | ||
| 606 | ($_token eq 'null') && do { | ||
| 607 | $ret = $channel->output ($_token) or __warn ($ret); | ||
| 608 | __slurp_semicolon (); | ||
| 609 | next; | ||
| 610 | }; | ||
| 611 | ($_token eq 'severity') && do { | ||
| 612 | my @args = ( NAME => __gettoken ()->[T_TOKEN]); | ||
| 613 | # check to see if there is a debug level | ||
| 614 | if (($token = __gettoken ())->[T_TOKENTYPE] != SEMICOLON) { | ||
| 615 | push (@args, (LEVEL => $token->[T_TOKEN])); | ||
| 616 | __slurp_semicolon (); | ||
| 617 | } | ||
| 618 | $ret = $channel->severity (@args) or __warn ($ret); | ||
| 619 | next; | ||
| 620 | }; | ||
| 621 | ($_token =~ /^print-(category|severity|time)$/) && do { | ||
| 622 | # convert into method name | ||
| 623 | $_token =~ s/-/_/g; | ||
| 624 | $ret = $channel->$_token (__gettoken ()->[T_TOKEN]) | ||
| 625 | or __warn ($ret); | ||
| 626 | __slurp_semicolon (); | ||
| 627 | next; | ||
| 628 | }; | ||
| 629 | die (Unix::Conf->_err ('__parse_channel', "`$_token' illegal channel directive")); | ||
| 630 | } | ||
| 631 | |||
| 632 | __slurp_semicolon (); | ||
| 633 | return (1); | ||
| 634 | } | ||
| 635 | |||
| 636 | sub __parse_category ($) | ||
| 637 | { | ||
| 638 | my $logging = $_[0]; | ||
| 639 | my ($ret, $channel, $category, @channels, @pos); | ||
| 640 | |||
| 641 | $category = __gettoken ()->[T_TOKEN]; | ||
| 642 | __slurp_openbr (); | ||
| 643 | while (($channel = __gettoken ()->[T_TOKEN]) ne '}') { | ||
| 644 | if ($logging->__valid_channel ($channel)) { | ||
| 645 | push (@channels, $channel); | ||
| 646 | } | ||
| 647 | else { | ||
| 648 | die (Unix::Conf->_err ('__parse_category', "`$channel' undefined channel")); | ||
| 649 | } | ||
| 650 | __slurp_semicolon (); | ||
| 651 | } | ||
| 652 | __slurp_semicolon (); | ||
| 653 | $ret = $logging->category ($category, \@channels) or __warn ($ret); | ||
| 654 | return (1); | ||
| 655 | } | ||
| 656 | |||
| 657 | sub __parse_trustedkeys | ||
| 658 | { | ||
| 659 | my ($tk, $token, $ret); | ||
| 660 | |||
| 661 | $tk = $Conf->new_trustedkeys () or __die ($tk); | ||
| 662 | __slurp_openbr (); | ||
| 663 | until (($token = __gettoken ())->[T_TOKENTYPE] == CLOSEBRACE) { | ||
| 664 | $ret = $tk->add_key ( | ||
| 665 | $token->[T_TOKEN], | ||
| 666 | __gettoken ()->[T_TOKEN], __gettoken ()->[T_TOKEN], | ||
| 667 | __gettoken ()->[T_TOKEN], __gettoken ()->[T_TOKEN], | ||
| 668 | ) or __warn ($ret); | ||
| 669 | __slurp_semicolon (); | ||
| 670 | } | ||
| 671 | __slurp_semicolon (); | ||
| 672 | return ($tk); | ||
| 673 | } | ||
| 674 | |||
| 675 | sub __parse_key | ||
| 676 | { | ||
| 677 | my ($token, $name, $algo, $secret, $obj); | ||
| 678 | $name = __gettoken ()->[T_TOKEN]; | ||
| 679 | __slurp_openbr (); | ||
| 680 | __die ('__parse_key', "expected `algorithm' got `$token->[T_TOKEN]'") | ||
| 681 | if (($token = __gettoken ())->[T_TOKEN] ne 'algorithm'); | ||
| 682 | $algo = __gettoken ()->[T_TOKEN]; | ||
| 683 | __slurp_semicolon (); | ||
| 684 | __die ('__parse_key', "expected `secret' got `$token->[T_TOKEN]'") | ||
| 685 | if (($token = __gettoken ())->[T_TOKEN] ne 'secret'); | ||
| 686 | $secret = __gettoken ()->[T_TOKEN]; | ||
| 687 | __slurp_semicolon (); __slurp_closebr (); __slurp_semicolon (); | ||
| 688 | |||
| 689 | $obj = $Conf->new_key (NAME => $name, ALGORITHM => $algo, SECRET => $secret) | ||
| 690 | or __die ($obj); | ||
| 691 | return ($obj); | ||
| 692 | } | ||
| 693 | |||
| 694 | sub __parse_controls | ||
| 695 | { | ||
| 696 | my ($token, $controls, $ret); | ||
| 697 | $controls = $Conf->new_controls () or __die ($controls); | ||
| 698 | __slurp_openbr (); | ||
| 699 | until (($token = __gettoken ())->[T_TOKENTYPE] == CLOSEBRACE) { | ||
| 700 | if ($token->[T_TOKEN] eq 'inet') { | ||
| 701 | my ($addr, $port, $allow); | ||
| 702 | $addr = __gettoken ()->[T_TOKEN]; | ||
| 703 | __warn ('__parse_controls', "expected `port' got `$token->[T_TOKEN]'") | ||
| 704 | unless (__gettoken ()->[T_TOKEN] eq 'port'); | ||
| 705 | $port = __gettoken ()->[T_TOKEN]; | ||
| 706 | __warn ('__parse_controls', "expected `allow' got `$token->[T_TOKEN]'") | ||
| 707 | unless (__gettoken ()->[T_TOKEN] eq 'allow'); | ||
| 708 | __slurp_openbr (); | ||
| 709 | # NOTE | ||
| 710 | # __parse_acl () will read in the trailing semicolon | ||
| 711 | # ideally it shouldn't unless it is a named acl | ||
| 712 | $allow = __parse_acl () or __warn ($allow); | ||
| 713 | # invoke method only if the __parse_acl call above suceeded | ||
| 714 | $ret = $controls->inet ($addr, $port, $allow) or __warn ($ret) | ||
| 715 | if ($allow); | ||
| 716 | } | ||
| 717 | elsif ($token->[T_TOKEN] eq 'unix') { | ||
| 718 | my ($path, $perm, $owner, $group); | ||
| 719 | $path = __gettoken ()->[T_TOKEN]; | ||
| 720 | __warn ('__parse_controls', "expected `perm' got `$token->[T_TOKEN]'") | ||
| 721 | unless (__gettoken ()->[T_TOKEN] eq 'perm'); | ||
| 722 | $perm = __gettoken ()->[T_TOKEN]; | ||
| 723 | __warn ('__parse_controls', "expected `owner' got `$token->[T_TOKEN]'") | ||
| 724 | unless (__gettoken ()->[T_TOKEN] eq 'owner'); | ||
| 725 | $owner = __gettoken ()->[T_TOKEN]; | ||
| 726 | __warn ('__parse_controls', "expected `group' got `$token->[T_TOKEN]'") | ||
| 727 | unless (__gettoken ()->[T_TOKEN] eq 'group'); | ||
| 728 | $group = __gettoken ()->[T_TOKEN]; | ||
| 729 | $ret = $controls->unix ($path, $perm, $owner, $group) or __warn ($ret); | ||
| 730 | __slurp_semicolon (); | ||
| 731 | } | ||
| 732 | else { | ||
| 733 | return ( | ||
| 734 | Unix::Conf->_err ( | ||
| 735 | '__parse_controls', | ||
| 736 | "unrecognized token `$token->[T_TOKEN]' in controls" | ||
| 737 | ) | ||
| 738 | ); | ||
| 739 | } | ||
| 740 | } | ||
| 741 | __slurp_semicolon (); | ||
| 742 | return ($controls); | ||
| 743 | } | ||
| 744 | |||
| 745 | my @server_statements = qw ( | ||
| 746 | bogus | ||
| 747 | support-ixfr | ||
| 748 | transfers | ||
| 749 | transfer-format | ||
| 750 | ); | ||
| 751 | |||
| 752 | sub __parse_server | ||
| 753 | { | ||
| 754 | my ($token, $_token, $server, $ret); | ||
| 755 | |||
| 756 | $server = $Conf->new_server ( NAME => __gettoken ()->[T_TOKEN] ) | ||
| 757 | or __die ($server); | ||
| 758 | __slurp_openbr (); | ||
| 759 | until (($token = __gettoken ())->[T_TOKENTYPE] == CLOSEBRACE) { | ||
| 760 | no strict 'refs'; | ||
| 761 | $_token = $token->[T_TOKEN]; | ||
| 762 | local $" = "|"; | ||
| 763 | ($_token =~ /^(@server_statements)$/) && do { | ||
| 764 | my $meth = $1; | ||
| 765 | $meth =~ tr/-/_/; | ||
| 766 | $ret = $server->$meth (__gettoken ()->[T_TOKEN]) | ||
| 767 | or __warn ($ret); | ||
| 768 | goto SEMICOLON; | ||
| 769 | }; | ||
| 770 | ($_token eq 'keys') && do { | ||
| 771 | my @keys; | ||
| 772 | __slurp_openbr (); | ||
| 773 | until (($token = __gettoken ())->[T_TOKENTYPE] == CLOSEBRACE) { | ||
| 774 | push (@keys, $token->[T_TOKEN]); | ||
| 775 | __slurp_semicolon (); | ||
| 776 | } | ||
| 777 | $ret = $server->keys (@keys) or __warn ($ret); | ||
| 778 | }; | ||
| 779 | SEMICOLON: | ||
| 780 | __slurp_semicolon (); | ||
| 781 | } | ||
| 782 | __slurp_semicolon (); | ||
| 783 | return ($server); | ||
| 784 | } | ||
| 785 | |||
| 786 | |||
| 787 | ############################################ Lexer ################################################### | ||
| 788 | |||
| 789 | # don't use [:posix:], as we may make this code compatible | ||
| 790 | # later with perl version below 5.6.0. | ||
| 791 | our $rHex = qr/[0-9a-fA-F]{0,4}/; | ||
| 792 | our $rDD = qr/[0-2]?[0-9]?[0-9]/; | ||
| 793 | |||
| 794 | our $rIPv4 = qr/(?:$rDD\.){3}$rDD/o; | ||
| 795 | #our $rIPv6 = qr/(?:$rHex:){1,7}$rHex/o; | ||
| 796 | our $rIPv4_PREFIX = qr($rDD(\.$rDD){0,3}/[1-3]?[0-9])o; | ||
| 797 | |||
| 798 | my @LexStack; | ||
| 799 | # this function is called when recursive calls to __gettoken () | ||
| 800 | # when a comment has been encountered. So after storing | ||
| 801 | # the original positional info, we store the info after | ||
| 802 | # the comment has been matched. This is needed if this state | ||
| 803 | # (//|#)<newline>. For // & # tokens are read in until newline. | ||
| 804 | # so we need to unget the newline, which is treated as TWS. | ||
| 805 | # As after matching '//' or '#' __gettoken () does | ||
| 806 | # not store positional info which is only stored just | ||
| 807 | # before returning the token. so push the stack, and store the | ||
| 808 | # positional info after, which is used to unget the newline to | ||
| 809 | # //|#.. | ||
| 810 | sub __push_lexstack () | ||
| 811 | { | ||
| 812 | push (@LexStack, [ $R_Offset, $_Lineno, $L_Offset ]); | ||
| 813 | $L_Offset = pos ($Line); | ||
| 814 | $R_Offset = length ($$RString); | ||
| 815 | $_Lineno = $Lineno; | ||
| 816 | } | ||
| 817 | |||
| 818 | sub __pop_lexstack () { ($R_Offset, $_Lineno, $L_Offset) = @{pop (@LexStack)}; } | ||
| 819 | |||
| 820 | # forward decl for recursive calls | ||
| 821 | sub __gettoken (); | ||
| 822 | |||
| 823 | # __gettoken () handles comments by parsing them, by calling itself | ||
| 824 | # recursively to get tokens. While doing so it enters L_INCOMMENT state | ||
| 825 | # where whitespace, newlines are returned, and position info is not | ||
| 826 | # updated. Comments are returned only when the parser is not in the | ||
| 827 | # P_INDIRECTIVE state. While lexing tokens, __gettoken () also carves | ||
| 828 | # up in the input stream part for a directive, comments and all, and | ||
| 829 | # stores in $$RString. This is used to recreate the directive as it is, | ||
| 830 | # in case it is not changed. Comments between directives are treated | ||
| 831 | # as dummy directives. Whitespace following directives (comments), are | ||
| 832 | # stored in $$TWS (traling whitespace), and stored alongwith the $$RString | ||
| 833 | # in the directive object. All this hoopla is so that we can modify as | ||
| 834 | # little of the configuartion file as possible. | ||
| 835 | sub __gettoken () | ||
| 836 | { | ||
| 837 | my $token; | ||
| 838 | |||
| 839 | GETLOOP: | ||
| 840 | while (1) { | ||
| 841 | # $Line empty | ||
| 842 | (!defined ($Line) || $Line =~ m/\G\z/gc) && do { | ||
| 843 | unless (__readline ()) { | ||
| 844 | $token = [EOF, undef, $R_Offset, $_Lineno, $L_Offset]; | ||
| 845 | last GETLOOP; | ||
| 846 | } | ||
| 847 | }; | ||
| 848 | |||
| 849 | # suck up all whitespace and store it in $$TWS if we are | ||
| 850 | # outside a directive. This is attached to the directive | ||
| 851 | # object, so that we can modify as little of a file as | ||
| 852 | # possible. Comments outside directives are also treated | ||
| 853 | # as directives (dummy) and whitespace following it is also | ||
| 854 | # preserved. When a comment is deleted, $$TWS following | ||
| 855 | # it is deleted. TWS stands for trailing whitespace | ||
| 856 | ($ParserState == P_NORMAL && $LexerState == L_NORMAL) && do { | ||
| 857 | while ($Line =~ m/\G(\s+)/gc) { | ||
| 858 | $$TWS .= $1; | ||
| 859 | # readline again | ||
| 860 | if ($Line =~ m/\G\z/gc) { | ||
| 861 | unless (__readline ()) { | ||
| 862 | $token = [EOF, undef, $R_Offset, $_Lineno, $L_Offset]; | ||
| 863 | last GETLOOP; | ||
| 864 | } | ||
| 865 | } | ||
| 866 | } | ||
| 867 | }; | ||
| 868 | |||
| 869 | # whitespace is needed only when inside a comment | ||
| 870 | # as we preserve comments only outside directives. | ||
| 871 | $Line =~ m/\G([ \t]+)/gc && do { | ||
| 872 | $$RString .= $1; | ||
| 873 | # return as we have been called recursively by | ||
| 874 | # __gettoken () and don't need to unget info. | ||
| 875 | if ($LexerState == L_INCOMMENT) { | ||
| 876 | $token = [_WHITESPACE, $1]; | ||
| 877 | last GETLOOP; | ||
| 878 | } | ||
| 879 | next; | ||
| 880 | }; | ||
| 881 | |||
| 882 | # newline is needed only when inside a comment | ||
| 883 | # as we preserve comments only outside directives. | ||
| 884 | # NEWLINE is needed to terminate # and // style | ||
| 885 | # comments. that is why it is not clubbed with | ||
| 886 | # _WHITESPACE above. | ||
| 887 | $Line =~ m/\G(\n)/gc && do { | ||
| 888 | $$RString .= $1; | ||
| 889 | # return as we have been called recursively by | ||
| 890 | # __gettoken () and don't need to unget info. | ||
| 891 | if ($LexerState == L_INCOMMENT) { | ||
| 892 | $token = [NEWLINE, $1, $R_Offset, $_Lineno, $L_Offset]; | ||
| 893 | last GETLOOP; | ||
| 894 | } | ||
| 895 | next; | ||
| 896 | }; | ||
| 897 | |||
| 898 | $Line =~ m/\G("[^"]+")/gc && do { | ||
| 899 | $$RString .= $1; | ||
| 900 | $token = [STRING, $1, $R_Offset, $_Lineno, $L_Offset]; | ||
| 901 | last GETLOOP; | ||
| 902 | }; | ||
| 903 | |||
| 904 | $Line =~ m[\G(#|//)]gc && do { | ||
| 905 | $$RString .= $1; | ||
| 906 | # if we are already inside a comment do not | ||
| 907 | # recognise # or // as starting a comment. | ||
| 908 | # else we will start eating up tokens till newline | ||
| 909 | # while our calling invocation of __gettoken () is | ||
| 910 | # doing the same. | ||
| 911 | if ($LexerState == L_INCOMMENT) { | ||
| 912 | $token = [UNRECOGNISED, $1]; | ||
| 913 | last GETLOOP; | ||
| 914 | } | ||
| 915 | |||
| 916 | my $comment = $1; | ||
| 917 | my $_token; | ||
| 918 | # this makes __gettoken () return whitespace, newlines etc. | ||
| 919 | # which it normally doesn't | ||
| 920 | $LexerState = L_INCOMMENT; | ||
| 921 | # eat data till but not including end of line. | ||
| 922 | __push_lexstack (); | ||
| 923 | while (1) { | ||
| 924 | $_token = __gettoken (); | ||
| 925 | last if ($_token->[T_TOKENTYPE] == EOF); | ||
| 926 | if ($_token->[T_TOKENTYPE] == NEWLINE) { | ||
| 927 | # unget so that the trailing newline will | ||
| 928 | # be read as TWS in the next call. | ||
| 929 | __ungettoken ($_token); | ||
| 930 | last; | ||
| 931 | } | ||
| 932 | $comment .= $_token->[T_TOKEN]; | ||
| 933 | } | ||
| 934 | __pop_lexstack (); | ||
| 935 | $LexerState = L_NORMAL; | ||
| 936 | # comment eaten but do not return it if | ||
| 937 | # we are inside a directive | ||
| 938 | next if ($ParserState == P_INDIRECTIVE); | ||
| 939 | $token = [COMMENT, $comment, $R_Offset, $_Lineno, $L_Offset]; | ||
| 940 | last GETLOOP; | ||
| 941 | }; | ||
| 942 | |||
| 943 | $Line =~ m[\G(\*/)]gc && do { | ||
| 944 | $$RString .= $1; | ||
| 945 | $token = [CLOSECCOMMENT, $1, $R_Offset, $_Lineno, $L_Offset]; | ||
| 946 | last GETLOOP; | ||
| 947 | }; | ||
| 948 | |||
| 949 | $Line =~ m[\G(/\*)]gc && do { | ||
| 950 | $$RString .= $1; | ||
| 951 | # do not recognise start of C style comment if | ||
| 952 | # already within one. | ||
| 953 | if ($LexerState == L_INCOMMENT) { | ||
| 954 | $token = [UNRECOGNISED, $1]; | ||
| 955 | last GETLOOP; | ||
| 956 | } | ||
| 957 | |||
| 958 | my $_token; | ||
| 959 | my $comment = $1; | ||
| 960 | $LexerState = L_INCOMMENT; | ||
| 961 | __push_lexstack (); | ||
| 962 | while (1) { | ||
| 963 | $_token = __gettoken (); | ||
| 964 | __die ("__gettoken", "Unexpected EOF inside comment, aborting") | ||
| 965 | if ($_token->[T_TOKENTYPE] == EOF); | ||
| 966 | $comment .= $_token->[T_TOKEN]; | ||
| 967 | last if ($_token->[T_TOKENTYPE] == CLOSECCOMMENT); | ||
| 968 | } | ||
| 969 | __pop_lexstack (); | ||
| 970 | $LexerState = L_NORMAL; | ||
| 971 | next if ($ParserState == P_INDIRECTIVE); | ||
| 972 | $token = [COMMENT, $comment, $R_Offset, $_Lineno, $L_Offset]; | ||
| 973 | last GETLOOP; | ||
| 974 | }; | ||
| 975 | |||
| 976 | $Line =~ m/\G(;)/gc && do { | ||
| 977 | $$RString .= $1; | ||
| 978 | $token = [SEMICOLON, $1, $R_Offset, $_Lineno, $L_Offset]; | ||
| 979 | last GETLOOP; | ||
| 980 | }; | ||
| 981 | |||
| 982 | $Line =~ m/\G(\{)/gc && do { | ||
| 983 | $$RString .= $1; | ||
| 984 | $token = [OPENBRACE, $1, $R_Offset, $_Lineno, $L_Offset]; | ||
| 985 | last GETLOOP; | ||
| 986 | }; | ||
| 987 | |||
| 988 | $Line =~ m/\G(\})/gc && do { | ||
| 989 | $$RString .= $1; | ||
| 990 | $token = [CLOSEBRACE, $1, $R_Offset, $_Lineno, $L_Offset]; | ||
| 991 | last GETLOOP; | ||
| 992 | }; | ||
| 993 | |||
| 994 | $Line =~ m/\G(!)/gc && do { | ||
| 995 | $$RString .= $1; | ||
| 996 | $token = [EXCLAMATION, $1, $R_Offset, $_Lineno, $L_Offset]; | ||
| 997 | last GETLOOP; | ||
| 998 | }; | ||
| 999 | |||
| 1000 | $Line =~ m/\G(\*)/gc && do { | ||
| 1001 | $$RString .= $1; | ||
| 1002 | $token = [ASTERISK, $1, $R_Offset, $_Lineno, $L_Offset]; | ||
| 1003 | last GETLOOP; | ||
| 1004 | }; | ||
| 1005 | |||
| 1006 | # this regex is not perfect, but passes for now. change to | ||
| 1007 | # something that matches perfectly, else use code to validate | ||
| 1008 | # and reset the pos to before on failure. | ||
| 1009 | $Line =~ m/\G($rIPv4_PREFIX)/ogc && do { | ||
| 1010 | $$RString .= $1; | ||
| 1011 | $token = [IP_PREFIX, $1, $R_Offset, $_Lineno, $L_Offset]; | ||
| 1012 | last GETLOOP; | ||
| 1013 | }; | ||
| 1014 | |||
| 1015 | # this regex is not perfect, but passes for now. change to | ||
| 1016 | # something that matches perfectly, else use code to validate | ||
| 1017 | # and reset the pos to before on failure. | ||
| 1018 | $Line =~ m/\G($rIPv4)/ogc && do { | ||
| 1019 | $$RString .= $1; | ||
| 1020 | $token = [IPv4, $1, $R_Offset, $_Lineno, $L_Offset]; | ||
| 1021 | last GETLOOP; | ||
| 1022 | }; | ||
| 1023 | |||
| 1024 | # make sure the digits are followed by a non word char. | ||
| 1025 | # if it is followed by a word char, should be a WORD. | ||
| 1026 | # we can't put the WORD regex before NUMBER as that would | ||
| 1027 | # match always against a NUMBER. | ||
| 1028 | $Line =~ m/\G([0-9]+)(?=\W)/gc && do { | ||
| 1029 | $$RString .= $1; | ||
| 1030 | $token = [NUMBER, $1, $R_Offset, $_Lineno, $L_Offset]; | ||
| 1031 | last GETLOOP; | ||
| 1032 | }; | ||
| 1033 | |||
| 1034 | $Line =~ m/\G([a-zA-Z0-9._-]+)/gc && do { | ||
| 1035 | $$RString .= $1; | ||
| 1036 | # add code to check and return a token type | ||
| 1037 | # of size spec if the parser expects one. | ||
| 1038 | $token = [WORD, $1, $R_Offset, $_Lineno, $L_Offset]; | ||
| 1039 | last GETLOOP; | ||
| 1040 | }; | ||
| 1041 | |||
| 1042 | $Line =~ m/\G(\S+)/gc && do { | ||
| 1043 | $$RString .= $1; | ||
| 1044 | $token = [UNRECOGNISED, $1, $R_Offset, $_Lineno, $L_Offset]; | ||
| 1045 | last GETLOOP; | ||
| 1046 | }; | ||
| 1047 | } | ||
| 1048 | |||
| 1049 | # store the posinfo for this token, which | ||
| 1050 | # will be returned with the next token as the | ||
| 1051 | # position to backup to, in case caller wants | ||
| 1052 | # to unget. Do so only if lexer is not in the | ||
| 1053 | # middle of a recursive call to parse comments. | ||
| 1054 | # also after the last token $RString is undef. so | ||
| 1055 | # check for that condition too. | ||
| 1056 | # if ($LexerState == L_NORMAL && defined ($RString)) { | ||
| 1057 | if (defined ($RString)) { | ||
| 1058 | $L_Offset = pos ($Line); | ||
| 1059 | $R_Offset = length ($$RString); | ||
| 1060 | $_Lineno = $Lineno; | ||
| 1061 | } | ||
| 1062 | return ($token); | ||
| 1063 | } | ||
| 1064 | |||
| 1065 | sub __readline () | ||
| 1066 | { | ||
| 1067 | unless (defined ($Line = $FH->getline ())) { | ||
| 1068 | __die ("__readline", "Unexpected EOF when token expected, aborting") | ||
| 1069 | if ($ParserState == P_INDIRECTIVE); | ||
| 1070 | return; | ||
| 1071 | } | ||
| 1072 | $Lineno = $FH->lineno (); | ||
| 1073 | return (1); | ||
| 1074 | } | ||
| 1075 | |||
| 1076 | # provides one level of unget. | ||
| 1077 | # get the position info from token and backup to | ||
| 1078 | # that point in the input stream, where the last | ||
| 1079 | # successfully lex'ed and accepted token ends. | ||
| 1080 | sub __ungettoken ($) | ||
| 1081 | { | ||
| 1082 | my $token = $_[0]; | ||
| 1083 | $$RString = substr ($$RString, 0, $token->[T_ROFFSET]); | ||
| 1084 | $FH->lineno ($token->[T_LINENO] - 1); | ||
| 1085 | __readline (); | ||
| 1086 | pos ($Line) = $token->[T_LOFFSET]; | ||
| 1087 | } | ||
| 1088 | |||
| 1089 | 1; |