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.

einfo.c 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 <errno.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <sys/mman.h>
  26. #include <unistd.h>
  27. #include <fcntl.h>
  28. #include <getopt.h>
  29. #define eprintf(...) fprintf ( stderr, __VA_ARGS__ )
  30. /** Command-line options */
  31. struct options {
  32. };
  33. /** Error usage information */
  34. struct einfo {
  35. uint32_t size;
  36. uint32_t error;
  37. uint32_t desc;
  38. uint32_t file;
  39. uint32_t line;
  40. } __attribute__ (( packed ));
  41. /**
  42. * Process einfo file
  43. *
  44. * @v infile Filename
  45. * @v opts Command-line options
  46. */
  47. static void einfo ( const char *infile, struct options *opts ) {
  48. int fd;
  49. struct stat stat;
  50. size_t len;
  51. void *start;
  52. struct einfo *einfo;
  53. /* Open einfo file */
  54. if ( ( fd = open ( infile, O_RDONLY ) ) < 0 ) {
  55. eprintf ( "Cannot open \"%s\": %s\n",
  56. infile, strerror ( errno ) );
  57. exit ( 1 );
  58. }
  59. /* Get file size */
  60. if ( fstat ( fd, &stat ) < 0 ) {
  61. eprintf ( "Cannot stat \"%s\": %s\n",
  62. infile, strerror ( errno ) );
  63. exit ( 1 );
  64. }
  65. len = stat.st_size;
  66. if ( len ) {
  67. /* Map file */
  68. if ( ( start = mmap ( NULL, len, PROT_READ, MAP_SHARED,
  69. fd, 0 ) ) == MAP_FAILED ) {
  70. eprintf ( "Cannot mmap \"%s\": %s\n",
  71. infile, strerror ( errno ) );
  72. exit ( 1 );
  73. }
  74. /* Iterate over einfo records */
  75. for ( einfo = start ; ( ( void * ) einfo ) < ( start + len ) ;
  76. einfo = ( ( ( void * ) einfo ) + einfo->size ) ) {
  77. printf ( "%08x\t%s\t%d\t%s\n", einfo->error,
  78. ( ( ( void * ) einfo ) + einfo->file ),
  79. einfo->line,
  80. ( ( ( void * ) einfo ) + einfo->desc ) );
  81. }
  82. }
  83. /* Unmap and close file */
  84. munmap ( start, len );
  85. close ( fd );
  86. }
  87. /**
  88. * Print help
  89. *
  90. * @v program_name Program name
  91. */
  92. static void print_help ( const char *program_name ) {
  93. eprintf ( "Syntax: %s file1.einfo [file2.einfo...]\n",
  94. program_name );
  95. }
  96. /**
  97. * Parse command-line options
  98. *
  99. * @v argc Argument count
  100. * @v argv Argument list
  101. * @v opts Options structure to populate
  102. */
  103. static int parse_options ( const int argc, char **argv,
  104. struct options *opts ) {
  105. char *end;
  106. int c;
  107. while (1) {
  108. int option_index = 0;
  109. static struct option long_options[] = {
  110. { "help", 0, NULL, 'h' },
  111. { 0, 0, 0, 0 }
  112. };
  113. if ( ( c = getopt_long ( argc, argv, "s:h",
  114. long_options,
  115. &option_index ) ) == -1 ) {
  116. break;
  117. }
  118. switch ( c ) {
  119. case 'h':
  120. print_help ( argv[0] );
  121. exit ( 0 );
  122. case '?':
  123. default:
  124. exit ( 2 );
  125. }
  126. }
  127. return optind;
  128. }
  129. int main ( int argc, char **argv ) {
  130. struct options opts = {
  131. };
  132. unsigned int infile_index;
  133. const char *infile;
  134. /* Parse command-line arguments */
  135. infile_index = parse_options ( argc, argv, &opts );
  136. if ( argc <= infile_index ) {
  137. print_help ( argv[0] );
  138. exit ( 2 );
  139. }
  140. /* Process each einfo file */
  141. for ( ; infile_index < argc ; infile_index++ ) {
  142. infile = argv[infile_index];
  143. einfo ( infile, &opts );
  144. }
  145. return 0;
  146. }