IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 30614


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

Location:
trunk/Ohana/src/opihi
Files:
7 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/include/dvoshell.h

    r28958 r30614  
    107107      MEAS_XCCD_ERR,
    108108      MEAS_YCCD_ERR,
     109      MEAS_POS_SYS_ERR,
    109110      MEAS_XMOSAIC,
    110111      MEAS_YMOSAIC,
     
    116117      MEAS_IMAGE_ID,
    117118      MEAS_PSF_QF,
     119      MEAS_PSF_QF_PERFECT,
    118120      MEAS_PSF_CHISQ,
    119121      MEAS_PSF_NDOF,
     
    218220      IMAGE_Y_UL_FP,
    219221      IMAGE_Y_UR_FP,
     222      IMAGE_X_ERR_SYS,
     223      IMAGE_Y_ERR_SYS,
     224      IMAGE_MAG_ERR_SYS,
     225      IMAGE_NFIT_PHOTOM,
     226      IMAGE_NFIT_ASTROM,
     227      IMAGE_NLINK_PHOTOM,
     228      IMAGE_NLINK_ASTROM
    220229};
    221230
  • trunk/Ohana/src/opihi/include/shell.h

    r27790 r30614  
    7676char         *expand_vars               PROTO((char *line));
    7777char         *expand_vectors            PROTO((char *line));
    78 char         *parse                     PROTO((char *line));
     78char         *parse                     PROTO((int *status, char *line));
    7979char        **parse_commands            PROTO((char *, int *));
    8080void          welcome                   PROTO((void));
  • trunk/Ohana/src/opihi/lib.shell/VectorOps.c

    r20936 r30614  
    178178  vec[0].Nelements = Nelements;
    179179  if (type == OPIHI_FLT) {
    180     REALLOCATE (vec[0].elements.Flt, opihi_flt, Nelements);
     180    REALLOCATE (vec[0].elements.Flt, opihi_flt, MAX(1, Nelements));
    181181    vec[0].type = OPIHI_FLT;
    182182  } else {
    183     REALLOCATE (vec[0].elements.Int, opihi_int, Nelements);
     183    REALLOCATE (vec[0].elements.Int, opihi_int, MAX(1, Nelements));
    184184    vec[0].type = OPIHI_INT;
    185185  }
  • trunk/Ohana/src/opihi/lib.shell/command.c

    r27790 r30614  
    1010  Command *cmd;
    1111
    12   // rawline = NULL;
    13   rawline = strcreate (line);
     12  // the input line is never NULL
     13  if (!line) { fprintf (stderr, "programming error\n"); abort(); }
     14
     15  rawline = strcreate (line);  // used for error messages which should echo the unparsed line
    1416
    1517  /* force a space between ! and first word: !ls becomes ! ls */
     
    2224  /* expand anything of the form $fred or $fred$sam, etc */
    2325  line = expand_vars (line);     /* line is freed here, new one allocated */
     26  if (!line) goto escape;
     27
    2428  /* expand anything of the form fred[N] */
    2529  line = expand_vectors (line);  /* line is freed here, new one allocated */
     30  if (!line) goto escape;
    2631
    2732  // print the line with the variables and vectors expanded, but before evaluating
     
    2934  if (VERBOSE_SHELL == OPIHI_VERBOSE_ON) gprint (GP_ERR, "opihi: %s\n", line);
    3035
    31   /* solve math expresions, assign variable, if needed */
    32   line = parse (line);        /* line is freed here, new one allocated */
    33   /* any entry in line of the form {foo} returns value or tmp vector / buffer */
     36  /* solve math expresions, assign variable, if needed.  any entry in line of the form {foo}
     37   * returns value or tmp vector / buffer */
     38  line = parse (&status, line);        /* line is freed here, new one allocated */
     39  if (!status) goto escape;
    3440
    3541  /* we may have reallocated line, return new pointer */
     
    3945  if (argc == 0) {
    4046      FREE (rawline);
     47      set_int_variable ("STATUS", TRUE);
    4148      return (TRUE);  /* empty command or assignment */
    4249  }
     
    7481  FREE (rawline);
    7582  return (status);
     83
     84 escape:
     85  set_int_variable ("STATUS", FALSE);
     86
     87  # if (DEBUG)
     88  gprint (GP_ERR, "command: %s, status: %d\n", line, FALSE);
     89  # endif
     90
     91  FREE (rawline);
     92
     93  if (VERBOSE_SHELL != OPIHI_VERBOSE_OFF) gprint (GP_ERR, "error on line: %s\n", rawline);
     94
     95  // return the current value of line, in case it was modified
     96  *outline = line;
     97  return FALSE;
    7698}
    7799
  • trunk/Ohana/src/opihi/lib.shell/expand_vars.c

    r15878 r30614  
    6464    /* variable assignment (skip these variables) */
    6565    if ((L == line) && (V1 != NULL)) {
    66       if ((*V1 == '=') || !strcmp (V1, "++") || !strcmp (V1, "--")) {
     66      int isAssignment;
     67
     68      isAssignment = FALSE;
     69      isAssignment |= (V1[0] == '=');
     70      isAssignment |= (V1[0] == '+') && (V1[1] == '=');
     71      isAssignment |= (V1[0] == '-') && (V1[1] == '=');
     72      isAssignment |= (V1[0] == '+') && (V1[1] == '+');
     73      isAssignment |= (V1[0] == '-') && (V1[1] == '-');
     74
     75      if (isAssignment) {
    6776        *N = *L;
    6877        free (V0);
  • 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}
  • trunk/Ohana/src/opihi/lib.shell/stack_math.c

    r27435 r30614  
    972972        *out = OP;                                              \
    973973      }                                                         \
    974       clear_stack (V1);                                         \
    975       return (TRUE);                                            \
     974      goto escape;                                              \
    976975    }                                                           \
    977976    if ((V1->vector->type == OPIHI_INT) && (FTYPE == 'S')) {    \
     
    982981        *out = OP;                                              \
    983982      }                                                         \
    984       clear_stack (V1);                                         \
    985       return (TRUE);                                            \
     983      goto escape;                                              \
    986984    }                                                           \
    987985    if ((V1->vector->type == OPIHI_INT) && (FTYPE == 's')) {    \
     
    992990        *out = OP;                                              \
    993991      }                                                         \
    994       clear_stack (V1);                                         \
    995       return (TRUE);                                            \
     992      goto escape;                                              \
    996993    } }                                                 
    997994
     
    10351032# undef V_FUNC
    10361033
     1034escape:
     1035
    10371036  if (V1[0].type == 'v') {
    10381037    free (V1[0].vector[0].elements.Ptr);
Note: See TracChangeset for help on using the changeset viewer.