Changeset 3685 for trunk/src


Ignore:
Timestamp:
Aug 15, 2025, 2:51:28 AM (4 weeks ago)
Author:
bird
Message:

kmk_redirect: adding an exitcode translation option (-X/--translate-exitcode <from>[=<to>]).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk/kmkbuiltin/redirect.c

    r3564 r3685  
    119119                           "Usage: %s [-[rwa+tb]<fd> <file>] [-d<fd>=<src-fd>] [-c<fd>] [--stdin-pipe]\n"
    120120                           "           [-Z] [-E <var=val>] [-A <var=val>] [-P <var=val>] [-D <var>]\n"
     121                           "           [-X <from-exitcode>[=<to>]\n"
    121122                           "           [-C <dir>] [--wcc-brain-damage] [-v] -- <program> [args]\n"
    122123                           "   or: %s --help\n"
     
    153154    /*                      0         1         2         3         4         5         6         7         8 */
    154155    /*                      012345678901234567890123456789012345678901234567890123456789012345678901234567890 */
     156                           "-X <from-exitcode>[=<to>], --translate-exitcode <from-exitcode>[=<to>]\n"
     157                           "    Translates exit codes.  The '<to>' value defaults to zero.\n"
    155158                           "-C <dir>, --chdir <dir>\n"
    156159                           "    The -C switch is for changing the current directory.  Please specify an\n"
     
    164167                           "\n"
    165168                           "On OS/2 the kernel variables BEGINLIBPATH, ENDLIBPATH and LIBPATHSTRICT can be\n"
    166                            "accessed as-if they were regular enviornment variables.\n"
     169                           "accessed as-if they were regular environment variables.\n"
    167170                           "\n"
    168171                           "This command was originally just a quick hack to avoid invoking the shell\n"
     
    13771380    REDIRECTORDERS  aOrders[32];
    13781381
     1382    unsigned        cExitCodeTranslations = 0;
     1383    struct
     1384    {
     1385        int         rcExitFrom, rcExitTo;
     1386    }               aExitCodeTranslations[8];
     1387
    13791388    int             iArg;
    13801389    const char     *pszExecutable      = NULL;
     
    14081417        return err(pCtx, 9, "getcwd failed");
    14091418
    1410     /* We start out with a read-only enviornment from kmk or the crt, and will
     1419    /* We start out with a read-only environment from kmk or the crt, and will
    14111420       duplicate it if we make changes to it. */
    14121421    cAllocatedEnvVars = 0;
     
    15021511                else if (strcmp(pszArg, "stdin-pipe") == 0)
    15031512                    chOpt = 'I';
     1513                else if (strcmp(pszArg, "translate-exitcode") == 0)
     1514                    chOpt = 'X';
    15041515                else
    15051516                {
     
    15371548                || chOpt == 'c'
    15381549                || chOpt == 'd'
    1539                 || chOpt == 'e')
     1550                || chOpt == 'e'
     1551                || chOpt == 'X')
    15401552            {
    15411553                if (*pszArg != '\0')
     
    16971709
    16981710            /*
     1711             * Exit code translation: from[=to]
     1712             */
     1713            if (chOpt == 'X')
     1714            {
     1715                int   rcExitTo = 0;
     1716                char *pszEqual;
     1717                int   rcExitFrom = (int)strtol(pszValue, &pszEqual, 0);
     1718                if (pszEqual == pszValue)
     1719                    rcExit = errx(pCtx, 2, "error: failed to convert 1st part of '--translate-exitcode %s' to a number",
     1720                                  pszValue);
     1721                else if (!pszEqual || !*pszEqual)
     1722                    rcExitTo = 0;
     1723                else if (*pszEqual != '=')
     1724                    rcExit = errx(pCtx, 2, "syntax error: expected '=' to separate the two values in: '--translate-exitcode %s'",
     1725                                  pszValue);
     1726                else
     1727                {
     1728                    char *pszEnd = 0;
     1729                    rcExitTo = (int)strtol(++pszEqual, &pszEnd, 0);
     1730                    if (pszEnd == pszEqual)
     1731                        rcExit = errx(pCtx, 2, "error: failed to convert 2nd half of '--translate-exitcode %s' to a number",
     1732                                      pszValue);
     1733                    else if (pszEnd && *pszEnd != '\0')
     1734                        rcExit = errx(pCtx, 2, "error: unexpected trailing chars in: '--translate-exitcode %s'", pszValue);
     1735                }
     1736                if (cExitCodeTranslations >= K_ELEMENTS(aExitCodeTranslations))
     1737                    rcExit = errx(pCtx, 2, "error: too many exit code translations");
     1738                else if (rcExit == 0)
     1739                {
     1740                    aExitCodeTranslations[cExitCodeTranslations].rcExitFrom = rcExitFrom;
     1741                    aExitCodeTranslations[cExitCodeTranslations].rcExitTo   = rcExitTo;
     1742                    cExitCodeTranslations++;
     1743                }
     1744                continue;
     1745            }
     1746
     1747            /*
    16991748             * Okay, it is some file descriptor operation.  Make sure we've got room for it.
    17001749             */
     
    20212070#endif
    20222071                                  &fChildExitCode);
     2072
     2073        /* Do exit code translations if rcExit is a child exit code. */
     2074        if (fChildExitCode)
     2075            while (cExitCodeTranslations-- > 0)
     2076                if (aExitCodeTranslations[cExitCodeTranslations].rcExitFrom == rcExit)
     2077                {
     2078                    if (cVerbosity > 0)
     2079                        warnx(pCtx, "info: translating exit code %d to %d",
     2080                              rcExit, aExitCodeTranslations[cExitCodeTranslations].rcExitTo);
     2081                    rcExit = aExitCodeTranslations[cExitCodeTranslations].rcExitTo;
     2082                    break;
     2083                }
    20232084    }
    20242085    else if (rcExit == 0)
Note: See TracChangeset for help on using the changeset viewer.