From e28182595db472cb20a0017fc3f1b8f7fa77a949 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 26 Nov 2007 01:58:14 +0000 Subject: simplify --- Makefile | 2 ++ tm.c | 45 +++++++++++++++++++++++++-------------------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 1fdba70..bc56417 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ +# $Id$ + PROG= tm NOMAN= diff --git a/tm.c b/tm.c index 694cd48..25736af 100644 --- a/tm.c +++ b/tm.c @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -28,8 +29,6 @@ struct table { struct tulpe *tulpe; - int state; - char *init; }; struct tulpe { @@ -43,8 +42,6 @@ struct tulpe { struct tape { struct cell *head; - struct cell *begin; - struct cell *end; }; struct cell { @@ -67,6 +64,9 @@ char stop = 'H'; int width = 15; int counter = 0; int cells = 0; +int state = 0; +int die = 0; +char *initstring; Tape *alloctape(char *); Cell *alloccell(void); @@ -77,6 +77,7 @@ int execute(Tape *, Table *, int); void printtape(Tape *, Tulpe *, int, char); void printtable(Table *); void printtulpe(Tulpe *, char); +void halt(int); Tape * alloctape(char *init) @@ -90,11 +91,9 @@ alloctape(char *init) c = alloccell(); t->head = c; - t->begin = c; - t->end = c; - s = init; if (init != NULL) { + s = init; while (*s != '\0' && *s != '\n') { t->head->symb = *(s++); movehead(t, 'R'); @@ -137,7 +136,7 @@ alloctable(char *fname) tab = malloc(sizeof(Table)); assert(tab); - tab->init = NULL; + initstring = NULL; fd = fopen(fname, "r"); assert(fd); @@ -147,9 +146,9 @@ alloctable(char *fname) case '\n': /* empty line */ continue; case '!': /* initial string */ - tab->init = calloc(len, sizeof(char)); - assert(tab->init); - memcpy(tab->init, ++buf, len); + initstring = calloc(len, sizeof(char)); + assert(initstring); + memcpy(initstring, ++buf, len); continue; case '%': ++buf; @@ -176,7 +175,7 @@ alloctable(char *fname) fclose(fd); tab->tulpe = t; - tab->state = t->curst; + state = t->curst; return tab; } @@ -187,7 +186,7 @@ findtulpe(Table *tab, Cell *cell) Tulpe *t; for (t = tab->tulpe; t != NULL; t = t->next) { - if (tab->state != t->curst) + if (state != t->curst) continue; if (cell->symb != t->ssymb) continue; @@ -210,7 +209,6 @@ movehead(Tape *tape, char move) c = alloccell(); tape->head->left = c; c->right = tape->head; - tape->begin = c; } tape->head = tape->head->left; break; @@ -221,7 +219,6 @@ movehead(Tape *tape, char move) c = alloccell(); tape->head->right = c; c->left = tape->head; - tape->end = c; } tape->head = tape->head->right; break; @@ -240,10 +237,10 @@ execute(Tape *tape, Table *tab, int u) { Tulpe *t; - if (tab->init != NULL) + if (initstring != NULL) printtape(tape, NULL, width, '\r'); - while (tab->state != stop) { + while (state != stop && die == 0) { t = findtulpe(tab, tape->head); /* read, find */ if (t == NULL) { fprintf(stderr, "\ncannot find proper tulpe"); @@ -256,7 +253,7 @@ execute(Tape *tape, Table *tab, int u) usleep(u); movehead(tape, t->move); /* move head */ - tab->state = t->newst; /* save new state */ + state = t->newst; /* save new state */ ++counter; } @@ -285,7 +282,6 @@ printtape(Tape *t, Tulpe *tulpe, int n, char fin) s[n + i] = c->symb; s[n] = t->head->symb; - /* printf("State: %c %c\t", state, move); */ printtulpe(tulpe, '\t'); ps = s; @@ -315,8 +311,11 @@ main(int argc, char **argv) if (**argv == 'f') delay /= 100; /* fast */ + signal(SIGHUP, halt); + signal(SIGINT, halt); + table = alloctable(argv[1]); - tape = alloctape(table->init); + tape = alloctape(initstring); execute(tape, table, delay); @@ -351,3 +350,9 @@ printtulpe(Tulpe *t, char fin) t->newst); printf("%c", fin); } + +void +halt(int sig) +{ + die = 1; +} -- cgit v1.2.3