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