Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.1 KiB

  1. #-----------------------------------------------------------------------------
  2. # parseinf.pl - returns the contents of a given section of an inf file.
  3. #-----------------------------------------------------------------------------
  4. # Parse the command line
  5. $filename = shift;
  6. $section_name = shift;
  7. if(!$filename) {
  8. print STDERR "$0: Error: you must specify a filename\n";
  9. &PrintHelp();
  10. exit 1;
  11. }
  12. # Open the inf file and read it into memory
  13. if(!-e $filename) {
  14. print STDERR "$0: Error: cant find $filename\n";
  15. exit 2;
  16. }
  17. if(!open(FILE, $filename)) {
  18. print STDERR "$0: Error: cant open $filename\n";
  19. exit 3;
  20. }
  21. @contents = <FILE>;
  22. close(FILE);
  23. # Create the start and end patterns for a section in an inf file
  24. if($section_name) {
  25. $start = '^\[' . $section_name . '\]$';
  26. $end = '^\[';
  27. # Search for $section_name in @contents
  28. # Note - &GetSection is inclusive - it returns the line containing the
  29. # section name and also the line containing the the next section name...
  30. @section = &GetSection($start, $end, \@contents);
  31. # If section is blank, then not even the given section name was found
  32. if(!@section) {
  33. print "$0: Warning: $section_name not found\n";
  34. exit 4;
  35. }
  36. # Get rid of first and last lines (GetSection is inclusive)
  37. shift @section;
  38. pop @section;
  39. }
  40. # If $section_name not specified, print whole file
  41. else {
  42. @section = @contents;
  43. }
  44. # Display the results
  45. print @section;
  46. #-----------------------------------------------------------------------------
  47. # PrintHelp - prints a usage message
  48. #-----------------------------------------------------------------------------
  49. sub PrintHelp {
  50. print STDERR <<EOT;
  51. Usage
  52. perl $0 <filename> [<section_name>]
  53. Returns the contents of a given section of an inf file.
  54. ARGUMENTS
  55. filename: Name of the inf file.
  56. section_name: Name of the section you want. Default: whole file will
  57. be printed.
  58. EXAMPLES
  59. To see the contents of the [Files] section of foo.inf, type:
  60. perl $0 foo.inf Files
  61. To see the entire contents of foo.inf, type:
  62. perl $0 foo.inf
  63. EOT
  64. } # PrintHelp
  65. #-----------------------------------------------------------------------------
  66. # GetSection - returns the set of array elements found between a start
  67. # pattern and an end pattern.
  68. #
  69. # Note - the set returned INCLUDES the lines containing the start
  70. # and end patterns.
  71. #
  72. # Usage:
  73. #
  74. # @section = &GetSection($start, $end, $array_ref);
  75. #
  76. # Where
  77. #
  78. # $start = start pattern
  79. #
  80. # $end = end pattern
  81. #
  82. # $array_ref = reference to an array, e.g. \@array
  83. #
  84. #-----------------------------------------------------------------------------
  85. sub GetSection {
  86. my($start, $end, $contents, @lines_to_keep);
  87. ($start, $end, $contents) = @_;
  88. @lines_to_keep = ();
  89. foreach (@$contents) {
  90. if(/$start/i ... /$end/i) {
  91. push(@lines_to_keep, $_);
  92. }
  93. }
  94. return @lines_to_keep;
  95. }