Example #1
0
void
usageOptions(Options * options, char *format,...)
{
  int i;
  Option *option;
  va_list args;

  if (options->version)
    options->version(stderr);
  va_start(args, format);
  if (format) {
    vfprintf(stderr, format, args);
    fprintf(stderr, "\n");
  }
  fprintf(stderr, "%s\n", options->usage);
  for (i = 0; i < options->optionNb; i++) {
    option = &(options->options[i]);
    fprintf(stderr, "   ");
    if (option->marker)
      fprintf(stderr, "-%c ", option->marker);
    if (option->longMarker)
      fprintf(stderr, "--%s", option->longMarker);
    fprintf(stderr, ": %s", optionTypeToString(option->type));
    if (option->defaultv)
      fprintf(stderr, " (%s)", option->defaultv);

    fprintf(stderr, "\n      %s\n", option->comment);
  }
  fflush(stderr);
  va_end(args);
}
Example #2
0
vector<McrouterOptionError> McrouterOptionsBase::updateFromDict(
  const unordered_map<string, string>& new_opts) {

  vector<McrouterOptionError> errors;
  unordered_set<string> seen;

  forEach([&errors, &seen, &new_opts](const std::string& name,
                                      McrouterOptionData::Type type,
                                      const boost::any& value) {
    auto it = new_opts.find(name);
    if (it != new_opts.end()) {
      try {
        fromString(it->second, value);
      } catch (const std::exception& ex) {
        McrouterOptionError e;
        e.requestedName = name;
        e.requestedValue = it->second;
        e.errorMsg = "couldn't convert value to " + optionTypeToString(type) +
                     ". Exception: " + ex.what();
        errors.push_back(std::move(e));
      } catch (...) {
        McrouterOptionError e;
        e.requestedName = name;
        e.requestedValue = it->second;
        e.errorMsg = "couldn't convert value to " + optionTypeToString(type);
        errors.push_back(std::move(e));
      }
      seen.insert(it->first);
    }
  });

  for (const auto& kv : new_opts) {
    if (seen.find(kv.first) == seen.end()) {
      McrouterOptionError e;
      e.requestedName = kv.first;
      e.requestedValue = kv.second;
      e.errorMsg = "Unknown option name";
      errors.push_back(e);
    }
  }

  return errors;
}
Example #3
0
static XF86ConfDevicePtr
configureDeviceSection (int screennum)
{
    OptionInfoPtr p;
    int i = 0;
    parsePrologue (XF86ConfDevicePtr, XF86ConfDeviceRec)

    /* Move device info to parser structure */
    if (asprintf(&ptr->dev_identifier, "Card%d", screennum) == -1)
        ptr->dev_identifier = NULL;
    ptr->dev_chipset = DevToConfig[screennum].GDev.chipset;
    ptr->dev_busid = DevToConfig[screennum].GDev.busID;
    ptr->dev_driver = DevToConfig[screennum].GDev.driver;
    ptr->dev_ramdac = DevToConfig[screennum].GDev.ramdac;
    for (i = 0;  (i < MAXDACSPEEDS) && (i < CONF_MAXDACSPEEDS);  i++)
        ptr->dev_dacSpeeds[i] = DevToConfig[screennum].GDev.dacSpeeds[i];
    ptr->dev_videoram = DevToConfig[screennum].GDev.videoRam;
    ptr->dev_textclockfreq = DevToConfig[screennum].GDev.textClockFreq;
    ptr->dev_bios_base = DevToConfig[screennum].GDev.BiosBase;
    ptr->dev_mem_base = DevToConfig[screennum].GDev.MemBase;
    ptr->dev_io_base = DevToConfig[screennum].GDev.IOBase;
    ptr->dev_clockchip = DevToConfig[screennum].GDev.clockchip;
    for (i = 0;  (i < MAXCLOCKS) && (i < DevToConfig[screennum].GDev.numclocks);  i++)
        ptr->dev_clock[i] = DevToConfig[screennum].GDev.clock[i];
    ptr->dev_clocks = i;
    ptr->dev_chipid = DevToConfig[screennum].GDev.chipID;
    ptr->dev_chiprev = DevToConfig[screennum].GDev.chipRev;
    ptr->dev_irq = DevToConfig[screennum].GDev.irq;

    /* Make sure older drivers don't segv */
    if (DevToConfig[screennum].GDev.options) {
    	/* Fill in the available driver options for people to use */
	const char *descrip =
	    "        ### Available Driver options are:-\n"
	    "        ### Values: <i>: integer, <f>: float, "
			"<bool>: \"True\"/\"False\",\n"
	    "        ### <string>: \"String\", <freq>: \"<f> Hz/kHz/MHz\",\n"
	    "        ### <percent>: \"<f>%\"\n"
	    "        ### [arg]: arg optional\n";
	ptr->dev_comment = strdup(descrip);
	if (ptr->dev_comment) {
    	    for (p = DevToConfig[screennum].GDev.options;
		 p->name != NULL; p++) {
		char *p_e;
		const char *prefix = "        #Option     ";
		const char *middle = " \t# ";
		const char *suffix = "\n";
		const char *opttype = optionTypeToString(p->type);
		char *optname;
		int len = strlen(ptr->dev_comment) + strlen(prefix) +
			  strlen(middle) + strlen(suffix) + 1;
		
		if (asprintf(&optname, "\"%s\"", p->name) == -1)
		    break;

		len += max(20, strlen(optname));
		len += strlen(opttype);

		ptr->dev_comment = realloc(ptr->dev_comment, len);
		if (!ptr->dev_comment)
		    break;
		p_e = ptr->dev_comment + strlen(ptr->dev_comment);
		sprintf(p_e, "%s%-20s%s%s%s", prefix, optname, middle,
			opttype, suffix);
		free(optname);
	    }
    	}
    }

    return ptr;
}
Example #4
0
static Bool
iniSaveOptions (CompObject *object,
		const char *plugin)
{
    CompOption *option = NULL;
    int	       nOption = 0;
    char       *filename, *directory, *fullPath, *strVal = NULL;

    if (plugin)
    {
	CompPlugin *p;
	p = findActivePlugin (plugin);
	if (!p)
	    return FALSE;

	option = (*p->vTable->getObjectOptions) (p, object, &nOption);
    }
    else
    {
	return FALSE;
    }

    if (!option)
	return FALSE;

    if (!iniGetFilename (object, plugin, &filename))
	return FALSE;

    IniFileData *fileData;

    fileData = iniGetFileDataFromFilename (filename);
    if (!fileData || (fileData && fileData->blockWrites))
    {
	free (filename);
	return FALSE;
    }

    if (!iniGetHomeDir (&directory))
	return FALSE;

    fullPath = malloc (sizeof (char) * (strlen (filename) + strlen (directory) + 2));
    if (!fullPath)
    {
	free (filename);
	free (directory);
	return FALSE;
    }

    sprintf (fullPath, "%s/%s", directory, filename);

    FILE *optionFile = fopen (fullPath, "w");

    if (!optionFile && iniMakeDirectories ())
	optionFile = fopen (fullPath, "w");

    if (!optionFile)
    {
	compLogMessage ("ini", CompLogLevelError,
			"Failed to write to %s, check you " \
			"have the correct permissions", fullPath);
	free (filename);
	free (directory);
	free (fullPath);
	return FALSE;
    }

    fileData->blockReads = TRUE;

    Bool status, firstInList;
    while (nOption--)
    {
	status = FALSE;
	int i;

	switch (option->type)
	{
	case CompOptionTypeBool:
	case CompOptionTypeInt:
	case CompOptionTypeFloat:
	case CompOptionTypeString:
	case CompOptionTypeColor:
	case CompOptionTypeKey:
	case CompOptionTypeButton:
	case CompOptionTypeEdge:
	case CompOptionTypeBell:
	case CompOptionTypeMatch:
	    strVal = iniOptionValueToString (GET_CORE_DISPLAY (object),
					     &option->value, option->type);
		if (strVal)
		{
		    fprintf (optionFile, "%s=%s\n", option->name, strVal);
		    free (strVal);
		}
		else
		    fprintf (optionFile, "%s=\n", option->name);
		break;
	case CompOptionTypeList:
	    firstInList = TRUE;
	    switch (option->value.list.type)
	    {
	    case CompOptionTypeBool:
	    case CompOptionTypeInt:
	    case CompOptionTypeFloat:
	    case CompOptionTypeString:
	    case CompOptionTypeColor:
	    case CompOptionTypeMatch:
	    {
		int stringLen = MAX_OPTION_LENGTH * option->value.list.nValue;
		char *itemVal;

		strVal = malloc (sizeof(char) * stringLen);
		if (!strVal) {
		    fclose(optionFile);
		    free(fullPath);
		    return FALSE;
		}
		strcpy (strVal, "");
		firstInList = TRUE;

		for (i = 0; i < option->value.list.nValue; i++)
		{
		    itemVal =
			iniOptionValueToString (GET_CORE_DISPLAY (object),
						&option->value.list.value[i],
						option->value.list.type);
		    if (!firstInList)
		        strncat (strVal, ",", stringLen);
		    firstInList = FALSE;
			
		    if (itemVal)
		    {
			strncat (strVal, itemVal, stringLen);
			free (itemVal);
		    }
		}

		fprintf (optionFile, "%s=%s\n", option->name, strVal);
		free (strVal);
		break;
	    }
	    default:
		compLogMessage ("ini", CompLogLevelWarn,
				"Unknown list option type %d, %s\n",
				option->value.list.type,
				optionTypeToString (option->value.list.type));
		break;
	    }
		break;
	default:
		break;
	}

	option++;
    }

    fileData->blockReads = FALSE;

    fclose (optionFile);

    free (filename);
    free (directory);
    free (fullPath);

    return TRUE;
}
Example #5
0
File: fuse.c Project: zmike/compiz
/* MULTIDPYERROR: only works with one or less displays present */
static char *
fuseGetStringFromInode(FuseInode *inode)
{
   CompOption *option;
   char str[256];

   if (!inode->parent)
     return NULL;

   option = fuseGetOptionFromInode(inode->parent);
   if (!option)
     return NULL;

   if (inode->flags & FUSE_INODE_FLAG_TRUNC)
     return strdup("");

   if (inode->type & FUSE_INODE_TYPE_TYPE)
     {
        return strdup(optionTypeToString(option->type));
     }
   else if (inode->type & (FUSE_INODE_TYPE_VALUE | FUSE_INODE_TYPE_ITEM_VALUE))
     {
        CompOptionValue *value = NULL;
        CompOptionType type;

        if (inode->type & FUSE_INODE_TYPE_ITEM_VALUE)
          {
             int i;

             if (sscanf(inode->name, "value%d", &i))
               {
                  if (i < option->value.list.nValue)
                    {
                       value = &option->value.list.value[i];
                       type = option->value.list.type;
                    }
               }
          }
        else
          {
             value = &option->value;
             type = option->type;
          }

        if (value)
          {
             switch (type)
               {
                case CompOptionTypeBool:
                  return strdup(value->b ? "true" : "false");

                case CompOptionTypeInt:
                  snprintf(str, 256, "%d", value->i);
                  return strdup(str);

                case CompOptionTypeFloat:
                  snprintf(str, 256, "%f", value->f);
                  return strdup(str);

                case CompOptionTypeString:
                  return strdup(value->s);

                case CompOptionTypeColor:
                  return colorToString(value->c);

                case CompOptionTypeKey:
                  if (core.displays)
                    return keyActionToString(core.displays, &value->action);

                case CompOptionTypeButton:
                  if (core.displays)
                    return buttonActionToString(core.displays,
                                                &value->action);

                case CompOptionTypeEdge:
                  return edgeMaskToString(value->action.edgeMask);

                case CompOptionTypeBell:
                  return strdup(value->action.bell ? "true" : "false");

                case CompOptionTypeMatch:
                  return matchToString(&value->match);

                default:
                  break;
               }
          }
     }
   else if (inode->type & FUSE_INODE_TYPE_MIN)
     {
        if (option->type == CompOptionTypeInt)
          snprintf(str, 256, "%d", option->rest.i.min);
        else
          snprintf(str, 256, "%f", option->rest.f.min);

        return strdup(str);
     }
   else if (inode->type & FUSE_INODE_TYPE_MAX)
     {
        if (option->type == CompOptionTypeInt)
          snprintf(str, 256, "%d", option->rest.i.max);
        else
          snprintf(str, 256, "%f", option->rest.f.max);

        return strdup(str);
     }
   else if (inode->type & FUSE_INODE_TYPE_PRECISION)
     {
        snprintf(str, 256, "%f", option->rest.f.precision);
        return strdup(str);
     }
   else if (inode->type & FUSE_INODE_TYPE_ITEM_COUNT)
     {
        snprintf(str, 256, "%d", option->value.list.nValue);
        return strdup(str);
     }
   else if (inode->type & FUSE_INODE_TYPE_ITEM_TYPE)
     {
        return strdup(optionTypeToString(option->value.list.type));
     }

   return NULL;
}