| 1 |
/* |
| 2 |
* Copyright (c) 2002-2022 the xdvik development team |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
* of this software and associated documentation files (the "Software"), to |
| 6 |
* deal in the Software without restriction, including without limitation the |
| 7 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 |
* sell copies of the Software, and to permit persons to whom the Software is |
| 9 |
* furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 |
* PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE LIABLE FOR ANY CLAIM, |
| 18 |
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 |
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifndef UTIL_H_ |
| 24 |
#define UTIL_H_ |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include "version.h" |
| 28 |
#include "kpathsea/c-stat.h" |
| 29 |
#include "kpathsea/hash.h" |
| 30 |
#include "kpathsea/tex-file.h" |
| 31 |
|
| 32 |
#if HAVE_POLL |
| 33 |
# include <poll.h> |
| 34 |
# define XIO_IN POLLIN |
| 35 |
# define XIO_OUT POLLOUT |
| 36 |
#else |
| 37 |
# if HAVE_SYS_SELECT_H |
| 38 |
# include <sys/select.h> |
| 39 |
# else |
| 40 |
# if HAVE_SELECT_H |
| 41 |
# include <select.h> |
| 42 |
# endif |
| 43 |
# endif |
| 44 |
# define XIO_IN 1 |
| 45 |
# define XIO_OUT 2 |
| 46 |
#endif |
| 47 |
|
| 48 |
#include "events.h" /* for child proc stuff */ |
| 49 |
|
| 50 |
FILE *try_fopen(const char *fname, const char *mode); |
| 51 |
FILE *try_fdopen(int fd, const char *mode); |
| 52 |
int try_open(const char *fname, int flags); |
| 53 |
int try_open_mode(const char *fname, int flags, mode_t mode); |
| 54 |
int try_dup(int oldfd); |
| 55 |
|
| 56 |
extern int xdvi_temp_fd(char **tempfilename); |
| 57 |
extern void xdvi_assert(const char *version, |
| 58 |
const char *filename, |
| 59 |
int lineno, |
| 60 |
Boolean condition, |
| 61 |
const char *fmt, |
| 62 |
...); |
| 63 |
|
| 64 |
typedef void (*child_exited_proc)(int status, struct xchild *this); |
| 65 |
|
| 66 |
extern void handle_child_exit(int status, struct xchild *this); |
| 67 |
extern char *read_child_error(int fd, void *data); |
| 68 |
extern Boolean fork_process(const char *file, Boolean redirect_stdout, |
| 69 |
const char *dirname, |
| 70 |
childProcT exit_proc, void *data, |
| 71 |
int killsig, |
| 72 |
char *const argv[]); |
| 73 |
extern void prep_fd(int fd, wide_bool noblock); |
| 74 |
|
| 75 |
struct bitmap; /* forward declaration */ |
| 76 |
extern void alloc_bitmap(struct bitmap *); |
| 77 |
|
| 78 |
extern char *my_realpath(const char *path, char *real); |
| 79 |
#ifdef HAVE_REALPATH |
| 80 |
#include <limits.h> |
| 81 |
# define REALPATH realpath |
| 82 |
#else |
| 83 |
# define REALPATH my_realpath |
| 84 |
#endif |
| 85 |
|
| 86 |
char *expand_homedir(const char *path); |
| 87 |
void set_dvi_name_expand(const char *new_filename); |
| 88 |
void set_dvi_name(char *new_filename); |
| 89 |
FILE *XFOPEN(const char *path, const char *mode); |
| 90 |
|
| 91 |
|
| 92 |
#ifndef HAVE_MEMICMP |
| 93 |
extern int memicmp(const char *, const char *, size_t); |
| 94 |
#endif |
| 95 |
|
| 96 |
/* NOTE: all of the following are already defined by kpathsea. */ |
| 97 |
/* extern void *xmalloc(unsigned); */ |
| 98 |
/* extern void *xrealloc(void *, unsigned); */ |
| 99 |
/* extern char *xstrdup(const char *); */ |
| 100 |
/* extern void xputenv(const char *, const char *); */ |
| 101 |
|
| 102 |
extern char *xmemdup(const char *, size_t); |
| 103 |
|
| 104 |
/* like xstrdup, but only copy len characters and zero-terminate at next index (allocates len+1 characters) */ |
| 105 |
extern char *xstrndup(const char *str, size_t len); |
| 106 |
|
| 107 |
extern char *xt_strdup(const char *); /* like xstrdup, but with XtMalloc() */ |
| 108 |
|
| 109 |
extern char *xstrcat(char *str1, const char *str2); |
| 110 |
extern int xpipe(int *); |
| 111 |
extern void close_a_file(void); |
| 112 |
extern unsigned long get_bytes(FILE *, int); |
| 113 |
extern long get_lbytes(FILE *, int); |
| 114 |
|
| 115 |
extern void do_abort(void); |
| 116 |
|
| 117 |
/* various levels of warning/error messages */ |
| 118 |
extern void xdvi_info(const char *fmt, ...); |
| 119 |
extern void xdvi_warning(const char *fmt, ...); |
| 120 |
extern void xdvi_error(const char *fmt, ...); |
| 121 |
extern void xdvi_fatal(const char *fmt, ...); |
| 122 |
extern void xdvi_abort(const char *fmt, ...); |
| 123 |
|
| 124 |
extern Boolean pointerlocate(int *, int *); |
| 125 |
extern unsigned long parse_debugging_string(const char *arg); |
| 126 |
extern unsigned long parse_debugging_option(const char *ptr); |
| 127 |
|
| 128 |
extern int get_avg_font_width(XFontStruct *font); |
| 129 |
extern char **split_line(const char *line, char sep, size_t begin, size_t end, size_t *ret_items); |
| 130 |
extern char *find_file(const char *filename, struct stat *statbuf, kpse_file_format_type pathinfo); |
| 131 |
extern char **src_format_arguments(char **argv, const char *filename, int lineno, int colno); |
| 132 |
|
| 133 |
/* |
| 134 |
hashtable wrapper functions, mostly used by dvi-draw.c to |
| 135 |
map filenames to integers. This uses the hashtable implementation |
| 136 |
from kpathsea, which is reasonably fast. |
| 137 |
*/ |
| 138 |
|
| 139 |
/* |
| 140 |
We use this dummy wrapper stuct, which we cast to void *, to get integer |
| 141 |
values into/from the hashtable (natively, kpahtsea only supports string |
| 142 |
values). |
| 143 |
*/ |
| 144 |
struct str_int_hash_item { |
| 145 |
int value; |
| 146 |
}; |
| 147 |
|
| 148 |
|
| 149 |
typedef hash_table_type hashTableT; /* from kpathsea */ |
| 150 |
extern Boolean find_str_int_hash(hashTableT *hashtable, const char *key, size_t *val); |
| 151 |
extern void put_str_int_hash(hashTableT *hashtable, const char *key, size_t val); |
| 152 |
|
| 153 |
|
| 154 |
/* |
| 155 |
* AVL tree structures. |
| 156 |
*/ |
| 157 |
|
| 158 |
#define AVL_COMMON \ |
| 159 |
const char *key; /* key */ \ |
| 160 |
int key_len; /* length of key */ \ |
| 161 |
int bal; /* AVL balancing information */ \ |
| 162 |
struct avl *left; \ |
| 163 |
struct avl *right |
| 164 |
|
| 165 |
struct avl { /* generic data structure */ |
| 166 |
AVL_COMMON; |
| 167 |
}; |
| 168 |
|
| 169 |
extern struct avl *avladd(const char *, size_t, struct avl **, size_t); |
| 170 |
|
| 171 |
|
| 172 |
extern Boolean copy_file(const char *from, const char *to); |
| 173 |
extern Boolean copy_fp(FILE *in, FILE *out); |
| 174 |
|
| 175 |
extern const char *get_text_encoding(void); |
| 176 |
extern char *iconv_convert_string(const char *from_enc, const char *to_enc, const char *str); |
| 177 |
|
| 178 |
extern void xdvi_bell(void); |
| 179 |
|
| 180 |
/* Various error reporting macros. |
| 181 |
The reasons why these are macros are: |
| 182 |
- possibility to use __FILE__, __LINE__ |
| 183 |
- (more importantly:) gcc can't do type checking on generic vararg |
| 184 |
macros, but does so for the printf() functions. |
| 185 |
*/ |
| 186 |
|
| 187 |
/* |
| 188 |
* Print an informative message to stdout, |
| 189 |
* unless the resource `hush_stdout' is set. |
| 190 |
*/ |
| 191 |
#define XDVI_INFO(X) do { \ |
| 192 |
fprintf(stdout, "%s: Info: ", globals.program_name); \ |
| 193 |
fflush(stdout); /* in case following goes to stderr accidentally ... */ \ |
| 194 |
fprintf X; \ |
| 195 |
fprintf(stdout, "\n"); \ |
| 196 |
fflush(stdout); /* to make sure it doesn't get intermingled with stderr */ \ |
| 197 |
} while(0) |
| 198 |
|
| 199 |
|
| 200 |
/* |
| 201 |
* Print a warning message to stderr. |
| 202 |
* This can't be shut off by `hush_stdout'. |
| 203 |
*/ |
| 204 |
#define XDVI_WARNING(X) do { \ |
| 205 |
fprintf(stderr, "%s: Warning: ", globals.program_name); \ |
| 206 |
fprintf X; \ |
| 207 |
fprintf(stderr, "\n"); \ |
| 208 |
} while(0) |
| 209 |
|
| 210 |
/* |
| 211 |
* Print an un-typed message to stderr, without starting a newline. |
| 212 |
* This can't be shut off by `hush_stdout'. |
| 213 |
*/ |
| 214 |
#define XDVI_MSG(X) do { \ |
| 215 |
fprintf(stderr, "%s: ", globals.program_name); \ |
| 216 |
fprintf X; \ |
| 217 |
} while(0) |
| 218 |
|
| 219 |
|
| 220 |
/* |
| 221 |
* Print an error message to stderr. |
| 222 |
* This can't be shut off by `hush_stdout'. |
| 223 |
*/ |
| 224 |
#define XDVI_ERROR(X) do { \ |
| 225 |
fprintf(stderr, "%s: Error: ", globals.program_name); \ |
| 226 |
fprintf X; \ |
| 227 |
fprintf(stderr, "\n"); \ |
| 228 |
} while(0) |
| 229 |
|
| 230 |
/* |
| 231 |
* Print an error message and quit. Use this only with extreme care, |
| 232 |
* if recovery is really impossible! |
| 233 |
*/ |
| 234 |
#define XDVI_FATAL(X) do { \ |
| 235 |
fprintf(stderr, "%s: Fatal error: ", globals.program_name); \ |
| 236 |
fprintf X; \ |
| 237 |
fprintf(stderr, "\n"); \ |
| 238 |
xdvi_exit(EXIT_FAILURE); \ |
| 239 |
} while(0) |
| 240 |
|
| 241 |
/* |
| 242 |
* Print an error message and abort. Use this instead of assertions |
| 243 |
* if you want to give a more informative message. |
| 244 |
*/ |
| 245 |
#define XDVI_ABORT(X) do { \ |
| 246 |
fprintf(stderr, "%s %s: %s:%d: Shouldn't happen: ", \ |
| 247 |
globals.program_name, \ |
| 248 |
XDVI_VERSION_INFO, \ |
| 249 |
__FILE__, __LINE__); \ |
| 250 |
fprintf X; \ |
| 251 |
fprintf(stderr, "\n"); \ |
| 252 |
do_abort(); \ |
| 253 |
} while(0) |
| 254 |
|
| 255 |
#endif /* UTIL_H_ */ |