/* WARNING: This file was generated by dkct. Changes you make here will be lost if dkct is run again! You should modify the original source and run dkct on it. Original source: dk4apphlv.ctr */ /* Copyright (C) 2015-2017, Dirk Krause Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above opyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /** @file dk4apphlv.c The dk4apphlv module. */ #line 10 "dk4apphlv.ctr" #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" #line 27 "dk4apphlv.ctr" 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; }