xref: /illumos-gate/usr/src/cmd/sort/convert.c (revision 533affcbc7fc4d0c8132976ea454aaa715fe2307)
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 /*
28  * convert
29  *   given a sort invocation, convert input files, and display sort collation
30  *   vectors to stdout.
31  */
32 
33 #include "main.h"
34 
35 #define	COLLATE_BUFSIZE	4096
36 
37 static void
38 convert(sort_t *S)
39 {
40 	stream_t *convert_chain = S->m_input_streams;
41 	stream_t *cur_streamp = convert_chain;
42 	flag_t coll_flags;
43 	ssize_t (*coll_convert)(field_t *, line_rec_t *, flag_t, vchar_t);
44 
45 	coll_convert = S->m_coll_convert;
46 
47 	if (S->m_field_options & FIELD_REVERSE_COMPARISONS)
48 		coll_flags = COLL_REVERSE;
49 	else
50 		coll_flags = 0;
51 
52 	if (S->m_entire_line)
53 		coll_flags |= COLL_UNIQUE;
54 
55 	while (cur_streamp != NULL) {
56 		u_longlong_t lineno = 0;
57 
58 		stream_open_for_read(S, cur_streamp);
59 		if (cur_streamp->s_status & STREAM_SINGLE ||
60 		    cur_streamp->s_status & STREAM_WIDE)
61 			stream_set(cur_streamp, STREAM_INSTANT);
62 
63 		SOP_PRIME(cur_streamp);
64 
65 		cur_streamp->s_current.l_collate.sp = safe_realloc(NULL,
66 		    COLLATE_BUFSIZE);
67 		cur_streamp->s_current.l_collate_bufsize = COLLATE_BUFSIZE;
68 
69 		for (;;) {
70 			(void) coll_convert(S->m_fields_head,
71 			    &cur_streamp->s_current, FCV_REALLOC,
72 			    S->m_field_separator);
73 
74 			(void) fprintf(stdout, "(%llu) ", lineno++);
75 			if (cur_streamp->s_status & STREAM_WIDE)
76 				(void) fprintf(stdout, "%.*ws\n\n",
77 				    cur_streamp->s_current.l_data_length,
78 				    cur_streamp->s_current.l_data.wp);
79 			else
80 				(void) fprintf(stdout, "%.*s\n\n",
81 				    cur_streamp->s_current.l_data_length,
82 				    cur_streamp->s_current.l_data.usp);
83 			xdump(stdout, cur_streamp->s_current.l_collate.usp,
84 			    cur_streamp->s_current.l_collate_length,
85 			    cur_streamp->s_status & STREAM_WIDE);
86 			(void) fprintf(stdout, "\n");
87 
88 			if (SOP_EOS(cur_streamp))
89 				break;
90 
91 			SOP_FETCH(cur_streamp);
92 		}
93 
94 		(void) SOP_FREE(cur_streamp);
95 		(void) SOP_CLOSE(cur_streamp);
96 
97 		cur_streamp = cur_streamp->s_next;
98 	}
99 }
100 
101 int
102 main(int argc, char *argv[])
103 {
104 	sort_t S;
105 
106 	initialize_pre(&S);
107 
108 	if (options(&S, argc, argv))
109 		return (E_ERROR);
110 
111 	initialize_post(&S);
112 
113 	convert(&S);
114 
115 	return (E_SUCCESS);
116 }
117