xref: /freebsd/usr.bin/sort/sort.h (revision e8da8c744b5150d2165eeed3ecf71584f19abb03)
1c66bbc91SGabor Kovesdan /*	$FreeBSD$	*/
2c66bbc91SGabor Kovesdan 
3c66bbc91SGabor Kovesdan /*-
4c66bbc91SGabor Kovesdan  * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
5c66bbc91SGabor Kovesdan  * Copyright (C) 2012 Oleg Moskalenko <oleg.moskalenko@citrix.com>
6c66bbc91SGabor Kovesdan  * All rights reserved.
7c66bbc91SGabor Kovesdan  *
8c66bbc91SGabor Kovesdan  * Redistribution and use in source and binary forms, with or without
9c66bbc91SGabor Kovesdan  * modification, are permitted provided that the following conditions
10c66bbc91SGabor Kovesdan  * are met:
11c66bbc91SGabor Kovesdan  * 1. Redistributions of source code must retain the above copyright
12c66bbc91SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer.
13c66bbc91SGabor Kovesdan  * 2. Redistributions in binary form must reproduce the above copyright
14c66bbc91SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer in the
15c66bbc91SGabor Kovesdan  *    documentation and/or other materials provided with the distribution.
16c66bbc91SGabor Kovesdan  *
17c66bbc91SGabor Kovesdan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18c66bbc91SGabor Kovesdan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19c66bbc91SGabor Kovesdan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20c66bbc91SGabor Kovesdan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21c66bbc91SGabor Kovesdan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22c66bbc91SGabor Kovesdan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23c66bbc91SGabor Kovesdan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24c66bbc91SGabor Kovesdan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25c66bbc91SGabor Kovesdan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26c66bbc91SGabor Kovesdan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27c66bbc91SGabor Kovesdan  * SUCH DAMAGE.
28c66bbc91SGabor Kovesdan  */
29c66bbc91SGabor Kovesdan 
30c66bbc91SGabor Kovesdan #if !defined(__BSD_SORT_H__)
31c66bbc91SGabor Kovesdan #define __BSD_SORT_H__
32c66bbc91SGabor Kovesdan 
33c66bbc91SGabor Kovesdan #include <errno.h>
34c66bbc91SGabor Kovesdan #include <stdbool.h>
35c66bbc91SGabor Kovesdan #include <stdio.h>
36c66bbc91SGabor Kovesdan #include <sysexits.h>
37c66bbc91SGabor Kovesdan #include <wchar.h>
38c66bbc91SGabor Kovesdan 
39c66bbc91SGabor Kovesdan #include <sys/types.h>
40c66bbc91SGabor Kovesdan #include <md5.h>
41c66bbc91SGabor Kovesdan 
42c66bbc91SGabor Kovesdan #define	VERSION	"2.3-FreeBSD"
43c66bbc91SGabor Kovesdan 
44c66bbc91SGabor Kovesdan #ifdef WITHOUT_NLS
45c66bbc91SGabor Kovesdan #define	getstr(n)	 nlsstr[n]
46c66bbc91SGabor Kovesdan #else
47c66bbc91SGabor Kovesdan #include <nl_types.h>
48c66bbc91SGabor Kovesdan 
49c66bbc91SGabor Kovesdan extern nl_catd catalog;
50c66bbc91SGabor Kovesdan #define	getstr(n)	 catgets(catalog, 1, n, nlsstr[n])
51c66bbc91SGabor Kovesdan #endif
52c66bbc91SGabor Kovesdan 
53c66bbc91SGabor Kovesdan extern const char *nlsstr[];
54c66bbc91SGabor Kovesdan 
55c66bbc91SGabor Kovesdan #if defined(SORT_THREADS)
565ca724dcSGabor Kovesdan #define MT_SORT_THRESHOLD (10000)
57c66bbc91SGabor Kovesdan extern size_t ncpu;
58c66bbc91SGabor Kovesdan extern size_t nthreads;
59c66bbc91SGabor Kovesdan #endif
60c66bbc91SGabor Kovesdan 
61c66bbc91SGabor Kovesdan /*
62c66bbc91SGabor Kovesdan  * If true, we output some debug information.
63c66bbc91SGabor Kovesdan  */
64c66bbc91SGabor Kovesdan extern bool debug_sort;
65c66bbc91SGabor Kovesdan 
66c66bbc91SGabor Kovesdan /*
67c66bbc91SGabor Kovesdan  * MD5 context for random hash function
68c66bbc91SGabor Kovesdan  */
69c66bbc91SGabor Kovesdan extern MD5_CTX md5_ctx;
70c66bbc91SGabor Kovesdan 
71c66bbc91SGabor Kovesdan /*
72c66bbc91SGabor Kovesdan  * sort.c
73c66bbc91SGabor Kovesdan  */
74c66bbc91SGabor Kovesdan 
75c66bbc91SGabor Kovesdan /*
76c66bbc91SGabor Kovesdan  * This structure holds main sort options which are NOT affecting the sort ordering.
77c66bbc91SGabor Kovesdan  */
78c66bbc91SGabor Kovesdan struct sort_opts
79c66bbc91SGabor Kovesdan {
80*e8da8c74SGabor Kovesdan 	wint_t		field_sep;
81c66bbc91SGabor Kovesdan 	int		sort_method;
82c66bbc91SGabor Kovesdan 	bool		cflag;
83c66bbc91SGabor Kovesdan 	bool		csilentflag;
84c66bbc91SGabor Kovesdan 	bool		kflag;
85c66bbc91SGabor Kovesdan 	bool		mflag;
86c66bbc91SGabor Kovesdan 	bool		sflag;
87c66bbc91SGabor Kovesdan 	bool		uflag;
88c66bbc91SGabor Kovesdan 	bool		zflag;
89c66bbc91SGabor Kovesdan 	bool		tflag;
90c66bbc91SGabor Kovesdan 	bool		complex_sort;
91c66bbc91SGabor Kovesdan };
92c66bbc91SGabor Kovesdan 
93c66bbc91SGabor Kovesdan /*
94c66bbc91SGabor Kovesdan  * Key value structure forward declaration
95c66bbc91SGabor Kovesdan  */
96c66bbc91SGabor Kovesdan struct key_value;
97c66bbc91SGabor Kovesdan 
98c66bbc91SGabor Kovesdan /*
99c66bbc91SGabor Kovesdan  * Cmp function
100c66bbc91SGabor Kovesdan  */
101c66bbc91SGabor Kovesdan typedef int (*cmpcoll_t)(struct key_value *kv1, struct key_value *kv2, size_t offset);
102c66bbc91SGabor Kovesdan 
103c66bbc91SGabor Kovesdan /*
104c66bbc91SGabor Kovesdan  * This structure holds "sort modifiers" - options which are affecting the sort ordering.
105c66bbc91SGabor Kovesdan  */
106c66bbc91SGabor Kovesdan struct sort_mods
107c66bbc91SGabor Kovesdan {
108c66bbc91SGabor Kovesdan 	cmpcoll_t	func;
109c66bbc91SGabor Kovesdan 	bool		bflag;
110c66bbc91SGabor Kovesdan 	bool		dflag;
111c66bbc91SGabor Kovesdan 	bool		fflag;
112c66bbc91SGabor Kovesdan 	bool		gflag;
113c66bbc91SGabor Kovesdan 	bool		iflag;
114c66bbc91SGabor Kovesdan 	bool		Mflag;
115c66bbc91SGabor Kovesdan 	bool		nflag;
116c66bbc91SGabor Kovesdan 	bool		rflag;
117c66bbc91SGabor Kovesdan 	bool		Rflag;
118c66bbc91SGabor Kovesdan 	bool		Vflag;
119c66bbc91SGabor Kovesdan 	bool		hflag;
120c66bbc91SGabor Kovesdan };
121c66bbc91SGabor Kovesdan 
122c66bbc91SGabor Kovesdan extern bool need_hint;
123c66bbc91SGabor Kovesdan 
124c66bbc91SGabor Kovesdan extern struct sort_opts sort_opts_vals;
125c66bbc91SGabor Kovesdan 
126c66bbc91SGabor Kovesdan extern struct sort_mods * const default_sort_mods;
127c66bbc91SGabor Kovesdan 
128c66bbc91SGabor Kovesdan #endif /* __BSD_SORT_H__ */
129