wdvi

network DVI viewer
Log | Files | Refs

commit 3db5d4af71a86b85bf994e32827dc325e167e7de
parent 71c37afff285b396ea5a0b6cd93781d879913799
Author: Kyle Milz <krwmilz@gmail.com>
Date:   Sat, 11 Sep 2021 23:25:05 +0000

use return value error checking in init_dvi()

Replace setjmp() and friends with return codes for error checking.

Diffstat:
Mdvi-init.c | 94++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mdvi-init.h | 4++--
Mevents.c | 3++-
Mxdvi.c | 3++-
4 files changed, 54 insertions(+), 50 deletions(-)

diff --git a/dvi-init.c b/dvi-init.c @@ -31,9 +31,8 @@ NOTE: #include <X11/Shell.h> /* defines XtNtitle and XtNiconName */ -#include "dvi-init.h" #include "events.h" /* mane */ -#include "font.h" +#include "font.h" /* struct font, struct glyph */ #include "special.h" /* init_prescan() */ #include "xdvi.h" #include "dvi.h" @@ -50,11 +49,6 @@ NOTE: #define VF_ID_BYTE 202 #define VF_MAGIC ((VF_PRE << 8) | VF_ID_BYTE) -static jmp_buf dvi_env; /* mechanism to relay dvi file errors */ -static const char *dvi_oops_msg; /* error message */ - -#define dvi_oops(str) (dvi_oops_msg = (str), longjmp(dvi_env, 1)) - static Boolean font_not_found; static Boolean dvi_is_valid = False; /* if not initializing */ @@ -470,15 +464,17 @@ define_font(FILE *file, wide_ubyte cmnd, struct font *vfparent, * process_preamble reads the information in the preamble and stores * it into global variables for later use. */ -static void +static Boolean process_preamble() { ubyte k; if (one(dvi_file) != PRE) - dvi_oops("Not a DVI file"); + /* Not a DVI file */ + return False; if (one(dvi_file) != 2) - dvi_oops("Wrong version of DVI output for this program"); + /* Wrong version of DVI output for this program */ + return False; numerator = four(dvi_file); denominator = four(dvi_file); magnification = four(dvi_file); @@ -489,6 +485,8 @@ process_preamble() k = one(dvi_file); Fread(job_id, sizeof(char), (int) k, dvi_file); job_id[k] = '\0'; + + return True; } /* @@ -496,7 +494,7 @@ process_preamble() * and leaves the file ready to start reading at that location. */ #define TMPSIZ 516 /* 4 trailer bytes + 512 junk bytes allowed */ -static void +static Boolean find_postamble() { long pos; @@ -516,7 +514,9 @@ find_postamble() p = p1; while (p > temp && *(--p) == TRAILER) ; if (p <= p1 - 4) break; /* found 4 TRAILER bytes */ - if (p <= temp) dvi_oops("DVI file corrupted"); + if (p <= temp) + /* DVI file corrupted */ + return False; } pos += p - temp; byte = *p; @@ -525,9 +525,12 @@ find_postamble() byte = one(dvi_file); } if (byte != 2) - dvi_oops("Wrong version of DVI output for this program"); + /* Wrong version of DVI output for this program */ + return False; Fseek(dvi_file, pos - 4, 0); Fseek(dvi_file, sfour(dvi_file), 0); + + return True; } @@ -545,13 +548,15 @@ read_postamble() struct font **fontpp; if (one(dvi_file) != POST) - dvi_oops("Postamble doesn't begin with POST"); + /* Postamble doesn't begin with POST */ + return False; last_page_offset = four(dvi_file); - if (numerator != four(dvi_file) - || denominator != four(dvi_file) - || magnification != four(dvi_file)) - dvi_oops("Postamble doesn't match preamble"); - /* read largest box height and width */ + if (numerator != four(dvi_file) || denominator != four(dvi_file) || + magnification != four(dvi_file)) + /* Postamble doesn't match preamble */ + return False; + + /* read largest box height and width */ dvi_unshrunk_page_h = (spell_conv(sfour(dvi_file)) >> 16) + offset_y; if (dvi_unshrunk_page_h < unshrunk_paper_h) dvi_unshrunk_page_h = unshrunk_paper_h; @@ -566,9 +571,11 @@ read_postamble() tn_table, TNTABLELEN, &tn_head) == NULL) return False; if (cmnd != POSTPOST) - dvi_oops("Non-fntdef command found in postamble"); + /* Non-fntdef command found in postamble */ + return False; if (font_not_found) - dvi_oops("Not all pixel files were found"); + /* Not all pixel files were found */ + return False; /* * free up fonts no longer in use */ @@ -727,20 +734,20 @@ set_titles() } /* - * internal_init_dvi is the main subroutine for reading the startup + * init_dvi_file is the main subroutine for reading the startup * information from the dvi file. */ -static Boolean -internal_init_dvi() +Boolean +init_dvi_file() { - process_preamble(); - find_postamble(); - if (!read_postamble()) { - fclose(dvi_file); - dvi_file = NULL; - return False; - } + if (!process_preamble()) + goto err; + if (!find_postamble()) + goto err; + if (!read_postamble()) + goto err; + prepare_pages(); if (current_page >= total_pages) current_page = total_pages - 1; warn_spec_now = warn_spec; @@ -748,20 +755,13 @@ internal_init_dvi() init_prescan(); set_titles(); dvi_is_valid = True; - return True; -} - -/** - ** init_dvi_file initializes the dvi file. Called only from main(). - **/ -void -init_dvi_file() -{ - if (setjmp(dvi_env)) - errx(1, "%s", dvi_oops_msg); + return True; - (void) internal_init_dvi(); +err: + fclose(dvi_file); + dvi_file = NULL; + return False; } /** @@ -781,7 +781,7 @@ check_dvi_file() ** Reload the dvi file (unconditionally). Called only from do_pages(). **/ -void +Boolean reload_dvi_file() { struct font *fontp; @@ -799,8 +799,10 @@ reload_dvi_file() fontp->flags &= ~FONT_IN_USE; Fseek(dvi_file, (long) 0, 0); - if (!internal_init_dvi()) - return; + if (!init_dvi_file()) + return False; if (list_fonts) putchar('\n'); + + return True; } diff --git a/dvi-init.h b/dvi-init.h @@ -2,8 +2,8 @@ Boolean check_dvi_file(void); void full_reset_colors(void); -void init_dvi_file(void); +Boolean init_dvi_file(void); void init_page(void); -void reload_dvi_file(void); +Boolean reload_dvi_file(void); void reset_colors(void); void reset_fonts(void); diff --git a/events.c b/events.c @@ -1901,7 +1901,8 @@ do_pages() ev_flags &= ~(EV_NEWPAGE | EV_EXPOSE | EV_PS_TOGGLE); if (ev_flags & EV_NEWDOC) { ev_flags &= ~EV_NEWDOC; - reload_dvi_file(); + if (!reload_dvi_file()) + warning_popup("Bad DVI document", "OK", NULL); } can_exposures(&mane); can_exposures(&alt); diff --git a/xdvi.c b/xdvi.c @@ -965,7 +965,8 @@ main(int argc, char **argv) */ init_font_open(); - init_dvi_file(); + if (init_dvi_file() == False) + errx(1, "Bad default DVI document"); prescan(); init_page();