/**
 * @file        hv_comm_shell_getopt.c
 * @brief       shell source code.
 * @details     This file provides the following functions: \n
 *              (1) shell init and handle \n
 * @author      HiView SoC Software Team
 * @version     1.0.1
 * @date        2022-09-05
 * @copyright   Copyright(c),2022-8, Hiview Software. All rights reserved.
 * @par         History:
 * <table>
 * <tr><th>Author                        <th>Date         <th>Change Description
 * <tr><td>HiView SoC Software Team      <td>2022-08-25   <td>create
 * </table>
*/


#include "hv_comm_shell_getopt.h"
#include "hv_vos_Comm.h"

int hv_opterr = 1,     /* if error message should be printed */
    hv_optind = 1,     /* index into parent argv vector */
    hv_optopt,         /* character checked for validity */
    hv_optreset;       /* reset getopt */
char    *hv_optarg;        /* argument associated with option */

#define BADCH   (int)'?'
#define BADARG  (int)':'
#define EMSG    ""

/*
 * getopt --
 *  Parse argc/argv argument vector.
 */
int Hv_getOpt(nargc, nargv, ostr)
    int nargc;
    char * const *nargv;
    const char *ostr;
{
    char *cp;
    char *__progname = NULL;
    char *place = EMSG;     /* option letter processing */
    char *oli;              /* option letter list index */

    if (__progname == NULL) {
        if ((cp = strrchr(nargv[0], '/')) != NULL)
            __progname = cp + 1;
        else
            __progname = nargv[0];
    }
    if (hv_optreset || !*place) {      /* update scanning pointer */
        hv_optreset = 0;
        if (hv_optind >= nargc || *(place = nargv[hv_optind]) != '-') {
            place = EMSG;
            return (-1);
        }
        if (place[1] && *++place == '-') {  /* found "--" */
            ++hv_optind;
            place = EMSG;
            return (-1);
        }
    }                   /* option letter okay? */
    if ((hv_optopt = (int)*place++) == (int)':' ||
        !(oli = strchr(ostr, hv_optopt))) {
        /*
         * if the user didn't specify '-' as an option,
         * assume it means -1.
         */
        if (hv_optopt == (int)'-')
            return (-1);
        if (!*place)
            ++hv_optind;
        if (hv_opterr && *ostr != ':')
            (void)Hv_Vos_printf(
                "%s: illegal option -- %c\n", __progname, hv_optopt);
        return (BADCH);
    }
    if (*++oli != ':') {            /* don't need argument */
        hv_optarg = NULL;
        if (!*place)
            ++hv_optind;
    }
    else {                  /* need an argument */
        if (*place)         /* no white space */
            hv_optarg = place;
        else if (nargc <= ++hv_optind) {   /* no arg */
            place = EMSG;
            if (*ostr == ':')
                return (BADARG);
            if (hv_opterr)
                (void)Hv_Vos_printf(
                    "%s: option requires an argument -- %c\n",
                    __progname, hv_optopt);
            return (BADCH);
        }
        else                /* white space */
            hv_optarg = nargv[hv_optind];
        place = EMSG;
        ++hv_optind;
    }
    return (hv_optopt);            /* dump back option letter */
}
