-
Notifications
You must be signed in to change notification settings - Fork 0
/
prog.c
2940 lines (2162 loc) · 66.9 KB
/
prog.c
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* libslack - https://libslack.org
*
* Copyright (C) 1999-2004, 2010, 2020-2023 raf <raf@raf.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*
* 20230824 raf <raf@raf.org>
*/
/*
=head1 NAME
I<libslack(prog)> - program framework module
=head1 SYNOPSIS
#include <slack/std.h>
#include <slack/prog.h>
typedef struct option option;
typedef struct Option Option;
typedef struct Options Options;
typedef void opt_action_int_t(int arg);
typedef void opt_action_optional_int_t(int *arg);
typedef void opt_action_string_t(const char *arg);
typedef void opt_action_optional_string_t(const char *arg);
typedef void opt_action_none_t(void);
typedef void func_t(void);
enum OptionArgument
{
OPT_NONE,
OPT_INTEGER,
OPT_STRING
};
enum OptionAction
{
OPT_NOTHING,
OPT_VARIABLE,
OPT_FUNCTION
};
typedef enum OptionArgument OptionArgument;
typedef enum OptionAction OptionAction;
struct Option
{
const char *name;
char short_name;
const char *argname;
const char *desc;
int has_arg;
OptionArgument arg_type;
OptionAction action;
void *object;
func_t *function;
};
struct Options
{
Options *parent;
Option *options;
};
void prog_init(void);
const char *prog_set_name(const char *name);
Options *prog_set_options(Options *options);
const char *prog_set_syntax(const char *syntax);
const char *prog_set_desc(const char *desc);
const char *prog_set_version(const char *version);
const char *prog_set_date(const char *date);
const char *prog_set_author(const char *author);
const char *prog_set_contact(const char *contact);
const char *prog_set_vendor(const char *vendor);
const char *prog_set_url(const char *url);
const char *prog_set_legal(const char *legal);
Msg *prog_set_out(Msg *out);
Msg *prog_set_err(Msg *err);
Msg *prog_set_dbg(Msg *dbg);
Msg *prog_set_alert(Msg *alert);
ssize_t prog_set_debug_level(size_t debug_level);
ssize_t prog_set_verbosity_level(size_t verbosity_level);
int prog_set_locker(Locker *locker);
const char *prog_name(void);
const Options *prog_options(void);
const char *prog_syntax(void);
const char *prog_desc(void);
const char *prog_version(void);
const char *prog_date(void);
const char *prog_author(void);
const char *prog_contact(void);
const char *prog_vendor(void);
const char *prog_url(void);
const char *prog_legal(void);
Msg *prog_out(void);
Msg *prog_err(void);
Msg *prog_dbg(void);
Msg *prog_alert(void);
size_t prog_debug_level(void);
size_t prog_verbosity_level(void);
int prog_out_fd(int fd);
int prog_out_stdout(void);
int prog_out_file(const char *path);
int prog_out_syslog(const char *ident, int option, int facility, int priority);
int prog_out_push_filter(msg_filter_t *filter);
int prog_out_none(void);
int prog_err_fd(int fd);
int prog_err_stderr(void);
int prog_err_file(const char *path);
int prog_err_syslog(const char *ident, int option, int facility, int priority);
int prog_err_push_filter(msg_filter_t *filter);
int prog_err_none(void);
int prog_dbg_fd(int fd);
int prog_dbg_stdout(void);
int prog_dbg_stderr(void);
int prog_dbg_file(const char *path);
int prog_dbg_syslog(const char *id, int option, int facility, int priority);
int prog_dbg_push_filter(msg_filter_t *filter);
int prog_dbg_none(void);
int prog_alert_fd(int fd);
int prog_alert_stdout(void);
int prog_alert_stderr(void);
int prog_alert_file(const char *path);
int prog_alert_syslog(const char *id, int option, int facility, int priority);
int prog_alert_push_filter(msg_filter_t *filter);
int prog_alert_none(void);
int prog_opt_process(int ac, char **av);
void prog_usage_msg(const char *format, ...);
void prog_help_msg(void);
void prog_version_msg(void);
const char *prog_basename(const char *path);
extern Options prog_options_table[1];
int opt_process(int argc, char **argv, Options *options, char *msgbuf, size_t bufsize);
char *opt_usage(char *buf, size_t size, Options *options);
=head1 DESCRIPTION
This module provides administrative services for arbitrary programs. The
services include program identification; flexible, complete command line
option processing; help, usage and version messages; flexible debug,
verbose, error and normal messaging (simple call syntax with arbitrary
message destinations including multiplexing).
This module exposes an alternate interface to I<GNU getopt_long(3)>. It
defines a way to specify command line option syntax, semantics and
descriptions in multiple, discrete chunks. The I<getopt> functions require
that the client specify the syntax and partial semantics for all options in
the same place (if it is to be done statically). This can be annoying when
library modules require their own command line options. This module allows
various parts of a program to (statically) specify their own command line
options independently, and link them together via C<parent> pointers.
Option syntax is specified in much the same way as for I<GNU
getopt_long(3)>. Option semantics are specified by an action
(C<OPT_NOTHING>, C<OPT_VARIABLE> or C<OPT_FUNCTION>), an argument type
(C<OPT_NONE>, C<OPT_INTEGER> or C<OPT_STRING>), and either an object
(C<int *>, C<char **>) or function (C<func()>, C<func(int)> or C<func(char *)>).
The I<opt_process(3)> and I<opt_usage(3)> functions are used by the I<prog>
functions and needn't be used directly. Instead, use I<prog_opt_process(3)>
to execute options and I<prog_usage_msg(3)> and I<prog_help_msg(3)> to
construct usage and help messages directly from the supplied option data.
They are exposed in case you don't want to use any other part of this
module.
=over 4
=cut
*/
#ifndef _BSD_SOURCE
#define _BSD_SOURCE /* For snprintf() on OpenBSD-4.7 */
#endif
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE /* New name for _BSD_SOURCE */
#endif
#include "config.h"
#include "std.h"
#include "msg.h"
#include "err.h"
#include "mem.h"
#include "prog.h"
#ifndef HAVE_SNPRINTF
#include "snprintf.h"
#endif
typedef struct Prog Prog;
struct Prog
{
const char *name;
Options *options;
const char *syntax;
const char *desc;
const char *version;
const char *date;
const char *author;
const char *contact;
const char *vendor;
const char *url;
const char *legal;
Msg *out;
Msg *err;
Msg *dbg;
Msg *log;
size_t debug_level;
size_t verbosity_level;
Locker *locker;
};
#ifndef TEST
static Prog g =
{
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, NULL
};
/*
=item C<void prog_init(void)>
Initialises the message, error, debug, and alert destinations to C<stdout>,
C<stderr>, C<stderr>, and C<stderr>, respectively. These are all C<null> by
default so this function must be called before any messages are emitted.
=cut
*/
void prog_init(void)
{
prog_out_stdout();
prog_err_stderr();
prog_dbg_stderr();
prog_alert_stderr();
}
/*
=item C<const char *prog_set_name(const char *name)>
Sets the program's name to C<name>. This is used when composing usage, help,
version, and error messages. On success, returns C<name>. On error, returns
C<null> with C<errno> set appropriately.
=cut
*/
#define RDLOCK(ret) { int rc; if ((rc = locker_rdlock(g.locker))) { set_errno(rc); return (ret); } }
#define WRLOCK(ret) { int rc; if ((rc = locker_wrlock(g.locker))) { set_errno(rc); return (ret); } }
#define UNLOCK(ret) { int rc; if ((rc = locker_unlock(g.locker))) { set_errno(rc); return (ret); } }
#define PROG_SET_STR_AND_RETURN(name, value) \
WRLOCK(NULL) \
name = value; \
UNLOCK(NULL) \
return value
#define PROG_SET_MSG_AND_RETURN(name, value) \
WRLOCK(NULL) \
if (name && name != value) \
msg_release(name); \
name = value; \
UNLOCK(NULL) \
return value
#define PROG_POP_MSG(name, lvalue) \
WRLOCK(-1) \
lvalue = name; \
name = NULL; \
UNLOCK(-1) \
#define PROG_SET_INT_AND_RETURN_PREVIOUS(name, value) \
size_t prev; \
WRLOCK(-1) \
prev = name; \
name = value; \
UNLOCK(-1) \
return prev
#define PROG_GET_PTR_AND_RETURN(name) \
void *value; \
RDLOCK(NULL) \
value = (void *)name; \
UNLOCK(NULL) \
return value
#define PROG_GET_INT_AND_RETURN(name) \
int value; \
RDLOCK(0) \
value = name; \
UNLOCK(0) \
return value
const char *prog_set_name(const char *name)
{
PROG_SET_STR_AND_RETURN(g.name, name);
}
/*
=item C<Options *prog_set_options(Options *options)>
Sets the program's options to C<options>. This is used when processing the
command line options with I<prog_opt_process(3)>. On success, returns
C<options>. On error, returns C<null> with C<errno> set appropriately.
=cut
*/
Options *prog_set_options(Options *options)
{
PROG_SET_STR_AND_RETURN(g.options, options);
}
/*
=item C<const char *prog_set_syntax(const char *syntax)>
Sets the program's command line syntax summary to C<syntax>. This is used
when composing usage and help messages. It must contain a one line summary
of the command line arguments, excluding the program name (e.g. C<"[options]
arg...">). On success, returns C<syntax>. On error, returns C<null> with
C<errno> set appropriately.
=cut
*/
const char *prog_set_syntax(const char *syntax)
{
PROG_SET_STR_AND_RETURN(g.syntax, syntax);
}
/*
=item C<const char *prog_set_desc(const char *desc)>
Sets the program's description to C<desc>. This is used when composing help
messages. On success, returns C<desc>. On error, returns C<null> with
C<errno> set appropriately.
=cut
*/
const char *prog_set_desc(const char *desc)
{
PROG_SET_STR_AND_RETURN(g.desc, desc);
}
/*
=item C<const char *prog_set_version(const char *version)>
Sets the program's version string to C<version>. This is used when composing
help and version messages. On success, returns C<version>. On error, returns
C<null> with C<errno> set appropriately.
=cut
*/
const char *prog_set_version(const char *version)
{
PROG_SET_STR_AND_RETURN(g.version, version);
}
/*
=item C<const char *prog_set_date(const char *date)>
Sets the program's release date to C<date>. This is used when composing help
messages. On success, returns C<date>. On error, returns C<null> with
C<errno> set appropriately.
=cut
*/
const char *prog_set_date(const char *date)
{
PROG_SET_STR_AND_RETURN(g.date, date);
}
/*
=item C<const char *prog_set_author(const char *author)>
Sets the program's author to C<author>. This is used when composing help
messages. It must contain the (free format) name of the author. Returns
C<author>. On error, returns C<null> with C<errno> set appropriately.
=cut
*/
const char *prog_set_author(const char *author)
{
PROG_SET_STR_AND_RETURN(g.author, author);
}
/*
=item C<const char *prog_set_contact(const char *contact)>
Sets the program's contact address to C<contact>. This is used when
composing help messages. It must contain the email address to which bug
reports should be sent. On success, returns C<contact>. On error, returns
C<null> with C<errno> set appropriately.
=cut
*/
const char *prog_set_contact(const char *contact)
{
PROG_SET_STR_AND_RETURN(g.contact, contact);
}
/*
=item C<const char *prog_set_vendor(const char *vendor)>
Sets the program's vendor to C<vendor>. This is used when composing help
messages. It must contain the (free format) name of the vendor. Returns
C<vendor>. On error, returns C<null> with C<errno> set appropriately.
=cut
*/
const char *prog_set_vendor(const char *vendor)
{
PROG_SET_STR_AND_RETURN(g.vendor, vendor);
}
/*
=item C<const char *prog_set_url(const char *url)>
Sets the program's URL to C<url>. This is used when composing help messages.
It must contain the URL where the program can be downloaded. On success,
returns C<url>. On error, returns C<null> with C<errno> set appropriately.
=cut
*/
const char *prog_set_url(const char *url)
{
PROG_SET_STR_AND_RETURN(g.url, url);
}
/*
=item C<const char *prog_set_legal(const char *legal)>
Sets the program's legal notice to C<legal>. This is used when composing
help messages. It is assumed that the legal notice may contain multiple
lines and so must contain its own newline characters. On success, returns
C<legal>. On error, returns C<null> with C<errno> set appropriately.
=cut
*/
const char *prog_set_legal(const char *legal)
{
PROG_SET_STR_AND_RETURN(g.legal, legal);
}
/*
=item C<Msg *prog_set_out(Msg *out)>
Sets the program's message destination to C<out>. This is used by I<msg(3)>
and I<vmsg(3)> which are, in turn, used to emit usage, version and help
messages. The program message destination is set to standard output by
I<prog_init(3)>, but it can be anything. However, it is probably best to
leave it as standard output at least until after command line option
processing is complete. See I<msg(3)> for details. On success, returns
C<out>. On error, returns C<null> with C<errno> set appropriately.
=cut
*/
Msg *prog_set_out(Msg *out)
{
PROG_SET_MSG_AND_RETURN(g.out, out);
}
/*
=item C<Msg *prog_set_err(Msg *err)>
Sets the program's error message destination to C<err>. This is used by
I<error(3)>, I<errorsys(3)>, I<fatal(3)>, I<fatalsys(3)>, I<dump(3)> and
I<dumpsys(3)>. The program error message destination is set to standard
error by I<prog_init(3)>, but it can be anything. See I<msg(3)> for details.
On success, returns C<err>. On error, returns C<null> with C<errno> set
appropriately.
=cut
*/
Msg *prog_set_err(Msg *err)
{
PROG_SET_MSG_AND_RETURN(g.err, err);
}
/*
=item C<Msg *prog_set_dbg(Msg *dbg)>
Sets the program's debug message destination to C<dbg>. This is set to
standard error by I<prog_init(3)>, but it can be set to anything. See
I<msg(3)> for details. On success, returns C<dbg>. On error, returns C<null>
with C<errno> set appropriately.
=cut
*/
Msg *prog_set_dbg(Msg *dbg)
{
PROG_SET_MSG_AND_RETURN(g.dbg, dbg);
}
/*
=item C<Msg *prog_set_alert(Msg *alert)>
Sets the program's alert message destination to C<alert>. This is set to
standard error by I<prog_init(3)> but it can be set to anything. See
I<msg(3)> for details. On success, returns C<alert>. On error, returns
C<null> with C<errno> set appropriately.
=cut
*/
Msg *prog_set_alert(Msg *alert)
{
PROG_SET_MSG_AND_RETURN(g.log, alert);
}
/*
=item C<ssize_t prog_set_debug_level(size_t debug_level)>
Sets the program's debug level to C<debug_level>. This is used when
determining whether or not to emit a debug message. The debug level
comprises two parts, the I<section> and the I<level>. The I<level> occupies
the low byte of C<debug_level>. The I<section> occupies the next three
bytes. This enables debugging to be partitioned into sections, allowing
users to turn on debugging at any level (from 0 up to 255) for particular
sections of a program (at most 24). Debug messages with a section value
whose bits overlap those of the program's current debug section and with a
level that is less than or equal to the program's current debug level are
emitted. As a convenience, if the program debug section is zero, debug
messages with a sufficiently small level are emitted regardless of the
message section. On success, returns the previous debug level. On error,
returns C<-1> with C<errno> set appropriately.
Example:
#define LEXER_SECTION (1 << 8)
#define PARSER_SECTION (2 << 8)
#define INTERP_SECTION (4 << 8)
prog_set_debug_level(LEXER_SECTION | PARSER_SECTION | 1);
debug((LEXER_SECTION | 1, "lexer debugmsg")) // yes
debug((LEXER_SECTION | 4, "lexer debugmsg")) // no (level too high)
debug((PARSER_SECTION | 1, "parser debugmsg")) // yes
debug((INTERP_SECTION | 1, "interp debugmsg")) // no (wrong section)
debug((1, "global debug")) // no (no section to match)
prog_set_debug_level(1);
debug((LEXER_SECTION | 1, "lexer debugmsg")) // yes
debug((LEXER_SECTION | 4, "lexer debugmsg")) // no (level too high)
debug((PARSER_SECTION | 1, "parser debugmsg")) // yes
debug((INTERP_SECTION | 1, "interp debugmsg")) // yes
debug((1, "global debugmsg")) // yes
debug((4, "global debugmsg")) // no (level too high)
=cut
*/
ssize_t prog_set_debug_level(size_t debug_level)
{
PROG_SET_INT_AND_RETURN_PREVIOUS(g.debug_level, debug_level);
}
/*
=item C<ssize_t prog_set_verbosity_level(size_t verbosity_level)>
Sets the program's verbosity level to C<verbosity_level>. This is used to
determine whether or not to emit verbose messages. Verbose messages with a
level that is less than or equal to the program's current verbosity level
are emitted. On success, returns the previous verbosity level. On error,
returns C<-1> with C<errno> set appropriately.
=cut
*/
ssize_t prog_set_verbosity_level(size_t verbosity_level)
{
PROG_SET_INT_AND_RETURN_PREVIOUS(g.verbosity_level, verbosity_level);
}
/*
=item C<int prog_set_locker(Locker *locker)>
Sets the locker (multiple thread synchronisation strategy) for this module.
This is only needed in multi-threaded programs. See I<locker(3)> for
details. On success, returns C<0>. On error, returns C<-1> with C<errno> set
appropriately.
=cut
*/
int prog_set_locker(Locker *locker)
{
if (g.locker)
return -1;
g.locker = locker;
return 0;
}
/*
=item C<const char *prog_name(void)>
Returns the program's name as set by I<prog_set_name(3)>. On error, returns
C<null> with C<errno> set appropriately.
=cut
*/
const char *prog_name(void)
{
PROG_GET_PTR_AND_RETURN(g.name);
}
/*
=item C<const Options *prog_options(void)>
Returns the program's options as set by I<prog_set_options(3)>. On error,
returns C<null> with C<errno> set appropriately.
=cut
*/
const Options *prog_options(void)
{
PROG_GET_PTR_AND_RETURN(g.options);
}
/*
=item C<const char *prog_syntax(void)>
Returns the program's command line syntax summary as set by
I<prog_set_syntax(3)>. On error, returns C<null> with C<errno> set
appropriately.
=cut
*/
const char *prog_syntax(void)
{
PROG_GET_PTR_AND_RETURN(g.syntax);
}
/*
=item C<const char *prog_desc(void)>
Returns the program's description as set by I<prog_set_desc(3)>. On error,
returns C<null> with C<errno> set appropriately.
=cut
*/
const char *prog_desc(void)
{
PROG_GET_PTR_AND_RETURN(g.desc);
}
/*
=item C<const char *prog_version(void)>
Returns the program's version string as set by I<prog_set_version(3)>. On
error, returns C<null> with C<errno> set appropriately.
=cut
*/
const char *prog_version(void)
{
PROG_GET_PTR_AND_RETURN(g.version);
}
/*
=item C<const char *prog_date(void)>
Returns the program's release date as set by I<prog_set_date(3)>. On error,
returns C<null> with C<errno> set appropriately.
=cut
*/
const char *prog_date(void)
{
PROG_GET_PTR_AND_RETURN(g.date);
}
/*
=item C<const char *prog_author(void)>
Returns the program's author as set by I<prog_set_author(3)>. On error,
returns C<null> with C<errno> set appropriately.
=cut
*/
const char *prog_author(void)
{
PROG_GET_PTR_AND_RETURN(g.author);
}
/*
=item C<const char *prog_contact(void)>
Returns the program's contact address as set by I<prog_set_contact(3)>. On
error, returns C<null> with C<errno> set appropriately.
=cut
*/
const char *prog_contact(void)
{
PROG_GET_PTR_AND_RETURN(g.contact);
}
/*
=item C<const char *prog_vendor(void)>
Returns the program's vendor as set by I<prog_set_vendor(3)>. On error,
returns C<null> with C<errno> set appropriately.
=cut
*/
const char *prog_vendor(void)
{
PROG_GET_PTR_AND_RETURN(g.vendor);
}
/*
=item C<const char *prog_url(void)>
Returns the program's URL as set by I<prog_set_url(3)>. On error, returns
C<null> with C<errno> set appropriately.
=cut
*/
const char *prog_url(void)
{
PROG_GET_PTR_AND_RETURN(g.url);
}
/*
=item C<const char *prog_legal(void)>
Returns the program's legal notice as set by I<prog_set_legal(3)>. On error,
returns C<null> with C<errno> set appropriately.
=cut
*/
const char *prog_legal(void)
{
PROG_GET_PTR_AND_RETURN(g.legal);
}
/*
=item C<Msg *prog_out(void)>
Returns the program's message destination as set by I<prog_set_out(3)>. On
error, returns C<null> with C<errno> set appropriately.
=cut
*/
Msg *prog_out(void)
{
PROG_GET_PTR_AND_RETURN(g.out);
}
/*
=item C<Msg *prog_err(void)>
Returns the program's error message destination as set by
I<prog_set_err(3)>. On error, returns C<null> with C<errno> set
appropriately.
=cut
*/
Msg *prog_err(void)
{
PROG_GET_PTR_AND_RETURN(g.err);
}
/*
=item C<Msg *prog_dbg(void)>
Returns the program's debug message destination as set by
I<prog_set_dbg(3)>. On error, returns C<null> with C<errno> set
appropriately.
=cut
*/
Msg *prog_dbg(void)
{
PROG_GET_PTR_AND_RETURN(g.dbg);
}
/*
=item C<Msg *prog_alert(void)>
Returns the program's alert message destination as set by
I<prog_set_alert(3)>. On error, returns C<null> with C<errno> set
appropriately.
=cut
*/
Msg *prog_alert(void)
{
PROG_GET_PTR_AND_RETURN(g.log);
}
/*
=item C<size_t prog_debug_level(void)>
Returns the program's debug level as set by I<prog_set_debug_level(3)>. On
error, returns C<0> with C<errno> set appropriately.
=cut
*/
size_t prog_debug_level(void)
{
PROG_GET_INT_AND_RETURN(g.debug_level);
}
/*
=item C<size_t prog_verbosity_level(void)>
Returns the program's verbosity level as set by
I<prog_set_verbosity_level(3)>. On error, returns C<0> with C<errno> set
appropriately.
=cut
*/
size_t prog_verbosity_level(void)
{
PROG_GET_INT_AND_RETURN(g.verbosity_level);
}
/*
=item C<int prog_out_fd(int fd)>
Sets the program's normal message destination to be the file descriptor,
C<fd>. On success, returns C<0>. On error, returns C<-1> with C<errno> set
appropriately.
=cut
*/
int prog_out_fd(int fd)
{
Msg *mesg;
if (!(mesg = msg_create_fd_with_locker(g.locker, fd)))
return -1;
if (!prog_set_out(mesg))
{
msg_release(mesg);
return -1;
}
return 0;
}
/*
=item C<int prog_out_stdout(void)>
Sets the program's normal message destination to be standard output. On
success, returns C<0>. On error, returns C<-1> with C<errno> set
appropriately.
=cut
*/
int prog_out_stdout(void)
{
return prog_out_fd(STDOUT_FILENO);
}
/*
=item C<int prog_out_file(const char *path)>
Sets the program's normal message destination to be the file specified by
C<path>. On success, returns C<0>. On error, returns C<-1> with C<errno> set
appropriately.
=cut
*/
int prog_out_file(const char *path)
{
Msg *mesg;