xref: /openbsd-src/usr.bin/sort/coll.h (revision 474067aa16f98acadbc6fc6ba12f0b8182ccc459)
1*474067aaSschwarze /*	$OpenBSD: coll.h,v 1.2 2019/05/13 17:00:12 schwarze Exp $	*/
279428148Smillert 
379428148Smillert /*-
479428148Smillert  * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
579428148Smillert  * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
679428148Smillert  * All rights reserved.
779428148Smillert  *
879428148Smillert  * Redistribution and use in source and binary forms, with or without
979428148Smillert  * modification, are permitted provided that the following conditions
1079428148Smillert  * are met:
1179428148Smillert  * 1. Redistributions of source code must retain the above copyright
1279428148Smillert  *    notice, this list of conditions and the following disclaimer.
1379428148Smillert  * 2. Redistributions in binary form must reproduce the above copyright
1479428148Smillert  *    notice, this list of conditions and the following disclaimer in the
1579428148Smillert  *    documentation and/or other materials provided with the distribution.
1679428148Smillert  *
1779428148Smillert  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1879428148Smillert  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1979428148Smillert  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2079428148Smillert  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2179428148Smillert  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2279428148Smillert  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2379428148Smillert  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2479428148Smillert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2579428148Smillert  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2679428148Smillert  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2779428148Smillert  * SUCH DAMAGE.
2879428148Smillert  */
2979428148Smillert 
3079428148Smillert #if !defined(__COLL_H__)
3179428148Smillert #define	__COLL_H__
3279428148Smillert 
3379428148Smillert #include "bwstring.h"
3479428148Smillert #include "sort.h"
3579428148Smillert 
3679428148Smillert /*
3779428148Smillert  * Sort hint data for -n
3879428148Smillert  */
3979428148Smillert struct n_hint {
4079428148Smillert 	unsigned long long	 n1;
4179428148Smillert 	unsigned char		 si;
4279428148Smillert 	bool			 empty;
4379428148Smillert 	bool			 neg;
4479428148Smillert };
4579428148Smillert 
4679428148Smillert /*
4779428148Smillert  * Sort hint data for -g
4879428148Smillert  */
4979428148Smillert struct g_hint {
5079428148Smillert 	double			 d;
5179428148Smillert 	bool			 nan;
5279428148Smillert 	bool			 notnum;
5379428148Smillert };
5479428148Smillert 
5579428148Smillert /*
5679428148Smillert  * Sort hint data for -M
5779428148Smillert  */
5879428148Smillert struct M_hint {
5979428148Smillert 	int			 m;
6079428148Smillert };
6179428148Smillert 
6279428148Smillert /*
6379428148Smillert  * Status of a sort hint object
6479428148Smillert  */
6579428148Smillert typedef enum {
6679428148Smillert 	HS_ERROR = -1, HS_UNINITIALIZED = 0, HS_INITIALIZED = 1
6779428148Smillert } hint_status;
6879428148Smillert 
6979428148Smillert /*
7079428148Smillert  * Sort hint object
7179428148Smillert  */
7279428148Smillert struct key_hint {
7379428148Smillert 	hint_status		status;
7479428148Smillert 	union
7579428148Smillert 	{
7679428148Smillert 		struct n_hint		nh;
7779428148Smillert 		struct g_hint		gh;
7879428148Smillert 		struct M_hint		Mh;
7979428148Smillert 	}			v;
8079428148Smillert };
8179428148Smillert 
8279428148Smillert /*
8379428148Smillert  * Key value
8479428148Smillert  */
8579428148Smillert struct key_value {
8679428148Smillert 	struct bwstring		*k; /* key string */
8779428148Smillert 	struct key_hint		 hint[0]; /* key sort hint */
8879428148Smillert };
8979428148Smillert 
9079428148Smillert /*
9179428148Smillert  * Set of keys container object.
9279428148Smillert  */
9379428148Smillert struct keys_array {
9479428148Smillert 	struct key_value	 key[0];
9579428148Smillert };
9679428148Smillert 
9779428148Smillert /*
9879428148Smillert  * Parsed -k option data
9979428148Smillert  */
10079428148Smillert struct key_specs {
10179428148Smillert 	struct sort_mods	 sm;
10279428148Smillert 	size_t			 c1;
10379428148Smillert 	size_t			 c2;
10479428148Smillert 	size_t			 f1;
10579428148Smillert 	size_t			 f2;
10679428148Smillert 	bool			 pos1b;
10779428148Smillert 	bool			 pos2b;
10879428148Smillert };
10979428148Smillert 
11079428148Smillert /*
11179428148Smillert  * Single entry in sort list.
11279428148Smillert  */
11379428148Smillert struct sort_list_item {
11479428148Smillert 	struct bwstring		*str;
11579428148Smillert 	struct keys_array	 ka;
11679428148Smillert };
11779428148Smillert 
11879428148Smillert /*
11979428148Smillert  * Function type, used to compare two list objects
12079428148Smillert  */
12179428148Smillert typedef int (*listcoll_t)(struct sort_list_item **ss1, struct sort_list_item **ss2);
12279428148Smillert 
12379428148Smillert extern struct key_specs *keys;
12479428148Smillert extern size_t keys_num;
12579428148Smillert 
12679428148Smillert /* funcs */
12779428148Smillert 
12879428148Smillert cmpcoll_t get_sort_func(struct sort_mods *sm);
12979428148Smillert 
13079428148Smillert struct keys_array *keys_array_alloc(void);
13179428148Smillert size_t keys_array_size(void);
13279428148Smillert void set_key_on_keys_array(struct keys_array *ka, struct bwstring *s, size_t ind);
13379428148Smillert void clean_keys_array(const struct bwstring *s, struct keys_array *ka);
13479428148Smillert 
13579428148Smillert struct sort_list_item *sort_list_item_alloc(void);
13679428148Smillert void sort_list_item_set(struct sort_list_item *si, struct bwstring *str);
13779428148Smillert void sort_list_item_clean(struct sort_list_item *si);
13879428148Smillert size_t sort_list_item_size(struct sort_list_item *si);
13979428148Smillert 
14079428148Smillert int preproc(struct bwstring *s, struct keys_array *ka);
14179428148Smillert int top_level_str_coll(const struct bwstring *, const struct bwstring *);
14279428148Smillert int key_coll(struct keys_array *ks1, struct keys_array *ks2, size_t offset);
14379428148Smillert int str_list_coll(struct bwstring *str1, struct sort_list_item **ss2);
14479428148Smillert int list_coll_by_str_only(struct sort_list_item **ss1, struct sort_list_item **ss2);
14579428148Smillert int list_coll(const void *ss1, const void *ss2);
14679428148Smillert int list_coll_offset(struct sort_list_item **ss1, struct sort_list_item **ss2, size_t offset);
14779428148Smillert 
14879428148Smillert listcoll_t get_list_call_func(size_t offset);
14979428148Smillert 
15079428148Smillert #endif /* __COLL_H__ */
151