xref: /titanic_51/usr/src/cmd/svr4pkg/libinst/echo.c (revision 5c51f1241dbbdf2656d0e10011981411ed0c9673)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 
30 
31 
32 /*
33  * System includes
34  */
35 
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <sys/types.h>
41 #include <zone.h>
42 
43 /*
44  * consolidation pkg command library includes
45  */
46 
47 #include "pkglib.h"
48 
49 /*
50  * internal global variables
51  */
52 
53 static boolean_t	debugFlag = B_FALSE;	/* debug messages enabled? */
54 static boolean_t	echoFlag = B_TRUE;	/* interactive msgs enabled? */
55 
56 /*
57  * *****************************************************************************
58  * global external (public) functions
59  * *****************************************************************************
60  */
61 
62 /*
63  * Name:	echo
64  * Synopsis:	Output an interactive message if interaction is enabled
65  * Description:	Main method for outputting an interactive message; call to
66  *		output interactive message if interation has not been disabled
67  *		by a previous call to echoSetFlag(0).
68  * Arguments:	format - [RO, RO*] (char *)
69  *			printf-style format for debugging message to be output
70  *		VARG_LIST - [RO] (?)
71  *			arguments as appropriate to 'format' specified
72  * Returns:	void
73  */
74 
75 /*PRINTFLIKE1*/
76 void
77 echo(char *fmt, ...)
78 {
79 	va_list ap;
80 
81 	/* output message if echoing is enabled */
82 
83 	if (echoFlag == B_TRUE) {
84 		va_start(ap, fmt);
85 
86 		(void) vfprintf(stderr, fmt, ap);
87 
88 		va_end(ap);
89 
90 		(void) putc('\n', stderr);
91 	}
92 }
93 
94 /*
95  * Name:	echoDebug
96  * Synopsis:	Output a debugging message if debugging is enabled
97  * Description:	Main method for outputting a debugging message; call to
98  *		output debugging message if debugging has been enabled
99  *		by a previous call to echoDebugSetFlag(1).
100  * Arguments:	format - [RO, RO*] (char *)
101  *			printf-style format for debugging message to be output
102  *		VARG_LIST - [RO] (?)
103  *			arguments as appropriate to 'format' specified
104  * Returns:	void
105  * NOTE:	format of message will be:
106  *			# [ aaa bbb ccc ] message
107  *		where:	aaa - process i.d.
108  *			bbb - zone i.d.
109  *			ccc - name of program
110  * 		for example:
111  *			# [ 25685   0 pkgadd     ] unable to get package list
112  */
113 
114 /*PRINTFLIKE1*/
115 void
116 echoDebug(char *a_fmt, ...)
117 {
118 	va_list ap;
119 
120 	/* output debugging message if debugging is enabled */
121 
122 	if (debugFlag == B_TRUE) {
123 		char	*p = get_prog_name();
124 
125 		(void) fprintf(stderr, "# [%6d %3d", getpid(), getzoneid());
126 
127 		if ((p != (char *)NULL) && (*p != '\0')) {
128 			fprintf(stderr, " %-11s", p);
129 		}
130 
131 		(void) fprintf(stderr, "] ");
132 
133 		va_start(ap, a_fmt);
134 
135 		(void) vfprintf(stderr, a_fmt, ap);
136 
137 		va_end(ap);
138 
139 		(void) putc('\n', stderr);
140 	}
141 }
142 
143 /*
144  * get the "interactive message enabled" flag
145  */
146 
147 boolean_t
148 echoGetFlag(void) {
149 	return (echoFlag);
150 }
151 
152 /*
153  * set the "interactive message enabled" flag
154  */
155 
156 boolean_t
157 echoSetFlag(boolean_t a_echoFlag)
158 {
159 	boolean_t	oldvalue;
160 
161 	oldvalue = echoFlag;
162 	echoFlag = a_echoFlag;
163 	return (oldvalue);
164 }
165 
166 /*
167  * get the "debugging message enabled" flag
168  */
169 
170 boolean_t
171 echoDebugGetFlag(void) {
172 	return (debugFlag);
173 }
174 
175 /*
176  * set the "debugging message enabled" flag
177  */
178 
179 boolean_t
180 echoDebugSetFlag(boolean_t a_debugFlag)
181 {
182 	boolean_t	oldvalue;
183 
184 	oldvalue = debugFlag;
185 	debugFlag = a_debugFlag;
186 	return (oldvalue);
187 }
188