Back to index

tetex-bin  3.0
util.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2002-2004 the xdvik development team
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to
00006  * deal in the Software without restriction, including without limitation the
00007  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
00008  * sell copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
00017  * PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE LIABLE FOR ANY CLAIM,
00018  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020  * OTHER DEALINGS IN THE SOFTWARE.
00021  */
00022 
00023 #ifndef UTIL_H_
00024 #define UTIL_H_
00025 
00026 #include <stdio.h>
00027 #include "xdvi-config.h"
00028 #include "version.h"
00029 #include "kpathsea/c-stat.h"
00030 #include "kpathsea/hash.h"
00031 #include "kpathsea/tex-file.h"
00032 
00033 #if HAVE_POLL
00034 # include <poll.h>
00035 # define XIO_IN POLLIN
00036 # define XIO_OUT POLLOUT
00037 #else
00038 # if HAVE_SYS_SELECT_H
00039 #  include <sys/select.h>
00040 # else
00041 #  if HAVE_SELECT_H
00042 #   include <select.h>
00043 #  endif
00044 # endif
00045 # define XIO_IN 1
00046 # define XIO_OUT 2
00047 #endif
00048 
00049 #include "events.h" /* for child proc stuff */
00050 
00051 /* double linked list */
00052 struct dl_list {
00053     void *item;
00054     struct dl_list *prev;
00055     struct dl_list *next;
00056 };
00057 
00058 struct dl_list *dl_list_head(struct dl_list *list);
00059 struct dl_list *dl_list_push_front(struct dl_list *list, void *item);
00060 struct dl_list *dl_list_insert(struct dl_list *list, void *item);
00061 struct dl_list *dl_list_truncate(struct dl_list *list);
00062 struct dl_list *dl_list_truncate_head(struct dl_list *list);
00063 struct dl_list *dl_list_remove(struct dl_list *list,
00064                             const void *elem,
00065                             int *count,
00066                             void **item,
00067                             Boolean (*compare_func)(const void *item1, const void *item2));
00068 Boolean dl_list_remove_item(struct dl_list **list);
00069 FILE *try_fopen(const char *fname, const char *mode);
00070 FILE *try_fdopen(int fd, const char *mode);
00071 int try_open(const char *fname, int flags);
00072 int try_open_mode(const char *fname, int flags, mode_t mode);
00073 
00074 extern int xdvi_temp_fd(char **tempfilename);
00075 
00076 
00077 typedef void (*child_exited_proc)(int status, struct xchild *this);
00078 
00079 extern void handle_child_exit(int status, struct xchild *this);
00080 extern char *read_child_error(int fd);
00081 extern Boolean fork_process(const char *file, Boolean redirect_stdout,
00082                          const char *dirname,
00083                          childProcT exit_proc, void *data,
00084                          char *const argv[]);
00085 extern void prep_fd(int fd, wide_bool noblock);
00086 
00087 struct bitmap; /* forward declaration */
00088 extern void alloc_bitmap(struct bitmap *);
00089 
00090 extern char *my_realpath(const char *path, char *real);
00091 #ifdef HAVE_REALPATH
00092 #include <limits.h>
00093 # define REALPATH realpath
00094 #else
00095 # define REALPATH my_realpath
00096 #endif
00097 
00098 char *expand_homedir(const char *path);
00099 void set_dvi_name_expand(const char *new_filename);
00100 void set_dvi_name(char *new_filename);
00101 FILE *XFOPEN(const char *path, const char *mode);
00102 
00103 
00104 #ifndef HAVE_MEMICMP
00105 extern int memicmp(const char *, const char *, size_t);
00106 #endif
00107 
00108 /* NOTE: all of the following are already defined by kpathsea. */
00109 /*  extern void *xmalloc(unsigned); */
00110 /*  extern void *xrealloc(void *, unsigned); */
00111 /*  extern char *xstrdup(const char *); */
00112 /*  extern char *xmemdump(const char *, size_t); */
00113 /*  extern void xputenv(const char *, const char *); */
00114 
00115 extern char *xstrcat(char *str1, const char *str2);
00116 extern int xpipe(int *);
00117 extern void close_a_file(void);
00118 extern unsigned long get_bytes(FILE *, int);
00119 extern long get_lbytes(FILE *, int);
00120 
00121 extern void xdvi_exit(int);
00122 extern void do_abort(void);
00123 
00124 /* various levels of warning/error messages */
00125 extern void xdvi_info(const char *fmt, ...);
00126 extern void xdvi_warning(const char *fmt, ...);
00127 extern void xdvi_error(const char *fmt, ...);
00128 extern void xdvi_fatal(const char *fmt, ...);
00129 extern void xdvi_abort(const char *fmt, ...);
00130 
00131 extern Boolean pointerlocate(int *, int *);
00132 extern unsigned long parse_debugging_string(const char *arg);
00133 extern unsigned long parse_debugging_option(const char *ptr);
00134 
00135 extern int get_avg_font_width(XFontStruct *font);
00136 extern char **split_line(const char *line, char sep, size_t begin, size_t end, size_t *ret_items);
00137 extern char *find_file(const char *filename, struct stat *statbuf, kpse_file_format_type pathinfo);
00138 
00139 /*
00140   hashtable wrapper functions, mostly used by dvi-draw.c to
00141   map filenames to integers. This uses the hashtable implementation
00142   from kpathsea, which is reasonably fast.
00143 */
00144 
00145 /*
00146   We use this dummy wrapper stuct, which we cast to void *, to get integer
00147   values into/from the hashtable (natively, kpahtsea only supports string
00148   values).
00149 */
00150 struct str_int_hash_item {
00151     int value;
00152 };
00153 
00154 
00155 typedef hash_table_type hashTableT; /* from kpathsea */
00156 extern Boolean find_str_int_hash(hashTableT *hashtable, const char *key, size_t *val);
00157 void put_str_int_hash(hashTableT *hashtable, const char *key, size_t val);
00158 
00159 Boolean copy_file(const char *from, const char *to);
00160 Boolean copy_fp(FILE *in, FILE *out);
00161 
00162 extern const char *get_text_encoding(void);
00163 extern char *iconv_convert_string(const char *from_enc, const char *to_enc, const char *str);
00164 
00165 
00166 /* Various error reporting macros.
00167    The reasons why these are macros are:
00168    - possibility to use __FILE__, __LINE__
00169    - (more importantly:) gcc can't do type checking on generic vararg
00170      macros, but does so for the printf() functions.
00171 */
00172 
00173 /*
00174  *     Print an informative message to stdout,
00175  *     unless the resource `hush_stdout' is set.
00176  */
00177 #define XDVI_INFO(X) do {                               \
00178        fprintf(stdout, "%s: Info: ", globals.program_name);    \
00179        fflush(stdout); /* in case following goes to stderr accidentally ... */ \
00180        fprintf X;                                       \
00181        fprintf(stdout, "\n");                                  \
00182        fflush(stdout); /* to make sure it doesn't get intermingled with stderr */ \
00183 } while(0)
00184 
00185 
00186 /*
00187  *     Print a warning message to stderr.
00188  *     This can't be shut off by `hush_stdout'.
00189  */
00190 #define XDVI_WARNING(X) do {                                   \
00191        fprintf(stderr, "%s: Warning: ", globals.program_name); \
00192        fprintf X;                                       \
00193        fprintf(stderr, "\n");                                  \
00194 } while(0)
00195 
00196 /*
00197  *     Print an un-typed message to stderr, without starting a newline.
00198  *     This can't be shut off by `hush_stdout'.
00199  */
00200 #define XDVI_MSG(X) do {                                \
00201        fprintf(stderr, "%s: ", globals.program_name);          \
00202        fprintf X;                                       \
00203 } while(0)
00204 
00205 
00206 /*
00207  *     Print an error message to stderr.
00208  *     This can't be shut off by `hush_stdout'.
00209  */
00210 #define XDVI_ERROR(X) do {                              \
00211        fprintf(stderr, "%s: Error: ", globals.program_name);   \
00212        fprintf X;                                       \
00213        fprintf(stderr, "\n");                                  \
00214 } while(0)
00215 
00216 /*
00217  *     Print an error message and quit. Use this only with extreme care,
00218  *     if recovery is really impossible!
00219  */
00220 #define XDVI_FATAL(X) do {                                     \
00221        fprintf(stderr, "%s: Fatal error: ", globals.program_name);    \
00222        fprintf X;                                              \
00223        fprintf(stderr, "\n");                                         \
00224        xdvi_exit(EXIT_FAILURE);                                \
00225 } while(0) 
00226 
00227 /*
00228  *     Print an error message and abort. Use this instead of assertions
00229  *     if you want to give a more informative message.
00230  */
00231 #define XDVI_ABORT(X) do {                              \
00232        fprintf(stderr, "%s %s: %s:%d: Shouldn't happen: ",     \
00233                      globals.program_name,                     \
00234                      XDVI_VERSION_INFO,                 \
00235                      __FILE__, __LINE__);               \
00236        fprintf X;                                       \
00237        fprintf(stderr, "\n");                                  \
00238        do_abort();                                      \
00239 } while(0)
00240 
00241 #endif /* UTIL_H_ */