--- nono/lib/mainapp.cpp 2026/04/29 17:04:37 1.1.1.4 +++ nono/lib/mainapp.cpp 2026/04/29 17:05:05 1.1.1.11 @@ -6,8 +6,10 @@ #include "mainapp.h" #include "mystring.h" +#include "netdriver_selector.h" #include #include +#include // グローバルインスタンス MainApp gMainApp; @@ -21,8 +23,7 @@ MainApp::ShowHelp(bool all) const #define p(msg) printf(" " msg "\n") printf("usage: %s []\n", getprogname()); - p("-A load and execute host binary (a.out or ELF)"); - p("-c vm directory"); + p("-c vm directory or configuration file"); p("-f fast mode"); if (IsGUI()) p("--fontsize fontsize in monitors {12,16} (default:12)"); @@ -31,8 +32,10 @@ MainApp::ShowHelp(bool all) const if (IsGUI()) p("-s,--scale screen scale, default 1.0"); p("--show-config show configuration variables"); + p("--show-hostnet show list of hostnet drivers"); p("-v show version"); p("-V = overwrite config option"); + p("-X [arg..] load and execute host binary (a.out or ELF)"); if (all) { printf("\n(options for developers)\n"); @@ -41,9 +44,10 @@ MainApp::ShowHelp(bool all) const p("-C output log to console"); p("-d debugger prompt on startup"); p("-D debugger on console"); + p("-H human68k console emulation"); p("-L =[,..] set loglevel (-Lhelp displays names)"); + p("--load-only load host binary (a.out or ELF) but not execute"); p("-M [,..] monitors to display at startup (-Mhelp displays names)"); - p("-X human68k console emulation"); } } @@ -52,28 +56,36 @@ MainApp::ShowHelp(bool all) const // 0x80 から始めておく。 enum { OPT_fontsize = 0x80, + OPT_load_only, OPT_help, OPT_show_config, OPT_show_config_all, + OPT_show_hostnet, }; static struct option longopts[] = { { "fontsize", required_argument, NULL, OPT_fontsize }, + { "load-only", required_argument, NULL, OPT_load_only }, { "help", no_argument, NULL, OPT_help }, { "show-config", no_argument, NULL, OPT_show_config }, // --show-config-all は開発用なのでヘルプには載せない { "show-config-all",no_argument, NULL, OPT_show_config_all }, + { "show-hostnet", no_argument, NULL, OPT_show_hostnet }, { NULL, 0, NULL, 0 }, }; -// 起動時の処理、VM の実行開始前まで。 +// VM の初期化、ステージ1。 +// VM 作成と設定確定あたりまで。スレッド生成を伴わないもの。 // 所々 CLI と GUI で処理が違う。 -bool -MainApp::Init(bool is_cli_, int ac, char *av[]) +// 戻り値は以下のいずれか。 +// MainApp::PASS .. 実行を継続(通常パス) +// EXIT_SUCCESS / EXIT_FAILURE .. この終了コードでアプリケーションを終了 +int +MainApp::Init1(bool is_cli_, int ac, char *av[]) { is_cli = is_cli_; if (!ParseOpt(ac, av)) { - return false; + return EXIT_FAILURE; } // ログ機構は引数処理後なるはや @@ -84,56 +96,48 @@ MainApp::Init(bool is_cli_, int ac, char // 設定を作成。コンストラクタで初期値を用意 gConfig.reset(new Config()); - // 引数で指定されたディレクトリの設定ファイルを読み込んで設定を更新 - ConfigFile file(vmdir + "nono.cfg"); + // 引数で指定されたディレクトリの設定ファイルがあれば読み込んで設定を更新 + ConfigFile file(vmfile); if (file.Load() == false) { - return false; + return EXIT_FAILURE; } if (gConfig->Update(file) == false) { - return false; + return EXIT_FAILURE; } // 最後にコマンドライン引数で更新 if (gConfig->Update(config_options) == false) { - return false; + return EXIT_FAILURE; } - // VM 種別を決定 - if (IsCLI() && human68k_file) { - vmtype = VMTYPE_RXZ; + // 1. VM 種別を決定 + // VM 種別文字列から vmtype を決定 + const ConfigItem& item = gConfig->Find("vmtype"); + std::string vmstr = string_tolower(item.AsString()); + if (vmstr == "x68030") { + vmtype = VMTYPE_X68030; + } else if (vmstr == "luna") { + vmtype = VMTYPE_LUNA1; + } else if (vmstr == "luna88k") { + vmtype = VMTYPE_LUNA88K; } else { - // VM 種別文字列から vmtype を決定 - const ConfigItem& item = gConfig->Find("vmtype"); - std::string vmstr = string_tolower(item.AsString()); - if (vmstr == "x68030") { - vmtype = VMTYPE_X68030; - } else if (vmstr == "luna") { - vmtype = VMTYPE_LUNA1; - } else if (vmstr == "luna88k") { - vmtype = VMTYPE_LUNA88K; + if (item.GetFrom() == ConfigItem::FromInitial) { + // 未指定の時 + warnx("vmtype must be specified"); } else { - if (item.GetFrom() == ConfigItem::FromInitial) { - // 未指定の時 - warnx("vmtype must be specified"); - } else { - // ユーザ由来の時 - item.Err("invalid vmtype"); - } - return false; + // ユーザ由来の時 + item.Err("invalid vmtype"); } + return EXIT_FAILURE; } - // VM 作成 + // 2. VM 作成 switch (vmtype) { case VMTYPE_X68030: gVM.reset(new VM_X68030()); break; case VMTYPE_LUNA1: - gVM.reset(new VM_LUNA()); - break; - - case VMTYPE_RXZ: - gVM.reset(new VM_RXZ()); + gVM.reset(new VM_LUNA1()); break; case VMTYPE_LUNA88K: @@ -144,33 +148,63 @@ MainApp::Init(bool is_cli_, int ac, char __unreachable(); } - // 設定内容表示 - // (MPU コンストラクタでクロック数の初期値を決めるため、その後で表示) + // 3. 動的なコンストラクション + if (!gVM->Create()) { + gVM.reset(); + return EXIT_FAILURE; + } + + // デバッガ (ここでもモニタを登録する) + debugger_create(); + + // 設定内容を表示して終了 + // (VM コンストラクタで変数の増減があるのでそれより後、 + // Create() でも SCSI パラメータを減らすので、それより後) gConfig->Fix(); if (show_config) { gConfig->Show(show_config - 1); + gVM.reset(); + return EXIT_SUCCESS; } - // 動的なコンストラクション - if (!gVM->Create()) { - return false; - } - - // ログレベルを設定。コンストラクト後すぐに行う。 + // 4. ログレベルを設定。コンストラクト後すぐに行う。 // -L help もここで処理。 if (!ParseLogopt()) { - return false; + gVM.reset(); + return EXIT_FAILURE; } - return true; + // 5. ホストネットワークドライバの作成。 + // + // ホストネットワークドライバを作成する時にはログ表示が欲しいが、通常の + // フローではオブジェクトが出揃った後でログレベルを割り当てるので、 + // にわたま問題になってしまう。そこでこれだけ特別対応する。 + // 手順 3. のコンストラクションで EthernetDevice が NetDriverSelector を + // 作成し、手順 4. で通常通りにログレベルの割り当てを受ける。このログを + // 使ってここ(手順5)で実際の NetDriver を作成し、同時にログレベルも引き + // 継ぐことにする。 + if ((bool)gNetDriverSelector) { + // 実際の NetDriver を作成 + NetDriver *newdriver = gNetDriverSelector->InitDriver(); + if (newdriver == NULL) { + gVM.reset(); + return EXIT_FAILURE; + } + // 成功すればセット + gNetDriver.reset(newdriver); + + // NetDriverSelector はこれ以降まったく使わないがとりあえず放置。 + } + + return PASS; } -// VM の実行部分。 -// Init() で引数を受け付けてから Start() でデバッガスレッドなどを起動するが、 +// VM の初期化、ステージ2。スレッド生成を伴う。 +// Init1() で引数を受け付けてから Init2() でデバッガスレッドなどを起動するが、 // -Mhelp とかが指定された場合はここでスレッド開始する前にプロセスを終了する -// 必要があるので、分けてある。 +// 必要があるので分けてある。wxapp.cpp も参照。 bool -MainApp::Start() +MainApp::Init2() { // VM 初期化。 // これ以降はスレッドを開始したかもしれないので false を返す際には @@ -196,19 +230,18 @@ MainApp::Start() bool MainApp::ParseOpt(int ac, char *av[]) { + struct stat st; + const char *cpath; int b; int c; fontsize = 12; screen_scale = 1.0; + cpath = "."; - while ((c = getopt_long(ac, av, "A:b:B:c:CdDfhL:M:s:vV:X:", + while ((c = getopt_long(ac, av, "b:B:c:CdDfhHL:M:s:vV:X:", longopts, NULL)) != -1) { switch (c) { - case 'A': - host_file = optarg; - break; - case 'b': debug_breakaddr.push_back(optarg); break; @@ -223,7 +256,11 @@ MainApp::ParseOpt(int ac, char *av[]) break; case 'c': - vmdir = std::string(optarg); + cpath = optarg; + // 空文字列なら再び初期値に + if (cpath[0] == '\0') { + cpath = "."; + } break; case 'C': @@ -242,6 +279,10 @@ MainApp::ParseOpt(int ac, char *av[]) fast_mode = true; break; + case 'H': + human_mode = true; + break; + case 'L': if (logopt[0] != '\0') { strlcat(logopt, ",", sizeof(logopt)); @@ -274,12 +315,15 @@ MainApp::ParseOpt(int ac, char *av[]) break; case 'X': - human68k_file = optarg; + load_and_exec = true; + FALLTHROUGH; + case OPT_load_only: + exec_file = optarg; for (int i = optind; i < ac; i++) { if (i != optind) { - strlcat(human68k_arg, " ", sizeof(human68k_arg)); + exec_arg += " "; } - strlcat(human68k_arg, av[i], sizeof(human68k_arg)); + exec_arg += av[i]; } optind = ac; break; @@ -308,6 +352,10 @@ MainApp::ParseOpt(int ac, char *av[]) show_config = 2; break; + case OPT_show_hostnet: + ShowHostnet(); + return false; + case 'h': default: ShowHelp(false); @@ -315,12 +363,33 @@ MainApp::ParseOpt(int ac, char *av[]) } } - // vmdir に '/' を付けておく - if (vmdir.empty()) { - vmdir = "."; + // 引数がディレクトリなら、それを VM ディレクトリとし、その中の + // nono.cfg を設定ファイルとする。 + // 引数がファイルなら、それを設定ファイルとし、そのファイルがある + // ディレクトリを VM ディレクトリとする。 + // -c DIR => vmdir = DIR, vmfile = DIR/nono.cfg + // -c DIR/FILE => vmdir = DIR, vmfile = FILE + if (stat(cpath, &st) < 0) { + warn("stat %s", cpath); + return false; } - if (vmdir.back() != '/') { - vmdir += '/'; + if (S_ISDIR(st.st_mode)) { + vmdir = std::string(cpath); + if (vmdir.back() != '/') { + vmdir += '/'; + } + vmfile = vmdir + "nono.cfg"; + } else if (S_ISREG(st.st_mode)) { + vmfile = std::string(cpath); + auto pos = vmfile.rfind('/'); + if (pos != std::string::npos) { + vmdir = vmfile.substr(0, pos + 1); + } else { + vmdir = "./"; + } + } else { + warnx("-c %s: path must be file or directory", cpath); + return false; } // CLI 版で -Mhelp つけても黙って起動するのはさすがにどうかと思う。 @@ -338,6 +407,14 @@ MainApp::ParseOpt(int ac, char *av[]) } } + // Human モードでは実行ファイル名が必要 + if (human_mode) { + if (exec_file == NULL) { + warnx("-H option needs -X"); + return false; + } + } + return true; } @@ -350,97 +427,190 @@ MainApp::ShowVersion() const NONO_MAJOR_VER, NONO_MINOR_VER, NONO_PATCH_VER, NONO_DATE); } - -// 引数 logopt のログ指定文字列をパースする。 -// arg は "foo=1,bar=2" 形式の文字列で、これを分解して -// それぞれ担当するオブジェクトのログレベルにセットする。 +// ログレベル指定文字列を処理する。 +// str はログレベル指定文字列を ',' で連結した "foo=1,bar=2" 形式の文字列で、 +// これを分解してそれぞれ担当するオブジェクトのログレベルにセットする。 +// "help" があれば設定は行わず一覧を表示。 bool MainApp::ParseLogopt() { - char *buf; - char *last; - char *p; - bool rv; - - // "help" (完全一致) なら識別子一覧を表示。 - if (strcmp(logopt, "help") == 0) { - for (auto& obj : gObjects) { - if (obj->logname != "?") { - printf("%s\n", obj->logname.c_str()); - } - } - // エイリアスとか - printf("sch -> scheduler\n"); - if (gMainApp.HasVMFeature(VMF_LUNA)) { - printf("scc -> sio\n"); + std::vector items; + + // 分解して.. + items = string_split(logopt, ','); + + // "help" があればヘルプを表示して終了 + for (const auto& item : items) { + if (item == "help") { + std::vector list = GetLogNames(); + // less したいだろうから stderr ではなく stdout に出力する + for (const auto& name : list) { + printf(" %s\n", name.c_str()); + } + return false; } - printf("all\n"); + } + + // ログレベルを設定 + std::string errmsg; + if (SetLogopt(items, &errmsg) == false) { + warnx("%s", errmsg.c_str()); return false; } - rv = false; - buf = strdup(logopt); - for (p = strtok_r(buf, ",", &last); - p; - p = strtok_r(NULL, ",", &last)) - { - const char *name; - char *v; - int val; - - name = p; - - v = strchr(p, '='); - if (v) { - *v++ = '\0'; - val = atoi(v); - } else { - val = 1; - } - // ここで name は変数名、val は値(省略されたら1) + return true; +} - if (strlen(name) < 1) { - printf("invalid logname '%s'\n", name); - goto abort; +// ログレベルを設定する。 +// ログレベル指定文字列を 1つずつに分解したリスト items を処理する。 +// "help" があるケースはここに来るまでに処理してあるので、ここには来ない。 +// 成功なら何も表示せず true を返す。失敗なら *errmsg にエラーメッセージを +// 格納して false を返す。 +// MainApp 内と Debugger からも呼ばれる。 +/*static*/ bool +MainApp::SetLogopt(const std::vector& items, std::string *errmsg) +{ + for (const auto& item : items) { + if (SetLogopt1(item.c_str(), errmsg) == false) { + return false; + } + } + if (0) { // デバッグ用 + for (const auto o : gObjects) { + printf("%-16s %d\n", o->GetName().c_str(), o->loglevel); } + } + return true; +} - // 短縮形とかエイリアスとか - if (strcmp(name, "sch") == 0) - name = "scheduler"; - if (gMainApp.HasVMFeature(VMF_LUNA)) { - if (strcmp(name, "scc") == 0) - name = "sio"; +// "logname[=loglevel]" 形式をパースしてオブジェクトにログレベルを設定する。 +// loglevel は省略なら 1 とする。 +// logname が "all" なら全オブジェクトにセットする。 +// そうでない場合は case ignore で完全一致するか前方一致で1つに確定すれば、 +// そのオブジェクトにログレベルを設定して true を返す。 +// 見付からないか候補が複数ある場合はエラーメッセージを *errmsg に出力して +// false を返す。 +// この関数は (このすぐ上の SetLogopt() を経由して) +// MainApp と Debugger から呼ばれることに注意。 +/*static*/ bool +MainApp::SetLogopt1(const std::string& item, std::string *errmsg) +{ + std::string name; + const char *v; + int level; + + v = strchr(item.c_str(), '='); + if (v) { + name = std::string(item.c_str(), v - item.c_str()); + level = atoi(++v); + } else { + name = item; + level = 1; + } + // ここで name は変数名、level は値(省略されたら1) + + if (name.empty()) { + *errmsg = "logname must be specified"; + return false; + } + + // "all" なら全部にセット + if (name == "all") { + for (auto obj : gObjects) { + if (obj->GetName().empty() == false) { + obj->loglevel = level; + } } + return true; + } - // 比較 - if (strcmp(name, "all") == 0) { - // "all" なら none 以外の全部にセット - for (auto& obj : gObjects) { - obj->loglevel = val; + std::vector found; // 候補オブジェクト + std::vector candidates; // 候補名 + + // 前方一致する名前(とそのオブジェクト)を列挙する + for (auto *obj : gObjects) { + const std::vector& aliases = obj->GetAliases(); + + for (const auto& a : aliases) { + // 完全一致したら即採用 + // (前方一致しつつ完全一致したらこの時点で採用して打ち切るため) + if (strcasecmp(a.c_str(), name.c_str()) == 0) { + obj->loglevel = level; + return true; } - } else { - // それ以外は一致するキーを探してセット。 - // 複数のオブジェクトが同じ logname を持ってもよい (CMMUとか)。 - std::string sname = name; - bool found = false; - for (auto& obj : gObjects) { - if (obj->logname == sname) { - obj->loglevel = val; - found = true; - } + + // 前方一致したら覚えておく + if (strncasecmp(a.c_str(), name.c_str(), name.size()) == 0) { + candidates.emplace_back(a); + found.push_back(obj); + } + } + } + + // 見付からない場合はエラー + if (found.empty()) { + *errmsg = string_format("Unknown logname \"%s\"", name.c_str()); + return false; + } + + // 1つだけなら確定 + if (found.size() == 1) { + Object *obj = found[0]; + obj->loglevel = level; + return true; + } + + // 複数あれば候補文字列を作成 + *errmsg = string_format("Ambiguous logname \"%s\": candidates are", + name.c_str()); + for (const auto& cand : candidates) { + *errmsg += string_format(" \"%s\"", cand.c_str()); + } + return false; +} + +// ログ名の一覧を取得する。 +// MainApp と Debugger から呼ばれる。 +/*static*/ std::vector +MainApp::GetLogNames() +{ + std::vector sortobj; + + // エイリアスを持つオブジェクトだけ抜き出す + for (const auto& obj : gObjects) { + if (obj->GetAliases().empty() == false) { + sortobj.emplace_back(obj); + } + } + + // aliases の一語目で sortobj をソートする + std::sort(sortobj.begin(), sortobj.end(), + [](const auto& a, const auto *b) { + const auto& sa = a->GetAliases()[0]; + const auto& sb = b->GetAliases()[0]; + return strcmp(sa.c_str(), sb.c_str()) < 0; + } + ); + + std::vector list; + for (const auto *obj : sortobj) { + const auto& aliases = obj->GetAliases(); + std::string str; + + str += aliases[0]; + if (aliases.size() > 1) { + str += " (alias:"; + for (int i = 1, sz = aliases.size(); i < sz; i++) { + str += ' '; + str += aliases[i]; } - // 見つからない場合はエラー - if (!found) { - printf("invalid logname '%s'\n", name); - goto abort; - } - } + str += ')'; + } + list.emplace_back(str); } + list.emplace_back("all"); - rv = true; - abort: - free(buf); - return rv; + return list; } // 関連するファイルのパスを取得。 @@ -500,3 +670,13 @@ MainApp::HasVMFeature(vmf_t feature) con uint32 vmf = 1 << GetVMType(); return (vmf & feature) != 0; } + +// コンパイルされている host netdriver の一覧を表示。 +void +MainApp::ShowHostnet() const +{ + auto list = NetDriverSelector::GetDrivers(); + for (const auto& name : list) { + printf(" %s\n", name.c_str()); + } +}