void cmd_processOptions(cmdlineParser const cpP, int const argc, const char ** const argv, const char ** const errorP) { struct optionx * longopts; longopts = createLongOptsArray(cpP->optionDescArray, cpP->numOptions); if (longopts == NULL) casprintf(errorP, "Unable to get memory for longopts array"); else { int endOfOptions; unsigned int i; *errorP = NULL; /* Set up initial assumption: No options present */ for (i = 0; i < cpP->numOptions; ++i) cpP->optionDescArray[i].present = false; endOfOptions = false; /* initial value */ while (!endOfOptions && !*errorP) { int const opterr0 = 0; /* Don't let getopt_long_only() print an error message */ unsigned int longoptsIndex; const char * unrecognizedOption; const char * optarg; getopt_long_onlyx(argc, (char**) argv, "", longopts, &longoptsIndex, opterr0, &endOfOptions, &optarg, &unrecognizedOption); if (unrecognizedOption) casprintf(errorP, "Unrecognized option: '%s'", unrecognizedOption); else { if (!endOfOptions) processOption(&cpP->optionDescArray[longoptsIndex], optarg, errorP); } } if (!*errorP) extractArguments(cpP, argc, argv); free(longopts); } }
bool select_card_by_name(const char *device_name) { int err; bool opened; char *msg; close_hctl(); unplugged = FALSE; opened = FALSE; if (device_name) { err = snd_mixer_attach(mixer, device_name); if (err >= 0) opened = TRUE; else { msg = casprintf(_("Cannot open mixer device '%s'."), device_name); show_alsa_error(msg, err); free(msg); } } if (opened) { mixer_device_name = cstrdup(device_name); err = snd_mixer_load(mixer); if (err < 0) fatal_alsa_error(_("cannot load mixer controls"), err); } display_card_info(); set_view_mode(view_mode); return opened; }
static char *read_file(const char *file_name, unsigned int *file_size) { FILE *f; int err; char *buf; unsigned int allocated = 2048; unsigned int bytes_read; f = fopen(file_name, "r"); if (!f) { err = errno; buf = casprintf("Cannot open file \"%s\".", file_name); show_error(buf, err); free(buf); return NULL; } *file_size = 0; buf = NULL; do { allocated *= 2; buf = crealloc(buf, allocated); bytes_read = fread(buf + *file_size, 1, allocated - *file_size, f); *file_size += bytes_read; } while (*file_size == allocated && allocated < MAX_FILE_SIZE); fclose(f); if (*file_size > 0 && buf[*file_size - 1] != '\n' && *file_size < allocated) { buf[*file_size] = '\n'; ++*file_size; } return buf; }
static void parseOptionValue(const char * const optarg, struct optionDesc * const optionP, const char ** const errorP) { switch (optionP->type) { case OPTTYPE_FLAG: *errorP = NULL; break; case OPTTYPE_INT: case OPTTYPE_UINT: parseInt(optionP->type, optarg, &optionP->value.u, &optionP->value.i, errorP); break; case OPTTYPE_STRING: if (optarg == NULL) casprintf(errorP, "Option requires a value"); else { *errorP = NULL; optionP->value.s = strdup(optarg); } break; case OPTTYPE_BINUINT: parseBinUint(optarg, &optionP->value.llu, errorP); break; case OPTTYPE_FLOAT: parseFloat(optarg, &optionP->value.d, errorP); break; } }
static void computeUrl(const char * const urlArg, const char ** const urlP) { if (strstr(urlArg, "://") != 0) { *urlP = strdup(urlArg); } else { casprintf(urlP, "http://%s/RPC2", urlArg); } }
static void parseBinUint(const char * const optarg, uint64_t * const valueP, const char ** const errorP) { if (optarg == NULL) casprintf(errorP, "Option requires a value"); else if (strlen(optarg) == 0) casprintf(errorP, "Numeric option value is null string"); else { const char * error; interpretBinUint(optarg, valueP, &error); if (error) { casprintf(errorP, "Invalid numeric option value '%s'. %s", optarg, error); strfree(error); } } }
static void processOption(struct optionDesc * const optionP, const char * const optarg, const char ** const errorP) { const char * error; parseOptionValue(optarg, optionP, &error); if (error) casprintf(errorP, "Error in '%s' option: %s", optionP->name, error); else optionP->present = true; }
static void parseInt(enum optiontype const type, const char * const optarg, unsigned int * const valueUintP, int * const valueIntP, const char ** const errorP) { if (optarg == NULL) casprintf(errorP, "Option requires a value"); else if (strlen(optarg) == 0) casprintf(errorP, "Numeric option value is null string"); else { char * tailptr; long const longvalue = strtol(optarg, &tailptr, 10); if (*tailptr != '\0') casprintf(errorP, "Non-numeric value " "for numeric option value: '%s'", optarg); else if (errno == ERANGE || longvalue > INT_MAX) casprintf(errorP, "Numeric value out of range: %s", optarg); else { if (type == OPTTYPE_UINT) { if (longvalue < 0) casprintf(errorP, "Unsigned numeric value is " "negative: %ld", longvalue); else { *errorP = NULL; *valueUintP = (unsigned int) longvalue; } } else { *errorP = NULL; *valueIntP = (int) longvalue; } } } }
static void parseFloat(const char * const optarg, double * const valueP, const char ** const errorP) { if (optarg == NULL) casprintf(errorP, "Option requires a value"); else if (strlen(optarg) == 0) casprintf(errorP, "Numeric option value is null string"); else { char * tailptr; double const doublevalue = strtod(optarg, &tailptr); if (*tailptr != '\0') casprintf(errorP, "Non-numeric value " "for numeric option value: '%s'", optarg); else if (errno == ERANGE) casprintf(errorP, "Numeric value out of range: %s", optarg); else { *errorP = NULL; *valueP = doublevalue; } } }