Annotation of qemu/roms/ipxe/src/util/einfo.c, revision 1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.