src/sg_senddiag.c
static int
build_diag_page(const char * inp, unsigned char * mp_arr, int * mp_arr_len,
                int max_arr_len)
{
    int in_len, k, j, m;
    unsigned int h;
    const char * lcp;
    char * cp;
    char * c2p;

    if ((NULL == inp) || (NULL == mp_arr) ||
        (NULL == mp_arr_len))
        return 1;
    lcp = inp;
    in_len = strlen(inp);
    if (0 == in_len)
        *mp_arr_len = 0;
    if ('-' == inp[0]) {        /* read from stdin */
        char line[512];
        char carry_over[4];
        int off = 0;
        int split_line;

        carry_over[0] = 0;
        for (j = 0; j < 512; ++j) {
            if (NULL == fgets(line, sizeof(line), stdin))
                break;
            in_len = strlen(line);
            if (in_len > 0) {
                if ('\n' == line[in_len - 1]) {
                    --in_len;
                    line[in_len] = '\0';
                    split_line = 0;
                } else
                    split_line = 1;
            }
            if (in_len < 1) {
                carry_over[0] = 0;
                continue;
            }
            if (carry_over[0]) {
                if (isxdigit(line[0])) {
                    carry_over[1] = line[0];
                    carry_over[2] = '\0';
                    if (1 == sscanf(carry_over, "%x", &h))
                        mp_arr[off - 1] = h;       /* back up and overwrite */
                    else {
                        fprintf(stderr, "build_diag_page: carry_over error "
                                "['%s'] around line %d\n", carry_over, j + 1);
                        return 1;
                    }
                    lcp = line + 1;
                    --in_len;
                } else
                    lcp = line;
                carry_over[0] = 0;
            } else
                lcp = line;
            m = strspn(lcp, " \t");
            if (m == in_len)
                continue;
            lcp += m;
            in_len -= m;
            if ('#' == *lcp)
                continue;
            k = strspn(lcp, "0123456789aAbBcCdDeEfF ,\t");
            if ((k < in_len) && ('#' != lcp[k])) {
                fprintf(stderr, "build_diag_page: syntax error at "
                        "line %d, pos %d\n", j + 1, m + k + 1);
                return 1;
            }
            for (k = 0; k < 1024; ++k) {
                if (1 == sscanf(lcp, "%x", &h)) {
                    if (h > 0xff) {
                        fprintf(stderr, "build_diag_page: hex number "
                                "larger than 0xff in line %d, pos %d\n",
                                j + 1, (int)(lcp - line + 1));
                        return 1;
                    }
                    if (split_line && (1 == strlen(lcp))) {
                        /* single trailing hex digit might be a split pair */
                        carry_over[0] = *lcp;
                    }
                    if ((off + k) >= max_arr_len) {
                        fprintf(stderr, "build_diag_page: array length "
                                "exceeded\n");
                        return 1;
                    }
                    mp_arr[off + k] = h;
                    lcp = strpbrk(lcp, " ,\t");
                    if (NULL == lcp)
                        break;
                    lcp += strspn(lcp, " ,\t");
                    if ('\0' == *lcp)
                        break;
                } else {
                    if ('#' == *lcp) {
                        --k;
                        break;
                    }
                    fprintf(stderr, "build_diag_page: error in "
                            "line %d, at pos %d\n", j + 1,
                            (int)(lcp - line + 1));
                    return 1;
                }
            }
            off += (k + 1);
        }
        *mp_arr_len = off;
    } else {        /* hex string on command line */
        k = strspn(inp, "0123456789aAbBcCdDeEfF, ");
        if (in_len != k) {
            fprintf(stderr, "build_diag_page: error at pos %d\n", k + 1);
            return 1;
        }
        for (k = 0; k < max_arr_len; ++k) {
            if (1 == sscanf(lcp, "%x", &h)) {
                if (h > 0xff) {
                    fprintf(stderr, "build_diag_page: hex number larger "
                            "than 0xff at pos %d\n", (int)(lcp - inp + 1));
                    return 1;
                }
                mp_arr[k] = h;
                cp = (char *)strchr(lcp, ',');
                c2p = (char *)strchr(lcp, ' ');
                if (NULL == cp)
                    cp = c2p;
                if (NULL == cp)
                    break;
                if (c2p && (c2p < cp))
                    cp = c2p;
                lcp = cp + 1;
            } else {
                fprintf(stderr, "build_diag_page: error at pos %d\n",
                        (int)(lcp - inp + 1));
                return 1;
            }
        }
        *mp_arr_len = k + 1;
        if (k == max_arr_len) {
            fprintf(stderr, "build_diag_page: array length exceeded\n");
            return 1;
        }
    }
    return 0;
}
/* Read hex numbers from command line (comma separated list) or from */
/* stdin (one per line, comma separated list or space separated list). */
/* Returns 0 if ok, or 1 if error. */
Read hex numbers from command line (comma separated list) or from
stdin (one per line, comma separated list or space separated list).
Returns 0 if ok, or 1 if error.
Read hex numbers from command line ( comma separated list ) or from stdin ( one per line comma separated list or space separated list ) . Returns 0 if ok or 1 if error .
static int
build_diag_page(const char * inp, unsigned char * mp_arr, int * mp_arr_len,
                int max_arr_len)
{
    int in_len, k, j, m;
    unsigned int h;
    const char * lcp;
    char * cp;
    char * c2p;
    if ((NULL == inp) || (NULL == mp_arr) ||
        (NULL == mp_arr_len))
        return 1;
    lcp = inp;
    in_len = strlen(inp);
    if (0 == in_len)
        *mp_arr_len = 0;
    if ('-' == inp[0]) {        
        char line[512];
        char carry_over[4];
        int off = 0;
        int split_line;
        carry_over[0] = 0;
        for (j = 0; j < 512; ++j) {
            if (NULL == fgets(line, sizeof(line), stdin))
                break;
            in_len = strlen(line);
            if (in_len > 0) {
                if ('\n' == line[in_len - 1]) {
                    --in_len;
                    line[in_len] = '\0';
                    split_line = 0;
                } else
                    split_line = 1;
            }
            if (in_len < 1) {
                carry_over[0] = 0;
                continue;
            }
            if (carry_over[0]) {
                if (isxdigit(line[0])) {
                    carry_over[1] = line[0];
                    carry_over[2] = '\0';
                    if (1 == sscanf(carry_over, "%x", &h))
                        mp_arr[off - 1] = h;       
                    else {
                        fprintf(stderr, "build_diag_page: carry_over error "
                                "['%s'] around line %d\n", carry_over, j + 1);
                        return 1;
                    }
                    lcp = line + 1;
                    --in_len;
                } else
                    lcp = line;
                carry_over[0] = 0;
            } else
                lcp = line;
            m = strspn(lcp, " \t");
            if (m == in_len)
                continue;
            lcp += m;
            in_len -= m;
            if ('#' == *lcp)
                continue;
            k = strspn(lcp, "0123456789aAbBcCdDeEfF ,\t");
            if ((k < in_len) && ('#' != lcp[k])) {
                fprintf(stderr, "build_diag_page: syntax error at "
                        "line %d, pos %d\n", j + 1, m + k + 1);
                return 1;
            }
            for (k = 0; k < 1024; ++k) {
                if (1 == sscanf(lcp, "%x", &h)) {
                    if (h > 0xff) {
                        fprintf(stderr, "build_diag_page: hex number "
                                "larger than 0xff in line %d, pos %d\n",
                                j + 1, (int)(lcp - line + 1));
                        return 1;
                    }
                    if (split_line && (1 == strlen(lcp))) {
                        carry_over[0] = *lcp;
                    }
                    if ((off + k) >= max_arr_len) {
                        fprintf(stderr, "build_diag_page: array length "
                                "exceeded\n");
                        return 1;
                    }
                    mp_arr[off + k] = h;
                    lcp = strpbrk(lcp, " ,\t");
                    if (NULL == lcp)
                        break;
                    lcp += strspn(lcp, " ,\t");
                    if ('\0' == *lcp)
                        break;
                } else {
                    if ('#' == *lcp) {
                        --k;
                        break;
                    }
                    fprintf(stderr, "build_diag_page: error in "
                            "line %d, at pos %d\n", j + 1,
                            (int)(lcp - line + 1));
                    return 1;
                }
            }
            off += (k + 1);
        }
        *mp_arr_len = off;
    } else {        
        k = strspn(inp, "0123456789aAbBcCdDeEfF, ");
        if (in_len != k) {
            fprintf(stderr, "build_diag_page: error at pos %d\n", k + 1);
            return 1;
        }
        for (k = 0; k < max_arr_len; ++k) {
            if (1 == sscanf(lcp, "%x", &h)) {
                if (h > 0xff) {
                    fprintf(stderr, "build_diag_page: hex number larger "
                            "than 0xff at pos %d\n", (int)(lcp - inp + 1));
                    return 1;
                }
                mp_arr[k] = h;
                cp = (char *)strchr(lcp, ',');
                c2p = (char *)strchr(lcp, ' ');
                if (NULL == cp)
                    cp = c2p;
                if (NULL == cp)
                    break;
                if (c2p && (c2p < cp))
                    cp = c2p;
                lcp = cp + 1;
            } else {
                fprintf(stderr, "build_diag_page: error at pos %d\n",
                        (int)(lcp - inp + 1));
                return 1;
            }
        }
        *mp_arr_len = k + 1;
        if (k == max_arr_len) {
            fprintf(stderr, "build_diag_page: array length exceeded\n");
            return 1;
        }
    }
    return 0;
}
static int build_diag_page ( const char * inp , unsigned char * mp_arr , int * mp_arr_len , int max_arr_len ) { int in_len , k , j , m ; unsigned int h ; const char * lcp ; char * cp ; char * c2p ; if ( ( NULL == inp ) || ( NULL == mp_arr ) || ( NULL == mp_arr_len ) ) return 1 ; lcp = inp ; in_len = strlen ( inp ) ; if ( 0 == in_len ) * mp_arr_len = 0 ; if ( ' ' == inp [ 0 ] ) { char line [ 512 ] ; char carry_over [ 4 ] ; int off = 0 ; int split_line ; carry_over [ 0 ] = 0 ; for ( j = 0 ; j < 512 ; ++ j ) { if ( NULL == fgets ( line , sizeof ( line ) , stdin ) ) break ; in_len = strlen ( line ) ; if ( in_len > 0 ) { if ( ' \n ' == line [ in_len - 1 ] ) { -- in_len ; line [ in_len ] = ' \0 ' ; split_line = 0 ; } else split_line = 1 ; } if ( in_len < 1 ) { carry_over [ 0 ] = 0 ; continue ; } if ( carry_over [ 0 ] ) { if ( isxdigit ( line [ 0 ] ) ) { carry_over [ 1 ] = line [ 0 ] ; carry_over [ 2 ] = ' \0 ' ; if ( 1 == sscanf ( carry_over , " " , & h ) ) mp_arr [ off - 1 ] = h ; else { fprintf ( stderr , " " " \n " , carry_over , j + 1 ) ; return 1 ; } lcp = line + 1 ; -- in_len ; } else lcp = line ; carry_over [ 0 ] = 0 ; } else lcp = line ; m = strspn ( lcp , " \t " ) ; if ( m == in_len ) continue ; lcp += m ; in_len -= m ; if ( ' ' == * lcp ) continue ; k = strspn ( lcp , " \t " ) ; if ( ( k < in_len ) && ( ' ' != lcp [ k ] ) ) { fprintf ( stderr , " " " \n " , j + 1 , m + k + 1 ) ; return 1 ; } for ( k = 0 ; k < 1024 ; ++ k ) { if ( 1 == sscanf ( lcp , " " , & h ) ) { if ( h > 0xff ) { fprintf ( stderr , " " " \n " , j + 1 , ( int ) ( lcp - line + 1 ) ) ; return 1 ; } if ( split_line && ( 1 == strlen ( lcp ) ) ) { carry_over [ 0 ] = * lcp ; } if ( ( off + k ) >= max_arr_len ) { fprintf ( stderr , " " " \n " ) ; return 1 ; } mp_arr [ off + k ] = h ; lcp = strpbrk ( lcp , " \t " ) ; if ( NULL == lcp ) break ; lcp += strspn ( lcp , " \t " ) ; if ( ' \0 ' == * lcp ) break ; } else { if ( ' ' == * lcp ) { -- k ; break ; } fprintf ( stderr , " " " \n " , j + 1 , ( int ) ( lcp - line + 1 ) ) ; return 1 ; } } off += ( k + 1 ) ; } * mp_arr_len = off ; } else { k = strspn ( inp , " " ) ; if ( in_len != k ) { fprintf ( stderr , " \n " , k + 1 ) ; return 1 ; } for ( k = 0 ; k < max_arr_len ; ++ k ) { if ( 1 == sscanf ( lcp , " " , & h ) ) { if ( h > 0xff ) { fprintf ( stderr , " " " \n " , ( int ) ( lcp - inp + 1 ) ) ; return 1 ; } mp_arr [ k ] = h ; cp = ( char * ) strchr ( lcp , ' ' ) ; c2p = ( char * ) strchr ( lcp , ' ' ) ; if ( NULL == cp ) cp = c2p ; if ( NULL == cp ) break ; if ( c2p && ( c2p < cp ) ) cp = c2p ; lcp = cp + 1 ; } else { fprintf ( stderr , " \n " , ( int ) ( lcp - inp + 1 ) ) ; return 1 ; } } * mp_arr_len = k + 1 ; if ( k == max_arr_len ) { fprintf ( stderr , " \n " ) ; return 1 ; } } return 0 ; }
Read hex numbers from command line (comma separated list) or from
stdin (one per line, comma separated list or space separated list).
Read hex numbers from command line ( comma separated list ) or from stdin ( one per line comma separated list or space separated list ) .
/* read from stdin */ /* back up and overwrite */ /* single trailing hex digit might be a split pair */ /* hex string on command line */