nl.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* 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
* 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 */
#pragma ident "%Z%%M% %I% %E% SMI"
#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
#endif
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 *s; /* Declare the temp array for args */
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
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 tempchr; /* Temporary holding variable. */
char swtch = 'n';
char cntck = 'n';
char type;
int cnt; /* line counter */
int startcnt = 1;
int increment = 1;
int blank = 1;
int blankctr = 0;
int c;
int lnt;
char last;
#ifndef XPG4
int option_end = 0;
#endif
sep[0] = '\t';
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#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) {
"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':
header = 'h';
hexpbuf =
if (regerrno)
break;
case '\0':
header = 'n';
break;
default:
}
break;
case 'b':
switch (*optarg) {
case 't':
body = 't';
break;
case 'a':
body = 'a';
break;
case 'n':
body = 'n';
break;
case 'p':
body = 'b';
bexpbuf =
if (regerrno)
break;
case '\0':
body = 't';
break;
default:
}
break;
case 'f':
switch (*optarg) {
case 'n':
footer = 'n';
break;
case 't':
footer = 't';
break;
case 'a':
footer = 'a';
break;
case 'p':
footer = 'f';
fexpbuf =
if (regerrno)
break;
case '\0':
footer = 'n';
break;
default:
}
break;
case 'p':
cntck = 'y';
else
break;
case 'v':
if (*optarg == '\0')
startcnt = 1;
else
break;
case 'i':
if (*optarg == '\0')
increment = 1;
else
break;
case 'w':
if (*optarg == '\0')
width = 6;
else
break;
case 'l':
if (*optarg == '\0')
blank = 1;
else
break;
case 'n':
switch (*optarg) {
case 'l':
format = 'l';
else
break;
case 'r':
else
break;
case '\0':
format = 'n';
break;
default:
break;
}
break;
case 's':
break;
case 'd':
break;
break;
default:
} /* end switch char returned from getopt() */
} /* end while getopt */
optind = 0;
if (argc > 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 (!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 =
if (regerrno)
break;
case '\0':
header = 'n';
break;
default:
}
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 =
if (regerrno)
break;
case '\0':
body = 't';
break;
default:
}
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 =
if (regerrno)
break;
case '\0':
footer = 'n';
break;
default:
}
break;
case 'p':
cntck = 'y';
else
{
}
break;
case 'v':
startcnt = 1;
else
break;
case 'i':
increment = 1;
else
break;
case 'w':
width = 6;
else
break;
case 'l':
blank = 1;
else
break;
case 'n':
switch (argv[j][i+2]) {
case 'l':
format = 'l';
else
{
}
break;
case 'r':
else
{
}
break;
case '\0':
format = 'n';
break;
default:
break;
}
break;
case 's':
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':
if (tempchr == '\0')break;
if (tempchr == '\0')break;
break;
case '-':
option_end = 1;
break;
}
default:
}
/* end of options, filename starting with '-' */
perror("");
return (1);
}
/* filename starting with char other than '-' */
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) {
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
*/
if (p[2] == delim1 &&
p[3] == delim2 &&
p[4] == delim1 &&
p[5] == delim2 &&
p[6] == '\n') {
if (cntck != 'y')
last = 'h';
swtch = 'y';
} else {
last = 'b';
swtch = 'y';
} else {
p[2] == '\n') {
last = 'f';
swtch = 'y';
}
}
}
}
if (p[0] != '\n') {
}
if (swtch == 'y') {
swtch = 'n';
} else {
switch (type) {
case 'n':
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') {
} else {
}
break;
case 'a':
if (p[0] == '\n') {
blankctr++;
blankctr = 0;
} else
} else {
blankctr = 0;
}
break;
case 'b':
} else {
}
break;
case 'h':
} else {
}
break;
case 'f':
} else {
}
break;
}
if (p[0] != '\n')
} /* Closing brace of "else" */
} /* Closing brace of "while". */
return (0);
}
/* REGEXP ERR ROUTINE */
static void
regerr(c)
int c;
{
"nl: invalid regular expression: error code %d\n"), c);
exit(1);
}
/* CALCULATE NUMBER ROUTINE */
static void
int n;
char * sep;
{
register int i;
if (format == 'z') {
pad = '0';
}
for (i = 0; i < width; i++)
if (format == 'l') {
while (nbuf[0] == ' ') {
for (i = 0; i < width; i++)
}
}
}
/* IF NUM > 10, THEN USE THIS CALCULATE ROUTINE */
static void
num(v, p)
int v, p;
{
if (v < 10)
nbuf[p] = v + '0';
else {
if (p > 0)
}
}
/* CONVERT ARG STRINGS TO STRING ARRAYS */
#ifdef XPG4
static int
convert(c, option_arg)
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';
return (k);
}
#else
/* Solaris version */
static int
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
{
}
}
s1[r] = '\0';
return (k);
}
#endif
static void
int width;
char * sep;
{
register int i;
pad = ' ';
for (i = 0; i < width; i++)
(void) printf(" ");
}
#ifdef XPG4
static void
int option;
char *option_arg;
{
if (option_arg != (char *)NULL) {
}
/* else getopt() will print illegal option message */
usage();
}
#else
/* Solaris version */
static void
char *option;
{
"nl: invalid option (%s)\n"), option);
usage();
}
#endif
void
usage()
{
exit(1);
}