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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stddef.h>
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <sys/mman.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <getopt.h>
  30. #define eprintf(...) fprintf ( stderr, __VA_ARGS__ )
  31. /** Command-line options */
  32. struct options {
  33. };
  34. /** Error usage information */
  35. struct einfo {
  36. uint32_t size;
  37. uint32_t error;
  38. uint32_t desc;
  39. uint32_t file;
  40. uint32_t line;
  41. } __attribute__ (( packed ));
  42. /**
  43. * Process einfo file
  44. *
  45. * @v infile Filename
  46. * @v opts Command-line options
  47. */
  48. static void einfo ( const char *infile,
  49. struct options *opts __attribute__ (( unused )) ) {
  50. int fd;
  51. struct stat stat;
  52. size_t len;
  53. void *start;
  54. struct einfo *einfo;
  55. /* Open einfo file */
  56. if ( ( fd = open ( infile, O_RDONLY ) ) < 0 ) {
  57. eprintf ( "Cannot open \"%s\": %s\n",
  58. infile, strerror ( errno ) );
  59. exit ( 1 );
  60. }
  61. /* Get file size */
  62. if ( fstat ( fd, &stat ) < 0 ) {
  63. eprintf ( "Cannot stat \"%s\": %s\n",
  64. infile, strerror ( errno ) );
  65. exit ( 1 );
  66. }
  67. len = stat.st_size;
  68. if ( len ) {
  69. /* Map file */
  70. if ( ( start = mmap ( NULL, len, PROT_READ, MAP_SHARED,
  71. fd, 0 ) ) == MAP_FAILED ) {
  72. eprintf ( "Cannot mmap \"%s\": %s\n",
  73. infile, strerror ( errno ) );
  74. exit ( 1 );
  75. }
  76. /* Iterate over einfo records */
  77. for ( einfo = start ; ( ( void * ) einfo ) < ( start + len ) ;
  78. einfo = ( ( ( void * ) einfo ) + einfo->size ) ) {
  79. printf ( "%08x\t%s\t%d\t%s\n", einfo->error,
  80. ( ( ( char * ) einfo ) + einfo->file ),
  81. einfo->line,
  82. ( ( ( char * ) einfo ) + einfo->desc ) );
  83. }
  84. }
  85. /* Unmap and close file */
  86. munmap ( start, len );
  87. close ( fd );
  88. }
  89. /**
  90. * Print help
  91. *
  92. * @v program_name Program name
  93. */
  94. static void print_help ( const char *program_name ) {
  95. eprintf ( "Syntax: %s file1.einfo [file2.einfo...]\n",
  96. program_name );
  97. }
  98. /**
  99. * Parse command-line options
  100. *
  101. * @v argc Argument count
  102. * @v argv Argument list
  103. * @v opts Options structure to populate
  104. */
  105. static int parse_options ( const int argc, char **argv,
  106. struct options *opts __attribute__ (( unused )) ) {
  107. int c;
  108. while (1) {
  109. int option_index = 0;
  110. static struct option long_options[] = {
  111. { "help", 0, NULL, 'h' },
  112. { 0, 0, 0, 0 }
  113. };
  114. if ( ( c = getopt_long ( argc, argv, "s:h",
  115. long_options,
  116. &option_index ) ) == -1 ) {
  117. break;
  118. }
  119. switch ( c ) {
  120. case 'h':
  121. print_help ( argv[0] );
  122. exit ( 0 );
  123. case '?':
  124. default:
  125. exit ( 2 );
  126. }
  127. }
  128. return optind;
  129. }
  130. int main ( int argc, char **argv ) {
  131. struct options opts = {
  132. };
  133. int infile_index;
  134. const char *infile;
  135. /* Parse command-line arguments */
  136. infile_index = parse_options ( argc, argv, &opts );
  137. if ( argc <= infile_index ) {
  138. print_help ( argv[0] );
  139. exit ( 2 );
  140. }
  141. /* Process each einfo file */
  142. for ( ; infile_index < argc ; infile_index++ ) {
  143. infile = argv[infile_index];
  144. einfo ( infile, &opts );
  145. }
  146. return 0;
  147. }