Skip to content

Commit

Permalink
Replace setjmp/longjmp with try/catch in Hyper (#34)
Browse files Browse the repository at this point in the history
Also, remove the global variable `env` of type `jmp_buf` which was never used in the Hyper component.
  • Loading branch information
GabrielDosReis authored Dec 27, 2023
1 parent 7b828c0 commit b5749a5
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 156 deletions.
2 changes: 0 additions & 2 deletions src/hyper/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#include <X11/cursorfont.h>
#include <sys/types.h>
#include <sys/signal.h>
#include <setjmp.h>
#include <sys/time.h>

#include "debug.h"
Expand All @@ -60,7 +59,6 @@
#include "lex.h"
#include "sockio.h"

jmp_buf env;
Window gActiveWindow;
int motion = 0;
int gNeedIconName = 0;
Expand Down
1 change: 0 additions & 1 deletion src/hyper/htadd.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include "openaxiom-c-macros.h"

#include <errno.h>
#include <setjmp.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <stdlib.h>
Expand Down
10 changes: 4 additions & 6 deletions src/hyper/htinp.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

#include <sys/stat.h>
#include <sys/signal.h>
#include <setjmp.h>

#include "openaxiom-c-macros.h"
#include "debug.h"
Expand All @@ -54,7 +53,6 @@ extern char **input_file_list;
extern int input_file_count;
extern int make_patch_files;
extern int kill_spad;
extern jmp_buf jmpbuf;

static void make_input_file_list(void );
static char * make_input_file_name(char * buf , char * filename);
Expand Down Expand Up @@ -178,13 +176,13 @@ make_the_input_file(UnloadedPage *page)
b = make_input_file_name(buf, page->fpos.name);
if (inListAndNewer(b, page->fpos.name)) {
printf("parsing: %s\n", page->name);
if (setjmp(jmpbuf)) {
printf("Syntax error!\n");
}
else {
try {
load_page((HyperDocPage *)page);
make_input_file_from_page(gWindow->page);
}
catch(const HyperError&) {
printf("Syntax error!\n");
}
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/hyper/hyper.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
#include <sys/types.h>
#include <sys/signal.h>
#include <sys/wait.h>
#include <setjmp.h>
#include <X11/cursorfont.h>
#include <stdlib.h>
#include <locale.h>
Expand Down Expand Up @@ -176,9 +175,6 @@ sigcld_handler(int sig)

}

extern jmp_buf env;


/* Clean up spad sockets on exit */
void
clean_socket(void )
Expand Down
1 change: 0 additions & 1 deletion src/hyper/initx.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@

#include <unistd.h>
#include <sys/signal.h>
#include <setjmp.h>
#include <X11/cursorfont.h>
#include <X11/Xresource.h>
#include <X11/Xatom.h>
Expand Down
23 changes: 3 additions & 20 deletions src/hyper/lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
#include "openaxiom-c-macros.h"

#include <ctype.h>
#include <setjmp.h>
#include <stdlib.h>
#include <string.h>

Expand All @@ -71,7 +70,6 @@

using namespace OpenAxiom;

static void spad_error_handler(void );
static int keyword_type(void );

extern int gTtFontIs850;
Expand All @@ -81,7 +79,6 @@ extern HDWindow *gWindow;

StateNode *top_state_node;
HyperDocPage *gPageBeingParsed; /* page currently being parsed */
jmp_buf jmpbuf;
char ebuffer[128];
short int gInSpadsrc = 0;
short int gInVerbatim;
Expand Down Expand Up @@ -362,7 +359,7 @@ int get_char()
if (cmd == EndOfPage)
return EOF;
if (cmd == SpadError)
spad_error_handler();
throw HyperError{};
}
get_string_buf(spad_socket, sock_buf, 1023);
/* this will be null if this is the last time*/
Expand Down Expand Up @@ -582,9 +579,7 @@ jump()
{
if (gWindow == NULL)
exit(-1);
longjmp(jmpbuf, 1);
fprintf(stderr, "(HyperDoc) Long Jump failed, Exiting\n");
exit(-1);
throw HyperError{};
}

void
Expand Down Expand Up @@ -995,18 +990,6 @@ get_expected_token(int type)
print_page_and_filename();
print_next_ten_tokens();
}
longjmp(jmpbuf, 1);
fprintf(stderr, "Could not jump to Error Page\n");
exit(-1);
throw HyperError{};
}
}

static void
spad_error_handler()
{
/* fprintf(stderr, "got a spad error\n"); */
longjmp(jmpbuf, 1);
fprintf(stderr, "(HyperDoc) Fatal Error: Could not jump to Error Page.\n");
exit(-1);
}

22 changes: 12 additions & 10 deletions src/hyper/macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#include "hyper.h"
#include "lex.h"

using namespace OpenAxiom;

static char * load_macro(MacroStore * macro);
static void get_parameter_strings(int number , char * macro_name);

Expand All @@ -62,7 +64,7 @@ scan_HyperDoc()
ret_val = get_token();
if (ret_val == EOF && number_of_left_braces) {
fprintf(stderr, "Scan_Hypertex: Unexpected End of File\n");
longjmp(jmpbuf, 1);
throw HyperError{};
}
switch (token.type) {
case openaxiom_Page_token:
Expand Down Expand Up @@ -121,7 +123,7 @@ load_macro(MacroStore *macro)
/** WOW, Somehow I had the location of the wrong macro **/
fprintf(stderr, "Expected macro name %s got insted %s in load_macro\n",
macro->name, token.id);
longjmp(jmpbuf, 1);
throw HyperError{};
}
get_expected_token(openaxiom_Rbrace_token);

Expand All @@ -133,7 +135,7 @@ load_macro(MacroStore *macro)
if (!number(token.id)) {
fprintf(stderr, "load_macro: Expected A Value Instead Got %s\n",
token.id);
longjmp(jmpbuf, 1);
throw HyperError{};
}
/** if it is a number, then I should store it in the parameter number
member of the macro structure **/
Expand All @@ -154,7 +156,7 @@ load_macro(MacroStore *macro)
/** The macro is not in a group, uh oh **/
fprintf(stderr, "load_macro:Expected a Left Brace got type %d\n",
token.type);
longjmp(jmpbuf, 1);
throw HyperError{};
}
start_fpos = fpos;
scan_HyperDoc();
Expand Down Expand Up @@ -200,7 +202,7 @@ push_parameters(ParameterList parms)

if (parms == NULL) {
fprintf(stderr, "Tried pushing a null list onto the parameter stack\n");
longjmp(jmpbuf, 1);
throw HyperError{};
}

parms->next = parameters;
Expand Down Expand Up @@ -269,12 +271,12 @@ parse_macro()
else {
fprintf(stderr,
"parse_macro: Tried to pop an empty parameter stack\n");
longjmp(jmpbuf, 1);
throw HyperError{};
}
}
else {
fprintf(stderr, "parse_macro: Unknown keyword %s\n", token.id);
longjmp(jmpbuf, 1);
throw HyperError{};
}
}

Expand Down Expand Up @@ -311,7 +313,7 @@ get_parameter_strings(int number,char * macro_name)
switch (c = get_char()) {
case EOF:
fprintf(stderr, "GetParameterStrings: Unexpected EOF\n");
longjmp(jmpbuf, 1);
throw HyperError{};
case '}':
lbrace_counter--;
if (lbrace_counter)
Expand Down Expand Up @@ -374,14 +376,14 @@ parse_parameters()
if (!number(token.id)) {
fprintf(stderr,
"Parse_parameter: Error Expected a number, got %s instead\n", token.id);
longjmp(jmpbuf, 1);
throw HyperError{};
}

if ((value = atoi(token.id)) > parameters->number) {
/** had a bad parameter number **/
fprintf(stderr,
"Parse_parameter: Had a bad parameter number %d\n", value);
longjmp(jmpbuf, 1);
throw HyperError{};
}

parse_from_string((parameters->list)[value - 1]);
Expand Down
4 changes: 3 additions & 1 deletion src/hyper/parse-aux.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
#include "lex.h"
#include "hyper.h"

using namespace OpenAxiom;

static void read_ht_file(HashTable * page_hash , HashTable * macro_hash , HashTable * patch_hash , FILE * db_fp , char * db_file);
static HyperDocPage * make_special_page(int type , const char * name);

Expand Down Expand Up @@ -625,7 +627,7 @@ find_fp(FilePosition fp)
ret_val = fseek(lfile, fp.pos, 0);
if (ret_val == -1) {
perror("fseeking to a page");
longjmp(jmpbuf, 1);
throw HyperError{};
}

/* now set some global values */
Expand Down
4 changes: 3 additions & 1 deletion src/hyper/parse-input.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
#include "lex.h"
#include "hyper.h"

using namespace OpenAxiom;

static void insert_item(InputItem * item);
static void add_box_to_rb_list(char * name , InputBox * box);
static int check_others(InputBox * list);
Expand Down Expand Up @@ -186,7 +188,7 @@ parse_inputstring()
size = atoi(token.id);
if (size < 0) {
fprintf(stderr, "Illegal size in Input string\n");
longjmp(jmpbuf, 1);
throw HyperError{};
}

/* get the default value */
Expand Down
Loading

0 comments on commit b5749a5

Please sign in to comment.