#include <objc/objc.h>
#include <netinfo/ni.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <NetInfo/nilib2.h>
char myname[128];
void _EXIT_(int err)
{
if (err == 0)
exit(0);
else if (err < NI_FAILED)
exit(err + 100);
else if (err == NI_FAILED)
exit(199);
else if (err == NI_FAILED+1)
exit(200);
else
exit(err + 200);
}
void errexit(char *s, ni_status status)
{
fprintf(stderr, "%s: %s\n", s, ni_error(status));
_EXIT_(status);
}
int ni_report_dir(dom, dir, keys, n)
void *dom; ni_id *dir; char *keys[]; int n; {
int i, j, k, looking;
ni_status rc;
ni_namelist n1, n2;
NI_INIT(&n1);
rc = ni_listprops(dom, dir, &n1);
if (rc) errexit("ni_listprops", rc);
for (k = 0; k < n; k++) {
looking = 1;
for (i = 0; i < n1.ni_namelist_len && looking; i++) {
if (!strcmp(keys[k], n1.ni_namelist_val[i])) {
looking = 0;
NI_INIT(&n2);
rc = ni_lookupprop(dom, dir, n1.ni_namelist_val[i], &n2);
if (rc) errexit("ni_listprops", rc);
if (n2.ni_namelist_len == 0)
printf("#NoValue#");
for (j = 0; j < n2.ni_namelist_len; j++) {
if (j) printf(",");
printf("%s", n2.ni_namelist_val[j]);
}
printf("\t");
ni_namelist_free(&n2);
}
}
if (looking) printf("#NoValue#\t");
}
printf("\n");
ni_namelist_free(&n1);
return(0);
}
int ni_report(dom, dir, keys, n)
void *dom; ni_id *dir; char *keys[]; int n; {
int i, rc;
ni_idlist children;
ni_id child;
NI_INIT(&children);
rc = ni_children(dom, dir, &children);
if (rc) errexit("ni_children", rc);
for (i = 0; i < children.ni_idlist_len; i++) {
child.nii_object = children.ni_idlist_val[i];
ni_report_dir(dom, &child, keys, n);
}
ni_idlist_free(&children);
return(0);
}
void usage()
{
fprintf(stderr, "usage: %s [-T timeout] [-t] domain directory property...\n",
myname);
exit(1);
}
int main(int argc, char *argv[])
{
void *dom;
ni_id dir;
int i, dp, firstprop, timeout;
ni_status rc;
BOOL bytag;
char *slash;
slash = rindex(argv[0], '/');
if (slash == NULL) strcpy(myname, argv[0]);
else strcpy(myname, slash+1);
if (argc < 3) usage();
bytag = NO;
timeout = 30;
dp = 0;
firstprop = 3;
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-t")) {
if (bytag) usage();
bytag = YES;
dp = i + 1;
firstprop += 1;
}
if (!strcmp(argv[i], "-T")) {
timeout = atoi(argv[++i]);
firstprop += 2;
}
}
if (firstprop >= argc) {
fprintf(stderr, "no properties\n");
usage();
}
if (dp == 0) dp = firstprop - 2;
if ((rc = do_open(myname, argv[dp], &dom, bytag, timeout, NULL, NULL))) {
_EXIT_(rc);
}
rc = ni_pathsearch(dom, &dir, argv[firstprop-1]);
if (rc) errexit("ni_pathsearch", rc);
ni_report(dom, &dir, argv+firstprop, argc-firstprop);
ni_free(dom);
exit(0);
}