Ignore:
Timestamp:
Mar 26, 2018, 10:25:56 PM (7 years ago)
Author:
bird
Message:

kmkbuiltin: funnel output thru output.c (usually via err.c).

File:
1 edited

Legend:

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

    r3159 r3192  
    4949 * Prints the usage and return 1.
    5050 */
    51 static int usage(FILE *pOut)
    52 {
    53     fprintf(pOut,
    54             "usage: md5sum [-bt] [-o list-file] file(s)\n"
    55             "   or: md5sum [-btwq] -c list-file(s)\n"
    56             "   or: md5sum [-btq] -C MD5 file\n"
    57             "\n"
    58             " -c, --check       Check MD5 and files found in the specified list file(s).\n"
    59             "                   The default is to compute MD5 sums of the specified files\n"
    60             "                   and print them to stdout in list form.\n"
    61             " -C, --check-file  This is followed by an MD5 sum and the file to check.\n"
    62             " -b, --binary      Read files in binary mode. (default)\n"
    63             " -t, --text        Read files in text mode.\n"
    64             " -m, --manifest    Output in kBuild fetch 'manifest' format.\n"
    65             " -p, --progress    Show progress indicator on large files.\n"
    66             " -o, --output      Name of the output list file. Useful with -p.\n"
    67             " -q, --status      Be quiet.\n"
    68             " -w, --warn        Ignored. Always warn, unless quiet.\n"
    69             " -h, --help        This usage info.\n"
    70             " -v, --version     Show version information and exit.\n"
    71             );
     51static int usage(PKMKBUILTINCTX pCtx, int fIsErr)
     52{
     53    kmk_builtin_ctx_printf(pCtx, fIsErr,
     54                           "usage: md5sum [-bt] [-o list-file] file(s)\n"
     55                           "   or: md5sum [-btwq] -c list-file(s)\n"
     56                           "   or: md5sum [-btq] -C MD5 file\n"
     57                           "\n"
     58                           " -c, --check       Check MD5 and files found in the specified list file(s).\n"
     59                           "                   The default is to compute MD5 sums of the specified files\n"
     60                           "                   and print them to stdout in list form.\n"
     61                           " -C, --check-file  This is followed by an MD5 sum and the file to check.\n"
     62                           " -b, --binary      Read files in binary mode. (default)\n"
     63                           " -t, --text        Read files in text mode.\n"
     64                           " -m, --manifest    Output in kBuild fetch 'manifest' format.\n"
     65                           " -p, --progress    Show progress indicator on large files.\n"
     66                           " -o, --output      Name of the output list file. Useful with -p.\n"
     67                           " -q, --status      Be quiet.\n"
     68                           " -w, --warn        Ignored. Always warn, unless quiet.\n"
     69                           " -h, --help        This usage info.\n"
     70                           " -v, --version     Show version information and exit.\n"
     71                           );
    7272    return 1;
    7373}
     
    405405 *
    406406 * @returns 0 if it matches, 1 if it doesn't or an error occurs.
     407 * @param   pCtx        The command execution context.
    407408 * @param   pszFilename The name of the file to check.
    408409 * @param   pszDigest   The MD5 digest string.
     
    411412 * @param   fProgress   Whether to show an progress indicator on large files.
    412413 */
    413 static int check_one_file(const char *pszFilename, const char *pszDigest, unsigned fText, unsigned fQuiet, unsigned fProgress)
     414static int check_one_file(PKMKBUILTINCTX pCtx, const char *pszFilename, const char *pszDigest, unsigned fText,
     415                          unsigned fQuiet, unsigned fProgress)
    414416{
    415417    unsigned char Digest[16];
     
    425427        {
    426428            if (!fQuiet)
    427                 fprintf(stdout, "%s: ", pszFilename);
     429                kmk_builtin_ctx_printf(pCtx, 0, "%s: ", pszFilename);
    428430            rc = check_md5sum(pvFile, Digest, fProgress);
    429431            close_file(pvFile);
    430432            if (!fQuiet)
    431433            {
    432                 fprintf(stdout, "%s\n", !rc ? "OK" : rc < 0 ? "FAILURE" : "ERROR");
    433                 fflush(stdout);
     434                kmk_builtin_ctx_printf(pCtx, 0, "%s\n", !rc ? "OK" : rc < 0 ? "FAILURE" : "ERROR");
    434435                if (rc > 0)
    435                     errx(1, "Error reading '%s': %s", pszFilename, strerror(rc));
     436                    errx(pCtx, 1, "Error reading '%s': %s", pszFilename, strerror(rc));
    436437            }
    437438            if (rc)
     
    441442        {
    442443            if (!fQuiet)
    443                 errx(1, "Failed to open '%s': %s", pszFilename, strerror(errno));
     444                errx(pCtx, 1, "Failed to open '%s': %s", pszFilename, strerror(errno));
    444445            rc = 1;
    445446        }
     
    447448    else
    448449    {
    449         errx(1, "Malformed MD5 digest '%s'!", pszDigest);
    450         errx(1, "                      %*s^", rc - 1, "");
     450        errx(pCtx, 1, "Malformed MD5 digest '%s'!", pszDigest);
     451        errx(pCtx, 1, "                      %*s^", rc - 1, "");
    451452        rc = 1;
    452453    }
     
    460461 *
    461462 * @returns 0 if all checks out file, 1 if one or more fails or there are read errors.
     463 * @param   pCtx            The command execution context.
    462464 * @param   pszFilename     The name of the file.
    463465 * @param   fText           The default mode, text or binary. Only used when fBinaryTextOpt is true.
    464466 * @param   fBinaryTextOpt  Whether a -b or -t option was specified and should be used.
    465467 * @param   fQuiet          Whether to be quiet.
    466  * @param   fProgress   Whether to show an progress indicator on large files.
    467  */
    468 static int check_files(const char *pszFilename, int fText, int fBinaryTextOpt, int fQuiet, unsigned fProgress)
     468 * @param   fProgress       Whether to show an progress indicator on large files.
     469 */
     470static int check_files(PKMKBUILTINCTX pCtx, const char *pszFilename, int fText, int fBinaryTextOpt,
     471                       int fQuiet, unsigned fProgress)
    469472{
    470473    int rc = 0;
     
    539542                        {
    540543                            if (!fQuiet)
    541                                 fprintf(stdout, "%s: ", pszFilename);
     544                                kmk_builtin_ctx_printf(pCtx, 0, "%s: ", pszFilename);
    542545                            rc2 = check_md5sum(pvFile, Digest, fProgress);
    543546                            close_file(pvFile);
    544547                            if (!fQuiet)
    545548                            {
    546                                 fprintf(stdout, "%s\n", !rc2 ? "OK" : rc2 < 0 ? "FAILURE" : "ERROR");
    547                                 fflush(stdout);
     549                                kmk_builtin_ctx_printf(pCtx, 0, "%s\n", !rc2 ? "OK" : rc2 < 0 ? "FAILURE" : "ERROR");
    548550                                if (rc2 > 0)
    549                                     errx(1, "Error reading '%s': %s", pszFilename, strerror(rc2));
     551                                    errx(pCtx, 1, "Error reading '%s': %s", pszFilename, strerror(rc2));
    550552                            }
    551553                            if (rc2)
     
    555557                        {
    556558                            if (!fQuiet)
    557                                 errx(1, "Failed to open '%s': %s", pszFilename, strerror(errno));
     559                                errx(pCtx, 1, "Failed to open '%s': %s", pszFilename, strerror(errno));
    558560                            rc = 1;
    559561                        }
     
    561563                    else if (!fQuiet)
    562564                    {
    563                         errx(1, "%s (%d): Ignoring malformed digest '%s' (digest)", pszFilename, iLine, pszDigest);
    564                         errx(1, "%s (%d):                            %*s^", pszFilename, iLine, rc2 - 1, "");
     565                        errx(pCtx, 1, "%s (%d): Ignoring malformed digest '%s' (digest)", pszFilename, iLine, pszDigest);
     566                        errx(pCtx, 1, "%s (%d):                            %*s^", pszFilename, iLine, rc2 - 1, "");
    565567                    }
    566568                }
    567569                else if (!fQuiet)
    568                     errx(1, "%s (%d): Ignoring malformed line!", pszFilename, iLine);
     570                    errx(pCtx, 1, "%s (%d): Ignoring malformed line!", pszFilename, iLine);
    569571            }
    570572            else if (!fQuiet)
    571                 errx(1, "%s (%d): Ignoring malformed line!", pszFilename, iLine);
     573                errx(pCtx, 1, "%s (%d): Ignoring malformed line!", pszFilename, iLine);
    572574        } /* while more lines */
    573575
     
    576578    else
    577579    {
    578         errx(1, "Failed to open '%s': %s", pszFilename, strerror(errno));
     580        errx(pCtx, 1, "Failed to open '%s': %s", pszFilename, strerror(errno));
    579581        rc = 1;
    580582    }
     
    588590 *
    589591 * @returns 0 on success, 1 on any kind of failure.
     592 * @param   pCtx            Command context.
    590593 * @param   pszFilename     The file to process.
    591594 * @param   fText           The mode to open the file in.
     
    595598 * @param   pOutput         Where to write the list. Progress is always written to stdout.
    596599 */
    597 static int md5sum_file(const char *pszFilename, unsigned fText, unsigned fQuiet, unsigned fProgress,
     600static int md5sum_file(PKMKBUILTINCTX pCtx, const char *pszFilename, unsigned fText, unsigned fQuiet, unsigned fProgress,
    598601                       unsigned fManifest, FILE *pOutput)
    599602{
     
    631634                if (pOutput)
    632635                    fprintf(pOutput, "%s %s%s\n", szDigest, fText ? "" : "*", pszFilename);
    633                 fprintf(stdout, "%s %s%s\n", szDigest, fText ? "" : "*", pszFilename);
     636                kmk_builtin_ctx_printf(pCtx, 0, "%s %s%s\n", szDigest, fText ? "" : "*", pszFilename);
    634637            }
    635638            else
     
    637640                if (pOutput)
    638641                    fprintf(pOutput, "%s_SIZE := %" KU64_PRI "\n%s_MD5  := %s\n", pszFilename, cbFile, pszFilename, szDigest);
    639                 fprintf(stdout, "%s_SIZE := %" KU64_PRI "\n%s_MD5  := %s\n", pszFilename, cbFile, pszFilename, szDigest);
     642                kmk_builtin_ctx_printf(pCtx, 0, "%s_SIZE := %" KU64_PRI "\n%s_MD5  := %s\n",
     643                                       pszFilename, cbFile, pszFilename, szDigest);
    640644            }
    641645            if (pOutput)
    642646                fflush(pOutput);
    643             fflush(stdout);
    644647        }
    645648        else
    646649        {
    647650            if (!fQuiet)
    648                 errx(1, "Failed to open '%s': %s", pszFilename, strerror(rc));
     651                errx(pCtx, 1, "Failed to open '%s': %s", pszFilename, strerror(rc));
    649652            rc = 1;
    650653        }
     
    653656    {
    654657        if (!fQuiet)
    655             errx(1, "Failed to open '%s': %s", pszFilename, strerror(errno));
     658            errx(pCtx, 1, "Failed to open '%s': %s", pszFilename, strerror(errno));
    656659        rc = 1;
    657660    }
     
    665668 * Somewhat similar to the GNU coreutil md5sum command.
    666669 */
    667 int kmk_builtin_md5sum(int argc, char **argv, char **envp)
     670int kmk_builtin_md5sum(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
    668671{
    669672    int i;
     
    679682    FILE *pOutput = NULL;
    680683
    681     g_progname = argv[0];
    682 
    683684    /*
    684685     * Print usage if no arguments.
    685686     */
    686687    if (argc <= 1)
    687         return usage(stderr);
     688        return usage(pCtx, 1);
    688689
    689690    /*
     
    751752
    752753                    case 'p':
    753                         fProgress = 1;
     754                        fProgress = 1 && isatty(fileno(stdout))
     755#ifndef KMK_BUILTIN_STANDALONE
     756                                 && (!pCtx->pOut || !pCtx->pOut->syncout)
     757#endif
     758                                  ;
    754759                        break;
    755760
     
    763768
    764769                    case 'h':
    765                         usage(stdout);
     770                        usage(pCtx, 0);
    766771                        return 0;
    767772
     
    783788                        else
    784789                        {
    785                             errx(1, "'-C' is missing the MD5 sum!");
     790                            errx(pCtx, 1, "'-C' is missing the MD5 sum!");
    786791                            return 1;
    787792                        }
     
    790795                        else
    791796                        {
    792                             errx(1, "'-C' is missing the filename!");
     797                            errx(pCtx, 1, "'-C' is missing the filename!");
    793798                            return 1;
    794799                        }
    795800
    796                         rc |= check_one_file(pszFilename, pszDigest, fText, fQuiet, fProgress && !fQuiet);
     801                        rc |= check_one_file(pCtx, pszFilename, pszDigest, fText, fQuiet, fProgress && !fQuiet);
    797802                        psz = "\0";
    798803                        break;
     
    806811                        if (fChecking)
    807812                        {
    808                             errx(1, "'-o' cannot be used with -c or -C!");
     813                            errx(pCtx, 1, "'-o' cannot be used with -c or -C!");
    809814                            return 1;
    810815                        }
     
    816821                        else
    817822                        {
    818                             errx(1, "'-o' is missing the file name!");
     823                            errx(pCtx, 1, "'-o' is missing the file name!");
    819824                            return 1;
    820825                        }
     
    825830
    826831                    default:
    827                         errx(1, "Invalid option '%c'! (%s)", *psz, argv[i]);
    828                         return usage(stderr);
     832                        errx(pCtx, 1, "Invalid option '%c'! (%s)", *psz, argv[i]);
     833                        return usage(pCtx, 1);
    829834                }
    830835            } while (*++psz);
    831836        }
    832837        else if (fChecking)
    833             rc |= check_files(argv[i], fText, fBinaryTextOpt, fQuiet, fProgress && !fQuiet);
     838            rc |= check_files(pCtx, argv[i], fText, fBinaryTextOpt, fQuiet, fProgress && !fQuiet);
    834839        else
    835840        {
     
    842847                if (!pOutput)
    843848                {
    844                     rc = err(1, "fopen(\"%s\", \"w" KMK_FOPEN_NO_INHERIT_MODE "\") failed", pszOutput);
     849                    rc = err(pCtx, 1, "fopen(\"%s\", \"w" KMK_FOPEN_NO_INHERIT_MODE "\") failed", pszOutput);
    845850                    break;
    846851                }
     
    848853            }
    849854
    850             rc |= md5sum_file(argv[i], fText, fQuiet, fProgress && !fQuiet && !fManifest, fManifest, pOutput);
     855            rc |= md5sum_file(pCtx, argv[i], fText, fQuiet, fProgress && !fQuiet && !fManifest, fManifest, pOutput);
    851856        }
    852857        i++;
     
    858863}
    859864
     865
     866#ifdef KMK_BUILTIN_STANDALONE
     867int main(int argc, char **argv, char **envp)
     868{
     869    KMKBUILTINCTX Ctx = { "kmk_md5sum", NULL };
     870    return kmk_builtin_md5sum(argc, argv, envp, &Ctx);
     871}
     872#endif
     873
     874
Note: See TracChangeset for help on using the changeset viewer.