diff options
-rw-r--r-- | apps/common/pdfapp.c | 15 | ||||
-rw-r--r-- | apps/pdfclean.c | 9 | ||||
-rw-r--r-- | apps/pdfdraw.c | 11 | ||||
-rw-r--r-- | apps/pdfinfo.c | 17 | ||||
-rw-r--r-- | apps/pdfshow.c | 9 | ||||
-rw-r--r-- | apps/unix/x11pdf.c | 2 | ||||
-rw-r--r-- | fitz/Jamfile | 3 | ||||
-rw-r--r-- | fitz/base_error.c | 72 | ||||
-rw-r--r-- | fitz/base_memory.c | 8 | ||||
-rw-r--r-- | fitz/base_string.c | 63 | ||||
-rw-r--r-- | fitz/fitz_base.h | 60 | ||||
-rw-r--r-- | fitz/fitz_stream.h | 10 | ||||
-rw-r--r-- | fitz/stm_filter.c | 4 | ||||
-rw-r--r-- | fitz/stm_open.c | 7 | ||||
-rw-r--r-- | fitz/util_strlcpy.c | 65 | ||||
-rw-r--r-- | fitz/util_strsep.c | 11 | ||||
-rw-r--r-- | mupdf/cmapdump.c | 6 | ||||
-rw-r--r-- | mupdf/pdf_colorspace1.c | 3 | ||||
-rw-r--r-- | mupdf/pdf_font.c | 7 | ||||
-rw-r--r-- | mupdf/pdf_stream.c | 3 |
20 files changed, 134 insertions, 251 deletions
diff --git a/apps/common/pdfapp.c b/apps/common/pdfapp.c index b01bd485..b3507097 100644 --- a/apps/common/pdfapp.c +++ b/apps/common/pdfapp.c @@ -83,16 +83,11 @@ void pdfapp_open(pdfapp_t *app, char *filename) error = pdf_loadxref(app->xref, filename); if (error) { - if (!strncmp(error->msg, "ioerror", 7)) - pdfapp_error(app, error); - pdfapp_warn(app, - "There was a problem with file \"%s\".\n" - "It may be corrupted or generated by faulty software.\n\n" - "%s\n\nTrying to continue anyway...", - filename, error->msg); - error = pdf_repairxref(app->xref, filename); - if (error) - pdfapp_error(app, error); + fz_catch(error, "trying to repair"); + pdfapp_warn(app, "There was a problem with file \"%s\".\nIt may be corrupted or generated by faulty software.\nTrying to repair the file.", filename); + error = pdf_repairxref(app->xref, filename); + if (error) + pdfapp_error(app, error); } /* diff --git a/apps/pdfclean.c b/apps/pdfclean.c index 7a8cc0fc..58fc9b68 100644 --- a/apps/pdfclean.c +++ b/apps/pdfclean.c @@ -26,10 +26,7 @@ int doexpand = 0; void die(fz_error *eo) { - fflush(stdout); - fz_printerror(eo); - fz_droperror(eo); - fflush(stderr); + fz_catch(eo, "aborting"); exit(1); } @@ -45,9 +42,7 @@ void openxref(char *filename, char *password) error = pdf_loadxref(xref, filename); if (error) { - fz_printerror(error); - fz_droperror(error); - fz_warn("trying to repair"); + fz_catch(error, "trying to repair"); error = pdf_repairxref(xref, filename); if (error) die(error); diff --git a/apps/pdfdraw.c b/apps/pdfdraw.c index 53df42c8..cc7ce53e 100644 --- a/apps/pdfdraw.c +++ b/apps/pdfdraw.c @@ -23,13 +23,10 @@ pdf_pagetree *pagetree = nil; void die(fz_error *eo) { - fflush(stdout); - fz_printerror(eo); - fz_droperror(eo); - fflush(stderr); + fz_catch(eo, "aborting"); if (drawgc) fz_droprenderer(drawgc); - abort(); + exit(1); } void openxref(char *filename, char *password) @@ -50,9 +47,7 @@ void openxref(char *filename, char *password) error = pdf_loadxref(xref, filename); if (error) { - fz_printerror(error); - fz_droperror(error); - fz_warn("trying to repair"); + fz_catch(error, "trying to repair"); error = pdf_repairxref(xref, filename); if (error) die(error); diff --git a/apps/pdfinfo.c b/apps/pdfinfo.c index 374b39c6..c785391d 100644 --- a/apps/pdfinfo.c +++ b/apps/pdfinfo.c @@ -30,14 +30,11 @@ pdf_pagetree *srcpages = nil; void die(fz_error *eo) { - fflush(stdout); - fz_printerror(eo); - fz_droperror(eo); - fflush(stderr); - if (drawgc) - fz_droprenderer(drawgc); - closesrc(); - abort(); + fz_catch(eo, "aborting"); + if (drawgc) + fz_droprenderer(drawgc); + closesrc(); + exit(-1); } void closesrc(void) @@ -78,9 +75,7 @@ void opensrc(char *filename, char *password, int loadpages) error = pdf_loadxref(src, filename); if (error) { - fz_printerror(error); - fz_droperror(error); - fz_warn("trying to repair"); + fz_catch(error, "trying to repair"); error = pdf_repairxref(src, filename); if (error) die(error); diff --git a/apps/pdfshow.c b/apps/pdfshow.c index b3239978..4d59c590 100644 --- a/apps/pdfshow.c +++ b/apps/pdfshow.c @@ -9,10 +9,7 @@ pdf_xref *xref = NULL; void die(fz_error *eo) { - fflush(stdout); - fz_printerror(eo); - fz_droperror(eo); - fflush(stderr); + fz_catch(eo, "aborting"); exit(1); } @@ -28,9 +25,7 @@ void openxref(char *filename, char *password) error = pdf_loadxref(xref, filename); if (error) { - fz_printerror(error); - fz_droperror(error); - fz_warn("trying to repair"); + fz_catch(error, "trying to repair"); error = pdf_repairxref(xref, filename); if (error) die(error); diff --git a/apps/unix/x11pdf.c b/apps/unix/x11pdf.c index 0b1ff37c..18507f62 100644 --- a/apps/unix/x11pdf.c +++ b/apps/unix/x11pdf.c @@ -84,7 +84,7 @@ void winwarn(pdfapp_t *app, char *msg) void winerror(pdfapp_t *app, fz_error *error) { - fz_printerror(error); + fz_catch(error, "aborting"); exit(1); } diff --git a/fitz/Jamfile b/fitz/Jamfile index 7099185e..61f2583d 100644 --- a/fitz/Jamfile +++ b/fitz/Jamfile @@ -17,11 +17,10 @@ Library libfitz : base_memory.c base_rect.c base_rune.c + base_string.c base_cleanname.c ; -if $(NEED_STRLCPY) { Library libfitz : util_strlcpy.c ; } -if $(NEED_STRSEP) { Library libfitz : util_strsep.c ; } if $(NEED_GETOPT) { Library libfitz : util_getopt.c ; } # MSVC does not have gettimeofday() diff --git a/fitz/base_error.c b/fitz/base_error.c index a2ebbdd5..c0d4a42f 100644 --- a/fitz/base_error.c +++ b/fitz/base_error.c @@ -1,71 +1,45 @@ #include "fitz_base.h" -void -fz_droperror(fz_error *eo) +void fz_warn(char *fmt, ...) { - if (eo->cause) - fz_droperror(eo->cause); - fz_free(eo); + va_list ap; + fprintf(stderr, "warning: "); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fprintf(stderr, "\n"); } -void -fz_printerror(fz_error *eo) +fz_error * fz_throwimp(const char *func, const char *file, int line, char *fmt, ...) { -#if 1 - if (eo->cause) - { - fz_printerror(eo->cause); - fprintf(stderr, "| %s:%d: %s(): %s\n", eo->file, eo->line, eo->func, eo->msg); - } - else - { - fprintf(stderr, "+ %s:%d: %s(): %s\n", eo->file, eo->line, eo->func, eo->msg); - } -#else - fprintf(stderr, "+ %s:%d: %s(): %s\n", eo->file, eo->line, eo->func, eo->msg); - eo = eo->cause; - - while (eo) - { - fprintf(stderr, "| %s:%d: %s(): %s\n", eo->file, eo->line, eo->func, eo->msg); - eo = eo->cause; - } -#endif + va_list ap; + fprintf(stderr, "+ %s:%d: %s(): ", file, line, func); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fprintf(stderr, "\n"); + return ((fz_error*)-1); } - -void -fz_warn(char *fmt, ...) +fz_error * fz_rethrowimp(fz_error *cause, const char *func, const char *file, int line, char *fmt, ...) { va_list ap; - fprintf(stderr, "warning: "); + fprintf(stderr, "| %s:%d: %s(): ", file, line, func); va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); fprintf(stderr, "\n"); + return cause; } -fz_error * -fz_throwimp(fz_error *cause, const char *func, const char *file, int line, char *fmt, ...) +fz_error * fz_catchimp(fz_error *cause, const char *func, const char *file, int line, char *fmt, ...) { va_list ap; - fz_error *eo; - - eo = fz_malloc(sizeof(fz_error)); - if (!eo) - return fz_outofmem; /* oops. we're *really* out of memory here. */ - + fprintf(stderr, "\\ %s:%d: %s(): ", file, line, func); va_start(ap, fmt); - vsnprintf(eo->msg, sizeof eo->msg, fmt, ap); - eo->msg[sizeof(eo->msg) - 1] = '\0'; + vfprintf(stderr, fmt, ap); va_end(ap); - - strlcpy(eo->func, func, sizeof eo->func); - strlcpy(eo->file, file, sizeof eo->file); - eo->line = line; - - eo->cause = cause; - - return eo; + fprintf(stderr, "\n"); + return cause; } diff --git a/fitz/base_memory.c b/fitz/base_memory.c index f124f8cc..cd6de8cb 100644 --- a/fitz/base_memory.c +++ b/fitz/base_memory.c @@ -20,14 +20,6 @@ static void stdfree(fz_memorycontext *mem, void *p) static fz_memorycontext defmem = { stdmalloc, stdrealloc, stdfree }; static fz_memorycontext *curmem = &defmem; -fz_error fz_koutofmem = { - {"out of memory"}, - {"<internal>"}, - {"<internal>"}, - 0, - nil -}; - fz_memorycontext * fz_currentmemorycontext() { diff --git a/fitz/base_string.c b/fitz/base_string.c new file mode 100644 index 00000000..ed37ca41 --- /dev/null +++ b/fitz/base_string.c @@ -0,0 +1,63 @@ +#include <string.h> + +char *fz_strsep(char **stringp, const char *delim) +{ + char *ret = *stringp; + if (ret == NULL) return NULL; + if ((*stringp = strpbrk(*stringp, delim)) != NULL) + *((*stringp)++) = '\0'; + return ret; +} + +int fz_strlcpy(char *dst, const char *src, int siz) +{ + register char *d = dst; + register const char *s = src; + register int n = siz; + + /* Copy as many bytes as will fit */ + if (n != 0 && --n != 0) { + do { + if ((*d++ = *s++) == 0) + break; + } while (--n != 0); + } + + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) { + if (siz != 0) + *d = '\0'; /* NUL-terminate dst */ + while (*s++) + ; + } + + return(s - src - 1); /* count does not include NUL */ +} + +int fz_strlcat(char *dst, const char *src, int siz) +{ + register char *d = dst; + register const char *s = src; + register int n = siz; + int dlen; + + /* Find the end of dst and adjust bytes left but don't go past end */ + while (*d != '\0' && n-- != 0) + d++; + dlen = d - dst; + n = siz - dlen; + + if (n == 0) + return dlen + strlen(s); + while (*s != '\0') { + if (n != 1) { + *d++ = *s; + n--; + } + s++; + } + *d = '\0'; + + return dlen + (s - src); /* count does not include NUL */ +} + diff --git a/fitz/fitz_base.h b/fitz/fitz_base.h index cd8d90ee..4ce6fbc0 100644 --- a/fitz/fitz_base.h +++ b/fitz/fitz_base.h @@ -32,13 +32,17 @@ /* Some useful semi-standard functions */ -#ifdef NEED_STRLCPY -extern int strlcpy(char *dst, const char *src, int n); -extern int strlcat(char *dst, const char *src, int n); -#endif +extern char *fz_strsep(char **stringp, const char *delim); +extern int fz_strlcpy(char *dst, const char *src, int n); +extern int fz_strlcat(char *dst, const char *src, int n); #ifdef NEED_STRSEP -extern char *strsep(char **stringp, const char *delim); +#define strsep fz_strsep +#endif + +#ifdef NEED_STRLCPY +#define strlcpy fz_strlcpy +#define strlcat fz_strlcat #endif #ifdef NEED_GETOPT @@ -171,29 +175,19 @@ int runenlen(int *r, int nrune); int fullrune(char *str, int n); char *cleanname(char *name); -typedef struct fz_error_s fz_error; - -struct fz_error_s -{ - char msg[184]; - char file[32]; - char func[32]; - int line; - fz_error *cause; -}; - -#define fz_outofmem (&fz_koutofmem) -extern fz_error fz_koutofmem; +typedef int fz_error; -void fz_printerror(fz_error *eo); -void fz_droperror(fz_error *eo); -void fz_warn(char *fmt, ...) __printflike(1,2); +#define fz_outofmem ((fz_error*)-1) -#define fz_throw(...) fz_throwimp(nil, __func__, __FILE__, __LINE__, __VA_ARGS__) -#define fz_rethrow(cause, ...) fz_throwimp(cause, __func__, __FILE__, __LINE__, __VA_ARGS__) +#define fz_throw(...) fz_throwimp(__func__, __FILE__, __LINE__, __VA_ARGS__) +#define fz_rethrow(cause, ...) fz_rethrowimp(cause, __func__, __FILE__, __LINE__, __VA_ARGS__) +#define fz_catch(cause, ...) fz_catchimp(cause, __func__, __FILE__, __LINE__, __VA_ARGS__) #define fz_okay ((fz_error*)0) -fz_error *fz_throwimp(fz_error *cause, const char *func, const char *file, int line, char *fmt, ...) __printflike(5, 6); +void fz_warn(char *fmt, ...) __printflike(1,2); +fz_error *fz_throwimp(const char *func, const char *file, int line, char *fmt, ...) __printflike(4, 5); +fz_error *fz_rethrowimp(fz_error *cause, const char *func, const char *file, int line, char *fmt, ...) __printflike(5, 6); +fz_error *fz_catchimp(fz_error *cause, const char *func, const char *file, int line, char *fmt, ...) __printflike(5, 6); typedef struct fz_memorycontext_s fz_memorycontext; @@ -244,24 +238,6 @@ static inline int fz_idiv(int a, int b) return a < 0 ? (a - b + 1) / b : a / b; } -/* from python */ -static inline void fz_idivmod(int x, int y, int *d, int *m) -{ - int xdivy = x / y; - int xmody = x - xdivy * y; - /* If the signs of x and y differ, and the remainder is non-0, - * C89 doesn't define whether xdivy is now the floor or the - * ceiling of the infinitely precise quotient. We want the floor, - * and we have it iff the remainder's sign matches y's. - */ - if (xmody && ((y ^ xmody) < 0)) { - xmody += y; - xdivy --; - } - *d = xdivy; - *m = xmody; -} - typedef struct fz_matrix_s fz_matrix; typedef struct fz_point_s fz_point; typedef struct fz_rect_s fz_rect; diff --git a/fitz/fitz_stream.h b/fitz/fitz_stream.h index 3f05801a..a904d04b 100644 --- a/fitz/fitz_stream.h +++ b/fitz/fitz_stream.h @@ -202,13 +202,9 @@ void fz_dropbuffer(fz_buffer *buf); typedef struct fz_filter_s fz_filter; -#define fz_ioneedin (&fz_kioneedin) -#define fz_ioneedout (&fz_kioneedout) -#define fz_iodone (&fz_kiodone) - -extern fz_error fz_kioneedin; -extern fz_error fz_kioneedout; -extern fz_error fz_kiodone; +#define fz_ioneedin ((fz_error*)1) +#define fz_ioneedout ((fz_error*)2) +#define fz_iodone ((fz_error*)3) /* * Evil looking macro to create an initialize a filter struct. diff --git a/fitz/stm_filter.c b/fitz/stm_filter.c index e633b3f2..edcdb200 100644 --- a/fitz/stm_filter.c +++ b/fitz/stm_filter.c @@ -1,10 +1,6 @@ #include "fitz_base.h" #include "fitz_stream.h" -fz_error fz_kioneedin = { "<ioneedin>", "<internal>", "<internal>", 0, nil }; -fz_error fz_kioneedout = { "<ioneedout>", "<internal>", "<internal>", 0, nil }; -fz_error fz_kiodone = { "<iodone>", "<internal>", "<internal>", 0, nil }; - fz_error * fz_process(fz_filter *f, fz_buffer *in, fz_buffer *out) { diff --git a/fitz/stm_open.c b/fitz/stm_open.c index 41fc8d68..c01ad725 100644 --- a/fitz/stm_open.c +++ b/fitz/stm_open.c @@ -42,11 +42,8 @@ fz_dropstream(fz_stream *stm) { if (stm->error) { - fflush(stdout); - fz_printerror(stm->error); - fz_droperror(stm->error); - fflush(stderr); - fz_warn("dropped unhandled ioerror"); + fz_catch(stm->error, "dropped unhandled ioerror"); + stm->error = fz_okay; } switch (stm->kind) diff --git a/fitz/util_strlcpy.c b/fitz/util_strlcpy.c deleted file mode 100644 index 15b378b2..00000000 --- a/fitz/util_strlcpy.c +++ /dev/null @@ -1,65 +0,0 @@ -#include <string.h> - -/* Copy src to string dst of size siz. At most siz-1 characters - * will be copied. Always NUL terminates (unless siz == 0). - * Returns strlen(src); if retval >= siz, truncation occurred. - */ - -int strlcpy(char *dst, const char *src, int siz) -{ - register char *d = dst; - register const char *s = src; - register int n = siz; - - /* Copy as many bytes as will fit */ - if (n != 0 && --n != 0) { - do { - if ((*d++ = *s++) == 0) - break; - } while (--n != 0); - } - - /* Not enough room in dst, add NUL and traverse rest of src */ - if (n == 0) { - if (siz != 0) - *d = '\0'; /* NUL-terminate dst */ - while (*s++) - ; - } - - return(s - src - 1); /* count does not include NUL */ -} - -/* Appends src to string dst of size siz (unlike strncat, siz is the - * full size of dst, not space left). At most siz-1 characters - * will be copied. Always NUL terminates (unless siz == 0). - * Returns strlen(src); if retval >= siz, truncation occurred. - */ - -int strlcat(char *dst, const char *src, int siz) -{ - register char *d = dst; - register const char *s = src; - register int n = siz; - int dlen; - - /* Find the end of dst and adjust bytes left but don't go past end */ - while (*d != '\0' && n-- != 0) - d++; - dlen = d - dst; - n = siz - dlen; - - if (n == 0) - return dlen + strlen(s); - while (*s != '\0') { - if (n != 1) { - *d++ = *s; - n--; - } - s++; - } - *d = '\0'; - - return dlen + (s - src); /* count does not include NUL */ -} - diff --git a/fitz/util_strsep.c b/fitz/util_strsep.c deleted file mode 100644 index e54903ce..00000000 --- a/fitz/util_strsep.c +++ /dev/null @@ -1,11 +0,0 @@ -#include <string.h> - -char *strsep(char **stringp, const char *delim) -{ - char *ret = *stringp; - if (ret == NULL) return NULL; - if ((*stringp = strpbrk(*stringp, delim)) != NULL) - *((*stringp)++) = '\0'; - return ret; -} - diff --git a/mupdf/cmapdump.c b/mupdf/cmapdump.c index 1da05ad9..1efc3868 100644 --- a/mupdf/cmapdump.c +++ b/mupdf/cmapdump.c @@ -85,16 +85,14 @@ main(int argc, char **argv) error = fz_openrfile(&fi, argv[i]); if (error) { - fprintf(stderr, "cmapdump: could not open input file %s\n", argv[i]); - fz_printerror(error); + fz_catch(error, "cmapdump: could not open input file %s\n", argv[i]); return 1; } error = pdf_parsecmap(&cmap, fi); if (error) { - fprintf(stderr, "cmapdump: could not parse input cmap %s\n", argv[i]); - fz_printerror(error); + fz_catch(error, "cmapdump: could not parse input cmap %s\n", argv[i]); return 1; } diff --git a/mupdf/pdf_colorspace1.c b/mupdf/pdf_colorspace1.c index dbc21a78..10b24615 100644 --- a/mupdf/pdf_colorspace1.c +++ b/mupdf/pdf_colorspace1.c @@ -470,8 +470,7 @@ static void separationtoxyz(fz_colorspace *fzcs, float *sep, float *xyz) error = pdf_evalfunction(cs->tint, sep, fzcs->n, alt, cs->base->n); if (error) { - fz_warn("separation: %s", error->msg); - fz_droperror(error); + fz_catch(error, "cannot evaluate separation function"); xyz[0] = 0; xyz[1] = 0; xyz[2] = 0; diff --git a/mupdf/pdf_font.c b/mupdf/pdf_font.c index 88b85cec..aeb9de92 100644 --- a/mupdf/pdf_font.c +++ b/mupdf/pdf_font.c @@ -919,12 +919,7 @@ pdf_loadfontdescriptor(pdf_fontdesc *fontdesc, pdf_xref *xref, fz_obj *dict, cha error = pdf_loadembeddedfont(fontdesc, xref, obj); if (error) { - fflush(stdout); - fz_printerror(error); - fz_droperror(error); - fflush(stderr); - fz_warn("ignored error when loading embedded font, attempting to load system font"); - + fz_catch(error, "ignored error when loading embedded font, attempting to load system font"); error = pdf_loadsystemfont(fontdesc, fontname, collection); if (error) goto cleanup; diff --git a/mupdf/pdf_stream.c b/mupdf/pdf_stream.c index 8e7be301..5be2348a 100644 --- a/mupdf/pdf_stream.c +++ b/mupdf/pdf_stream.c @@ -15,8 +15,7 @@ pdf_isstream(pdf_xref *xref, int oid, int gen) error = pdf_cacheobject(xref, oid, gen); if (error) { - fz_warn("%s", error->msg); - fz_droperror(error); + fz_catch(error, "could not load object, ignoring error"); return 0; } |