1*f41ccc36Sespie# $OpenBSD: LaLoFile.pm,v 1.5 2023/07/06 08:29:26 espie Exp $ 2dd9b5fdeSespie 3dd9b5fdeSespie# Copyright (c) 2007-2010 Steven Mestdagh <steven@openbsd.org> 4b8664c47Sespie# Copyright (c) 2012 Marc Espie <espie@openbsd.org> 5dd9b5fdeSespie# 6dd9b5fdeSespie# Permission to use, copy, modify, and distribute this software for any 7dd9b5fdeSespie# purpose with or without fee is hereby granted, provided that the above 8dd9b5fdeSespie# copyright notice and this permission notice appear in all copies. 9dd9b5fdeSespie# 10dd9b5fdeSespie# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11dd9b5fdeSespie# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12dd9b5fdeSespie# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13dd9b5fdeSespie# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14dd9b5fdeSespie# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15dd9b5fdeSespie# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16dd9b5fdeSespie# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17dd9b5fdeSespie 18*f41ccc36Sespieuse v5.36; 19dd9b5fdeSespie 20dd9b5fdeSespiepackage LT::LaLoFile; 21f98ddbc5Sespieuse LT::Trace; 22f98ddbc5Sespie 23dd9b5fdeSespiemy %file_cache; # which files have been parsed 24dd9b5fdeSespiemy $cache_by_fullname = {}; 25dd9b5fdeSespiemy $cache_by_inode = {}; 26dd9b5fdeSespie 27dd9b5fdeSespie# allows special treatment for some keywords 28*f41ccc36Sespiesub set($self, $k, $v) 29dd9b5fdeSespie{ 30dd9b5fdeSespie $self->{$k} = $v; 31dd9b5fdeSespie} 32dd9b5fdeSespie 33*f41ccc36Sespiesub stringize($self, $k) 34dd9b5fdeSespie{ 35dd9b5fdeSespie if (defined $self->{$k}) { 36dd9b5fdeSespie return $self->{$k}; 37dd9b5fdeSespie } 38dd9b5fdeSespie return ''; 39dd9b5fdeSespie} 40dd9b5fdeSespie 41*f41ccc36Sespiesub read($class, $filename) 42dd9b5fdeSespie{ 43dd9b5fdeSespie my $info = $class->new; 44dd9b5fdeSespie open(my $fh, '<', $filename) or die "Cannot read $filename: $!\n"; 4528252c27Safresh1 while (my $line = <$fh>) { 4628252c27Safresh1 chomp($line); 4728252c27Safresh1 next if $line =~ /^\#/; 4828252c27Safresh1 next if $line =~ /^\s*$/; 4928252c27Safresh1 if ($line =~ m/^(\S+)\=\'(.*)\'$/) { 50dd9b5fdeSespie $info->set($1, $2); 5128252c27Safresh1 } elsif ($line =~ m/^(\S+)\=(\S+)$/) { 52dd9b5fdeSespie $info->set($1, $2); 53dd9b5fdeSespie } 54dd9b5fdeSespie } 55dd9b5fdeSespie return $info; 56dd9b5fdeSespie} 57dd9b5fdeSespie 58*f41ccc36Sespiesub parse($class, $filename) 59dd9b5fdeSespie{ 60f98ddbc5Sespie tprint {"parsing $filename"}; 61dd9b5fdeSespie 62dd9b5fdeSespie if (defined $cache_by_fullname->{$filename}) { 63f98ddbc5Sespie tsay {" (cached)"}; 64dd9b5fdeSespie return $cache_by_fullname->{$filename}; 65dd9b5fdeSespie } 66dd9b5fdeSespie my $key = join("/", (stat $filename)[0,1]); 67dd9b5fdeSespie if (defined $cache_by_inode->{$key}) { 68f98ddbc5Sespie tsay {" (cached)"}; 69dd9b5fdeSespie return $cache_by_inode->{$key}; 70dd9b5fdeSespie } 71f98ddbc5Sespie tsay {""}; 72dd9b5fdeSespie return $cache_by_inode->{$key} = $cache_by_fullname->{$filename} = 73dd9b5fdeSespie $class->read($filename); 74dd9b5fdeSespie} 75dd9b5fdeSespie 76*f41ccc36Sespiesub new($class) 77dd9b5fdeSespie{ 78dd9b5fdeSespie bless {}, $class; 79dd9b5fdeSespie} 80dd9b5fdeSespie 81dd9b5fdeSespie1; 82