static int _GetToken()

in lsvmutils/grubcfg.c [231:316]


static int _GetToken(const char** text, const char** tok)
{
    const char* p = *text;

    /* Initialzie output parameter */
    *tok = NULL;

    /* Skip over horizontal whitespace */
    while (*p && Isspace(*p) && *p != '\n')
        p++;

    /* Ignore comments */
    if (*p == '#')
    {
        while (*p && *p != '\n')
            p++;
    }

    /* Handle end-of-file */
    if (*p == '\0')
    {
        *text = p;
        return '\0';
    }

    /* Handle '\n' */
    if (*p == '\n')
    {
        *text = p + 1;
        return '\n';
    }

    /* Handle ';' */
    if (*p == ';')
    {
        *text = p + 1;
        return ';';
    }

    /* Get the next agument */
    {
        Buf buf = BUF_INITIALIZER;

        /* Build the output string */
        while (*p && !Isspace(*p) && *p != ';')
        {
            if (*p == '"' || *p == '\'')
            {
                char c = *p++;

                while (*p && *p != c)
                {
                    if (*p == '\\' && *p != '\0')
                    {
                        BufAppend(&buf, &p[1], sizeof(char));
                        p += 2;
                    }
                    else
                    {
                        BufAppend(&buf, p, sizeof(char));
                        p++;
                    }
                }

                if (*p == c)
                    p++;
            }
            else
            {
                BufAppend(&buf, p, sizeof(char));
                p++;
            }
        }

        /* Zero-terminate the string */
        {
            const char eos = '\0';
            BufAppend(&buf, &eos, sizeof(char));
        }

        *tok = (char*)buf.data;
        *text = p;
    }

    return 'I';
}