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

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