VirtualBox

Changeset 1695 in kBuild for trunk/src/kmk/kmkbuiltin


Ignore:
Timestamp:
Aug 31, 2008 8:24:55 PM (17 years ago)
Author:
bird
Message:

append: Added the -d and -t options for writing define...endef and truncating respectively.

File:
1 edited

Legend:

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

    r1442 r1695  
    4242{
    4343    fprintf(pf,
    44             "usage: %s [-cnv] file [string ...]\n"
     44            "usage: %s [-dcntv] file [string ...]\n"
    4545            "   or: %s --version\n"
    46             "   or: %s --help\n",
     46            "   or: %s --help\n"
     47            "\n"
     48            "Options:\n"
     49            "  -d  Enclose the output in define ... endef, taking the name from\n"
     50            "      the first argument following the file name.\n"
     51            "  -c  Output the command for specified target(s). [builtin only]\n"
     52            "  -n  Insert a new line between the strings.\n"
     53            "  -t  Truncate the file instead of appending\n"
     54            "  -v  Output the value(s) for specified variable(s). [builtin only]\n"
     55            ,
    4756            g_progname, g_progname, g_progname);
    4857    return 1;
     
    5766    int i;
    5867    int fFirst;
     68    int iFile;
    5969    FILE *pFile;
    6070    int fNewLine = 0;
    61 #ifndef kmk_builtin_append
     71    int fTruncate = 0;
     72    int fDefine = 0;
    6273    int fVariables = 0;
    6374    int fCommands = 0;
    64 #endif
    6575
    6676    g_progname = argv[0];
     
    7383       &&  argv[i][0] == '-'
    7484       &&  argv[i][1] != '\0' /* '-' is a file */
    75        &&  strchr("-cnv", argv[i][1]) /* valid option char */
     85       &&  strchr("-cdntv", argv[i][1]) /* valid option char */
    7686       )
    7787    {
     
    8494                {
    8595                    case 'c':
     96                        if (fVariables)
     97                        {
     98                            errx(1, "Option '-c' clashes with '-v'.");
     99                            return usage(stderr);
     100                        }
    86101#ifndef kmk_builtin_append
    87102                        fCommands = 1;
     
    91106                        return usage(stderr);
    92107#endif
     108                    case 'd':
     109                        if (fVariables)
     110                        {
     111                            errx(1, "Option '-d' must come before '-v'!");
     112                            return usage(stderr);
     113                        }
     114                        fDefine = 1;
     115                        break;
    93116                    case 'n':
    94117                        fNewLine = 1;
    95118                        break;
     119                    case 't':
     120                        fTruncate = 1;
     121                        break;
    96122                    case 'v':
     123                        if (fCommands)
     124                        {
     125                            errx(1, "Option '-v' clashes with '-c'.");
     126                            return usage(stderr);
     127                        }
    97128#ifndef kmk_builtin_append
    98129                        fVariables = 1;
     
    120151    }
    121152
     153    if (i + fDefine >= argc)
     154    {
     155        if (i <= argc)
     156            errx(1, "missing filename!");
     157        else
     158            errx(1, "missing define name!");
     159        return usage(stderr);
     160    }
     161
    122162    /*
    123163     * Open the output file.
    124164     */
    125     if (i >= argc)
    126     {
    127         errx(1, "missing filename!");
    128         return usage(stderr);
    129     }
    130     pFile = fopen(argv[i], "a");
     165    iFile = i;
     166    pFile = fopen(argv[i], fTruncate ? "w" : "a");
    131167    if (!pFile)
    132168        return err(1, "failed to open '%s'.", argv[i]);
     169
     170    /*
     171     * Start define?
     172     */
     173    if (fDefine)
     174    {
     175        i++;
     176        fprintf(pFile, "define %s\n", argv[i]);
     177    }
    133178
    134179    /*
     
    177222    }
    178223
    179     /*
    180      * Add the newline and close the file.
    181      */
    182     if (    fputc('\n', pFile) == EOF
     224    /*
     225     * Add the newline, closing the define if needed, and close the file.
     226     */
     227    if (    (  fDefine
     228             ? fwrite(fFirst ?        "endef\n"      :        "\nendef\n",
     229                      1,
     230                      fFirst ? sizeof("endef\n") - 1 : sizeof("\nendef\n") - 1,
     231                      pFile) < sizeof("endef\n")
     232             : fputc('\n', pFile) == EOF)
    183233        ||  ferror(pFile))
    184234    {
    185235        fclose(pFile);
    186         return errx(1, "error writing to '%s'!", argv[1]);
     236        return errx(1, "error writing to '%s'!", argv[iFile]);
    187237    }
    188238    if (fclose(pFile))
    189         return err(1, "failed to fclose '%s'!", argv[1]);
     239        return err(1, "failed to fclose '%s'!", argv[iFile]);
    190240    return 0;
    191241}
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette