src/sg_ses.c
static int
parse_index(struct opts_t *op)
{
    int n;
    const char * cp;
    char * mallcp;
    char * c2p;
    const struct element_type_t * etp;
    char b[64];

    op->ind_given = 1;
    if ((cp = strchr(op->index_str, ','))) {
        if (0 == strcmp("-1", cp + 1))
            n = -1;
        else {
            n = sg_get_num(cp + 1);
            if ((n < 0) || (n > 255)) {
                pr2serr("bad argument to '--index', after comma expect "
                        "number from -1 to 255\n");
                return SG_LIB_SYNTAX_ERROR;
            }
        }
        op->ind_indiv = n;
        n = cp - op->index_str;
        if (n >= (int)sizeof(b)) {
            pr2serr("bad argument to '--index', string prior to comma too "
                    "long\n");
            return SG_LIB_SYNTAX_ERROR;
        }
    } else {
        n = strlen(op->index_str);
        if (n >= (int)sizeof(b)) {
            pr2serr("bad argument to '--index', string too long\n");
            return SG_LIB_SYNTAX_ERROR;
        }
    }
    strncpy(b, op->index_str, n);
    b[n] = '\0';
    if (0 == strcmp("-1", b)) {
        if (cp) {
            pr2serr("bad argument to '--index', unexpected '-1' type header "
                    "index\n");
            return SG_LIB_SYNTAX_ERROR;
        }
        op->ind_th = 0;
        op->ind_indiv = -1;
    } else if (isdigit(b[0])) {
        n = sg_get_num(b);
        if ((n < 0) || (n > 255)) {
            pr2serr("bad numeric argument to '--index', expect number from 0 "
                    "to 255\n");
            return SG_LIB_SYNTAX_ERROR;
        }
        if (cp)
            op->ind_th = n;
        else {
            op->ind_th = 0;
            op->ind_indiv = n;
        }
    } else if ('_' == b[0]) {
        if ((c2p = strchr(b + 1, '_')))
            *c2p = '\0';
        n = sg_get_num(b + 1);
        if ((n < 0) || (n > 255)) {
            pr2serr("bad element type code for '--index', expect value from "
                    "0 to 255\n");
            return SG_LIB_SYNTAX_ERROR;
        }
        element_type_by_code.elem_type_code = n;
        mallcp = (char *)malloc(8);  /* willfully forget about freeing this */
        mallcp[0] = '_';
        snprintf(mallcp + 1, 6, "%d", n);
        element_type_by_code.abbrev = mallcp;
        if (c2p) {
            n = sg_get_num(c2p + 1);
            if ((n < 0) || (n > 255)) {
                pr2serr("bad element type code <num> for '--index', expect "
                        "<num> from 0 to 255\n");
                return SG_LIB_SYNTAX_ERROR;
            }
            op->ind_et_inst = n;
        }
        op->ind_etp = &element_type_by_code;
        if (NULL == cp)
            op->ind_indiv = -1;
    } else { /* element type abbreviation perhaps followed by <num> */
        int blen = strlen(b);

        for (etp = element_type_arr; etp->desc; ++etp) {
            n = strlen(etp->abbrev);
            if ((n == blen) && (0 == strncmp(b, etp->abbrev, n)))
                break;
        }
        if (NULL == etp->desc) {
            pr2serr("bad element type abbreviation [%s] for '--index'\n"
                    "use '--enumerate' to see possibles\n", b);
            return SG_LIB_SYNTAX_ERROR;
        }
        if ((int)strlen(b) > n) {
            n = sg_get_num(b + n);
            if ((n < 0) || (n > 255)) {
                pr2serr("bad element type abbreviation <num> for '--index', "
                        "expect <num> from 0 to 255\n");
                return SG_LIB_SYNTAX_ERROR;
            }
            op->ind_et_inst = n;
        }
        op->ind_etp = etp;
        if (NULL == cp)
            op->ind_indiv = -1;
    }
    if (op->verbose > 1) {
        if (op->ind_etp)
            pr2serr("   element type abbreviation: %s, etp_num=%d, "
                    "individual index=%d\n", op->ind_etp->abbrev,
                    op->ind_et_inst, op->ind_indiv);
        else
            pr2serr("   type header index=%d, individual index=%d\n",
                    op->ind_th, op->ind_indiv);
    }
    return 0;
}
/* Return 0 for okay, else an error */
Return 0 for okay, else an error
Return 0 for okay else an error
static int
parse_index(struct opts_t *op)
{
    int n;
    const char * cp;
    char * mallcp;
    char * c2p;
    const struct element_type_t * etp;
    char b[64];
    op->ind_given = 1;
    if ((cp = strchr(op->index_str, ','))) {
        if (0 == strcmp("-1", cp + 1))
            n = -1;
        else {
            n = sg_get_num(cp + 1);
            if ((n < 0) || (n > 255)) {
                pr2serr("bad argument to '--index', after comma expect "
                        "number from -1 to 255\n");
                return SG_LIB_SYNTAX_ERROR;
            }
        }
        op->ind_indiv = n;
        n = cp - op->index_str;
        if (n >= (int)sizeof(b)) {
            pr2serr("bad argument to '--index', string prior to comma too "
                    "long\n");
            return SG_LIB_SYNTAX_ERROR;
        }
    } else {
        n = strlen(op->index_str);
        if (n >= (int)sizeof(b)) {
            pr2serr("bad argument to '--index', string too long\n");
            return SG_LIB_SYNTAX_ERROR;
        }
    }
    strncpy(b, op->index_str, n);
    b[n] = '\0';
    if (0 == strcmp("-1", b)) {
        if (cp) {
            pr2serr("bad argument to '--index', unexpected '-1' type header "
                    "index\n");
            return SG_LIB_SYNTAX_ERROR;
        }
        op->ind_th = 0;
        op->ind_indiv = -1;
    } else if (isdigit(b[0])) {
        n = sg_get_num(b);
        if ((n < 0) || (n > 255)) {
            pr2serr("bad numeric argument to '--index', expect number from 0 "
                    "to 255\n");
            return SG_LIB_SYNTAX_ERROR;
        }
        if (cp)
            op->ind_th = n;
        else {
            op->ind_th = 0;
            op->ind_indiv = n;
        }
    } else if ('_' == b[0]) {
        if ((c2p = strchr(b + 1, '_')))
            *c2p = '\0';
        n = sg_get_num(b + 1);
        if ((n < 0) || (n > 255)) {
            pr2serr("bad element type code for '--index', expect value from "
                    "0 to 255\n");
            return SG_LIB_SYNTAX_ERROR;
        }
        element_type_by_code.elem_type_code = n;
        mallcp = (char *)malloc(8);  
        mallcp[0] = '_';
        snprintf(mallcp + 1, 6, "%d", n);
        element_type_by_code.abbrev = mallcp;
        if (c2p) {
            n = sg_get_num(c2p + 1);
            if ((n < 0) || (n > 255)) {
                pr2serr("bad element type code <num> for '--index', expect "
                        "<num> from 0 to 255\n");
                return SG_LIB_SYNTAX_ERROR;
            }
            op->ind_et_inst = n;
        }
        op->ind_etp = &element_type_by_code;
        if (NULL == cp)
            op->ind_indiv = -1;
    } else { 
        int blen = strlen(b);
        for (etp = element_type_arr; etp->desc; ++etp) {
            n = strlen(etp->abbrev);
            if ((n == blen) && (0 == strncmp(b, etp->abbrev, n)))
                break;
        }
        if (NULL == etp->desc) {
            pr2serr("bad element type abbreviation [%s] for '--index'\n"
                    "use '--enumerate' to see possibles\n", b);
            return SG_LIB_SYNTAX_ERROR;
        }
        if ((int)strlen(b) > n) {
            n = sg_get_num(b + n);
            if ((n < 0) || (n > 255)) {
                pr2serr("bad element type abbreviation <num> for '--index', "
                        "expect <num> from 0 to 255\n");
                return SG_LIB_SYNTAX_ERROR;
            }
            op->ind_et_inst = n;
        }
        op->ind_etp = etp;
        if (NULL == cp)
            op->ind_indiv = -1;
    }
    if (op->verbose > 1) {
        if (op->ind_etp)
            pr2serr("   element type abbreviation: %s, etp_num=%d, "
                    "individual index=%d\n", op->ind_etp->abbrev,
                    op->ind_et_inst, op->ind_indiv);
        else
            pr2serr("   type header index=%d, individual index=%d\n",
                    op->ind_th, op->ind_indiv);
    }
    return 0;
}
static int parse_index ( struct opts_t * op ) { int n ; const char * cp ; char * mallcp ; char * c2p ; const struct element_type_t * etp ; char b [ 64 ] ; op -> ind_given = 1 ; if ( ( cp = strchr ( op -> index_str , ' ' ) ) ) { if ( 0 == strcmp ( " " , cp + 1 ) ) n = -1 ; else { n = sg_get_num ( cp + 1 ) ; if ( ( n < 0 ) || ( n > 255 ) ) { pr2serr ( " " " \n " ) ; return SG_LIB_SYNTAX_ERROR ; } } op -> ind_indiv = n ; n = cp - op -> index_str ; if ( n >= ( int ) sizeof ( b ) ) { pr2serr ( " " " \n " ) ; return SG_LIB_SYNTAX_ERROR ; } } else { n = strlen ( op -> index_str ) ; if ( n >= ( int ) sizeof ( b ) ) { pr2serr ( " \n " ) ; return SG_LIB_SYNTAX_ERROR ; } } strncpy ( b , op -> index_str , n ) ; b [ n ] = ' \0 ' ; if ( 0 == strcmp ( " " , b ) ) { if ( cp ) { pr2serr ( " " " \n " ) ; return SG_LIB_SYNTAX_ERROR ; } op -> ind_th = 0 ; op -> ind_indiv = -1 ; } else if ( isdigit ( b [ 0 ] ) ) { n = sg_get_num ( b ) ; if ( ( n < 0 ) || ( n > 255 ) ) { pr2serr ( " " " \n " ) ; return SG_LIB_SYNTAX_ERROR ; } if ( cp ) op -> ind_th = n ; else { op -> ind_th = 0 ; op -> ind_indiv = n ; } } else if ( ' ' == b [ 0 ] ) { if ( ( c2p = strchr ( b + 1 , ' ' ) ) ) * c2p = ' \0 ' ; n = sg_get_num ( b + 1 ) ; if ( ( n < 0 ) || ( n > 255 ) ) { pr2serr ( " " " \n " ) ; return SG_LIB_SYNTAX_ERROR ; } element_type_by_code . elem_type_code = n ; mallcp = ( char * ) malloc ( 8 ) ; mallcp [ 0 ] = ' ' ; snprintf ( mallcp + 1 , 6 , " " , n ) ; element_type_by_code . abbrev = mallcp ; if ( c2p ) { n = sg_get_num ( c2p + 1 ) ; if ( ( n < 0 ) || ( n > 255 ) ) { pr2serr ( " " " \n " ) ; return SG_LIB_SYNTAX_ERROR ; } op -> ind_et_inst = n ; } op -> ind_etp = & element_type_by_code ; if ( NULL == cp ) op -> ind_indiv = -1 ; } else { int blen = strlen ( b ) ; for ( etp = element_type_arr ; etp -> desc ; ++ etp ) { n = strlen ( etp -> abbrev ) ; if ( ( n == blen ) && ( 0 == strncmp ( b , etp -> abbrev , n ) ) ) break ; } if ( NULL == etp -> desc ) { pr2serr ( " \n " " \n " , b ) ; return SG_LIB_SYNTAX_ERROR ; } if ( ( int ) strlen ( b ) > n ) { n = sg_get_num ( b + n ) ; if ( ( n < 0 ) || ( n > 255 ) ) { pr2serr ( " " " \n " ) ; return SG_LIB_SYNTAX_ERROR ; } op -> ind_et_inst = n ; } op -> ind_etp = etp ; if ( NULL == cp ) op -> ind_indiv = -1 ; } if ( op -> verbose > 1 ) { if ( op -> ind_etp ) pr2serr ( " " " \n " , op -> ind_etp -> abbrev , op -> ind_et_inst , op -> ind_indiv ) ; else pr2serr ( " \n " , op -> ind_th , op -> ind_indiv ) ; } return 0 ; }
Return 0 for okay, else an error
Return 0 for okay else an error
/* willfully forget about freeing this */ /* element type abbreviation perhaps followed by <num> */