7 changed files with 278 additions and 59 deletions
@ -0,0 +1,126 @@
|
||||
/**
|
||||
* ***** BEGIN LICENSE BLOCK ***** |
||||
* |
||||
* Copyright 2017-2022 Yzena Tech |
||||
* |
||||
* Licensed under the Yzena Viral User License, Version 0.1 (the "Yzena Viral |
||||
* User License" or "YVUL"), the GNU Affero General Public License (the "GNU |
||||
* AGPL"), Version 3.0, and the Server Side Public License (the "SSPL"), |
||||
* Version 1. You may not use this file except in compliance with all of those |
||||
* licenses. |
||||
* |
||||
* You may obtain a copy of the Yzena Viral User License at |
||||
* |
||||
* https://yzena.com/yzena-viral-user-license/
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the Yzena Viral User License is distributed under the |
||||
* following disclaimer: |
||||
* |
||||
* As far as the law allows, this software comes as is, without any |
||||
* warranty or condition, and no contributor will be liable to anyone for |
||||
* any damages related to this software or this license, under any kind of |
||||
* legal claim. |
||||
* |
||||
* You may obtain a copy of the GNU Affero General Public License at |
||||
* |
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the GNU Affero General Public License is distributed under |
||||
* the following disclaimer: |
||||
* |
||||
* This software is distributed in the hope that it will be useful, but |
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero |
||||
* General Public License for more details. |
||||
* |
||||
* You may obtain a copy of the Server Side Public License at |
||||
* |
||||
* https://www.mongodb.com/licensing/server-side-public-license
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the Server Side Public License is distributed under the |
||||
* following disclaimer: |
||||
* |
||||
* This software is distributed in the hope that it will be useful, but |
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Server |
||||
* Side Public License for more details. |
||||
* |
||||
* ****** END LICENSE BLOCK ****** |
||||
* |
||||
* ***************************************************************** |
||||
* |
||||
* ******* BEGIN FILE DESCRIPTION ******* |
||||
* |
||||
* Source file with main(), to be dispatched to the correct program. This is |
||||
* separate because all programs need the same setup. |
||||
* |
||||
* ******** END FILE DESCRIPTION ******** |
||||
*/ |
||||
|
||||
#include <yc/yc.h> |
||||
|
||||
#include "main.h" |
||||
|
||||
#include "rig/rig.h" |
||||
|
||||
static const y_Main mains[] = { |
||||
{ .name = y_PATHSTR("rigc"), .func = rigc_main, .need_children = false, .need_timers = false }, |
||||
{ .name = y_PATHSTR("rig"), .func = rig_main, .need_children = true, .need_timers = true }, |
||||
}; |
||||
|
||||
int |
||||
main(int argc, y_pchar* argv[]) |
||||
{ |
||||
y_Status s; |
||||
y_MainFunc mainfunc = NULL; |
||||
bool need_children = false; |
||||
bool need_timers = false; |
||||
size_t i; |
||||
|
||||
if (y_err(argv == NULL || argv[0] == NULL)) |
||||
{ |
||||
// We don't have output at this point; we need to just quit.
|
||||
exit((int) y_status_exits[y_STATUS_MISSING_CMD_LINE_ARG]); |
||||
} |
||||
|
||||
// This is before y_main() because I only want to start the signal/process
|
||||
// thread if it is needed. This function is also specifically written to be
|
||||
// safe in this case.
|
||||
y_pchar* b = y_pbasename(argv[0]); |
||||
|
||||
for (i = 0; i < sizeof(mains) / sizeof(y_Main); ++i) |
||||
{ |
||||
if (!y_pstrcmp(b, mains[i].name)) |
||||
{ |
||||
mainfunc = mains[i].func; |
||||
need_children = mains[i].need_children; |
||||
need_timers = mains[i].need_timers; |
||||
break; |
||||
} |
||||
} |
||||
|
||||
s = y_main(need_children, need_timers); |
||||
if (y_err(s != y_STATUS_SUCCESS)) y_errexit(s); |
||||
|
||||
// This is after the call to y_main() in order to have I/O available.
|
||||
if (y_err(mainfunc == NULL)) |
||||
{ |
||||
y_panica("Unrecognized command name"); |
||||
} |
||||
|
||||
y_StackPool p = y_strucon_stackpool(); |
||||
|
||||
y_stackpool_enterScope(p); |
||||
|
||||
s = mainfunc(argc, argv); |
||||
|
||||
#ifdef YC_DEBUG |
||||
y_stackpool_exitScope(p); |
||||
#endif // YC_DEBUG
|
||||
|
||||
// TODO: Make sure every error path prints a message.
|
||||
y_exit(y_status_exits[s]); |
||||
} |
@ -0,0 +1,115 @@
|
||||
/**
|
||||
* ***** BEGIN LICENSE BLOCK ***** |
||||
* |
||||
* Copyright 2017-2022 Yzena Tech |
||||
* |
||||
* Licensed under the Yzena Viral User License, Version 0.1 (the "Yzena Viral |
||||
* User License" or "YVUL"), the GNU Affero General Public License (the "GNU |
||||
* AGPL"), Version 3.0, and the Server Side Public License (the "SSPL"), |
||||
* Version 1. You may not use this file except in compliance with all of those |
||||
* licenses. |
||||
* |
||||
* You may obtain a copy of the Yzena Viral User License at |
||||
* |
||||
* https://yzena.com/yzena-viral-user-license/
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the Yzena Viral User License is distributed under the |
||||
* following disclaimer: |
||||
* |
||||
* As far as the law allows, this software comes as is, without any |
||||
* warranty or condition, and no contributor will be liable to anyone for |
||||
* any damages related to this software or this license, under any kind of |
||||
* legal claim. |
||||
* |
||||
* You may obtain a copy of the GNU Affero General Public License at |
||||
* |
||||
* https://www.gnu.org/licenses/agpl-3.0.html
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the GNU Affero General Public License is distributed under |
||||
* the following disclaimer: |
||||
* |
||||
* This software is distributed in the hope that it will be useful, but |
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero |
||||
* General Public License for more details. |
||||
* |
||||
* You may obtain a copy of the Server Side Public License at |
||||
* |
||||
* https://www.mongodb.com/licensing/server-side-public-license
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the Server Side Public License is distributed under the |
||||
* following disclaimer: |
||||
* |
||||
* This software is distributed in the hope that it will be useful, but |
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Server |
||||
* Side Public License for more details. |
||||
* |
||||
* ****** END LICENSE BLOCK ****** |
||||
* |
||||
* ***************************************************************** |
||||
* |
||||
* ******* BEGIN FILE DESCRIPTION ******* |
||||
* |
||||
* Definitions needed for a main file. |
||||
* |
||||
* ******** END FILE DESCRIPTION ******** |
||||
*/ |
||||
|
||||
#ifndef YC_MAIN_PRIVATE_H |
||||
#define YC_MAIN_PRIVATE_H |
||||
|
||||
/* For C++ compatibility */ |
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
#include <yc/yc.h> |
||||
|
||||
#include "yc.h" |
||||
|
||||
//! @cond INTERNAL
|
||||
|
||||
/**
|
||||
* @file src/main.h |
||||
*/ |
||||
|
||||
/**
|
||||
* @defgroup main_internal main_internal |
||||
* @brief Internal definitions for the main() function. |
||||
* @{ |
||||
*/ |
||||
|
||||
/**
|
||||
* A struct with information about a program. |
||||
*/ |
||||
typedef struct y_Main |
||||
{ |
||||
/// The name of the program.
|
||||
const y_pchar* const name; |
||||
|
||||
/// The main() function to call.
|
||||
y_MainFunc func; |
||||
|
||||
/// Whether the program needs child processes.
|
||||
bool need_children; |
||||
|
||||
/// Whether the program needs timers.
|
||||
bool need_timers; |
||||
|
||||
} y_Main; |
||||
|
||||
/**
|
||||
* @} |
||||
*/ |
||||
|
||||
//! @endcond INTERNAL
|
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif // YC_MAIN_PRIVATE_H
|
Loading…
Reference in new issue