summaryrefslogtreecommitdiff
path: root/support/dktools/dk4apphlv.ctr
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
committerNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
commite0c6872cf40896c7be36b11dcc744620f10adf1d (patch)
tree60335e10d2f4354b0674ec22d7b53f0f8abee672 /support/dktools/dk4apphlv.ctr
Initial commit
Diffstat (limited to 'support/dktools/dk4apphlv.ctr')
-rw-r--r--support/dktools/dk4apphlv.ctr248
1 files changed, 248 insertions, 0 deletions
diff --git a/support/dktools/dk4apphlv.ctr b/support/dktools/dk4apphlv.ctr
new file mode 100644
index 0000000000..aec00122f8
--- /dev/null
+++ b/support/dktools/dk4apphlv.ctr
@@ -0,0 +1,248 @@
+%% options
+
+copyright owner = Dirk Krause
+copyright year = 2015-xxxx
+license = bsd
+
+
+
+%% module
+
+#include "dk4conf.h"
+#include "dk4types.h"
+#include "dk4mpl.h"
+#include "dk4app.h"
+#include "dk4fs.h"
+#include "dk4tspdk.h"
+#include "dk4fput.h"
+#include "dk4mem.h"
+#include "dk4strm.h"
+#include "dk4strmr.h"
+#include "dk4enc.h"
+#include "dk4const.h"
+
+
+
+$!trace-include
+
+
+
+int
+dk4app_can_run_normally(dk4_app_t *app)
+{
+ int back = 0;
+ if (NULL != app) {
+ back = 1;
+ if (0 != app->hvl_cmd) {
+ back = 0;
+ }
+ }
+ return back;
+}
+
+
+
+/** Show all strings in a text array, line by line.
+ CRT on Windows: Required.
+ @param app Application structure.
+ @param array Array containing texts to show.
+*/
+static
+void
+dk4app_show_text_array(dk4_app_t *app, const dkChar * const *array)
+{
+ while(*array) {
+ dk4fputs(*(array++), stdout, NULL);
+ dk4fputc(dkT('\n'), stdout, NULL);
+ }
+}
+
+
+
+/** Object used to show help.
+*/
+typedef struct {
+ FILE *outfile; /**< Output file. */
+} dk4_help_shower_t;
+
+
+
+/** Handler function for single characters.
+ CRT on Windows: Required.
+ @param obj Object to modify while processing the character.
+ @param c Character to process.
+ @param pos Current position in file or data stream.
+ @param erp Error report, may be NULL.
+ @return DK4_TSP_RES_OK if the character was processed
+ successfully,
+ DK4_TSP_RES_ERROR if there was an error but we can
+ continue,
+ DK4_TSP_RES_FATAL if there was a fatal error so we
+ should abort processing.
+*/
+static
+int
+dk4app_help_show_char_handler(
+ void *obj,
+ dkChar c,
+ dk4_text_stream_position_t *pos,
+ dk4_er_t *erp
+)
+{
+ dk4_help_shower_t *helpshow;
+ if (dkT('\r') != c) {
+ helpshow = (dk4_help_shower_t *)obj;
+ dk4fputc(c, helpshow->outfile, NULL);
+ if (dkT('\n') == c) {
+ fflush(helpshow->outfile);
+ }
+ }
+ return (DK4_TSP_RES_OK);
+}
+
+
+
+/** Show short built-in help text.
+ CRT on Windows: Required.
+ @param app Application structure.
+*/
+static
+void
+dk4app_show_help(dk4_app_t *app)
+{
+ const dkChar * const *ptr;
+
+ if (NULL != app->help_text) {
+ ptr = app->help_text;
+ if (0 != (DK4_APP_CMD_ERROR & (app->hvl_cmd))) {
+ dk4fput_initialize_stderr();
+ } else {
+ dk4fput_initialize_stdout();
+ }
+ while (NULL != *ptr) {
+ dk4fputs(
+ *(ptr++),
+ ((DK4_APP_CMD_ERROR & (app->hvl_cmd)) ? stderr : stdout),
+ NULL
+ );
+ dk4fputc(
+ dkT('\n'),
+ ((DK4_APP_CMD_ERROR & (app->hvl_cmd)) ? stderr : stdout),
+ NULL
+ );
+ }
+ }
+}
+
+
+
+/** Show help text (full manual if available, short help text otherwise).
+ CRT on Windows: Required.
+ @param app Application structure.
+*/
+static
+void
+dk4app_show_manual(dk4_app_t *app)
+{
+ unsigned char inb[4096];
+ dkChar fnb[DK4_MAX_PATH];
+ dk4_tspdk_t tspdk;
+ dk4_stream_t *istrm;
+ dk4_help_shower_t helpshow;
+ size_t sz;
+ int res;
+ int done = 0;
+
+ if (NULL != app->help_file_name) {
+ res = dk4app_search_data_file(
+ fnb, DK4_SIZEOF(fnb,dkChar), app, app->help_file_name,
+ DK4_FS_DATA_MAX_SYS, 1, NULL
+ );
+ if (0 != res) {
+ istrm = dk4stream_open_file_reader(fnb, NULL);
+ if (NULL != istrm) {
+ if (0 != (DK4_APP_CMD_ERROR & (app->hvl_cmd))) {
+ dk4fput_initialize_stderr();
+ helpshow.outfile = stderr;
+ } else {
+ dk4fput_initialize_stdout();
+ helpshow.outfile = stdout;
+ }
+ res = dk4tspdk_setup_char(
+ &tspdk, (void *)(&helpshow), dk4app_help_show_char_handler,
+ app->encoding, DK4_FILE_ENCODING_UTF8, NULL
+ );
+ if (0 != res) {
+ do {
+ sz = sizeof(inb);
+ if (0 < dk4stream_read(inb, &sz, istrm, NULL)) {
+ if (0 < sz) {
+ (void)dk4tspdk_add_bytes(&tspdk, inb, sz);
+ done = 1;
+ }
+ } else {
+ sz = 0;
+ }
+ } while (0 < sz);
+ (void)dk4tspdk_finish(&tspdk);
+ }
+ dk4stream_close(istrm, NULL);
+ }
+ }
+ }
+ if (0 == done) {
+ dk4app_show_help(app);
+ }
+}
+
+
+
+int
+dk4app_help_version_license(dk4_app_t *app)
+{
+ int back = 0;
+ if (NULL != app) {
+ back = 1;
+ if (0 != app->hvl_cmd) {
+ if (DK4_APP_CMD_ERROR & (app->hvl_cmd)) {
+ dk4app_show_manual(app);
+ back = 0;
+ } else {
+ switch (app->hvl_cmd) {
+ case DK4_APP_CMD_MANUAL: {
+ dk4app_show_manual(app);
+ } break;
+ case DK4_APP_CMD_HELP: {
+ dk4app_show_help(app);
+ } break;
+ case DK4_APP_CMD_VERSION: {
+ if (NULL != app->vers_text) {
+ dk4fput_initialize_stdout();
+ dk4fputs(app->vers_text, stdout, NULL);
+ dk4fputc(dkT('\n'), stdout, NULL);
+ }
+ } break;
+ case DK4_APP_CMD_LICENSE: {
+ if (NULL != app->lic_text) {
+ dk4fput_initialize_stdout();
+ dk4app_show_text_array(app, app->lic_text);
+ }
+ } break;
+ default: {
+ /* ERROR: --help, --version, --license mutually exclusive! */
+ dk4app_log_base1(app, DK4_LL_ERROR, 81);
+ app->hvl_cmd |= DK4_APP_CMD_ERROR;
+ dk4app_show_manual(app);
+ back = 0;
+ } break;
+ }
+ }
+ } else {
+ /* BUG: None of the options found in command line */
+ back = 0;
+ }
+ }
+ return back;
+}
+
+