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