sysinit.c revision 199767f8919635c4928607450d9e0abb932109ce
/* $FreeBSD$ */
/*-
* Copyright (c) 2013 Hans Petter Selasky. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* This utility sorts sysinit structure entries in binary format and
* prints out the result in C-format.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <err.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sysexits.h>
#include "sysinit.h"
static int opt_R;
static const char *input_f;
static const char *output_f;
static const char *struct_name;
static const char *keyword;
static struct sysinit_data **start;
static struct sysinit_data **stop;
static int input_file = -1;
static int output_file = -1;
static char scratch_buf[4096];
static int success;
static void do_sysinit(void);
/* the following function converts the numbers into host endian format */
static uint32_t
{
temp = 0;
while (val) {
endian >>= 4;
val >>= 4;
}
return (temp);
}
static void
{
}
static void *
{
void *ptr;
return (ptr);
}
static void
usage(void)
{
"\t" "-k sysinit -s sysinit_data [ -R (reverse)]");
}
static void
cleanup(void)
{
if (output_file >= 0)
if (input_file >= 0)
if (success == 0) {
if (output_f)
}
}
static int
{
return (1);
return (-1);
return (1);
return (-1);
return (0); /* equal */
}
static int
{
return (-1);
return (1);
return (-1);
return (1);
return (0); /* equal */
}
int
{
struct sysinit_data **sipp;
int c;
int entries;
switch (c) {
case 'i':
break;
case 'o':
break;
case 'R':
opt_R = 1;
break;
case 'k':
break;
case 's':
break;
default:
usage();
}
}
usage();
cleanup();
if (input_file < 0)
if (output_file < 0)
(unsigned int)sizeof(struct sysinit_data));
}
if (off < 0)
for (c = 0; c != entries; c++)
/* switch all fields to host endian order */
}
if (opt_R == 0) {
/* sort entries, rising numerical order */
} else {
/* sort entries, falling numerical order */
}
/* safe all strings */
}
do_sysinit();
do_sysinit();
else
success = 1;
return (0);
}
static void
do_sysinit(void)
{
struct sysinit_data **sipp;
int c;
"/*\n"
" * This file was automatically generated.\n"
" * Please do not edit.\n"
" */\n\n");
/* write out externals */
continue;
if ((*sipp)->dw_msb_value == 0)
continue;
"/* #%04u: %s entry at %s:%u */\n",
(unsigned int)(*sipp)->dw_file_line);
(*sipp)->b_global_name);
}
"const void *%s[] = {\n", struct_name);
/* write out actual table */
continue;
if ((*sipp)->dw_msb_value == 0)
continue;
"\t&%s, /* #%04u */\n",
(*sipp)->b_global_name, (unsigned int)c);
}
"\t(const void *)0\n"
"};\n");
}