* Copyright (c) 1993 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* %sccs.include.redist.c%
static char sccsid
[] = "@(#)init.c 5.1 (Berkeley) %G%";
extern struct coldesc clist
[(ND
+1)*2];
* clist (list of columns which correspond to one or more icol or tcol)
* is in increasing order of columns.
* Fields are kept in increasing order of fields.
* keep clist in order--inserts a column in a sorted array
for (i
= 0; i
< ncols
; i
++)
if (field
->icol
.num
<= clist
[i
].num
)
if (field
->icol
.num
!= clist
[i
].num
) {
memmove(clist
+i
+1, clist
+i
, sizeof(COLDESC
)*(ncols
-i
));
clist
[i
].num
= field
->icol
.num
;
if (field
->tcol
.num
&& field
->tcol
.num
!= field
->icol
.num
) {
for (i
= 0; i
< ncols
; i
++)
if (field
->tcol
.num
<= clist
[i
].num
)
if (field
->tcol
.num
!= clist
[i
].num
) {
memmove(clist
+i
+1, clist
+i
,sizeof(COLDESC
)*(ncols
-i
));
clist
[i
].num
= field
->tcol
.num
;
* matches fields with the appropriate columns--n^2 but who cares?
fldtab
[0].tcol
.p
= clist
+ncols
-1;
for (++fldtab
; fldtab
->icol
.num
; ++fldtab
) {
for (i
= 0; fldtab
->icol
.num
!= clist
[i
].num
; i
++);
fldtab
->icol
.p
= clist
+ i
;
for (i
= 0; fldtab
->tcol
.num
!= clist
[i
].num
; i
++);
fldtab
->tcol
.p
= clist
+ i
;
* interprets a column in a -k field
setcolumn(pos
, cur_fld
, gflag
)
col
= cur_fld
->icol
.num
? (&(*cur_fld
).tcol
) : (&(*cur_fld
).icol
);
pos
+= sscanf(pos
, "%d", &(col
->num
));
if (col
->num
<= 0 && !(col
->num
== 0 && col
== &(cur_fld
->tcol
)))
errx(2, "field numbers must be positive");
errx(2, "cannot indent end of line");
pos
+= sscanf(++pos
, "%d", &(col
->indent
));
if (&cur_fld
->icol
== col
)
errx(2, "illegal offset");
if (optval(*pos
, cur_fld
->tcol
.num
))
while (tmp
= optval(*pos
, cur_fld
->tcol
.num
)) {
if (cur_fld
->icol
.num
== 0)
setfield(pos
, cur_fld
, gflag
)
errx(2, "too many sort keys. (Limit is %d)", ND
-1);
cur_fld
->weights
= ascii
;
cur_fld
->mask
= alltable
;
pos
= setcolumn(pos
, cur_fld
, gflag
);
if (*pos
== '\0') /* key extends to EOL. */
errx(2, "illegal field descriptor");
setcolumn((++pos
), cur_fld
, gflag
);
* Assign appropriate mask table and weight table.
* If the global weights are reversed, the local field
if (((tmp
& R
) ^ (gflag
& R
)) && tmp
& F
)
cur_fld
->weights
= RFtable
;
cur_fld
->weights
= Ftable
;
else if ((tmp
& R
) ^ (gflag
& R
))
cur_fld
->weights
= Rascii
;
cur_fld
->flags
|= (gflag
& (BI
| BT
));
if (!cur_fld
->tcol
.indent
) /* BT has no meaning at end of field */
cur_fld
->flags
&= (D
|F
|I
|N
|R
|BI
);
if (cur_fld
->tcol
.num
&& !(!(cur_fld
->flags
& BI
)
&& cur_fld
->flags
& BT
) && (cur_fld
->tcol
.num
<= cur_fld
->icol
.num
&& cur_fld
->tcol
.indent
< cur_fld
->icol
.indent
))
errx(2, "fields out of order");
return (cur_fld
->tcol
.num
);
static char vbuf
[ND
*20], *vpos
, *tpos
;
for (i
= 1; i
< *argc
; i
++) {
vpos
+= sprintf(vpos
, "-k");
tpos
+= sscanf(tpos
, "%d", &v
);
vpos
+= sprintf(vpos
, "%d", v
+1);
tpos
+= sscanf(++tpos
, "%d", &x
);
vpos
+= sprintf(vpos
, ".%d", x
+1);
vpos
+= sprintf(vpos
, ",");
argv
[i
+1][0] == '-' && isdigit(argv
[i
+1][1])) {
tpos
+= sscanf(tpos
, "%d", &w
);
tpos
+= sscanf(++tpos
, "%d", &x
);
vpos
+= sprintf(vpos
, "%d", w
+1);
vpos
+= sprintf(vpos
, ".%d", x
);
vpos
+= sprintf(vpos
, "%d", w
);
for (j
= i
+1; j
< *argc
; j
++)
* ascii, Rascii, Ftable, and RFtable map
* REC_D -> REC_D; {not REC_D} -> {not REC_D}.
* gweights maps REC_D -> (0 or 255); {not REC_D} -> {not gweights[REC_D]}.
* Note: when sorting in forward order, to encode character zero in a key,
* use \001\001; character 1 becomes \001\002. In this case, character 0
* is reserved for the field delimiter. Analagously for -r (fld_d = 255).
* Note: this is only good for ASCII sorting. For different LC 's,
* all bets are off. See also num_init in number.c
for (i
=0; i
< 256; i
++) {
if (i
> REC_D
&& i
< 255 - REC_D
+1)
Ftable
[i
] = Ftable
[i
- ('a' -'A')];
RFtable
[i
] = RFtable
[i
- ('a' - 'A')];
} else if (REC_D
>= 'A' && REC_D
< 'Z' && i
< 'a' && i
> REC_D
) {
RFtable
[i
] = Rascii
[i
] - 1;
if (i
== '\n' || isprint(i
))
if (i
== '\n' || i
== '\t' || i
== ' ' || isalnum(i
))
Rascii
[REC_D
] = RFtable
[REC_D
] = REC_D
;
if (REC_D
>= 'A' && REC_D
< 'Z')
++Ftable
[REC_D
+ ('a' - 'A')];
if (gflags
& R
&& (!(gflags
& F
) || !SINGL_FLD
))
else if (!(gflags
& F
) || !SINGL_FLD
)
memmove(gweights
, wts
, sizeof(gweights
));
incr
= (gflags
& R
) ? -1 : 1;
for (i
= 0; i
< REC_D
; i
++)
gweights
[REC_D
] = ((gflags
& R
) ? 255 : 0);
if (SINGL_FLD
&& gflags
& F
) {
for (i
= 0; i
< REC_D
; i
++) {
ascii
[REC_D
] = Rascii
[REC_D
] = gweights
[REC_D
];