Add mandoc-manpage for cut(1) and clean up code

and mark it as finished in README.
This commit is contained in:
FRIGN 2015-01-18 11:30:31 +01:00
parent 699f246239
commit 76ca226e81
3 changed files with 81 additions and 71 deletions

2
README
View File

@ -21,7 +21,7 @@ The following tools are implemented (* == finished):
* comm yes none * comm yes none
cp no -H, -i, -L cp no -H, -i, -L
* cron non-posix none * cron non-posix none
cut yes none * cut yes none
date yes none date yes none
dirname yes none dirname yes none
du no -H, -L, -x du no -H, -L, -x

126
cut.1
View File

@ -1,60 +1,70 @@
.TH CUT 1 sbase\-VERSION .Dd January 18, 2015
.SH NAME .Dt CUT 1 sbase\-VERSION
cut \- extract columns of data .Sh NAME
.SH SYNOPSIS .Nm cut
.B cut \-b .Nd extract columns of data
.I list .Sh SYNOPSIS
.RB [ \-n ] .Nm cut
.RI [ file ...] .Fl b Ar list
.br .Op Fl n
.B cut \-c .Op Ar file ...
.I list .Nm cut
.RI [ file ...] .Fl c Ar list
.br .Op Ar file ...
.B cut \-f .Nm cut
.I list .Fl f Ar list
.RB [ \-d .Op Fl d Ar delim
.IR delim ] .Op Fl s
.RB [ \-s ] .Op Ar file ...
.RI [ file ...] .Sh DESCRIPTION
.SH DESCRIPTION .Nm
.B cut out bytes, characters or delimited fields from each line of
out bytes, characters, or delimited fields from each line of the given .Ar file
files and write to stdout. With no file, or when file is `-', cut reads and write to stdout.
from stdin. .Pp
.P If no
.I list .Ar file
is a comma or space separated list of numbers and ranges where numbering is given or
starts from 1. Ranges are on the form `N-M'. If N or M is missing, the .Ar file
beginning or end of line is assumed. Numbers and ranges may be repeated, is '-',
overlapping, and in any order. Selected input is written in the same .Nm
order that it is read, and is written exactly once. reads from stdin.
.SH OPTIONS .Pp
.TP .Ar list
.BI \-b \ list is a comma or space separated list of numbers and ranges starting
The from 1. Ranges have the form 'N-M'. If N or M is missing,
.I list beginning or end of line is assumed. Numbers and ranges
specifies byte positions. may be repeated, overlapping and in any order.
.TP .Pp
.BI \-c \ list Selected input is written in the same order it is read
The and is written exactly once.
.I list .Sh OPTIONS
specifies character positions. .Bl -tag -width Ds
.TP .It Fl b Ar list | Fl c Ar list
.BI \-d \ delim .Ar list
specifies byte | character positions.
.It Fl n
Do not split multibyte characters. A character is written when its
last byte is selected.
.It Fl f Ar list
.Ar list
specifies field numbers. Lines not containing field
delimiters are passed through, unless
.Fl s
is specified.
.It Fl d Ar delim
Use first byte of Use first byte of
.I delim .Ar delim
as field delimiter, instead of tab. as field delimiter. Default is \et.
.TP .It Fl s
.BI \-f \ list
The
.I list
specifies field numbers. Lines not containing field delimiters are
passed through untouched.
.TP
.B \-n
Do not split characters. A character is output if its last byte is
selected.
.TP
.B \-s
Suppress lines not containing field delimiters. Suppress lines not containing field delimiters.
.El
.Sh STANDARDS
The
.Nm
utility is compliant with the
.St -p1003.1-2008
specification.
.Pp
The possibility of separating numbers and ranges with a space
is an extension to that specification.

24
cut.c
View File

@ -6,14 +6,6 @@
#include "text.h" #include "text.h"
#include "util.h" #include "util.h"
static void
usage(void)
{
eprintf("usage: cut -b list [-n] [file...]\n"
" cut -c list [file...]\n"
" cut -f list [-d delim] [-s] [file...]\n");
}
typedef struct Range { typedef struct Range {
size_t min, max; size_t min, max;
struct Range *next; struct Range *next;
@ -135,6 +127,14 @@ cut(FILE *fp)
} }
} }
static void
usage(void)
{
eprintf("usage: cut -b list [-n] [file ...]\n"
" cut -c list [file ...]\n"
" cut -f list [-d delim] [-s] [file ...]\n");
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
@ -163,12 +163,12 @@ main(int argc, char *argv[])
if (!mode) if (!mode)
usage(); usage();
if (!argc) { if (!argc)
cut(stdin); cut(stdin);
} else for (; argc--; argv++) { else for (; argc--; argv++) {
if (!strcmp(*argv, "-")) { if (!strcmp(*argv, "-"))
cut(stdin); cut(stdin);
} else { else {
if (!(fp = fopen(*argv, "r"))) { if (!(fp = fopen(*argv, "r"))) {
weprintf("fopen %s:", *argv); weprintf("fopen %s:", *argv);
continue; continue;