13034Sdougm /*
23034Sdougm * CDDL HEADER START
33034Sdougm *
43034Sdougm * The contents of this file are subject to the terms of the
53034Sdougm * Common Development and Distribution License (the "License").
63034Sdougm * You may not use this file except in compliance with the License.
73034Sdougm *
83034Sdougm * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93034Sdougm * or http://www.opensolaris.org/os/licensing.
103034Sdougm * See the License for the specific language governing permissions
113034Sdougm * and limitations under the License.
123034Sdougm *
133034Sdougm * When distributing Covered Code, include this CDDL HEADER in each
143034Sdougm * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153034Sdougm * If applicable, add the following below this CDDL HEADER, with the
163034Sdougm * fields enclosed by brackets "[]" replaced with your own identifying
173034Sdougm * information: Portions Copyright [yyyy] [name of copyright owner]
183034Sdougm *
193034Sdougm * CDDL HEADER END
203034Sdougm */
213034Sdougm
223034Sdougm /*
233910Sdougm * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
243034Sdougm * Use is subject to license terms.
253034Sdougm */
263034Sdougm
273034Sdougm #pragma ident "%Z%%M% %I% %E% SMI"
283034Sdougm
293034Sdougm #include <stdlib.h>
303034Sdougm #include <stdio.h>
313034Sdougm #include <string.h>
323034Sdougm #include <ctype.h>
333034Sdougm #include <unistd.h>
343034Sdougm #include <getopt.h>
353034Sdougm #include <libgen.h>
363034Sdougm
373034Sdougm #include <libshare.h>
383034Sdougm #include "sharemgr.h"
393034Sdougm #include <libintl.h>
403034Sdougm #include <locale.h>
413034Sdougm
423034Sdougm char *protocol = NULL;
433034Sdougm static int help = 0;
443034Sdougm
453910Sdougm static int run_command(char *, int, char **, char *, sa_handle_t);
463034Sdougm extern sa_command_t *sa_lookup(char *, char *);
473034Sdougm extern void sub_command_help(char *proto);
483034Sdougm
493034Sdougm static void
global_help()503034Sdougm global_help()
513034Sdougm {
523034Sdougm (void) printf(gettext("usage: sharemgr [-h | <command> [options]]\n"));
533034Sdougm sub_command_help(NULL);
543034Sdougm }
553034Sdougm
563034Sdougm int
main(int argc,char * argv[])573034Sdougm main(int argc, char *argv[])
583034Sdougm {
593034Sdougm int c;
603034Sdougm int rval;
613034Sdougm char *command = NULL;
623910Sdougm sa_handle_t handle;
633034Sdougm
643034Sdougm /*
653034Sdougm * make sure locale and gettext domain is setup
663034Sdougm */
673034Sdougm (void) setlocale(LC_ALL, "");
683034Sdougm (void) textdomain(TEXT_DOMAIN);
693034Sdougm
703034Sdougm /*
713034Sdougm * parse enough of command line to get protocol, if any.
723034Sdougm * Note that options need to come "after" the subcommand.
733034Sdougm */
743034Sdougm command = basename(argv[0]);
753034Sdougm if (strcmp(command, "share") != 0 && strcmp(command, "unshare") != 0) {
76*4653Sdougm while ((c = getopt(argc, argv, "h?")) != EOF) {
77*4653Sdougm switch (c) {
78*4653Sdougm default:
79*4653Sdougm case 'h':
80*4653Sdougm case '?':
81*4653Sdougm help = 1;
82*4653Sdougm break;
83*4653Sdougm }
843034Sdougm }
85*4653Sdougm if (argc == 1)
86*4653Sdougm help = 1;
873034Sdougm }
883034Sdougm
893034Sdougm if (strcmp(command, "sharemgr") == 0) {
90*4653Sdougm command = argv[optind];
91*4653Sdougm argv++;
92*4653Sdougm argc--;
933034Sdougm }
943034Sdougm
953034Sdougm if (help) {
963034Sdougm /* no subcommand */
973034Sdougm global_help();
983034Sdougm exit(SA_OK);
993034Sdougm }
1003034Sdougm
1013034Sdougm /*
1023034Sdougm * now have enough to parse rest of command line
1033082Sdougm *
1043082Sdougm * First, initialize the plugin architecture.
1053082Sdougm * Plugins are needed in the event of a global help
1063082Sdougm * request.
1073082Sdougm *
1083082Sdougm * reset optind to 1 so the parsing that takes place in
1093082Sdougm * sa_init() will work correctly.
1103034Sdougm */
1113082Sdougm
1123082Sdougm optind = 1;
1133910Sdougm handle = sa_init(SA_INIT_SHARE_API);
1143082Sdougm
1153082Sdougm /*
1163082Sdougm * reset optind again since we will start parsing all over in
1173082Sdougm * the sub-commands.
1183082Sdougm */
1193082Sdougm optind = 1;
1203910Sdougm rval = run_command(command, argc, argv, protocol, handle);
1213034Sdougm
1223910Sdougm sa_fini(handle);
1233034Sdougm return (rval);
1243034Sdougm }
1253034Sdougm
1263034Sdougm static int
run_command(char * command,int argc,char * argv[],char * proto,sa_handle_t handle)1273910Sdougm run_command(char *command, int argc, char *argv[], char *proto,
1283910Sdougm sa_handle_t handle)
1293034Sdougm {
1303034Sdougm sa_command_t *cmdvec;
1313034Sdougm int ret;
1323034Sdougm
1333034Sdougm /*
1343034Sdougm * To get here, we know there should be a command due to the
1353034Sdougm * preprocessing done earlier. Need to find the protocol
1363034Sdougm * that is being affected. If no protocol, then it is ALL
1373034Sdougm * protocols.
1383034Sdougm *
1393034Sdougm * We don't currently use the protocol here at this point. It
1403034Sdougm * is left in as a placeholder for the future addition of
1413034Sdougm * protocol specific sub-commands.
1423034Sdougm *
1433034Sdougm * Known sub-commands are handled at this level. An unknown
1443034Sdougm * command will be passed down to the shared object that
1453034Sdougm * actually implements it. We can do this since the semantics
1463034Sdougm * of the common sub-commands is well defined.
1473034Sdougm */
1483034Sdougm
1493034Sdougm cmdvec = sa_lookup(command, proto);
1503034Sdougm if (cmdvec == NULL) {
1513034Sdougm (void) printf(gettext("command %s not found\n"), command);
1523034Sdougm exit(1);
1533034Sdougm }
1543034Sdougm /*
1553034Sdougm * need to check priviledges and restrict what can be done
1563034Sdougm * based on least priviledge and sub-command so pass this in
1573034Sdougm * as a flag.
1583034Sdougm */
1593910Sdougm ret = cmdvec->cmdfunc(handle, cmdvec->priv, argc, argv);
1603034Sdougm return (ret);
1613034Sdougm }
162