Imported Upstream version 7.1
[platform/upstream/dos2unix.git] / common.h
1 /*
2  *   Copyright (C) 2009-2014 Erwin Waterlander
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
7  *   are met:
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
12  *      the distribution.
13  *
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.
25  */
26
27 #ifndef __D2U_COMMON_H
28 #define __D2U_COMMON_H
29
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
34
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. */
40 #define _GNU_SOURCE
41
42 #ifdef ENABLE_NLS
43
44 #include <libintl.h>
45 #define _(String) gettext (String)
46 #define gettext_noop(String) String
47 #define N_(String) gettext_noop (String)
48
49 #else
50
51 #define _(String) (String)
52 #define N_(String) String
53 #define textdomain(Domain)
54 #define bindtextdomain(Package, Directory)
55
56 #endif
57
58 #if defined(__DJGPP__) || defined(__TURBOC__) /* DJGPP */
59 #  include <dir.h>
60 #else
61 #  if !defined(__MSYS__) && !defined(_MSC_VER)
62 #    include <libgen.h>
63 #  endif
64 #endif
65 #if !defined(__TURBOC__) && !defined(_MSC_VER)
66 #include <unistd.h>
67 #endif
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #ifdef __GNUC__
72 #ifndef strcmpi
73 #  include <strings.h>
74 #  define strcmpi(s1, s2) strcasecmp(s1, s2)
75 #endif
76 #endif
77 #ifdef _MSC_VER
78 #  include <sys/utime.h>
79 #else
80 #  include <utime.h>
81 #endif
82 #include <limits.h>
83 #ifdef __TURBOC__
84 #define __FLAT__
85 #endif
86 #include <sys/stat.h>
87 #include <errno.h>
88 #ifdef D2U_UNICODE
89 #include <wchar.h>
90 #endif
91
92 #if (defined(__WATCOMC__) && defined(__NT__))  /* Watcom */
93 #  define _WIN32 1
94 #endif
95
96 #if defined(__WATCOMC__) && defined(__I86__) /* Watcom C, 16 bit Intel */
97 #define __MSDOS__ 1
98 #endif
99
100 #if defined(__WATCOMC__) && defined(__DOS__) /* Watcom C, 32 bit DOS */
101 #define __MSDOS__ 1
102 #endif
103
104 #if defined(ENABLE_NLS) || (defined(D2U_UNICODE) && !defined(__MSDOS__) && !defined(_WIN32) && !defined(__OS2__))
105 /* setlocale() is also needed for nl_langinfo() */
106 #include <locale.h>
107 #endif
108
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 */
113 #define NO_MKSTEMP 1
114 #endif
115
116 #if  defined(__TURBOC__) || defined(__DJGPP__) || defined(__MINGW32__) || defined(__WATCOMC__) || defined(_MSC_VER)
117 /* Some compilers have no chown(). */
118 #define NO_CHOWN 1
119 #endif
120
121 /* Watcom C defines S_ISLNK */
122 #ifdef __WATCOMC__
123 #undef S_ISLNK
124 #endif
125
126 /* Microsoft Visual C++ */
127 #ifdef _MSC_VER
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 */
133 #endif
134
135 #if defined(__MSDOS__) || defined(_WIN32) || defined(__OS2__)
136 /* Systems without soft links use 'stat' instead of 'lstat'. */
137 #define STAT stat
138 #else
139 #define STAT lstat
140 #endif
141
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
145 #endif
146
147 #if defined(__MSDOS__) || defined(_WIN32) || defined(__CYGWIN__) || defined(__OS2__) /* DJGPP, MINGW32 and OS/2 */
148 /* required for setmode() and O_BINARY */
149 #include <fcntl.h>
150 #include <io.h>
151 #endif
152
153 #if defined(__MSDOS__) || defined(_WIN32) || defined(__CYGWIN__) || defined(__OS2__)
154   #define R_CNTRL   "rb"
155   #define W_CNTRL   "wb"
156 #else
157   #define R_CNTRL   "r"
158   #define W_CNTRL   "w"
159 #endif
160
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_UTF8 0x40
168 #define WCHAR_T_TOO_SMALL 0x80
169 #define UNICODE_CONVERSION_ERROR 0x100
170
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
181
182 #define FROMTO_DOS2UNIX 0
183 #define FROMTO_MAC2UNIX 1
184 #define FROMTO_UNIX2DOS 2
185 #define FROMTO_UNIX2MAC 3
186
187 #define INFO_DOS  0x1
188 #define INFO_UNIX 0x2
189 #define INFO_MAC  0x4
190 #define INFO_BOM  0x8
191 #define INFO_TEXT 0x10
192 #define INFO_DEFAULT 0x1F
193 #define INFO_CONVERT 0x20
194
195 #define SYMLINK_SKIP 0
196 #define SYMLINK_FOLLOW 1
197 #define SYMLINK_REPLACE 2
198
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
204 typedef struct
205 {
206   int NewFile;                          /* is in new file mode? */
207   int verbose;                          /* 0 = quiet, 1 = normal, 2 = verbose */
208   int KeepDate;                         /* should keep date stamp? */
209   int ConvMode;                         /* 0: ascii, 1: 7bit, 2: iso */
210   int FromToMode;                       /* 0: dos2unix/unix2dos, 1: mac2unix/unix2mac */
211   int NewLine;                          /* if TRUE, then additional newline */
212   int Force;                            /* if TRUE, force conversion of all files. */
213   int Follow;                           /* 0: skip symlink, 1: follow symbolic link, 2: replace symlink. */
214   int status;
215   int stdio_mode;                       /* if TRUE, stdio mode */
216   int error;                            /* an error occurred */
217   int bomtype;                          /* byte order mark */
218   int add_bom;                          /* 1: write BOM */
219   int keep_bom;                         /* 1: write BOM if input file has BOM. 0: Do not write BOM */
220   int keep_utf16;                       /* 1: write UTF-16 format when input file is UTF-16 format */
221   int file_info;                        /* 1: print file information */
222 } CFlag;
223
224
225 int symbolic_link(const char *path);
226 int regfile(char *path, int allowSymlinks, CFlag *ipFlag, const char *progname);
227 int regfile_target(char *path, CFlag *ipFlag, const char *progname);
228 void PrintUsage(const char *progname);
229 void PrintBSDLicense(void);
230 void PrintVersion(const char *progname, const char *localedir);
231 #ifdef ENABLE_NLS
232 void PrintLocaledir(const char *localedir);
233 #endif
234 FILE* OpenInFile(char *ipFN);
235 FILE* OpenOutFile(int fd);
236 #if defined(__TURBOC__) || defined(__MSYS__)
237 char *dirname(char *path);
238 #endif
239 #ifdef NO_MKSTEMP
240 FILE* MakeTempFileFrom(const char *OutFN, char **fname_ret);
241 #else
242 int MakeTempFileFrom(const char *OutFN, char **fname_ret);
243 #endif
244 int ResolveSymbolicLink(char *lFN, char **rFN, CFlag *ipFlag, const char *progname);
245 FILE *read_bom (FILE *f, int *bomtype);
246 FILE *write_bom (FILE *f, CFlag *ipFlag, const char *progname);
247 void print_bom (const int bomtype, const char *filename, const char *progname);
248 int check_unicode(FILE *InF, FILE *TempF,  CFlag *ipFlag, const char *ipInFN, const char *progname);
249 void print_messages_stdio(const CFlag *pFlag, const char *progname);
250 void print_messages_newfile(const CFlag *pFlag, const char *infile, const char *outfile, const char *progname, const int RetVal);
251 void print_messages_oldfile(const CFlag *pFlag, const char *infile, const char *progname, const int RetVal);
252 int ConvertNewFile(char *ipInFN, char *ipOutFN, CFlag *ipFlag, const char *progname,
253                    int (*Convert)(FILE*, FILE*, CFlag *, const char *)
254 #ifdef D2U_UNICODE
255                  , int (*ConvertW)(FILE*, FILE*, CFlag *, const char *)
256 #endif
257                   );
258 int ConvertStdio(CFlag *ipFlag, const char *progname,
259                    int (*Convert)(FILE*, FILE*, CFlag *, const char *)
260 #ifdef D2U_UNICODE
261                  , int (*ConvertW)(FILE*, FILE*, CFlag *, const char *)
262 #endif
263                   );
264 int parse_options(int argc, char *argv[], CFlag *pFlag, const char *localedir, const char *progname,
265                   void (*PrintLicense)(void),
266                   int (*Convert)(FILE*, FILE*, CFlag *, const char *)
267 #ifdef D2U_UNICODE
268                 , int (*ConvertW)(FILE*, FILE*, CFlag *, const char *)
269 #endif
270                   );
271 #ifdef D2U_UNICODE
272 wint_t d2u_getwc(FILE *f, int bomtype);
273 wint_t d2u_ungetwc(wint_t wc, FILE *f, int bomtype);
274 wint_t d2u_putwc(wint_t wc, FILE *f, CFlag *ipFlag);
275 #endif
276
277 #endif