1 #ifndef COMMON_H
2 #define COMMON_H 1
3 
4 #ifndef PACKAGE_NAME
5 #  ifdef HAVE_CONFIG_H
6 #    include "config.h"
7 #  else /* HAVE_CONFIG_H */
8 #    define PACKAGE_NAME "Enca"
9 #    define PACKAGE_TARNAME "enca"
10 #    define PACKAGE_VERSION ""
11 #    define DEFAULT_EXTERNAL_CONVERTER ""
12 #    define DEFAULT_CONVERTER_LIST "built-in"
13 #  endif /* HAVE_CONFIG_H */
14 #endif /* not PACKAGE_NAME */
15 
16 #include <ctype.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <sys/types.h>
20 #include <assert.h>
21 #include <stdio.h>
22 
23 #include "../lib/internal.h"
24 
25 /* define or correctly redefine EXIT_* values */
26 #if !(defined EXIT_SUCCESS) || (EXIT_SUCCESS != 0) || (EXIT_FAILURE == 0)
27 #  define EXIT_SUCCESS 0
28 #  define EXIT_FAILURE 1
29 #endif /* !(defined EXIT_SUCCESS) || (EXIT_SUCCESS != 0) || (EXIT_FAILURE == 0) */
30 #define EXIT_TROUBLE 2
31 
32 /* str- an mem- function, theoretically they are all in string.h */
33 #ifdef HAVE_STRING_H
34 #  include <string.h>
35 #else /* HAVE_STRING_H */
36 #  ifdef HAVE_STRINGS_H
37 #    include <strings.h>
38 #  endif /* HAVE_STRINGS_H */
39 #endif /* HAVE_STRING_H */
40 
41 #ifdef HAVE_MEMORY_H
42 #  include <memory.h>
43 #endif /* HAVE_MEMORY_H */
44 
45 #ifdef HAVE_ERRNO_H
46 #  include <errno.h>
47 #else /* HAVE_ERRNO_H */
48 extern int errno;
49 #endif /* HAVE_ERRNO_H */
50 
51 /* portable isatty(), assume never on tty when neither isatty/ttyname is
52    available */
53 #ifdef HAVE_ISATTY
54 #  define enca_isatty(fd) (isatty(fd))
55 #elif HAVE_TTYNAME
56 #  define enca_isatty (ttyname(fd) != NULL)
57 #else /* HAVE_ISATTY || HAVE_TTYNAME */
58 #  define enca_isatty (0)
59 #endif /* HAVE_ISATTY || HAVE_TTYNAME */
60 
61 /* make sure STDIN_FILENO and STDOUT_FILENO are defined */
62 #ifndef STDIN_FILENO
63 #  define STDIN_FILENO 0
64 #endif /* not STDIN_FILENO */
65 
66 #ifndef STDOUT_FILENO
67 #  define STDOUT_FILENO 1
68 #endif /* not STDOUT_FILENO */
69 
70 /* Conversion error codes:
71    ok
72    conversion between these encodings is not possible
73    i/o failure,
74    child died
75    converter library is cheating
76    malformed input,
77    cannot exec external converter
78 
79    FIXME: this is an ISO C violation E[a-z0-9]+ are reserved for error names
80  */
81 typedef enum {
82   ERR_OK =  0,
83   ERR_CANNOT,
84   ERR_IOFAIL,
85   ERR_CHILD,
86   ERR_LIBCOM,
87   ERR_MALFORM,
88   ERR_EXEC = 15
89 } ConvertErrorCode;
90 
91 /* Output type. */
92 typedef enum {
93   OTYPE_CS2CS = 0,
94   OTYPE_RFC1345,
95   OTYPE_HUMAN,
96   OTYPE_DETAILS,
97   OTYPE_CANON,
98   OTYPE_ICONV,
99   OTYPE_MIME,
100   OTYPE_CONVERT,
101   OTYPE_ALIASES
102 } OutputType;
103 
104 typedef void *pointer; /* untyped pointer */
105 typedef const void *cpointer; /* constant untyped pointer */
106 typedef unsigned char byte; /* byte */
107 
108 /* Forward type declarations. */
109 typedef struct _Abbreviation Abbreviation;
110 typedef struct _Buffer Buffer;
111 typedef struct _File File;
112 typedef struct _Options Options;
113 
114 /* Struct abbreviation. */
115 struct _Abbreviation {
116   const char *name; /* full name */
117   cpointer data; /* corresponding value */
118 };
119 
120 /* Struct I/O buffer. */
121 struct _Buffer {
122   size_t size; /* buffer size */
123   ssize_t pos; /* position in buffer */
124   byte *data; /* the buffer itself buffer */
125 };
126 
127 /* Struct file stream. */
128 struct _File {
129   char *name; /* file name, NULL if stdin/stdout */
130   Buffer *buffer; /* buffer for i/o operations */
131   FILE *stream; /* the stream, NULL when not opened */
132   off_t size; /* file size, nonsense when name == NULL */
133 };
134 
135 /* Struct options. */
136 struct _Options {
137   int verbosity_level; /* Verbosity level. */
138   char *language; /* Language of analysed files. */
139   OutputType output_type; /* What kind of action is expected after guess. */
140   EncaEncoding target_enc; /* Target encoding for conversion. */
141   char *target_enc_str; /* How user specified the target encoding. */
142   int prefix_filename; /* Do prepend filename: before results? */
143 };
144 
145 /* Enca options. */
146 extern Options options;
147 /* Path-stripped argv[0] for everybody. */
148 extern char *program_name;
149 /* size of the main i/o buffer */
150 extern size_t buffer_size;
151 
152 /* Function prototypes. */
153 Buffer *buffer_new(size_t size);
154 void    buffer_free(Buffer *buf);
155 
156 const Abbreviation* expand_abbreviation(const char *name,
157                                         const Abbreviation *atable,
158                                         size_t size,
159                                         const char *object_name);
160 
161 const char* ffname_r(const char *fname);
162 const char* ffname_w(const char *fname);
163 ssize_t     file_read(File *file);
164 byte*       file_getline(File *file);
165 ssize_t     file_write(File *file);
166 File*       file_temporary(Buffer *buffer,
167                            int ulink);
168 off_t       file_seek(File *file,
169                       off_t offset,
170                       int whence);
171 int         file_truncate(File *file,
172                           off_t length);
173 int         file_unlink(const char *fname);
174 int         file_open(File *file,
175                       const char *mode);
176 int         file_close(File *file);
177 File*       file_new(const char *fname,
178                      Buffer *buffer);
179 void        file_free(File *file);
180 char**      process_opt(int argc,
181                         char *argv[]);
182 void print_aliases(size_t cs);
183 OutputType get_output_type(void);
184 
185 int         copy_and_convert       (File *file_from,
186                                     File *file_to,
187                                     const byte *xlat);
188 const char* format_request_string  (EncaEncoding e1,
189                                     EncaEncoding e2,
190                                     EncaSurface mask);
191 int         convert                (File *file,
192                                     EncaEncoding from_enc);
193 int         add_converter          (const char *cname);
194 int         external_converter_listed (void);
195 void        print_converter_list   (void);
196 void        set_requested_enc      (EncaEncoding enc);
197 
198 #ifdef HAVE_LIBRECODE
199 int         convert_recode         (File *file,
200                                     EncaEncoding from_enc);
201 #endif /* HAVE_LIBRECODE */
202 
203 #ifdef HAVE_GOOD_ICONV
204 int         convert_iconv          (File *file,
205                                     EncaEncoding from_enc);
206 #endif /* HAVE_GOOD_ICONV */
207 
208 #ifdef ENABLE_EXTERNAL
209 int         convert_external         (File *file,
210                                       EncaEncoding from_enc);
211 void        set_external_converter   (const char *extc);
212 int         check_external_converter (void);
213 #endif /* ENABLE_EXTERNAL */
214 
215 char*       detect_lang            (const char *lang);
216 #ifdef HAVE_NL_LANGINFO
217 const char* get_lang_codeset       (void);
218 #endif /* HAVE_NL_LANGINFO */
219 
220 #endif /* not COMMON_H */
221 /* vim: ts=2
222  */
223