2 * Copyright (C) 2009-2014 Erwin Waterlander
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice in the documentation and/or other materials provided with
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
20 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
21 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #ifndef __D2U_COMMON_H
28 #define __D2U_COMMON_H
30 /* define feature test macros for realpath() -- needed on */
31 /* systems that have S_ISLNK, but chicken/egg means we must */
32 /* define early, before including stdlib.h (or sys/stat.h) */
33 #define _XOPEN_SOURCE 500
35 /* similarly, instead of realpath we like to use, if */
36 /* available, the canonicalize_file_name() function, which */
37 /* is a GNU extension. We only ACTUALLY use the function if */
38 /* USE_CANONICALIZE_FILE_NAME is defined, but we don't define*/
39 /* that until later. So...define the feature test macro now. */
45 #define _(String) gettext (String)
46 #define gettext_noop(String) String
47 #define N_(String) gettext_noop (String)
51 #define _(String) (String)
52 #define N_(String) String
53 #define textdomain(Domain)
54 #define bindtextdomain(Package, Directory)
58 #if defined(__DJGPP__) || defined(__TURBOC__) /* DJGPP */
61 # if !defined(__MSYS__) && !defined(_MSC_VER)
65 #if !defined(__TURBOC__) && !defined(_MSC_VER)
74 # define strcmpi(s1, s2) strcasecmp(s1, s2)
78 # include <sys/utime.h>
92 #if (defined(__WATCOMC__) && defined(__NT__)) /* Watcom */
96 #if defined(__WATCOMC__) && defined(__I86__) /* Watcom C, 16 bit Intel */
100 #if defined(__WATCOMC__) && defined(__DOS__) /* Watcom C, 32 bit DOS */
104 #if defined(ENABLE_NLS) || (defined(D2U_UNICODE) && !defined(__MSDOS__) && !defined(_WIN32) && !defined(__OS2__))
105 /* setlocale() is also needed for nl_langinfo() */
109 #if defined(__TURBOC__) || defined(__DJGPP__) || defined(__MINGW32__) || defined(_MSC_VER)
110 /* Some compilers have no mkstemp().
111 * Use mktemp() instead.
112 * BORLANDC, DJGPP, MINGW32 */
116 #if defined(__TURBOC__) || defined(__DJGPP__) || defined(__MINGW32__) || defined(__WATCOMC__) || defined(_MSC_VER)
117 /* Some compilers have no chown(). */
121 /* Watcom C defines S_ISLNK */
126 /* Microsoft Visual C++ */
128 #define S_ISCHR( m ) (((m) & _S_IFMT) == _S_IFCHR)
129 #define S_ISDIR( m ) (((m) & _S_IFMT) == _S_IFDIR)
130 #define S_ISFIFO( m ) (((m) & _S_IFMT) == _S_IFIFO)
131 #define S_ISREG( m ) (((m) & _S_IFMT) == _S_IFREG)
132 #define NO_CHMOD 1 /* no chmod() available */
135 #if defined(__MSDOS__) || defined(_WIN32) || defined(__OS2__)
136 /* Systems without soft links use 'stat' instead of 'lstat'. */
142 #if defined(__MSDOS__) || defined(_WIN32) || defined(__OS2__)
143 /* On some systems rename() will always fail if target file already exists. */
144 #define NEED_REMOVE 1
147 #if defined(__MSDOS__) || defined(_WIN32) || defined(__CYGWIN__) || defined(__OS2__) /* DJGPP, MINGW32 and OS/2 */
148 /* required for setmode() and O_BINARY */
153 #if defined(__MSDOS__) || defined(_WIN32) || defined(__CYGWIN__) || defined(__OS2__)
161 #define BINARY_FILE 0x1
162 #define NO_REGFILE 0x2
163 #define WRONG_CODEPAGE 0x4
164 #define OUTPUTFILE_SYMLINK 0x8
165 #define INPUT_TARGET_NO_REGFILE 0x10
166 #define OUTPUT_TARGET_NO_REGFILE 0x20
167 #define LOCALE_NOT_UTF 0x40 /* Locale not an Unicode Transformation Format */
168 #define WCHAR_T_TOO_SMALL 0x80
169 #define UNICODE_CONVERSION_ERROR 0x100
171 #define CONVMODE_ASCII 0
172 #define CONVMODE_UTF16LE 1
173 #define CONVMODE_UTF16BE 2
174 #define CONVMODE_7BIT 3
175 #define CONVMODE_437 437
176 #define CONVMODE_850 850
177 #define CONVMODE_860 860
178 #define CONVMODE_863 863
179 #define CONVMODE_865 865
180 #define CONVMODE_1252 1252
182 #define FROMTO_DOS2UNIX 0
183 #define FROMTO_MAC2UNIX 1
184 #define FROMTO_UNIX2DOS 2
185 #define FROMTO_UNIX2MAC 3
188 #define INFO_UNIX 0x2
191 #define INFO_TEXT 0x10
192 #define INFO_DEFAULT 0x1F
193 #define INFO_CONVERT 0x20
195 #define SYMLINK_SKIP 0
196 #define SYMLINK_FOLLOW 1
197 #define SYMLINK_REPLACE 2
199 #define FILE_MBS 0 /* Multi-byte string or 8-bit char */
200 #define FILE_UTF16LE 1 /* UTF-16 Little Endian */
201 #define FILE_UTF16BE 2 /* UTF-16 Big Endian */
202 #define FILE_UTF8 3 /* UTF-8 */
203 #define FILE_GB18030 4 /* GB18030 */
205 /* locale conversion targets */
206 #define TARGET_UTF8 0
207 #define TARGET_GB18030 1
211 int NewFile; /* is in new file mode? */
212 int verbose; /* 0 = quiet, 1 = normal, 2 = verbose */
213 int KeepDate; /* should keep date stamp? */
214 int ConvMode; /* 0: ascii, 1: 7bit, 2: iso */
215 int FromToMode; /* 0: dos2unix/unix2dos, 1: mac2unix/unix2mac */
216 int NewLine; /* if TRUE, then additional newline */
217 int Force; /* if TRUE, force conversion of all files. */
218 int Follow; /* 0: skip symlink, 1: follow symbolic link, 2: replace symlink. */
220 int stdio_mode; /* if TRUE, stdio mode */
221 int error; /* an error occurred */
222 int bomtype; /* byte order mark */
223 int add_bom; /* 1: write BOM */
224 int keep_bom; /* 1: write BOM if input file has BOM. 0: Do not write BOM */
225 int keep_utf16; /* 1: write UTF-16 format when input file is UTF-16 format */
226 int file_info; /* 1: print file information */
227 int locale_target; /* locale conversion target. 0: UTF-8; 1: GB18030 */
231 int symbolic_link(const char *path);
232 int regfile(char *path, int allowSymlinks, CFlag *ipFlag, const char *progname);
233 int regfile_target(char *path, CFlag *ipFlag, const char *progname);
234 void PrintUsage(const char *progname);
235 void PrintBSDLicense(void);
236 void PrintVersion(const char *progname, const char *localedir);
238 void PrintLocaledir(const char *localedir);
240 FILE* OpenInFile(char *ipFN);
241 FILE* OpenOutFile(int fd);
242 #if defined(__TURBOC__) || defined(__MSYS__)
243 char *dirname(char *path);
246 FILE* MakeTempFileFrom(const char *OutFN, char **fname_ret);
248 int MakeTempFileFrom(const char *OutFN, char **fname_ret);
250 int ResolveSymbolicLink(char *lFN, char **rFN, CFlag *ipFlag, const char *progname);
251 FILE *read_bom (FILE *f, int *bomtype);
252 FILE *write_bom (FILE *f, CFlag *ipFlag, const char *progname);
253 void print_bom (const int bomtype, const char *filename, const char *progname);
254 int check_unicode(FILE *InF, FILE *TempF, CFlag *ipFlag, const char *ipInFN, const char *progname);
255 void print_messages_stdio(const CFlag *pFlag, const char *progname);
256 void print_messages_newfile(const CFlag *pFlag, const char *infile, const char *outfile, const char *progname, const int RetVal);
257 void print_messages_oldfile(const CFlag *pFlag, const char *infile, const char *progname, const int RetVal);
258 int ConvertNewFile(char *ipInFN, char *ipOutFN, CFlag *ipFlag, const char *progname,
259 int (*Convert)(FILE*, FILE*, CFlag *, const char *)
261 , int (*ConvertW)(FILE*, FILE*, CFlag *, const char *)
264 int ConvertStdio(CFlag *ipFlag, const char *progname,
265 int (*Convert)(FILE*, FILE*, CFlag *, const char *)
267 , int (*ConvertW)(FILE*, FILE*, CFlag *, const char *)
270 int parse_options(int argc, char *argv[], CFlag *pFlag, const char *localedir, const char *progname,
271 void (*PrintLicense)(void),
272 int (*Convert)(FILE*, FILE*, CFlag *, const char *)
274 , int (*ConvertW)(FILE*, FILE*, CFlag *, const char *)
278 wint_t d2u_getwc(FILE *f, int bomtype);
279 wint_t d2u_ungetwc(wint_t wc, FILE *f, int bomtype);
280 wint_t d2u_putwc(wint_t wc, FILE *f, CFlag *ipFlag);