xref: /illumos-gate/usr/src/cmd/sort/convert.c (revision 101e15b5f8a77d9433805e541996abaabc9ca8c1)
1*101e15b5SRichard Lowe /*
2*101e15b5SRichard Lowe  * CDDL HEADER START
3*101e15b5SRichard Lowe  *
4*101e15b5SRichard Lowe  * The contents of this file are subject to the terms of the
5*101e15b5SRichard Lowe  * Common Development and Distribution License, Version 1.0 only
6*101e15b5SRichard Lowe  * (the "License").  You may not use this file except in compliance
7*101e15b5SRichard Lowe  * with the License.
8*101e15b5SRichard Lowe  *
9*101e15b5SRichard Lowe  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*101e15b5SRichard Lowe  * or http://www.opensolaris.org/os/licensing.
11*101e15b5SRichard Lowe  * See the License for the specific language governing permissions
12*101e15b5SRichard Lowe  * and limitations under the License.
13*101e15b5SRichard Lowe  *
14*101e15b5SRichard Lowe  * When distributing Covered Code, include this CDDL HEADER in each
15*101e15b5SRichard Lowe  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*101e15b5SRichard Lowe  * If applicable, add the following below this CDDL HEADER, with the
17*101e15b5SRichard Lowe  * fields enclosed by brackets "[]" replaced with your own identifying
18*101e15b5SRichard Lowe  * information: Portions Copyright [yyyy] [name of copyright owner]
19*101e15b5SRichard Lowe  *
20*101e15b5SRichard Lowe  * CDDL HEADER END
21*101e15b5SRichard Lowe  */
22*101e15b5SRichard Lowe /*
23*101e15b5SRichard Lowe  * Copyright 2000-2002 Sun Microsystems, Inc.  All rights reserved.
24*101e15b5SRichard Lowe  * Use is subject to license terms.
25*101e15b5SRichard Lowe  */
26*101e15b5SRichard Lowe 
27*101e15b5SRichard Lowe /*
28*101e15b5SRichard Lowe  * convert
29*101e15b5SRichard Lowe  *   given a sort invocation, convert input files, and display sort collation
30*101e15b5SRichard Lowe  *   vectors to stdout.
31*101e15b5SRichard Lowe  */
32*101e15b5SRichard Lowe 
33*101e15b5SRichard Lowe #include "main.h"
34*101e15b5SRichard Lowe 
35*101e15b5SRichard Lowe #define	COLLATE_BUFSIZE	4096
36*101e15b5SRichard Lowe 
37*101e15b5SRichard Lowe static void
convert(sort_t * S)38*101e15b5SRichard Lowe convert(sort_t *S)
39*101e15b5SRichard Lowe {
40*101e15b5SRichard Lowe 	stream_t *convert_chain = S->m_input_streams;
41*101e15b5SRichard Lowe 	stream_t *cur_streamp = convert_chain;
42*101e15b5SRichard Lowe 	flag_t coll_flags;
43*101e15b5SRichard Lowe 	ssize_t (*coll_convert)(field_t *, line_rec_t *, flag_t, vchar_t);
44*101e15b5SRichard Lowe 
45*101e15b5SRichard Lowe 	coll_convert = S->m_coll_convert;
46*101e15b5SRichard Lowe 
47*101e15b5SRichard Lowe 	if (S->m_field_options & FIELD_REVERSE_COMPARISONS)
48*101e15b5SRichard Lowe 		coll_flags = COLL_REVERSE;
49*101e15b5SRichard Lowe 	else
50*101e15b5SRichard Lowe 		coll_flags = 0;
51*101e15b5SRichard Lowe 
52*101e15b5SRichard Lowe 	if (S->m_entire_line)
53*101e15b5SRichard Lowe 		coll_flags |= COLL_UNIQUE;
54*101e15b5SRichard Lowe 
55*101e15b5SRichard Lowe 	while (cur_streamp != NULL) {
56*101e15b5SRichard Lowe 		u_longlong_t lineno = 0;
57*101e15b5SRichard Lowe 
58*101e15b5SRichard Lowe 		stream_open_for_read(S, cur_streamp);
59*101e15b5SRichard Lowe 		if (cur_streamp->s_status & STREAM_SINGLE ||
60*101e15b5SRichard Lowe 		    cur_streamp->s_status & STREAM_WIDE)
61*101e15b5SRichard Lowe 			stream_set(cur_streamp, STREAM_INSTANT);
62*101e15b5SRichard Lowe 
63*101e15b5SRichard Lowe 		SOP_PRIME(cur_streamp);
64*101e15b5SRichard Lowe 
65*101e15b5SRichard Lowe 		cur_streamp->s_current.l_collate.sp = safe_realloc(NULL,
66*101e15b5SRichard Lowe 		    COLLATE_BUFSIZE);
67*101e15b5SRichard Lowe 		cur_streamp->s_current.l_collate_bufsize = COLLATE_BUFSIZE;
68*101e15b5SRichard Lowe 
69*101e15b5SRichard Lowe 		for (;;) {
70*101e15b5SRichard Lowe 			(void) coll_convert(S->m_fields_head,
71*101e15b5SRichard Lowe 			    &cur_streamp->s_current, FCV_REALLOC,
72*101e15b5SRichard Lowe 			    S->m_field_separator);
73*101e15b5SRichard Lowe 
74*101e15b5SRichard Lowe 			(void) fprintf(stdout, "(%llu) ", lineno++);
75*101e15b5SRichard Lowe 			if (cur_streamp->s_status & STREAM_WIDE)
76*101e15b5SRichard Lowe 				(void) fprintf(stdout, "%.*ws\n\n",
77*101e15b5SRichard Lowe 				    cur_streamp->s_current.l_data_length,
78*101e15b5SRichard Lowe 				    cur_streamp->s_current.l_data.wp);
79*101e15b5SRichard Lowe 			else
80*101e15b5SRichard Lowe 				(void) fprintf(stdout, "%.*s\n\n",
81*101e15b5SRichard Lowe 				    cur_streamp->s_current.l_data_length,
82*101e15b5SRichard Lowe 				    cur_streamp->s_current.l_data.usp);
83*101e15b5SRichard Lowe 			xdump(stdout, cur_streamp->s_current.l_collate.usp,
84*101e15b5SRichard Lowe 			    cur_streamp->s_current.l_collate_length,
85*101e15b5SRichard Lowe 			    cur_streamp->s_status & STREAM_WIDE);
86*101e15b5SRichard Lowe 			(void) fprintf(stdout, "\n");
87*101e15b5SRichard Lowe 
88*101e15b5SRichard Lowe 			if (SOP_EOS(cur_streamp))
89*101e15b5SRichard Lowe 				break;
90*101e15b5SRichard Lowe 
91*101e15b5SRichard Lowe 			SOP_FETCH(cur_streamp);
92*101e15b5SRichard Lowe 		}
93*101e15b5SRichard Lowe 
94*101e15b5SRichard Lowe 		(void) SOP_FREE(cur_streamp);
95*101e15b5SRichard Lowe 		(void) SOP_CLOSE(cur_streamp);
96*101e15b5SRichard Lowe 
97*101e15b5SRichard Lowe 		cur_streamp = cur_streamp->s_next;
98*101e15b5SRichard Lowe 	}
99*101e15b5SRichard Lowe }
100*101e15b5SRichard Lowe 
101*101e15b5SRichard Lowe int
main(int argc,char * argv[])102*101e15b5SRichard Lowe main(int argc, char *argv[])
103*101e15b5SRichard Lowe {
104*101e15b5SRichard Lowe 	sort_t S;
105*101e15b5SRichard Lowe 
106*101e15b5SRichard Lowe 	initialize_pre(&S);
107*101e15b5SRichard Lowe 
108*101e15b5SRichard Lowe 	if (options(&S, argc, argv))
109*101e15b5SRichard Lowe 		return (E_ERROR);
110*101e15b5SRichard Lowe 
111*101e15b5SRichard Lowe 	initialize_post(&S);
112*101e15b5SRichard Lowe 
113*101e15b5SRichard Lowe 	convert(&S);
114*101e15b5SRichard Lowe 
115*101e15b5SRichard Lowe 	return (E_SUCCESS);
116*101e15b5SRichard Lowe }
117