Exemple #1
0
static void subcommand_usage(char *cmd, char *subcmd, int status)
{
	int i, len = strlen(command_options);
	const struct sd_option *sd_opt;
	char name[64];

	printf("Usage: %s %s %s", program_name, cmd, subcmd);
	for (i = 0; i < len; i++) {
		sd_opt = find_opt(command_options[i]);
		if (sd_opt->has_arg)
			printf(" [-%c %s]", sd_opt->val, sd_opt->name);
		else
			printf(" [-%c]", sd_opt->val);
	}
	if (command_arg)
		printf(" %s", command_arg);
	printf("\nOptions:\n");
	for (i = 0; i < len; i++) {
		sd_opt = find_opt(command_options[i]);
		sprintf(name, "-%c, --%s", sd_opt->val, sd_opt->name);
		printf("  %-24s%s\n", name, sd_opt->desc);
	}

	exit(status);
}
Exemple #2
0
/* Parse a single argument with matching templates. */
static int
parse_arg(struct fuse_args* args, int *argi, const char* arg,
		struct fuse_args *outargs, void *data,
		const struct fuse_opt *opts, fuse_opt_proc_t proc, bool is_opt)
{
	int sep_idx;
	const struct fuse_opt *opt = find_opt(opts, arg, &sep_idx);

	if (opt) {
		/* An argument can match to multiple templates. Process them
		 * all. */
		for (; opt != NULL && opt->templ != NULL;
			opt = find_opt(++opt, arg, &sep_idx)) {

			if (sep_idx > 0 && opt->templ[sep_idx] == ' ' &&
				arg[sep_idx] == '\0') {
				/* The template "-x %y" requests a separate
				 * parameter "%y". Try to find one. */
				char *new_arg;
				int rv;

				if (next_arg(args, argi) == -1)
					return -1;

				/* ...but processor callbacks expect a concatenated
				 * argument "-xfoo". */
				if ((new_arg = malloc(sep_idx +
									strlen(args->argv[*argi]) + 1)) == NULL)
					return -1;

				strncpy(new_arg, arg, sep_idx); /* -x */
				strcpy(new_arg + sep_idx, args->argv[*argi]); /* foo */
				rv = parse_matched_arg(new_arg, outargs, opt, sep_idx,
									data, proc, is_opt);
				free(new_arg);

				if (rv == -1)
					return -1;
			}
			else {
				int rv;
				rv = parse_matched_arg(arg, outargs, opt, sep_idx,
									data, proc, is_opt);
				if (rv == -1)
					return -1;
			}
		}
		return 0;
	}
	else {
		/* No templates matched to it so just invoke the callback. */
		return call_proc(proc, data, arg, FUSE_OPT_KEY_OPT, outargs, is_opt);
	}
}
Exemple #3
0
void subcommand_usage(char *cmd, char *subcmd, int status)
{
	int i, n, len = strlen(command_opts);
	const struct sd_option *sd_opt;
	const struct subcommand *sub, *subsub;
	char name[64];

	printf("Usage: %s %s %s", program_name, cmd, subcmd);

	/* Show subcmd's subcommands if necessary */
	sub = find_subcmd(cmd, subcmd);
	subsub = sub->sub;
	if (subsub) {
		n = 0;
		while (subsub[n].name)
			n++;
		if (n == 1)
			printf(" %s", subsub[0].name);
		else if (n > 1) {
			printf(" {%s", subsub[0].name);
			for (i = 1; i < n; i++)
				printf("|%s", subsub[i].name);
			printf("}");
		}
	}

	for (i = 0; i < len; i++) {
		sd_opt = find_opt(command_opts[i]);
		if (sd_opt->has_arg)
			printf(" [-%c %s]", sd_opt->ch, sd_opt->name);
		else
			printf(" [-%c]", sd_opt->ch);
	}
	if (command_arg)
		printf(" %s", command_arg);

	printf("\n");
	if (subsub) {
		printf("Available subcommands:\n");
		for (i = 0; subsub[i].name; i++)
			printf("  %-24s%s\n", subsub[i].name, subsub[i].desc);

	}

	printf("Options:\n");
	for (i = 0; i < len; i++) {
		sd_opt = find_opt(command_opts[i]);
		snprintf(name, sizeof(name), "-%c, --%s",
			 sd_opt->ch, sd_opt->name);
		printf("  %-24s%s\n", name, sd_opt->desc);
	}

	exit(status);
}
Exemple #4
0
AVOption *av_set_string(void *obj, const char *name, const char *val){
    AVOption *o= find_opt(obj, name, NULL);
    if(o && o->offset==0 && o->type == FF_OPT_TYPE_CONST && o->unit){
        return set_all_opt(obj, o->unit, o->default_val);
    }
    if(!o || !val || o->offset<=0)
        return NULL;
    if(o->type != FF_OPT_TYPE_STRING){
        for(;;){
            int i;
            char buf[256];
            int cmd=0;
            double d;
            char *error = NULL;

            if(*val == '+' || *val == '-')
                cmd= *(val++);

            for(i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++)
                buf[i]= val[i];
            buf[i]=0;
            val+= i;

            d = ff_eval2(buf, const_values, const_names, NULL, NULL, NULL, NULL, NULL, &error);
            if(isnan(d)) {
                AVOption *o_named= find_opt(obj, buf, o->unit);
                if(o_named && o_named->type == FF_OPT_TYPE_CONST)
                    d= o_named->default_val;
                else if(!strcmp(buf, "default")) d= o->default_val;
                else if(!strcmp(buf, "max"    )) d= o->max;
                else if(!strcmp(buf, "min"    )) d= o->min;
                else {
                    if (!error)
                        av_log(NULL, AV_LOG_ERROR, "Unable to parse option value \"%s\": %s\n", val, error);
                    return NULL;
                }
            }
            if(o->type == FF_OPT_TYPE_FLAGS){
                if     (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d;
                else if(cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d;
            }else if(cmd=='-')
                d= -d;

            av_set_number(obj, name, d, 1, 1);
            if(!*val)
                return o;
        }
        return NULL;
    }

    memcpy(((uint8_t*)obj) + o->offset, val, sizeof(val));
    return o;
}
Exemple #5
0
//FIXME use eval.c maybe?
AVOption *av_set_string(void *obj, const char *name, const char *val){
    AVOption *o= find_opt(obj, name, NULL);
    if(o && o->offset==0 && o->type == FF_OPT_TYPE_CONST && o->unit){
        return set_all_opt(obj, o->unit, o->default_val);
    }
    if(!o || !val || o->offset<=0) 
        return NULL;
    if(o->type != FF_OPT_TYPE_STRING){
        for(;;){
            int i;
            char buf[256], *tail;
            int cmd=0;
            double d;

            if(*val == '+' || *val == '-')
                cmd= *(val++);
            
            for(i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++)
                buf[i]= val[i];
            buf[i]=0;
            val+= i;
            
            d= av_parse_num(buf, &tail);
            if(tail <= buf){
                AVOption *o_named= find_opt(obj, buf, o->unit);
                if(o_named && o_named->type == FF_OPT_TYPE_CONST) 
                    d= o_named->default_val;
                else if(!strcmp(buf, "default")) d= o->default_val;
                else if(!strcmp(buf, "max"    )) d= o->max;
                else if(!strcmp(buf, "min"    )) d= o->min;
                else return NULL;
            }
            if(o->type == FF_OPT_TYPE_FLAGS){
                if     (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d;
                else if(cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d;
            }else if(cmd=='-')
                d= -d;

            av_set_number(obj, name, d, 1, 1);
            if(!*val)
                return o;
        }
        return NULL;
    }
    
    memcpy(((uint8_t*)obj) + o->offset, val, sizeof(val));
    return o;
}
Exemple #6
0
static AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum){
    AVOption *o= find_opt(obj, name, NULL);
    void *dst;
    if(!o || o->offset<=0) 
        return NULL;
    
    if(o->max*den < num*intnum || o->min*den > num*intnum)
        return NULL;
        
    dst= ((uint8_t*)obj) + o->offset;

    switch(o->type){
    case FF_OPT_TYPE_FLAGS:   
    case FF_OPT_TYPE_INT:   *(int       *)dst= lrintf(num/den)*intnum; break;
    case FF_OPT_TYPE_INT64: *(int64_t   *)dst= lrintf(num/den)*intnum; break;
    case FF_OPT_TYPE_FLOAT: *(float     *)dst= num*intnum/den;         break;
    case FF_OPT_TYPE_DOUBLE:*(double    *)dst= num*intnum/den;         break;
    case FF_OPT_TYPE_RATIONAL:
        if((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
        else                *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
    default:
        return NULL;
    }
    return o;
}
Exemple #7
0
bool install_util::CmdlineGetOpt(LPCTSTR argv, const std::wstring& opt_name, std::wstring* opt_value)
{
  bool bRetCode = false;
  bool bResult = false;
  std::wstring find_opt(L"--");
  std::wstring inner_value;
  bool bFindFlag = false;

  YG_PROCESS_ERROR(argv);
  YG_PROCESS_ERROR(!opt_name.empty());
  YG_PROCESS_ERROR(opt_value);

  find_opt.append(opt_name + L"=");
  bRetCode = !!wcsncmp(find_opt.c_str(), argv, find_opt.length());
  YG_PROCESS_ERROR (!bRetCode);

  inner_value = argv + find_opt.length();
  bFindFlag = true;
  YG_PROCESS_ERROR(bFindFlag);

  *opt_value = inner_value;
  bResult = true;
Exit0:
  return bResult;
}
Exemple #8
0
int		exec_opt(t_option *option, int ac, char **av)
{
  int           (*funct_tab[5])(t_option *option, char **av, int *i) =
    {
      &opt_p,
      &opt_y,
      &opt_x,
      &opt_f,
      &opt_h
    };
  int		i;
  int		j;
  int		cool;
  
  i = 1;
  cool = 0;
  while (i < ac)
    {
      j = find_opt(av[i]);
      if (j >= 5) 
	print_rules(&cool);
      else
	  funct_tab[j](option, av, &i);
      ++i;
    }
  return (0);
}
Exemple #9
0
/**
 * 
 * @param buf a buffer which is used for returning non string values as strings, can be NULL
 * @param buf_len allocated length in bytes of buf
 */
const char *av_get_string(void *obj, const char *name, AVOption **o_out, char *buf, int buf_len){
    AVOption *o= find_opt(obj, name, NULL);
    void *dst;
    if(!o || o->offset<=0)
        return NULL;
    if(o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
        return NULL;

    dst= ((uint8_t*)obj) + o->offset;
    if(o_out) *o_out= o;
    
    if(o->type == FF_OPT_TYPE_STRING)
        return dst;
    
    switch(o->type){
    case FF_OPT_TYPE_FLAGS:     snprintf(buf, buf_len, "0x%08X",*(int    *)dst);break;
    case FF_OPT_TYPE_INT:       snprintf(buf, buf_len, "%d" , *(int    *)dst);break;
    case FF_OPT_TYPE_INT64:     snprintf(buf, buf_len, "%Ld", *(int64_t*)dst);break;
    case FF_OPT_TYPE_FLOAT:     snprintf(buf, buf_len, "%f" , *(float  *)dst);break;
    case FF_OPT_TYPE_DOUBLE:    snprintf(buf, buf_len, "%f" , *(double *)dst);break;
    case FF_OPT_TYPE_RATIONAL:  snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
    default: return NULL;
    }
    return buf;
}
Exemple #10
0
static int process_gopt(struct fuse_opt_context *ctx, const char *arg, int iso)
{
    unsigned sep;
    const struct fuse_opt *opt = find_opt(ctx->opt, arg, &sep);
    if (opt) {
        for (; opt; opt = find_opt(opt + 1, arg, &sep)) {
            int res;
            if (sep && opt->templ[sep] == ' ' && !arg[sep])
                res = process_opt_sep_arg(ctx, opt, sep, arg, iso);
            else
                res = process_opt(ctx, opt, sep, arg, iso);
            if (res == -1)
                return -1;
        }
        return 0;
    } else
        return call_proc(ctx, arg, FUSE_OPT_KEY_OPT, iso);
}
Exemple #11
0
static const struct sd_option *build_sd_options(const char *opts)
{
	static struct sd_option sd_opts[256], *p;
	int i, len = strlen(opts);

	p = sd_opts;
	for (i = 0; i < len; i++)
		*p++ = *find_opt(opts[i]);
	memset(p, 0, sizeof(struct sd_option));

	return sd_opts;
}
Exemple #12
0
static void tn_do_opt(struct tn_contex *ctx, unsigned char opt)
{
	struct tn_opt *m_opt = find_opt(ctx, opt);

	if (m_opt == NULL) {
		if (ctx->telwish == DO || ctx->telwish == DONT)
			tn_sendi(ctx, WONT, opt);
		if (ctx->telwish == WILL || ctx->telwish == WONT)
			tn_sendi(ctx, DONT, opt);
		return;
	}

	switch (ctx->telwish) {
	case WILL:
		if (!m_opt->sent_do) {
			if (!m_opt->can_will)
				tn_sendi(ctx, DONT, opt);
			else
				tn_sendi(ctx, DO, opt);
		}
		m_opt->sent_do = !m_opt->sent_do;
		break;
	case WONT:
		if (!m_opt->sent_do) {
			if (!m_opt->can_wont)
				tn_sendi(ctx, DO, opt);
			else
				tn_sendi(ctx, DONT, opt);
		}
		m_opt->sent_do = !m_opt->sent_do;
		break;
	case DO:
		if (!m_opt->sent_will) {
			if (!m_opt->i_do)
				tn_sendi(ctx, WONT, opt);
			else
				tn_sendi(ctx, WILL, opt);
		}
		m_opt->sent_will = !m_opt->sent_will;
		break;
	case DONT:
		if (!m_opt->sent_will) {
			if (!m_opt->i_do)
				tn_sendi(ctx, WONT, opt);
			else
				tn_sendi(ctx, WILL, opt);
		}
		m_opt->sent_will = !m_opt->sent_will;
		break;
	}
}
Exemple #13
0
static struct mntentchn *find_rw_mount(const char *device)
{
	struct mntentchn *mc;
	char *fsname = canonicalize(device);

	mc = getmntdevbackward(fsname, NULL);
	while (mc) {
		if (strcmp(mc->m.mnt_type, fstype) == 0 &&
		    find_opt(mc->m.mnt_opts, "rw", NULL) >= 0)
			break;
		mc = getmntdevbackward(fsname, mc);
	}
	my_free(fsname);
	return mc;
}
Exemple #14
0
static void update_mount_state(struct nilfs_mount_info *mi,
			       const struct mount_options *mo)
{
	pid_t pid = 0;
	pp_opt_t pp = ULONG_MAX;
	char *exopts;
	int rungc;

	rungc = (find_opt(mo->extra_opts, nogc_opt_fmt, NULL) < 0) && !(mo->flags & MS_RDONLY) && !(mo->flags & MS_BIND);

	if (!check_mtab()) {
		if (rungc)
			printf(_("%s not started\n"), NILFS_CLEANERD_NAME);
		return;
	}

	if (rungc) {
		if (find_opt(mo->extra_opts, pp_opt_fmt, &pp) < 0)
			pp = mi->protperiod;
		if (nilfs_launch_cleanerd(mi->device, mi->mntdir, pp,
					  &pid) < 0)
			error(_("%s aborted"), NILFS_CLEANERD_NAME);
		else if (verbose)
			printf(_("%s: started %s\n"), progname,
			       NILFS_CLEANERD_NAME);
	}

	my_free(mi->optstr);
	exopts = fix_extra_opts_string(mo->extra_opts, pid, pp);
	mi->optstr = fix_opts_string(((mo->flags & ~MS_NOMTAB) | MS_NETDEV),
				     exopts, NULL);

	update_mtab_entry(mi->device, mi->mntdir, fstype, mi->optstr, 0, 0,
			  !mi->mounted);
	my_free(exopts);
}
Exemple #15
0
/**
 *  Find the option descriptor and option argument (if any) for the
 *  next command line argument.  DO NOT modify the descriptor.  Put
 *  all the state in the state argument so that the option can be skipped
 *  without consequence (side effect).
 */
static tSuccess
next_opt(tOptions* pOpts, tOptState* pOptState)
{
    {
        tSuccess res = find_opt(pOpts, pOptState);
        if (! SUCCESSFUL(res))
            return res;
    }

    if (  ((pOptState->flags & OPTST_DEFINED) != 0)
       && ((pOptState->pOD->fOptState & OPTST_NO_COMMAND) != 0)) {
        fprintf(stderr, zNotCmdOpt, pOptState->pOD->pz_Name);
        return FAILURE;
    }

    pOptState->flags |= (pOptState->pOD->fOptState & OPTST_PERSISTENT_MASK);

    /*
     *  Figure out what to do about option arguments.  An argument may be
     *  required, not associated with the option, or be optional.  We detect the
     *  latter by examining for an option marker on the next possible argument.
     *  Disabled mode option selection also disables option arguments.
     */
    {
        enum { ARG_NONE, ARG_MAY, ARG_MUST } arg_type = ARG_NONE;
        tSuccess res;

        if ((pOptState->flags & OPTST_DISABLED) != 0)
            arg_type = ARG_NONE;

        else if (OPTST_GET_ARGTYPE(pOptState->flags) == OPARG_TYPE_NONE)
            arg_type = ARG_NONE;

        else if (pOptState->flags & OPTST_ARG_OPTIONAL)
            arg_type = ARG_MAY;

        else
            arg_type = ARG_MUST;

        switch (arg_type) {
        case ARG_MUST: res = next_opt_arg_must(pOpts, pOptState); break;
        case ARG_MAY:  res = next_opt_arg_may( pOpts, pOptState); break;
        case ARG_NONE: res = next_opt_arg_none(pOpts, pOptState); break;
        }

        return res;
    }
}
Exemple #16
0
char *replace_opt(char *s, const char *fmt, void *varp, const char *instead)
{
	char *ep, *sp;
	size_t oldopt_len, newopt_len, rest_len;
	int ind;

	if (!s || *s == '\0' || (ind = find_opt(s, fmt, varp)) < 0)
		return append_opt(s, instead, NULL);

	if (!instead)
		instead = "";

	/* Scan end of the option */
	sp = s + ind;
	ep = strchrnulq(sp, ',', '"');

	if (*instead == '\0') { /* Remove the option */
		if (*ep == ',') {
			/* Get rid of ',' at the end position */
			ep++;
		} else if (ind > 0) {
			/*
			 * (*ep) was a null char - get rid of ','
			 * immediately preceding the start position.
			 */
			ind--; sp--;
		}
	}

	oldopt_len = ep - sp;
	newopt_len = strlen(instead);
	rest_len = strlen(ep);

	if (newopt_len < oldopt_len) {
		/* move remaining options forward */
		memmove(sp + newopt_len, ep, rest_len + 1 /* '\0' */);
	}

	s = xrealloc(s, ind + newopt_len + rest_len + 1 /* '\0' */);
	memcpy(sp, instead, newopt_len);

	if (newopt_len > oldopt_len) {
		/* move remaining options backward */
		memmove(sp + newopt_len, ep, rest_len + 1 /* '\0' */);
	}
	return s;
}
Exemple #17
0
/**
 *  Find the option descriptor and option argument (if any) for the
 *  next command line argument.  DO NOT modify the descriptor.  Put
 *  all the state in the state argument so that the option can be skipped
 *  without consequence (side effect).
 *
 * @param opts the program option descriptor
 * @param o_st  the state of the next found option
 */
LOCAL tSuccess
next_opt(tOptions * opts, tOptState * o_st)
{
    {
        tSuccess res = find_opt(opts, o_st);
        if (! SUCCESSFUL(res))
            return res;
    }

    if (  ((o_st->flags & OPTST_DEFINED) != 0)
            && ((o_st->pOD->fOptState & OPTST_NO_COMMAND) != 0)) {
        fprintf(stderr, zNotCmdOpt, o_st->pOD->pz_Name);
        return FAILURE;
    }

    return get_opt_arg(opts, o_st);
}
Exemple #18
0
static char *build_short_options(const char *opts)
{
	static char sopts[256], *p;
	const struct sd_option *sd_opt;
	int i, len = strlen(opts);

	p = sopts;
	for (i = 0; i < len; i++) {
		sd_opt = find_opt(opts[i]);
		*p++ = sd_opt->val;
		if (sd_opt->has_arg)
			*p++ = ':';
	}
	*p = '\0';

	return sopts;
}
Exemple #19
0
/** @name process_this_arg
 * function used to process a specific arg and find of what type is it.
 * @param argc number of arguments in the command line.
 * @param argv vector of arguments.
 * @param ip integer pointing to the current option.
 * @param opt structure with possible command line options.
 */
static int process_this_arg(int argc, char **argv, int *ip,
                            cmdl_st *opt)
{
    cmdl_st *this_opt;
    int i = *ip;
    this_opt = find_opt(argv[i], opt);
    if (this_opt == 0) {   /* no option was recognized */
        if (argv[i][0] == '-') {
            printf("Unknow option %s\n", argv[i]);
            return 0;
        }
        return 1;
    }
    switch (this_opt->type) {
    case CLT_OPT:
        *((int*) this_opt->var) = 1;
        return 1;

    case CLT_INT:
        if (argc-1<=i) { /* no more input */
            fprintf(stderr, "Error: No argument for parameter %s\n", argv[i]);
            return 0;
        }
        if (!is_num(argv[i+1])) {
            printf("Argument to parameter %s is not an int.\n", argv[i]);
            return 0;
        }
        *((int*) this_opt->var) = atoi(argv[i+1]);
        *ip = i+1;
        return 1;

    case CLT_STR:
        if (argc-1<=i) { /* no more input */
            printf("No argument for parameter %s\n", argv[i]);
            return 0;
        }
        strcpy((char *) this_opt->var, argv[i+1]);
        *ip = i+1;
        return 1;

    default:
        fprintf(stderr, "invalid type for command line option\n");
    }
    return 0;
}
Exemple #20
0
bool install_util::CmdlineIs(LPCTSTR argv, const std::wstring& opt_name)
{
  bool bRetCode = false;
  bool bResult = false;
  std::wstring find_opt(L"--");
  std::wstring inner_value;

  YG_PROCESS_ERROR(argv);
  YG_PROCESS_ERROR(!opt_name.empty());

  find_opt.append(opt_name);
  bRetCode = !!wcsncmp(find_opt.c_str(), argv, find_opt.length());
  YG_PROCESS_ERROR (!bRetCode);
  YG_PROCESS_ERROR (*(argv + find_opt.length()) == L'\0');

  bResult = true;
Exit0:
  return bResult;
}
Exemple #21
0
static struct option *build_long_options(const char *opts)
{
	static struct option lopts[256], *p;
	const struct sd_option *sd_opt;
	int i, len = strlen(opts);

	p = lopts;
	for (i = 0; i < len; i++) {
		sd_opt = find_opt(opts[i]);
		p->name = sd_opt->name;
		p->has_arg = sd_opt->has_arg;
		p->flag = NULL;
		p->val = sd_opt->val;
		p++;
	}
	memset(p, 0, sizeof(struct option));

	return lopts;
}
Exemple #22
0
static int av_get_number(void *obj, const char *name, AVOption **o_out, double *num, int *den, int64_t *intnum){
    AVOption *o= find_opt(obj, name, NULL);
    void *dst;
    if(!o || o->offset<=0)
        goto error;

    dst= ((uint8_t*)obj) + o->offset;

    if(o_out) *o_out= o;

    switch(o->type){
    case FF_OPT_TYPE_FLAGS:   
    case FF_OPT_TYPE_INT:       *intnum= *(int    *)dst;return 0;
    case FF_OPT_TYPE_INT64:     *intnum= *(int64_t*)dst;return 0;
    case FF_OPT_TYPE_FLOAT:     *num=    *(float  *)dst;return 0;
    case FF_OPT_TYPE_DOUBLE:    *num=    *(double *)dst;return 0;
    case FF_OPT_TYPE_RATIONAL:  *intnum= ((AVRational*)dst)->num; 
                                *den   = ((AVRational*)dst)->den;
                                                        return 0;
    }
error:
    *den=*intnum=0;
    return -1;
}
Exemple #23
0
 // ignores gap range
 void track_local(int row, int col) const {
     if (row == -1 || col == -1) {
         return;
     }
     int min_row = row;
     int min_col = col;
     find_opt(min_row, min_col);
     if (at(min_row, min_col) == 0) {
         return;
     }
     // go right to col
     for (int j = min_col; j <= col; j++) {
         track(min_row, j) = COL_INC;
     }
     // go bottom to row
     for (int i = min_row; i <= row; i++) {
         track(i, col) = ROW_INC;
     }
     while (at(min_row, min_col) < 0) {
         go_prev(min_row, min_col);
         ASSERT_TRUE(in(min_row, min_col));
     }
     track_local(min_row, min_col);
 }
Exemple #24
0
/*
 * Returns 1 if opt was matched with any option from opts,
 * otherwise returns 0.
 */
int
fuse_opt_match(const struct fuse_opt *opts, const char *opt)
{
	return find_opt(opts, opt, NULL) != NULL ? 1 : 0;
}
Exemple #25
0
int longopt(int argc, char* argv[], const option_t* opts)
{
    if (cursor>=argc) /* no more options */
        return LONGOPT_DONE;

    /* Parse command-line string: */
    char* s = argv[cursor++];
    
    if (s[0]!='-') { /* non-option argument */
        optparam = s;
        return LONGOPT_ARG;
    }

    int index;
    if (s[rememberi]=='\0') rememberi = 1;
    size_t p = 0;
    if (s[1]=='-') { /* long option */
        p = strcspn(s, "=");
        index = find_opt(opts, 0, s, p);
    }
    else { /* short option */
        index = find_opt(opts, s[rememberi], NULL, 0);
    }
    if (index==-1) { /* option not recognized */
        if (s[1]=='-') {
            strncpy(errbuf, s, 1024);
        }
        else {
            errbuf[0] = '-';
            errbuf[1] = s[rememberi];
            errbuf[2] = '\0';
        }
        optparam = errbuf;
        rememberi = 1;
        return LONGOPT_UNKNOWN_OPT;
    }

    /* dealing option parameter */
    switch (opts[index].has_arg) {
    case LONGOPT_REQUIRE: /* require argument */
        if (get_arg(argc, argv, s, rememberi, p)!=0) {
            errindex = index;
            optparam = argv[cursor-1];
            return LONGOPT_NEED_PARAM;
        }
        break;
    case LONGOPT_OPTIONAL: /* optional argument */
        if (get_arg(argc, argv, s, rememberi, p)!=0)
            optparam = NULL;
        break;
    default: /* no argument require */
        if (s[1]!='-') {
            ++rememberi;
            if (s[rememberi]!='\0')
                --cursor;
            else
                rememberi = 1;
        }
    }
    
    return index;
}
Exemple #26
0
int fuse_opt_match(const struct fuse_opt *opts, const char *opt)
{
	unsigned dummy;
	return find_opt(opts, opt, &dummy) ? 1 : 0;
}
int     scanopt (scanopt_t *svoid, char **arg, int *optindex)
{
	char   *optname = NULL, *optarg = NULL, *pstart;
	int     namelen = 0, arglen = 0;
	int     errcode = 0, has_next;
	const optspec_t *optp;
	struct _scanopt_t *s;
	struct _aux *auxp;
	int     is_short;
	int     opt_offset = -1;

	s = (struct _scanopt_t *) svoid;

	/* Normalize return-parameters. */
	SAFE_ASSIGN (arg, NULL);
	SAFE_ASSIGN (optindex, s->index);

	if (s->index >= s->argc)
		return 0;

	/* pstart always points to the start of our current scan. */
	pstart = s->argv[s->index] + s->subscript;
	if (!pstart)
		return 0;

	if (s->subscript == 0) {

		/* test for exact match of "--" */
		if (pstart[0] == '-' && pstart[1] == '-' && !pstart[2]) {
			SAFE_ASSIGN (optindex, s->index + 1);
			INC_INDEX (s, 1);
			return 0;
		}

		/* Match an opt. */
		if (matchlongopt
		    (pstart, &optname, &namelen, &optarg, &arglen)) {

			/* it LOOKS like an opt, but is it one?! */
			if (!find_opt
			    (s, 1, optname, namelen, &errcode,
			     &opt_offset)) {
				scanopt_err (s, 0, errcode);
				return errcode;
			}
			/* We handle this below. */
			is_short = 0;

			/* Check for short opt.  */
		}
		else if (pstart[0] == '-' && pstart[1]) {
			/* Pass through to below. */
			is_short = 1;
			s->subscript++;
			pstart++;
		}

		else {
			/* It's not an option. We're done. */
			return 0;
		}
	}

	/* We have to re-check the subscript status because it
	 * may have changed above. */

	if (s->subscript != 0) {

		/* we are somewhere in a run of short opts,
		 * e.g., at the 'z' in `tar -xzf` */

		optname = pstart;
		namelen = 1;
		is_short = 1;

		if (!find_opt
		    (s, 0, pstart, namelen, &errcode, &opt_offset)) {
			scanopt_err(s, 1, errcode);
			return errcode;
		}

		optarg = pstart + 1;
		if (!*optarg) {
			optarg = NULL;
			arglen = 0;
		}
		else
			arglen = (int) strlen (optarg);
	}

	/* At this point, we have a long or short option matched at opt_offset into
	 * the s->options array (and corresponding aux array).
	 * A trailing argument is in {optarg,arglen}, if any.
	 */

	/* Look ahead in argv[] to see if there is something
	 * that we can use as an argument (if needed). */
	has_next = s->index + 1 < s->argc;

	optp = s->options + opt_offset;
	auxp = s->aux + opt_offset;

	/* case: no args allowed */
	if (auxp->flags & ARG_NONE) {
		if (optarg && !is_short) {
			scanopt_err(s, is_short, SCANOPT_ERR_ARG_NOT_ALLOWED);
			INC_INDEX (s, 1);
			return SCANOPT_ERR_ARG_NOT_ALLOWED;
		}
		else if (!optarg)
			INC_INDEX (s, 1);
		else
			s->subscript++;
		return optp->r_val;
	}

	/* case: required */
	if (auxp->flags & ARG_REQ) {
		if (!optarg && !has_next) {
			scanopt_err(s, is_short, SCANOPT_ERR_ARG_NOT_FOUND);
			return SCANOPT_ERR_ARG_NOT_FOUND;
		}

		if (!optarg) {
			/* Let the next argv element become the argument. */
			SAFE_ASSIGN (arg, s->argv[s->index + 1]);
			INC_INDEX (s, 2);
		}
		else {
			SAFE_ASSIGN (arg, (char *) optarg);
			INC_INDEX (s, 1);
		}
		return optp->r_val;
	}

	/* case: optional */
	if (auxp->flags & ARG_OPT) {
		SAFE_ASSIGN (arg, optarg);
		INC_INDEX (s, 1);
		return optp->r_val;
	}


	/* Should not reach here. */
	return 0;
}
Exemple #28
0
/*
 * An extra page replacement algorithm implementation, known as the optimal page
 * replacement algorithm. Please note, this is not a realistic algorithm for OSs
 * because we don't know what's going to be called in the future. But, here we do
 * so I'm going to take advantage of it...The idea is to find the optimal replacement
 * page by looking into the future pages to be allocated. The optimal victim frame is 
 * the one that has a page reference the farthest away in the future. If there are multiple
 * frames that don't have a reference in the future, the optimal is the first frame that
 * has this property.
 * 
 *		:param arr: an array of pages to be allocated
 *		:param arr_size: the number of pages to be allocated
 *		:param frame_nun: the number of frames in physical memory
 *		:param stats: an array which will eventually store the number of page faults
 *					  for this run of the algorithm (at index 0), and the number of
 *					  references (at index 1). These are then used in by the caller
 *					  to calculate the miss rate
 *		:param verbose: "boolean" to indicate whether to run in verbose mode or not;
 *						if so, then each allocation process is displayed.
 */
void extra(int arr[], int arr_size, int frame_num, int stats[], int verbose) {

	/* initialize frame array to -1's */
	int frames[frame_num], i;
	for (i = 0; i < frame_num; i++) {
		frames[i] = -1;
	}

	/* initialize local variables: 
	 *	- res: stores the result of whether or not the page is already in memory
	 *	- num_faults: stores the current number of page faults that have occured
	 *	- num_refs: stores the current number of page references that have occured
	 * 	- faulted: temp boolean value to determine if the current page resulted in
	 *			   a fault or not
	 *	- is_filled: boolean value to determine if the frames are filled
	 *	- count: int to keep the current count of the page we're on
	 *	- num_allocated: stores the current number of pages allocated into a frame
	 */
	int res, faulted = 0, is_filled = 0, count = 0;
	int num_faults = 0, num_refs = 0, num_allocated = 0;

	/* for each page in arr...follow optimal replacement policy */
	for (i = 0; i < arr_size; i++) {

		/* see if page is already in frames */
		res = search(frames, frame_num, arr[i]);
		faulted = 0;
		count++;

		/* if the frame is full, count towards references */
		if (is_filled || num_allocated >= frame_num) {
			is_filled = 1;
			num_refs++;
		}

		/* is page is not in frame, replacement is needed */
		if (res == -1) {

			/* if the frames aren't filled, don't look for optimal, just
			 *  put it in the next free space */
			int index;
			if (!is_filled) 
				index = num_allocated;
			else
				index = find_opt(arr, arr_size, frames, frame_num, count);

			/* replace optimal element w/ page */
			frames[index] = arr[i];

			/* increment number allocated, and set faulted to true */
			num_allocated++;
			faulted = 1;
		}

		/* if the frame is full, and the page wasn't in frame then count towards faults*/
		if (is_filled && faulted) {
			num_faults++;
		}

		/* only print current operation if verbose mode is on */
		if (verbose)
			display(frames, frame_num, arr[i], (faulted && is_filled));
	}

	/* set number of faults and references to 'return' array so miss
	 * rate can be calculated by the caller function */
	stats[0] = num_faults;
	stats[1] = num_refs;
}
Exemple #29
0
/* Handle the switch beginning at ARGV for the language indicated by
   LANG_MASK.  Returns the number of switches consumed.  */
static unsigned int
handle_option (const char **argv, unsigned int lang_mask)
{
  size_t opt_index;
  const char *opt, *arg = 0;
  char *dup = 0;
  int value = 1;
  unsigned int result = 0;
  const struct cl_option *option;

  opt = argv[0];

  /* Drop the "no-" from negative switches.  */
  if ((opt[1] == 'W' || opt[1] == 'f')
      && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
    {
      size_t len = strlen (opt) - 3;

      dup = xmalloc (len + 1);
      dup[0] = '-';
      dup[1] = opt[1];
      memcpy (dup + 2, opt + 5, len - 2 + 1);
      opt = dup;
      value = 0;
    }

  /* APPLE LOCAL mainline */
  opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
  if (opt_index == cl_options_count)
    goto done;

  option = &cl_options[opt_index];

  /* Reject negative form of switches that don't take negatives as
     unrecognized.  */
  if (!value && (option->flags & CL_REJECT_NEGATIVE))
    goto done;

  /* We've recognized this switch.  */
  result = 1;

  /* Sort out any argument the switch takes.  */
  if (option->flags & CL_JOINED)
    {
      /* Have arg point to the original switch.  This is because
	 some code, such as disable_builtin_function, expects its
	 argument to be persistent until the program exits.  */
      arg = argv[0] + cl_options[opt_index].opt_len + 1;
      if (!value)
	arg += strlen ("no-");

      if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
	{
	  if (option->flags & CL_SEPARATE)
	    {
	      arg = argv[1];
	      result = 2;
	    }
	  else
	    /* Missing argument.  */
	    arg = NULL;
	}
    }
  else if (option->flags & CL_SEPARATE)
    {
      arg = argv[1];
      result = 2;
    }

  /* Now we've swallowed any potential argument, complain if this
     is a switch for a different front end.  */
  /* APPLE LOCAL begin mainline */
  if (!(option->flags & (lang_mask | CL_COMMON | CL_TARGET)))
  /* APPLE LOCAL end mainline */
    {
      complain_wrong_lang (argv[0], option, lang_mask);
      goto done;
    }
  /* APPLE LOCAL begin iframework for 4.3 4094959 */
  else if ((option->flags & CL_TARGET)
	   && (option->flags & CL_LANG_ALL)
	   && !(option->flags & lang_mask))
    {
      /* Complain for target flag language mismatches if any languages
	 are specified.  */
      complain_wrong_lang (argv[0], option, lang_mask);
      goto done;
    }
  /* APPLE LOCAL end iframework for 4.3 4094959 */

  if (arg == NULL && (option->flags & (CL_JOINED | CL_SEPARATE)))
    {
      if (!lang_hooks.missing_argument (opt, opt_index))
	error ("missing argument to \"%s\"", opt);
      goto done;
    }

  /* If the switch takes an integer, convert it.  */
  if (arg && (option->flags & CL_UINTEGER))
    {
      value = integral_argument (arg);
      if (value == -1)
	{
	  error ("argument to \"%s\" should be a non-negative integer",
		 option->opt_text);
	  goto done;
	}
    }

  if (option->flag_var)
    {
      if (option->has_set_value)
	{
	  if (value)
	    *option->flag_var = option->set_value;
	  else
	    *option->flag_var = !option->set_value;
	}
      else
	*option->flag_var = value;
    }
  
/* APPLE LOCAL begin optimization pragmas 3124235/3420242 */
  else if (option->access_flag)
    {
      if (option->has_set_value)
	{
	  if (value)
	    (option->access_flag) (option->set_value, 1);
	  else
	    (option->access_flag) (!option->set_value, 1);
	}
      else
	(option->access_flag) (value, 1);
    }
/* APPLE LOCAL end optimization pragmas 3124235/3420242 */

  if (option->flags & lang_mask)
    if (lang_hooks.handle_option (opt_index, arg, value) == 0)
      result = 0;

  if (result && (option->flags & CL_COMMON))
    if (common_handle_option (opt_index, arg, value) == 0)
      result = 0;

 done:
  if (dup)
    free (dup);
  return result;
}
Exemple #30
0
 /** Return minimum value of matrix */
 int opt_score() const {
     int min_row = rows() - 1;
     int min_col = cols() - 1;
     find_opt(min_row, min_col);
     return at(min_row, min_col);
 }