xref: /illumos-gate/usr/src/cmd/nl/nl.c (revision 7c478bd9)
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 1995 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 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <locale.h>
34*7c478bd9Sstevel@tonic-gate #include <regexpr.h>
35*7c478bd9Sstevel@tonic-gate #include <stdio.h>
36*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
37*7c478bd9Sstevel@tonic-gate #include <string.h>
38*7c478bd9Sstevel@tonic-gate #include <errno.h>
39*7c478bd9Sstevel@tonic-gate #include <wchar.h>
40*7c478bd9Sstevel@tonic-gate #include <wctype.h>
41*7c478bd9Sstevel@tonic-gate #include <limits.h>
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #define	EXPSIZ		512
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate #ifdef XPG4
46*7c478bd9Sstevel@tonic-gate #define	USAGE "usage: nl [-p] [-b type] [-d delim] [ -f type] " \
47*7c478bd9Sstevel@tonic-gate 	"[-h type] [-i incr] [-l num] [-n format]\n" \
48*7c478bd9Sstevel@tonic-gate 	"[-s sep] [-v startnum] [-w width] [file]\n"
49*7c478bd9Sstevel@tonic-gate #else
50*7c478bd9Sstevel@tonic-gate #define	USAGE "usage: nl [-p] [-btype] [-ddelim] [ -ftype] " \
51*7c478bd9Sstevel@tonic-gate 	"[-htype] [-iincr] [-lnum] [-nformat] [-ssep] " \
52*7c478bd9Sstevel@tonic-gate 	"[-vstartnum] [-wwidth] [file]\n"
53*7c478bd9Sstevel@tonic-gate #endif
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate #ifdef u370
56*7c478bd9Sstevel@tonic-gate 	int nbra, sed;	/* u370 - not used in nl.c, but extern in regexp.h */
57*7c478bd9Sstevel@tonic-gate #endif
58*7c478bd9Sstevel@tonic-gate static int width = 6;	/* Declare default width of number */
59*7c478bd9Sstevel@tonic-gate static char nbuf[100];	/* Declare bufsize used in convert/pad/cnt routines */
60*7c478bd9Sstevel@tonic-gate static char *bexpbuf;	/* Declare the regexp buf */
61*7c478bd9Sstevel@tonic-gate static char *hexpbuf;	/* Declare the regexp buf */
62*7c478bd9Sstevel@tonic-gate static char *fexpbuf;	/* Declare the regexp buf */
63*7c478bd9Sstevel@tonic-gate static char delim1 = '\\';
64*7c478bd9Sstevel@tonic-gate static char delim2 = ':';	/* Default delimiters. */
65*7c478bd9Sstevel@tonic-gate static char pad = ' ';	/* Declare the default pad for numbers */
66*7c478bd9Sstevel@tonic-gate static char *s;	/* Declare the temp array for args */
67*7c478bd9Sstevel@tonic-gate static char s1[EXPSIZ];	/* Declare the conversion array */
68*7c478bd9Sstevel@tonic-gate static char format = 'n'; /* Declare the format of numbers to be rt just */
69*7c478bd9Sstevel@tonic-gate static int q = 2;	/* Initialize arg pointer to drop 1st 2 chars */
70*7c478bd9Sstevel@tonic-gate static int k;	/* Declare var for return of convert */
71*7c478bd9Sstevel@tonic-gate static int r;	/* Declare the arg array ptr for string args */
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate #ifdef XPG4
74*7c478bd9Sstevel@tonic-gate static int convert(int, char *);
75*7c478bd9Sstevel@tonic-gate #else
76*7c478bd9Sstevel@tonic-gate static int convert(char *);
77*7c478bd9Sstevel@tonic-gate #endif
78*7c478bd9Sstevel@tonic-gate static void num(int, int);
79*7c478bd9Sstevel@tonic-gate static void npad(int, char *);
80*7c478bd9Sstevel@tonic-gate #ifdef XPG4
81*7c478bd9Sstevel@tonic-gate static void optmsg(int, char *);
82*7c478bd9Sstevel@tonic-gate #else
83*7c478bd9Sstevel@tonic-gate static void optmsg(char *);
84*7c478bd9Sstevel@tonic-gate #endif
85*7c478bd9Sstevel@tonic-gate static void pnum(int, char *);
86*7c478bd9Sstevel@tonic-gate static void regerr(int);
87*7c478bd9Sstevel@tonic-gate static void usage();
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate extern char *optarg;	/* getopt support */
90*7c478bd9Sstevel@tonic-gate extern int optind;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate int
93*7c478bd9Sstevel@tonic-gate main(argc, argv)
94*7c478bd9Sstevel@tonic-gate int argc;
95*7c478bd9Sstevel@tonic-gate char *argv[];
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate 	register int j;
98*7c478bd9Sstevel@tonic-gate 	register int i = 0;
99*7c478bd9Sstevel@tonic-gate 	register char *p;
100*7c478bd9Sstevel@tonic-gate 	register char header = 'n';
101*7c478bd9Sstevel@tonic-gate 	register char body = 't';
102*7c478bd9Sstevel@tonic-gate 	register char footer = 'n';
103*7c478bd9Sstevel@tonic-gate 	char line[LINE_MAX];
104*7c478bd9Sstevel@tonic-gate 	char tempchr;	/* Temporary holding variable. */
105*7c478bd9Sstevel@tonic-gate 	char swtch = 'n';
106*7c478bd9Sstevel@tonic-gate 	char cntck = 'n';
107*7c478bd9Sstevel@tonic-gate 	char type;
108*7c478bd9Sstevel@tonic-gate 	int cnt;	/* line counter */
109*7c478bd9Sstevel@tonic-gate 	int pass1 = 1;	/* First pass flag. 1=pass1, 0=additional passes. */
110*7c478bd9Sstevel@tonic-gate 	char sep[EXPSIZ];
111*7c478bd9Sstevel@tonic-gate 	char pat[EXPSIZ];
112*7c478bd9Sstevel@tonic-gate 	int startcnt = 1;
113*7c478bd9Sstevel@tonic-gate 	int increment = 1;
114*7c478bd9Sstevel@tonic-gate 	int blank = 1;
115*7c478bd9Sstevel@tonic-gate 	int blankctr = 0;
116*7c478bd9Sstevel@tonic-gate 	int c;
117*7c478bd9Sstevel@tonic-gate 	int lnt;
118*7c478bd9Sstevel@tonic-gate 	char last;
119*7c478bd9Sstevel@tonic-gate 	FILE *iptr = stdin;
120*7c478bd9Sstevel@tonic-gate 	FILE *optr = stdout;
121*7c478bd9Sstevel@tonic-gate #ifndef XPG4
122*7c478bd9Sstevel@tonic-gate 	int option_end = 0;
123*7c478bd9Sstevel@tonic-gate #endif
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	sep[0] = '\t';
126*7c478bd9Sstevel@tonic-gate 	sep[1] = '\0';
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
129*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
130*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
131*7c478bd9Sstevel@tonic-gate #endif
132*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate #ifdef XPG4
135*7c478bd9Sstevel@tonic-gate 	/*
136*7c478bd9Sstevel@tonic-gate 	 * XPG4:  Allow either a space or no space between the
137*7c478bd9Sstevel@tonic-gate 	 *	  options and their required arguments.
138*7c478bd9Sstevel@tonic-gate 	 */
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	while (argc > 0) {
141*7c478bd9Sstevel@tonic-gate 		while ((c = getopt(argc, argv,
142*7c478bd9Sstevel@tonic-gate 			"pb:d:f:h:i:l:n:s:v:w:")) != EOF) {
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 			switch (c) {
145*7c478bd9Sstevel@tonic-gate 			case 'h':
146*7c478bd9Sstevel@tonic-gate 				switch (*optarg) {
147*7c478bd9Sstevel@tonic-gate 				case 'n':
148*7c478bd9Sstevel@tonic-gate 					header = 'n';
149*7c478bd9Sstevel@tonic-gate 					break;
150*7c478bd9Sstevel@tonic-gate 				case 't':
151*7c478bd9Sstevel@tonic-gate 					header = 't';
152*7c478bd9Sstevel@tonic-gate 					break;
153*7c478bd9Sstevel@tonic-gate 				case 'a':
154*7c478bd9Sstevel@tonic-gate 					header = 'a';
155*7c478bd9Sstevel@tonic-gate 					break;
156*7c478bd9Sstevel@tonic-gate 				case 'p':
157*7c478bd9Sstevel@tonic-gate 					(void) strcpy(pat, optarg+1);
158*7c478bd9Sstevel@tonic-gate 					header = 'h';
159*7c478bd9Sstevel@tonic-gate 					hexpbuf =
160*7c478bd9Sstevel@tonic-gate 					    compile(pat, (char *)0, (char *)0);
161*7c478bd9Sstevel@tonic-gate 					if (regerrno)
162*7c478bd9Sstevel@tonic-gate 						regerr(regerrno);
163*7c478bd9Sstevel@tonic-gate 					break;
164*7c478bd9Sstevel@tonic-gate 				case '\0':
165*7c478bd9Sstevel@tonic-gate 					header = 'n';
166*7c478bd9Sstevel@tonic-gate 					break;
167*7c478bd9Sstevel@tonic-gate 				default:
168*7c478bd9Sstevel@tonic-gate 					optmsg(c, optarg);
169*7c478bd9Sstevel@tonic-gate 				}
170*7c478bd9Sstevel@tonic-gate 				break;
171*7c478bd9Sstevel@tonic-gate 			case 'b':
172*7c478bd9Sstevel@tonic-gate 				switch (*optarg) {
173*7c478bd9Sstevel@tonic-gate 				case 't':
174*7c478bd9Sstevel@tonic-gate 					body = 't';
175*7c478bd9Sstevel@tonic-gate 					break;
176*7c478bd9Sstevel@tonic-gate 				case 'a':
177*7c478bd9Sstevel@tonic-gate 					body = 'a';
178*7c478bd9Sstevel@tonic-gate 					break;
179*7c478bd9Sstevel@tonic-gate 				case 'n':
180*7c478bd9Sstevel@tonic-gate 					body = 'n';
181*7c478bd9Sstevel@tonic-gate 					break;
182*7c478bd9Sstevel@tonic-gate 				case 'p':
183*7c478bd9Sstevel@tonic-gate 					(void) strcpy(pat, optarg+1);
184*7c478bd9Sstevel@tonic-gate 					body = 'b';
185*7c478bd9Sstevel@tonic-gate 					bexpbuf =
186*7c478bd9Sstevel@tonic-gate 					compile(pat, (char *)0, (char *)0);
187*7c478bd9Sstevel@tonic-gate 					if (regerrno)
188*7c478bd9Sstevel@tonic-gate 						regerr(regerrno);
189*7c478bd9Sstevel@tonic-gate 					break;
190*7c478bd9Sstevel@tonic-gate 				case '\0':
191*7c478bd9Sstevel@tonic-gate 					body = 't';
192*7c478bd9Sstevel@tonic-gate 					break;
193*7c478bd9Sstevel@tonic-gate 				default:
194*7c478bd9Sstevel@tonic-gate 					optmsg(c, optarg);
195*7c478bd9Sstevel@tonic-gate 				}
196*7c478bd9Sstevel@tonic-gate 				break;
197*7c478bd9Sstevel@tonic-gate 			case 'f':
198*7c478bd9Sstevel@tonic-gate 				switch (*optarg) {
199*7c478bd9Sstevel@tonic-gate 				case 'n':
200*7c478bd9Sstevel@tonic-gate 					footer = 'n';
201*7c478bd9Sstevel@tonic-gate 					break;
202*7c478bd9Sstevel@tonic-gate 				case 't':
203*7c478bd9Sstevel@tonic-gate 					footer = 't';
204*7c478bd9Sstevel@tonic-gate 					break;
205*7c478bd9Sstevel@tonic-gate 				case 'a':
206*7c478bd9Sstevel@tonic-gate 					footer = 'a';
207*7c478bd9Sstevel@tonic-gate 					break;
208*7c478bd9Sstevel@tonic-gate 				case 'p':
209*7c478bd9Sstevel@tonic-gate 					(void) strcpy(pat, optarg+1);
210*7c478bd9Sstevel@tonic-gate 					footer = 'f';
211*7c478bd9Sstevel@tonic-gate 					fexpbuf =
212*7c478bd9Sstevel@tonic-gate 					    compile(pat, (char *)0, (char *)0);
213*7c478bd9Sstevel@tonic-gate 					if (regerrno)
214*7c478bd9Sstevel@tonic-gate 						regerr(regerrno);
215*7c478bd9Sstevel@tonic-gate 					break;
216*7c478bd9Sstevel@tonic-gate 				case '\0':
217*7c478bd9Sstevel@tonic-gate 					footer = 'n';
218*7c478bd9Sstevel@tonic-gate 					break;
219*7c478bd9Sstevel@tonic-gate 				default:
220*7c478bd9Sstevel@tonic-gate 					optmsg(c, optarg);
221*7c478bd9Sstevel@tonic-gate 				}
222*7c478bd9Sstevel@tonic-gate 				break;
223*7c478bd9Sstevel@tonic-gate 			case 'p':
224*7c478bd9Sstevel@tonic-gate 				if (optarg == (char *)NULL)
225*7c478bd9Sstevel@tonic-gate 					cntck = 'y';
226*7c478bd9Sstevel@tonic-gate 				else
227*7c478bd9Sstevel@tonic-gate 					optmsg(c, optarg);
228*7c478bd9Sstevel@tonic-gate 				break;
229*7c478bd9Sstevel@tonic-gate 			case 'v':
230*7c478bd9Sstevel@tonic-gate 				if (*optarg == '\0')
231*7c478bd9Sstevel@tonic-gate 					startcnt = 1;
232*7c478bd9Sstevel@tonic-gate 				else
233*7c478bd9Sstevel@tonic-gate 					startcnt = convert(c, optarg);
234*7c478bd9Sstevel@tonic-gate 				break;
235*7c478bd9Sstevel@tonic-gate 			case 'i':
236*7c478bd9Sstevel@tonic-gate 				if (*optarg == '\0')
237*7c478bd9Sstevel@tonic-gate 					increment = 1;
238*7c478bd9Sstevel@tonic-gate 				else
239*7c478bd9Sstevel@tonic-gate 					increment = convert(c, optarg);
240*7c478bd9Sstevel@tonic-gate 				break;
241*7c478bd9Sstevel@tonic-gate 			case 'w':
242*7c478bd9Sstevel@tonic-gate 				if (*optarg == '\0')
243*7c478bd9Sstevel@tonic-gate 					width = 6;
244*7c478bd9Sstevel@tonic-gate 				else
245*7c478bd9Sstevel@tonic-gate 					width = convert(c, optarg);
246*7c478bd9Sstevel@tonic-gate 				break;
247*7c478bd9Sstevel@tonic-gate 			case 'l':
248*7c478bd9Sstevel@tonic-gate 				if (*optarg == '\0')
249*7c478bd9Sstevel@tonic-gate 					blank = 1;
250*7c478bd9Sstevel@tonic-gate 				else
251*7c478bd9Sstevel@tonic-gate 					blank = convert(c, optarg);
252*7c478bd9Sstevel@tonic-gate 				break;
253*7c478bd9Sstevel@tonic-gate 			case 'n':
254*7c478bd9Sstevel@tonic-gate 				switch (*optarg) {
255*7c478bd9Sstevel@tonic-gate 				case 'l':
256*7c478bd9Sstevel@tonic-gate 					if (*(optarg+1) == 'n')
257*7c478bd9Sstevel@tonic-gate 						format = 'l';
258*7c478bd9Sstevel@tonic-gate 					else
259*7c478bd9Sstevel@tonic-gate 						optmsg(c, optarg);
260*7c478bd9Sstevel@tonic-gate 					break;
261*7c478bd9Sstevel@tonic-gate 				case 'r':
262*7c478bd9Sstevel@tonic-gate 					if ((*(optarg+1) == 'n') ||
263*7c478bd9Sstevel@tonic-gate 					    (*(optarg+1) == 'z'))
264*7c478bd9Sstevel@tonic-gate 						format = *(optarg+1);
265*7c478bd9Sstevel@tonic-gate 					else
266*7c478bd9Sstevel@tonic-gate 						optmsg(c, optarg);
267*7c478bd9Sstevel@tonic-gate 					break;
268*7c478bd9Sstevel@tonic-gate 				case '\0':
269*7c478bd9Sstevel@tonic-gate 					format = 'n';
270*7c478bd9Sstevel@tonic-gate 					break;
271*7c478bd9Sstevel@tonic-gate 				default:
272*7c478bd9Sstevel@tonic-gate 					optmsg(c, optarg);
273*7c478bd9Sstevel@tonic-gate 					break;
274*7c478bd9Sstevel@tonic-gate 				}
275*7c478bd9Sstevel@tonic-gate 				break;
276*7c478bd9Sstevel@tonic-gate 			case 's':
277*7c478bd9Sstevel@tonic-gate 				(void) strcpy(sep, optarg);
278*7c478bd9Sstevel@tonic-gate 				break;
279*7c478bd9Sstevel@tonic-gate 			case 'd':
280*7c478bd9Sstevel@tonic-gate 				delim1 = *optarg;
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 				if (*(optarg+1) == '\0')
283*7c478bd9Sstevel@tonic-gate 					break;
284*7c478bd9Sstevel@tonic-gate 				delim2 = *(optarg+1);
285*7c478bd9Sstevel@tonic-gate 				if (*(optarg+2) != '\0')
286*7c478bd9Sstevel@tonic-gate 					optmsg(c, optarg);
287*7c478bd9Sstevel@tonic-gate 				break;
288*7c478bd9Sstevel@tonic-gate 			default:
289*7c478bd9Sstevel@tonic-gate 				optmsg(c, optarg);
290*7c478bd9Sstevel@tonic-gate 			} /* end switch char returned from getopt() */
291*7c478bd9Sstevel@tonic-gate 		} /* end while getopt */
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 		argv += optind;
294*7c478bd9Sstevel@tonic-gate 		argc -= optind;
295*7c478bd9Sstevel@tonic-gate 		optind = 0;
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate 		if (argc > 0) {
298*7c478bd9Sstevel@tonic-gate 			if ((iptr = fopen(argv[0], "r")) == NULL)  {
299*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "nl: %s: ", argv[0]);
300*7c478bd9Sstevel@tonic-gate 				perror("");
301*7c478bd9Sstevel@tonic-gate 				return (1);
302*7c478bd9Sstevel@tonic-gate 			}
303*7c478bd9Sstevel@tonic-gate 			++argv;
304*7c478bd9Sstevel@tonic-gate 			--argc;
305*7c478bd9Sstevel@tonic-gate 		}
306*7c478bd9Sstevel@tonic-gate 	} /* end while argc > 0 */
307*7c478bd9Sstevel@tonic-gate /* end XPG4 version of argument parsing */
308*7c478bd9Sstevel@tonic-gate #else
309*7c478bd9Sstevel@tonic-gate /*
310*7c478bd9Sstevel@tonic-gate  * Solaris:  For backward compatibility, do not allow a space between the
311*7c478bd9Sstevel@tonic-gate  *	     options and their arguments.  Option arguments are optional,
312*7c478bd9Sstevel@tonic-gate  *	     not required as in the XPG4 version of nl.
313*7c478bd9Sstevel@tonic-gate  */
314*7c478bd9Sstevel@tonic-gate for (j = 1; j < argc; j++) {
315*7c478bd9Sstevel@tonic-gate 	if (argv[j][i] == '-' && (c = argv[j][i + 1])) {
316*7c478bd9Sstevel@tonic-gate 		if (!option_end) {
317*7c478bd9Sstevel@tonic-gate 			switch (c) {
318*7c478bd9Sstevel@tonic-gate 			case 'h':
319*7c478bd9Sstevel@tonic-gate 				switch (argv[j][i + 2]) {
320*7c478bd9Sstevel@tonic-gate 					case 'n':
321*7c478bd9Sstevel@tonic-gate 						header = 'n';
322*7c478bd9Sstevel@tonic-gate 						break;
323*7c478bd9Sstevel@tonic-gate 					case 't':
324*7c478bd9Sstevel@tonic-gate 						header = 't';
325*7c478bd9Sstevel@tonic-gate 						break;
326*7c478bd9Sstevel@tonic-gate 					case 'a':
327*7c478bd9Sstevel@tonic-gate 						header = 'a';
328*7c478bd9Sstevel@tonic-gate 						break;
329*7c478bd9Sstevel@tonic-gate 					case 'p':
330*7c478bd9Sstevel@tonic-gate 						s = argv[j];
331*7c478bd9Sstevel@tonic-gate 						q = 3;
332*7c478bd9Sstevel@tonic-gate 						r = 0;
333*7c478bd9Sstevel@tonic-gate 						while (s[q] != '\0') {
334*7c478bd9Sstevel@tonic-gate 							pat[r] = s[q];
335*7c478bd9Sstevel@tonic-gate 							r++;
336*7c478bd9Sstevel@tonic-gate 							q++;
337*7c478bd9Sstevel@tonic-gate 						}
338*7c478bd9Sstevel@tonic-gate 						pat[r] = '\0';
339*7c478bd9Sstevel@tonic-gate 						header = 'h';
340*7c478bd9Sstevel@tonic-gate 					hexpbuf =
341*7c478bd9Sstevel@tonic-gate 					    compile(pat, (char *)0, (char *)0);
342*7c478bd9Sstevel@tonic-gate 					if (regerrno)
343*7c478bd9Sstevel@tonic-gate 						regerr(regerrno);
344*7c478bd9Sstevel@tonic-gate 						break;
345*7c478bd9Sstevel@tonic-gate 					case '\0':
346*7c478bd9Sstevel@tonic-gate 						header = 'n';
347*7c478bd9Sstevel@tonic-gate 						break;
348*7c478bd9Sstevel@tonic-gate 					default:
349*7c478bd9Sstevel@tonic-gate 						optmsg(argv[j]);
350*7c478bd9Sstevel@tonic-gate 				}
351*7c478bd9Sstevel@tonic-gate 				break;
352*7c478bd9Sstevel@tonic-gate 			case 'b':
353*7c478bd9Sstevel@tonic-gate 				switch (argv[j][i + 2]) {
354*7c478bd9Sstevel@tonic-gate 					case 't':
355*7c478bd9Sstevel@tonic-gate 						body = 't';
356*7c478bd9Sstevel@tonic-gate 						break;
357*7c478bd9Sstevel@tonic-gate 					case 'a':
358*7c478bd9Sstevel@tonic-gate 						body = 'a';
359*7c478bd9Sstevel@tonic-gate 						break;
360*7c478bd9Sstevel@tonic-gate 					case 'n':
361*7c478bd9Sstevel@tonic-gate 						body = 'n';
362*7c478bd9Sstevel@tonic-gate 						break;
363*7c478bd9Sstevel@tonic-gate 					case 'p':
364*7c478bd9Sstevel@tonic-gate 						s = argv[j];
365*7c478bd9Sstevel@tonic-gate 						q = 3;
366*7c478bd9Sstevel@tonic-gate 						r = 0;
367*7c478bd9Sstevel@tonic-gate 						while (s[q] != '\0') {
368*7c478bd9Sstevel@tonic-gate 							pat[r] = s[q];
369*7c478bd9Sstevel@tonic-gate 							r++;
370*7c478bd9Sstevel@tonic-gate 							q++;
371*7c478bd9Sstevel@tonic-gate 						}
372*7c478bd9Sstevel@tonic-gate 						pat[r] = '\0';
373*7c478bd9Sstevel@tonic-gate 						body = 'b';
374*7c478bd9Sstevel@tonic-gate 					bexpbuf =
375*7c478bd9Sstevel@tonic-gate 					    compile(pat, (char *)0, (char *)0);
376*7c478bd9Sstevel@tonic-gate 					if (regerrno)
377*7c478bd9Sstevel@tonic-gate 						regerr(regerrno);
378*7c478bd9Sstevel@tonic-gate 						break;
379*7c478bd9Sstevel@tonic-gate 					case '\0':
380*7c478bd9Sstevel@tonic-gate 						body = 't';
381*7c478bd9Sstevel@tonic-gate 						break;
382*7c478bd9Sstevel@tonic-gate 					default:
383*7c478bd9Sstevel@tonic-gate 						optmsg(argv[j]);
384*7c478bd9Sstevel@tonic-gate 				}
385*7c478bd9Sstevel@tonic-gate 				break;
386*7c478bd9Sstevel@tonic-gate 			case 'f':
387*7c478bd9Sstevel@tonic-gate 				switch (argv[j][i + 2]) {
388*7c478bd9Sstevel@tonic-gate 					case 'n':
389*7c478bd9Sstevel@tonic-gate 						footer = 'n';
390*7c478bd9Sstevel@tonic-gate 						break;
391*7c478bd9Sstevel@tonic-gate 					case 't':
392*7c478bd9Sstevel@tonic-gate 						footer = 't';
393*7c478bd9Sstevel@tonic-gate 						break;
394*7c478bd9Sstevel@tonic-gate 					case 'a':
395*7c478bd9Sstevel@tonic-gate 						footer = 'a';
396*7c478bd9Sstevel@tonic-gate 						break;
397*7c478bd9Sstevel@tonic-gate 					case 'p':
398*7c478bd9Sstevel@tonic-gate 						s = argv[j];
399*7c478bd9Sstevel@tonic-gate 						q = 3;
400*7c478bd9Sstevel@tonic-gate 						r = 0;
401*7c478bd9Sstevel@tonic-gate 						while (s[q] != '\0') {
402*7c478bd9Sstevel@tonic-gate 							pat[r] = s[q];
403*7c478bd9Sstevel@tonic-gate 							r++;
404*7c478bd9Sstevel@tonic-gate 							q++;
405*7c478bd9Sstevel@tonic-gate 						}
406*7c478bd9Sstevel@tonic-gate 						pat[r] = '\0';
407*7c478bd9Sstevel@tonic-gate 						footer = 'f';
408*7c478bd9Sstevel@tonic-gate 					fexpbuf =
409*7c478bd9Sstevel@tonic-gate 					    compile(pat, (char *)0, (char *)0);
410*7c478bd9Sstevel@tonic-gate 					if (regerrno)
411*7c478bd9Sstevel@tonic-gate 						regerr(regerrno);
412*7c478bd9Sstevel@tonic-gate 						break;
413*7c478bd9Sstevel@tonic-gate 					case '\0':
414*7c478bd9Sstevel@tonic-gate 						footer = 'n';
415*7c478bd9Sstevel@tonic-gate 						break;
416*7c478bd9Sstevel@tonic-gate 					default:
417*7c478bd9Sstevel@tonic-gate 						optmsg(argv[j]);
418*7c478bd9Sstevel@tonic-gate 				}
419*7c478bd9Sstevel@tonic-gate 				break;
420*7c478bd9Sstevel@tonic-gate 			case 'p':
421*7c478bd9Sstevel@tonic-gate 				if (argv[j][i+2] == '\0')
422*7c478bd9Sstevel@tonic-gate 				cntck = 'y';
423*7c478bd9Sstevel@tonic-gate 				else
424*7c478bd9Sstevel@tonic-gate 				{
425*7c478bd9Sstevel@tonic-gate 				optmsg(argv[j]);
426*7c478bd9Sstevel@tonic-gate 				}
427*7c478bd9Sstevel@tonic-gate 				break;
428*7c478bd9Sstevel@tonic-gate 			case 'v':
429*7c478bd9Sstevel@tonic-gate 				if (argv[j][i+2] == '\0')
430*7c478bd9Sstevel@tonic-gate 				startcnt = 1;
431*7c478bd9Sstevel@tonic-gate 				else
432*7c478bd9Sstevel@tonic-gate 				startcnt = convert(argv[j]);
433*7c478bd9Sstevel@tonic-gate 				break;
434*7c478bd9Sstevel@tonic-gate 			case 'i':
435*7c478bd9Sstevel@tonic-gate 				if (argv[j][i+2] == '\0')
436*7c478bd9Sstevel@tonic-gate 				increment = 1;
437*7c478bd9Sstevel@tonic-gate 				else
438*7c478bd9Sstevel@tonic-gate 				increment = convert(argv[j]);
439*7c478bd9Sstevel@tonic-gate 				break;
440*7c478bd9Sstevel@tonic-gate 			case 'w':
441*7c478bd9Sstevel@tonic-gate 				if (argv[j][i+2] == '\0')
442*7c478bd9Sstevel@tonic-gate 				width = 6;
443*7c478bd9Sstevel@tonic-gate 				else
444*7c478bd9Sstevel@tonic-gate 				width = convert(argv[j]);
445*7c478bd9Sstevel@tonic-gate 				break;
446*7c478bd9Sstevel@tonic-gate 			case 'l':
447*7c478bd9Sstevel@tonic-gate 				if (argv[j][i+2] == '\0')
448*7c478bd9Sstevel@tonic-gate 				blank = 1;
449*7c478bd9Sstevel@tonic-gate 				else
450*7c478bd9Sstevel@tonic-gate 				blank = convert(argv[j]);
451*7c478bd9Sstevel@tonic-gate 				break;
452*7c478bd9Sstevel@tonic-gate 			case 'n':
453*7c478bd9Sstevel@tonic-gate 				switch (argv[j][i+2]) {
454*7c478bd9Sstevel@tonic-gate 					case 'l':
455*7c478bd9Sstevel@tonic-gate 						if (argv[j][i+3] == 'n')
456*7c478bd9Sstevel@tonic-gate 						format = 'l';
457*7c478bd9Sstevel@tonic-gate 						else
458*7c478bd9Sstevel@tonic-gate 				{
459*7c478bd9Sstevel@tonic-gate 				optmsg(argv[j]);
460*7c478bd9Sstevel@tonic-gate 				}
461*7c478bd9Sstevel@tonic-gate 						break;
462*7c478bd9Sstevel@tonic-gate 					case 'r':
463*7c478bd9Sstevel@tonic-gate 						if ((argv[j][i+3] == 'n') ||
464*7c478bd9Sstevel@tonic-gate 						    (argv[j][i+3] == 'z'))
465*7c478bd9Sstevel@tonic-gate 						format = argv[j][i+3];
466*7c478bd9Sstevel@tonic-gate 						else
467*7c478bd9Sstevel@tonic-gate 				{
468*7c478bd9Sstevel@tonic-gate 				optmsg(argv[j]);
469*7c478bd9Sstevel@tonic-gate 				}
470*7c478bd9Sstevel@tonic-gate 						break;
471*7c478bd9Sstevel@tonic-gate 					case '\0':
472*7c478bd9Sstevel@tonic-gate 						format = 'n';
473*7c478bd9Sstevel@tonic-gate 						break;
474*7c478bd9Sstevel@tonic-gate 					default:
475*7c478bd9Sstevel@tonic-gate 				optmsg(argv[j]);
476*7c478bd9Sstevel@tonic-gate 					break;
477*7c478bd9Sstevel@tonic-gate 				}
478*7c478bd9Sstevel@tonic-gate 				break;
479*7c478bd9Sstevel@tonic-gate 			case 's':
480*7c478bd9Sstevel@tonic-gate 				if (argv[j][i + 2] != '\0') {
481*7c478bd9Sstevel@tonic-gate 					s = argv[j];
482*7c478bd9Sstevel@tonic-gate 					q = 2;
483*7c478bd9Sstevel@tonic-gate 					r = 0;
484*7c478bd9Sstevel@tonic-gate 					while (s[q] != '\0') {
485*7c478bd9Sstevel@tonic-gate 						sep[r] = s[q];
486*7c478bd9Sstevel@tonic-gate 						r++;
487*7c478bd9Sstevel@tonic-gate 						q++;
488*7c478bd9Sstevel@tonic-gate 					}
489*7c478bd9Sstevel@tonic-gate 					sep[r] = '\0';
490*7c478bd9Sstevel@tonic-gate 				}
491*7c478bd9Sstevel@tonic-gate 				/* else default sep is tab (set above) */
492*7c478bd9Sstevel@tonic-gate 				break;
493*7c478bd9Sstevel@tonic-gate 			case 'd':
494*7c478bd9Sstevel@tonic-gate 				tempchr = argv[j][i+2];
495*7c478bd9Sstevel@tonic-gate 				if (tempchr == '\0')break;
496*7c478bd9Sstevel@tonic-gate 				delim1 = tempchr;
497*7c478bd9Sstevel@tonic-gate 
498*7c478bd9Sstevel@tonic-gate 				tempchr = argv[j][i+3];
499*7c478bd9Sstevel@tonic-gate 				if (tempchr == '\0')break;
500*7c478bd9Sstevel@tonic-gate 				delim2 = tempchr;
501*7c478bd9Sstevel@tonic-gate 				if (argv[j][i+4] != '\0')optmsg(argv[j]);
502*7c478bd9Sstevel@tonic-gate 				break;
503*7c478bd9Sstevel@tonic-gate 			case '-':
504*7c478bd9Sstevel@tonic-gate 				if (argv[j][i + 2] == '\0') {
505*7c478bd9Sstevel@tonic-gate 					option_end = 1;
506*7c478bd9Sstevel@tonic-gate 					break;
507*7c478bd9Sstevel@tonic-gate 				}
508*7c478bd9Sstevel@tonic-gate 			default:
509*7c478bd9Sstevel@tonic-gate 				optmsg(argv[j]);
510*7c478bd9Sstevel@tonic-gate 			}
511*7c478bd9Sstevel@tonic-gate 		} else if ((iptr = fopen(argv[j], "r")) == NULL)  {
512*7c478bd9Sstevel@tonic-gate 			/* end of options, filename starting with '-' */
513*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "nl: %s: ", argv[j]);
514*7c478bd9Sstevel@tonic-gate 			perror("");
515*7c478bd9Sstevel@tonic-gate 			return (1);
516*7c478bd9Sstevel@tonic-gate 		}
517*7c478bd9Sstevel@tonic-gate 	} else if ((iptr = fopen(argv[j], "r")) == NULL)  {
518*7c478bd9Sstevel@tonic-gate 		/* filename starting with char other than '-' */
519*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "nl: %s: ", argv[j]);
520*7c478bd9Sstevel@tonic-gate 		perror("");
521*7c478bd9Sstevel@tonic-gate 		return (1);
522*7c478bd9Sstevel@tonic-gate 	}
523*7c478bd9Sstevel@tonic-gate } /* closing brace of for loop */
524*7c478bd9Sstevel@tonic-gate /* end Solaris version of argument parsing */
525*7c478bd9Sstevel@tonic-gate #endif
526*7c478bd9Sstevel@tonic-gate 
527*7c478bd9Sstevel@tonic-gate 	/* ON FIRST PASS ONLY, SET LINE COUNTER (cnt) = startcnt & */
528*7c478bd9Sstevel@tonic-gate 	/* SET DEFAULT BODY TYPE TO NUMBER ALL LINES.	*/
529*7c478bd9Sstevel@tonic-gate 	if (pass1) {
530*7c478bd9Sstevel@tonic-gate 		cnt = startcnt;
531*7c478bd9Sstevel@tonic-gate 		type = body;
532*7c478bd9Sstevel@tonic-gate 		last = 'b';
533*7c478bd9Sstevel@tonic-gate 		pass1 = 0;
534*7c478bd9Sstevel@tonic-gate 	}
535*7c478bd9Sstevel@tonic-gate 
536*7c478bd9Sstevel@tonic-gate /*
537*7c478bd9Sstevel@tonic-gate  *		DO WHILE THERE IS INPUT
538*7c478bd9Sstevel@tonic-gate  *		CHECK TO SEE IF LINE IS NUMBERED,
539*7c478bd9Sstevel@tonic-gate  *		IF SO, CALCULATE NUM, PRINT NUM,
540*7c478bd9Sstevel@tonic-gate  *		THEN OUTPUT SEPERATOR CHAR AND LINE
541*7c478bd9Sstevel@tonic-gate  */
542*7c478bd9Sstevel@tonic-gate 
543*7c478bd9Sstevel@tonic-gate 	while ((p = fgets(line, sizeof (line), iptr)) != NULL) {
544*7c478bd9Sstevel@tonic-gate 	if (p[0] == delim1 && p[1] == delim2) {
545*7c478bd9Sstevel@tonic-gate 		if (p[2] == delim1 &&
546*7c478bd9Sstevel@tonic-gate 		    p[3] == delim2 &&
547*7c478bd9Sstevel@tonic-gate 		    p[4] == delim1 &&
548*7c478bd9Sstevel@tonic-gate 		    p[5] == delim2 &&
549*7c478bd9Sstevel@tonic-gate 		    p[6] == '\n') {
550*7c478bd9Sstevel@tonic-gate 			if (cntck != 'y')
551*7c478bd9Sstevel@tonic-gate 				cnt = startcnt;
552*7c478bd9Sstevel@tonic-gate 			type = header;
553*7c478bd9Sstevel@tonic-gate 			last = 'h';
554*7c478bd9Sstevel@tonic-gate 			swtch = 'y';
555*7c478bd9Sstevel@tonic-gate 		} else {
556*7c478bd9Sstevel@tonic-gate 			if (p[2] == delim1 && p[3] == delim2 && p[4] == '\n') {
557*7c478bd9Sstevel@tonic-gate 				if (cntck != 'y' && last != 'h')
558*7c478bd9Sstevel@tonic-gate 					cnt = startcnt;
559*7c478bd9Sstevel@tonic-gate 				type = body;
560*7c478bd9Sstevel@tonic-gate 				last = 'b';
561*7c478bd9Sstevel@tonic-gate 				swtch = 'y';
562*7c478bd9Sstevel@tonic-gate 			} else {
563*7c478bd9Sstevel@tonic-gate 				if (p[0] == delim1 && p[1] == delim2 &&
564*7c478bd9Sstevel@tonic-gate 							p[2] == '\n') {
565*7c478bd9Sstevel@tonic-gate 					if (cntck != 'y' && last == 'f')
566*7c478bd9Sstevel@tonic-gate 						cnt = startcnt;
567*7c478bd9Sstevel@tonic-gate 					type = footer;
568*7c478bd9Sstevel@tonic-gate 					last = 'f';
569*7c478bd9Sstevel@tonic-gate 					swtch = 'y';
570*7c478bd9Sstevel@tonic-gate 				}
571*7c478bd9Sstevel@tonic-gate 			}
572*7c478bd9Sstevel@tonic-gate 		}
573*7c478bd9Sstevel@tonic-gate 	}
574*7c478bd9Sstevel@tonic-gate 	if (p[0] != '\n') {
575*7c478bd9Sstevel@tonic-gate 		lnt = strlen(p);
576*7c478bd9Sstevel@tonic-gate 		if (p[lnt-1] == '\n')
577*7c478bd9Sstevel@tonic-gate 			p[lnt-1] = NULL;
578*7c478bd9Sstevel@tonic-gate 	}
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate 	if (swtch == 'y') {
581*7c478bd9Sstevel@tonic-gate 		swtch = 'n';
582*7c478bd9Sstevel@tonic-gate 		(void) fprintf(optr, "\n");
583*7c478bd9Sstevel@tonic-gate 	} else {
584*7c478bd9Sstevel@tonic-gate 		switch (type) {
585*7c478bd9Sstevel@tonic-gate 		case 'n':
586*7c478bd9Sstevel@tonic-gate 			npad(width, sep);
587*7c478bd9Sstevel@tonic-gate 			break;
588*7c478bd9Sstevel@tonic-gate 		case 't':
589*7c478bd9Sstevel@tonic-gate 			/*
590*7c478bd9Sstevel@tonic-gate 			 * XPG4: The wording of Spec 1170 is misleading;
591*7c478bd9Sstevel@tonic-gate 			 * the official interpretation is to number all
592*7c478bd9Sstevel@tonic-gate 			 * non-empty lines, ie: the Solaris code has not
593*7c478bd9Sstevel@tonic-gate 			 * been changed.
594*7c478bd9Sstevel@tonic-gate 			 */
595*7c478bd9Sstevel@tonic-gate 			if (p[0] != '\n') {
596*7c478bd9Sstevel@tonic-gate 				pnum(cnt, sep);
597*7c478bd9Sstevel@tonic-gate 				cnt += increment;
598*7c478bd9Sstevel@tonic-gate 			} else {
599*7c478bd9Sstevel@tonic-gate 				npad(width, sep);
600*7c478bd9Sstevel@tonic-gate 			}
601*7c478bd9Sstevel@tonic-gate 			break;
602*7c478bd9Sstevel@tonic-gate 		case 'a':
603*7c478bd9Sstevel@tonic-gate 			if (p[0] == '\n') {
604*7c478bd9Sstevel@tonic-gate 				blankctr++;
605*7c478bd9Sstevel@tonic-gate 				if (blank == blankctr) {
606*7c478bd9Sstevel@tonic-gate 					blankctr = 0;
607*7c478bd9Sstevel@tonic-gate 					pnum(cnt, sep);
608*7c478bd9Sstevel@tonic-gate 					cnt += increment;
609*7c478bd9Sstevel@tonic-gate 				} else
610*7c478bd9Sstevel@tonic-gate 					npad(width, sep);
611*7c478bd9Sstevel@tonic-gate 			} else {
612*7c478bd9Sstevel@tonic-gate 				blankctr = 0;
613*7c478bd9Sstevel@tonic-gate 				pnum(cnt, sep);
614*7c478bd9Sstevel@tonic-gate 				cnt += increment;
615*7c478bd9Sstevel@tonic-gate 			}
616*7c478bd9Sstevel@tonic-gate 			break;
617*7c478bd9Sstevel@tonic-gate 		case 'b':
618*7c478bd9Sstevel@tonic-gate 			if (step(p, bexpbuf)) {
619*7c478bd9Sstevel@tonic-gate 				pnum(cnt, sep);
620*7c478bd9Sstevel@tonic-gate 				cnt += increment;
621*7c478bd9Sstevel@tonic-gate 			} else {
622*7c478bd9Sstevel@tonic-gate 				npad(width, sep);
623*7c478bd9Sstevel@tonic-gate 			}
624*7c478bd9Sstevel@tonic-gate 			break;
625*7c478bd9Sstevel@tonic-gate 		case 'h':
626*7c478bd9Sstevel@tonic-gate 			if (step(p, hexpbuf)) {
627*7c478bd9Sstevel@tonic-gate 				pnum(cnt, sep);
628*7c478bd9Sstevel@tonic-gate 				cnt += increment;
629*7c478bd9Sstevel@tonic-gate 			} else {
630*7c478bd9Sstevel@tonic-gate 				npad(width, sep);
631*7c478bd9Sstevel@tonic-gate 			}
632*7c478bd9Sstevel@tonic-gate 			break;
633*7c478bd9Sstevel@tonic-gate 		case 'f':
634*7c478bd9Sstevel@tonic-gate 			if (step(p, fexpbuf)) {
635*7c478bd9Sstevel@tonic-gate 				pnum(cnt, sep);
636*7c478bd9Sstevel@tonic-gate 				cnt += increment;
637*7c478bd9Sstevel@tonic-gate 			} else {
638*7c478bd9Sstevel@tonic-gate 				npad(width, sep);
639*7c478bd9Sstevel@tonic-gate 			}
640*7c478bd9Sstevel@tonic-gate 			break;
641*7c478bd9Sstevel@tonic-gate 		}
642*7c478bd9Sstevel@tonic-gate 		if (p[0] != '\n')
643*7c478bd9Sstevel@tonic-gate 			p[lnt-1] = '\n';
644*7c478bd9Sstevel@tonic-gate 		(void) fprintf(optr, "%s", line);
645*7c478bd9Sstevel@tonic-gate 
646*7c478bd9Sstevel@tonic-gate 	}	/* Closing brace of "else" */
647*7c478bd9Sstevel@tonic-gate 	}	/* Closing brace of "while". */
648*7c478bd9Sstevel@tonic-gate 	(void) fclose(iptr);
649*7c478bd9Sstevel@tonic-gate 
650*7c478bd9Sstevel@tonic-gate 	return (0);
651*7c478bd9Sstevel@tonic-gate }
652*7c478bd9Sstevel@tonic-gate 
653*7c478bd9Sstevel@tonic-gate /*		REGEXP ERR ROUTINE		*/
654*7c478bd9Sstevel@tonic-gate 
655*7c478bd9Sstevel@tonic-gate static void
656*7c478bd9Sstevel@tonic-gate regerr(c)
657*7c478bd9Sstevel@tonic-gate int c;
658*7c478bd9Sstevel@tonic-gate {
659*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
660*7c478bd9Sstevel@tonic-gate 		"nl: invalid regular expression: error code %d\n"), c);
661*7c478bd9Sstevel@tonic-gate 	exit(1);
662*7c478bd9Sstevel@tonic-gate }
663*7c478bd9Sstevel@tonic-gate 
664*7c478bd9Sstevel@tonic-gate /*		CALCULATE NUMBER ROUTINE	*/
665*7c478bd9Sstevel@tonic-gate 
666*7c478bd9Sstevel@tonic-gate static void
667*7c478bd9Sstevel@tonic-gate pnum(n, sep)
668*7c478bd9Sstevel@tonic-gate int	n;
669*7c478bd9Sstevel@tonic-gate char *	sep;
670*7c478bd9Sstevel@tonic-gate {
671*7c478bd9Sstevel@tonic-gate 	register int	i;
672*7c478bd9Sstevel@tonic-gate 
673*7c478bd9Sstevel@tonic-gate 	if (format == 'z') {
674*7c478bd9Sstevel@tonic-gate 		pad = '0';
675*7c478bd9Sstevel@tonic-gate 	}
676*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < width; i++)
677*7c478bd9Sstevel@tonic-gate 		nbuf[i] = pad;
678*7c478bd9Sstevel@tonic-gate 	num(n, width - 1);
679*7c478bd9Sstevel@tonic-gate 	if (format == 'l') {
680*7c478bd9Sstevel@tonic-gate 		while (nbuf[0] == ' ') {
681*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < width; i++)
682*7c478bd9Sstevel@tonic-gate 				nbuf[i] = nbuf[i+1];
683*7c478bd9Sstevel@tonic-gate 			nbuf[width-1] = ' ';
684*7c478bd9Sstevel@tonic-gate 		}
685*7c478bd9Sstevel@tonic-gate 	}
686*7c478bd9Sstevel@tonic-gate 	(void) printf("%s%s", nbuf, sep);
687*7c478bd9Sstevel@tonic-gate }
688*7c478bd9Sstevel@tonic-gate 
689*7c478bd9Sstevel@tonic-gate /*		IF NUM > 10, THEN USE THIS CALCULATE ROUTINE		*/
690*7c478bd9Sstevel@tonic-gate 
691*7c478bd9Sstevel@tonic-gate static void
692*7c478bd9Sstevel@tonic-gate num(v, p)
693*7c478bd9Sstevel@tonic-gate int v, p;
694*7c478bd9Sstevel@tonic-gate {
695*7c478bd9Sstevel@tonic-gate 	if (v < 10)
696*7c478bd9Sstevel@tonic-gate 		nbuf[p] = v + '0';
697*7c478bd9Sstevel@tonic-gate 	else {
698*7c478bd9Sstevel@tonic-gate 		nbuf[p] = (v % 10) + '0';
699*7c478bd9Sstevel@tonic-gate 		if (p > 0)
700*7c478bd9Sstevel@tonic-gate 			num(v / 10, p - 1);
701*7c478bd9Sstevel@tonic-gate 	}
702*7c478bd9Sstevel@tonic-gate }
703*7c478bd9Sstevel@tonic-gate 
704*7c478bd9Sstevel@tonic-gate /*		CONVERT ARG STRINGS TO STRING ARRAYS	*/
705*7c478bd9Sstevel@tonic-gate 
706*7c478bd9Sstevel@tonic-gate #ifdef XPG4
707*7c478bd9Sstevel@tonic-gate static int
708*7c478bd9Sstevel@tonic-gate convert(c, option_arg)
709*7c478bd9Sstevel@tonic-gate int c;
710*7c478bd9Sstevel@tonic-gate char *option_arg;
711*7c478bd9Sstevel@tonic-gate {
712*7c478bd9Sstevel@tonic-gate 	s = option_arg;
713*7c478bd9Sstevel@tonic-gate 	q = r = 0;
714*7c478bd9Sstevel@tonic-gate 	while (s[q] != '\0') {
715*7c478bd9Sstevel@tonic-gate 		if (s[q] >= '0' && s[q] <= '9') {
716*7c478bd9Sstevel@tonic-gate 			s1[r] = s[q];
717*7c478bd9Sstevel@tonic-gate 			r++;
718*7c478bd9Sstevel@tonic-gate 			q++;
719*7c478bd9Sstevel@tonic-gate 		} else
720*7c478bd9Sstevel@tonic-gate 			optmsg(c, option_arg);
721*7c478bd9Sstevel@tonic-gate 	}
722*7c478bd9Sstevel@tonic-gate 	s1[r] = '\0';
723*7c478bd9Sstevel@tonic-gate 	k = atoi(s1);
724*7c478bd9Sstevel@tonic-gate 	return (k);
725*7c478bd9Sstevel@tonic-gate }
726*7c478bd9Sstevel@tonic-gate #else
727*7c478bd9Sstevel@tonic-gate /* Solaris version */
728*7c478bd9Sstevel@tonic-gate static int
729*7c478bd9Sstevel@tonic-gate convert(argv)
730*7c478bd9Sstevel@tonic-gate char *argv;
731*7c478bd9Sstevel@tonic-gate {
732*7c478bd9Sstevel@tonic-gate 	s = (char *)argv;
733*7c478bd9Sstevel@tonic-gate 	q = 2;
734*7c478bd9Sstevel@tonic-gate 	r = 0;
735*7c478bd9Sstevel@tonic-gate 	while (s[q] != '\0') {
736*7c478bd9Sstevel@tonic-gate 		if (s[q] >= '0' && s[q] <= '9')
737*7c478bd9Sstevel@tonic-gate 		{
738*7c478bd9Sstevel@tonic-gate 		s1[r] = s[q];
739*7c478bd9Sstevel@tonic-gate 		r++;
740*7c478bd9Sstevel@tonic-gate 		q++;
741*7c478bd9Sstevel@tonic-gate 		}
742*7c478bd9Sstevel@tonic-gate 		else
743*7c478bd9Sstevel@tonic-gate 				{
744*7c478bd9Sstevel@tonic-gate 				optmsg(argv);
745*7c478bd9Sstevel@tonic-gate 				}
746*7c478bd9Sstevel@tonic-gate 	}
747*7c478bd9Sstevel@tonic-gate 	s1[r] = '\0';
748*7c478bd9Sstevel@tonic-gate 	k = atoi(s1);
749*7c478bd9Sstevel@tonic-gate 	return (k);
750*7c478bd9Sstevel@tonic-gate }
751*7c478bd9Sstevel@tonic-gate #endif
752*7c478bd9Sstevel@tonic-gate 
753*7c478bd9Sstevel@tonic-gate /*		CALCULATE NUM/TEXT SEPRATOR		*/
754*7c478bd9Sstevel@tonic-gate 
755*7c478bd9Sstevel@tonic-gate static void
756*7c478bd9Sstevel@tonic-gate npad(width, sep)
757*7c478bd9Sstevel@tonic-gate 	int	width;
758*7c478bd9Sstevel@tonic-gate 	char *	sep;
759*7c478bd9Sstevel@tonic-gate {
760*7c478bd9Sstevel@tonic-gate 	register int i;
761*7c478bd9Sstevel@tonic-gate 
762*7c478bd9Sstevel@tonic-gate 	pad = ' ';
763*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < width; i++)
764*7c478bd9Sstevel@tonic-gate 		nbuf[i] = pad;
765*7c478bd9Sstevel@tonic-gate 	(void) printf("%s", nbuf);
766*7c478bd9Sstevel@tonic-gate 
767*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < (int) strlen(sep); i++)
768*7c478bd9Sstevel@tonic-gate 		(void) printf(" ");
769*7c478bd9Sstevel@tonic-gate }
770*7c478bd9Sstevel@tonic-gate 
771*7c478bd9Sstevel@tonic-gate #ifdef XPG4
772*7c478bd9Sstevel@tonic-gate static void
773*7c478bd9Sstevel@tonic-gate optmsg(option, option_arg)
774*7c478bd9Sstevel@tonic-gate int option;
775*7c478bd9Sstevel@tonic-gate char *option_arg;
776*7c478bd9Sstevel@tonic-gate {
777*7c478bd9Sstevel@tonic-gate 	if (option_arg != (char *)NULL) {
778*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
779*7c478bd9Sstevel@tonic-gate 			"nl: invalid option (-%c %s)\n"), option, option_arg);
780*7c478bd9Sstevel@tonic-gate 	}
781*7c478bd9Sstevel@tonic-gate 	/* else getopt() will print illegal option message */
782*7c478bd9Sstevel@tonic-gate 	usage();
783*7c478bd9Sstevel@tonic-gate }
784*7c478bd9Sstevel@tonic-gate #else
785*7c478bd9Sstevel@tonic-gate /* Solaris version */
786*7c478bd9Sstevel@tonic-gate static void
787*7c478bd9Sstevel@tonic-gate optmsg(option)
788*7c478bd9Sstevel@tonic-gate char *option;
789*7c478bd9Sstevel@tonic-gate {
790*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
791*7c478bd9Sstevel@tonic-gate 		"nl: invalid option (%s)\n"), option);
792*7c478bd9Sstevel@tonic-gate 	usage();
793*7c478bd9Sstevel@tonic-gate }
794*7c478bd9Sstevel@tonic-gate #endif
795*7c478bd9Sstevel@tonic-gate 
796*7c478bd9Sstevel@tonic-gate void
797*7c478bd9Sstevel@tonic-gate usage()
798*7c478bd9Sstevel@tonic-gate {
799*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(USAGE));
800*7c478bd9Sstevel@tonic-gate 	exit(1);
801*7c478bd9Sstevel@tonic-gate }
802