1 /*
2  * Copyright (c) 2016,2017,2019 Daichi GOTO
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef _TTT_H_
29 #define _TTT_H_
30 
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdbool.h>
37 #include <string.h>
38 #include <sysexits.h>
39 #include <locale.h>
40 #include <ctype.h>
41 #include <time.h>
42 #include <errno.h>
43 #include <err.h>
44 
45 #define BUFFER_SIZE 			4096
46 #define STDIN_FILE			"/dev/stdin"
47 
48 #define CMDARGS_DEFAULT			0x00000000
49 #define CMDARGS_R_NONE			0x00000001
50 #define CMDARGS_R_NEED			0x00000002
51 #define CMDARGS_R_ARGARG_NONE		0x00000004
52 #define CMDARGS_R_ARGARG_1_NEED		0x00000008
53 #define CMDARGS_R_ARGARG_2_NEED		0x00000010
54 #define CMDARGS_R_ARGARG_3_NEED		0x00000020
55 #define CMDARGS_R_ARGARG_TO_SSVSTR	0x00000040
56 #define CMDARGS_F_NONE			0x00000100
57 #define CMDARGS_F_NEED			0x00000200
58 #define CMDARGS_STDIN_TO_TMPFILE	0x00000400
59 #define CMDARGS_A_NONE			0x00001000
60 #define CMDARGS_A_NEED			0x00002000
61 #define CMDARGS_R_MINIMUMNUM_IS_0	0x00010000
62 #define CMDARGS_R_MINIMUMNUM_IS_1	0x00020000
63 #define CMDARGS_DELIMITER_ONLY_1	0x00040000
64 #define CMDARGS_DELIMITER_ONLY_2	0x00100000
65 #define CMDARGS_DELIMITER_ONLY_3	0x00200000
66 
67 struct tttcmdargs {
68 	char	*cmdname;
69 	int	flags[63];
70 	char	*flags_arg[63];
71 	int 	r_argc;
72 	int	r_argv_max;
73 	int	*r_argv;
74 	int	r_index_max;
75 	int	*r_index_exist;
76 	int	*r_index_to_argv;
77 	char	**r_argv_arg1;
78 	char	**r_argv_arg2;
79 	char	**r_argv_arg3;
80 	char	*r_argv_delim;
81 	int	f_argc;
82 	char	**f_argv;
83 	int	a_argc;
84 	char	**a_argv;
85 };
86 
87 extern struct tttcmdargs cmdargs;
88 
89 #define TEXTSET_END			{ NULL, NULL, NULL }
90 
91 struct textset {
92 	char	*key;
93 	char	*locale;
94 	char	*text;
95 };
96 
97 extern struct textset deftextsets[];
98 extern struct textset cmdtextsets[] __attribute__((__weak__));
99 extern struct textset *textsets[];
100 
101 struct ssvline {
102 	char		**data;
103 	int		nf;
104 	struct ssvline	*next;
105 	int		outputed;
106 };
107 
108 int getcmdargs(const int, char *[], const char *, int);
109 int getcmdargs_unlinktmpf(void);
110 char *gettext(const char *);
111 int gyo(const char *);
112 int settext(const char *, const char *, const char *);
113 int settime(struct tm *, const char *, const char *);
114 void ssvfile2ssvlines(const char *, struct ssvline *);
115 char *ssvstr2str(char *, char *);
116 int ssvstr2str_len(char *);
117 char *str2ssvstr(char *, char *);
118 int str2ssvstr_len(char *);
119 int gettimestr(char [], const char *, struct tm *);
120 void usage(void);
121 void printcmdargs(void);
122 void version(void);
123 
124 char *_str2ssvstr(char *);
125 char *_ssvstr2str(char *);
126 char *_ltgt2charref(char *);
127 char *_quote2charref(char *);
128 
129 #include "ttt_aliases.h"
130 #include "ttt_text.h"
131 #include "ttt_utils.h"
132 #include "ttt_debug.h"
133 
134 #ifdef __linux__
135 #include <bsd/stdlib.h>
136 #endif
137 
138 #endif /* !_TTT_H_ */
139