HIDDEN int
dsk_free(fb *ifp)
{
    close(ifp->if_fd);
    if (bu_file_delete(ifp->if_name)) {
	return 0;
    } else {
	return 1;
    }
}
示例#2
0
文件: color.c 项目: kanzure/brlcad
/*
 * used by the 'color' command when provided the -e option
 */
static int
edcolor(struct ged *gedp, int argc, const char *argv[])
{
    struct mater *mp;
    struct mater *zot;
    FILE *fp;
    int c;
    char line[128];
    static char hdr[] = "LOW\tHIGH\tRed\tGreen\tBlue\n";
    char tmpfil[MAXPATHLEN];
    char *editstring = NULL;

    GED_CHECK_DATABASE_OPEN(gedp, GED_ERROR);
    GED_CHECK_READ_ONLY(gedp, GED_ERROR);
    GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);

    bu_optind = 1;
    /* First, grab the editstring off of the argv list */
    while ((c = bu_getopt(argc, (char * const *)argv, "E:")) != -1) {
	switch (c) {
	    case 'E' :
		editstring = bu_optarg;
		break;
	    default :
		break;
	}
    }

    argc -= bu_optind - 1;
    argv += bu_optind - 1;

    /* initialize result */
    bu_vls_trunc(gedp->ged_result_str, 0);

    fp = bu_temp_file(tmpfil, MAXPATHLEN);
    if (fp == NULL) {
	bu_vls_printf(gedp->ged_result_str, "%s: could not create tmp file", argv[0]);
	return GED_ERROR;
    }

    fprintf(fp, "%s", hdr);
    for (mp = rt_material_head(); mp != MATER_NULL; mp = mp->mt_forw) {
	fprintf(fp, "%d\t%d\t%3d\t%3d\t%3d",
		      mp->mt_low, mp->mt_high,
		      mp->mt_r, mp->mt_g, mp->mt_b);
	fprintf(fp, "\n");
    }
    (void)fclose(fp);

    if (!_ged_editit(editstring, (const char *)tmpfil)) {
	bu_vls_printf(gedp->ged_result_str, "%s: editor returned bad status. Aborted\n", argv[0]);
	return GED_ERROR;
    }

    /* Read file and process it */
    if ((fp = fopen(tmpfil, "r")) == NULL) {
	perror(tmpfil);
	return GED_ERROR;
    }

    if (bu_fgets(line, sizeof (line), fp) == NULL ||
	line[0] != hdr[0]) {
	bu_vls_printf(gedp->ged_result_str, "%s: Header line damaged, aborting\n", argv[0]);
	(void)fclose(fp);
	return GED_ERROR;
    }

    if (db_version(gedp->ged_wdbp->dbip) < 5) {
	/* Zap all the current records, both in core and on disk */
	while (rt_material_head() != MATER_NULL) {
	    zot = rt_material_head();
	    rt_new_material_head(zot->mt_forw);
	    color_zaprec(gedp, zot);
	    bu_free((void *)zot, "mater rec");
	}

	while (bu_fgets(line, sizeof (line), fp) != NULL) {
	    int cnt;
	    int low, hi, r, g, b;

	    /* character-separated numbers (ideally a space) */
	    cnt = sscanf(line, "%d%*c%d%*c%d%*c%d%*c%d",
			 &low, &hi, &r, &g, &b);
	    if (cnt != 9) {
		bu_vls_printf(gedp->ged_result_str, "%s: Discarding %s\n", argv[0], line);
		continue;
	    }
	    BU_ALLOC(mp, struct mater);
	    mp->mt_low = low;
	    mp->mt_high = hi;
	    mp->mt_r = r;
	    mp->mt_g = g;
	    mp->mt_b = b;
	    mp->mt_daddr = MATER_NO_ADDR;
	    rt_insert_color(mp);
	    color_putrec(gedp, mp);
	}
    } else {
	struct bu_vls vls = BU_VLS_INIT_ZERO;

	/* free colors in rt_material_head */
	rt_color_free();

	while (bu_fgets(line, sizeof (line), fp) != NULL) {
	    int cnt;
	    int low, hi, r, g, b;

	    /* character-separated numbers (ideally a space) */
	    cnt = sscanf(line, "%d%*c%d%*c%d%*c%d%*c%d",
			 &low, &hi, &r, &g, &b);

	    /* check to see if line is reasonable */
	    if (cnt != 5) {
		bu_vls_printf(gedp->ged_result_str, "%s: Discarding %s\n", argv[0], line);
		continue;
	    }
	    bu_vls_printf(&vls, "{%d %d %d %d %d} ", low, hi, r, g, b);
	}

	db5_update_attribute("_GLOBAL", "regionid_colortable", bu_vls_addr(&vls), gedp->ged_wdbp->dbip);
	db5_import_color_table(bu_vls_addr(&vls));
	bu_vls_free(&vls);
    }

    (void)fclose(fp);
    bu_file_delete(tmpfil);

    /* if there are drawables, update their colors */
    if (gedp->ged_gdp)
	ged_color_soltab(gedp->ged_gdp->gd_headDisplay);

    return GED_OK;
}
示例#3
0
int
main(int argc, char *argv[])
{
    char _template[512] = {0};
    char buf[SIZE] = {0};

    FILE *fp = NULL;
    long count = 0;
    int tfd = 0;
    int ret = 0;

    if ((BU_STR_EQUAL(argv[1],"-h") || BU_STR_EQUAL(argv[1],"-?")) && argc == 2) {
	bu_log("Usage: %s (takes no arguments)\n",argv[0]);
	exit(1);
    }

    if (argc > 1) {
	bu_log("%s: unrecognized argument(s)\n", argv[0]);
	bu_log("        Program continues running:\n", argv[0]);
    }

    if ((count = bu_mread(0, buf, sizeof(buf))) < (long)sizeof(buf)) {
	if (count < 0) {
	    perror("buffer: mem read");
	    exit(1);
	}
	/* Entire input sequence fit into buf */
	if (write(1, buf, count) != count) {
	    perror("buffer: stdout write 1");
	    exit(1);
	}
	exit(0);
    }

    /* Create temporary file to hold data, get r/w file descriptor */
    fp = bu_temp_file(_template, 512);
    if (fp == NULL || (tfd = fileno(fp)) < 0) {
	perror(_template);
	goto err;
    }

    /* Stash away first buffer full */
    if (write(tfd, buf, count) != count) {
	perror("buffer: tmp write1");
	goto err;
    }

    /* Continue reading and writing additional buffer loads to temp file */
    while ((count = bu_mread(0, buf, sizeof(buf))) > 0) {
	if (write(tfd, buf, count) != count) {
	    perror("buffer: tmp write2");
	    goto err;
	}
    }
    if (count < 0) {
	perror("buffer: read");
	goto err;
    }

    /* All input read, regurgitate it all on stdout */
    if (lseek(tfd, 0, 0) < 0) {
	perror("buffer: lseek");
	goto err;
    }
    while ((count = bu_mread(tfd, buf, sizeof(buf))) > 0) {
	if (write(1, buf, count) != count) {
	    perror("buffer: stdout write 2");
	    goto err;
	}
    }
    if (count < 0) {
	perror("buffer: tmp read");
	goto err;
    }

    ret = 0;
    goto clean;
err:
    ret = 1;
clean:
    /* clean up */
    if (fp) {
	fclose(fp);
	fp = NULL;
    }
    bu_file_delete(_template);

    return ret;
}
示例#4
0
int
ged_put_comb(struct ged *gedp, int argc, const char *argv[])
{
    struct directory *dp;
    struct rt_db_internal intern;
    struct rt_comb_internal *comb;
    char new_name_v4[NAMESIZE+1];
    char *new_name;
    int offset;
    int save_comb_flag = 0;
    static const char *usage = "comb_name is_Region id air material los color shader inherit boolean_expr";
    static const char *noregionusage = "comb_name n color shader inherit boolean_expr";
    static const char *regionusage = "comb_name y id air material los color shader inherit boolean_expr";
    const char *saved_name = NULL;

    GED_CHECK_DATABASE_OPEN(gedp, GED_ERROR);
    GED_CHECK_READ_ONLY(gedp, GED_ERROR);
    GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);

    /* initialize result */
    bu_vls_trunc(gedp->ged_result_str, 0);

    /* must be wanting help */
    if (argc == 1) {
	bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
	return GED_HELP;
    }

    if (argc < 7 || 11 < argc) {
	bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
	return GED_ERROR;
    }

    comb = (struct rt_comb_internal *)NULL;
    dp = db_lookup(gedp->ged_wdbp->dbip, argv[1], LOOKUP_QUIET);
    if (dp != RT_DIR_NULL) {
	if (!(dp->d_flags & RT_DIR_COMB)) {
	    bu_vls_printf(gedp->ged_result_str, "%s: %s is not a combination, so cannot be edited this way\n", argv[0], argv[1]);
	    return GED_ERROR;
	}

	if (rt_db_get_internal(&intern, dp, gedp->ged_wdbp->dbip, (fastf_t *)NULL, &rt_uniresource) < 0) {
	    bu_vls_printf(gedp->ged_result_str, "%s: Database read error, aborting\n", argv[0]);
	    return GED_ERROR;
	}

	comb = (struct rt_comb_internal *)intern.idb_ptr;
	saved_name = save_comb(gedp, dp); /* Save combination to a temp name */
	save_comb_flag = 1;
    }

    /* empty the existing combination */
    if (comb) {
	db_free_tree(comb->tree, &rt_uniresource);
	comb->tree = NULL;
    } else {
	/* make an empty combination structure */
	BU_ALLOC(comb, struct rt_comb_internal);
	if (comb == NULL)
	    bu_bomb("Unable to allocate comb memory");
	RT_COMB_INTERNAL_INIT(comb);
    }

    if (db_version(gedp->ged_wdbp->dbip) < 5) {
	new_name = new_name_v4;
	if (dp == RT_DIR_NULL)
	    NAMEMOVE(argv[1], new_name_v4);
	else
	    NAMEMOVE(dp->d_namep, new_name_v4);
    } else {
	if (dp == RT_DIR_NULL)
	    new_name = (char *)argv[1];
	else
	    new_name = dp->d_namep;
    }

    if (*argv[2] == 'y' || *argv[2] == 'Y')
	comb->region_flag = 1;
    else
	comb->region_flag = 0;

    if (comb->region_flag) {
	if (argc != 11) {
	    bu_vls_printf(gedp->ged_result_str, "region_flag is set, incorrect number of arguments supplied.\n");
	    bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], regionusage);
	    return GED_ERROR;
	}

	comb->region_id = atoi(argv[3]);
	comb->aircode = atoi(argv[4]);
	comb->GIFTmater = atoi(argv[5]);
	comb->los = atoi(argv[6]);

	offset = 6;
    } else {
	if (argc != 7) {
	    bu_vls_printf(gedp->ged_result_str, "region_flag is not set, incorrect number of arguments supplied.\n");
	    bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], noregionusage);
	    return GED_ERROR;
	}
	offset = 2;
    }

    put_rgb_into_comb(comb, argv[offset + 1]);
    bu_vls_strcpy(&comb->shader, argv[offset +2]);

    if (*argv[offset + 3] == 'y' || *argv[offset + 3] == 'Y')
	comb->inherit = 1;
    else
	comb->inherit = 0;

    if (put_tree_into_comb(gedp, comb, dp, argv[1], new_name, argv[offset + 4]) == GED_ERROR) {
	if (comb && dp) {
	    restore_comb(gedp, dp, saved_name);
	    bu_vls_printf(gedp->ged_result_str, "%s: \toriginal restored\n", argv[0]);
	}
	bu_file_delete(_ged_tmpfil);
	return GED_ERROR;
    } else if (save_comb_flag) {
	/* eliminate the temporary combination */
	const char *av[3];

	av[0] = "kill";
	av[1] = saved_name;
	av[2] = NULL;
	(void)ged_kill(gedp, 2, (const char **)av);
    }

    bu_file_delete(_ged_tmpfil);
    return GED_OK;
}
示例#5
0
int
main(int ac, char *av[])
{
    struct stat sb;

    /* vars expected from cmd line parsing */
    const char* dsp1_fname = NULL;
    const char* dsp2_fname = NULL;
    const char* dsp3_fname = NULL;
    int arg_err = 0;
    long has_force = 0;
    long has_help  = 0;

    /* note the arg structs have to be static to compile */
    /* FIXME: this '-?' arg doesn't wok correctly due to some TCLAPisms */
    static bu_arg_vars h_arg = {
      BU_ARG_SwitchArg,
      "?",
      "short-help",
      "Same as '-h' or '--help'",
      BU_ARG_NOT_REQUIRED,
      BU_ARG_NOT_REQUIRED,
      {0},        /* value in first field of union */
      BU_ARG_BOOL /* type in union */
    };

    /* define a force option to allow user to shoot himself in the foot */
    static bu_arg_vars f_arg = {
      BU_ARG_SwitchArg,
      "f",
      "force",
      "Allow overwriting existing files.",
      BU_ARG_NOT_REQUIRED,
      BU_ARG_NOT_REQUIRED,
      {0},        /* value in first field of union */
      BU_ARG_BOOL /* type in union */
    };

    /* need two file names */
    static bu_arg_vars dsp1_arg = {
      BU_ARG_UnlabeledValueArg,
      "",
      "dsp_infile1",
      "first dsp input file name",
      BU_ARG_REQUIRED,
      BU_ARG_REQUIRED,
      {0},        /* value in first field of union */
      BU_ARG_BOOL /* type in union */
    };

    /* need two file names */
    static bu_arg_vars dsp2_arg = {
      BU_ARG_UnlabeledValueArg,
      "",
      "dsp_infile2",
      "second dsp input file name",
      BU_ARG_REQUIRED,
      BU_ARG_REQUIRED,
      {0},        /* value in first field of union */
      BU_ARG_BOOL /* type in union */
    };

    /* the output file name */
    static bu_arg_vars dsp3_arg = {
      BU_ARG_UnlabeledValueArg,
      "",
      "dsp_outfile",
      "dsp output file name",
      BU_ARG_REQUIRED,
      BU_ARG_REQUIRED,
      {0},        /* value in first field of union */
      BU_ARG_BOOL /* type in union */
    };

    /* place the arg pointers in an array */
    static bu_arg_vars *args[]
      = {&h_arg, &f_arg,
         &dsp1_arg, &dsp2_arg, &dsp3_arg,
         NULL};

    /* for C90 we have to initialize a struct's union
     * separately for other than its first field
     */
    dsp1_arg.val.s    = 0;
    dsp1_arg.val_type = BU_ARG_STRING;

    dsp2_arg.val.s    = 0;
    dsp2_arg.val_type = BU_ARG_STRING;

    dsp3_arg.val.s    = 0;
    dsp3_arg.val_type = BU_ARG_STRING;

    /* parse the args */
    arg_err = bu_arg_parse(args, ac, av);

    if (arg_err == BU_ARG_PARSE_ERR) {
        /* the TCLAP exception handler has fired with its own message
         * so need no words here */
        bu_exit(EXIT_SUCCESS, NULL);
    }

    /* Get the value parsed by each arg. */
    has_force  = f_arg.val.l;
    has_help   = h_arg.val.l;
    dsp1_fname = dsp1_arg.val.s;
    dsp2_fname = dsp2_arg.val.s;
    dsp3_fname = dsp3_arg.val.s;

    /* take appropriate action... */

    /* note this exit is success because it is expected
     * behavior--important for good auto-man-page handling */
    if (has_help) {
      bu_arg_free(args);
      bu_exit(EXIT_SUCCESS, usage);
    }

    /* TCLAP doesn't check for confusion in file names */
    if (BU_STR_EQUAL(dsp3_fname, dsp1_fname)
        || BU_STR_EQUAL(dsp3_fname, dsp2_fname)) {
      bu_arg_free(args);
      bu_exit(EXIT_FAILURE, "overwriting an input file (use the '-f' option to continue)\n");
    }

    /* nor does it check for existing files (FIXME: add to TCLAP) */
    if (!stat(dsp3_fname, &sb)) {
      if (has_force) {
        printf("WARNING: overwriting an existing file...\n");
        bu_file_delete(dsp3_fname);
      }
      else {
        bu_arg_free(args);
        bu_exit(EXIT_FAILURE, "overwriting an existing file (use the '-f' option to continue)\n");
      }
    }

    /* show results */

    bu_arg_free(args);

    return 0;
}
int
ged_edcodes(struct ged *gedp, int argc, const char *argv[])
{
    int i;
    int nflag = 0;
    int status;
    int sort_by_ident=0;
    int sort_by_region=0;
    int c;
    char **av;
    FILE *fp = NULL;
    char tmpfil[MAXPATHLEN] = {0};
    const char *editstring = NULL;

    static const char *usage = "[-i|-n|-r|-E editor] object(s)";

    GED_CHECK_DATABASE_OPEN(gedp, GED_ERROR);
    GED_CHECK_READ_ONLY(gedp, GED_ERROR);
    GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);

    /* initialize result */
    bu_vls_trunc(gedp->ged_result_str, 0);

    /* must be wanting help */
    if (argc == 1) {
	bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
	return GED_HELP;
    }

    bu_optind = 1;
    while ((c = bu_getopt(argc, (char * const *)argv, "E:inr")) != -1) {
	switch (c) {
	    case 'E' :
		editstring = bu_optarg;
		break;
	    case 'i':
		sort_by_ident = 1;
		break;
	    case 'n':
		nflag = 1;
		break;
	    case 'r':
		sort_by_region = 1;
		break;
	}
    }

    if ((nflag + sort_by_ident + sort_by_region) > 1) {
	bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
	return GED_ERROR;
    }

    argc -= bu_optind - 1;
    argv += bu_optind - 1;

    if (nflag) {
	struct directory *dp;

	for (i = 1; i < argc; ++i) {
	    if ((dp = db_lookup(gedp->ged_wdbp->dbip, argv[i], LOOKUP_NOISY)) != RT_DIR_NULL) {
		status = edcodes_collect_regnames(gedp, dp, 0);

		if (status != EDCODES_OK) {
		    if (status == EDCODES_HALT)
			bu_vls_printf(gedp->ged_result_str, "%s: nesting is too deep\n", argv[0]);

		    return GED_ERROR;
		}
	    }
	}

	return GED_OK;
    }

    fp = bu_temp_file(tmpfil, MAXPATHLEN);
    if (!fp)
	return GED_ERROR;

    av = (char **)bu_malloc(sizeof(char *)*(argc + 2), "ged_edcodes av");
    av[0] = "wcodes";
    av[1] = tmpfil;
    for (i = 2; i < argc + 1; ++i)
	av[i] = (char *)argv[i-1];

    av[i] = NULL;

    (void)fclose(fp);

    if (ged_wcodes(gedp, argc + 1, (const char **)av) == GED_ERROR) {
	bu_file_delete(tmpfil);
	bu_free((void *)av, "ged_edcodes av");
	return GED_ERROR;
    }

    if (sort_by_ident || sort_by_region) {
	char **line_array;
	char aline[RT_MAXLINE];
	FILE *f_srt;
	int line_count=0;
	int j;

	if ((f_srt=fopen(tmpfil, "r+")) == NULL) {
	    bu_vls_printf(gedp->ged_result_str, "%s: Failed to open temp file for sorting\n", argv[0]);
	    bu_file_delete(tmpfil);
	    return GED_ERROR;
	}

	/* count lines */
	while (bu_fgets(aline, RT_MAXLINE, f_srt)) {
	    line_count++;
	}

	/* build array of lines */
	line_array = (char **)bu_calloc(line_count, sizeof(char *), "edcodes line array");

	/* read lines and save into the array */
	rewind(f_srt);
	line_count = 0;
	while (bu_fgets(aline, RT_MAXLINE, f_srt)) {
	    line_array[line_count] = bu_strdup(aline);
	    line_count++;
	}

	/* sort the array of lines */
	if (sort_by_ident) {
	    bu_sort((void *)line_array, line_count, sizeof(char *), edcodes_id_compare, NULL);
	} else {
	    bu_sort((void *)line_array, line_count, sizeof(char *), edcodes_reg_compare, NULL);
	}

	/* rewrite the temp file using the sorted lines */
	rewind(f_srt);
	for (j = 0; j < line_count; j++) {
	    fprintf(f_srt, "%s", line_array[j]);
	    bu_free(line_array[j], "ged_edcodes line array element");
	}
	bu_free((char *)line_array, "ged_edcodes line array");
	fclose(f_srt);
    }

    if (_ged_editit(editstring, tmpfil)) {
	av[0] = "rcodes";
	av[2] = NULL;
	status = ged_rcodes(gedp, 2, (const char **)av);
    } else
	status = GED_ERROR;

    bu_file_delete(tmpfil);
    bu_free((void *)av, "ged_edcodes av");
    return status;
}