xref: /titanic_51/usr/src/cmd/lp/lib/lp/printlist.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1997 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.7	*/
32 /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
33 
34 #include "stdio.h"
35 #include "string.h"
36 
37 #include "lp.h"
38 
39 #define	DFLT_PREFIX	0
40 #define	DFLT_SUFFIX	0
41 #define	DFLT_SEP	"\n"
42 #define	DFLT_NEWLINE	"\n"
43 
44 int			printlist_qsep	= 0;
45 
46 static char		*print_prefix	= DFLT_PREFIX,
47 			*print_suffix	= DFLT_SUFFIX,
48 			*print_sep	= DFLT_SEP,
49 			*print_newline	= DFLT_NEWLINE;
50 
51 static void		q_print( int, char * , char * );
52 
53 /**
54  ** printlist_setup() - ARRANGE FOR CUSTOM PRINTING
55  ** printlist_unsetup() - RESET STANDARD PRINTING
56  **/
57 
58 void
59 printlist_setup(char *prefix, char *suffix, char *sep, char *newline)
60 {
61 	if (prefix)
62 		print_prefix = prefix;
63 	if (suffix)
64 		print_suffix = suffix;
65 	if (sep)
66 		print_sep = sep;
67 	if (newline)
68 		print_newline = newline;
69 	return;
70 }
71 
72 void
73 printlist_unsetup()
74 {
75 	print_prefix = DFLT_PREFIX;
76 	print_suffix = DFLT_SUFFIX;
77 	print_sep = DFLT_SEP;
78 	print_newline = DFLT_NEWLINE;
79 	return;
80 }
81 
82 /**
83  ** printlist() - PRINT LIST ON OPEN CHANNEL
84  **/
85 
86 int
87 printlist(FILE *fp, char **list)
88 {
89 	return (fdprintlist(fileno(fp), list));
90 }
91 
92 int
93 fdprintlist(int fd, char **list)
94 {
95 	register char		*sep;
96 
97 	if (list)
98 	    for (sep = ""; *list; *list++, sep = print_sep) {
99 
100 		(void)fdprintf (fd, "%s%s", sep, NB(print_prefix));
101 		if (printlist_qsep)
102 			q_print (fd, *list, print_sep);
103 		else
104 			(void)fdprintf (fd, "%s", *list);
105 		errno = 0;
106 		(void)fdprintf (fd, "%s", NB(print_suffix));
107 		if (errno != 0)
108 			return (-1);
109 
110 	    }
111 	(void)fdprintf (fd, print_newline);
112 
113 	return (0);
114 }
115 
116 
117 static void
118 q_print(int fd, char *str, char *sep)
119 {
120 	while (*str) {
121 		if (strchr(sep, *str))
122 			fdputc('\\', fd);
123 		fdputc(*str, fd);
124 		str++;
125 	}
126 	return;
127 }
128