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.9KB

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