IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Feb 13, 2011, 11:28:01 AM (15 years ago)
Author:
eugene
Message:

add += and -= to opihi; allow 0 length vectors; fix mem leak in some binary math operations; check and handle some error cases in command.c

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/lib.shell/parse.c

    r27491 r30614  
    11# include "opihi.h"
    22
    3 char *parse (char *line) {
     3char *parse (int *status, char *line) {
    44
    55  double fval;
    66  float *fptr;
    77  char *newline, *N, *L, *val, *B, *V, *V0, *V1, *c1, *c2, *p;
    8   int Nx, Ny, Nbytes, status, size, NLINE, isBuffer, inRange;
     8  int Nx, Ny, Nbytes, filestatus, size, NLINE, isBuffer, inRange;
    99  FILE *f;
    1010  Vector *vec;
    1111  Buffer *buf;
     12
     13  *status = TRUE;
    1214
    1315  Ny = 0;
     
    4951    }
    5052
     53    if (!strncmp (V1, "+=", 2)) {
     54        V1 ++;
     55        if (*V1 == 0) goto error;
     56        V1 ++;
     57        if (*V1 == 0) goto error;
     58
     59        val = get_variable (V0);
     60        if (val == NULL) {
     61            fval = 0.0;
     62        } else {
     63            fval = atof (val);
     64        }
     65
     66        /* dvomath returns a new string, or NULL, with the result of the expression */
     67        val = dvomath (1, &V1, &size, 0);
     68        if (val == NULL) goto error;
     69        fval += atof(val);
     70        // save the result
     71        set_variable (V0, fval);
     72        goto escape;
     73    }
     74
     75    if (!strncmp (V1, "-=", 2)) {
     76        V1 ++;
     77        if (*V1 == 0) goto error;
     78        V1 ++;
     79        if (*V1 == 0) goto error;
     80
     81        val = get_variable (V0);
     82        if (val == NULL) {
     83            fval = 0.0;
     84        } else {
     85            fval = atof (val);
     86        }
     87
     88        /* dvomath returns a new string, or NULL, with the result of the expression */
     89        val = dvomath (1, &V1, &size, 0);
     90        if (val == NULL) goto error;
     91        fval -= atof(val);
     92        // save the result
     93        set_variable (V0, fval);
     94        goto escape;
     95    }
     96
    5197    /* not an assignement, syntax error */
    5298    if (*V1 != '=') goto error;
     
    73119      Nbytes = fread (val, 1, 1023, f);
    74120      val[Nbytes] = 0;
    75       status = pclose (f);
     121      filestatus = pclose (f);
    76122      free (B);
    77123
    78       if (status) gprint (GP_ERR, "warning: exit status of command %d\n", status);
     124      if (filestatus) gprint (GP_ERR, "warning: exit status of command %d\n", filestatus);
    79125
    80126      /* convert all but last return to ' '.  drop last return */
     
    88134        }
    89135      }
     136      // save the resulting value
     137      set_str_variable (V0, val);
     138      goto escape;  /* frees temp variables */
     139    }
     140
     141    /* simple variable assignment */
     142    /* dvomath returns a new string, or NULL, with the result of the expression */
     143    val = dvomath (1, &V1, &size, 0);
     144    if (val == NULL) {
     145      while (OHANA_WHITESPACE (*V1)) V1++;
     146      val = strcreate (V1);
    90147    }
    91 
    92     /* simple variable assignment */
    93     if (*V1 != '`') {
    94       /* dvomath returns a new string, or NULL, with the result of the expression */
    95       val = dvomath (1, &V1, &size, 0);
    96       if (val == NULL) {
    97         while (OHANA_WHITESPACE (*V1)) V1++;
    98         val = strcreate (V1);
    99       }
    100     }
    101     /* both dvomath and command replacement create (char *) val */
     148    // save the result
    102149    set_str_variable (V0, val);
    103150    goto escape;  /* frees temp variables */
     
    172219      if (!inRange) {
    173220        gprint (GP_ERR, "no element %d,%d\n", Nx, Ny);
    174         goto escape;
     221        goto error;
    175222      }
    176223      if (Nx < 0) Nx += buf[0].header.Naxis[0];
     
    188235      if (!inRange) {
    189236        gprint (GP_ERR, "no element %d\n", Nx);
    190         goto escape;
     237        goto error;
    191238      }
    192239      if (Nx < 0) Nx += vec[0].Nelements;
     
    197244      }
    198245    }
    199 
    200246    goto escape;
    201247  }
     
    226272    if (c1 == NULL) {
    227273      gprint (GP_ERR, "no close brackets!\n");
    228       goto escape;
     274      goto error;
    229275    }
    230276    c2 = strchr (L, '{');
    231277    if ((c2 != NULL) && (c2 < c1)) {
    232278      gprint (GP_ERR, "can't nest brackets!\n");
    233       goto escape;
     279      goto error;
    234280    }
    235281    *c1 = 0;
     
    240286    if (val == NULL) {
    241287      print_error ();
    242       goto escape;
     288      goto error;
    243289    }
    244290
     
    257303 
    258304  REALLOCATE (newline, char, strlen (newline) + 1);
     305  *status = TRUE;
    259306  return (newline);
    260307
    261308error:
    262309  gprint (GP_ERR, "syntax error\n");
    263 
    264 escape:
     310  // assignment or increment operation: free line and return NULL
    265311  if (line != (char *) NULL) free (line);
    266312  if (val != (char *) NULL) free (val);
    267313  if (V0 != (char *) NULL) free (V0);
     314  *status = FALSE;
     315  return NULL;
     316
     317escape:
     318  // assignment or increment operation: free line and return NULL
     319  if (line != (char *) NULL) free (line);
     320  if (val != (char *) NULL) free (val);
     321  if (V0 != (char *) NULL) free (V0);
     322  *status = TRUE;
    268323  return (NULL);
    269324}
Note: See TracChangeset for help on using the changeset viewer.