8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / svc / svccfg / svccfg_main.c
blobaee16b5da27268d6e0051ba78d3c5d77fb00320b
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
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * svccfg - modify service configuration repository
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
34 #include <errno.h>
35 #include <libintl.h>
36 #include <libscf.h>
37 #include <libscf_priv.h>
38 #include <libuutil.h>
39 #include <locale.h>
40 #include <signal.h>
41 #include <stdarg.h>
42 #include <stddef.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
48 #include "svccfg.h"
50 #ifndef TEXT_DOMAIN
51 #define TEXT_DOMAIN "SUNW_OST_OSCMD"
52 #endif /* TEXT_DOMAIN */
54 #define MAX_CMD_LINE_SZ 2048
56 static const char *myname;
57 int g_verbose = 0;
58 const char *fmri;
60 static void
61 usage()
63 (void) fprintf(stderr, gettext(
64 "Usage:\tsvccfg [-v] [-s FMRI] [-f file]\n"
65 "\tsvccfg [-v] [-s FMRI] <command> [args]\n"));
66 exit(UU_EXIT_USAGE);
69 void *
70 safe_malloc(size_t sz)
72 void *p;
74 if ((p = calloc(1, sz)) == NULL)
75 uu_die(gettext("Out of memory.\n"));
77 return (p);
80 char *
81 safe_strdup(const char *cp)
83 char *result;
85 result = strdup(cp);
86 if (result == NULL)
87 uu_die(gettext("Out of memory.\n"));
89 return (result);
93 * Send a message to the user. If we're interactive, send it to stdout.
94 * Otherwise send it to stderr.
96 static void
97 vmessage(const char *fmt, va_list va)
99 int interactive = est->sc_cmd_flags & SC_CMD_IACTIVE;
100 FILE *strm = interactive ? stdout : stderr;
101 const char *ptr;
103 if (!interactive) {
104 if (est->sc_cmd_file == NULL)
105 (void) fprintf(stderr, "%s: ", myname);
106 else
107 (void) fprintf(stderr, "%s (%s, line %d): ", myname,
108 est->sc_cmd_filename, est->sc_cmd_lineno - 1);
111 if (vfprintf(strm, fmt, va) < 0 && interactive)
112 uu_die(gettext("printf() error"));
114 ptr = strchr(fmt, '\0');
115 if (*(ptr - 1) != '\n')
116 (void) fprintf(strm, ": %s.\n", strerror(errno));
120 * Display a warning. Should usually be predicated by g_verbose.
122 /* PRINTFLIKE1 */
123 void
124 warn(const char *fmt, ...)
126 va_list va;
128 va_start(va, fmt);
129 vmessage(fmt, va);
130 va_end(va);
134 * Syntax error.
136 void
137 synerr(int com)
139 if (est->sc_cmd_flags & SC_CMD_IACTIVE) {
140 help(com);
141 return;
144 warn(gettext("Syntax error.\n"));
146 if ((est->sc_cmd_flags & SC_CMD_DONT_EXIT) == 0)
147 exit(1);
151 * Semantic error. Display the warning and exit if we're not interactive.
153 /* PRINTFLIKE1 */
154 void
155 semerr(const char *fmt, ...)
157 va_list va;
159 va_start(va, fmt);
160 vmessage(fmt, va);
161 va_end(va);
163 if ((est->sc_cmd_flags & (SC_CMD_IACTIVE | SC_CMD_DONT_EXIT)) == 0)
164 exit(1);
167 /*ARGSUSED*/
168 static void
169 initialize(int argc, char *argv[])
171 myname = uu_setpname(argv[0]);
172 (void) atexit(lscf_cleanup);
174 (void) setlocale(LC_ALL, "");
175 (void) textdomain(TEXT_DOMAIN);
177 (void) lxml_init();
178 internal_init();
179 engine_init();
180 lscf_init(); /* must follow engine_init() */
181 tmpl_init();
185 main(int argc, char *argv[])
187 char *cmd, *command_file = NULL;
188 char *fmri = NULL;
189 int c;
191 while ((c = getopt(argc, argv, "vf:s:")) != EOF)
192 switch (c) {
193 case 'v':
194 g_verbose = 1;
195 break;
197 case 's':
198 fmri = optarg;
199 break;
201 case 'f':
202 command_file = optarg;
203 break;
205 default:
206 usage();
207 break;
210 initialize(argc, argv);
212 if (fmri != NULL)
213 lscf_select(fmri);
215 if (command_file != NULL)
216 return (engine_source(command_file, 0));
218 if (optind == argc) {
219 if (isatty(fileno(stdin)))
220 return (engine_interp());
221 else
222 return (engine_source("-", 0));
226 * Knit together remaining arguments into a single statement.
228 cmd = safe_malloc(MAX_CMD_LINE_SZ);
229 for (c = optind; c < argc; c++) {
230 (void) strlcat(cmd, argv[c], MAX_CMD_LINE_SZ);
231 (void) strlcat(cmd, " ", MAX_CMD_LINE_SZ);
234 return (engine_exec(cmd));