Ticket #12562: addattr_main.cpp.patch

File addattr_main.cpp.patch, 3.6 KB (added by Pete, 8 years ago)

usability fixes for addattr (main.cpp)

  • main.

    old new  
    9090        }
    9191    }
    9292
    93     // type didn't show up - in this case, we parse the string
    94     // as number and use it directly as type code
     93    // type didn't show up - in this case, we try to parse
     94    // the string as number and use it directly as type code
    9595
    96     if (sscanf(string, "%" B_SCNu32, _result) == 1)
     96    if (sscanf(string, "%" B_SCNi32, _result) == 1)
    9797        return B_OK;
    9898
     99    // if that didn't work, try the string as a char-type-code
     100    // enclosed in single quotes
    99101    uchar type[4];
    100102    if (sscanf(string, "'%c%c%c%c'", &type[0], &type[1], &type[2], &type[3]) == 4) {
    101103        *_result = (type[0] << 24) | (type[1] << 16) | (type[2] << 8) | type[3];
     
    109111void
    110112usage(int returnValue)
    111113{
    112     fprintf(stderr, "usage: %s [-t type] [ -P ] attr value file1 [file2...]\n"
    113         "   or: %s [-f value-from-file] [-t type] [ -P ] attr file1 [file2...]\n\n"
     114    fprintf(stderr, "usage: %s [-t type|-c code] [ -P ] attr value file1 [file2...]\n"
     115        "   or: %s [-f value-from-file] [-t type|-c code] [ -P ] attr file1 [file2...]\n\n"
    114116        "\t-P : Don't resolve links\n"
    115         "\tType is one of:\n"
    116         "\t\tstring, mime, int, llong, float, double, bool, time, icon, raw\n"
    117         "\t\tor a numeric value (ie. 0x1234, 42, 'ABCD', ...)\n"
    118         "\tThe default is \"string\"\n", kProgramName, kProgramName);
     117        "\tThe '-t' and '-c' options are alternatives; use one or the other.\n"
     118        "\ttype is one of:\n"
     119        "\t\tstring, mime, int, int32, uint32, llong, int64, uint64,\n"
     120        "\t\tfloat, double, bool, icon, time, raw\n"
     121        "\t\tor a numeric value (ie. 0x1234, 42, ...),\n"
     122        "\t\tor an escape-quoted type code, eg. \\'MICN\\'\n"
     123        "\tThe default is \"string\"\n"
     124        "\tcode is a four-char type ID (eg. MICN)\n", kProgramName, kProgramName);
    119125
    120126    exit(returnValue);
    121127}
     
    126132{
    127133    fprintf(stderr, "%s: attribute type \"%s\" is not valid\n", kProgramName,
    128134        attrTypeName);
    129     fprintf(stderr, "\tTry one of: string, mime, int, llong, float, double,\n");
    130     fprintf(stderr, "\t\tbool, time, icon, raw, or a numeric value (ie. 0x1234, 42, 'ABCD'"
    131         ", ...)\n");
     135    fprintf(stderr, "\tTry one of: string, mime, int, llong, float, double,\n"
     136        "\t\tbool, icon, time, raw, or a numeric value (ie. 0x1234, 42, ...),\n"
     137        "\t\tor a quoted type code, eg.: \\'MICN\\'\n"
     138        "\t\tOr enter the actual type code with the '-c' option\n");
     139
     140    exit(1);
     141}
     142
     143
     144void
     145invalidTypeCode(const char* attrTypeName)
     146{
     147    fprintf(stderr, "%s: attribute type code \"%s\" is not valid\n", kProgramName,
     148        attrTypeName);
     149    fprintf(stderr, "\tIt must be exactly four characters\n");
    132150
    133151    exit(1);
    134152}
     
    139157{
    140158    fprintf(stderr, "%s: attribute value \"%s\" is not valid\n", kProgramName,
    141159        value);
    142     fprintf(stderr, "\tBool accepts: 0, f, false, disabled, off,\n");
    143     fprintf(stderr, "\t\t1, t, true, enabled, on\n");
     160    fprintf(stderr, "\tBool accepts: 0, f, false, disabled, off,\n"
     161        "\t\t1, t, true, enabled, on\n");
    144162
    145163    exit(1);
    146164}
     
    155173    bool resolveLinks = true;
    156174
    157175    int c;
    158     while ((c = getopt_long(argc, argv, "hf:t:P", kLongOptions, NULL)) != -1) {
     176    while ((c = getopt_long(argc, argv, "hf:t:c:P", kLongOptions, NULL)) != -1) {
    159177        switch (c) {
    160178            case 0:
    161179                break;
     
    202220                if (typeForString(optarg, &attrType) != B_OK)
    203221                    invalidAttrType(optarg);
    204222                break;
     223            case 'c':
     224                if (strlen(optarg) == 4) {
     225                    // Get the type code directly
     226                    char code[] = "'    '";
     227                    strncpy(code+1, optarg, 4);
     228                    if (typeForString(code, &attrType) == B_OK)
     229                        break;
     230                }
     231                invalidTypeCode(optarg);    // exits...
    205232            case 'P':
    206233                resolveLinks = false;
    207234                break;