Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/vis12/src/lib/common/input.c
ViewVC logotype

Annotation of /branches/vis12/src/lib/common/input.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1725 - (view) (download) (as text)

1 : jhr 1232 /*! \file input.c
2 :     *
3 : jhr 1301 * \author John Reppy, Gordon Kindlmann
4 : jhr 1232 */
5 :    
6 :     /*
7 :     * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
8 :     * All rights reserved.
9 :     */
10 :     #include "Diderot/diderot.h"
11 :     #include <teem/hest.h>
12 :    
13 : jhr 1301 //! Option type tags
14 :     typedef enum {
15 :     optTypeUnknown, // 0
16 :     optTypeFlag, // 1
17 :     optTypeBool, // 2
18 :     optTypeInt, // 3
19 :     optTypeReal, // 4
20 :     optTypeReal2, // 5
21 :     optTypeReal3, // 6
22 :     optTypeReal4, // 7
23 :     optTypeString // 8
24 :     } opt_type_t;
25 :    
26 :     /*! The struct containing the definition of *one* option, and storage
27 :     * for its value once parsed
28 :     */
29 :     typedef struct {
30 :     char *name; //!< option name
31 :     char *desc; //!< option description (a phrase or sentence)
32 :     opt_type_t ty; //!< option type
33 :     union { //! storage that is passed into hest
34 :     int b; //!< storage for boolean-valued options
35 :     double r[4]; //!< storage for real-valued options
36 :     int64_t i[4]; //!< storage for integer-valued options
37 :     char *s; //!< storage for string-valued options
38 :     } hval;
39 :     void *valp; //!< pointer to Diderot global variable being set
40 :     } opt_def_t;
41 :    
42 : jhr 1671 /*! container for *all* options, the machinery for parsing them,
43 : jhr 1301 * and means of managing their allocation. There is an opaque typedef
44 :     * around this in src/include/Diderot/options.h.
45 :     *
46 :     * The "airArray *mop" is the primary means of managing the things that
47 :     * are dynamically allocated; anything that is allocated is registered
48 :     * in the mop so that it can be freed from a single place later (look
49 :     * for calls to airMopAdd)
50 :     *
51 :     * Note that "odef" is a dynamically re-allocated array (managed by
52 :     * "odefArr"), and it is an array of opt_def_t pointers, and not an
53 :     * array of opt_def_t's, which is necessary: the values that hest sets
54 :     * are set via a pointer, and those values are stored in the opt_def_t,
55 :     * so the memory location of the opt_def_t cannot change, so these are
56 :     * allocated individually once. If odef was an array of opt_def_t's,
57 :     * then the location of the individual op_def_t would change if odef
58 :     * was reallocated for a different length.
59 :     */
60 :     struct Diderot_struct_options {
61 : jhr 1671 char *progInfo; //!< documentation string for describing whole
62 :     //! program, intended to be one or more sentences
63 : jhr 1301 hestOpt *hopt; //!< dynamically managed hest options
64 :     hestParm *hparm; //!< hest parameters
65 :     opt_def_t **odef; //!< dynamically reallocated via odefArr
66 :     unsigned int odefNum; //!< length of meaningful values in odef[]
67 :     airArray *odefArr; //!< manages odef and odefNum;
68 :     airArray *mop; //!< manages hopt, hparm, and odefArr
69 :     };
70 :    
71 :     static void *OptDefNew ()
72 : jhr 1232 {
73 : jhr 1301 opt_def_t *odef = AIR_CALLOC(1, opt_def_t);
74 :     odef->name = odef->desc = 0;
75 :     odef->ty = optTypeUnknown;
76 :     odef->valp = 0;
77 :     return odef;
78 : jhr 1232 }
79 :    
80 : jhr 1301 /*! free an opt_def_t. This doesn't need to be called directly; it will
81 : jhr 1671 * be used by the airArray to manage "odef" and what it points to.
82 : jhr 1301 */
83 :     static void *optDefNix (void *_odef)
84 : jhr 1232 {
85 : jhr 1301 opt_def_t *odef = (opt_def_t *)_odef;
86 :     airFree(odef->name);
87 :     airFree(odef->desc);
88 :     if (optTypeString == odef->ty) {
89 :     airFree(odef->hval.s);
90 :     }
91 :     airFree(odef);
92 :     return 0;
93 :     }
94 : jhr 1232
95 : jhr 1671 /*! create the container for all option information and register standard
96 :     * command-line options.
97 : jhr 1301 */
98 :     Diderot_Options_t *Diderot_OptNew ()
99 :     {
100 :     static const char *me = "Diderot_Options_New";
101 : jhr 1671 Diderot_Options_t *opts = AIR_CALLOC(1, Diderot_Options_t);
102 :     if (opts == 0) {
103 : jhr 1301 fprintf(stderr, "%s: unable to allocate output\n", me);
104 :     exit(1);
105 :     }
106 : jhr 1671 opts->progInfo = 0;
107 :     opts->mop = airMopNew();
108 :     opts->hopt = 0;
109 :     opts->hparm = hestParmNew();
110 :     opts->hparm->noArgsIsNoProblem = AIR_TRUE;
111 :     airMopAdd (opts->mop, opts->hparm, (airMopper)hestParmFree, airMopAlways);
112 :     opts->odef = 0;
113 :     opts->odefNum = 0;
114 : jhr 1301 /*
115 :     ** the airArray odefArr manages the allocation of odef, and as a
116 :     ** convenience also sets odefNum to the number of values reliably
117 :     ** set in odef
118 :     */
119 : jhr 1671 opts->odefArr = airArrayNew (
120 :     (void**)(&opts->odef),
121 :     &opts->odefNum,
122 :     sizeof(opt_def_t *),
123 : jhr 1301 8 /* allocation increment */);
124 : jhr 1232
125 : jhr 1671 if (opts->odefArr == 0) {
126 : jhr 1301 fprintf(stderr, "%s: unable to allocate option array\n", me);
127 :     exit(1);
128 : jhr 1232 }
129 : jhr 1301 /*
130 :     ** the airArray will set odef[I] = OptDefNew for newly allocated
131 :     ** elements I of odef, and will call optDefNix(odef[I]) for
132 :     ** elements of odef that are about to be freed
133 :     */
134 : jhr 1671 airArrayPointerCB (opts->odefArr, OptDefNew, optDefNix);
135 :     airMopAdd (opts->mop, opts->odefArr, (airMopper)airArrayNuke, airMopAlways);
136 :    
137 :     return opts;
138 : jhr 1301 }
139 : jhr 1232
140 : jhr 1301 /*! free everything associated with a Diderot_Options_t
141 :     */
142 :     void Diderot_OptFree (Diderot_Options_t *dopts)
143 :     {
144 :     if (dopts != 0) {
145 :     airFree(dopts->progInfo);
146 :     /* frees everything connected to the mop, and the mop itself */
147 :     airMopOkay(dopts->mop);
148 :     free(dopts);
149 :     }
150 :     return;
151 : jhr 1232 }
152 :    
153 : jhr 1301 /*! sets the one piece of information in the Diderot_Options_t
154 :     * which is not really for the options; "progInfo" is to describe the
155 :     * whole program.
156 :     */
157 :     void Diderot_OptProgramInfoSet (
158 :     Diderot_Options_t *dopts,
159 :     const char *progInfo)
160 : jhr 1232 {
161 : jhr 1301 static const char *me = "Diderot_OptProgramInfoSet";
162 : jhr 1232
163 : jhr 1301 if (progInfo) {
164 :     if (!( dopts->progInfo = airStrdup(progInfo) )) {
165 :     fprintf(stderr, "%s: unable to copy program info", me);
166 :     exit(1);
167 :     }
168 :     }
169 :     }
170 : jhr 1232
171 : jhr 1301 /*! main function for adding options to a Diderot_Options_t.
172 :     */
173 :     static opt_def_t *OptAdd (
174 :     Diderot_Options_t *dopts,
175 :     const char *name,
176 :     const char *desc,
177 :     void *valp,
178 :     opt_type_t ty)
179 :     {
180 :     static const char *me = "OptAdd";
181 :     unsigned int nidx;
182 :    
183 :     if ((name == 0) || (desc == 0)) {
184 :     fprintf(stderr, "%s: need both name (%p) and desc (%p) non-NULL\n",
185 :     me, (void *)name, (void *)desc);
186 :     exit(1);
187 : jhr 1232 }
188 : jhr 1301 /* calling airArrayLenIncr may trigger re-allocation of odef;
189 :     * nidx is the index of the newly reserved element, odef[nidx]
190 :     */
191 :     nidx = airArrayLenIncr(dopts->odefArr, 1);
192 :     if (dopts->odef == 0) {
193 :     fprintf(stderr, "%s: unable to reallocate option array\n", me);
194 :     exit(1);
195 :     }
196 :     /* code here uses "odf" to copy one value of odef[] */
197 :     opt_def_t *odf = dopts->odef[nidx];
198 :     odf->name = airStrdup(name);
199 :     odf->desc = airStrdup(desc);
200 :     if ((odf->name == 0) || (odf->desc == 0)) {
201 :     fprintf(stderr, "%s: unable to allocate strings (%p, %p)\n",
202 :     me, (void *)odf->name,(void *) odf->desc);
203 :     exit(1);
204 :     }
205 :     odf->valp = valp;
206 :     odf->ty = ty;
207 :    
208 :     return odf;
209 :     }
210 : jhr 1232
211 : jhr 1301 void Diderot_OptAddFlag (
212 :     Diderot_Options_t *dopts,
213 :     const char *name, const char *desc,
214 :     bool *flg)
215 :     {
216 :     opt_def_t *odf = OptAdd(dopts, name, desc, (void*)flg, optTypeFlag);
217 :     hestOptAdd (
218 :     &dopts->hopt, name, NULL, airTypeInt, 0, 0, &odf->hval.b,
219 :     NULL, desc);
220 : jhr 1232 }
221 :    
222 : jhr 1301 /*! \brief add a boolean-valued option to the list of options.
223 :     * \param dopts the list of options
224 :     * \param name the name of the option
225 :     * \param desc a short description string
226 :     * \param v a pointer to the location where the value gets stored
227 :     * \param hasDflt true if there is an initial value
228 :     */
229 :     void Diderot_OptAddBool (
230 :     Diderot_Options_t *dopts,
231 :     const char *name, const char *desc,
232 :     bool *v, bool hasDflt)
233 : jhr 1232 {
234 : jhr 1301 opt_def_t *odf = OptAdd(dopts, name, desc, (void*)v, optTypeBool);
235 :     hestOptAdd (
236 :     &dopts->hopt, name, "bool", airTypeBool, 1, 1, &odf->hval.b,
237 :     hasDflt ? (*v ? "true" : "false") : NULL, desc);
238 :     }
239 : jhr 1232
240 : jhr 1301 /*
241 :     ** On the use of AIR_STRLEN_HUGE below: default values for
242 :     ** command-line options are communicated to hest by a *string*, but
243 :     ** the actual default value comes to us in Diderot global. So these
244 :     ** functions have to convert the value from the Diderot variable into
245 :     ** a string.
246 :     **
247 :     ** The problem is that we don't know how long string will have to be,
248 :     ** and there are few good functions for dealing with this. snprintf
249 :     ** is good for making sure you don't over-write a buffer of fixed
250 :     ** size, but you still have to allocate the buffer. asprintf allocates
251 :     ** as needed, but GLK didn't want to assume it was available (perhaps
252 :     ** this should be revisited)
253 :     **
254 :     ** So instead, we just use a huge buffer, and assume that it will be
255 :     ** more than big enough. Feel free to fix this.
256 :     */
257 : jhr 1232
258 : jhr 1301 /*! \brief add an integer option to the list of options.
259 :     * \param dopts the list of options
260 :     * \param name the name of the option
261 :     * \param desc a short description string
262 :     * \param v a pointer to the location where the value gets stored
263 :     * \param hasDflt true if there is an initial value
264 :     */
265 :     void Diderot_OptAddInt (
266 :     Diderot_Options_t *dopts,
267 :     const char *name, const char *desc, Diderot_int_t *v, bool hasDflt)
268 :     {
269 :     char buf[AIR_STRLEN_HUGE] = "";
270 :     if (hasDflt) {
271 : jhr 1671 snprintf(buf, sizeof(buf), "%ld", (long)*v);
272 : jhr 1232 }
273 : jhr 1301 opt_def_t *odf = OptAdd(dopts, name, desc, (void*)v, optTypeInt);
274 :     hestOptAdd (
275 :     &dopts->hopt, name, "int", airTypeLongInt, 1, 1, odf->hval.i,
276 :     hasDflt ? buf : NULL, desc);
277 :     }
278 : jhr 1232
279 : jhr 1301 /*! \brief add a real-valued option to the list of options.
280 :     * \param dopts the list of options
281 :     * \param name the name of the option
282 :     * \param desc a short description string
283 :     * \param v a pointer to the location where the value gets stored
284 :     * \param hasDflt true if there is an initial value
285 :     */
286 :     void Diderot_OptAddReal (
287 :     Diderot_Options_t *dopts,
288 :     const char *name, const char *desc, Diderot_real_t *v, bool hasDflt)
289 :     {
290 :     char buf[AIR_STRLEN_HUGE] = "";
291 :     if (hasDflt) {
292 : jhr 1671 snprintf(buf, sizeof(buf), "%lf", (double)*v);
293 : jhr 1301 }
294 :     opt_def_t *odf = OptAdd(dopts, name, desc, (void*)v, optTypeReal);
295 :     hestOptAdd (
296 :     &dopts->hopt, name, "val", airTypeDouble, 1, 1, odf->hval.r,
297 :     hasDflt ? buf : NULL, desc);
298 : jhr 1232 }
299 : jhr 1301
300 :     /*! \brief add a 2-element vector option to the list of options.
301 :     * \param dopts the list of options
302 :     * \param name the name of the option
303 :     * \param desc a short description string
304 :     * \param v a pointer to the location where the value gets stored
305 :     * \param hasDflt true if there is an initial value
306 :     */
307 :     void Diderot_OptAddReal2 (
308 :     Diderot_Options_t *dopts,
309 :     const char *name, const char *desc, Diderot_vec2_t *v, bool hasDflt)
310 :     {
311 :     char buf[AIR_STRLEN_HUGE] = "";
312 :    
313 :     if (hasDflt) {
314 :     Diderot_union2_t u;
315 :     u.v = *v;
316 : jhr 1671 snprintf(buf, sizeof(buf), "%lf %lf", (double)(u.r[0]), (double)(u.r[1]));
317 : jhr 1301 }
318 :     opt_def_t *odf = OptAdd(dopts, name, desc, (void*)v, optTypeReal2);
319 :     hestOptAdd (
320 :     &dopts->hopt, name, "x y", airTypeDouble, 2, 2, odf->hval.r,
321 :     hasDflt ? buf : NULL, desc);
322 :     }
323 :    
324 :     /*! \brief add a 3-element vector option to the list of options.
325 :     * \param dopts the list of options
326 :     * \param name the name of the option
327 :     * \param desc a short description string
328 :     * \param v a pointer to the location where the value gets stored
329 :     * \param hasDflt true if there is an initial value
330 :     */
331 :     void Diderot_OptAddReal3 (
332 :     Diderot_Options_t *dopts,
333 :     const char *name, const char *desc, Diderot_vec3_t *v, bool hasDflt)
334 :     {
335 :     char buf[AIR_STRLEN_HUGE] = "";
336 :    
337 :     if (hasDflt) {
338 :     Diderot_union3_t u;
339 :     u.v = *v;
340 : jhr 1671 snprintf(buf, sizeof(buf), "%lf %lf %lf",
341 :     (double)(u.r[0]), (double)(u.r[1]), (double)(u.r[2]));
342 : jhr 1301 }
343 :     opt_def_t *odf = OptAdd(dopts, name, desc, (void*)v, optTypeReal3);
344 :     hestOptAdd (
345 :     &dopts->hopt, name, "x y z", airTypeDouble, 3, 3, odf->hval.r,
346 :     hasDflt ? buf : NULL, desc);
347 :     }
348 :    
349 :     /*! \brief add a 4-element vector option to the list of options.
350 :     * \param dopts the list of options
351 :     * \param name the name of the option
352 :     * \param desc a short description string
353 :     * \param v a pointer to the location where the value gets stored
354 :     * \param hasDflt true if there is an initial value
355 :     */
356 :     void Diderot_OptAddReal4 (
357 :     Diderot_Options_t *dopts,
358 :     const char *name, const char *desc,
359 :     Diderot_vec4_t *v, bool hasDflt)
360 :     {
361 :     char buf[AIR_STRLEN_HUGE] = "";
362 :    
363 :     if (hasDflt) {
364 :     Diderot_union4_t u;
365 :     u.v = *v;
366 : jhr 1671 snprintf(buf, sizeof(buf), "%lf %lf %lf %lf",
367 : jhr 1301 (double)(u.r[0]), (double)(u.r[1]),
368 :     (double)(u.r[2]), (double)(u.r[3]));
369 :     }
370 :     opt_def_t *odf = OptAdd(dopts, name, desc, (void*)v, optTypeReal4);
371 :     hestOptAdd (
372 :     &dopts->hopt, name, "x y z w", airTypeDouble, 4, 4, odf->hval.r,
373 :     hasDflt ? buf : NULL, desc);
374 :     }
375 :    
376 :     /*! \brief add a string-vauled option to the list of options.
377 :     * \param dopts the list of options
378 :     * \param name the name of the option
379 :     * \param desc a short description string
380 :     * \param v a pointer to the location where the value gets stored
381 :     * \param hasDflt true if there is an initial value
382 :     */
383 :     void Diderot_OptAddString (
384 :     Diderot_Options_t *dopts,
385 :     const char *name, const char *desc,
386 :     char **v, bool hasDflt)
387 :     {
388 :     opt_def_t *odf = OptAdd(dopts, name, desc, (void*)v, optTypeString);
389 :     hestOptAdd (
390 :     &dopts->hopt, name, "str", airTypeString, 1, 1, &odf->hval.s,
391 :     hasDflt ? *v : NULL, desc);
392 :     }
393 :    
394 :     /*! given argc/argv, learn values for all options and set them
395 :     */
396 :     void Diderot_OptProcess (Diderot_Options_t *dopts, int argc, const char **argv)
397 :     {
398 :     static const char *me = "Diderot_ProcessOptions";
399 :    
400 :     hestParseOrDie (
401 :     dopts->hopt, argc-1, argv+1, dopts->hparm, argv[0],
402 :     dopts->progInfo ? dopts->progInfo : "(no program info set)",
403 :     AIR_TRUE, AIR_TRUE, AIR_TRUE);
404 :     airMopAdd(dopts->mop, dopts->hopt, (airMopper)hestOptFree, airMopAlways);
405 :     airMopAdd(dopts->mop, dopts->hopt, (airMopper)hestParseFree, airMopAlways);
406 :    
407 :     // convert values learned by hest to their Diderot representations
408 :     for (int i = 0; i < dopts->odefNum; i++) {
409 :     opt_def_t *odf = dopts->odef[i];
410 :     switch (odf->ty) {
411 :     case optTypeFlag:
412 :     case optTypeBool:
413 :     *(bool*)(odf->valp) = odf->hval.b ? true : false;
414 :     break;
415 :     case optTypeInt:
416 :     *(Diderot_int_t *)(odf->valp) = (Diderot_int_t)(odf->hval.i[0]);
417 :     break;
418 :     case optTypeReal:
419 :     *(Diderot_real_t *)(odf->valp) = (Diderot_real_t)(odf->hval.r[0]);
420 :     break;
421 :     case optTypeReal2:
422 :     *(Diderot_vec2_t *)(odf->valp) = vec2((Diderot_real_t)(odf->hval.r[0]),
423 :     (Diderot_real_t)(odf->hval.r[1]));
424 :     break;
425 :     case optTypeReal3:
426 :     *(Diderot_vec3_t *)(odf->valp) = vec3((Diderot_real_t)(odf->hval.r[0]),
427 :     (Diderot_real_t)(odf->hval.r[1]),
428 :     (Diderot_real_t)(odf->hval.r[2]));
429 :     break;
430 :     case optTypeReal4:
431 :     *(Diderot_vec4_t *)(odf->valp) = vec4((Diderot_real_t)(odf->hval.r[0]),
432 :     (Diderot_real_t)(odf->hval.r[1]),
433 :     (Diderot_real_t)(odf->hval.r[2]),
434 :     (Diderot_real_t)(odf->hval.r[3]));
435 :     break;
436 :     case optTypeString: {
437 :     char *s = airStrdup(odf->hval.s);
438 :     if (s == 0) {
439 :     fprintf(stderr, "%s: unable to allocate string copy\n", me);
440 :     exit(1);
441 :     }
442 :     *(char **)(odf->valp) = s;
443 :     } break;
444 :     default:
445 :     fprintf(stderr, "%s: unknown option type %d\n", me, odf->ty);
446 :     exit(1);
447 :     } /* switch */
448 :     }
449 :     }

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0