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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
|
/*
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: dk3app.ctr
*/
/*
Copyright (C) 2011-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.
*/
#ifndef DK3APP_H_INCLUDED
/** Avoid multiple inclusions. */
#define DK3APP_H_INCLUDED 1
#line 10 "dk3app.ctr"
/** @file dk3app.h Application support.
*/
#include "dk3conf.h"
#include "dk3types.h"
#ifdef __cplusplus
extern "C" {
#endif
/** Open application structure for command line application.
* Diagnostic messages are written to stderr and to the log file.
* The log file $HOME/log/<i>application</i>-<i>pid</i>.log
* is removed automatically by dk3app_close() unless there was an
* error message.
* @param argc Number of command line arguments.
* @param argv Command line arguments array.
* @param gn Application group name.
* @return Pointer to new application on success, NULL on error.
*/
dk3_app_t *
dk3app_open_command(int argc, dkChar const * const * argv, dkChar const *gn);
/** Open application structure for GUI application.
* Diagnostic messages are written to the log file only.
* The log file is removed automatically by dk3app_open() unless
* there were error messages.
* Typically this function is invoked from a DkWxAppHelper constructor.
* @param argc Number of command line arguments.
* @param argv Command line arguments array.
* @param gn Application group name.
* @return Pointer to new application on success, NULL on error.
*/
dk3_app_t *
dk3app_open_gui(int argc, dkChar const * const * argv, dkChar const *gn);
/** First silence check to correct the type if necessary.
Do not report errors directly, set app->de instead.
@param app Application structure.
@param argc Number of command line arguments.
@param argv Command line arguments array.
*/
int
dk3app_first_silence_check(dk3_app_t *app,int argc,dkChar const * const *argv);
/** Open application structure for a daemon.
* Log messages are appended to
* ${localstatedir}/log/<i>applicationname</i>/<i>applicationname</i>.log.
* @param wd Working directory when starting the program.
* @param argc Number of command line arguments.
* @param argv Command line arguments array.
* @param gn Application group name.
* @return Pointer to new application on success, NULL on error.
*/
dk3_app_t *
dk3app_open_daemon(dkChar const *wd, int argc, dkChar const * const * argv, dkChar const *gn);
/** Open application structure for a silent application (no diagnostics).
* @param argc Number of command line arguments.
* @param argv Command line arguments array.
* @param gn Application group name.
* @return Pointer to new application on success, NULL on error.
*/
dk3_app_t *
dk3app_open_silent(int argc, dkChar const * const * argv, dkChar const *gn);
/** Close application structure, release dynamic memory and resources
* assigned to the object.
* @param app Application to close.
*/
void
dk3app_close(dk3_app_t *app);
/** Log a message consisting of multiple parts.
* @param app Application structure.
* @param ll Log level.
* @param msg Message parts array. Pointer to an array
* of string pointers, all array elements are concatenated to
* create the message text.
* @param nmsg Number of elements in the array.
*/
void
dk3app_log_msg(dk3_app_t *app, int ll, dkChar const * const * msg, size_t nmsg);
/** Log message consisting of one part.
* @param app Application structure.
* @param ll Log level.
* @param msg Message texts array, typically localized, obtained
* from dk3app_messages().
* @param i1 Index of message text in \a msg.
*/
void
dk3app_log_1(dk3_app_t *app, int ll, dkChar const * const * msg, size_t i1);
/** Log message consisting of two parts.
* @param app Application structure.
* @param ll Log level.
* @param msg Message texts array, typically localized, obtained
* from dk3app_messages().
* @param i Index of message text in \a msg.
* @param st String to write as second part.
*/
void
dk3app_log_2(
dk3_app_t *app, int ll, dkChar const * const * msg,
size_t i, dkChar const *st
);
/** Log message consisting of three parts.
* @param app Application structure.
* @param ll Log level.
* @param msg Message string array.
* @param i1 Index of first message text in \a msg.
* @param i2 Index of third message text in \a msg.
* @param st Second message text.
*/
void
dk3app_log_3(
dk3_app_t *app, int ll, dkChar const * const * msg,
size_t i1, size_t i2, dkChar const *st
);
/** Log message consisting of five parts.
* @param app Application structure.
* @param ll Log level.
* @param msg Message string array.
* @param i1 Index of first message text in \a msg.
* @param i2 Index of third message text in \a msg.
* @param i3 Index of fifth message text in \a msg.
* @param st1 Second message text.
* @param st2 Fourth message text.
*/
void
dk3app_log_5(
dk3_app_t *app, int ll, dkChar const * const * msg,
size_t i1, size_t i2, size_t i3, dkChar const *st1, dkChar const *st2
);
/** Log message consisting of one part.
* @param app Application structure.
* @param ll Log level.
* @param i1 Index of message text in messages array.
*/
void
dk3app_log_i1(dk3_app_t *app, int ll, size_t i1);
/** Log message consisting of two parts.
* @param app Application structure.
* @param ll Log level.
* @param i Index of first message text in messages array.
* @param st Second message text.
*/
void
dk3app_log_i2(dk3_app_t *app, int ll, size_t i, dkChar const * st);
/** Log message consisting of three parts.
* @param app Application structure.
* @param ll Log level.
* @param i1 Index of first message text in messages array.
* @param i2 Index of third message text in messages array.
* @param st Second message text.
*/
void
dk3app_log_i3(
dk3_app_t *app, int ll,
size_t i1, size_t i2, dkChar const *st
);
/** Log message consisting of five parts.
* @param app Application structure.
* @param ll Log level.
* @param i1 Index of first message text in messages array.
* @param i2 Index of third message text in messages array.
* @param i3 Index of fifth message text in messages array.
* @param st1 Second message text.
* @param st2 Fourth message text.
*/
void
dk3app_log_i5(
dk3_app_t *app, int ll,
size_t i1, size_t i2, size_t i3, dkChar const *st1, dkChar const *st2
);
/** Set file name for current source file position.
The source file position is used when writing diagnostic messages.
@param app Application structure.
@param fn Source file name. Use NULL to disable printing
the file name.
*/
void
dk3app_set_source_file(dk3_app_t *app, dkChar const *fn);
/** Set source line number for current source file position.
The source file position is used when writing diagnostic messages.
@param app Application structure.
@param li Line number. Use 0UL to disable printing the
line number.
*/
void
dk3app_set_source_line(dk3_app_t *app, unsigned long li);
/** Get current source file name.
@param app Application structure.
@return Current source file name, may be NULL.
*/
dkChar const *
dk3app_get_source_file(dk3_app_t const *app);
/** Get current source line number.
@param app Application structure.
@return Current source file line number, may be 0UL.
*/
unsigned long
dk3app_get_source_line(dk3_app_t const *app);
/** Get number of command line arguments.
@param app Application structure.
@return Number of command line arguments.
*/
int
dk3app_get_argc(dk3_app_t const *app);
/** Get command line arguments array.
@param app Application structure.
@return Command line arguments array.
*/
dkChar const * const *
dk3app_get_argv(dk3_app_t const *app);
/** Retrieve application name.
@param app Application structure.
@return Application name on success, NULL on error.
*/
dkChar const *
dk3app_get_appname(dk3_app_t const *app);
/** Retrieve name of executable file.
* @param app Application structure.
* @return Executable file name on success, NULL on error.
*/
dkChar const *
dk3app_get_execfilename(dk3_app_t const *app);
/** Retrieve binary directory name.
@param app Application structure.
@return Name of bin directory on success, NULL on error.
*/
dkChar const *
dk3app_get_bindir(dk3_app_t const *app);
/** Get system configuration directory (/etc or /usr/local/etc).
@param app Application structure.
@return Directory name on success, NULL on error.
*/
dkChar const *
dk3app_get_etcdir(dk3_app_t const *app);
/** Get data root directory (/usr/share or /usr/local/share).
@param app Application structure.
@return Directory name on success, NULL on error.
*/
dkChar const *
dk3app_get_sharedir(dk3_app_t const *app);
/** Get local state directory (/var or /usr/local/var).
@param app Application structure.
@return Directory name on success, NULL on error.
*/
dkChar const *
dk3app_get_vardir(dk3_app_t const *app);
/** Get user login name.
@param app Application structure.
@return Login name on success, NULL on error.
*/
dkChar const *
dk3app_get_logname(dk3_app_t const *app);
/** Get host name.
@param app Application structure.
@return Host name on success, NULL on error.
*/
dkChar const *
dk3app_get_hostname(dk3_app_t const *app);
/** Get users home directory.
@param app Application structure.
@return Home directory on success, NULL on error.
*/
dkChar const *
dk3app_get_homedir(dk3_app_t const *app);
/** Get application group name.
@param app Application structure.
@return Group name on success, NULL on error.
*/
dkChar const *
dk3app_get_appgroup(dk3_app_t const *app);
/** Retrieve users language.
@param app Application structure.
@return Pointer to users language or NULL if no language is defined.
*/
dkChar const *
dk3app_get_language(dk3_app_t const *app);
/** Retrieve users region.
@param app Application structure.
@return Pointer to users region or NULL if no region is defined.
*/
dkChar const *
dk3app_get_region(dk3_app_t const *app);
/** Retrieve internal encoding.
@param app Application structure.
@return Internal (in-memory) encoding.
*/
int
dk3app_get_encoding(dk3_app_t const *app);
/** Retrieve output file encoding.
@param app Application structure.
@return Output file encoding.
*/
int
dk3app_get_output_encoding(dk3_app_t const *app);
/** Retrieve default encoding expected on stdin.
@param app Application structure.
@return Expected input encoding (DK3_FILE_ENCODING_xxx).
*/
int
dk3app_get_input_stdin_encoding(dk3_app_t const *app);
/** Retrieve default encoding expected on files.
@param app Application structure.
@return Expected input encoding (DK3_FILE_ENCODING_xxx).
*/
int
dk3app_get_input_file_encoding(dk3_app_t const *app);
/** Get file name for temporary file.
The file name is stored into the buffer @a db.
@param app Application structure.
@param db Destination buffer.
@param sz Size of \a db (number of characters).
@return 1 on success, 0 on error.
*/
int
dk3app_get_temp_file_name(dk3_app_t *app, dkChar *db, size_t sz);
/** Set a preference value.
Preferences set by this function are written to the
$HOME/.dk3app/<i>application</i>/dk3pref.conf file during
dk3app_close().
@param app Application structure.
@param k Key (preference name).
@param v Value.
@return 1 on success, 0 on error.
*/
int
dk3app_set_pref(dk3_app_t *app, dkChar const *k, dkChar const *v);
/** Get a preference value.
On success the preference value is written to @a d.
@param app Application structure.
@param k Key (preference name).
@param d Destination buffer.
@param s Size of \a d (number of characters).
@return 1 on success, 0 on error.
*/
int
dk3app_get_pref(dk3_app_t *app, dkChar const *k, dkChar *d, size_t s);
/** Get boolean preference value.
@param app Application structure.
@param name Preference name.
@param dv Default value if preference not found.
@return Preference value or default value.
*/
int
dk3app_get_pref_bool(dk3_app_t *app, dkChar const *name, int dv);
/** Get int preference value.
@param app Application structure.
@param name Preference name.
@param dv Default value.
@return Preference value or default value.
*/
int
dk3app_get_pref_int(dk3_app_t *app, dkChar const * name, int dv);
/** Get a system preference value.
Command line and user-set preferences are ignored.
@param app Application structure.
@param k Key (preference name).
@param d Destination buffer.
@param s Size of \a d (number of characters).
@return 1 on success, 0 on error.
*/
int
dk3app_get_sys_pref(dk3_app_t *app, dkChar const *k, dkChar *d, size_t s);
/** Unconfigure application.
@param app Application structure.
*/
void
dk3app_unconfigure(dk3_app_t *app);
/** Find configuration file.
On success the function returns a file name collection.
Configuration files with low priority are at the start of the
collection, files with higher priority are at the end.
This allows higher priorized configuration files to overwrite
settings made by the previously processed files.
After you are done with the search results set, use
dk3search_close() to close the set and release resources assigned
to it.
@param app Application structure.
@param fn File name to search for (short filename
without leading path).
@param exclu Flag: Exclude users configuration file.
@return Collection of file names (full path names including the
leading directory), use dk3search_close() after
use to destroy it.
*/
dk3_search_t *
dk3app_find_config_file(dk3_app_t *app, dkChar const *fn, int exclu);
/** Find configuration file in reverse search order
(most significant file first).
This function is typically used to process the first file from
the set only which is the most significant file.
After you are done with the search results set, use
dk3search_close() to close the set and release resources assigned
to it.
@param app Application structure.
@param fn File name to search for (short file name without
leading directory).
@param exclu Flag: Exclude users configuration files.
@return Collection of file names (full path names with leading
directory), use dk3search_close() after
use to destroy it.
*/
dk3_search_t *
dk3app_find_config_file_revers(dk3_app_t *app, dkChar const *fn, int exclu);
/** Find data file.
The full path name of the file found is written to @a db on success.
@param app Application structure.
@param fn File name to search for (short file name without leading
directory).
@param db Destination buffer.
@param sz Size of \a db (number of characters).
@return 1 on success, 0 on error.
*/
int
dk3app_find_data_file(dk3_app_t *app, dkChar const *fn, dkChar *db, size_t sz);
/** Get string table.
@param app Application structure.
@param fn Short file name of string table.
@param dv Default value, used if file not found.
@return New string table if found or \a dv on problems.
*/
dkChar const * const *
dk3app_messages(dk3_app_t *app, dkChar const *fn, dkChar const * const *dv);
/** Find and print help text.
The function searches for a help text file in the users
language. If no file is found the default text is printed.
@param app Application structure.
@param fn Help text file name (short name wih).
@param dt Default text to use if file not found.
*/
void
dk3app_help(dk3_app_t *app, dkChar const *fn, dkChar const * const *dt);
/** Get log level used for stderr.
@param app Application structure.
@return Log level required for logging to stderr.
*/
int
dk3app_get_stderr_log_level(dk3_app_t const *app);
/** Set log level used for stderr.
@param app Application structure.
@param ll New log level.
*/
void
dk3app_set_stderr_log_level(dk3_app_t *app, int ll);
/** Report write error.
@param app Application structure.
@param ll Log level.
@param wished Number of bytes wished to write.
@param written Number of bytes written.
*/
void
dk3app_log_write_error(dk3_app_t *app, int ll, size_t wished, size_t written);
/** Report fwrite error.
@param app Application structure.
@param ll Log level.
@param wished Number of elements wished to write.
@param written Number of elements written.
*/
void
dk3app_log_fwrite_error(dk3_app_t *app, int ll, size_t wished, size_t written);
/** Get a localized text.
@param app Application structure.
@param num Index of string in array.
@return Pointer to localized string on success, NULL on error.
*/
dkChar const *
dk3app_localized(dk3_app_t const *app, size_t num);
/** Get a non-localized text.
@param num Index of string in array.
@return Pointer to localized string on success, NULL on error.
*/
dkChar const *
dk3app_not_localized(size_t num);
/** Initialize random number generator.
Set up allowed PRNG types and seed best PRNG found.
If at least one of the PRNG types specified in @a il is available
we attempt to seed this PRNG.
The following preferences are used by this function:
- /rand/types is used if il is NULL.
- /rand/file/allowed configured whether or not usage o
seed file in the users home directory is allowed
- /rand/egd/name can be used to specify of the EDG socket
if an EGD (entropy gathering daemon) is running on the
system. By default /dev/urandom and/or /dev/random
are used to seed a PRNG if available.
@param app Application structure.
@param il Text line to process, may be NULL.
If this argument is NULL, the /rand/types preference is used
to find the allowed PRNG types.
@return 1 on success, 0 on error.
*/
int
dk3app_rand_init(dk3_app_t *app, dkChar const *il);
/** Retrieve list of allowed PRNG types from a text line.
If the text line \a il pointer is NULL the
/rand/types preference value is used.
@param app Application structure.
@param il Text line to process.
@return Combination of DK3_PRNG_xxx.
*/
int
dk3app_get_allowed_prng_types(dk3_app_t *app, dkChar const *il);
/** Retrieve pseudo-random bytes.
@param app Application structure.
@param db Destination buffer.
@param sz Size of \a db (number of bytes).
@return Number of bytes found.
*/
size_t
dk3app_rand_bytes(dk3_app_t *app, void *db, size_t sz);
/** Retrieve pseudo-random bytes.
The bytes will not be used for cryptographic or authentication
purposes, so we can use a less unpredictive (less complicated und
thus faster) generation mechanism.
@param app Application structure.
@param db Destination buffer.
@param sz Size of \a db (number of bytes).
@return Number of bytes found.
*/
size_t
dk3app_rand_bytes_non_crypto(dk3_app_t *app, void *db, size_t sz);
/** Attempt to save random seed to file when closing an application.
@param app Application structure.
*/
void
dk3app_rand_end(dk3_app_t *app);
/** Get default input encoding for stdin ignoring preferences.
@param app Application structure.
@return Encoding (DK3_FILE_ENCODING_xxx).
*/
int
dk3app_get_default_stdin_encoding(dk3_app_t const *app);
/** Get default input encoding for files ignoring preferences.
@param app Application structure.
@return Encoding (DK3_FILE_ENCODING_xxx).
*/
int
dk3app_get_default_file_encoding(dk3_app_t const *app);
/** Process standard input character by character.
Attempt to save stdin data to file so we can do a BOM check.
@param app Application structure.
@param obj Object to modify while processing data.
@param hf Handler function.
@param de Default encoding.
*/
int
dk3app_process_stdin_chars(
dk3_app_t *app,
void *obj,
dk3_char_handler_t *hf,
int de
);
/** Process standard input line by line.
Attempt to save stdin data to file so we can do a BOM check.
@param app Application structure.
@param obj Object to modify while reading data.
@param hf Handler function.
@param buffer Line buffer.
@param szbuffer Size of buffer.
@param de Default encoding.
*/
int
dk3app_process_stdin_lines(
dk3_app_t *app,
void *obj,
dk3_line_handler_t *hf,
dkChar *buffer,
size_t szbuffer,
int de
);
/** Get maximum log level used by the application.
@param app Application structure.
@return Maximum log level used by the application.
*/
int
dk3app_max_log_level(dk3_app_t const *app);
/** Remove trailing newline and handle backslash escape sequences.
This function is run on lines read from string table file.
@param lb Line buffer.
*/
void
dk3app_squeeze_line(char *lb);
#if !DK3_ON_WINDOWS
#if DK3_HAVE_SETUID && DK3_HAVE_SETGID && DK3_HAVE_CHOWN
#if DK3_CHAR_SIZE == 1
/** Change ownership to writable files.
This function is used by daemons before switching to a
non-privileged user account.
@param app Application structure.
@param uid New user ID for process.
@param gid New group ID for process.
@return 1 on success, 0 on error.
*/
int
dk3app_change_user(dk3_app_t *app, uid_t uid, gid_t gid);
#endif
#endif
#endif
/** Retrieve directory setting from preference.
@param app Application structure.
@param pn Preference name.
@param buf Result buffer.
@param bs Size of bs (number of dkChar).
@param ch Flag: Check existance of directory.
@return 1 on success, 0 on error.
*/
int
dk3app_get_dir_from_pref(
dk3_app_t *app,
dkChar const *pn,
dkChar *buf,
size_t bs,
int ch
);
#ifdef __cplusplus
}
#endif
#endif
|