xref: /illumos-gate/usr/src/cmd/lp/lib/lp/printlist.c (revision 2a8bcb4e)
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 /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
31 
32 #include "stdio.h"
33 #include "string.h"
34 
35 #include "lp.h"
36 
37 #define	DFLT_PREFIX	0
38 #define	DFLT_SUFFIX	0
39 #define	DFLT_SEP	"\n"
40 #define	DFLT_NEWLINE	"\n"
41 
42 int			printlist_qsep	= 0;
43 
44 static char		*print_prefix	= DFLT_PREFIX,
45 			*print_suffix	= DFLT_SUFFIX,
46 			*print_sep	= DFLT_SEP,
47 			*print_newline	= DFLT_NEWLINE;
48 
49 static void		q_print( int, char * , char * );
50 
51 /**
52  ** printlist_setup() - ARRANGE FOR CUSTOM PRINTING
53  ** printlist_unsetup() - RESET STANDARD PRINTING
54  **/
55 
56 void
printlist_setup(char * prefix,char * suffix,char * sep,char * newline)57 printlist_setup(char *prefix, char *suffix, char *sep, char *newline)
58 {
59 	if (prefix)
60 		print_prefix = prefix;
61 	if (suffix)
62 		print_suffix = suffix;
63 	if (sep)
64 		print_sep = sep;
65 	if (newline)
66 		print_newline = newline;
67 	return;
68 }
69 
70 void
printlist_unsetup()71 printlist_unsetup()
72 {
73 	print_prefix = DFLT_PREFIX;
74 	print_suffix = DFLT_SUFFIX;
75 	print_sep = DFLT_SEP;
76 	print_newline = DFLT_NEWLINE;
77 	return;
78 }
79 
80 /**
81  ** printlist() - PRINT LIST ON OPEN CHANNEL
82  **/
83 
84 int
printlist(FILE * fp,char ** list)85 printlist(FILE *fp, char **list)
86 {
87 	return (fdprintlist(fileno(fp), list));
88 }
89 
90 int
fdprintlist(int fd,char ** list)91 fdprintlist(int fd, char **list)
92 {
93 	register char		*sep;
94 
95 	if (list)
96 	    for (sep = ""; *list; *list++, sep = print_sep) {
97 
98 		(void)fdprintf (fd, "%s%s", sep, NB(print_prefix));
99 		if (printlist_qsep)
100 			q_print (fd, *list, print_sep);
101 		else
102 			(void)fdprintf (fd, "%s", *list);
103 		errno = 0;
104 		(void)fdprintf (fd, "%s", NB(print_suffix));
105 		if (errno != 0)
106 			return (-1);
107 
108 	    }
109 	(void)fdprintf (fd, print_newline);
110 
111 	return (0);
112 }
113 
114 
115 static void
q_print(int fd,char * str,char * sep)116 q_print(int fd, char *str, char *sep)
117 {
118 	while (*str) {
119 		if (strchr(sep, *str))
120 			fdputc('\\', fd);
121 		fdputc(*str, fd);
122 		str++;
123 	}
124 	return;
125 }
126