94a59ff7a9c76e48cc5e52b75e84582fd45ec52f
[alexxy/gromacs.git] / src / gmxlib / selection / params.c
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11  * Copyright (c) 2001-2009, The GROMACS development team,
12  * check out http://www.gromacs.org for more information.
13
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * If you want to redistribute modifications, please consider that
20  * scientific software is very special. Version control is crucial -
21  * bugs must be traceable. We will be happy to consider code for
22  * inclusion in the official distribution, but derived work must not
23  * be called official GROMACS. Details are found in the README & COPYING
24  * files - if they are missing, get the official version at www.gromacs.org.
25  *
26  * To help us fund GROMACS development, we humbly ask that you cite
27  * the papers on the package - you can find them in the top README file.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \internal \file
32  * \brief
33  * Implementation of functions in selparam.h.
34  */
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include <smalloc.h>
40 #include <string2.h>
41 #include <vec.h>
42
43 #include <position.h>
44 #include <selmethod.h>
45 #include <selparam.h>
46
47 #include "parsetree.h"
48 #include "position.h"
49 #include "selelem.h"
50
51 /*!
52  * \param[in] name   Name of the parameter to search.
53  * \param[in] nparam Number of parameters in the \p param array.
54  * \param[in] param  Parameter array to search.
55  * \returns   Pointer to the parameter in the \p param
56  *   or NULL if no parameter with name \p name was found.
57  *
58  * The comparison is case-sensitive.
59  */
60 gmx_ana_selparam_t *
61 gmx_ana_selparam_find(const char *name, int nparam, gmx_ana_selparam_t *param)
62 {
63     int                i;
64
65     if (nparam == 0)
66     {
67         return NULL;
68     }
69     /* Find the first non-null parameter */
70     i = 0;
71     while (i < nparam && param[i].name == NULL)
72     {
73         ++i;
74     }
75     /* Process the special case of a NULL parameter */
76     if (name == NULL)
77     {
78         return (i == 0) ? NULL : &param[i-1];
79     }
80     for ( ; i < nparam; ++i)
81     {
82         if (!strcmp(param[i].name, name))
83         {
84             return &param[i];
85         }
86         /* Check for 'no' prefix on gmx_boolean parameters */
87         if (param[i].val.type == NO_VALUE
88             && strlen(name) > 2 && name[0] == 'n' && name[1] == 'o'
89             && !strcmp(param[i].name, name+2))
90         {
91             return &param[i];
92         }
93     }
94     return NULL;
95 }
96
97 /*! \brief
98  * Does a type conversion on a \c t_selexpr_value.
99  *
100  * \param[in,out] value    Value to convert.
101  * \param[in]     type     Type to convert to.
102  * \param[in]     scanner  Scanner data structure.
103  * \returns       0 on success, a non-zero value on error.
104  */
105 static int
106 convert_value(t_selexpr_value *value, e_selvalue_t type, void *scanner)
107 {
108     if (value->type == type || type == NO_VALUE)
109     {
110         return 0;
111     }
112     if (value->bExpr)
113     {
114         /* Conversion from atom selection to position using default
115          * reference positions. */
116         if (value->type == GROUP_VALUE && type == POS_VALUE)
117         {
118             value->u.expr =
119                 _gmx_sel_init_position(value->u.expr, NULL, scanner);
120             if (value->u.expr == NULL)
121             {
122                 return -1;
123             }
124             value->type = type;
125             return 0;
126         }
127         return -1;
128     }
129     else
130     {
131         /* Integers to floating point are easy */
132         if (value->type == INT_VALUE && type == REAL_VALUE)
133         {
134             real r1 = (real)value->u.i.i1;
135             real r2 = (real)value->u.i.i2;
136             value->u.r.r1 = r1;
137             value->u.r.r2 = r2;
138             value->type = type;
139             return 0;
140         }
141         /* Reals that are integer-valued can also be converted */
142         if (value->type == REAL_VALUE && type == INT_VALUE
143             && gmx_within_tol(value->u.r.r1, (int)value->u.r.r1, GMX_REAL_EPS)
144             && gmx_within_tol(value->u.r.r2, (int)value->u.r.r2, GMX_REAL_EPS))
145         {
146             int i1 = (int)value->u.r.r1;
147             int i2 = (int)value->u.r.r2;
148             value->u.i.i1 = i1;
149             value->u.i.i2 = i2;
150             value->type = type;
151             return 0;
152         }
153     }
154     return -1;
155 }
156
157 /*! \brief
158  * Does a type conversion on a list of values.
159  *
160  * \param[in,out] values   Values to convert.
161  * \param[in]     type     Type to convert to.
162  * \param[in]     scanner  Scanner data structure.
163  * \returns       0 on success, a non-zero value on error.
164  */
165 static int
166 convert_values(t_selexpr_value *values, e_selvalue_t type, void *scanner)
167 {
168     t_selexpr_value *value;
169     int              rc, rc1;
170
171     rc = 0;
172     value = values;
173     while (value)
174     {
175         rc1 = convert_value(value, type, scanner);
176         if (rc1 != 0 && rc == 0)
177         {
178             rc = rc1;
179         }
180         value = value->next;
181     }
182     /* FIXME: More informative error messages */
183     return rc;
184 }
185
186 /*! \brief
187  * Adds a child element for a parameter, keeping the parameter order.
188  *
189  * \param[in,out] root  Root element to which the child is added.
190  * \param[in]     child Child to add.
191  * \param[in]     param Parameter for which this child is a value.
192  *
193  * Puts \p child in the child list of \p root such that the list remains
194  * in the same order as the corresponding parameters.
195  */
196 static void
197 place_child(t_selelem *root, t_selelem *child, gmx_ana_selparam_t *param)
198 {
199     gmx_ana_selparam_t *ps;
200     int                 n;
201
202     ps = root->u.expr.method->param;
203     n  = param - ps;
204     /* Put the child element in the correct place */
205     if (!root->child || n < root->child->u.param - ps)
206     {
207         child->next = root->child;
208         root->child = child;
209     }
210     else
211     {
212         t_selelem *prev;
213
214         prev = root->child;
215         while (prev->next && prev->next->u.param - ps >= n)
216         {
217             prev = prev->next;
218         }
219         child->next = prev->next;
220         prev->next  = child;
221     }
222 }
223
224 /*! \brief
225  * Comparison function for sorting integer ranges.
226  * 
227  * \param[in] a Pointer to the first range.
228  * \param[in] b Pointer to the second range.
229  * \returns   -1, 0, or 1 depending on the relative order of \p a and \p b.
230  *
231  * The ranges are primarily sorted based on their starting point, and
232  * secondarily based on length (longer ranges come first).
233  */
234 static int
235 cmp_int_range(const void *a, const void *b)
236 {
237     if (((int *)a)[0] < ((int *)b)[0])
238     {
239         return -1;
240     }
241     if (((int *)a)[0] > ((int *)b)[0])
242     {
243         return 1;
244     }
245     if (((int *)a)[1] > ((int *)b)[1])
246     {
247         return -1;
248     }
249     return 0;
250 }
251
252 /*! \brief
253  * Comparison function for sorting real ranges.
254  *
255  * \param[in] a Pointer to the first range.
256  * \param[in] b Pointer to the second range.
257  * \returns   -1, 0, or 1 depending on the relative order of \p a and \p b.
258  *
259  * The ranges are primarily sorted based on their starting point, and
260  * secondarily based on length (longer ranges come first).
261  */
262 static int
263 cmp_real_range(const void *a, const void *b)
264 {
265     if (((real *)a)[0] < ((real *)b)[0])
266     {
267         return -1;
268     }
269     if (((real *)a)[0] > ((real *)b)[0])
270     {
271         return 1;
272     }
273     if (((real *)a)[1] > ((real *)b)[1])
274     {
275         return -1;
276     }
277     return 0;
278 }
279
280 /*! \brief
281  * Parses the values for a parameter that takes integer or real ranges.
282  * 
283  * \param[in] nval   Number of values in \p values.
284  * \param[in] values Pointer to the list of values.
285  * \param     param  Parameter to parse.
286  * \returns   TRUE if the values were parsed successfully, FALSE otherwise.
287  */
288 static gmx_bool
289 parse_values_range(int nval, t_selexpr_value *values, gmx_ana_selparam_t *param)
290 {
291     t_selexpr_value    *value;
292     int                *idata;
293     real               *rdata;
294     int                 i, j, n;
295
296     param->flags &= ~SPAR_DYNAMIC;
297     if (param->val.type != INT_VALUE && param->val.type != REAL_VALUE)
298     {
299         gmx_bug("internal error");
300         return FALSE;
301     }
302     idata = NULL;
303     rdata = NULL;
304     if (param->val.type == INT_VALUE)
305     {
306         snew(idata, nval*2);
307     }
308     else
309     {
310         snew(rdata, nval*2);
311     }
312     value = values;
313     i = 0;
314     while (value)
315     {
316         if (value->bExpr)
317         {
318             _gmx_selparser_error("expressions not supported within range parameters");
319             return FALSE;
320         }
321         if (value->type != param->val.type)
322         {
323             gmx_bug("internal error");
324             return FALSE;
325         }
326         if (param->val.type == INT_VALUE)
327         {
328             /* Make sure the input range is in increasing order */
329             if (value->u.i.i1 > value->u.i.i2)
330             {
331                 int tmp       = value->u.i.i1;
332                 value->u.i.i1 = value->u.i.i2;
333                 value->u.i.i2 = tmp;
334             }
335             /* Check if the new range overlaps or extends the previous one */
336             if (i > 0 && value->u.i.i1 <= idata[i-1]+1 && value->u.i.i2 >= idata[i-2]-1)
337             {
338                 idata[i-2] = min(idata[i-2], value->u.i.i1);
339                 idata[i-1] = max(idata[i-1], value->u.i.i2);
340             }
341             else
342             {
343                 idata[i++] = value->u.i.i1;
344                 idata[i++] = value->u.i.i2;
345             }
346         }
347         else
348         {
349             /* Make sure the input range is in increasing order */
350             if (value->u.r.r1 > value->u.r.r2)
351             {
352                 real tmp      = value->u.r.r1;
353                 value->u.r.r1 = value->u.r.r2;
354                 value->u.r.r2 = tmp;
355             }
356             /* Check if the new range overlaps or extends the previous one */
357             if (i > 0 && value->u.r.r1 <= rdata[i-1] && value->u.r.r2 >= rdata[i-2])
358             {
359                 rdata[i-2] = min(rdata[i-2], value->u.r.r1);
360                 rdata[i-1] = max(rdata[i-1], value->u.r.r2);
361             }
362             else
363             {
364                 rdata[i++] = value->u.r.r1;
365                 rdata[i++] = value->u.r.r2;
366             }
367         }
368         value = value->next;
369     }
370     n = i/2;
371     /* Sort the ranges and merge consequent ones */
372     if (param->val.type == INT_VALUE)
373     {
374         qsort(idata, n, 2*sizeof(int), &cmp_int_range);
375         for (i = j = 2; i < 2*n; i += 2)
376         {
377             if (idata[j-1]+1 >= idata[i])
378             {
379                 if (idata[i+1] > idata[j-1])
380                 {
381                     idata[j-1] = idata[i+1];
382                 }
383             }
384             else
385             {
386                 idata[j]   = idata[i];
387                 idata[j+1] = idata[i+1];
388                 j += 2;
389             }
390         }
391     }
392     else
393     {
394         qsort(rdata, n, 2*sizeof(real), &cmp_real_range);
395         for (i = j = 2; i < 2*n; i += 2)
396         {
397             if (rdata[j-1]+1 >= rdata[i])
398             {
399                 if (rdata[i+1] > rdata[j-1])
400                 {
401                     rdata[j-1] = rdata[i+1];
402                 }
403             }
404             else
405             {
406                 rdata[j]   = rdata[i];
407                 rdata[j+1] = rdata[i+1];
408                 j += 2;
409             }
410         }
411     }
412     n = j/2;
413     /* Store the values */
414     if (param->flags & SPAR_VARNUM)
415     {
416         param->val.nr  = n;
417         if (param->val.type == INT_VALUE)
418         {
419             srenew(idata, j);
420             _gmx_selvalue_setstore_alloc(&param->val, idata, j);
421         }
422         else
423         {
424             srenew(rdata, j);
425             _gmx_selvalue_setstore_alloc(&param->val, rdata, j);
426         }
427     }
428     else
429     {
430         if (n != param->val.nr)
431         {
432             _gmx_selparser_error("the value of parameter '%s' should consist of exactly one range",
433                                  param->name);       
434             sfree(idata);
435             sfree(rdata);
436             return FALSE;
437         }
438         if (param->val.type == INT_VALUE)
439         {
440             memcpy(param->val.u.i, idata, 2*n*sizeof(int));
441             sfree(idata);
442         }
443         else
444         {
445             memcpy(param->val.u.r, rdata, 2*n*sizeof(real));
446             sfree(rdata);
447         }
448     }
449     if (param->nvalptr)
450     {
451         *param->nvalptr = param->val.nr;
452     }
453     param->nvalptr = NULL;
454
455     return TRUE;
456 }
457
458 /*! \brief
459  * Parses the values for a parameter that takes a variable number of values.
460  * 
461  * \param[in] nval   Number of values in \p values.
462  * \param[in] values Pointer to the list of values.
463  * \param     param  Parameter to parse.
464  * \param     root   Selection element to which child expressions are added.
465  * \returns   TRUE if the values were parsed successfully, FALSE otherwise.
466  *
467  * For integer ranges, the sequence of numbers from the first to second value
468  * is stored, each as a separate value.
469  */
470 static gmx_bool
471 parse_values_varnum(int nval, t_selexpr_value *values,
472                     gmx_ana_selparam_t *param, t_selelem *root)
473 {
474     t_selexpr_value    *value;
475     int                 i, j;
476
477     param->flags &= ~SPAR_DYNAMIC;
478     /* Update nval if there are integer ranges. */
479     if (param->val.type == INT_VALUE)
480     {
481         value = values;
482         while (value)
483         {
484             if (value->type == INT_VALUE && !value->bExpr)
485             {
486                 nval += abs(value->u.i.i2 - value->u.i.i1);
487             }
488             value = value->next;
489         }
490     }
491
492     /* Check that the value type is actually implemented */
493     if (param->val.type != INT_VALUE && param->val.type != REAL_VALUE
494         && param->val.type != STR_VALUE && param->val.type != POS_VALUE)
495     {
496         gmx_bug("internal error");
497         return FALSE;
498     }
499
500     /* Reserve appropriate amount of memory */
501     if (param->val.type == POS_VALUE)
502     {
503         gmx_ana_pos_reserve(param->val.u.p, nval, 0);
504         gmx_ana_pos_set_nr(param->val.u.p, nval);
505         gmx_ana_indexmap_init(&param->val.u.p->m, NULL, NULL, INDEX_UNKNOWN);
506     }
507     else
508     {
509         _gmx_selvalue_reserve(&param->val, nval);
510     }
511
512     value = values;
513     i     = 0;
514     while (value)
515     {
516         if (value->bExpr)
517         {
518             _gmx_selparser_error("expressions not supported within value lists");
519             return FALSE;
520         }
521         if (value->type != param->val.type)
522         {
523             gmx_bug("internal error");
524             return FALSE;
525         }
526         switch (param->val.type)
527         {
528             case INT_VALUE:
529                 if (value->u.i.i1 <= value->u.i.i2)
530                 {
531                     for (j = value->u.i.i1; j <= value->u.i.i2; ++j)
532                     {
533                         param->val.u.i[i++] = j;
534                     }
535                 }
536                 else
537                 {
538                     for (j = value->u.i.i1; j >= value->u.i.i2; --j)
539                     {
540                         param->val.u.i[i++] = j;
541                     }
542                 }
543                 break;
544             case REAL_VALUE:
545                 if (value->u.r.r1 != value->u.r.r2)
546                 {
547                     _gmx_selparser_error("real ranges not supported for parameter '%s'", param->name);
548                     return FALSE;
549                 }
550                 param->val.u.r[i++] = value->u.r.r1;
551                 break;
552             case STR_VALUE:  param->val.u.s[i++] = strdup(value->u.s); break;
553             case POS_VALUE:  copy_rvec(value->u.x, param->val.u.p->x[i++]); break;
554             default: /* Should not be reached */
555                 gmx_bug("internal error");
556                 return FALSE;
557         }
558         value = value->next;
559     }
560     param->val.nr = i;
561     if (param->nvalptr)
562     {
563         *param->nvalptr = param->val.nr;
564     }
565     param->nvalptr = NULL;
566     /* Create a dummy child element to store the string values.
567      * This element is responsible for freeing the values, but carries no
568      * other function. */
569     if (param->val.type == STR_VALUE)
570     {
571         t_selelem *child;
572
573         child = _gmx_selelem_create(SEL_CONST);
574         _gmx_selelem_set_vtype(child, STR_VALUE);
575         child->name = param->name;
576         child->flags &= ~SEL_ALLOCVAL;
577         child->flags |= SEL_FLAGSSET | SEL_VARNUMVAL | SEL_ALLOCDATA;
578         child->v.nr = param->val.nr;
579         _gmx_selvalue_setstore(&child->v, param->val.u.s);
580         /* Because the child is not group-valued, the u union is not used
581          * for anything, so we can abuse it by storing the parameter value
582          * as place_child() expects, but this is really ugly... */
583         child->u.param = param;
584         place_child(root, child, param);
585     }
586
587     return TRUE;
588 }
589
590 /*! \brief
591  * Adds a new subexpression reference to a selection element.
592  *
593  * \param[in,out] root  Root element to which the subexpression is added.
594  * \param[in]     param Parameter for which this expression is a value.
595  * \param[in]     expr  Expression to add.
596  * \returns       The created child element.
597  *
598  * Creates a new \ref SEL_SUBEXPRREF element and adds it into the child
599  * list of \p root.
600  * If \p expr is already a \ref SEL_SUBEXPRREF, it is used as it is.
601  * \ref SEL_ALLOCVAL is cleared for the returned element.
602  */
603 static t_selelem *
604 add_child(t_selelem *root, gmx_ana_selparam_t *param, t_selelem *expr)
605 {
606     t_selelem          *child;
607     int                 rc;
608
609     if (root->type != SEL_EXPRESSION && root->type != SEL_MODIFIER)
610     {
611         gmx_bug("unsupported root element for selection parameter parser");
612         return NULL;
613     }
614     /* Create a subexpression reference element if necessary */
615     if (expr->type == SEL_SUBEXPRREF)
616     {
617         child = expr;
618     }
619     else
620     {
621         child = _gmx_selelem_create(SEL_SUBEXPRREF);
622         if (!child)
623         {
624             return NULL;
625         }
626         _gmx_selelem_set_vtype(child, expr->v.type);
627         child->child  = expr;
628     }
629     /* Setup the child element */
630     child->flags &= ~SEL_ALLOCVAL;
631     child->u.param = param;
632     if (child->v.type != param->val.type)
633     {
634         _gmx_selparser_error("invalid expression value for parameter '%s'",
635                              param->name);
636         goto on_error;
637     }
638     rc = _gmx_selelem_update_flags(child);
639     if (rc != 0)
640     {
641         goto on_error;
642     }
643     if ((child->flags & SEL_DYNAMIC) && !(param->flags & SPAR_DYNAMIC))
644     {
645         _gmx_selparser_error("parameter '%s' does not support dynamic values",
646                              param->name);
647         goto on_error;
648     }
649     if (!(child->flags & SEL_DYNAMIC))
650     {
651         param->flags &= ~SPAR_DYNAMIC;
652     }
653     /* Put the child element in the correct place */
654     place_child(root, child, param);
655     return child;
656
657 on_error:
658     if (child != expr)
659     {
660         _gmx_selelem_free(child);
661     }
662     return NULL;
663 }
664
665 /*! \brief
666  * Parses an expression value for a parameter that takes a variable number of values.
667  * 
668  * \param[in] nval   Number of values in \p values.
669  * \param[in] values Pointer to the list of values.
670  * \param     param  Parameter to parse.
671  * \param     root   Selection element to which child expressions are added.
672  * \returns   TRUE if the values were parsed successfully, FALSE otherwise.
673  */
674 static gmx_bool
675 parse_values_varnum_expr(int nval, t_selexpr_value *values,
676                          gmx_ana_selparam_t *param, t_selelem *root)
677 {
678     t_selexpr_value    *value;
679     t_selelem          *child;
680     t_selelem          *expr;
681
682     if (nval != 1 || !values->bExpr)
683     {
684         gmx_bug("internal error");
685         return FALSE;
686     }
687
688     value = values;
689     child = add_child(root, param, value->u.expr);
690     value->u.expr = NULL;
691     if (!child)
692     {
693         return FALSE;
694     }
695
696     /* Process single-valued expressions */
697     /* TODO: We should also handle SEL_SINGLEVAL expressions here */
698     if (child->v.type == POS_VALUE || child->v.type == GROUP_VALUE)
699     {
700         /* Set the value storage */
701         _gmx_selvalue_setstore(&child->v, param->val.u.ptr);
702         param->val.nr = 1;
703         if (param->nvalptr)
704         {
705             *param->nvalptr = param->val.nr;
706         }
707         param->nvalptr = NULL;
708         return TRUE;
709     }
710
711     if (!(child->flags & SEL_VARNUMVAL))
712     {
713         _gmx_selparser_error("invalid expression value for parameter '%s'",
714                              param->name);
715         return FALSE;
716     }
717
718     child->flags   |= SEL_ALLOCVAL;
719     param->val.nr   = -1;
720     *param->nvalptr = param->val.nr;
721     /* Rest of the initialization is done during compilation in
722      * init_method(). */
723
724     return TRUE;
725 }
726
727 /*! \brief
728  * Initializes the storage of an expression value.
729  *
730  * \param[in,out] sel   Selection element that evaluates the value.
731  * \param[in]     param Parameter to receive the value.
732  * \param[in]     i     The value of \p sel evaluates the value \p i for
733  *   \p param.
734  *
735  * Initializes the data pointer of \p sel such that the result is stored
736  * as the value \p i of \p param.
737  * This function is used internally by parse_values_std().
738  */
739 static gmx_bool
740 set_expr_value_store(t_selelem *sel, gmx_ana_selparam_t *param, int i)
741 {
742     if (sel->v.type != GROUP_VALUE && !(sel->flags & SEL_SINGLEVAL))
743     {
744         _gmx_selparser_error("invalid expression value for parameter '%s'",
745                              param->name);
746         return FALSE;
747     }
748     switch (sel->v.type)
749     {
750         case INT_VALUE:   sel->v.u.i = &param->val.u.i[i]; break;
751         case REAL_VALUE:  sel->v.u.r = &param->val.u.r[i]; break;
752         case STR_VALUE:   sel->v.u.s = &param->val.u.s[i]; break;
753         case POS_VALUE:   sel->v.u.p = &param->val.u.p[i]; break;
754         case GROUP_VALUE: sel->v.u.g = &param->val.u.g[i]; break;
755         default: /* Error */
756             gmx_bug("internal error");
757             return FALSE;
758     }
759     sel->v.nr = 1;
760     sel->v.nalloc = -1;
761     return TRUE;
762 }
763
764 /*! \brief
765  * Parses the values for a parameter that takes a constant number of values.
766  * 
767  * \param[in] nval   Number of values in \p values.
768  * \param[in] values Pointer to the list of values.
769  * \param     param  Parameter to parse.
770  * \param     root   Selection element to which child expressions are added.
771  * \returns   TRUE if the values were parsed successfully, FALSE otherwise.
772  *
773  * For integer ranges, the sequence of numbers from the first to second value
774  * is stored, each as a separate value.
775  */
776 static gmx_bool
777 parse_values_std(int nval, t_selexpr_value *values, gmx_ana_selparam_t *param,
778                  t_selelem *root)
779 {
780     t_selexpr_value   *value;
781     t_selelem         *child;
782     int                i, j;
783     gmx_bool               bDynamic;
784
785     /* Handle atom-valued parameters */
786     if (param->flags & SPAR_ATOMVAL)
787     {
788         if (nval > 1)
789         {
790             _gmx_selparser_error("extra values for parameter '%s'", param->name);
791             return FALSE;
792         }
793         value = values;
794         if (value->bExpr)
795         {
796             child = add_child(root, param, value->u.expr);
797             value->u.expr = NULL;
798             if (!child)
799             {
800                 return FALSE;
801             }
802             child->flags |= SEL_ALLOCVAL;
803             if (child->v.type != GROUP_VALUE && (child->flags & SEL_ATOMVAL))
804             {
805                 /* Rest of the initialization is done during compilation in
806                  * init_method(). */
807                 /* TODO: Positions are not correctly handled */
808                 param->val.nr = -1;
809                 if (param->nvalptr)
810                 {
811                     *param->nvalptr = -1;
812                 }
813                 return TRUE;
814             }
815             param->flags  &= ~SPAR_ATOMVAL;
816             param->val.nr  = 1;
817             if (param->nvalptr)
818             {
819                 *param->nvalptr = 1;
820             }
821             param->nvalptr = NULL;
822             if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE
823                 || param->val.type == STR_VALUE)
824             {
825                 _gmx_selvalue_reserve(&param->val, 1);
826             }
827             return set_expr_value_store(child, param, 0);
828         }
829         /* If we reach here, proceed with normal parameter handling */
830         param->val.nr = 1;
831         if (param->val.type == INT_VALUE || param->val.type == REAL_VALUE
832             || param->val.type == STR_VALUE)
833         {
834             _gmx_selvalue_reserve(&param->val, 1);
835         }
836         param->flags &= ~SPAR_ATOMVAL;
837         param->flags &= ~SPAR_DYNAMIC;
838     }
839
840     value = values;
841     i = 0;
842     bDynamic = FALSE;
843     while (value && i < param->val.nr)
844     {
845         if (value->type != param->val.type)
846         {
847             _gmx_selparser_error("incorrect value for parameter '%s' skipped", param->name);
848             value = value->next;
849             continue;
850         }
851         if (value->bExpr)
852         {
853             child = add_child(root, param, value->u.expr);
854             /* Clear the expression from the value once it is stored */
855             value->u.expr = NULL;
856             /* Check that the expression is valid */
857             if (!child)
858             {
859                 return FALSE;
860             }
861             if (!set_expr_value_store(child, param, i))
862             {
863                 return FALSE;
864             }
865             if (child->flags & SEL_DYNAMIC)
866             {
867                 bDynamic = TRUE;
868             }
869         }
870         else
871         {
872             /* Value is not an expression */
873             switch (value->type)
874             {
875                 case INT_VALUE:
876                     if (value->u.i.i1 <= value->u.i.i2)
877                     {
878                         for (j = value->u.i.i1; j <= value->u.i.i2 && i < param->val.nr; ++j)
879                         {
880                             param->val.u.i[i++] = j;
881                         }
882                         if (j != value->u.i.i2 + 1)
883                         {
884                             _gmx_selparser_error("extra values for parameter '%s' skipped", param->name);
885                         }
886                     }
887                     else
888                     {
889                         for (j = value->u.i.i1; j >= value->u.i.i2 && i < param->val.nr; --j)
890                         {
891                             param->val.u.i[i++] = j;
892                         }
893                         if (j != value->u.i.i2 - 1)
894                         {
895                             _gmx_selparser_error("extra values for parameter '%s' skipped", param->name);
896                         }
897                     }
898                     --i;
899                     break;
900                 case REAL_VALUE:
901                     if (value->u.r.r1 != value->u.r.r2)
902                     {
903                         _gmx_selparser_error("real ranges not supported for parameter '%s'", param->name);
904                         return FALSE;
905                     }
906                     param->val.u.r[i] = value->u.r.r1;
907                     break;
908                 case STR_VALUE:
909                     param->val.u.s[i] = strdup(value->u.s);
910                     break;
911                 case POS_VALUE:
912                     gmx_ana_pos_init_const(&param->val.u.p[i], value->u.x);
913                     break;
914                 case NO_VALUE:
915                 case GROUP_VALUE:
916                     gmx_bug("internal error");
917                     return FALSE;
918             }
919         }
920         ++i;
921         value = value->next;
922     }
923     if (value)
924     {
925         _gmx_selparser_error("extra values for parameter '%s'", param->name);
926         return FALSE;
927     }
928     if (i < param->val.nr)
929     {
930         _gmx_selparser_error("not enough values for parameter '%s'", param->name);
931         return FALSE;
932     }
933     if (!bDynamic)
934     {
935         param->flags &= ~SPAR_DYNAMIC;
936     }
937     if (param->nvalptr)
938     {
939         *param->nvalptr = param->val.nr;
940     }
941     param->nvalptr = NULL;
942
943     return TRUE;
944 }
945
946 /*! \brief
947  * Parses the values for a gmx_boolean parameter.
948  *
949  * \param[in] name   Name by which the parameter was given.
950  * \param[in] nval   Number of values in \p values.
951  * \param[in] values Pointer to the list of values.
952  * \param     param  Parameter to parse.
953  * \returns   TRUE if the values were parsed successfully, FALSE otherwise.
954  */
955 static gmx_bool
956 parse_values_gmx_bool(const char *name, int nval, t_selexpr_value *values, gmx_ana_selparam_t *param)
957 {
958     gmx_bool bSetNo;
959     int  len;
960
961     if (param->val.type != NO_VALUE)
962     {
963         gmx_bug("internal error");
964         return FALSE;
965     }
966     if (nval > 1 || (values && values->type != INT_VALUE))
967     {
968         _gmx_selparser_error("gmx_boolean parameter '%s' takes only a yes/no/on/off/0/1 value", param->name);
969         return FALSE;
970     }
971
972     bSetNo = FALSE;
973     /* Check if the parameter name is given with a 'no' prefix */
974     len = strlen(name);
975     if (len > 2 && name[0] == 'n' && name[1] == 'o'
976         && strncmp(name+2, param->name, len-2) == 0)
977     {
978         bSetNo = TRUE;
979     }
980     if (bSetNo && nval > 0)
981     {
982         _gmx_selparser_error("gmx_boolean parameter 'no%s' should not have a value", param->name);
983         return FALSE;
984     }
985     if (values && values->u.i.i1 == 0)
986     {
987         bSetNo = TRUE;
988     }
989
990     *param->val.u.b = bSetNo ? FALSE : TRUE;
991     return TRUE;
992 }
993
994 /*! \brief
995  * Parses the values for an enumeration parameter.
996  *
997  * \param[in] nval   Number of values in \p values.
998  * \param[in] values Pointer to the list of values.
999  * \param     param  Parameter to parse.
1000  * \returns   TRUE if the values were parsed successfully, FALSE otherwise.
1001  */
1002 static gmx_bool
1003 parse_values_enum(int nval, t_selexpr_value *values, gmx_ana_selparam_t *param)
1004 {
1005     int  i, len, match;
1006
1007     if (nval != 1)
1008     {
1009         _gmx_selparser_error("a single value is required for parameter '%s'", param->name);
1010         return FALSE;
1011     }
1012     if (values->type != STR_VALUE || param->val.type != STR_VALUE)
1013     {
1014         gmx_bug("internal error");
1015         return FALSE;
1016     }
1017     if (values->bExpr)
1018     {
1019         _gmx_selparser_error("expression value for enumerated parameter '%s' not supported", param->name);
1020         return FALSE;
1021     }
1022
1023     len = strlen(values->u.s);
1024     i = 1;
1025     match = 0;
1026     while (param->val.u.s[i] != NULL)
1027     {
1028         if (strncmp(values->u.s, param->val.u.s[i], len) == 0)
1029         {
1030             /* Check if there is a duplicate match */
1031             if (match > 0)
1032             {
1033                 _gmx_selparser_error("ambiguous value for parameter '%s'", param->name);
1034                 return FALSE;
1035             }
1036             match = i;
1037         }
1038         ++i;
1039     }
1040     if (match == 0)
1041     {
1042         _gmx_selparser_error("invalid value for parameter '%s'", param->name);
1043         return FALSE;
1044     }
1045     param->val.u.s[0] = param->val.u.s[match];
1046     return TRUE;
1047 }
1048
1049 /*! \brief
1050  * Replaces constant expressions with their values.
1051  *
1052  * \param[in,out] values First element in the value list to process.
1053  */
1054 static void
1055 convert_const_values(t_selexpr_value *values)
1056 {
1057     t_selexpr_value *val;
1058
1059     val = values;
1060     while (val)
1061     {
1062         if (val->bExpr && val->u.expr->v.type != GROUP_VALUE &&
1063             val->u.expr->type == SEL_CONST)
1064         {
1065             t_selelem *expr = val->u.expr;
1066             val->bExpr = FALSE;
1067             switch (expr->v.type)
1068             {
1069                 case INT_VALUE:
1070                     val->u.i.i1 = val->u.i.i2 = expr->v.u.i[0];
1071                     break;
1072                 case REAL_VALUE:
1073                     val->u.r.r1 = val->u.r.r2 = expr->v.u.r[0];
1074                     break;
1075                 case STR_VALUE:
1076                     val->u.s = expr->v.u.s[0];
1077                     break;
1078                 case POS_VALUE:
1079                     copy_rvec(expr->v.u.p->x[0], val->u.x);
1080                     break;
1081                 default:
1082                     gmx_bug("internal error");
1083                     break;
1084             }
1085             _gmx_selelem_free(expr);
1086         }
1087         val = val->next;
1088     }
1089 }
1090
1091 /*!
1092  * \param     pparams List of parameters from the selection parser.
1093  * \param[in] nparam  Number of parameters in \p params.
1094  * \param     params  Array of parameters to parse.
1095  * \param     root    Selection element to which child expressions are added.
1096  * \param[in] scanner Scanner data structure.
1097  * \returns   TRUE if the parameters were parsed successfully, FALSE otherwise.
1098  *
1099  * Initializes the \p params array based on the parameters in \p pparams.
1100  * See the documentation of \c gmx_ana_selparam_t for different options
1101  * available for parsing.
1102  *
1103  * The list \p pparams and any associated values are freed after the parameters
1104  * have been processed, no matter is there was an error or not.
1105  */
1106 gmx_bool
1107 _gmx_sel_parse_params(t_selexpr_param *pparams, int nparam, gmx_ana_selparam_t *params,
1108                       t_selelem *root, void *scanner)
1109 {
1110     t_selexpr_param    *pparam;
1111     gmx_ana_selparam_t *oparam;
1112     gmx_bool                bOk, rc;
1113     int                 i;
1114
1115     /* Check that the value pointers of SPAR_VARNUM parameters are NULL and
1116      * that they are not NULL for other parameters */
1117     bOk = TRUE;
1118     for (i = 0; i < nparam; ++i)
1119     {
1120         if (params[i].val.type != POS_VALUE && (params[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
1121         {
1122             if (params[i].val.u.ptr != NULL)
1123             {
1124                 _gmx_selparser_error("warning: value pointer of parameter '%s' is not NULL\n"
1125                                      "         although it should be for SPAR_VARNUM and SPAR_ATOMVAL parameters\n",
1126                                      params[i].name);
1127             }
1128             if ((params[i].flags & SPAR_VARNUM)
1129                 && (params[i].flags & SPAR_DYNAMIC) && !params[i].nvalptr)
1130             {
1131                 _gmx_selparser_error("error: nvalptr of parameter '%s' is NULL\n"
1132                                      "       but both SPAR_VARNUM and SPAR_DYNAMIC are specified\n",
1133                                      params[i].name);
1134                 bOk = FALSE;
1135             }
1136         }
1137         else
1138         {
1139             if (params[i].val.u.ptr == NULL)
1140             {
1141                 _gmx_selparser_error("error: value pointer of parameter '%s' is NULL\n",
1142                                      params[i].name);
1143                 bOk = FALSE;
1144             }
1145         }
1146     }
1147     if (!bOk)
1148     {
1149         _gmx_selexpr_free_params(pparams);
1150         return FALSE;
1151     }
1152     /* Parse the parameters */
1153     pparam = pparams;
1154     i      = 0;
1155     while (pparam)
1156     {
1157         /* Find the parameter and make some checks */
1158         if (pparam->name != NULL)
1159         {
1160             i = -1;
1161             oparam = gmx_ana_selparam_find(pparam->name, nparam, params);
1162         }
1163         else if (i >= 0)
1164         {
1165             oparam = &params[i];
1166             if (oparam->name != NULL)
1167             {
1168                 oparam = NULL;
1169                 _gmx_selparser_error("too many NULL parameters provided");
1170                 bOk = FALSE;
1171                 goto next_param;
1172             }
1173             ++i;
1174         }
1175         else
1176         {
1177             _gmx_selparser_error("all NULL parameters should appear in the beginning of the list");
1178             bOk = FALSE;
1179             pparam = pparam->next;
1180             continue;
1181         }
1182         if (!oparam)
1183         {
1184             _gmx_selparser_error("unknown parameter '%s' skipped", pparam->name);
1185             bOk = FALSE;
1186             goto next_param;
1187         }
1188         if (oparam->flags & SPAR_SET)
1189         {
1190             _gmx_selparser_error("parameter '%s' set multiple times, extra values skipped", pparam->name);
1191             bOk = FALSE;
1192             goto next_param;
1193         }
1194         oparam->flags |= SPAR_SET;
1195         /* Process the values for the parameter */
1196         convert_const_values(pparam->value);
1197         if (convert_values(pparam->value, oparam->val.type, scanner) != 0)
1198         {
1199             _gmx_selparser_error("invalid value for parameter '%s'", pparam->name);
1200             bOk = FALSE;
1201             goto next_param;
1202         }
1203         if (oparam->val.type == NO_VALUE)
1204         {
1205             rc = parse_values_gmx_bool(pparam->name, pparam->nval, pparam->value, oparam);
1206         }
1207         else if (oparam->flags & SPAR_RANGES)
1208         {
1209             rc = parse_values_range(pparam->nval, pparam->value, oparam);
1210         }
1211         else if (oparam->flags & SPAR_VARNUM)
1212         {
1213             if (pparam->nval == 1 && pparam->value->bExpr)
1214             {
1215                 rc = parse_values_varnum_expr(pparam->nval, pparam->value, oparam, root);
1216             }
1217             else
1218             {
1219                 rc = parse_values_varnum(pparam->nval, pparam->value, oparam, root);
1220             }
1221         }
1222         else if (oparam->flags & SPAR_ENUMVAL)
1223         {
1224             rc = parse_values_enum(pparam->nval, pparam->value, oparam);
1225         }
1226         else
1227         {
1228             rc = parse_values_std(pparam->nval, pparam->value, oparam, root);
1229         }
1230         if (!rc)
1231         {
1232             bOk = FALSE;
1233         }
1234         /* Advance to the next parameter */
1235 next_param:
1236         pparam = pparam->next;
1237     }
1238     /* Check that all required parameters are present */
1239     for (i = 0; i < nparam; ++i)
1240     {
1241         if (!(params[i].flags & SPAR_OPTIONAL) && !(params[i].flags & SPAR_SET))
1242         {
1243             _gmx_selparser_error("required parameter '%s' not specified", params[i].name);
1244             bOk = FALSE;
1245         }
1246     }
1247     
1248     _gmx_selexpr_free_params(pparams);
1249     return bOk;
1250 }