#include <sys/param.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <err.h>
#include <sysexits.h>
#include <netsmb/smb_lib.h>
#include <netsmb/smb_conn.h>
#include "common.h"
extern char *__progname;
static void help(void);
static void help_usage(void);
static int cmd_help(int argc, char *argv[]);
int verbose;
typedef int cmd_fn_t (int argc, char *argv[]);
typedef void cmd_usage_t (void);
#define CMDFL_NO_KMOD 0x0001
static struct commands {
const char * name;
cmd_fn_t* fn;
cmd_usage_t * usage;
int flags;
} commands[] = {
{"help", cmd_help, help_usage, CMDFL_NO_KMOD},
{"lookup", cmd_lookup, lookup_usage, CMDFL_NO_KMOD},
{"status", cmd_status, status_usage, 0},
{"view", cmd_view, view_usage, 0},
{NULL, NULL, NULL, 0}
};
static struct commands *
lookupcmd(const char *name)
{
struct commands *cmd;
for (cmd = commands; cmd->name; cmd++) {
if (strcmp(cmd->name, name) == 0)
return cmd;
}
return NULL;
}
int
cmd_help(int argc, char *argv[])
{
struct commands *cmd;
char *cp;
if (argc < 2)
help_usage();
cp = argv[1];
cmd = lookupcmd(cp);
if (cmd == NULL)
errx(EX_DATAERR, "unknown command %s", cp);
if (cmd->usage == NULL)
errx(EX_DATAERR, "no specific help for command %s", cp);
cmd->usage();
exit(0);
}
int
main(int argc, char *argv[])
{
struct commands *cmd;
char *cp;
int opt;
if (argc < 2)
help();
while ((opt = getopt(argc, argv, "hv")) != EOF) {
switch (opt) {
case 'h':
help();
case 'v':
verbose = 1;
break;
default:
warnx("invalid option %c", opt);
help();
}
}
if (optind >= argc)
help();
cp = argv[optind];
cmd = lookupcmd(cp);
if (cmd == NULL)
errx(EX_DATAERR, "unknown command %s", cp);
if (cmd->flags & CMDFL_NO_KMOD)
setlocale(LC_CTYPE, "");
else if ((errno = smb_load_library()) != 0)
errx(EX_UNAVAILABLE, "couldn't load the library");
argc -= optind;
argv += optind;
optind = optreset = 1;
return cmd->fn(argc, argv);
}
static void
help(void) {
fprintf(stderr, "\n");
fprintf(stderr, "usage: %s [-hv] subcommand [args]\n", __progname);
fprintf(stderr, "where subcommands are:\n"
" help display help on specified subcommand\n"
" lookup resolve NetBIOS name to IP address\n"
" status resolve IP address or DNS name to NetBIOS names\n"
" view list resources on specified host\n"
"\n");
exit(1);
}
static void
help_usage(void) {
fprintf(stderr, "usage: smbutil help command\n");
exit(1);
}