1189251Ssam/*	$NetBSD: hesinfo.c,v 1.1 1999/01/25 22:45:55 lukem Exp $	*/
2189251Ssam
3189251Ssam/* Copyright 1988, 1996 by the Massachusetts Institute of Technology.
4189251Ssam *
5189251Ssam * Permission to use, copy, modify, and distribute this
6189251Ssam * software and its documentation for any purpose and without
7189251Ssam * fee is hereby granted, provided that the above copyright
8189251Ssam * notice appear in all copies and that both that copyright
9189251Ssam * notice and this permission notice appear in supporting
10189251Ssam * documentation, and that the name of M.I.T. not be used in
11189251Ssam * advertising or publicity pertaining to distribution of the
12189251Ssam * software without specific, written prior permission.
13189251Ssam * M.I.T. makes no representations about the suitability of
14189251Ssam * this software for any purpose.  It is provided "as is"
15189251Ssam * without express or implied warranty.
16189251Ssam */
17189251Ssam
18214734Srpaulo/* This file is a simple driver for the Hesiod library. */
19189251Ssam
20189251Ssam
21189251Ssam#include <sys/cdefs.h>
22189251Ssam#include <err.h>
23189251Ssam#include <errno.h>
24189251Ssam#include <hesiod.h>
25189251Ssam#include <stdio.h>
26189251Ssam#include <stdlib.h>
27189251Ssam#include <string.h>
28189251Ssam#include <unistd.h>
29189251Ssam
30189251Ssamint
31189251Ssammain(int argc, char **argv)
32189251Ssam{
33189251Ssam	char  **list, **p, *bindname, *name, *type;
34189251Ssam	int     lflag = 0, errflg = 0, bflag = 0, c;
35189251Ssam	void   *context;
36189251Ssam
37189251Ssam	while ((c = getopt(argc, argv, "lb")) != -1) {
38189251Ssam		switch (c) {
39189251Ssam		case 'l':
40189251Ssam			lflag = 1;
41189251Ssam			break;
42189251Ssam		case 'b':
43189251Ssam			bflag = 1;
44189251Ssam			break;
45189251Ssam		default:
46189251Ssam			errflg++;
47189251Ssam			break;
48189251Ssam		}
49189251Ssam	}
50189251Ssam	if (argc - optind != 2 || errflg) {
51189251Ssam		fprintf(stderr, "usage: hesinfo [-bl] name type\n");
52189251Ssam		fprintf(stderr, "\t-l selects long format\n");
53189251Ssam		fprintf(stderr, "\t-b also does hes_to_bind conversion\n");
54189251Ssam		exit(2);
55189251Ssam	}
56189251Ssam	name = argv[optind];
57189251Ssam	type = argv[optind + 1];
58189251Ssam
59189251Ssam	if (hesiod_init(&context) < 0) {
60189251Ssam		if (errno == ENOEXEC)
61189251Ssam			warnx(
62189251Ssam			    "hesiod_init: Invalid Hesiod configuration file.");
63189251Ssam		else
64189251Ssam			warn("hesiod_init");
65189251Ssam	}
66189251Ssam	/* Display bind name if requested. */
67189251Ssam	if (bflag) {
68189251Ssam		if (lflag)
69189251Ssam			printf("hes_to_bind(%s, %s) expands to\n", name, type);
70189251Ssam		bindname = hesiod_to_bind(context, name, type);
71189251Ssam		if (!bindname) {
72189251Ssam			if (lflag)
73189251Ssam				printf("nothing\n");
74189251Ssam			if (errno == ENOENT)
75				warnx("hesiod_to_bind: Unknown rhs-extension.");
76			else
77				warn("hesiod_to_bind");
78			exit(1);
79		}
80		printf("%s\n", bindname);
81		free(bindname);
82		if (lflag)
83			printf("which ");
84	}
85	if (lflag)
86		printf("resolves to\n");
87
88	/* Do the hesiod resolve and check for errors. */
89	list = hesiod_resolve(context, name, type);
90	if (!list) {
91		if (lflag)
92			printf("nothing\n");
93		if (errno == ENOENT)
94			warnx("hesiod_resolve: Hesiod name not found.");
95		else
96			warn("hesiod_resolve");
97		exit(1);
98	}
99	/* Display the results. */
100	for (p = list; *p; p++)
101		printf("%s\n", *p);
102
103	hesiod_free_list(context, list);
104	hesiod_end(context);
105	exit(0);
106}
107