text: /*                         W H A T I D . C
   * BRL-CAD
   *
   * Copyright (c) 2008-2021 United States Government as represented by
   * the U.S. Army Research Laboratory.
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public License
   * version 2.1 as published by the Free Software Foundation.
   *
   * This library is distributed in the hope that it will be useful, but
   * WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this file; see the file named COPYING for more
   * information.
   */
  /** @file libged/whatid.c
   *
   * The whatid command.
   *
   */
  
  #include "common.h"
  
  #include <string.h>
  
  #include "bu/cmd.h"
  
  #include "../ged_private.h"
  
  
  int
  ged_whatid_core(struct ged *gedp, int argc, const char *argv[])
  {
      struct directory *dp;
      struct rt_db_internal intern;
      struct rt_comb_internal *comb;
      static const char *usage = "region";
  
      GED_CHECK_DATABASE_OPEN(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 != 2) {
  	bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
  	return GED_ERROR;
      }
  
      dp = db_lookup(gedp->dbip, argv[1], LOOKUP_NOISY);
      if (dp == RT_DIR_NULL)
  	return GED_ERROR;
  
      if (!(dp->d_flags & RT_DIR_REGION)) {
  	bu_vls_printf(gedp->ged_result_str, "%s is not a region", argv[1]);
  	return GED_ERROR;
      }
  
      if (rt_db_get_internal(&intern, dp, gedp->dbip, (fastf_t *)NULL, &rt_uniresource) < 0)
  	return GED_ERROR;
      comb = (struct rt_comb_internal *)intern.idb_ptr;
  
      bu_vls_printf(gedp->ged_result_str, "%ld", comb->region_id);
      rt_db_free_internal(&intern);
  
      return GED_OK;
  }
  
  
  #ifdef GED_PLUGIN
  #include "../include/plugin.h"
  struct ged_cmd_impl whatid_cmd_impl = {
      "whatid",
      ged_whatid_core,
      GED_CMD_DEFAULT
  };
  
  const struct ged_cmd whatid_cmd = { &whatid_cmd_impl };
  const struct ged_cmd *whatid_cmds[] = { &whatid_cmd, NULL };
  
  static const struct ged_plugin pinfo = { GED_API,  whatid_cmds, 1 };
  
  COMPILER_DLLEXPORT const struct ged_plugin *ged_plugin_info()
  {
      return &pinfo;
  }
  #endif /* GED_PLUGIN */
  
  /*
   * Local Variables:
   * mode: C
   * tab-width: 8
   * indent-tabs-mode: t
   * c-file-style: "stroustrup"
   * End:
   * ex: shiftwidth=4 tabstop=8
   */
  