xref: /freebsd/usr.bin/sort/coll.h (revision 19fae0f66023a97a9b464b3beeeabb2081f575b3)
1 /*	$FreeBSD$	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
7  * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #if !defined(__COLL_H__)
33 #define	__COLL_H__
34 
35 #include "bwstring.h"
36 #include "sort.h"
37 
38 /*
39  * Sort hint data for -n
40  */
41 struct n_hint
42 {
43 	unsigned long long	 n1;
44 	unsigned char		 si;
45 	bool			 empty;
46 	bool			 neg;
47 };
48 
49 /*
50  * Sort hint data for -g
51  */
52 struct g_hint
53 {
54 	double			 d;
55 	bool			 nan;
56 	bool			 notnum;
57 };
58 
59 /*
60  * Sort hint data for -M
61  */
62 struct M_hint
63 {
64 	int			 m;
65 };
66 
67 /*
68  * Sort hint data for -R
69  *
70  * This stores the first 12 bytes of the digest rather than the full output to
71  * avoid increasing the size of the 'key_hint' object via the 'v' union.
72  */
73 struct R_hint
74 {
75 	unsigned char		 cached[12];
76 };
77 
78 /*
79  * Status of a sort hint object
80  */
81 typedef enum
82 {
83 	HS_ERROR = -1, HS_UNINITIALIZED = 0, HS_INITIALIZED = 1
84 } hint_status;
85 
86 /*
87  * Sort hint object
88  */
89 struct key_hint
90 {
91 	hint_status		status;
92 	union
93 	{
94 		struct n_hint		nh;
95 		struct g_hint		gh;
96 		struct M_hint		Mh;
97 		struct R_hint		Rh;
98 	}			v;
99 };
100 
101 /*
102  * Key value
103  */
104 struct key_value
105 {
106 	struct bwstring		*k; /* key string */
107 	struct key_hint		 hint[0]; /* key sort hint */
108 } __packed;
109 
110 /*
111  * Set of keys container object.
112  */
113 struct keys_array
114 {
115 	struct key_value	 key[0];
116 };
117 
118 /*
119  * Parsed -k option data
120  */
121 struct key_specs
122 {
123 	struct sort_mods	 sm;
124 	size_t			 c1;
125 	size_t			 c2;
126 	size_t			 f1;
127 	size_t			 f2;
128 	bool			 pos1b;
129 	bool			 pos2b;
130 };
131 
132 /*
133  * Single entry in sort list.
134  */
135 struct sort_list_item
136 {
137 	struct bwstring		*str;
138 	struct keys_array	 ka;
139 };
140 
141 /*
142  * Function type, used to compare two list objects
143  */
144 typedef int (*listcoll_t)(struct sort_list_item **ss1, struct sort_list_item **ss2);
145 
146 extern struct key_specs *keys;
147 extern size_t keys_num;
148 
149 /*
150  * Main localised symbols. These must be wint_t as they may hold WEOF.
151  */
152 extern wint_t symbol_decimal_point;
153 extern wint_t symbol_thousands_sep;
154 extern wint_t symbol_negative_sign;
155 extern wint_t symbol_positive_sign;
156 
157 /* funcs */
158 
159 cmpcoll_t get_sort_func(struct sort_mods *sm);
160 
161 struct keys_array *keys_array_alloc(void);
162 size_t keys_array_size(void);
163 struct key_value *get_key_from_keys_array(struct keys_array *ka, size_t ind);
164 void set_key_on_keys_array(struct keys_array *ka, struct bwstring *s, size_t ind);
165 void clean_keys_array(const struct bwstring *s, struct keys_array *ka);
166 
167 struct sort_list_item *sort_list_item_alloc(void);
168 void sort_list_item_set(struct sort_list_item *si, struct bwstring *str);
169 void sort_list_item_clean(struct sort_list_item *si);
170 size_t sort_list_item_size(struct sort_list_item *si);
171 
172 int preproc(struct bwstring *s, struct keys_array *ka);
173 int top_level_str_coll(const struct bwstring *, const struct bwstring *);
174 int key_coll(struct keys_array *ks1, struct keys_array *ks2, size_t offset);
175 int str_list_coll(struct bwstring *str1, struct sort_list_item **ss2);
176 int list_coll_by_str_only(struct sort_list_item **ss1, struct sort_list_item **ss2);
177 int list_coll(struct sort_list_item **ss1, struct sort_list_item **ss2);
178 int list_coll_offset(struct sort_list_item **ss1, struct sort_list_item **ss2, size_t offset);
179 
180 listcoll_t get_list_call_func(size_t offset);
181 
182 #endif /* __COLL_H__ */
183