dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / dfs.cmds / sharemgr / sharemgr_main.c
blob8fd346c67859d33509ed8b4deceaaf1e0028cc97
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <unistd.h>
34 #include <getopt.h>
35 #include <libgen.h>
37 #include <libshare.h>
38 #include "sharemgr.h"
39 #include <libintl.h>
40 #include <locale.h>
42 char *protocol = NULL;
43 static int help = 0;
45 static int run_command(char *, int, char **, char *, sa_handle_t);
46 extern sa_command_t *sa_lookup(char *, char *);
47 extern void sub_command_help(char *proto);
49 static void
50 global_help()
52 (void) printf(gettext("usage: sharemgr [-h | <command> [options]]\n"));
53 sub_command_help(NULL);
56 int
57 main(int argc, char *argv[])
59 int c;
60 int rval;
61 char *command = NULL;
62 sa_handle_t handle;
65 * make sure locale and gettext domain is setup
67 (void) setlocale(LC_ALL, "");
68 (void) textdomain(TEXT_DOMAIN);
71 * parse enough of command line to get protocol, if any.
72 * Note that options need to come "after" the subcommand.
74 command = basename(argv[0]);
75 if (strcmp(command, "share") != 0 && strcmp(command, "unshare") != 0) {
76 while ((c = getopt(argc, argv, "h?")) != EOF) {
77 switch (c) {
78 default:
79 case 'h':
80 case '?':
81 help = 1;
82 break;
85 if (argc == 1)
86 help = 1;
89 if (strcmp(command, "sharemgr") == 0) {
90 command = argv[optind];
91 argv++;
92 argc--;
95 if (help) {
96 /* no subcommand */
97 global_help();
98 exit(SA_OK);
102 * now have enough to parse rest of command line
104 * First, initialize the plugin architecture.
105 * Plugins are needed in the event of a global help
106 * request.
108 * reset optind to 1 so the parsing that takes place in
109 * sa_init() will work correctly.
112 optind = 1;
113 handle = sa_init(SA_INIT_SHARE_API);
116 * reset optind again since we will start parsing all over in
117 * the sub-commands.
119 optind = 1;
120 rval = run_command(command, argc, argv, protocol, handle);
122 sa_fini(handle);
123 return (rval);
126 static int
127 run_command(char *command, int argc, char *argv[], char *proto,
128 sa_handle_t handle)
130 sa_command_t *cmdvec;
131 int ret;
134 * To get here, we know there should be a command due to the
135 * preprocessing done earlier. Need to find the protocol
136 * that is being affected. If no protocol, then it is ALL
137 * protocols.
139 * We don't currently use the protocol here at this point. It
140 * is left in as a placeholder for the future addition of
141 * protocol specific sub-commands.
143 * Known sub-commands are handled at this level. An unknown
144 * command will be passed down to the shared object that
145 * actually implements it. We can do this since the semantics
146 * of the common sub-commands is well defined.
149 cmdvec = sa_lookup(command, proto);
150 if (cmdvec == NULL) {
151 (void) printf(gettext("command %s not found\n"), command);
152 exit(1);
155 * need to check priviledges and restrict what can be done
156 * based on least priviledge and sub-command so pass this in
157 * as a flag.
159 ret = cmdvec->cmdfunc(handle, cmdvec->priv, argc, argv);
160 return (ret);