summaryrefslogtreecommitdiff
path: root/support/dktools/dk4apphlv.ctr
blob: aec00122f8cf11cc6790dfb89bcbf9e058517352 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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;
}