呼叫函式GetCommandLine() { & CommandLineToArgvW() } API anytime可以取得命令列的資訊.

LPTSTR WINAPI GetCommandLine(void);

/*
 *-------------------------------------------------------------------------
 *
 * setargv --
 *
 *      Parse the Windows command line string into argc/argv. Done here
 *      because we don't trust the builtin argument parser in crt0. Windows
 *      applications are responsible for breaking their command line into
 *      arguments.
 *
 *      2N backslashes + quote -> N backslashes + begin quoted string
 *      2N + 1 backslashes + quote -> literal
 *      N backslashes + non-quote -> literal
 *      quote + quote in a quoted string -> single quote
 *      quote + quote not in quoted string -> empty string
 *      quote -> begin quoted string
 *
 * Results:
 *      Fills argcPtr with the number of arguments and argvPtr with the array
 *      of arguments.
 *
 * Side effects:
 *      Memory allocated.
 *
 *--------------------------------------------------------------------------
 */

#if defined(__CYGWIN__)
static void
setargv(
    int *argcPtr,               /* Filled with number of argument strings. */
    TCHAR ***argvPtr)           /* Filled with argument strings (malloc'd). */
{
    TCHAR *cmdLine, *p, *arg, *argSpace;
    TCHAR **argv;
    int argc, size, inquote, copy, slashes;

    cmdLine = GetCommandLine();

    /*
     * Precompute an overly pessimistic guess at the number of arguments in
     * the command line by counting non-space spans.
     */

    size = 2;
    for (p = cmdLine; *p != TEXT('\0'); p++) {
        if ((*p == TEXT(' ')) || (*p == TEXT('\t'))) {  /* INTL: ISO space. */
            size++;
            while ((*p == TEXT(' ')) || (*p == TEXT('\t'))) { /* INTL: ISO space. */
                p++;
            }
            if (*p == TEXT('\0')) {
                break;
            }
        }
    }
    argSpace = (TCHAR *) ckalloc(
            (unsigned) (size * sizeof(TCHAR *) + (_tcslen(cmdLine) * sizeof(TCHAR)) + 10));
    argv = (TCHAR **) argSpace;
    argSpace += size * sizeof(TCHAR *);
    size--;

    p = cmdLine;
    for (argc = 0; argc < size; argc++) {
        argv[argc] = arg = argSpace;
        while ((*p == TEXT(' ')) || (*p == TEXT('\t'))) {       /* INTL: ISO space. */
            p++;
        }
        if (*p == TEXT('\0')) {
            break;
        }

        inquote = 0;
        slashes = 0;
        while (1) {
            copy = 1;
            while (*p == TEXT('\\')) {
                slashes++;
                p++;
            }
            if (*p == TEXT('"')) {
                if ((slashes & 1) == 0) {
                    copy = 0;
                    if ((inquote) && (p[1] == TEXT('"'))) {
                        p++;
                        copy = 1;
                    } else {
                        inquote = !inquote;
                    }
                }
                slashes >>= 1;
            }

            while (slashes) {
                *arg = TEXT('\\');
                arg++;
                slashes--;
            }

            if ((*p == TEXT('\0')) || (!inquote &&
                    ((*p == TEXT(' ')) || (*p == TEXT('\t'))))) {       /* INTL: ISO space. */
                break;
            }
            if (copy != 0) {
                *arg = *p;
                arg++;
            }
            p++;
        }
        *arg = TEXT('\0');
        argSpace = arg + 1;
    }
    argv[argc] = NULL;

    *argcPtr = argc;
    *argvPtr = argv;
}
#endif /* __CYGWIN__ */
arrow
arrow
    全站熱搜

    Person 發表在 痞客邦 留言(0) 人氣()