From: j8takagi Date: Fri, 22 Feb 2019 14:01:08 +0000 (+0900) Subject: ソースコードの推敲 X-Git-Tag: v0.5p9~7 X-Git-Url: https://j8takagi.net/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8808be96beb5502d13c0cd957b9fc3cec72ac6e3;p=YACASL2.git ソースコードの推敲 --- diff --git a/include/exec.h b/include/exec.h index b38e59a..5794e86 100644 --- a/include/exec.h +++ b/include/exec.h @@ -39,11 +39,6 @@ extern EXECMODE execmode; */ void addcerrlist_exec(); -/** - * @brief アセンブル結果読み込みエラーをエラーリストに追加する - */ -void addcerrlist_load(); - /** * @brief 指定されたファイルからアセンブル結果を読み込む * diff --git a/include/word.h b/include/word.h index 8e70f2e..7215a54 100644 --- a/include/word.h +++ b/include/word.h @@ -8,6 +8,11 @@ */ typedef unsigned short WORD; +/** + * @brief アセンブル結果読み込みエラーをエラーリストに追加する + */ +void addcerrlist_load(); + /** * @brief wordデータ型についてのエラーをエラーリストに追加する * diff --git a/src/casl2.c b/src/casl2.c index da5a45b..6c17d50 100644 --- a/src/casl2.c +++ b/src/casl2.c @@ -135,7 +135,7 @@ int main(int argc, char *argv[]) goto casl2fin; case '?': fprintf(stderr, usage, argv[0]); - setcerr(127, ""); /* invalid option */ + setcerr(212, ""); /* invalid option */ goto casl2fin; } } @@ -143,7 +143,7 @@ int main(int argc, char *argv[]) /* ソースファイルが指定されていない場合は終了 */ if(argv[optind] == NULL) { setcerr(126, ""); /* no source file */ - fprintf(stderr, "CASL2 error - %d: %s\n", cerr->num, cerr->msg); + fprintf(stderr, "casl2 error - %d: %s\n", cerr->num, cerr->msg); goto casl2fin; } create_cmdtable(HASH_CMDTYPE); /* 命令の名前とタイプがキーのハッシュ表を作成 */ @@ -153,7 +153,7 @@ int main(int argc, char *argv[]) } /* アセンブル */ if(assemble(i, af, 0) == false || asmode.onlylabel == true) { - goto freecmdtable; + goto shutdown; } /* オブジェクトファイル名が指定されている場合は、アセンブル結果をオブジェクトファイルに出力 */ if(objfile != NULL) { @@ -163,10 +163,10 @@ int main(int argc, char *argv[]) if(asmode.onlyassemble == false) { exec(); /* 仮想マシンCOMET IIの実行 */ } -freecmdtable: - shutdown(); /* 仮想マシンCOMET IIのシャットダウン */ - free_cmdtable(HASH_CMDTYPE); +shutdown: + shutdown(); /* 仮想マシンCOMET IIのシャットダウン */ casl2fin: + free_cmdtable(HASH_CMDTYPE); FREE(objfile); if(cerr->num > 0) { stat = 1; diff --git a/src/casl2rev.c b/src/casl2rev.c index 6d0d527..e4d381e 100644 --- a/src/casl2rev.c +++ b/src/casl2rev.c @@ -24,6 +24,7 @@ int main(int argc, char *argv[]) const char *version = PACKAGE_VERSION, *cmdversion = "disassemble of YACASL2 version %s\n"; const char *usage = "Usage: %s [-vh] FILE\n"; + /* エラーの定義 */ cerr_init(); addcerrlist_load(); addcerrlist_exec(); @@ -33,13 +34,14 @@ int main(int argc, char *argv[]) switch(opt) { case 'v': fprintf(stdout, cmdversion, version); - return 0; + goto casl2revfin; case 'h': fprintf(stdout, usage, argv[0]); - return 0; + goto casl2revfin; case '?': fprintf(stderr, usage, argv[0]); - exit(1); + setcerr(212, ""); /* invalid option */ + goto casl2revfin; } } if(argv[optind] == NULL) { @@ -48,7 +50,10 @@ int main(int argc, char *argv[]) exit(1); } disassemble_file(argv[optind]); /* プログラム実行 */ - stat = (cerr->num == 0) ? 0 : 1; +casl2revfin: + if(cerr->num > 0) { + stat = 1; + } freecerr(); /* エラーの解放 */ return stat; } diff --git a/src/comet2.c b/src/comet2.c index 55bab75..36fb5ed 100644 --- a/src/comet2.c +++ b/src/comet2.c @@ -17,6 +17,19 @@ static struct option longopts[] = { {0, 0, 0, 0}, }; + +/** + * @brief comet2のエラー定義 + */ +CERR cerr_comet2[] = { + { 127, "invalid option" }, +}; + +void addcerrlist_comet2() +{ + addcerrlist(ARRAYSIZE(cerr_comet2), cerr_comet2); +} + /** * @brief comet2コマンドのメイン * @@ -32,6 +45,12 @@ int main(int argc, char *argv[]) const char *version = PACKAGE_VERSION, *cmdversion = "comet2 of YACASL2 version %s\n"; const char *usage = "Usage: %s [-tTdmvh] [-M ] [-C ] FILE\n"; + /* エラーの定義 */ + cerr_init(); + addcerrlist_comet2(); + addcerrlist_load(); + addcerrlist_exec(); + /* オプションの処理 */ while((opt = getopt_long(argc, argv, "tTdmM:C:vh", longopts, NULL)) != -1) { switch(opt) { @@ -56,33 +75,32 @@ int main(int argc, char *argv[]) break; case 'v': fprintf(stdout, cmdversion, version); - return 0; + goto comet2fin; case 'h': fprintf(stdout, usage, argv[0]); - return 0; + goto comet2fin; case '?': fprintf(stderr, usage, argv[0]); - exit(1); + setcerr(212, ""); /* invalid option */ + goto comet2fin; } } - /* エラーの定義 */ - cerr_init(); - addcerrlist_load(); - addcerrlist_exec(); - if(argv[optind] == NULL) { setcerr(211, ""); /* object file not specified */ fprintf(stderr, "comet2 error - %d: %s\n", cerr->num, cerr->msg); - goto fin; + goto comet2fin; } reset(memsize, clocks); /* COMET II仮想マシンのリセット */ execptr->start = 0; - if((execptr->end = loadassemble(argv[optind], execptr->start)) > 0 && cerr->num == 0) { + execptr->end = loadassemble(argv[optind], execptr->start); + if(execptr->end > 0 && cerr->num == 0) { exec(); /* プログラム実行 */ } shutdown(); /* COMET II仮想マシンのシャットダウン */ -fin: - stat = (cerr->num == 0) ? 0 : 1; +comet2fin: + if(cerr->num > 0) { + stat = 1; + } freecerr(); /* エラーの解放 */ return stat; } diff --git a/src/comet2monitor.c b/src/comet2monitor.c index 4154e9c..1087285 100644 --- a/src/comet2monitor.c +++ b/src/comet2monitor.c @@ -13,7 +13,19 @@ static struct option longopts[] = { }; /** - * @brief comet2コマンドのメイン + * @brief casl2のエラー定義 + */ +CERR cerr_comet2monitor[] = { + { 401, "invalid option" }, +}; + +void addcerrlist_comet2monitor() +{ + addcerrlist(ARRAYSIZE(cerr_comet2monitor), cerr_comet2monitor); +} + +/** + * @brief comet2monitorコマンドのメイン * * @return 正常終了時は0、異常終了時は1 * @@ -27,6 +39,12 @@ int main(int argc, char *argv[]) const char *version = PACKAGE_VERSION, *cmdversion = "comet2monitor: COMET II machine code monitor of YACASL2 version %s\n"; const char *usage = "Usage: %s [-vh] [-M ] [-C ]\n"; + /* エラーの定義 */ + cerr_init(); + addcerrlist_load(); + addcerrlist_exec(); + addcerrlist_comet2monitor(); + /* オプションの処理 */ while((opt = getopt_long(argc, argv, "M:C:vh", longopts, NULL)) != -1) { switch(opt) { @@ -41,27 +59,25 @@ int main(int argc, char *argv[]) return 0; case 'h': fprintf(stdout, usage, argv[0]); - return 0; + goto comet2monitorfin; case '?': fprintf(stderr, usage, argv[0]); - exit(1); + setcerr(212, ""); /* invalid option */ + goto comet2monitorfin; } } - /* エラーの定義 */ - cerr_init(); - addcerrlist_load(); - addcerrlist_exec(); - create_cmdtable(HASH_CMDTYPE); reset(memsize, clocks); /* COMET II仮想マシンのリセット */ execptr->start = 0; execmode.monitor = true; exec(); /* プログラム実行 */ - shutdown(); +comet2monitorfin: free_cmdtable(HASH_CMDTYPE); free_cmdtable(HASH_CODE); - stat = (cerr->num == 0) ? 0 : 1; + if(cerr->num > 0) { + stat = 1; + } freecerr(); /* エラーの解放 */ return stat; } diff --git a/src/dumpword.c b/src/dumpword.c index 05cfe9a..b81d47c 100644 --- a/src/dumpword.c +++ b/src/dumpword.c @@ -23,11 +23,17 @@ static struct option longopts[] = { int main(int argc, char *argv[]) { bool logicalmode = false; /* レジストリの内容を論理値(0から65535)で表示する場合はtrue */ - int opt; + int opt, stat = 0; WORD word; const char *version = PACKAGE_VERSION, *cmdversion = "dumpword of YACASL2 version %s\n"; const char *usage = "Usage: %s [-alh] WORD\n"; + /* エラーの定義 */ + cerr_init(); + addcerrlist_load(); + addcerrlist_word(); + + /* オプションの処理 */ while((opt = getopt_long(argc, argv, "alvh", longopts, NULL)) != -1) { switch(opt) { case 'l': @@ -35,35 +41,35 @@ int main(int argc, char *argv[]) break; case 'v': fprintf(stdout, cmdversion, version); - return 0; + goto dumpwordfin; case 'h': fprintf(stdout, usage, argv[0]); - return 0; + goto dumpwordfin; case '?': fprintf(stderr, usage, argv[0]); - exit(1); + setcerr(212, ""); /* invalid option */ + goto dumpwordfin; } } - /* エラーの定義 */ - cerr_init(); - addcerrlist_word(); - if(argv[optind] == NULL) { + setcerr(213, ""); /* invalid argument */ fprintf(stderr, usage, argv[0]); - freecerr(); - exit(1); + goto dumpwordfin; } /* WORD値に変換 */ word = nh2word(argv[optind]); if(cerr->num > 0) { fprintf(stderr, "Dumpword Error - %d: %s\n", cerr->num, cerr->msg); - freecerr(); - exit(1); + goto dumpwordfin; } fprintf(stdout, "%6s: ", argv[optind]); print_dumpword(word, logicalmode); fprintf(stdout, "\n"); - freecerr(); - return 0; +dumpwordfin: + if(cerr->num > 0) { + stat = 1; + } + freecerr(); /* エラーの解放 */ + return stat; } diff --git a/src/exec.c b/src/exec.c index 8b5047e..99a01d7 100644 --- a/src/exec.c +++ b/src/exec.c @@ -85,14 +85,6 @@ static CERR cerr_exec[] = { { 209, "SVC output - memory overflow" }, }; -/** - * @brief アセンブル結果読み込みエラーの定義 - */ -static CERR cerr_load[] = { - { 210, "load - memory overflow" }, - { 211, "object file not specified" }, -}; - /** * @brief 実行モード: trace, logical, dump, dump_start, dump_end, monitor, step */ @@ -216,11 +208,6 @@ WORD get_val_adr_x(WORD adr, WORD oprx) } /* exec.hで定義された関数群 */ -void addcerrlist_load() -{ - addcerrlist(ARRAYSIZE(cerr_load), cerr_load); -} - void addcerrlist_exec() { addcerrlist(ARRAYSIZE(cerr_exec), cerr_exec); diff --git a/src/monitor.c b/src/monitor.c index 30893b6..d02a435 100644 --- a/src/monitor.c +++ b/src/monitor.c @@ -370,10 +370,23 @@ void free_moncmdline(MONCMDLINE *moncmdl) } } +int monquit() +{ + int stat = 0; + shutdown(); + freebps(); + free_cmdtable(HASH_CMDTYPE); + free_cmdtable(HASH_CODE); + if(cerr->num > 0) { + stat = 1; + } + freecerr(); + return stat; +} + void monitor() { char *buf = NULL; - int i; MONCMDLINE *moncmdl; MONCMDTYPE cmdtype = MONREPEAT; @@ -385,20 +398,14 @@ void monitor() if(!buf[0]) { cmdtype = MONQUIT; } - if((i = strcspn(buf, "\n")) > 0 || buf[0] == '\n') { - buf[i] = '\0'; - } + strip_end(buf); /* 文字列末尾の改行と空白を削除 */ if((moncmdl = monlinetok(buf)) != NULL) { cmdtype = monitorcmd(moncmdl->cmd, moncmdl->args); free_moncmdline(moncmdl); } FREE(buf); if(cmdtype == MONQUIT) { - shutdown(); - freebps(); - free_cmdtable(HASH_CMDTYPE); - freecerr(); - exit(0); + exit(monquit()); } } while(cmdtype == MONREPEAT); } diff --git a/src/struct.c b/src/struct.c index a617cec..72c1c9c 100644 --- a/src/struct.c +++ b/src/struct.c @@ -112,7 +112,7 @@ unsigned hash_cmdtype(const char *cmd, CMDTYPE type) */ bool create_cmdtable(CMDTAB_HASH hash) { - CMDTAB *p; + CMDTAB *p = NULL; unsigned hashval; int i; diff --git a/src/word.c b/src/word.c index bae1e82..e11b730 100644 --- a/src/word.c +++ b/src/word.c @@ -25,15 +25,6 @@ WORD n2word(const char *str); */ WORD h2word(const char *str); -/** - * @brief wordのエラー定義 - */ -static CERR cerr_word[] = { - { 114, "not integer" }, - { 115, "not hex" }, - { 116, "out of hex range" }, -}; - WORD n2word(const char *str) { assert(isdigit(str[0]) || str[0] == '-'); @@ -73,7 +64,31 @@ WORD h2word(const char *str) return w; } +/** + * @brief wordのエラー定義 + */ +static CERR cerr_word[] = { + { 114, "not integer" }, + { 115, "not hex" }, + { 116, "out of hex range" }, +}; + +/** + * @brief ファイル読み込みのエラー定義 + */ +static CERR cerr_load[] = { + { 210, "load - memory overflow" }, + { 211, "object file not specified" }, + { 212, "invalid option" }, + { 213, "invalid argument" }, +}; + /* word.hで定義された関数群 */ +void addcerrlist_load() +{ + addcerrlist(ARRAYSIZE(cerr_load), cerr_load); +} + void addcerrlist_word() { addcerrlist(ARRAYSIZE(cerr_word), cerr_word); diff --git a/test/system/casl2_err/err_126/0.txt b/test/system/casl2_err/err_126/0.txt index 155a049..7e5ffef 100644 --- a/test/system/casl2_err/err_126/0.txt +++ b/test/system/casl2_err/err_126/0.txt @@ -1 +1 @@ -CASL2 error - 126: no source file +casl2 error - 126: no source file