1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 1997 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
29*7c478bd9Sstevel@tonic-gate
30*7c478bd9Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
31*7c478bd9Sstevel@tonic-gate
32*7c478bd9Sstevel@tonic-gate #include "stdio.h"
33*7c478bd9Sstevel@tonic-gate #include "string.h"
34*7c478bd9Sstevel@tonic-gate #include "stdlib.h"
35*7c478bd9Sstevel@tonic-gate
36*7c478bd9Sstevel@tonic-gate #include "lp.h"
37*7c478bd9Sstevel@tonic-gate
38*7c478bd9Sstevel@tonic-gate #define N_COMPRESSED 9999
39*7c478bd9Sstevel@tonic-gate
40*7c478bd9Sstevel@tonic-gate /**
41*7c478bd9Sstevel@tonic-gate ** printsdn() - PRINT A SCALED DECIMAL NUMBER NICELY
42*7c478bd9Sstevel@tonic-gate **/
43*7c478bd9Sstevel@tonic-gate
44*7c478bd9Sstevel@tonic-gate #define DFLT_PREFIX 0
45*7c478bd9Sstevel@tonic-gate #define DFLT_SUFFIX 0
46*7c478bd9Sstevel@tonic-gate #define DFLT_NEWLINE "\n"
47*7c478bd9Sstevel@tonic-gate
48*7c478bd9Sstevel@tonic-gate static char *print_prefix = DFLT_PREFIX,
49*7c478bd9Sstevel@tonic-gate *print_suffix = DFLT_SUFFIX,
50*7c478bd9Sstevel@tonic-gate *print_newline = DFLT_NEWLINE;
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate void
printsdn_setup(char * prefix,char * suffix,char * newline)53*7c478bd9Sstevel@tonic-gate printsdn_setup(char *prefix, char *suffix, char *newline)
54*7c478bd9Sstevel@tonic-gate {
55*7c478bd9Sstevel@tonic-gate if (prefix)
56*7c478bd9Sstevel@tonic-gate print_prefix = prefix;
57*7c478bd9Sstevel@tonic-gate if (suffix)
58*7c478bd9Sstevel@tonic-gate print_suffix = suffix;
59*7c478bd9Sstevel@tonic-gate if (newline)
60*7c478bd9Sstevel@tonic-gate print_newline = newline;
61*7c478bd9Sstevel@tonic-gate return;
62*7c478bd9Sstevel@tonic-gate }
63*7c478bd9Sstevel@tonic-gate
64*7c478bd9Sstevel@tonic-gate void
printsdn_unsetup()65*7c478bd9Sstevel@tonic-gate printsdn_unsetup ()
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate print_prefix = DFLT_PREFIX;
68*7c478bd9Sstevel@tonic-gate print_suffix = DFLT_SUFFIX;
69*7c478bd9Sstevel@tonic-gate print_newline = DFLT_NEWLINE;
70*7c478bd9Sstevel@tonic-gate return;
71*7c478bd9Sstevel@tonic-gate }
72*7c478bd9Sstevel@tonic-gate
73*7c478bd9Sstevel@tonic-gate
74*7c478bd9Sstevel@tonic-gate void
printsdn(FILE * fp,SCALED sdn)75*7c478bd9Sstevel@tonic-gate printsdn(FILE *fp, SCALED sdn)
76*7c478bd9Sstevel@tonic-gate {
77*7c478bd9Sstevel@tonic-gate fdprintsdn(fileno(fp), sdn);
78*7c478bd9Sstevel@tonic-gate }
79*7c478bd9Sstevel@tonic-gate
80*7c478bd9Sstevel@tonic-gate
81*7c478bd9Sstevel@tonic-gate void
fdprintsdn(int fd,SCALED sdn)82*7c478bd9Sstevel@tonic-gate fdprintsdn(int fd, SCALED sdn)
83*7c478bd9Sstevel@tonic-gate {
84*7c478bd9Sstevel@tonic-gate register char *dec = "9999.999",
85*7c478bd9Sstevel@tonic-gate *z;
86*7c478bd9Sstevel@tonic-gate
87*7c478bd9Sstevel@tonic-gate if (sdn.val <= 0)
88*7c478bd9Sstevel@tonic-gate return;
89*7c478bd9Sstevel@tonic-gate
90*7c478bd9Sstevel@tonic-gate (void)fdprintf (fd, "%s", NB(print_prefix));
91*7c478bd9Sstevel@tonic-gate
92*7c478bd9Sstevel@tonic-gate /*
93*7c478bd9Sstevel@tonic-gate * Let's try to be a bit clever in dealing with decimal
94*7c478bd9Sstevel@tonic-gate * numbers. If the number is an integer, don't print
95*7c478bd9Sstevel@tonic-gate * a decimal point. If it isn't an integer, strip trailing
96*7c478bd9Sstevel@tonic-gate * zeros from the fraction part, and don't print more
97*7c478bd9Sstevel@tonic-gate * than the thousandths place.
98*7c478bd9Sstevel@tonic-gate */
99*7c478bd9Sstevel@tonic-gate if (-1000. < sdn.val && sdn.val < 10000.) {
100*7c478bd9Sstevel@tonic-gate
101*7c478bd9Sstevel@tonic-gate /*
102*7c478bd9Sstevel@tonic-gate * Printing 0 will give us 0.000.
103*7c478bd9Sstevel@tonic-gate */
104*7c478bd9Sstevel@tonic-gate sprintf (dec, "%.3f", sdn.val);
105*7c478bd9Sstevel@tonic-gate
106*7c478bd9Sstevel@tonic-gate /*
107*7c478bd9Sstevel@tonic-gate * Skip zeroes from the end until we hit
108*7c478bd9Sstevel@tonic-gate * '.' or not-0. If we hit '.', clobber it;
109*7c478bd9Sstevel@tonic-gate * if we hit not-0, it has to be in fraction
110*7c478bd9Sstevel@tonic-gate * part, so leave it.
111*7c478bd9Sstevel@tonic-gate */
112*7c478bd9Sstevel@tonic-gate z = dec + strlen(dec) - 1;
113*7c478bd9Sstevel@tonic-gate while (*z == '0' && *z != '.')
114*7c478bd9Sstevel@tonic-gate z--;
115*7c478bd9Sstevel@tonic-gate if (*z == '.')
116*7c478bd9Sstevel@tonic-gate *z = '\0';
117*7c478bd9Sstevel@tonic-gate else
118*7c478bd9Sstevel@tonic-gate *++z = '\0';
119*7c478bd9Sstevel@tonic-gate
120*7c478bd9Sstevel@tonic-gate (void)fdprintf(fd, "%s", dec);
121*7c478bd9Sstevel@tonic-gate
122*7c478bd9Sstevel@tonic-gate } else
123*7c478bd9Sstevel@tonic-gate (void)fdprintf(fd, "%.3f", sdn.val);
124*7c478bd9Sstevel@tonic-gate
125*7c478bd9Sstevel@tonic-gate if (sdn.sc == 'i' || sdn.sc == 'c')
126*7c478bd9Sstevel@tonic-gate fdputc(sdn.sc, fd);
127*7c478bd9Sstevel@tonic-gate
128*7c478bd9Sstevel@tonic-gate (void)fdprintf(fd, "%s%s", NB(print_suffix), NB(print_newline));
129*7c478bd9Sstevel@tonic-gate return;
130*7c478bd9Sstevel@tonic-gate }
131*7c478bd9Sstevel@tonic-gate
132*7c478bd9Sstevel@tonic-gate
133*7c478bd9Sstevel@tonic-gate /**
134*7c478bd9Sstevel@tonic-gate ** _getsdn() - PARSE SCALED DECIMAL NUMBER
135*7c478bd9Sstevel@tonic-gate **/
136*7c478bd9Sstevel@tonic-gate
137*7c478bd9Sstevel@tonic-gate SCALED
_getsdn(char * str,char ** p_after,int is_cpi)138*7c478bd9Sstevel@tonic-gate _getsdn(char *str, char **p_after, int is_cpi)
139*7c478bd9Sstevel@tonic-gate {
140*7c478bd9Sstevel@tonic-gate static SCALED sdn = { 0.0 , 0 };
141*7c478bd9Sstevel@tonic-gate
142*7c478bd9Sstevel@tonic-gate char * rest;
143*7c478bd9Sstevel@tonic-gate
144*7c478bd9Sstevel@tonic-gate
145*7c478bd9Sstevel@tonic-gate /*
146*7c478bd9Sstevel@tonic-gate * A nonzero "errno" is our only method of indicating error.
147*7c478bd9Sstevel@tonic-gate */
148*7c478bd9Sstevel@tonic-gate errno = 0;
149*7c478bd9Sstevel@tonic-gate
150*7c478bd9Sstevel@tonic-gate if (is_cpi && STREQU(str, NAME_PICA)) {
151*7c478bd9Sstevel@tonic-gate sdn.val = 10;
152*7c478bd9Sstevel@tonic-gate sdn.sc = 0;
153*7c478bd9Sstevel@tonic-gate if (p_after)
154*7c478bd9Sstevel@tonic-gate *p_after = str + strlen(NAME_PICA);
155*7c478bd9Sstevel@tonic-gate
156*7c478bd9Sstevel@tonic-gate } else if (is_cpi && STREQU(str, NAME_ELITE)) {
157*7c478bd9Sstevel@tonic-gate sdn.val = 12;
158*7c478bd9Sstevel@tonic-gate sdn.sc = 0;
159*7c478bd9Sstevel@tonic-gate if (p_after)
160*7c478bd9Sstevel@tonic-gate *p_after = str + strlen(NAME_ELITE);
161*7c478bd9Sstevel@tonic-gate
162*7c478bd9Sstevel@tonic-gate } else if (is_cpi && STREQU(str, NAME_COMPRESSED)) {
163*7c478bd9Sstevel@tonic-gate sdn.val = N_COMPRESSED;
164*7c478bd9Sstevel@tonic-gate sdn.sc = 0;
165*7c478bd9Sstevel@tonic-gate if (p_after)
166*7c478bd9Sstevel@tonic-gate *p_after = str + strlen(NAME_COMPRESSED);
167*7c478bd9Sstevel@tonic-gate
168*7c478bd9Sstevel@tonic-gate } else {
169*7c478bd9Sstevel@tonic-gate sdn.val = strtod(str, &rest);
170*7c478bd9Sstevel@tonic-gate if (sdn.val <= 0) {
171*7c478bd9Sstevel@tonic-gate lp_errno = LP_EBADSDN;
172*7c478bd9Sstevel@tonic-gate errno = EINVAL;
173*7c478bd9Sstevel@tonic-gate return (sdn);
174*7c478bd9Sstevel@tonic-gate }
175*7c478bd9Sstevel@tonic-gate
176*7c478bd9Sstevel@tonic-gate while (*rest && *rest == ' ')
177*7c478bd9Sstevel@tonic-gate rest++;
178*7c478bd9Sstevel@tonic-gate
179*7c478bd9Sstevel@tonic-gate switch (*rest) {
180*7c478bd9Sstevel@tonic-gate case 0:
181*7c478bd9Sstevel@tonic-gate sdn.sc = 0;
182*7c478bd9Sstevel@tonic-gate if (p_after)
183*7c478bd9Sstevel@tonic-gate *p_after = rest;
184*7c478bd9Sstevel@tonic-gate break;
185*7c478bd9Sstevel@tonic-gate case 'i':
186*7c478bd9Sstevel@tonic-gate case 'c':
187*7c478bd9Sstevel@tonic-gate sdn.sc = *rest++;
188*7c478bd9Sstevel@tonic-gate if (p_after)
189*7c478bd9Sstevel@tonic-gate *p_after = rest;
190*7c478bd9Sstevel@tonic-gate break;
191*7c478bd9Sstevel@tonic-gate default:
192*7c478bd9Sstevel@tonic-gate lp_errno = LP_EBADSDN;
193*7c478bd9Sstevel@tonic-gate errno = EINVAL;
194*7c478bd9Sstevel@tonic-gate sdn.sc = 0;
195*7c478bd9Sstevel@tonic-gate break;
196*7c478bd9Sstevel@tonic-gate }
197*7c478bd9Sstevel@tonic-gate }
198*7c478bd9Sstevel@tonic-gate
199*7c478bd9Sstevel@tonic-gate return (sdn);
200*7c478bd9Sstevel@tonic-gate }
201