Blame show.c

Packit 400c17
/*
Packit 400c17
	Copyright(C) 2017, Red Hat, Inc.
Packit 400c17
Packit 400c17
	This program is free software: you can redistribute it and/or modify
Packit 400c17
	it under the terms of the GNU General Public License as published by
Packit 400c17
	the Free Software Foundation, either version 3 of the License, or
Packit 400c17
	(at your option) any later version.
Packit 400c17
Packit 400c17
	This program is distributed in the hope that it will be useful,
Packit 400c17
	but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 400c17
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 400c17
	GNU General Public License for more details.
Packit 400c17
Packit 400c17
	You should have received a copy of the GNU General Public License
Packit 400c17
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 400c17
*/
Packit 400c17
Packit 400c17
#include <getopt.h>
Packit 400c17
#include <stdlib.h>
Packit 400c17
#include <unistd.h>
Packit 400c17
#include "objects.h"
Packit 400c17
#include "utils.h"
Packit 400c17
Packit 400c17
struct {
Packit 400c17
	bool debug;
Packit 400c17
	bool hide_kabi;
Packit 400c17
	bool hide_kabi_new;
Packit 400c17
	FILE *file;
Packit 400c17
} show_config = {false, false, false, NULL};
Packit 400c17
Packit 400c17
static void show_usage()
Packit 400c17
{
Packit 400c17
	printf("Usage:\n"
Packit 400c17
	       "\tshow [options] kabi_file...\n"
Packit 400c17
	       "\nOptions:\n"
Packit 400c17
	       "    -h, --help:\t\tshow this message\n"
Packit 400c17
	       "    -k, --hide-kabi:\thide changes made by RH_KABI_REPLACE()\n"
Packit 400c17
	       "    -n, --hide-kabi-new:\n\t\t\thide the kabi trickery made by"
Packit 400c17
	       " RH_KABI_REPLACE, but show the new field\n"
Packit 400c17
Packit 400c17
	       "    -d, --debug:\tprint the raw tree\n"
Packit 400c17
	       "    --no-offset:\tdon't display the offset of struct fields\n");
Packit 400c17
	exit(1);
Packit 400c17
}
Packit 400c17
Packit 400c17
/*
Packit 400c17
 * Performs the show command
Packit 400c17
 */
Packit 400c17
int show(int argc, char **argv)
Packit 400c17
{
Packit 400c17
	obj_t *root;
Packit 400c17
	int opt, opt_index, ret = 0;
Packit 400c17
	struct option loptions[] = {
Packit 400c17
		{"debug", no_argument, 0, 'd'},
Packit 400c17
		{"hide-kabi", no_argument, 0, 'k'},
Packit 400c17
		{"hide-kabi-new", no_argument, 0, 'n'},
Packit 400c17
		{"help", no_argument, 0, 'h'},
Packit 400c17
		{"no-offset", no_argument, &display_options.no_offset, 1},
Packit 400c17
		{0, 0, 0, 0}
Packit 400c17
	};
Packit 400c17
Packit 400c17
	memset(&display_options, 0, sizeof(display_options));
Packit 400c17
Packit 400c17
	while ((opt = getopt_long(argc, argv, "dknh",
Packit 400c17
				  loptions, &opt_index)) != -1) {
Packit 400c17
		switch (opt) {
Packit 400c17
		case 0:
Packit 400c17
			break;
Packit 400c17
		case 'd':
Packit 400c17
			show_config.debug = true;
Packit 400c17
			break;
Packit 400c17
		case 'n':
Packit 400c17
			show_config.hide_kabi_new = true;
Packit 400c17
			/* fall through */
Packit 400c17
		case 'k':
Packit 400c17
			show_config.hide_kabi = true;
Packit 400c17
			break;
Packit 400c17
		case 'h':
Packit 400c17
		default:
Packit 400c17
			show_usage();
Packit 400c17
		}
Packit 400c17
	}
Packit 400c17
Packit 400c17
	if (optind >= argc)
Packit 400c17
		show_usage();
Packit 400c17
Packit 400c17
	while (optind < argc) {
Packit 400c17
		char *fn = argv[optind++];
Packit 400c17
Packit 400c17
		show_config.file = safe_fopen(fn);
Packit 400c17
Packit 400c17
		root = obj_parse(show_config.file, fn);
Packit 400c17
Packit 400c17
		if (show_config.hide_kabi)
Packit 400c17
			obj_hide_kabi(root, show_config.hide_kabi_new);
Packit 400c17
Packit 400c17
		if (show_config.debug)
Packit 400c17
			obj_debug_tree(root);
Packit 400c17
Packit 400c17
		obj_print_tree(root);
Packit 400c17
		if (optind < argc)
Packit 400c17
			putchar('\n');
Packit 400c17
Packit 400c17
		obj_free(root);
Packit 400c17
		fclose(show_config.file);
Packit 400c17
	}
Packit 400c17
Packit 400c17
	return ret;
Packit 400c17
}