summaryrefslogtreecommitdiff
path: root/usr/src/cmd/nl/nl.c
blob: fd6b857d03dc0ab36cacc04bcf0c79cad30087cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 1995 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/


#include <locale.h>
#include <regexpr.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <wchar.h>
#include <wctype.h>
#include <limits.h>

#define	EXPSIZ		512

#ifdef XPG4
#define	USAGE "usage: nl [-p] [-b type] [-d delim] [ -f type] " \
	"[-h type] [-i incr] [-l num] [-n format]\n" \
	"[-s sep] [-v startnum] [-w width] [file]\n"
#else
#define	USAGE "usage: nl [-p] [-btype] [-ddelim] [ -ftype] " \
	"[-htype] [-iincr] [-lnum] [-nformat] [-ssep] " \
	"[-vstartnum] [-wwidth] [file]\n"
#endif

#ifdef u370
	int nbra, sed;	/* u370 - not used in nl.c, but extern in regexp.h */
#endif
static int width = 6;	/* Declare default width of number */
static char nbuf[100];	/* Declare bufsize used in convert/pad/cnt routines */
static char *bexpbuf;	/* Declare the regexp buf */
static char *hexpbuf;	/* Declare the regexp buf */
static char *fexpbuf;	/* Declare the regexp buf */
static char delim1 = '\\';
static char delim2 = ':';	/* Default delimiters. */
static char pad = ' ';	/* Declare the default pad for numbers */
static char *s;	/* Declare the temp array for args */
static char s1[EXPSIZ];	/* Declare the conversion array */
static char format = 'n'; /* Declare the format of numbers to be rt just */
static int q = 2;	/* Initialize arg pointer to drop 1st 2 chars */
static int k;	/* Declare var for return of convert */
static int r;	/* Declare the arg array ptr for string args */

#ifdef XPG4
static int convert(int, char *);
#else
static int convert(char *);
#endif
static void num(int, int);
static void npad(int, char *);
#ifdef XPG4
static void optmsg(int, char *);
#else
static void optmsg(char *);
#endif
static void pnum(int, char *);
static void regerr(int);
static void usage();

extern char *optarg;	/* getopt support */
extern int optind;

int
main(int argc, char *argv[])
{
	register int j;
	register int i = 0;
	register char *p;
	register char header = 'n';
	register char body = 't';
	register char footer = 'n';
	char line[LINE_MAX];
	char tempchr;	/* Temporary holding variable. */
	char swtch = 'n';
	char cntck = 'n';
	char type;
	int cnt;	/* line counter */
	int pass1 = 1;	/* First pass flag. 1=pass1, 0=additional passes. */
	char sep[EXPSIZ];
	char pat[EXPSIZ];
	int startcnt = 1;
	int increment = 1;
	int blank = 1;
	int blankctr = 0;
	int c;
	int lnt;
	char last;
	FILE *iptr = stdin;
	FILE *optr = stdout;
#ifndef XPG4
	int option_end = 0;
#endif

	sep[0] = '\t';
	sep[1] = '\0';

	(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
#define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
#endif
	(void) textdomain(TEXT_DOMAIN);

#ifdef XPG4
	/*
	 * XPG4:  Allow either a space or no space between the
	 *	  options and their required arguments.
	 */

	while (argc > 0) {
		while ((c = getopt(argc, argv,
		    "pb:d:f:h:i:l:n:s:v:w:")) != EOF) {

			switch (c) {
			case 'h':
				switch (*optarg) {
				case 'n':
					header = 'n';
					break;
				case 't':
					header = 't';
					break;
				case 'a':
					header = 'a';
					break;
				case 'p':
					(void) strcpy(pat, optarg+1);
					header = 'h';
					hexpbuf =
					    compile(pat, NULL,  NULL);
					if (regerrno)
						regerr(regerrno);
					break;
				case '\0':
					header = 'n';
					break;
				default:
					optmsg(c, optarg);
				}
				break;
			case 'b':
				switch (*optarg) {
				case 't':
					body = 't';
					break;
				case 'a':
					body = 'a';
					break;
				case 'n':
					body = 'n';
					break;
				case 'p':
					(void) strcpy(pat, optarg+1);
					body = 'b';
					bexpbuf =
					    compile(pat, NULL, NULL);
					if (regerrno)
						regerr(regerrno);
					break;
				case '\0':
					body = 't';
					break;
				default:
					optmsg(c, optarg);
				}
				break;
			case 'f':
				switch (*optarg) {
				case 'n':
					footer = 'n';
					break;
				case 't':
					footer = 't';
					break;
				case 'a':
					footer = 'a';
					break;
				case 'p':
					(void) strcpy(pat, optarg+1);
					footer = 'f';
					fexpbuf =
					    compile(pat, NULL, NULL);
					if (regerrno)
						regerr(regerrno);
					break;
				case '\0':
					footer = 'n';
					break;
				default:
					optmsg(c, optarg);
				}
				break;
			case 'p':
				if (optarg == (char *)NULL)
					cntck = 'y';
				else
					optmsg(c, optarg);
				break;
			case 'v':
				if (*optarg == '\0')
					startcnt = 1;
				else
					startcnt = convert(c, optarg);
				break;
			case 'i':
				if (*optarg == '\0')
					increment = 1;
				else
					increment = convert(c, optarg);
				break;
			case 'w':
				if (*optarg == '\0')
					width = 6;
				else
					width = convert(c, optarg);
				break;
			case 'l':
				if (*optarg == '\0')
					blank = 1;
				else
					blank = convert(c, optarg);
				break;
			case 'n':
				switch (*optarg) {
				case 'l':
					if (*(optarg+1) == 'n')
						format = 'l';
					else
						optmsg(c, optarg);
					break;
				case 'r':
					if ((*(optarg+1) == 'n') ||
					    (*(optarg+1) == 'z'))
						format = *(optarg+1);
					else
						optmsg(c, optarg);
					break;
				case '\0':
					format = 'n';
					break;
				default:
					optmsg(c, optarg);
					break;
				}
				break;
			case 's':
				(void) strcpy(sep, optarg);
				break;
			case 'd':
				delim1 = *optarg;

				if (*(optarg+1) == '\0')
					break;
				delim2 = *(optarg+1);
				if (*(optarg+2) != '\0')
					optmsg(c, optarg);
				break;
			default:
				optmsg(c, optarg);
			} /* end switch char returned from getopt() */
		} /* end while getopt */

		argv += optind;
		argc -= optind;
		optind = 0;

		if (argc > 0) {
			if ((iptr = fopen(argv[0], "r")) == NULL)  {
				(void) fprintf(stderr, "nl: %s: ", argv[0]);
				perror("");
				return (1);
			}
			++argv;
			--argc;
		}
	} /* end while argc > 0 */
/* end XPG4 version of argument parsing */
#else
/*
 * Solaris:  For backward compatibility, do not allow a space between the
 *	     options and their arguments.  Option arguments are optional,
 *	     not required as in the XPG4 version of nl.
 */
for (j = 1; j < argc; j++) {
	if (argv[j][i] == '-' && (c = argv[j][i + 1])) {
		if (!option_end) {
			switch (c) {
			case 'h':
				switch (argv[j][i + 2]) {
					case 'n':
						header = 'n';
						break;
					case 't':
						header = 't';
						break;
					case 'a':
						header = 'a';
						break;
					case 'p':
						s = argv[j];
						q = 3;
						r = 0;
						while (s[q] != '\0') {
							pat[r] = s[q];
							r++;
							q++;
						}
						pat[r] = '\0';
						header = 'h';
						hexpbuf =
						    compile(pat, NULL, NULL);
						if (regerrno)
							regerr(regerrno);
						break;
					case '\0':
						header = 'n';
						break;
					default:
						optmsg(argv[j]);
				}
				break;
			case 'b':
				switch (argv[j][i + 2]) {
					case 't':
						body = 't';
						break;
					case 'a':
						body = 'a';
						break;
					case 'n':
						body = 'n';
						break;
					case 'p':
						s = argv[j];
						q = 3;
						r = 0;
						while (s[q] != '\0') {
							pat[r] = s[q];
							r++;
							q++;
						}
						pat[r] = '\0';
						body = 'b';
						bexpbuf =
						    compile(pat, NULL, NULL);
						if (regerrno)
							regerr(regerrno);
						break;
					case '\0':
						body = 't';
						break;
					default:
						optmsg(argv[j]);
				}
				break;
			case 'f':
				switch (argv[j][i + 2]) {
					case 'n':
						footer = 'n';
						break;
					case 't':
						footer = 't';
						break;
					case 'a':
						footer = 'a';
						break;
					case 'p':
						s = argv[j];
						q = 3;
						r = 0;
						while (s[q] != '\0') {
							pat[r] = s[q];
							r++;
							q++;
						}
						pat[r] = '\0';
						footer = 'f';
						fexpbuf =
						    compile(pat, NULL, NULL);
						if (regerrno)
							regerr(regerrno);
						break;
					case '\0':
						footer = 'n';
						break;
					default:
						optmsg(argv[j]);
				}
				break;
			case 'p':
				if (argv[j][i+2] == '\0')
				cntck = 'y';
				else
				{
				optmsg(argv[j]);
				}
				break;
			case 'v':
				if (argv[j][i+2] == '\0')
				startcnt = 1;
				else
				startcnt = convert(argv[j]);
				break;
			case 'i':
				if (argv[j][i+2] == '\0')
				increment = 1;
				else
				increment = convert(argv[j]);
				break;
			case 'w':
				if (argv[j][i+2] == '\0')
				width = 6;
				else
				width = convert(argv[j]);
				break;
			case 'l':
				if (argv[j][i+2] == '\0')
				blank = 1;
				else
				blank = convert(argv[j]);
				break;
			case 'n':
				switch (argv[j][i+2]) {
					case 'l':
						if (argv[j][i+3] == 'n')
						format = 'l';
						else
				{
				optmsg(argv[j]);
				}
						break;
					case 'r':
						if ((argv[j][i+3] == 'n') ||
						    (argv[j][i+3] == 'z'))
						format = argv[j][i+3];
						else
				{
				optmsg(argv[j]);
				}
						break;
					case '\0':
						format = 'n';
						break;
					default:
				optmsg(argv[j]);
					break;
				}
				break;
			case 's':
				if (argv[j][i + 2] != '\0') {
					s = argv[j];
					q = 2;
					r = 0;
					while (s[q] != '\0') {
						sep[r] = s[q];
						r++;
						q++;
					}
					sep[r] = '\0';
				}
				/* else default sep is tab (set above) */
				break;
			case 'd':
				tempchr = argv[j][i+2];
				if (tempchr == '\0')break;
				delim1 = tempchr;

				tempchr = argv[j][i+3];
				if (tempchr == '\0')break;
				delim2 = tempchr;
				if (argv[j][i+4] != '\0')optmsg(argv[j]);
				break;
			case '-':
				if (argv[j][i + 2] == '\0') {
					option_end = 1;
					break;
				}
				/* FALLTHROUGH */
			default:
				optmsg(argv[j]);
			}
		} else if ((iptr = fopen(argv[j], "r")) == NULL)  {
			/* end of options, filename starting with '-' */
			(void) fprintf(stderr, "nl: %s: ", argv[j]);
			perror("");
			return (1);
		}
	} else if ((iptr = fopen(argv[j], "r")) == NULL)  {
		/* filename starting with char other than '-' */
		(void) fprintf(stderr, "nl: %s: ", argv[j]);
		perror("");
		return (1);
	}
} /* closing brace of for loop */
/* end Solaris version of argument parsing */
#endif

	/* ON FIRST PASS ONLY, SET LINE COUNTER (cnt) = startcnt & */
	/* SET DEFAULT BODY TYPE TO NUMBER ALL LINES.	*/
	if (pass1) {
		cnt = startcnt;
		type = body;
		last = 'b';
		pass1 = 0;
	}

/*
 *		DO WHILE THERE IS INPUT
 *		CHECK TO SEE IF LINE IS NUMBERED,
 *		IF SO, CALCULATE NUM, PRINT NUM,
 *		THEN OUTPUT SEPERATOR CHAR AND LINE
 */

	while ((p = fgets(line, sizeof (line), iptr)) != NULL) {
	if (p[0] == delim1 && p[1] == delim2) {
		if (p[2] == delim1 &&
		    p[3] == delim2 &&
		    p[4] == delim1 &&
		    p[5] == delim2 &&
		    p[6] == '\n') {
			if (cntck != 'y')
				cnt = startcnt;
			type = header;
			last = 'h';
			swtch = 'y';
		} else {
			if (p[2] == delim1 && p[3] == delim2 && p[4] == '\n') {
				if (cntck != 'y' && last != 'h')
					cnt = startcnt;
				type = body;
				last = 'b';
				swtch = 'y';
			} else {
				if (p[0] == delim1 && p[1] == delim2 &&
							p[2] == '\n') {
					if (cntck != 'y' && last == 'f')
						cnt = startcnt;
					type = footer;
					last = 'f';
					swtch = 'y';
				}
			}
		}
	}
	if (p[0] != '\n') {
		lnt = strlen(p);
		if (p[lnt-1] == '\n')
			p[lnt-1] = '\0';
	}

	if (swtch == 'y') {
		swtch = 'n';
		(void) fprintf(optr, "\n");
	} else {
		switch (type) {
		case 'n':
			npad(width, sep);
			break;
		case 't':
			/*
			 * XPG4: The wording of Spec 1170 is misleading;
			 * the official interpretation is to number all
			 * non-empty lines, ie: the Solaris code has not
			 * been changed.
			 */
			if (p[0] != '\n') {
				pnum(cnt, sep);
				cnt += increment;
			} else {
				npad(width, sep);
			}
			break;
		case 'a':
			if (p[0] == '\n') {
				blankctr++;
				if (blank == blankctr) {
					blankctr = 0;
					pnum(cnt, sep);
					cnt += increment;
				} else
					npad(width, sep);
			} else {
				blankctr = 0;
				pnum(cnt, sep);
				cnt += increment;
			}
			break;
		case 'b':
			if (step(p, bexpbuf)) {
				pnum(cnt, sep);
				cnt += increment;
			} else {
				npad(width, sep);
			}
			break;
		case 'h':
			if (step(p, hexpbuf)) {
				pnum(cnt, sep);
				cnt += increment;
			} else {
				npad(width, sep);
			}
			break;
		case 'f':
			if (step(p, fexpbuf)) {
				pnum(cnt, sep);
				cnt += increment;
			} else {
				npad(width, sep);
			}
			break;
		}
		if (p[0] != '\n')
			p[lnt-1] = '\n';
		(void) fprintf(optr, "%s", line);

	}	/* Closing brace of "else" */
	}	/* Closing brace of "while". */
	(void) fclose(iptr);

	return (0);
}

/*		REGEXP ERR ROUTINE		*/

static void
regerr(int c)
{
	(void) fprintf(stderr, gettext(
	    "nl: invalid regular expression: error code %d\n"), c);
	exit(1);
}

/*		CALCULATE NUMBER ROUTINE	*/

static void
pnum(int n, char *sep)
{
	register int	i;

	if (format == 'z') {
		pad = '0';
	}
	for (i = 0; i < width; i++)
		nbuf[i] = pad;
	num(n, width - 1);
	if (format == 'l') {
		while (nbuf[0] == ' ') {
			for (i = 0; i < width; i++)
				nbuf[i] = nbuf[i+1];
			nbuf[width-1] = ' ';
		}
	}
	(void) printf("%s%s", nbuf, sep);
}

/*		IF NUM > 10, THEN USE THIS CALCULATE ROUTINE		*/

static void
num(int v, int p)
{
	if (v < 10)
		nbuf[p] = v + '0';
	else {
		nbuf[p] = (v % 10) + '0';
		if (p > 0)
			num(v / 10, p - 1);
	}
}

/*		CONVERT ARG STRINGS TO STRING ARRAYS	*/

#ifdef XPG4
static int
convert(int c, char *option_arg)
{
	s = option_arg;
	q = r = 0;
	while (s[q] != '\0') {
		if (s[q] >= '0' && s[q] <= '9') {
			s1[r] = s[q];
			r++;
			q++;
		} else
			optmsg(c, option_arg);
	}
	s1[r] = '\0';
	k = atoi(s1);
	return (k);
}
#else
/* Solaris version */
static int
convert(char *argv)
{
	s = (char *)argv;
	q = 2;
	r = 0;
	while (s[q] != '\0') {
		if (s[q] >= '0' && s[q] <= '9') {
			s1[r] = s[q];
			r++;
			q++;
		} else {
			optmsg(argv);
		}
	}
	s1[r] = '\0';
	k = atoi(s1);
	return (k);
}
#endif

/*		CALCULATE NUM/TEXT SEPRATOR		*/

static void
npad(int width, char *sep)
{
	register int i;

	pad = ' ';
	for (i = 0; i < width; i++)
		nbuf[i] = pad;
	(void) printf("%s", nbuf);

	for (i = 0; i < (int)strlen(sep); i++)
		(void) printf(" ");
}

#ifdef XPG4
static void
optmsg(int option, char *option_arg)
{
	if (option_arg != (char *)NULL) {
		(void) fprintf(stderr, gettext(
		    "nl: invalid option (-%c %s)\n"), option, option_arg);
	}
	/* else getopt() will print illegal option message */
	usage();
}
#else
/* Solaris version */
static void
optmsg(char *option)
{
	(void) fprintf(stderr, gettext("nl: invalid option (%s)\n"), option);
	usage();
}
#endif

void
usage()
{
	(void) fprintf(stderr, gettext(USAGE));
	exit(1);
}