コード例 #1
0
ファイル: setdynflag.c プロジェクト: andreiw/polaris
int
main(int argc, char **argv)
{
	ulong_t flval = 0L;
	int c;

	progname = basename(argv[0]);

	while ((c = getopt(argc, argv, "f:")) != EOF) {
		switch (c) {
		case 'f':
			if ((flval = strtoul(optarg, NULL, 0)) == 0 &&
			    (flval = parse_flag(optarg)) == 0)
				usage();
			break;
		default:
			usage();
		}
	}

	if (flval == 0 || argc - optind != 1)
		usage();

	set_flag(argv[optind], flval);

	return (0);
}
コード例 #2
0
ファイル: main.c プロジェクト: JTimothyKing/MoarVM
int main(int argc, char *argv[])
{
    MVMInstance *instance;
    const char  *input_file;

    int dump = 0;
    int argi = 1;
    int flag;

    for (; (flag = parse_flag(argv[argi])) != NOT_A_FLAG; ++argi) {
        switch (flag) {
        case FLAG_CRASH:
            MVM_crash_on_error();
            continue;

        case FLAG_DUMP:
            dump = 1;
            continue;

        case FLAG_HELP:
            puts(USAGE);
            return EXIT_SUCCESS;

#if MVM_TRACING
        case FLAG_TRACING:
            MVM_interp_enable_tracing();
            continue;
#endif

        default:
            fprintf(stderr, "ERROR: Unknown flag %s.\n\n%s\n", argv[argi], USAGE);
            return EXIT_FAILURE;
        }
    }

    if (argi >= argc) {
        fprintf(stderr, "ERROR: Missing input file.\n\n%s\n", USAGE);
        return EXIT_FAILURE;
    }

    instance   = MVM_vm_create_instance();
    input_file = argv[argi++];

    /* stash the rest of the raw command line args in the instance */
    instance->num_clargs = argc - argi;
    instance->raw_clargs = argv + argi;
    instance->prog_name  = input_file;

    if (dump) MVM_vm_dump_file(instance, input_file);
    else MVM_vm_run_file(instance, input_file);

    MVM_vm_destroy_instance(instance);

    return EXIT_SUCCESS;
}
コード例 #3
0
ファイル: mpick.c プロジェクト: Duncaen/mblaze
static struct expr *
parse_cmp()
{
	enum prop prop;
	enum op op;

	if (token("depth"))
		prop = PROP_DEPTH;
	else if (token("kept"))
		prop = PROP_KEPT;
	else if (token("index"))
		prop = PROP_INDEX;
	else if (token("replies")) {
		prop = PROP_REPLIES;
		need_thr = 1;
	} else if (token("size"))
		prop = PROP_SIZE;
	else if (token("total"))
		prop = PROP_TOTAL;
	else
		return parse_flag();

	if (!(op = parse_op()))
		parse_error("invalid comparison at '%.15s'", pos);

	int64_t n;
	if (parse_num(&n)) {
		struct expr *e = mkexpr(op);
		e->a.prop = prop;
		e->b.num = n;
		return e;
	} else if (token("cur")) {
		struct expr *e = mkexpr(op);
		e->a.prop = prop;
		e->b.var = VAR_CUR;
		e->extra = 1;
		return e;
	}

	return 0;
}
コード例 #4
0
ファイル: main.c プロジェクト: hoelzro/MoarVM
int main(int argc, char *argv[])
{
    MVMInstance *instance;
    const char  *input_file;
    const char  *executable_name = NULL;
    const char  *lib_path[8];

    int dump         = 0;
    int full_cleanup = 0;
    int argi         = 1;
    int lib_path_i   = 0;
    int flag;
    for (; (flag = parse_flag(argv[argi])) != NOT_A_FLAG; ++argi) {
        switch (flag) {
            case FLAG_CRASH:
            MVM_crash_on_error();
            continue;

            case FLAG_DUMP:
            dump = 1;
            continue;

            case FLAG_FULL_CLEANUP:
            full_cleanup = 1;
            continue;

            case FLAG_HELP:
            puts(USAGE);
            return EXIT_SUCCESS;

#if MVM_TRACING
            case FLAG_TRACING:
            MVM_interp_enable_tracing();
            continue;
#endif

            case OPT_EXECNAME:
            executable_name = argv[argi] + strlen("--execname=");
            continue;

            case OPT_LIBPATH:
            if (lib_path_i == 7) { /* 0..7 == 8 */
                fprintf(stderr, "ERROR: Only up to eight --libpath options are allowed.\n");
                return EXIT_FAILURE;
            }

            lib_path[lib_path_i++] = argv[argi] + strlen("--libpath=");
            continue;

            case FLAG_VERSION: {
            char *spesh_disable;
            char *jit_disable;

            printf("This is MoarVM version %s", MVM_VERSION);
            if (MVM_jit_support()) {
                printf(" built with JIT support");

                spesh_disable = getenv("MVM_SPESH_DISABLE");
                jit_disable = getenv("MVM_JIT_DISABLE");
                if (spesh_disable && strlen(spesh_disable) != 0) {
                    printf(" (disabled via MVM_SPESH_DISABLE)");
                } else if (jit_disable && strlen(jit_disable) != 0) {
                    printf(" (disabled via MVM_JIT_DISABLE)");
                }
            }
            printf("\n");
            return EXIT_SUCCESS;
            }

            default:
            fprintf(stderr, "ERROR: Unknown flag %s.\n\n%s\n", argv[argi], USAGE);
            return EXIT_FAILURE;
        }
    }

    lib_path[lib_path_i] = NULL;

    if (argi >= argc) {
        fprintf(stderr, "ERROR: Missing input file.\n\n%s\n", USAGE);
        return EXIT_FAILURE;
    }

    instance   = MVM_vm_create_instance();
    input_file = argv[argi++];

    /* stash the rest of the raw command line args in the instance */
    MVM_vm_set_clargs(instance, argc - argi, argv + argi);
    MVM_vm_set_prog_name(instance, input_file);
    MVM_vm_set_exec_name(instance, executable_name);
    MVM_vm_set_lib_path(instance, lib_path_i, lib_path);

    if (dump) MVM_vm_dump_file(instance, input_file);
    else MVM_vm_run_file(instance, input_file);

    if (full_cleanup) {
        MVM_vm_destroy_instance(instance);
        return EXIT_SUCCESS;
    }
    else {
        MVM_vm_exit(instance);
    }
}
コード例 #5
0
ファイル: parser-mpcmd.c プロジェクト: kax4/mpv
bool m_config_parse_mp_command_line(m_config_t *config, struct playlist *files,
                                    int argc, char **argv)
{
    int mode = 0;
    struct playlist_entry *local_start = NULL;
    bool shuffle = false;

    int local_params_count = 0;
    struct playlist_param *local_params = 0;

    assert(config != NULL);
    assert(!config->file_local_mode);

    config->mode = M_COMMAND_LINE;
    mode = GLOBAL;
#ifdef CONFIG_MACOSX_FINDER
    if (macosx_finder_args(config, files, argc, argv))
        return true;
#endif

    struct parse_state p = {config, argc, argv};
    while (split_opt(&p)) {
        if (p.mp_opt) {
            int r;
            if (mode == GLOBAL && !(p.mp_opt->flags & M_OPT_PRE_PARSE)) {
                r = m_config_set_option(config, p.arg, p.param);
            } else {
                r = m_config_check_option(config, p.arg, p.param);
            }
            if (r <= M_OPT_EXIT)
                goto err_out;
            if (r < 0) {
                char *msg = m_option_strerror(r);
                if (!msg)
                    goto print_err;
                mp_tmsg(MSGT_CFGPARSER, MSGL_FATAL,
                        "Error parsing commandline option %.*s: %s\n",
                        BSTR_P(p.arg), msg);
                goto err_out;
            }

            // Handle some special arguments outside option parser.

            if (!bstrcmp0(p.arg, "{")) {
                if (mode != GLOBAL) {
                    mp_msg(MSGT_CFGPARSER, MSGL_ERR,
                           "'--{' can not be nested.\n");
                    goto err_out;
                }
                mode = LOCAL;
                // Needed for option checking.
                m_config_enter_file_local(config);
                assert(!local_start);
                local_start = files->last;
                continue;
            }

            if (!bstrcmp0(p.arg, "}")) {
                if (mode != LOCAL) {
                    mp_msg(MSGT_CFGPARSER, MSGL_ERR,
                           "Too many closing '--}'.\n");
                    goto err_out;
                }
                if (local_params_count) {
                    // The files added between '{' and '}' are the entries from
                    // the entry _after_ local_start, until the end of the list.
                    // If local_start is NULL, the list was empty on '{', and we
                    // want all files in the list.
                    struct playlist_entry *cur
                        = local_start ? local_start->next : files->first;
                    if (!cur)
                        mp_msg(MSGT_CFGPARSER, MSGL_WARN, "Ignored options!\n");
                    while (cur) {
                        playlist_entry_add_params(cur, local_params,
                                                local_params_count);
                        cur = cur->next;
                    }
                }
                local_params_count = 0;
                mode = GLOBAL;
                m_config_leave_file_local(config);
                local_start = NULL;
                shuffle = false;
                continue;
            }

            if (bstrcmp0(p.arg, "shuffle") == 0) {
                shuffle = parse_flag(p.arg, p.param);
                continue;
            }

            if (bstrcmp0(p.arg, "playlist") == 0) {
                // append the playlist to the local args
                char *param0 = bstrdup0(NULL, p.param);
                struct playlist *pl = playlist_parse_file(param0);
                talloc_free(param0);
                if (!pl)
                    goto print_err;
                playlist_transfer_entries(files, pl);
                talloc_free(pl);
                continue;
            }

            if (mode == LOCAL) {
                MP_TARRAY_APPEND(NULL, local_params, local_params_count,
                                 (struct playlist_param) {p.arg, p.param});
            }
コード例 #6
0
ファイル: qrstat.c プロジェクト: valhallasw/son-of-gridengine
static bool 
sge_parse_qrstat(sge_gdi_ctx_class_t *ctx, lList **answer_list,
                 qrstat_env_t *qrstat_env, lList **cmdline)
{
   bool ret = true;
   
   DENTER(TOP_LAYER, "sge_parse_qrstat");

   qrstat_env->is_summary = true;
   while (lGetNumberOfElem(*cmdline)) {
      u_long32 value;
   
      /* -help */
      if (opt_list_has_X(*cmdline, "-help")) {
         sge_usage(QRSTAT, stdout);
         DEXIT;
         SGE_EXIT((void**)&ctx, 0);
      }

      /* -u */
      while (parse_multi_stringlist(cmdline, "-u", answer_list, 
                                    &(qrstat_env->user_list), ST_Type, ST_name)) {
         continue;
      }

      /* -explain */
      while (parse_flag(cmdline, "-explain", answer_list, &value)) {
         qrstat_filter_add_core_attributes(qrstat_env);
         qrstat_filter_add_explain_attributes(qrstat_env);
         qrstat_env->is_explain = (value > 0) ? true : false;
         continue;
      }

      /* -xml */
      while (parse_flag(cmdline, "-xml", answer_list, &value)) {
         qrstat_filter_add_core_attributes(qrstat_env);
         qrstat_filter_add_xml_attributes(qrstat_env);
         qrstat_env->is_xml = (value > 0) ? true : false;
         continue;
      }

      /* -ar */
      while (parse_u_longlist(cmdline, "-ar", answer_list, &(qrstat_env->ar_id_list))) {         
         qrstat_filter_add_core_attributes(qrstat_env);
         qrstat_filter_add_ar_attributes(qrstat_env);
         qrstat_filter_add_ar_where(qrstat_env);
         qrstat_env->is_summary = false;
         continue;      
      }

      if (lGetNumberOfElem(*cmdline)) {
         sge_usage(QRSTAT, stdout);
         answer_list_add(answer_list, MSG_PARSE_TOOMANYOPTIONS, 
                         STATUS_ESEMANTIC, ANSWER_QUALITY_ERROR);
         ret = false;
         break;
      }
   } 

   if (qrstat_env->is_summary) {
      char user[128] = "";
      if (sge_uid2user(geteuid(), user, sizeof(user), MAX_NIS_RETRIES)) {
         answer_list_add_sprintf(answer_list, STATUS_ESEMANTIC,
                                 ANSWER_QUALITY_CRITICAL,
                                 MSG_SYSTEM_RESOLVEUSER_U, (u_long32)geteuid());
         ret = false;
      } else {
         str_list_transform_user_list(&(qrstat_env->user_list), answer_list, user);
         qrstat_filter_add_core_attributes(qrstat_env);
         qrstat_filter_add_u_where(qrstat_env);
      }
   }

   DRETURN(ret);
}
コード例 #7
0
ファイル: h5copy.c プロジェクト: ElaraFX/hdf5
int
main (int argc, const char *argv[])
{
    hid_t        fid_src = -1;
    hid_t        fid_dst = -1;
    unsigned     flag = 0;
    unsigned     verbose = 0;
    unsigned     parents = 0;
    hid_t        ocpl_id = (-1);          /* Object copy property list */
    hid_t        lcpl_id = (-1);          /* Link creation property list */
    int          opt;
    int          li_ret;
    h5tool_link_info_t linkinfo;

    h5tools_setprogname(PROGRAMNAME);
    h5tools_setstatus(EXIT_SUCCESS);

    /* initialize h5tools lib */
    h5tools_init();

    /* init linkinfo struct */
    HDmemset(&linkinfo, 0, sizeof(h5tool_link_info_t));

    /* Check for no command line parameters */
    if(argc == 1) 
    {
        usage();
        leave(EXIT_FAILURE);
    } /* end if */

    /* parse command line options */
    while ((opt = get_option(argc, argv, s_opts, l_opts)) != EOF)
    {
        switch ((char)opt)
        {
        case 'd':
            oname_dst = HDstrdup(opt_arg);
            break;

        case 'f':
            /* validate flag */
            if (parse_flag(opt_arg,&flag)<0)
            {
                usage();
                leave(EXIT_FAILURE);
            }
            str_flag = HDstrdup(opt_arg);
            break;

        case 'h':
            usage();
            leave(EXIT_SUCCESS);
            break;

        case 'i':
            fname_src = HDstrdup(opt_arg);
            break;

        case 'o':
            fname_dst = HDstrdup(opt_arg);
            break;

        case 'p':
            parents = 1;
            break;

        case 's':
            oname_src = HDstrdup(opt_arg);
            break;

        case 'V':
            print_version(h5tools_getprogname());
            leave(EXIT_SUCCESS);
            break;

        case 'v':
            verbose = 1;
            break;

        default:
            usage();
            leave(EXIT_FAILURE);
        }
    } /* end of while */

/*-------------------------------------------------------------------------
 * check for missing file/object names
 *-------------------------------------------------------------------------*/

    if (fname_src==NULL)
    {
        error_msg("Input file name missing\n");
        usage();
        leave(EXIT_FAILURE);
    }

    if (fname_dst==NULL)
    {
        error_msg("Output file name missing\n");
        usage();
        leave(EXIT_FAILURE);
    }

    if (oname_src==NULL)
    {
        error_msg("Source object name missing\n");
        usage();
        leave(EXIT_FAILURE);
    }

    if (oname_dst==NULL)
    {
        error_msg("Destination object name missing\n");
        usage();
        leave(EXIT_FAILURE);
    }

   /*-------------------------------------------------------------------------
    * open output file
    *-------------------------------------------------------------------------*/

    /* Attempt to open an existing HDF5 file first. Need to open the dst file
       before the src file just in case that the dst and src are the same file
     */
    fid_dst = h5tools_fopen(fname_dst, H5F_ACC_RDWR, H5P_DEFAULT, NULL, NULL, 0);

   /*-------------------------------------------------------------------------
    * open input file
    *-------------------------------------------------------------------------*/

    fid_src = h5tools_fopen(fname_src, H5F_ACC_RDONLY, H5P_DEFAULT, NULL, NULL, 0);

   /*-------------------------------------------------------------------------
    * test for error in opening input file
    *-------------------------------------------------------------------------*/
    if (fid_src==-1)
    {
        error_msg("Could not open input file <%s>...Exiting\n", fname_src);
        leave(EXIT_FAILURE);
    }


   /*-------------------------------------------------------------------------
    * create an output file when failed to open it
    *-------------------------------------------------------------------------*/

    /* If we couldn't open an existing file, try creating file */
    /* (use "EXCL" instead of "TRUNC", so we don't blow away existing non-HDF5 file) */
    if(fid_dst < 0)
        fid_dst = H5Fcreate(fname_dst, H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);

   /*-------------------------------------------------------------------------
    * test for error in opening output file
    *-------------------------------------------------------------------------*/
    if (fid_dst==-1)
    {
        error_msg("Could not open output file <%s>...Exiting\n", fname_dst);
        leave(EXIT_FAILURE);
    }

   /*-------------------------------------------------------------------------
    * print some info
    *-------------------------------------------------------------------------*/

    if (verbose)
    {
        printf("Copying file <%s> and object <%s> to file <%s> and object <%s>\n",
        fname_src, oname_src, fname_dst, oname_dst);
        if (flag) {
            HDassert(str_flag);
            printf("Using %s flag\n", str_flag);
        }
    }


   /*-------------------------------------------------------------------------
    * create property lists for copy
    *-------------------------------------------------------------------------*/

    /* create property to pass copy options */
    if ( (ocpl_id = H5Pcreate(H5P_OBJECT_COPY)) < 0)
        goto error;

    /* set options for object copy */
    if (flag)
    {
        if ( H5Pset_copy_object(ocpl_id, flag) < 0)
            goto error;
    }

    /* Create link creation property list */
    if((lcpl_id = H5Pcreate(H5P_LINK_CREATE)) < 0) {
        error_msg("Could not create link creation property list\n");
        goto error;
    } /* end if */

    /* Check for creating intermediate groups */
    if(parents) {
        /* Set the intermediate group creation property */
        if(H5Pset_create_intermediate_group(lcpl_id, 1) < 0) {
            error_msg("Could not set property for creating parent groups\n");
            goto error;
        } /* end if */

        /* Display some output if requested */
        if(verbose)
            printf("%s: Creating parent groups\n", h5tools_getprogname());
    } /* end if */
    else /* error, if parent groups doesn't already exist in destination file */
    {
        size_t        i, len;

        len = HDstrlen(oname_dst);        

        /* check if all the parents groups exist. skip root group */
        for (i = 1; i < len; i++)
        {
            if ('/'==oname_dst[i])
            {
                char         *str_ptr;

                str_ptr = (char *)HDcalloc(i + 1, sizeof(char));
                HDstrncpy(str_ptr, oname_dst, i);
                str_ptr[i]='\0';
                if (H5Lexists(fid_dst, str_ptr, H5P_DEFAULT) <= 0)
                {
                    error_msg("group <%s> doesn't exist. Use -p to create parent groups.\n", str_ptr);
                    HDfree(str_ptr);
                    goto error;
                }
                HDfree(str_ptr);
            }
        }
    }

   /*-------------------------------------------------------------------------
    * do the copy
    *-------------------------------------------------------------------------*/
 
    if(verbose)
        linkinfo.opt.msg_mode = 1;
 
    li_ret = H5tools_get_symlink_info(fid_src, oname_src, &linkinfo, 1);
    if (li_ret == 0) /* dangling link */
    {
        if(H5Lcopy(fid_src, oname_src, 
                   fid_dst, oname_dst,
                   H5P_DEFAULT, H5P_DEFAULT) < 0)
            goto error;
    }
    else /* valid link */
    {
        if (H5Ocopy(fid_src,          /* Source file or group identifier */
                  oname_src,        /* Name of the source object to be copied */
                  fid_dst,          /* Destination file or group identifier  */
                  oname_dst,        /* Name of the destination object  */
                  ocpl_id,          /* Object copy property list */
                  lcpl_id)<0)       /* Link creation property list */
            goto error;
    }

    /* free link info path */
    if (linkinfo.trg_path)
        HDfree(linkinfo.trg_path);

    /* close propertis */
    if(H5Pclose(ocpl_id)<0)
        goto error;
    if(H5Pclose(lcpl_id)<0)
        goto error;

    /* close files */
    if (H5Fclose(fid_src)<0)
        goto error;
    if (H5Fclose(fid_dst)<0)
        goto error;

    leave(EXIT_SUCCESS);

error:
    printf("Error in copy...Exiting\n");

    /* free link info path */
    if (linkinfo.trg_path)
        HDfree(linkinfo.trg_path);

 H5E_BEGIN_TRY {
    H5Pclose(ocpl_id);
    H5Pclose(lcpl_id);
    H5Fclose(fid_src);
    H5Fclose(fid_dst);
 } H5E_END_TRY;

 leave(EXIT_FAILURE);
}
コード例 #8
0
ファイル: sunlabel.c プロジェクト: edgar-pek/PerspicuOS
static int
parse_label(struct sun_disklabel *sl, const char *file)
{
	char offset[32];
	char size[32];
	char flag[32];
	char tag[32];
	char buf[128];
	char text[128];
	char volname[SUN_VOLNAME_LEN + 1];
	struct sun_disklabel sl1;
	char *bp;
	const char *what;
	uint8_t part;
	FILE *fp;
	int line;
	int rv;
	int wantvtoc;
	unsigned alt, cyl, hd, nr, sec;

	line = wantvtoc = 0;
	if ((fp = fopen(file, "r")) == NULL)
		err(1, "fopen");
	sl1 = *sl;
	bzero(&sl1.sl_part, sizeof(sl1.sl_part));
	while (fgets(buf, sizeof(buf), fp) != NULL) {
		/*
		 * In order to recognize a partition entry, we search
		 * for lines starting with a single letter followed by
		 * a colon as their first non-white characters.  We
		 * silently ignore any other lines, so any comment etc.
		 * lines in the label template will be ignored.
		 *
		 * XXX We should probably also recognize the geometry
		 * fields on top, and allow changing the geometry
		 * emulated by this disk.
		 */
		for (bp = buf; isspace(*bp); bp++)
			;
		if (strncmp(bp, "text:", strlen("text:")) == 0) {
			bp += strlen("text:");
			rv = sscanf(bp,
			    " %s cyl %u alt %u hd %u sec %u",
			    text, &cyl, &alt, &hd, &sec);
			if (rv != 5) {
				warnx("%s, line %d: text label does not "
				    "contain required fields",
				    file, line + 1);
				fclose(fp);
				return (1);
			}
			if (alt != 2) {
				warnx("%s, line %d: # alt must be equal 2",
				    file, line + 1);
				fclose(fp);
				return (1);
			}
			if (cyl == 0 || cyl > USHRT_MAX) {
				what = "cyl";
				nr = cyl;
			unreasonable:
				warnx("%s, line %d: # %s %d unreasonable",
				    file, line + 1, what, nr);
				fclose(fp);
				return (1);
			}
			if (hd == 0 || hd > USHRT_MAX) {
				what = "hd";
				nr = hd;
				goto unreasonable;
			}
			if (sec == 0 || sec > USHRT_MAX) {
				what = "sec";
				nr = sec;
				goto unreasonable;
			}
			if (mediasize == 0)
				warnx("unit size unknown, no sector count "
				    "check could be done");
			else if ((uintmax_t)(cyl + alt) * sec * hd >
				 (uintmax_t)mediasize / sectorsize) {
				warnx("%s, line %d: sector count %ju exceeds "
				    "unit size %ju",
				    file, line + 1,
				    (uintmax_t)(cyl + alt) * sec * hd,
				    (uintmax_t)mediasize / sectorsize);
				fclose(fp);
				return (1);
			}
			sl1.sl_pcylinders = cyl + alt;
			sl1.sl_ncylinders = cyl;
			sl1.sl_acylinders = alt;
			sl1.sl_nsectors = sec;
			sl1.sl_ntracks = hd;
			memset(sl1.sl_text, 0, sizeof(sl1.sl_text));
			snprintf(sl1.sl_text, sizeof(sl1.sl_text),
			    "%s cyl %u alt %u hd %u sec %u",
			    text, cyl, alt, hd, sec);
			continue;
		}
		if (strncmp(bp, "volume name:", strlen("volume name:")) == 0) {
			wantvtoc = 1; /* Volume name requires VTOC. */
			bp += strlen("volume name:");
#if SUN_VOLNAME_LEN != 8
# error "scanf field width does not match SUN_VOLNAME_LEN"
#endif
			/*
			 * We set the field length to one more than
			 * SUN_VOLNAME_LEN to allow detecting an
			 * overflow.
			 */
			memset(volname, 0, sizeof volname);
			rv = sscanf(bp, " %9[^\n]", volname);
			if (rv != 1) {
				/* Clear the volume name. */
				memset(sl1.sl_vtoc_volname, 0,
				    SUN_VOLNAME_LEN);
			} else {
				memcpy(sl1.sl_vtoc_volname, volname,
				    SUN_VOLNAME_LEN);
				if (volname[SUN_VOLNAME_LEN] != '\0')
					warnx(
"%s, line %d: volume name longer than %d characters, truncating",
					    file, line + 1, SUN_VOLNAME_LEN);
			}
			continue;
		}
		if (strlen(bp) < 2 || bp[1] != ':') {
			line++;
			continue;
		}
		rv = sscanf(bp, "%c: %30s %30s %30s %30s",
		    &part, size, offset, tag, flag);
		if (rv < 3) {
		syntaxerr:
			warnx("%s: syntax error on line %d",
			    file, line + 1);
			fclose(fp);
			return (1);
		}
		if (parse_size(&sl1, part - 'a', size) ||
		    parse_offset(&sl1, part - 'a', offset))
			goto syntaxerr;
		if (rv > 3) {
			wantvtoc = 1;
			if (rv == 5 && parse_flag(&sl1, part - 'a', flag))
				goto syntaxerr;
			if (parse_tag(&sl1, part - 'a', tag))
				goto syntaxerr;
		}
		line++;
	}
	fclose(fp);
	if (wantvtoc) {
		sl1.sl_vtoc_sane = SUN_VTOC_SANE;
		sl1.sl_vtoc_vers = SUN_VTOC_VERSION;
		sl1.sl_vtoc_nparts = SUN_NPART;
	} else {
		sl1.sl_vtoc_sane = 0;
		sl1.sl_vtoc_vers = 0;
		sl1.sl_vtoc_nparts = 0;
		bzero(&sl1.sl_vtoc_map, sizeof(sl1.sl_vtoc_map));
	}
	*sl = sl1;
	return (check_label(sl));
}
コード例 #9
0
ファイル: wgdb.c プロジェクト: springgod/whitedb
int main(int argc, char **argv) {

  char *shmname = NULL;
  void *shmptr = NULL;
  int i, scan_to;
  gint shmsize;
  wg_int rlock = 0;
  wg_int wlock = 0;

  /* look for commands in argv[1] or argv[2] */
  if(argc < 3) scan_to = argc;
  else scan_to = 3;
  shmsize = 0; /* 0 size causes default size to be used */

  /* 1st loop through, shmname is NULL for default. If
   * the first argument is not a recognizable command, it
   * is assumed to be the shmname and the next argument
   * is checked against known commands.
   */
  for(i=1; i<scan_to; i++) {
    if (!strcmp(argv[i],"help") || !strcmp(argv[i],"-h")) {
      usage(argv[0]);
      exit(0);
    }
    if (!strcmp(argv[i],"version") || !strcmp(argv[i],"-v")) {
      wg_print_code_version();
      exit(0);
    }
    if (!strcmp(argv[i],"free")) {
      /* free shared memory */
      wg_delete_database(shmname);
      exit(0);
    }
    if(argc>(i+1) && !strcmp(argv[i],"import")){
      wg_int err, minsize, maxsize;
      int flags = 0;

      if(argv[i+1][0] == '-') {
        flags = parse_flag(argv[++i]);
        if(argc<=(i+1)) {
          /* Filename argument missing */
          usage(argv[0]);
          exit(1);
        }
      }

      err = wg_check_dump(NULL, argv[i+1], &minsize, &maxsize);
      if(err) {
        fprintf(stderr, "Import failed.\n");
        break;
      }

      shmptr=wg_attach_memsegment(shmname, minsize, maxsize, 1,
        (flags & FLAGS_LOGGING));
      if(!shmptr) {
        fprintf(stderr, "Failed to attach to database.\n");
        exit(1);
      }

      /* Locking is handled internally by the dbdump.c functions */
      err = wg_import_dump(shmptr,argv[i+1]);
      if(!err)
        printf("Database imported.\n");
      else if(err<-1)
        fprintf(stderr, "Fatal error in wg_import_dump, db may have"\
          " become corrupt\n");
      else
        fprintf(stderr, "Import failed.\n");
      break;
    }
    else if(argc>(i+1) && !strcmp(argv[i],"export")){
      wg_int err;
      int flags = 0;

      if(argv[i+1][0] == '-') {
        flags = parse_flag(argv[++i]);
        if(argc<=(i+1)) {
          /* Filename argument missing */
          usage(argv[0]);
          exit(1);
        }
      }

      shmptr=wg_attach_database(shmname, shmsize);
      if(!shmptr) {
        fprintf(stderr, "Failed to attach to database.\n");
        exit(1);
      }

      /* Locking is handled internally by the dbdump.c functions */
      if(flags & FLAGS_FORCE)
        err = wg_dump_internal(shmptr,argv[i+1], 0);
      else
        err = wg_dump(shmptr,argv[i+1]);

      if(err<-1)
        fprintf(stderr, "Fatal error in wg_dump, db may have"\
          " become corrupt\n");
      else if(err)
        fprintf(stderr, "Export failed.\n");
      break;
    }
#ifdef USE_DBLOG
    else if(argc>(i+1) && !strcmp(argv[i],"replay")){
      wg_int err;

      shmptr=wg_attach_database(shmname, shmsize);
      if(!shmptr) {
        fprintf(stderr, "Failed to attach to database.\n");
        exit(1);
      }

      WLOCK(shmptr, wlock);
      err = wg_replay_log(shmptr,argv[i+1]);
      WULOCK(shmptr, wlock);
      if(!err)
        printf("Log suggessfully imported from file.\n");
      else if(err<-1)
        fprintf(stderr, "Fatal error when importing, database may have "\
          "become corrupt\n");
      else
        fprintf(stderr, "Failed to import log (database unmodified).\n");
      break;
    }
#endif
    else if(argc>(i+1) && !strcmp(argv[i],"exportcsv")){
      shmptr=wg_attach_database(shmname, shmsize);
      if(!shmptr) {
        fprintf(stderr, "Failed to attach to database.\n");
        exit(1);
      }

      RLOCK(shmptr, wlock);
      wg_export_db_csv(shmptr,argv[i+1]);
      RULOCK(shmptr, wlock);
      break;
    }
    else if(argc>(i+1) && !strcmp(argv[i],"importcsv")){
      wg_int err;

      shmptr=wg_attach_database(shmname, shmsize);
      if(!shmptr) {
        fprintf(stderr, "Failed to attach to database.\n");
        exit(1);
      }

      WLOCK(shmptr, wlock);
      err = wg_import_db_csv(shmptr,argv[i+1]);
      WULOCK(shmptr, wlock);
      if(!err)
        printf("Data imported from file.\n");
      else if(err<-1)
        fprintf(stderr, "Fatal error when importing, data may be partially"\
          " imported\n");
      else
        fprintf(stderr, "Import failed.\n");
      break;
    }

#ifdef USE_REASONER
    else if(argc>(i+1) && !strcmp(argv[i],"importprolog")){
      wg_int err;

      shmptr=wg_attach_database(shmname, shmsize);
      if(!shmptr) {
        fprintf(stderr, "Failed to attach to database.\n");
        exit(1);
      }
      err = wg_import_prolog_file(shmptr,argv[i+1]);
      if(!err)
        printf("Data imported from prolog file.\n");
      else if(err<-1)
        fprintf(stderr, "Fatal error when importing, data may be partially"\
          " imported\n");
      else
        fprintf(stderr, "Import failed.\n");
      break;
    }
    else if(argc>(i+1) && !strcmp(argv[i],"importotter")){
      wg_int err;

      shmptr=wg_attach_database(shmname, shmsize);
      if(!shmptr) {
        fprintf(stderr, "Failed to attach to database.\n");
        exit(1);
      }
      err = wg_import_otter_file(shmptr,argv[i+1]);
      if(!err)
        printf("Data imported from otter file.\n");
      else if(err<-1)
        fprintf(stderr, "Fatal error when importing otter file, data may be partially"\
          " imported\n");
      else
        fprintf(stderr, "Import failed.\n");
      break;
    }
    else if(argc>i && !strcmp(argv[i],"runreasoner")){
      wg_int err;

      shmptr=wg_attach_database(shmname, shmsize);
      if(!shmptr) {
        fprintf(stderr, "Failed to attach to database.\n");
        exit(1);
      }
      //printf("about to call wg_run_reasoner\n");
      err = wg_run_reasoner(shmptr,argc,argv);
      //if(!err);
        //printf("wg_run_reasoner finished ok.\n");
      //else
        //fprintf(stderr, "wg_run_reasoner finished with an error %d.\n",err);
      //break;
      break;
    }
    else if(argc>i && !strcmp(argv[i],"testreasoner")){
      wg_int err;
      //printf("about to call wg_test_reasoner\n");
      err = wg_test_reasoner(argc,argv);
      //if(!err);
        //printf("wg_test_reasoner finished ok.\n");
      //else
        //fprintf(stderr, "wg_test_reasoner finished with an error %d.\n",err);
      //break;
      break;
    }

#endif

#ifdef HAVE_RAPTOR
    else if(argc>(i+2) && !strcmp(argv[i],"exportrdf")){
      wg_int err;
      int pref_fields = atol(argv[i+1]);

      shmptr=wg_attach_database(shmname, shmsize);
      if(!shmptr) {
        fprintf(stderr, "Failed to attach to database.\n");
        exit(1);
      }

      printf("Exporting with %d prefix fields.\n", pref_fields);
      RLOCK(shmptr, wlock);
      err = wg_export_raptor_rdfxml_file(shmptr, pref_fields, argv[i+2]);
      RULOCK(shmptr, wlock);
      if(err)
        fprintf(stderr, "Export failed.\n");
      break;
    }
    else if(argc>(i+3) && !strcmp(argv[i],"importrdf")){
      wg_int err;
      int pref_fields = atol(argv[i+1]);
      int suff_fields = atol(argv[i+2]);

      shmptr=wg_attach_database(shmname, shmsize);
      if(!shmptr) {
        fprintf(stderr, "Failed to attach to database.\n");
        exit(1);
      }

      printf("Importing with %d prefix fields, %d suffix fields.\n,",
        pref_fields, suff_fields);
      WLOCK(shmptr, wlock);
      err = wg_import_raptor_file(shmptr, pref_fields, suff_fields,
        wg_rdfparse_default_callback, argv[i+3]);
      WULOCK(shmptr, wlock);
      if(!err)
        printf("Data imported from file.\n");
      else if(err<-1)
        fprintf(stderr, "Fatal error when importing, data may be partially"\
          " imported\n");
      else
        fprintf(stderr, "Import failed.\n");
      break;
    }
#endif
    else if(!strcmp(argv[i],"test")) {
      /* This test function does it's own memory allocation. */
      wg_run_tests(WG_TEST_QUICK, 2);
      break;
    }
    else if(!strcmp(argv[i],"fulltest")) {
      wg_run_tests(WG_TEST_FULL, 2);
      break;
    }
    else if(!strcmp(argv[i], "header")) {
      shmptr=wg_attach_database(shmname, shmsize);
      if(!shmptr) {
        fprintf(stderr, "Failed to attach to database.\n");
        exit(1);
      }
      RLOCK(shmptr, wlock);
      wg_show_db_memsegment_header(shmptr);
      RULOCK(shmptr, wlock);
      break;
    }
#ifdef _WIN32
    else if(!strcmp(argv[i],"server")) {
      int flags = 0;
      if(argc>(i+1) && argv[i+1][0] == '-') {
        flags = parse_flag(argv[++i]);
      }

      if(argc>(i+1)) {
        shmsize = parse_shmsize(argv[i+1]);
        if(!shmsize)
          fprintf(stderr, "Failed to parse memory size, using default.\n");
      }
      shmptr=wg_attach_memsegment(shmname, shmsize, shmsize, 1,
        (flags & FLAGS_LOGGING));
      if(!shmptr) {
        fprintf(stderr, "Failed to attach to database.\n");
        exit(1);
      }
      printf("Press Ctrl-C to end and release the memory.\n");
      while(_getch() != 3);
      break;
    }
#else
    else if(!strcmp(argv[i],"create")) {
      int flags = 0;
      if(argc>(i+1) && argv[i+1][0] == '-') {
        flags = parse_flag(argv[++i]);
      }

      if(argc>(i+1)) {
        shmsize = parse_shmsize(argv[i+1]);
        if(!shmsize)
          fprintf(stderr, "Failed to parse memory size, using default.\n");
      }
      shmptr=wg_attach_memsegment(shmname, shmsize, shmsize, 1,
        (flags & FLAGS_LOGGING));
      if(!shmptr) {
        fprintf(stderr, "Failed to attach to database.\n");
        exit(1);
      }
      break;
    }
#endif
    else if(argc>(i+1) && !strcmp(argv[i], "fill")) {
      int rows = atol(argv[i+1]);
      if(!rows) {
        fprintf(stderr, "Invalid number of rows.\n");
        exit(1);
      }

      shmptr=wg_attach_database(shmname, shmsize);
      if(!shmptr) {
        fprintf(stderr, "Failed to attach to database.\n");
        exit(1);
      }

      WLOCK(shmptr, wlock);
      if(argc > (i+2) && !strcmp(argv[i+2], "mix"))
        wg_genintdata_mix(shmptr, rows, TESTREC_SIZE);
      else if(argc > (i+2) && !strcmp(argv[i+2], "desc"))
        wg_genintdata_desc(shmptr, rows, TESTREC_SIZE);
      else
        wg_genintdata_asc(shmptr, rows, TESTREC_SIZE);
      WULOCK(shmptr, wlock);
      printf("Data inserted\n");
      break;
    }
    else if(argc>(i+1) && !strcmp(argv[i],"select")) {
      int rows = atol(argv[i+1]);
      int from = 0;

      if(!rows) {
        fprintf(stderr, "Invalid number of rows.\n");
        exit(1);
      }
      if(argc > (i+2))
        from = atol(argv[i+2]);

      shmptr=wg_attach_database(shmname, shmsize);
      if(!shmptr) {
        fprintf(stderr, "Failed to attach to database.\n");
        exit(1);
      }
      RLOCK(shmptr, wlock);
      selectdata(shmptr, rows, from);
      RULOCK(shmptr, wlock);
      break;
    }
    else if(argc>(i+1) && !strcmp(argv[i],"add")) {
      int err;
      shmptr=wg_attach_database(shmname, shmsize);
      if(!shmptr) {
        fprintf(stderr, "Failed to attach to database.\n");
        exit(1);
      }
      WLOCK(shmptr, wlock);
      err = add_row(shmptr, argv+i+1, argc-i-1);
      WULOCK(shmptr, wlock);
      if(!err)
        printf("Row added.\n");
      break;
    }
    else if(argc>(i+2) && !strcmp(argv[i],"del")) {
      shmptr=wg_attach_database(shmname, shmsize);
      if(!shmptr) {
        fprintf(stderr, "Failed to attach to database.\n");
        exit(1);
      }
      /* Delete works like query(), except deletes the matching rows */
      del(shmptr, argv+i+1, argc-i-1);
      break;
      break;
    }
    else if(argc>(i+3) && !strcmp(argv[i],"query")) {
      shmptr=wg_attach_database(shmname, shmsize);
      if(!shmptr) {
        fprintf(stderr, "Failed to attach to database.\n");
        exit(1);
      }
      /* Query handles it's own locking */
      query(shmptr, argv+i+1, argc-i-1);
      break;
    }
    else if(argc>i && !strcmp(argv[i],"addjson")){
      wg_int err;

      shmptr=wg_attach_database(shmname, shmsize);
      if(!shmptr) {
        fprintf(stderr, "Failed to attach to database.\n");
        exit(1);
      }

      WLOCK(shmptr, wlock);
      /* the filename parameter is optional */
      err = wg_parse_json_file(shmptr, (argc>(i+1) ? argv[i+1] : NULL));
      WULOCK(shmptr, wlock);
      if(!err)
        printf("JSON document imported.\n");
      else if(err<-1)
        fprintf(stderr, "Fatal error when importing, data may be partially"\
          " imported\n");
      else
        fprintf(stderr, "Import failed.\n");
      break;
    }
    else if(argc>(i+1) && !strcmp(argv[i],"findjson")) {
      shmptr=wg_attach_database(shmname, shmsize);
      if(!shmptr) {
        fprintf(stderr, "Failed to attach to database.\n");
        exit(1);
      }
      WLOCK(shmptr, wlock);
      findjson(shmptr, argv[i+1]);
      WULOCK(shmptr, wlock);
      break;
    }

    shmname = argv[1]; /* no match, assume shmname was given */
  }

  if(i==scan_to) {
    /* loop completed normally ==> no commands found */
    usage(argv[0]);
  }
  if(shmptr) {
    RULOCK(shmptr, rlock);
    WULOCK(shmptr, wlock);
    wg_detach_database(shmptr);
  }
  exit(0);
}
コード例 #10
0
ファイル: main.c プロジェクト: KnightOS/scas
void parse_arguments(int argc, char **argv) {
	int i;
	for (i = 1; i < argc; ++i) {
		if (argv[i][0] == '-' && argv[i][1] != '\0') {
			if (strcmp("-o", argv[i]) == 0 || strcmp("--output", argv[i]) == 0) {
				scas_runtime.output_file = argv[++i];
			} else if (strcmp("-S", argv[i]) == 0 || strcmp("--symbols", argv[i]) == 0) {
				validate_scas_optarg(i, argc, argv);
				scas_runtime.symbol_file = argv[++i];
			} else if (strcmp("-L", argv[i]) == 0 || strcmp("--listing", argv[i]) == 0) {
				validate_scas_optarg(i, argc, argv);
				scas_runtime.listing_file = argv[++i];
			} else if (strcmp("-i", argv[i]) == 0 || strcmp("--input", argv[i]) == 0) {
				validate_scas_optarg(i, argc, argv);
				list_add(scas_runtime.input_files, argv[++i]);
			} else if (strcmp("-c", argv[i]) == 0 || strcmp("--merge", argv[i]) == 0) {
				scas_runtime.jobs &= ~LINK;
			} else if (argv[i][1] == 'f') {
				parse_flag(argv[i]);
			} else if (argv[i][1] == 'I' || strcmp("--include", argv[i]) == 0) {
				char *path;
				if (argv[i][1] == 'I' && argv[i][2] != 0) {
					// -I/path/goes/here
					path = argv[i] + 2;
				} else {
					// [-I | --include] path/goes/here
					validate_scas_optarg(i, argc, argv);
					path = argv[++i];
				}
				int l = strlen(scas_runtime.include_path);
				scas_runtime.include_path = realloc(scas_runtime.include_path, l + strlen(path) + 2);
				strcat(scas_runtime.include_path, ":");
				strcat(scas_runtime.include_path, path);
			} else if (argv[i][1] == 'v') {
				int j;
				for (j = 1; argv[i][j] != '\0'; ++j) {
					if (argv[i][j] == 'v') {
						scas_runtime.verbosity++;
					} else {
						scas_abort("Invalid option %s", argv[i]);
					}
				}
			} else if (argv[i][1] == 'D' || strcmp("--define", argv[i]) == 0) {
				char *name = NULL, *value = NULL;
				if (argv[i][1] == 'D' && argv[i][2]) {
					name = argv[i] + 2;
				} else {
					validate_scas_optarg(i, argc, argv);
					name = argv[++i];
				}
				value = strchr(name, '=');
				if (value) {
					*value = '\0';
					++value;
				} else {
					value = "1";
				}
				macro_t *macro = malloc(sizeof(macro_t));
				macro->parameters = create_list();
				macro->macro_lines = create_list();
				list_add(macro->macro_lines, strdup(value));
				macro->name = strdup(name);
				list_add(scas_runtime.macros, macro);
			} else {
				scas_abort("Invalid option %s", argv[i]);
			}
		} else {
			if (scas_runtime.output_file != NULL || i != argc - 1 || scas_runtime.input_files->length == 0) {
				scas_log(L_INFO, "Added input file '%s'", argv[i]);
				list_add(scas_runtime.input_files, argv[i]);
			} else if (scas_runtime.output_file == NULL && i == argc - 1) {
				scas_runtime.output_file = argv[i];
			}
		}
	}
}