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
|
%% options
copyright owner = Dirk Krause
copyright year = 2011-xxxx
license = bsd
%% module
#include "dk3all.h"
#include "dkt.h"
#include "dkwt.h"
$!trace-include
/** Job structure for dkwt groups.
*/
typedef struct {
dk3_app_t *app; /**< Application structure. */
dkChar const * const *msg; /**< Localized message texts. */
dkChar const * const *kwnl; /**< Keywords, not localized. */
dk3_option_set_t *opt; /**< Options set. */
int f_long; /**< Flag: Long user listing. */
int exval; /**< Exit status code. */
} DKWT_GRP_J;
/** Data for the option set.
*/
static dk3_option_t const dkwt_groups_options[] = {
{ dkT('l'), dkT("long"), 0 },
{ dkT('s'), dkT("short"), 0 }
};
/** Number of options in the dkt_sort_options array.
*/
static size_t const dkwt_groups_szoptions =
sizeof(dkwt_groups_options)/sizeof(dk3_option_t);
/** Initialize job structure.
@param j Job structure.
*/
static
void
dkwt_groups_job_init(DKWT_GRP_J *j)
{
j->app = NULL; j->msg = NULL; j->kwnl = NULL; j->opt = NULL;
j->f_long = 1; j->exval = DKT_RESULT_ERR_UNSPECIFIC;
}
/** Clean up job structure after use.
@param j Job structure.
*/
static
void
dkwt_groups_job_cleanup(DKWT_GRP_J *j)
{
if(j->opt) {
dk3opt_close(j->opt);
} j->opt = NULL;
}
/** Process command line arguments.
@param j Job structure.
@return 1 on success (can continue), 0 on error.
*/
static
int
dkwt_groups_process_argv(DKWT_GRP_J *j)
{
int back = 0;
int xargc;
dkChar const * const *xargv;
xargc = dk3app_get_argc(j->app);
xargv = dk3app_get_argv(j->app);
xargv++; xargv++; xargc--; xargc--;
j->opt = dk3opt_open_app(
dkwt_groups_options,
dkwt_groups_szoptions,
dkT('\0'),
NULL,
xargc,
xargv,
j->app
);
if(j->opt) {
if(0 == dk3opt_get_error_code(j->opt)) {
back = 1;
if(dk3opt_is_set(j->opt, dkT('l'))) {
j->f_long = 2;
if(dk3opt_is_set(j->opt, dkT('s'))) {
back = 0;
/* ERROR: -l and -s are exclusive */
dk3app_log_1(j->app, DK3_LL_ERROR, j->msg, 76);
}
} else {
if(dk3opt_is_set(j->opt, dkT('s'))) {
j->f_long = 0;
}
}
if(dk3opt_get_num_args(j->opt) > 0) {
/* Warning: Arguments ignored. */
dk3app_log_1(j->app, DK3_LL_ERROR, j->msg, 76);
}
}
}
return back;
}
/** Run the "list groups" operation.
@param j Job structure.
*/
static
void
dkwt_groups_run(DKWT_GRP_J *j)
{
dkwt_group_list_t *gl;
dkwt_group_t *gp;
int is_first = 1;
dkChar const *ptr;
gl = dkwt_tool_open_group_list(j->app, j->f_long);
if(gl) {
dkwt_tool_reset_group_list(gl);
dk3sf_initialize_stdout();
while((gp = (dkwt_group_t *)dk3sto_it_next(gl->i_grp)) != NULL) {
if(gp->groupname) {
if((j->f_long) && (!(is_first))) {
dk3sf_fputc(dkT('\n'), stdout);
} is_first = 0;
dk3sf_fputs(gp->groupname, stdout);
dk3sf_fputc(dkT('\n'), stdout);
if(j->f_long) {
ptr = gp->groupname;
while(*(ptr++)) { dk3sf_fputc(dkT('-'), stdout); }
dk3sf_fputc(dkT('\n'), stdout);
dk3sf_fputs((j->msg)[39], stdout);
dk3sf_fputs((j->msg)[(gp->gtype) ? 42 : 41], stdout);
dk3sf_fputc(dkT('\n'), stdout);
if(gp->comment) {
dk3sf_fputs((j->msg)[40], stdout);
dk3sf_fputs(gp->comment, stdout);
dk3sf_fputc(dkT('\n'), stdout);
}
if(gp->textsid) {
dk3sf_fputs((j->msg)[43], stdout);
dk3sf_fputs(gp->textsid, stdout);
dk3sf_fputc(dkT('\n'), stdout);
}
}
}
}
dkwt_tool_close_group_list(gl);
}
}
int
dkwt_groups(
dk3_app_t *app,
dkChar const * const *msg,
dkChar const * const *kwnl
)
{
int back = DKT_RESULT_ERR_UNSPECIFIC;
DKWT_GRP_J j;
dkwt_groups_job_init(&j);
j.app = app; j.msg = msg; j.kwnl = kwnl;
if(dkwt_groups_process_argv(&j)) {
back = j.exval = DKT_RESULT_OK;
dkwt_groups_run(&j);
}
dkwt_groups_job_cleanup(&j);
return back;
}
|