Пример #1
0
static int db_deltree_cb(DBT *key, DBT *value, const char *filter, void *data)
{
	int res = 0;

	if (keymatch(dbt_data2str_full(key, "<bad key>"), filter)) {
		astdb->del(astdb, key, 0);
		res = 1;
	}
	return res;
}
Пример #2
0
static int db_show_cb(DBT *key, DBT *value, const char *filter, void *data)
{
	struct ast_cli_args *a = data;
	const char *key_s = dbt_data2str_full(key, "<bad key>");
	const char *value_s = dbt_data2str_full(value, "<bad value>");

	if (keymatch(key_s, filter)) {
		ast_cli(a->fd, "%-50s: %-25s\n", key_s, value_s);
		return 1;
	}

	return 0;
}
Пример #3
0
static int database_show(int fd, int argc, char *argv[])
{
	char prefix[256];
	DBT key, data;
	char *keys, *values;
	int res;
	int pass;

	if (argc == 4) {
		/* Family and key tree */
		snprintf(prefix, sizeof(prefix), "/%s/%s", argv[2], argv[3]);
	} else if (argc == 3) {
		/* Family only */
		snprintf(prefix, sizeof(prefix), "/%s", argv[2]);
	} else if (argc == 2) {
		/* Neither */
		prefix[0] = '\0';
	} else {
		return RESULT_SHOWUSAGE;
	}
	ast_mutex_lock(&dblock);
	if (dbinit()) {
		ast_mutex_unlock(&dblock);
		ast_cli(fd, "Database unavailable\n");
		return RESULT_SUCCESS;	
	}
	memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));
	pass = 0;
	while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
		if (key.size) {
			keys = key.data;
			keys[key.size - 1] = '\0';
		} else {
			keys = "<bad key>";
		}
		if (data.size) {
			values = data.data;
			values[data.size - 1]='\0';
		} else {
			values = "<bad value>";
		}
		if (keymatch(keys, prefix)) {
				ast_cli(fd, "%-50s: %-25s\n", keys, values);
		}
	}
	ast_mutex_unlock(&dblock);
	return RESULT_SUCCESS;	
}
Пример #4
0
int ast_db_deltree(const char *family, const char *keytree)
{
	char prefix[256];
	DBT key, data;
	char *keys;
	int res;
	int pass;
	int counter = 0;
	
	if (family) {
		if (keytree) {
			snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
		} else {
			snprintf(prefix, sizeof(prefix), "/%s", family);
		}
	} else if (keytree) {
		return -1;
	} else {
		prefix[0] = '\0';
	}
	
	ast_mutex_lock(&dblock);
	if (dbinit()) {
		ast_mutex_unlock(&dblock);
		return -1;
	}
	
	memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));
	pass = 0;
	while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
		if (key.size) {
			keys = key.data;
			keys[key.size - 1] = '\0';
		} else {
			keys = "<bad key>";
		}
		if (keymatch(keys, prefix)) {
			astdb->del(astdb, &key, 0);
			counter++;
		}
	}
	astdb->sync(astdb, 0);
	ast_mutex_unlock(&dblock);
	return counter;
}
Пример #5
0
static int db_gettree_cb(DBT *key, DBT *value, const char *filter, void *data)
{
	struct ast_db_entry **ret = data;
	struct ast_db_entry *cur;
	const char *key_s = dbt_data2str_full(key, "<bad key>");
	const char *value_s = dbt_data2str_full(value, "<bad value>");
	size_t key_slen = strlen(key_s) + 1, value_slen = strlen(value_s) + 1;

	if (keymatch(key_s, filter) && (cur = ast_malloc(sizeof(*cur) + key_slen + value_slen))) {
		cur->next = *ret;
		cur->key = cur->data + value_slen;
		strcpy(cur->data, value_s);
		strcpy(cur->key, key_s);
		*ret = cur;
		return 1;
	}

	return 0;
}
Пример #6
0
parse_switches (j_decompress_ptr cinfo, int argc, char **argv,
		int last_file_arg_seen, boolean for_real)
/* Parse optional switches.
 * Returns argv[] index of first file-name argument (== argc if none).
 * Any file names with indexes <= last_file_arg_seen are ignored;
 * they have presumably been processed in a previous iteration.
 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
 * processing.
 */
{
  int argn;
  char * arg;

  /* Set up default JPEG parameters. */
  requested_fmt = DEFAULT_FMT;	/* set default output file format */
  outfilename = NULL;
  cinfo->err->trace_level = 0;

  /* Scan command line options, adjust parameters */

  for (argn = 1; argn < argc; argn++) {
    arg = argv[argn];
    if (*arg != '-') {
      /* Not a switch, must be a file name argument */
      if (argn <= last_file_arg_seen) {
	outfilename = NULL;	/* -outfile applies to just one input file */
	continue;		/* ignore this name if previously processed */
      }
      break;			/* else done parsing switches */
    }
    arg++;			/* advance past switch marker character */

    if (keymatch(arg, "bmp", 1)) {
      /* BMP output format. */
      requested_fmt = FMT_BMP;

    } else if (keymatch(arg, "colors", 1) || keymatch(arg, "colours", 1) ||
	       keymatch(arg, "quantize", 1) || keymatch(arg, "quantise", 1)) {
      /* Do color quantization. */
      int val;

      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (sscanf(argv[argn], "%d", &val) != 1)
	usage();
      cinfo->desired_number_of_colors = val;
      cinfo->quantize_colors = TRUE;

    } else if (keymatch(arg, "dct", 2)) {
      /* Select IDCT algorithm. */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (keymatch(argv[argn], "int", 1)) {
	cinfo->dct_method = JDCT_ISLOW;
      } else if (keymatch(argv[argn], "fast", 2)) {
	cinfo->dct_method = JDCT_IFAST;
      } else if (keymatch(argv[argn], "float", 2)) {
	cinfo->dct_method = JDCT_FLOAT;
      } else
	usage();

    } else if (keymatch(arg, "dither", 2)) {
      /* Select dithering algorithm. */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (keymatch(argv[argn], "fs", 2)) {
	cinfo->dither_mode = JDITHER_FS;
      } else if (keymatch(argv[argn], "none", 2)) {
	cinfo->dither_mode = JDITHER_NONE;
      } else if (keymatch(argv[argn], "ordered", 2)) {
	cinfo->dither_mode = JDITHER_ORDERED;
      } else
	usage();

    } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
      /* Enable debug printouts. */
      /* On first -d, print version identification */
      static boolean printed_version = FALSE;

      if (! printed_version) {
	fprintf(stderr, "Independent JPEG Group's DJPEG, version %s\n%s\n",
		JVERSION, JCOPYRIGHT);
	printed_version = TRUE;
      }
      cinfo->err->trace_level++;

    } else if (keymatch(arg, "fast", 1)) {
      /* Select recommended processing options for quick-and-dirty output. */
      cinfo->two_pass_quantize = FALSE;
      cinfo->dither_mode = JDITHER_ORDERED;
      if (! cinfo->quantize_colors) /* don't override an earlier -colors */
	cinfo->desired_number_of_colors = 216;
      cinfo->dct_method = JDCT_FASTEST;
      cinfo->do_fancy_upsampling = FALSE;

    } else if (keymatch(arg, "gif", 1)) {
      /* GIF output format. */
      requested_fmt = FMT_GIF;

    } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
      /* Force monochrome output. */
      cinfo->out_color_space = JCS_GRAYSCALE;

    } else if (keymatch(arg, "map", 3)) {
      /* Quantize to a color map taken from an input file. */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (for_real) {		/* too expensive to do twice! */
#ifdef QUANT_2PASS_SUPPORTED	/* otherwise can't quantize to supplied map */
	FILE * mapfile;

	if ((mapfile = fopen(argv[argn], READ_BINARY)) == NULL) {
	  fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
	  exit(EXIT_FAILURE);
	}
	read_color_map(cinfo, mapfile);
	fclose(mapfile);
	cinfo->quantize_colors = TRUE;
#else
	ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
      }

    } else if (keymatch(arg, "maxmemory", 3)) {
      /* Maximum memory in Kb (or Mb with 'm'). */
      long lval;
      char ch = 'x';

      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
	usage();
      if (ch == 'm' || ch == 'M')
	lval *= 1000L;
      cinfo->mem->max_memory_to_use = lval * 1000L;

    } else if (keymatch(arg, "nosmooth", 3)) {
      /* Suppress fancy upsampling */
      cinfo->do_fancy_upsampling = FALSE;

    } else if (keymatch(arg, "onepass", 3)) {
      /* Use fast one-pass quantization. */
      cinfo->two_pass_quantize = FALSE;

    } else if (keymatch(arg, "os2", 3)) {
      /* BMP output format (OS/2 flavor). */
      requested_fmt = FMT_OS2;

    } else if (keymatch(arg, "outfile", 4)) {
      /* Set output file name. */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      outfilename = argv[argn];	/* save it away for later use */

    } else if (keymatch(arg, "pnm", 1) || keymatch(arg, "ppm", 1)) {
      /* PPM/PGM output format. */
      requested_fmt = FMT_PPM;

    } else if (keymatch(arg, "rle", 1)) {
      /* RLE output format. */
      requested_fmt = FMT_RLE;

    } else if (keymatch(arg, "scale", 1)) {
      /* Scale the output image by a fraction M/N. */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (sscanf(argv[argn], "%d/%d",
		 &cinfo->scale_num, &cinfo->scale_denom) < 1)
	usage();

    } else if (keymatch(arg, "targa", 1)) {
      /* Targa output format. */
      requested_fmt = FMT_TARGA;

    } else {
      usage();			/* bogus switch */
    }
  }

  return argn;			/* return index of next arg (file name) */
}
Пример #7
0
int
main (int argc, char **argv)
{
  int argn;
  char * arg;
  int verbose = 0;

  /* On Mac, fetch a command line. */
#ifdef USE_CCOMMAND
  argc = ccommand(&argv);
#endif

  progname = argv[0];
  if (progname == NULL || progname[0] == 0)
    progname = "rdjpgcom";	/* in case C library doesn't provide it */

  /* Parse switches, if any */
  for (argn = 1; argn < argc; argn++) {
    arg = argv[argn];
    if (arg[0] != '-')
      break;			/* not switch, must be file name */
    arg++;			/* advance over '-' */
    if (keymatch(arg, "verbose", 1)) {
      verbose++;
    } else
      usage();
  }

  /* Open the input file. */
  /* Unix style: expect zero or one file name */
  if (argn < argc-1) {
    fprintf(stderr, "%s: only one input file\n", progname);
    usage();
  }
  if (argn < argc) {
    if ((infile = fopen(argv[argn], READ_BINARY)) == NULL) {
      fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
      exit(EXIT_FAILURE);
    }
  } else {
    /* default input file is stdin */
#ifdef USE_SETMODE		/* need to hack file mode? */
    setmode(fileno(stdin), O_BINARY);
#endif
#ifdef USE_FDOPEN		/* need to re-open in binary mode? */
    if ((infile = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
      fprintf(stderr, "%s: can't open stdin\n", progname);
      exit(EXIT_FAILURE);
    }
#else
    infile = stdin;
#endif
  }

  /* Scan the JPEG headers. */
  (void) scan_JPEG_header(verbose);

  /* All done. */
  exit(EXIT_SUCCESS);
  return 0;			/* suppress no-return-value warnings */
}
Пример #8
0
parse_switches (j_compress_ptr cinfo, int argc, char **argv,
		int last_file_arg_seen, jpegBoolean for_real)
/* Parse optional switches.
 * Returns argv[] index of first file-name argument (== argc if none).
 * Any file names with indexes <= last_file_arg_seen are ignored;
 * they have presumably been processed in a previous iteration.
 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
 * processing.
 */
{
  int argn;
  char * arg;
  int quality;			/* -quality parameter */
  int q_scale_factor;		/* scaling percentage for -qtables */
  jpegBoolean force_baseline;
  jpegBoolean simple_progressive;
  char * qtablefile = NULL;	/* saves -qtables filename if any */
  char * qslotsarg = NULL;	/* saves -qslots parm if any */
  char * samplearg = NULL;	/* saves -sample parm if any */
  char * scansarg = NULL;	/* saves -scans parm if any */

  /* Set up default JPEG parameters. */
  /* Note that default -quality level need not, and does not,
   * match the default scaling for an explicit -qtables argument.
   */
  quality = 75;			/* default -quality value */
  q_scale_factor = 100;		/* default to no scaling for -qtables */
  force_baseline = FALSE;	/* by default, allow 16-bit quantizers */
  simple_progressive = FALSE;
  is_targa = FALSE;
  outfilename = NULL;
  cinfo->err->trace_level = 0;

  /* Scan command line options, adjust parameters */

  for (argn = 1; argn < argc; argn++) {
    arg = argv[argn];
    if (*arg != '-') {
      /* Not a switch, must be a file name argument */
      if (argn <= last_file_arg_seen) {
	outfilename = NULL;	/* -outfile applies to just one input file */
	continue;		/* ignore this name if previously processed */
      }
      break;			/* else done parsing switches */
    }
    arg++;			/* advance past switch marker character */

    if (keymatch(arg, "arithmetic", 1)) {
      /* Use arithmetic coding. */
#ifdef C_ARITH_CODING_SUPPORTED
      cinfo->arith_code = TRUE;
#else
      fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
	      progname);
      exit(EXIT_FAILURE);
#endif

    } else if (keymatch(arg, "baseline", 1)) {
      /* Force baseline output (8-bit quantizer values). */
      force_baseline = TRUE;

    } else if (keymatch(arg, "dct", 2)) {
      /* Select DCT algorithm. */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (keymatch(argv[argn], "int", 1)) {
	cinfo->dct_method = JDCT_ISLOW;
      } else if (keymatch(argv[argn], "fast", 2)) {
	cinfo->dct_method = JDCT_IFAST;
      } else if (keymatch(argv[argn], "float", 2)) {
	cinfo->dct_method = JDCT_FLOAT;
      } else
	usage();

    } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
      /* Enable debug printouts. */
      /* On first -d, print version identification */
      static jpegBoolean printed_version = FALSE;

      if (! printed_version) {
	fprintf(stderr, "Independent JPEG Group's CJPEG, version %s\n%s\n",
		JVERSION, JCOPYRIGHT);
	printed_version = TRUE;
      }
      cinfo->err->trace_level++;

    } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
      /* Force a monochrome JPEG file to be generated. */
      jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);

    } else if (keymatch(arg, "maxmemory", 3)) {
      /* Maximum memory in Kb (or Mb with 'm'). */
      long lval;
      char ch = 'x';

      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
	usage();
      if (ch == 'm' || ch == 'M')
	lval *= 1000L;
      cinfo->mem->max_memory_to_use = lval * 1000L;

    } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
      /* Enable entropy parm optimization. */
#ifdef ENTROPY_OPT_SUPPORTED
      cinfo->optimize_coding = TRUE;
#else
      fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
	      progname);
      exit(EXIT_FAILURE);
#endif

    } else if (keymatch(arg, "outfile", 4)) {
      /* Set output file name. */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      outfilename = argv[argn];	/* save it away for later use */

    } else if (keymatch(arg, "progressive", 1)) {
      /* Select simple progressive mode. */
#ifdef C_PROGRESSIVE_SUPPORTED
      simple_progressive = TRUE;
      /* We must postpone execution until num_components is known. */
#else
      fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
	      progname);
      exit(EXIT_FAILURE);
#endif

    } else if (keymatch(arg, "quality", 1)) {
      /* Quality factor (quantization table scaling factor). */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (sscanf(argv[argn], "%d", &quality) != 1)
	usage();
      /* Change scale factor in case -qtables is present. */
      q_scale_factor = jpeg_quality_scaling(quality);

    } else if (keymatch(arg, "qslots", 2)) {
      /* Quantization table slot numbers. */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      qslotsarg = argv[argn];
      /* Must delay setting qslots until after we have processed any
       * colorspace-determining switches, since jpeg_set_colorspace sets
       * default quant table numbers.
       */

    } else if (keymatch(arg, "qtables", 2)) {
      /* Quantization tables fetched from file. */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      qtablefile = argv[argn];
      /* We postpone actually reading the file in case -quality comes later. */

    } else if (keymatch(arg, "restart", 1)) {
      /* Restart interval in MCU rows (or in MCUs with 'b'). */
      long lval;
      char ch = 'x';

      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
	usage();
      if (lval < 0 || lval > 65535L)
	usage();
      if (ch == 'b' || ch == 'B') {
	cinfo->restart_interval = (unsigned int) lval;
	cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
      } else {
	cinfo->restart_in_rows = (int) lval;
	/* restart_interval will be computed during startup */
      }

    } else if (keymatch(arg, "sample", 2)) {
      /* Set sampling factors. */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      samplearg = argv[argn];
      /* Must delay setting sample factors until after we have processed any
       * colorspace-determining switches, since jpeg_set_colorspace sets
       * default sampling factors.
       */

    } else if (keymatch(arg, "scans", 2)) {
      /* Set scan script. */
#ifdef C_MULTISCAN_FILES_SUPPORTED
      if (++argn >= argc)	/* advance to next argument */
	usage();
      scansarg = argv[argn];
      /* We must postpone reading the file in case -progressive appears. */
#else
      fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n",
	      progname);
      exit(EXIT_FAILURE);
#endif

    } else if (keymatch(arg, "smooth", 2)) {
      /* Set input smoothing factor. */
      int val;

      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (sscanf(argv[argn], "%d", &val) != 1)
	usage();
      if (val < 0 || val > 100)
	usage();
      cinfo->smoothing_factor = val;

    } else if (keymatch(arg, "targa", 1)) {
      /* Input file is Targa format. */
      is_targa = TRUE;

    } else {
      usage();			/* bogus switch */
    }
  }

  /* Post-switch-scanning cleanup */

  if (for_real) {

    /* Set quantization tables for selected quality. */
    /* Some or all may be overridden if -qtables is present. */
    jpeg_set_quality(cinfo, quality, force_baseline);

    if (qtablefile != NULL)	/* process -qtables if it was present */
      if (! read_quant_tables(cinfo, qtablefile,
			      q_scale_factor, force_baseline))
	usage();

    if (qslotsarg != NULL)	/* process -qslots if it was present */
      if (! set_quant_slots(cinfo, qslotsarg))
	usage();

    if (samplearg != NULL)	/* process -sample if it was present */
      if (! set_sample_factors(cinfo, samplearg))
	usage();

#ifdef C_PROGRESSIVE_SUPPORTED
    if (simple_progressive)	/* process -progressive; -scans can override */
      jpeg_simple_progression(cinfo);
#endif

#ifdef C_MULTISCAN_FILES_SUPPORTED
    if (scansarg != NULL)	/* process -scans if it was present */
      if (! read_scan_script(cinfo, scansarg))
	usage();
#endif
  }

  return argn;			/* return index of next arg (file name) */
}
Пример #9
0
parse_switches (j_compress_ptr cinfo, int argc, char **argv,
		int last_file_arg_seen, boolean for_real)
/* Parse optional switches.
 * Returns argv[] index of first file-name argument (== argc if none).
 * Any file names with indexes <= last_file_arg_seen are ignored;
 * they have presumably been processed in a previous iteration.
 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
 * processing.
 */
{
  int argn;
  char * arg;
  boolean force_baseline;
  boolean simple_progressive;
  char * qualityarg = NULL;	/* saves -quality parm if any */
  char * qtablefile = NULL;	/* saves -qtables filename if any */
  char * qslotsarg = NULL;	/* saves -qslots parm if any */
  char * samplearg = NULL;	/* saves -sample parm if any */
  char * scansarg = NULL;	/* saves -scans parm if any */

  /* Set up default JPEG parameters. */

  force_baseline = FALSE;	/* by default, allow 16-bit quantizers */
#ifdef C_PROGRESSIVE_SUPPORTED
  simple_progressive = cinfo->num_scans == 0 ? FALSE : TRUE;
#else
  simple_progressive = FALSE;
#endif
  is_targa = FALSE;
  outfilename = NULL;
  memdst = FALSE;
  cinfo->err->trace_level = 0;

  /* Scan command line options, adjust parameters */

  for (argn = 1; argn < argc; argn++) {
    arg = argv[argn];
    if (*arg != '-') {
      /* Not a switch, must be a file name argument */
      if (argn <= last_file_arg_seen) {
	outfilename = NULL;	/* -outfile applies to just one input file */
	continue;		/* ignore this name if previously processed */
      }
      break;			/* else done parsing switches */
    }
    arg++;			/* advance past switch marker character */

    if (keymatch(arg, "arithmetic", 1)) {
      /* Use arithmetic coding. */
#ifdef C_ARITH_CODING_SUPPORTED
      cinfo->arith_code = TRUE;
#else
      fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
	      progname);
      exit(EXIT_FAILURE);
#endif

    } else if (keymatch(arg, "baseline", 1)) {
      /* Force baseline-compatible output (8-bit quantizer values). */
      force_baseline = TRUE;

    } else if (keymatch(arg, "dct", 2)) {
      /* Select DCT algorithm. */
      if (++argn >= argc) { /* advance to next argument */
        fprintf(stderr, "%s: missing argument for dct\n", progname);
	usage();
      }
      if (keymatch(argv[argn], "int", 1)) {
	cinfo->dct_method = JDCT_ISLOW;
      } else if (keymatch(argv[argn], "fast", 2)) {
	cinfo->dct_method = JDCT_IFAST;
      } else if (keymatch(argv[argn], "float", 2)) {
	cinfo->dct_method = JDCT_FLOAT;
      } else
        fprintf(stderr, "%s: invalid argument for dct\n", progname);
	usage();

    } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
      /* Enable debug printouts. */
      /* On first -d, print version identification */
      static boolean printed_version = FALSE;

      if (! printed_version) {
	fprintf(stderr, "%s version %s (build %s)\n",
		PACKAGE_NAME, VERSION, BUILD);
	fprintf(stderr, "%s\n\n", JCOPYRIGHT);
	fprintf(stderr, "Emulating The Independent JPEG Group's software, version %s\n\n",
		JVERSION);
	printed_version = TRUE;
      }
      cinfo->err->trace_level++;

    } else if (keymatch(arg, "fastcrush", 4)) {
      cinfo->optimize_scans = FALSE;

    } else if (keymatch(arg, "flat", 4)) {
      cinfo->use_flat_quant_tbl = TRUE;
      jpeg_set_quality(cinfo, 75, TRUE);

    } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
      /* Force a monochrome JPEG file to be generated. */
      jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);

    } else if (keymatch(arg, "rgb", 3)) {
      /* Force an RGB JPEG file to be generated. */
      jpeg_set_colorspace(cinfo, JCS_RGB);

    } else if (keymatch(arg, "lambda1", 7)) {
      if (++argn >= argc)	/* advance to next argument */
	usage();
      cinfo->lambda_log_scale1 = atof(argv[argn]);

    } else if (keymatch(arg, "lambda2", 7)) {
      if (++argn >= argc)	/* advance to next argument */
	usage();
      cinfo->lambda_log_scale2 = atof(argv[argn]);

    } else if (keymatch(arg, "maxmemory", 3)) {
      /* Maximum memory in Kb (or Mb with 'm'). */
      long lval;
      char ch = 'x';

      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
	usage();
      if (ch == 'm' || ch == 'M')
	lval *= 1000L;
      cinfo->mem->max_memory_to_use = lval * 1000L;

    } else if (keymatch(arg, "multidcscan", 3)) {
      cinfo->one_dc_scan = FALSE;

    } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
      /* Enable entropy parm optimization. */
#ifdef ENTROPY_OPT_SUPPORTED
      cinfo->optimize_coding = TRUE;
#else
      fprintf(stderr, "%s: sorry, entropy optimization was not compiled in\n",
	      progname);
      exit(EXIT_FAILURE);
#endif

    } else if (keymatch(arg, "outfile", 4)) {
      /* Set output file name. */
      if (++argn >= argc)	{ /* advance to next argument */
        fprintf(stderr, "%s: missing argument for outfile\n", progname);
	usage();
      }
      outfilename = argv[argn];	/* save it away for later use */

    } else if (keymatch(arg, "progressive", 1)) {
      /* Select simple progressive mode. */
#ifdef C_PROGRESSIVE_SUPPORTED
      simple_progressive = TRUE;
      /* We must postpone execution until num_components is known. */
#else
      fprintf(stderr, "%s: sorry, progressive output was not compiled in\n",
	      progname);
      exit(EXIT_FAILURE);
#endif

    } else if (keymatch(arg, "memdst", 2)) {
      /* Use in-memory destination manager */
#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
      memdst = TRUE;
#else
      fprintf(stderr, "%s: sorry, in-memory destination manager was not compiled in\n",
              progname);
      exit(EXIT_FAILURE);
#endif

    } else if (keymatch(arg, "quality", 1)) {
      /* Quality ratings (quantization table scaling factors). */
      if (++argn >= argc)	{ /* advance to next argument */
        fprintf(stderr, "%s: missing argument for quality\n", progname);
	usage();
      }
      qualityarg = argv[argn];

    } else if (keymatch(arg, "qslots", 2)) {
      /* Quantization table slot numbers. */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      qslotsarg = argv[argn];
      /* Must delay setting qslots until after we have processed any
       * colorspace-determining switches, since jpeg_set_colorspace sets
       * default quant table numbers.
       */

    } else if (keymatch(arg, "qtables", 2)) {
      /* Quantization tables fetched from file. */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      qtablefile = argv[argn];
      /* We postpone actually reading the file in case -quality comes later. */

    } else if (keymatch(arg, "restart", 1)) {
      /* Restart interval in MCU rows (or in MCUs with 'b'). */
      long lval;
      char ch = 'x';

      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
	usage();
      if (lval < 0 || lval > 65535L)
	usage();
      if (ch == 'b' || ch == 'B') {
	cinfo->restart_interval = (unsigned int) lval;
	cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
      } else {
	cinfo->restart_in_rows = (int) lval;
	/* restart_interval will be computed during startup */
      }

    } else if (keymatch(arg, "revert", 3)) {
      /* revert to old JPEG default */
      cinfo->use_moz_defaults = FALSE;
      jpeg_set_defaults(cinfo);

    } else if (keymatch(arg, "sample", 2)) {
      /* Set sampling factors. */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      samplearg = argv[argn];
      /* Must delay setting sample factors until after we have processed any
       * colorspace-determining switches, since jpeg_set_colorspace sets
       * default sampling factors.
       */

    } else if (keymatch(arg, "scans", 4)) {
      /* Set scan script. */
#ifdef C_MULTISCAN_FILES_SUPPORTED
      if (++argn >= argc)	/* advance to next argument */
	usage();
      scansarg = argv[argn];
      /* We must postpone reading the file in case -progressive appears. */
#else
      fprintf(stderr, "%s: sorry, multi-scan output was not compiled in\n",
	      progname);
      exit(EXIT_FAILURE);
#endif

    } else if (keymatch(arg, "smooth", 2)) {
      /* Set input smoothing factor. */
      int val;

      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (sscanf(argv[argn], "%d", &val) != 1)
	usage();
      if (val < 0 || val > 100)
	usage();
      cinfo->smoothing_factor = val;

    } else if (keymatch(arg, "targa", 1)) {
      /* Input file is Targa format. */
      is_targa = TRUE;

    } else if (keymatch(arg, "notrellis", 1)) {
      /* disable trellis quantization */
      cinfo->trellis_quant = FALSE;
      
    } else if (keymatch(arg, "tune-psnr", 6)) {
      cinfo->use_flat_quant_tbl = TRUE;
      cinfo->lambda_log_scale1 = 9.0;
      cinfo->lambda_log_scale2 = 0.0;
      cinfo->use_lambda_weight_tbl = FALSE;
      jpeg_set_quality(cinfo, 75, TRUE);
      
    } else if (keymatch(arg, "tune-ssim", 6)) {
      cinfo->use_flat_quant_tbl = TRUE;
      cinfo->lambda_log_scale1 = 12.0;
      cinfo->lambda_log_scale2 = 13.5;
      cinfo->use_lambda_weight_tbl = FALSE;
      jpeg_set_quality(cinfo, 75, TRUE);
      
    } else if (keymatch(arg, "tune-ms-ssim", 6)) {
      cinfo->use_flat_quant_tbl = FALSE;
      cinfo->lambda_log_scale1 = 14.25;
      cinfo->lambda_log_scale2 = 12.75;
      cinfo->use_lambda_weight_tbl = TRUE;
      jpeg_set_quality(cinfo, 75, TRUE);
      
    } else if (keymatch(arg, "tune-hvs-psnr", 6)) {
      cinfo->use_flat_quant_tbl = FALSE;
      cinfo->lambda_log_scale1 = 16.0;
      cinfo->lambda_log_scale2 = 15.5;
      cinfo->use_lambda_weight_tbl = TRUE;
      jpeg_set_quality(cinfo, 75, TRUE);
      
    } else {
      fprintf(stderr, "%s: unknown option '%s'\n", progname, arg);
      usage();			/* bogus switch */
    }
  }

  /* Post-switch-scanning cleanup */

  if (for_real) {

    /* Set quantization tables for selected quality. */
    /* Some or all may be overridden if -qtables is present. */
    if (qualityarg != NULL)	/* process -quality if it was present */
      if (! set_quality_ratings(cinfo, qualityarg, force_baseline)) {
        fprintf(stderr, "%s: can't set quality ratings\n", progname);
	usage();
      }

    if (qtablefile != NULL)	/* process -qtables if it was present */
      if (! read_quant_tables(cinfo, qtablefile, force_baseline)) {
        fprintf(stderr, "%s: can't read qtable file\n", progname);
	usage();
      }

    if (qslotsarg != NULL)	/* process -qslots if it was present */
      if (! set_quant_slots(cinfo, qslotsarg))
	usage();

    if (samplearg != NULL)	/* process -sample if it was present */
      if (! set_sample_factors(cinfo, samplearg)) {
        fprintf(stderr, "%s: can't set sample factors\n", progname);
	usage();
      }

#ifdef C_PROGRESSIVE_SUPPORTED
    if (simple_progressive)	/* process -progressive; -scans can override */
      jpeg_simple_progression(cinfo);
#endif

#ifdef C_MULTISCAN_FILES_SUPPORTED
    if (scansarg != NULL)	/* process -scans if it was present */
      if (! read_scan_script(cinfo, scansarg))
	usage();
#endif
  }

  return argn;			/* return index of next arg (file name) */
}
Пример #10
0
parse_switches (j_compress_ptr cinfo, int argc, char **argv,
		int last_file_arg_seen, boolean for_real)
/* Parse optional switches.
 * Returns argv[] index of first file-name argument (== argc if none).
 * Any file names with indexes <= last_file_arg_seen are ignored;
 * they have presumably been processed in a previous iteration.
 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
 * processing.
 */
{
  int argn;
  char * arg;
  boolean simple_progressive;
  char * scansarg = NULL;	/* saves -scans parm if any */

  /* Set up default JPEG parameters. */
  simple_progressive = FALSE;
  outfilename = NULL;
  copyoption = JCOPYOPT_DEFAULT;
  transformoption.transform = JXFORM_NONE;
  transformoption.trim = FALSE;
  transformoption.force_grayscale = FALSE;
  cinfo->err->trace_level = 0;

  /* Scan command line options, adjust parameters */

  for (argn = 1; argn < argc; argn++) {
    arg = argv[argn];
    if (*arg != '-') {
      /* Not a switch, must be a file name argument */
      if (argn <= last_file_arg_seen) {
	outfilename = NULL;	/* -outfile applies to just one input file */
	continue;		/* ignore this name if previously processed */
      }
      break;			/* else done parsing switches */
    }
    arg++;			/* advance past switch marker character */

    if (keymatch(arg, "arithmetic", 1)) {
      /* Use arithmetic coding. */
#ifdef C_ARITH_CODING_SUPPORTED
      cinfo->arith_code = TRUE;
#else
      fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
	      progname);
      exit(EXIT_FAILURE);
#endif

    } else if (keymatch(arg, "copy", 1)) {
      /* Select which extra markers to copy. */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (keymatch(argv[argn], "none", 1)) {
	copyoption = JCOPYOPT_NONE;
      } else if (keymatch(argv[argn], "comments", 1)) {
	copyoption = JCOPYOPT_COMMENTS;
      } else if (keymatch(argv[argn], "all", 1)) {
	copyoption = JCOPYOPT_ALL;
      } else
	usage();

    } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
      /* Enable debug printouts. */
      /* On first -d, print version identification */
      static boolean printed_version = FALSE;

      if (! printed_version) {
	fprintf(stderr, "Independent JPEG Group's JPEGTRAN, version %s\n%s\n",
		JVERSION, JCOPYRIGHT);
	printed_version = TRUE;
      }
      cinfo->err->trace_level++;

    } else if (keymatch(arg, "flip", 1)) {
      /* Mirror left-right or top-bottom. */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (keymatch(argv[argn], "horizontal", 1))
	select_transform(JXFORM_FLIP_H);
      else if (keymatch(argv[argn], "vertical", 1))
	select_transform(JXFORM_FLIP_V);
      else
	usage();

    } else if (keymatch(arg, "grayscale", 1) || keymatch(arg, "greyscale",1)) {
      /* Force to grayscale. */
#if TRANSFORMS_SUPPORTED
      transformoption.force_grayscale = TRUE;
#else
      select_transform(JXFORM_NONE);	/* force an error */
#endif

    } else if (keymatch(arg, "maxmemory", 3)) {
      /* Maximum memory in Kb (or Mb with 'm'). */
      long lval;
      char ch = 'x';

      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
	usage();
      if (ch == 'm' || ch == 'M')
	lval *= 1000L;
      cinfo->mem->max_memory_to_use = lval * 1000L;

    } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
      /* Enable entropy parm optimization. */
#ifdef ENTROPY_OPT_SUPPORTED
      cinfo->optimize_coding = TRUE;
#else
      fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
	      progname);
      exit(EXIT_FAILURE);
#endif

    } else if (keymatch(arg, "outfile", 4)) {
      /* Set output file name. */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      outfilename = argv[argn];	/* save it away for later use */

    } else if (keymatch(arg, "progressive", 1)) {
      /* Select simple progressive mode. */
#ifdef C_PROGRESSIVE_SUPPORTED
      simple_progressive = TRUE;
      /* We must postpone execution until num_components is known. */
#else
      fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
	      progname);
      exit(EXIT_FAILURE);
#endif

    } else if (keymatch(arg, "restart", 1)) {
      /* Restart interval in MCU rows (or in MCUs with 'b'). */
      long lval;
      char ch = 'x';

      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
	usage();
      if (lval < 0 || lval > 65535L)
	usage();
      if (ch == 'b' || ch == 'B') {
	cinfo->restart_interval = (unsigned int) lval;
	cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
      } else {
	cinfo->restart_in_rows = (int) lval;
	/* restart_interval will be computed during startup */
      }

    } else if (keymatch(arg, "rotate", 2)) {
      /* Rotate 90, 180, or 270 degrees (measured clockwise). */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (keymatch(argv[argn], "90", 2))
	select_transform(JXFORM_ROT_90);
      else if (keymatch(argv[argn], "180", 3))
	select_transform(JXFORM_ROT_180);
      else if (keymatch(argv[argn], "270", 3))
	select_transform(JXFORM_ROT_270);
      else
	usage();

    } else if (keymatch(arg, "scans", 1)) {
      /* Set scan script. */
#ifdef C_MULTISCAN_FILES_SUPPORTED
      if (++argn >= argc)	/* advance to next argument */
	usage();
      scansarg = argv[argn];
      /* We must postpone reading the file in case -progressive appears. */
#else
      fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n",
	      progname);
      exit(EXIT_FAILURE);
#endif

    } else if (keymatch(arg, "transpose", 1)) {
      /* Transpose (across UL-to-LR axis). */
      select_transform(JXFORM_TRANSPOSE);

    } else if (keymatch(arg, "transverse", 6)) {
      /* Transverse transpose (across UR-to-LL axis). */
      select_transform(JXFORM_TRANSVERSE);

    } else if (keymatch(arg, "trim", 3)) {
      /* Trim off any partial edge MCUs that the transform can't handle. */
      transformoption.trim = TRUE;

    } else {
      usage();			/* bogus switch */
    }
  }

  /* Post-switch-scanning cleanup */

  if (for_real) {

#ifdef C_PROGRESSIVE_SUPPORTED
    if (simple_progressive)	/* process -progressive; -scans can override */
      jpeg_simple_progression(cinfo);
#endif

#ifdef C_MULTISCAN_FILES_SUPPORTED
    if (scansarg != NULL)	/* process -scans if it was present */
      if (! read_scan_script(cinfo, scansarg))
	usage();
#endif
  }

  return argn;			/* return index of next arg (file name) */
}
Пример #11
0
parse_switches (j_compress_ptr cinfo, int argc, char **argv,
		int last_file_arg_seen, boolean for_real)
{
  int argn;
  char * arg;
  boolean simple_progressive;
  char * scansarg = NULL;	

  
  simple_progressive = FALSE;
  outfilename = NULL;
  copyoption = JCOPYOPT_DEFAULT;
  transformoption.transform = JXFORM_NONE;
  transformoption.trim = FALSE;
  transformoption.force_grayscale = FALSE;
  cinfo->err->trace_level = 0;

  

  for (argn = 1; argn < argc; argn++) {
    arg = argv[argn];
    if (*arg != '-') {
      
      if (argn <= last_file_arg_seen) {
	outfilename = NULL;	
	continue;		
      }
      break;			
    }
    arg++;			

    if (keymatch(arg, "arithmetic", 1)) {
      
#ifdef C_ARITH_CODING_SUPPORTED
      cinfo->arith_code = TRUE;
#else
      fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
	      progname);
      exit(EXIT_FAILURE);
#endif

    } else if (keymatch(arg, "copy", 1)) {
      
      if (++argn >= argc)	
	usage();
      if (keymatch(argv[argn], "none", 1)) {
	copyoption = JCOPYOPT_NONE;
      } else if (keymatch(argv[argn], "comments", 1)) {
	copyoption = JCOPYOPT_COMMENTS;
      } else if (keymatch(argv[argn], "all", 1)) {
	copyoption = JCOPYOPT_ALL;
      } else
	usage();

    } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
      
      
      static boolean printed_version = FALSE;

      if (! printed_version) {
	fprintf(stderr, "Independent JPEG Group's JPEGTRAN, version %s\n%s\n",
		JVERSION, JCOPYRIGHT);
	printed_version = TRUE;
      }
      cinfo->err->trace_level++;

    } else if (keymatch(arg, "flip", 1)) {
      
      if (++argn >= argc)	
	usage();
      if (keymatch(argv[argn], "horizontal", 1))
	select_transform(JXFORM_FLIP_H);
      else if (keymatch(argv[argn], "vertical", 1))
	select_transform(JXFORM_FLIP_V);
      else
	usage();

    } else if (keymatch(arg, "grayscale", 1) || keymatch(arg, "greyscale",1)) {
      
#if TRANSFORMS_SUPPORTED
      transformoption.force_grayscale = TRUE;
#else
      select_transform(JXFORM_NONE);	
#endif

    } else if (keymatch(arg, "maxmemory", 3)) {
      
      long lval;
      char ch = 'x';

      if (++argn >= argc)	
	usage();
      if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
	usage();
      if (ch == 'm' || ch == 'M')
	lval *= 1000L;
      cinfo->mem->max_memory_to_use = lval * 1000L;

    } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
      
#ifdef ENTROPY_OPT_SUPPORTED
      cinfo->optimize_coding = TRUE;
#else
      fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
	      progname);
      exit(EXIT_FAILURE);
#endif

    } else if (keymatch(arg, "outfile", 4)) {
      
      if (++argn >= argc)	
	usage();
      outfilename = argv[argn];	

    } else if (keymatch(arg, "progressive", 1)) {
      
#ifdef C_PROGRESSIVE_SUPPORTED
      simple_progressive = TRUE;
      
#else
      fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
	      progname);
      exit(EXIT_FAILURE);
#endif

    } else if (keymatch(arg, "restart", 1)) {
      
      long lval;
      char ch = 'x';

      if (++argn >= argc)	
	usage();
      if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
	usage();
      if (lval < 0 || lval > 65535L)
	usage();
      if (ch == 'b' || ch == 'B') {
	cinfo->restart_interval = (unsigned int) lval;
	cinfo->restart_in_rows = 0; 
      } else {
	cinfo->restart_in_rows = (int) lval;
	
      }

    } else if (keymatch(arg, "rotate", 2)) {
      
      if (++argn >= argc)	
	usage();
      if (keymatch(argv[argn], "90", 2))
	select_transform(JXFORM_ROT_90);
      else if (keymatch(argv[argn], "180", 3))
	select_transform(JXFORM_ROT_180);
      else if (keymatch(argv[argn], "270", 3))
	select_transform(JXFORM_ROT_270);
      else
	usage();

    } else if (keymatch(arg, "scans", 1)) {
      
#ifdef C_MULTISCAN_FILES_SUPPORTED
      if (++argn >= argc)	
	usage();
      scansarg = argv[argn];
      
#else
      fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n",
	      progname);
      exit(EXIT_FAILURE);
#endif

    } else if (keymatch(arg, "transpose", 1)) {
      
      select_transform(JXFORM_TRANSPOSE);

    } else if (keymatch(arg, "transverse", 6)) {
      
      select_transform(JXFORM_TRANSVERSE);

    } else if (keymatch(arg, "trim", 3)) {
      
      transformoption.trim = TRUE;

    } else {
      usage();			
    }
  }

  

  if (for_real) {

#ifdef C_PROGRESSIVE_SUPPORTED
    if (simple_progressive)	
      jpeg_simple_progression(cinfo);
#endif

#ifdef C_MULTISCAN_FILES_SUPPORTED
    if (scansarg != NULL)	
      if (! read_scan_script(cinfo, scansarg))
	usage();
#endif
  }

  return argn;			
}
Пример #12
0
struct ast_db_entry *ast_db_gettree(const char *family, const char *keytree)
{
	char prefix[256];
	DBT key, data;
	char *keys, *values;
	int values_len;
	int res;
	int pass;
	struct ast_db_entry *last = NULL;
	struct ast_db_entry *cur, *ret=NULL;

	if (!ast_strlen_zero(family)) {
		if (!ast_strlen_zero(keytree)) {
			/* Family and key tree */
			snprintf(prefix, sizeof(prefix), "/%s/%s", family, prefix);
		} else {
			/* Family only */
			snprintf(prefix, sizeof(prefix), "/%s", family);
		}
	} else {
		prefix[0] = '\0';
	}
	ast_mutex_lock(&dblock);
	if (dbinit()) {
		ast_mutex_unlock(&dblock);
		ast_log(LOG_WARNING, "Database unavailable\n");
		return NULL;	
	}
	memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));
	pass = 0;
	while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
		if (key.size) {
			keys = key.data;
			keys[key.size - 1] = '\0';
		} else {
			keys = "<bad key>";
		}
		if (data.size) {
			values = data.data;
			values[data.size - 1] = '\0';
		} else {
			values = "<bad value>";
		}
		values_len = strlen(values) + 1;
		if (keymatch(keys, prefix) && (cur = ast_malloc(sizeof(*cur) + strlen(keys) + 1 + values_len))) {
			cur->next = NULL;
			cur->key = cur->data + values_len;
			strcpy(cur->data, values);
			strcpy(cur->key, keys);
			if (last) {
				last->next = cur;
			} else {
				ret = cur;
			}
			last = cur;
		}
	}
	ast_mutex_unlock(&dblock);
	return ret;	
}
Пример #13
0
int SpeciationType(FILE* database, char * Name){
    /* This subroutine is used to find out what the input species is.
     0) not found within database
     1) aqueous
     2) adsorption
     3) cation exchange
     4) mineral
     */
    double tmpval[WORDS_LINE];
    int i, j, return_val ;
    char keyword[WORD_WIDTH], line[LINE_WIDTH], word[WORD_WIDTH];

    if ( strcmp(Name, "pH") == 0) return(1);

    char ** tmpstr = (char**) malloc(WORDS_LINE*sizeof(char*));
    for ( i = 0; i < WORDS_LINE; i ++)
        tmpstr[i] = (char*) malloc(WORD_WIDTH*sizeof(char));
    
    return_val = 0 ;

    sprintf(word, "'%s'",Name);
    rewind(database);
    fgets(line, LINE_WIDTH, database);
    while (keymatch(line, "'End of primary'", tmpval, tmpstr)!=1){
        if (keymatch(line, "NULL", tmpval,tmpstr)!=2){
            if(strcmp(word, tmpstr[0])==0){
	      for ( i = 0; i < WORDS_LINE; i ++)
		free(tmpstr[i]);
	      free(tmpstr);
	      return(1);
            }
        }
        fgets(line, LINE_WIDTH, database);
    }
    fgets(line, LINE_WIDTH, database);
    while (keymatch(line, "'End of secondary'", tmpval, tmpstr)!=1){
        if (keymatch(line, "NULL", tmpval,tmpstr)!=2){
            if(strcmp(word, tmpstr[0])==0){
	      for ( i = 0; i < WORDS_LINE; i ++)
		free(tmpstr[i]);
	      free(tmpstr);
              return(5);
            }
        }
        fgets(line, LINE_WIDTH, database);
    }
    fgets(line, LINE_WIDTH, database);
    while (keymatch(line, "'End of minerals'", tmpval, tmpstr)!=1){
        if (keymatch(line, "NULL", tmpval,tmpstr)!=2){
            if(strcmp(word, tmpstr[0])==0){
	      for ( i = 0; i < WORDS_LINE; i ++)
		free(tmpstr[i]);
	      free(tmpstr);
	      return(4);
            }
        }
        fgets(line, LINE_WIDTH, database);
    }
    fgets(line, LINE_WIDTH, database);
    while (strcmp(line, "End of surface complexation\r\n")!=1){
        // notice that in crunchflow database, starting from surface complexation, there is not apostrophe marks around blocking keywords.
        if (keymatch(line, "NULL", tmpval,tmpstr)!=2){
            if(strcmp(word, tmpstr[0])==0){
	      for ( i = 0; i < WORDS_LINE; i ++)
		free(tmpstr[i]);
	      free(tmpstr);
	      return(2);
            }
        }
        fgets(line, LINE_WIDTH, database);
    }
    fgets(line, LINE_WIDTH, database);
    while (!feof(database)){
        if (keymatch(line, "NULL", tmpval,tmpstr)!=2){
            if(strcmp(word, tmpstr[0])==0){
	      for ( i = 0; i < WORDS_LINE; i ++)
		free(tmpstr[i]);
	      free(tmpstr);
	      return(3);
            }
        }
        fgets(line, LINE_WIDTH, database);
    }
    
    for ( i = 0; i < WORDS_LINE; i ++)
      free(tmpstr[i]);
    free(tmpstr);

    return( 0 );
}
Пример #14
0
void Lookup( FILE* database, Chem_Data CD, int lookupflg){
    
    /* lookupflg: used to lookup for
     0) uniform temperature field ( thermo decoupled ). Read temperature from CD->Temperature and return value for the whole field.
     1) values for each grid block at each time ( thermo coupled ). Read temperature from each grid block and return value for each grid block;
     */
    double tmpval[WORDS_LINE];
    //  CD->Keq = (double*) malloc( CD->NumSsc * sizeof(double));
    //  CD->KeqKinect = (double*) malloc( CD->NumMin * sizeof(double));
    
    // Kinetic reactions is currently only applicable to minerals
    int i, j , k, l, keq_position, total_temp_points;
    char keyword[WORD_WIDTH], line[LINE_WIDTH], word[WORD_WIDTH], tmp[WORD_WIDTH];
    char ** tmpstr = (char**) malloc(WORDS_LINE*sizeof(char*));


    for ( i = 0; i < WORDS_LINE; i ++){
        tmpstr[i] = (char*) malloc(WORD_WIDTH*sizeof(char));
	memset( tmpstr[i], 0 , WORD_WIDTH);
    }

    for ( i = 0; i < CD->NumStc + CD->NumSsc; i ++)
        wrap(CD->chemtype[i].ChemName);
    rewind(database);
    fgets(line, LINE_WIDTH, database);
    while (keymatch(line, "'temperature points'", tmpval, tmpstr)!=1){
        fgets(line, LINE_WIDTH, database);
    }
    total_temp_points = tmpval[0];
    for ( i = 0 ; i < tmpval[0]; i ++){
        if ( tmpval[i+1] == CD->Temperature ){
            fprintf(stderr, " Temperature point %6.4f C found in database!\n",tmpval[i+1]);
            keq_position = i+1;
        }
    }
    while (keymatch(line, "'Debye-Huckel adh'", tmpval, tmpstr)!=1){
        fgets(line, LINE_WIDTH, database);
    }
    CD->DH.adh = tmpval[keq_position -1];
    while (keymatch(line, "'Debye-Huckel bdh'", tmpval, tmpstr)!=1){
        fgets(line, LINE_WIDTH, database);
    }
    CD->DH.bdh = tmpval[keq_position -1];
    while (keymatch(line, "'Debye-Huckel bdt'", tmpval, tmpstr)!=1){
        fgets(line, LINE_WIDTH, database);
    }
    CD->DH.bdt = tmpval[keq_position -1];
    
    fprintf(stderr, " Debye-Huckel Parameters set to A=%6.4f; B=%6.4f; b=%6.4f\n",CD->DH.adh, CD->DH.bdh, CD->DH.bdt);
    
    rewind(database);
    fgets(line, LINE_WIDTH, database);
    while (keymatch(line, "'End of primary'", tmpval, tmpstr)!=1){
        if (keymatch(line, "NULL", tmpval,tmpstr)!=2){
            for( i = 0 ; i < CD->NumStc; i ++)
                if(strcmp(CD->chemtype[i].ChemName, tmpstr[0])==0){
                    fprintf(stderr, " Primary species %s found in database!\n MolarMass = %6.4f\n",CD->chemtype[i].ChemName,tmpval[2]);
                    CD->chemtype[i].MolarMass = tmpval[2];
                    CD->chemtype[i].Charge    = tmpval[1];
                    CD->chemtype[i].SizeF     = tmpval[0];
                }
        }
        fgets(line, LINE_WIDTH, database);
    }
    while (keymatch(line, "'End of secondary'", tmpval, tmpstr)!=1){
        if (keymatch(line, "NULL", tmpval,tmpstr)!=2){
            for( i = CD->NumStc ; i < CD->NumSsc + CD->NumStc; i ++)
                if(strcmp(CD->chemtype[i].ChemName, tmpstr[0])==0){
                    fprintf(stderr, " Secondary species %s found in database!\n",CD->chemtype[i].ChemName);
                    fprintf(stderr, " %s",line);
		    CD->chemtype[i].itype = 1;
                    for ( j = 0; j < WORDS_LINE; j++){
		      //                        	    fprintf(stderr, "%6.4f\t%s\n",tmpval[j],tmpstr[j]);
                        for ( k = 0 ; k < CD->NumSdc; k ++)
                            if(strcmp(CD->chemtype[k].ChemName, tmpstr[j])== 0)
                                CD->Dependency[i-CD->NumStc][k] = atof(tmpstr[j-1]);
                    }
                    CD->Keq[i- CD->NumStc] = tmpval[(int)tmpval[0]+keq_position];
                    fprintf(stderr, " Keq = %6.4f\n",CD->Keq[i-CD->NumStc]);
                    CD->chemtype[i].MolarMass = tmpval[(int)tmpval[0]+total_temp_points+3];
                    CD->chemtype[i].Charge    = tmpval[(int)tmpval[0]+total_temp_points+2];
                    CD->chemtype[i].SizeF     = tmpval[(int)tmpval[0]+total_temp_points+1];
                    fprintf(stderr, " MolarMass = %6.4f, Charge = %6.4f, SizeFactor = %6.4f\n", CD->chemtype[i].MolarMass, CD->chemtype[i].Charge, CD->chemtype[i].SizeF);
                }
        }
        fgets(line, LINE_WIDTH, database);
    }
    while (keymatch(line, "'End of minerals'", tmpval, tmpstr)!=1){
        if (keymatch(line, "NULL", tmpval,tmpstr)!=2){
            for( i = CD->NumSpc+ CD->NumAds + CD->NumCex ; i < CD->NumStc; i ++)
                if(strcmp(CD->chemtype[i].ChemName, tmpstr[0])==0){
                    fprintf(stderr, " Mineral %s found in database!\n",CD->chemtype[i].ChemName);
                    fprintf(stderr, " %s",line);
		    CD->chemtype[i].itype = 4;
                    CD->KeqKinect_all[i- CD->NumSpc - CD->NumAds - CD->NumCex] = tmpval[ (int)tmpval[1]+keq_position + 1];
                    for ( j = 1; j < WORDS_LINE; j++){
                        //  fprintf(stderr, "%6.4f\t%s\n",tmpval[j],tmpstr[j]);
                        for ( k = 0 ; k < CD->NumStc + CD->NumSsc; k ++)
                            if(strcmp(CD->chemtype[k].ChemName, tmpstr[j])== 0){
                                if ( k < CD->NumStc){
                                    CD->Dep_kinetic_all[i - CD->NumStc + CD->NumMin][k] = atof(tmpstr[j-1]);
                                }
                                else{
                                    for ( l = 0 ; l < CD->NumSpc; l ++)
                                        CD->Dep_kinetic_all[i - CD->NumStc + CD->NumMin][l] += atof(tmpstr[j-1])* CD->Dependency[ k - CD->NumStc][l];
                                    CD->KeqKinect_all[i- CD->NumSpc - CD->NumAds - CD->NumCex] += atof(tmpstr[j-1]) * CD->Keq[k - CD->NumStc];
                                }
                            }
                    }
                    CD->Dep_kinetic_all[i - CD->NumStc + CD->NumMin][i] = -1.0;
                    fprintf(stderr, " Keq = %6.4f\n",CD->KeqKinect_all[i- CD->NumSpc - CD->NumAds - CD->NumCex]);
                    CD->chemtype[i].MolarMass = tmpval[(int)tmpval[1]+total_temp_points+2];
                    CD->chemtype[i].MolarVolume= tmpval[0];
                    CD->chemtype[i].Charge   = 0;
                    fprintf(stderr, " MolarMass = %6.4f, MolarVolume = %6.4f\n", CD->chemtype[i].MolarMass, CD->chemtype[i].MolarVolume);
                }
        }
        fgets(line, LINE_WIDTH, database);
    }
    for ( i = 0; i < CD->NumMkr + CD->NumAkr ; i ++){
        for ( j = CD->NumSpc + CD->NumAds + CD->NumCex; j < CD->NumStc; j ++){
            strcpy(tmp, CD->kinetics[i].species);
            wrap(tmp);
            if ( strcmp(tmp, CD->chemtype[j].ChemName) == 0){
                fprintf(stderr, " Selecting the kinetic species %s from all possible species\n",tmp);
                CD->KeqKinect[i] = CD->KeqKinect_all[j - CD->NumStc + CD->NumMin];
                //	fprintf(stderr, " %6.4f\n", CD->KeqKinect[j - CD->NumStc + CD->NumMin]);
                for ( k = 0; k < CD->NumStc; k ++){
                    CD->Dep_kinetic[i][k] = CD->Dep_kinetic_all[j - CD->NumStc + CD->NumMin][k];
                }
            }
        }
    }
    while (strcmp(line, "End of surface complexation\r\n")!=1){
        if (keymatch(line, "NULL", tmpval,tmpstr)!=2){
            for( i = CD->NumStc ; i < CD->NumSsc + CD->NumStc; i ++)
                if(strcmp(CD->chemtype[i].ChemName, tmpstr[0])==0){
                    fprintf(stderr, " Secondary surface complexation %s found in database!\n",CD->chemtype[i].ChemName);
                    fprintf(stderr, " %s",line);
		    CD->chemtype[i].itype = 2;
                    for ( j = 0; j < WORDS_LINE; j++){
                        //      fprintf(stderr, "%6.4f\t%s\n",tmpval[j],tmpstr[j]);
                        for ( k = 0 ; k < CD->NumSdc; k ++)
                            if(strcmp(CD->chemtype[k].ChemName, tmpstr[j])== 0)
                                CD->Dependency[i-CD->NumStc][k] = atof(tmpstr[j-1]);
                    }
                    CD->Keq[i- CD->NumStc] = tmpval[(int)tmpval[0]+keq_position];
                    fprintf(stderr, " Keq = %6.4f\n",CD->Keq[i-CD->NumStc]);
                }
        }
        fgets(line, LINE_WIDTH, database);
    }
    while (!feof(database)){
      if (keymatch(line, "NULL", tmpval,tmpstr)!=2){
	for( i = CD->NumStc ; i < CD->NumSsc + CD->NumStc; i ++)
	  if(strcmp(CD->chemtype[i].ChemName, tmpstr[0])==0){
	    fprintf(stderr, " Secondary ion exchange %s found in database!\n",CD->chemtype[i].ChemName);
	    fprintf(stderr, " %s",line);
	    CD->chemtype[i].itype = 3;
	    for ( j = 0; j < WORDS_LINE; j++){
	      //    fprintf(stderr, "%6.4f\t%s\n",tmpval[j],tmpstr[j]);
	      for ( k = 0 ; k < CD->NumSdc; k ++)
		if(strcmp(CD->chemtype[k].ChemName, tmpstr[j])== 0)
		  CD->Dependency[i-CD->NumStc][k] = atof(tmpstr[j-1]);
	    }
	    CD->Keq[i- CD->NumStc] = tmpval[(int)tmpval[0]+1];
	    fprintf(stderr, " Keq = %6.4f\n",CD->Keq[i-CD->NumStc]);
	  }
      }
      fgets(line, LINE_WIDTH, database);
    }
    /*
     while (strcmp(line, "End of surface complexation parameters\r\n")!=1){
     if (keymatch(line, "NULL", tmpval,tmpstr)!=2){
     for( i = 0 ; i < CD->NumSsc + CD->NumStc; i ++)
     if(strcmp(CD->chemtype[i].ChemName, tmpstr[0])==0){
     fprintf(stderr, " Property of surface complexation %s found in database!\n",CD->chemtype[i].ChemName);
     CD->chemtype[i].Charge   = tmpval[0];;
     fprintf(stderr, " Charge = %6.4f\n", CD->chemtype[i].Charge);
     }
     
     }
     fgets(line, LINE_WIDTH, database);
     }
     */
    
    /*  for ( i = 0; i < CD->NumMkr; i ++){
     fprintf(stderr, " K %s L %s\n", CD->kinetics[i].species, CD->kinetics[i].Label);
     }
     */
    for ( i = 0 ; i < CD->NumMkr; i ++){
        rewind(database);
        fgets(line, LINE_WIDTH, database);
        while (strcmp(line, "Begin mineral kinetics\r\n") != 0){
            fgets(line, LINE_WIDTH, database);
        }
        fgets(line, LINE_WIDTH, database);
        while (strcmp(line, "End of mineral kinetics\r\n") != 0){
            if(keymatch(line, "NULL", tmpval, tmpstr)!=2){
                if(strcmp(CD->kinetics[i].species, tmpstr[0])==0){
                    //	  fprintf(stderr, " %s found in kinetics\n", CD->kinetics[i].species);
                    fgets(line, LINE_WIDTH, database);
                    keymatch(line, "NULL", tmpval, tmpstr);
                    //	  fprintf(stderr, " Labels %s ", tmpstr[2]); for (j = 0; j < strlen(tmpstr[2]); j++) fprintf(stderr, "%d\t",tmpstr[2][j]); fprintf(stderr, "\n");
                    if ( strcmp(CD->kinetics[i].Label, tmpstr[2]) == 0){
                        fprintf(stderr, " Mineral kinetics %s %s found in database!\n",CD->kinetics[i].species, CD->kinetics[i].Label);
                        fgets(line, LINE_WIDTH, database);
                        keymatch(line, "NULL", tmpval, tmpstr);
                        if (strcmp(tmpstr[2],"tst")==0) CD->kinetics[i].type = 1;
                        if (strcmp(tmpstr[2],"PrecipitationOnly")==0) CD->kinetics[i].type =2;
                        if (strcmp(tmpstr[2],"DissolutionOnly")==0) CD->kinetics[i].type =3;
                        if (strcmp(tmpstr[2],"monod") == 0) CD->kinetics[i].type =4;
                        fgets(line, LINE_WIDTH, database);
                        keymatch(line, "NULL", tmpval, tmpstr);
                        if (strcmp(tmpstr[0],"rate(25C)") == 0){
                            CD->kinetics[i].rate = tmpval[0];
                            fprintf(stderr, " Rate is %f\n", CD->kinetics[i].rate);
                        }
                        fgets(line, LINE_WIDTH, database);
                        keymatch(line, "NULL", tmpval, tmpstr);
                        if (strcmp(tmpstr[0],"activation")== 0){
                            CD->kinetics[i].actv = tmpval[0];
                            fprintf(stderr, " Activation is %f\n", CD->kinetics[i].actv);
                        }
                        fgets(line, LINE_WIDTH, database);
                        keymatch(line, "NULL", tmpval, tmpstr);
                        if (strcmp(tmpstr[0],"dependence")==0){
                            strcpy(CD->kinetics[i].dep_species[0], tmpstr[2]);
                            wrap(CD->kinetics[i].dep_species[0]);
                            CD->kinetics[i].num_dep = 1;
                            // assume that all mineral kinetic only depend on one species !!
                            
                            /* require further elaboration after this !! */
                            
                            for ( k = 0 ; k < CD->NumStc; k++){
                                if ( strcmp(CD->kinetics[i].dep_species[0], CD->chemtype[k].ChemName)==0)
                                    CD->kinetics[i].dep_position[0] = k;
                            }
                            CD->kinetics[i].dep_power[0] = tmpval[0];
                            fprintf(stderr, " Dependency: %s %f\n", CD->kinetics[i].dep_species[0], CD->kinetics[i].dep_power[0]);
                        }
                        wrap(CD->kinetics[i].species);
                    }
                }
                
            }
            fgets(line, LINE_WIDTH, database);
        }
    }
    for (i = 0; i < CD->NumMkr; i ++)
        for (j = 0; j < CD->NumStc; j ++){
            if( strcmp(CD->kinetics[i].species, CD->chemtype[j].ChemName) == 0){
                CD->kinetics[i].position = j;
                //	fprintf(stderr, " %s %s\n", CD->kinetics[i].species, CD->chemtype[j].ChemName);
            }
        }
    for (i = 0; i < CD->NumStc; i ++)
        CD->Totalconc[i][i] = 1.0;
    for (i = CD->NumStc; i < CD->NumStc + CD->NumSsc; i++)
        for ( j = 0; j <CD->NumSdc; j ++)
            CD->Totalconc[j][i] += CD->Dependency[i-CD->NumStc][j];
    fprintf(stderr, " Dependency Matrix!\n\t\t");
    for (i = 0; i < CD->NumSdc; i ++)
        fprintf(stderr, "%6s\t", CD->chemtype[i].ChemName);
    fprintf(stderr, "\n");
    for (i = 0; i < CD->NumSsc; i ++){
        fprintf(stderr, " %12s\t", CD->chemtype[i + CD->NumStc].ChemName);
        for (j = 0; j < CD->NumSdc; j ++)
            fprintf(stderr, " %6.2f\t",CD->Dependency[i][j]);
        fprintf(stderr, " %6.2f\n", CD->Keq[i]);
    }
    
    fprintf(stderr, " Total Concentration Matrix!\n\t\t");
    for (i = 0; i < CD->NumStc + CD->NumSsc; i ++)
        fprintf(stderr, "%6s\t", CD->chemtype[i].ChemName);
    fprintf(stderr, "\n");
    for (i = 0; i < CD->NumStc; i ++){
        fprintf(stderr, " Sum%12s\t", CD->chemtype[i].ChemName);
        for (j = 0; j < CD->NumStc + CD->NumSsc; j ++)
            fprintf(stderr, " %6.2f\t",CD->Totalconck[i][j] = CD->Totalconc[i][j]);
        fprintf(stderr, "\n");
    }
    
    fprintf(stderr, " Kinetic Mass Matrx!\n\t\t");
    for ( i = 0 ; i < CD->NumStc; i ++)
        fprintf(stderr, " %6s\t", CD->chemtype[i].ChemName);
    fprintf(stderr, "\n");
    for ( j = 0; j < CD->NumMkr + CD->NumAkr; j ++){
        fprintf(stderr, " %6s\t", CD->chemtype[j + CD->NumSpc + CD->NumAds + CD->NumCex].ChemName);
        for ( i = 0; i < CD->NumStc; i++){
            fprintf(stderr, " %6.2f\t", CD->Dep_kinetic[j][i]);
        }
        fprintf(stderr, "\tKeq = %6.2f\n", CD->KeqKinect[j]);
    }
    for ( i = 0; i < WORDS_LINE; i ++)
      free(tmpstr[i]);
    free(tmpstr);
    
}
Пример #15
0
parse_switches (j_file_ptr cinfo, int argc, char **argv,
		int last_file_arg_seen, boolean for_real)
/* Parse optional switches.
 * Returns argv[] index of first file-name argument (== argc if none).
 * Any file names with indexes <= last_file_arg_seen are ignored;
 * they have presumably been processed in a previous iteration.
 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
 * processing.
 */
{
  int argn;
  char * arg;

  /* Set up default JPEG parameters. */
  requested_fmt = DEFAULT_FMT;	/* set default output file format */
  outfilename = NULL;
  cinfo->err->trace_level = 4; /* for now we want debugging output*/

  /* Scan command line options, adjust parameters */

  for (argn = 1; argn < argc; argn++) {
    arg = argv[argn];
    if (*arg != '-') {
      /* Not a switch, must be a file name argument */
      if (argn <= last_file_arg_seen) {
	outfilename = NULL;	/* -outfile applies to just one input file */
	continue;		/* ignore this name if previously processed */
      }
      break;			/* else done parsing switches */
    }
    arg++;			/* advance past switch marker character */

    if (keymatch(arg, "bmp", 1)) {
      /* BMP output format. */
      requested_fmt = FMT_BMP;

    } else if (keymatch(arg, "gif", 1)) {
      /* GIF output format. */
      requested_fmt = FMT_GIF;

    } else if (keymatch(arg, "os2", 3)) {
      /* BMP output format (OS/2 flavor). */
      requested_fmt = FMT_OS2;

    } else if (keymatch(arg, "outfile", 4)) {
      /* Set output file name. */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      outfilename = argv[argn];	/* save it away for later use */

    } else if (keymatch(arg, "pnm", 1) || keymatch(arg, "ppm", 1)) {
      /* PPM/PGM output format. */
      requested_fmt = FMT_PPM;

    } else if (keymatch(arg, "rle", 1)) {
      /* RLE output format. */
      requested_fmt = FMT_RLE;

    } else if (keymatch(arg, "targa", 1)) {
      /* Targa output format. */
      requested_fmt = FMT_TARGA;

    } else {
      usage();			/* bogus switch */
    }
  }

  return argn;			/* return index of next arg (file name) */
}
Пример #16
0
static char *handle_cli_database_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
	char prefix[256];
	DBT key, data;
	char *keys, *values;
	int res;
	int pass;
	int counter = 0;

	switch (cmd) {
	case CLI_INIT:
		e->command = "database show";
		e->usage =
			"Usage: database show [family [keytree]]\n"
			"       Shows Asterisk database contents, optionally restricted\n"
			"       to a given family, or family and keytree.\n";
		return NULL;
	case CLI_GENERATE:
		return NULL;
	}

	if (a->argc == 4) {
		/* Family and key tree */
		snprintf(prefix, sizeof(prefix), "/%s/%s", a->argv[2], a->argv[3]);
	} else if (a->argc == 3) {
		/* Family only */
		snprintf(prefix, sizeof(prefix), "/%s", a->argv[2]);
	} else if (a->argc == 2) {
		/* Neither */
		prefix[0] = '\0';
	} else {
		return CLI_SHOWUSAGE;
	}
	ast_mutex_lock(&dblock);
	if (dbinit()) {
		ast_mutex_unlock(&dblock);
		ast_cli(a->fd, "Database unavailable\n");
		return CLI_SUCCESS;	
	}
	memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));
	pass = 0;
	while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
		if (key.size) {
			keys = key.data;
			keys[key.size - 1] = '\0';
		} else {
			keys = "<bad key>";
		}
		if (data.size) {
			values = data.data;
			values[data.size - 1]='\0';
		} else {
			values = "<bad value>";
		}
		if (keymatch(keys, prefix)) {
			ast_cli(a->fd, "%-50s: %-25s\n", keys, values);
			counter++;
		}
	}
	ast_mutex_unlock(&dblock);
	ast_cli(a->fd, "%d results found.\n", counter);
	return CLI_SUCCESS;	
}