summaryrefslogtreecommitdiff
path: root/support/dktools/dkwt-gr.c
blob: 8ee012ccc0d4e7c8a95fd179d9228998d9f45f4c (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
/*
	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: dkwt-gr.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.
*/

/**	@file dkwt-gr.c The dkwt-gr module.
*/


#line 10 "dkwt-gr.ctr"

#include "dk3all.h"
#include "dkt.h"
#include "dkwt.h"





#line 18 "dkwt-gr.ctr"



/**	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;
}