165532Snectar/*	$NetBSD: hesinfo.c,v 1.1 1999/01/25 22:45:55 lukem Exp $	*/
265532Snectar
365532Snectar/* Copyright 1988, 1996 by the Massachusetts Institute of Technology.
465532Snectar *
565532Snectar * Permission to use, copy, modify, and distribute this
665532Snectar * software and its documentation for any purpose and without
765532Snectar * fee is hereby granted, provided that the above copyright
865532Snectar * notice appear in all copies and that both that copyright
965532Snectar * notice and this permission notice appear in supporting
1065532Snectar * documentation, and that the name of M.I.T. not be used in
1165532Snectar * advertising or publicity pertaining to distribution of the
1265532Snectar * software without specific, written prior permission.
1365532Snectar * M.I.T. makes no representations about the suitability of
1465532Snectar * this software for any purpose.  It is provided "as is"
1565532Snectar * without express or implied warranty.
1665532Snectar */
1765532Snectar
1865532Snectar/* This file is a simple driver for the Hesiod library. */
1965532Snectar
2065532Snectar
2165532Snectar#include <sys/cdefs.h>
2299112Sobrien__FBSDID("$FreeBSD$");
2365532Snectar
2465532Snectar#include <err.h>
2565532Snectar#include <errno.h>
2665532Snectar#include <hesiod.h>
2765532Snectar#include <stdio.h>
2865532Snectar#include <stdlib.h>
2965532Snectar#include <string.h>
3065532Snectar#include <unistd.h>
3165532Snectar
3265532Snectarint
33102944Sdwmalonemain(int argc, char **argv)
3465532Snectar{
3565532Snectar	char  **list, **p, *bindname, *name, *type;
3665532Snectar	int     lflag = 0, errflg = 0, bflag = 0, c;
3765532Snectar	void   *context;
3865532Snectar
3965532Snectar	while ((c = getopt(argc, argv, "lb")) != -1) {
4065532Snectar		switch (c) {
4165532Snectar		case 'l':
4265532Snectar			lflag = 1;
4365532Snectar			break;
4465532Snectar		case 'b':
4565532Snectar			bflag = 1;
4665532Snectar			break;
4765532Snectar		default:
4865532Snectar			errflg++;
4965532Snectar			break;
5065532Snectar		}
5165532Snectar	}
5265532Snectar	if (argc - optind != 2 || errflg) {
5395258Sdes		fprintf(stderr, "usage: hesinfo [-bl] name type\n");
5465532Snectar		fprintf(stderr, "\t-l selects long format\n");
5565532Snectar		fprintf(stderr, "\t-b also does hes_to_bind conversion\n");
5665532Snectar		exit(2);
5765532Snectar	}
5865532Snectar	name = argv[optind];
5965532Snectar	type = argv[optind + 1];
6065532Snectar
6165532Snectar	if (hesiod_init(&context) < 0) {
6265532Snectar		if (errno == ENOEXEC)
6365532Snectar			warnx(
6465532Snectar			    "hesiod_init: Invalid Hesiod configuration file.");
6565532Snectar		else
6665532Snectar			warn("hesiod_init");
6765532Snectar	}
6865532Snectar	/* Display bind name if requested. */
6965532Snectar	if (bflag) {
7065532Snectar		if (lflag)
7165532Snectar			printf("hes_to_bind(%s, %s) expands to\n", name, type);
7265532Snectar		bindname = hesiod_to_bind(context, name, type);
7365532Snectar		if (!bindname) {
7465532Snectar			if (lflag)
7565532Snectar				printf("nothing\n");
7665532Snectar			if (errno == ENOENT)
7765532Snectar				warnx("hesiod_to_bind: Unknown rhs-extension.");
7865532Snectar			else
7965532Snectar				warn("hesiod_to_bind");
8065532Snectar			exit(1);
8165532Snectar		}
8265532Snectar		printf("%s\n", bindname);
8365532Snectar		free(bindname);
8465532Snectar		if (lflag)
8565532Snectar			printf("which ");
8665532Snectar	}
8765532Snectar	if (lflag)
8865532Snectar		printf("resolves to\n");
8965532Snectar
9065532Snectar	/* Do the hesiod resolve and check for errors. */
9165532Snectar	list = hesiod_resolve(context, name, type);
9265532Snectar	if (!list) {
9365532Snectar		if (lflag)
9465532Snectar			printf("nothing\n");
9565532Snectar		if (errno == ENOENT)
9665532Snectar			warnx("hesiod_resolve: Hesiod name not found.");
9765532Snectar		else
9865532Snectar			warn("hesiod_resolve");
9965532Snectar		exit(1);
10065532Snectar	}
10165532Snectar	/* Display the results. */
10265532Snectar	for (p = list; *p; p++)
10365532Snectar		printf("%s\n", *p);
10465532Snectar
10565532Snectar	hesiod_free_list(context, list);
10665532Snectar	hesiod_end(context);
10765532Snectar	exit(0);
10865532Snectar}
109