Example #1
0
static int
split_cfg_path(const char *path, int *pargc, char ***pargv,
	       grecs_value_t ***pvalv)
{
	int argc;
	char **argv;
	char *delim = ".";
	char static_delim[2] = { 0, 0 };
  
	if (path[0] == '\\') {
		argv = calloc(2, sizeof (*argv));
		if (!argv)
			return WRDSE_NOSPACE;
		argv[0] = strdup(path + 1);
		if (!argv[0]) {
			free(argv);
			return WRDSE_NOSPACE;
		}
		argv[1] = NULL;
		argc = 1;
	} else {
		int rc;
		struct wordsplit ws;
		
		if (strchr("./:;,^~", path[0])) {
			delim = static_delim;
			delim[0] = path[0];
			path++;
		}
		ws.ws_delim = delim;
      
		rc = wordsplit(path, &ws,
			       WRDSF_DELIM | WRDSF_DEFFLAGS);
		if (rc)
			return rc;
		argc = ws.ws_wordc;
		argv = ws.ws_wordv;
		ws.ws_wordc = 0;
		ws.ws_wordv = NULL;
		wordsplit_free(&ws);
	}

	*pargv = argv;
	*pargc = argc;
	if (pvalv) {
		int i;
		grecs_value_t **valv;
		
		valv = grecs_calloc(argc, sizeof(valv[0]));
		for (i = 0; i < argc; i++) {
			char *p = strchr(argv[i], '=');
			if (p) {
				*p++ = 0;
				valv[i] = parse_label(p);
			}
		}
		*pvalv = valv;
	}
	return 0;
}
Example #2
0
char *
_dico_ldap_expand_user(const char *query, const char *user)
{
    struct wordsplit ws;
    const char *env[3];
    char *res;
    
    env[0] = "user";
    env[1] = user;
    env[2] = NULL;

    ws.ws_env = env;
    if (wordsplit(query, &ws,
		  WRDSF_NOSPLIT | WRDSF_NOCMD |
		  WRDSF_ENV | WRDSF_ENV_KV)) {
	dico_log(L_ERR, 0, _("cannot expand query `%s': %s"), query,
		 wordsplit_strerror(&ws));
	return NULL;
    }

    res = ws.ws_wordv[0];
    ws.ws_wordv[0] = NULL;
    wordsplit_free(&ws);
    return res;
}    
Example #3
0
void
register_sasl()
{
  int rc;
  char *listmech;
  struct wordsplit ws;
  
  if (!sasl_enable || init_sasl_0())
      return;
  rc =  gsasl_server_mechlist(ctx, &listmech);
  if (rc != GSASL_OK) {
      dico_log(L_ERR, 0, _("cannot get list of available SASL mechanisms: "
			   "%s"),
	       gsasl_strerror (rc));
      return;
  }

  if (wordsplit(listmech, &ws,
		WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_SQUEEZE_DELIMS) == 0) {
      int i;
      for (i = 0; i < ws.ws_wordc; i++) {
	  if (!disabled_mechanism_p(ws.ws_wordv[i])) {
	      char *name = xdico_sasl_mech_to_capa(ws.ws_wordv[i]);
	      dicod_capa_register(name, NULL, init_sasl_1, NULL);
	      dicod_capa_add(name);
	  }
      }
      wordsplit_free(&ws);
  }
  free(listmech);
}
Example #4
0
File: system.c Project: abligh/tar
static void
run_decompress_program (void)
{
  int i;
  const char *p, *prog = NULL;
  struct wordsplit ws;
  int wsflags = (WRDSF_DEFFLAGS | WRDSF_ENV | WRDSF_DOOFFS) & ~WRDSF_NOVAR;

  ws.ws_env = (const char **) environ;
  ws.ws_offs = 1;

  for (p = first_decompress_program (&i); p; p = next_decompress_program (&i))
    {
      if (prog)
	{
	  WARNOPT (WARN_DECOMPRESS_PROGRAM,
		   (0, errno, _("cannot run %s"), prog));
	  WARNOPT (WARN_DECOMPRESS_PROGRAM,
		   (0, 0, _("trying %s"), p));
	}
      if (wordsplit (p, &ws, wsflags))
	FATAL_ERROR ((0, 0, _("cannot split string '%s': %s"),
		      p, wordsplit_strerror (&ws)));
      wsflags |= WRDSF_REUSE;
      memmove(ws.ws_wordv, ws.ws_wordv + ws.ws_offs,
	      sizeof(ws.ws_wordv[0])*ws.ws_wordc);
      ws.ws_wordv[ws.ws_wordc] = (char *) "-d";
      prog = p;
      execvp (ws.ws_wordv[0], ws.ws_wordv);
      ws.ws_wordv[ws.ws_wordc] = NULL;
    }
  if (!prog)
    FATAL_ERROR ((0, 0, _("unable to run decompression program")));
  exec_fatal (prog);
}
Example #5
0
static int
db_open(void **phandle, dico_url_t url, const char *options)
{
    struct _dico_ldap_handle hstr, *handle;
    int rc;
    long debug = 0;
    
    struct dico_option option[] = {
	{ DICO_OPTSTR(base), dico_opt_string, &hstr.base },
	{ DICO_OPTSTR(binddn), dico_opt_string, &hstr.binddn },
	{ DICO_OPTSTR(passwd), dico_opt_string, &hstr.passwd },
	{ DICO_OPTSTR(tls), dico_opt_bool, &hstr.tls },
	{ DICO_OPTSTR(debug), dico_opt_long, &debug },
	{ DICO_OPTSTR(user-filter), dico_opt_string, &hstr.user_filter },
	{ DICO_OPTSTR(group-filter), dico_opt_string, &hstr.group_filter },
	{ NULL }
    };

    memset(&hstr, 0, sizeof(hstr));
    if (options) {
	struct wordsplit ws;

	if (wordsplit(options, &ws, WRDSF_DEFFLAGS)) {
	    dico_log(L_ERR, 0, _("cannot parse options `%s': %s"), options,
		     wordsplit_strerror(&ws));
	    return 1;
	}

	rc = dico_parseopt(option, ws.ws_wordc, ws.ws_wordv,
			   DICO_PARSEOPT_PARSE_ARGV0, NULL);
	wordsplit_free(&ws);
	if (rc)
	    return 1;
	hstr.debug = debug;
    }
    hstr.url = url->string;
    
    handle = malloc(sizeof(*handle));
    if (!handle) {
	dico_log(L_ERR, errno, _("cannot allocate handle"));
	return 1;
    }
    *handle = hstr;
    
    rc = _dico_conn_setup(handle) ||
	 _dico_ldap_bind(handle);
    if (rc) {
	free_ldap_handle(handle);
    } else {
	*phandle = handle;
    }
    return rc;
}
Example #6
0
File: system.c Project: abligh/tar
static _Noreturn void
xexec (const char *cmd)
{
  struct wordsplit ws;

  ws.ws_env = (const char **) environ;
  if (wordsplit (cmd, &ws, (WRDSF_DEFFLAGS | WRDSF_ENV) & ~WRDSF_NOVAR))
    FATAL_ERROR ((0, 0, _("cannot split string '%s': %s"),
		  cmd, wordsplit_strerror (&ws)));
  execvp (ws.ws_wordv[0], ws.ws_wordv);
  exec_fatal (cmd);
}
Example #7
0
static struct grecs_value *
parse_label(const char *str)
{
	struct grecs_value *val = NULL;
	size_t i;
	struct wordsplit ws;
	size_t len = strlen (str);
  
	if (len > 1 && str[0] == '(' && str[len-1] == ')') {
		struct grecs_list *lst;
      
		ws.ws_delim = ",";
		if (wordsplit_len (str + 1, len - 2, &ws,
				      WRDSF_DEFFLAGS|WRDSF_DELIM|
				      WRDSF_WS)) {
			return NULL;
		}

		lst = grecs_value_list_create();
		for (i = 0; i < ws.ws_wordc; i++) {
			struct grecs_value *p = grecs_zalloc(sizeof(*p));
			p->type = GRECS_TYPE_STRING;
			p->v.string = ws.ws_wordv[i];
			grecs_list_append(lst, p);
		}
		val = grecs_malloc(sizeof(*val));
		val->type = GRECS_TYPE_LIST;
		val->v.list = lst;
	} else {      
		if (wordsplit(str, &ws, WRDSF_DEFFLAGS))
			return NULL;
		val = grecs_zalloc(sizeof(*val));
		if (ws.ws_wordc == 1) {
			val->type = GRECS_TYPE_STRING;
			val->v.string = ws.ws_wordv[0];
		} else {
			val->type = GRECS_TYPE_ARRAY;
			val->v.arg.c = ws.ws_wordc;
			val->v.arg.v = grecs_calloc(ws.ws_wordc,
						    sizeof(val->v.arg.v[0]));
			for (i = 0; i < ws.ws_wordc; i++) {
				val->v.arg.v[i] =
					grecs_zalloc(sizeof(*val->v.arg.v[0]));
				val->v.arg.v[i]->type = GRECS_TYPE_STRING;
				val->v.arg.v[i]->v.string = ws.ws_wordv[i];
			}
		}
	}
	ws.ws_wordc = 0;
	wordsplit_free(&ws);
	return val;
}
Example #8
0
static void
cvs_addfn (struct exclude *ex, char const *pattern, int options, void *data)
{
  struct wordsplit ws;
  size_t i;

  if (wordsplit (pattern, &ws,
		 WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_SQUEEZE_DELIMS))
    return;
  for (i = 0; i < ws.ws_wordc; i++)
    add_exclude (ex, ws.ws_wordv[i], options);
  wordsplit_free (&ws);
}
Example #9
0
void
lexicon_init (void)
{
  Ustr *lexicons_path;
  FILE *lexicons;
  Ustr *line;
  AUstr words;

  lexicons_path = ustr_dup (cmdline_pkgdatadir ());
  if (!lexicons_path)
    abort ();
  if (!ustr_add_cstr (&lexicons_path, "/lexicons"))
    abort ();
  line = ustr_dup_empty ();
  if (!line)
    abort ();
  austr_init (&words);

  lexicons = fopen (ustr_cstr (lexicons_path), "r");
  if (!lexicons)
    abort ();

  lexicon_list = 0;
  while (errno = 0, ustr_sc_del (&line), ustr_io_getline (&line, lexicons))
    {
      unsigned int i;
      wordsplit (&words, line);
      for (i = 0; i < austr_length (&words); ++i)
	{
	  LexiconList *nlist;
	  nlist = malloc (sizeof (LexiconList));
	  if (!nlist)
	    abort ();
	  nlist->name = ustr_dup (austr_i (&words, i));
	  nlist->lexicon = 0;
	  nlist->next = lexicon_list;
	  lexicon_list = nlist;
	}
    }
  if (errno != 0)
    {
      perror ("galcry-backend, lexicons");
      fclose (lexicons);
      abort ();
    }

  fclose (lexicons);
  austr_deinit (&words);
  ustr_sc_free (&lexicons_path);
}
Example #10
0
static int
expvar (struct wordsplit *wsp, const char *str, size_t len,
	struct wordsplit_node **ptail, const char **pend, int flg)
{
  size_t i = 0;
  const char *defstr = NULL;
  const char *value;
  const char *vptr;
  struct wordsplit_node *newnode;
  const char *start = str - 1;

  if (ISALPHA (str[0]) || str[0] == '_')
    {
      for (i = 1; i < len; i++)
	if (!(ISALNUM (str[i]) || str[i] == '_'))
	  break;
      *pend = str + i - 1;
    }
  else if (str[0] == '{')
    {
      str++;
      len--;
      for (i = 1; i < len; i++)
	if (str[i] == '}' || str[i] == ':')
	  break;
      if (str[i] == ':')
	{
	  size_t j;

	  defstr = str + i + 1;
	  if (find_closing_cbrace (str, i + 1, len, &j))
	    {
	      wsp->ws_errno = WRDSE_CBRACE;
	      return 1;
	    }
	  *pend = str + j;
	}
      else if (str[i] == '}')
	{
	  defstr = NULL;
	  *pend = str + i;
	}
      else
	{
	  wsp->ws_errno = WRDSE_CBRACE;
	  return 1;
	}
    }
  else
    {
      if (wsnode_new (wsp, &newnode))
	return 1;
      wsnode_insert (wsp, newnode, *ptail, 0);
      *ptail = newnode;
      newnode->flags = _WSNF_WORD | flg;
      newnode->v.word = malloc (3);
      if (!newnode->v.word)
	return _wsplt_nomem (wsp);
      newnode->v.word[0] = '$';
      newnode->v.word[1] = str[0];
      newnode->v.word[2] = 0;
      *pend = str;
      return 0;
    }

  /* Actually expand the variable */
  /* str - start of the variable name
     i   - its length
     defstr - default replacement str */

  vptr = wordsplit_find_env (wsp, str, i);
  if (vptr)
    {
      value = strdup (vptr);
      if (!value)
	return _wsplt_nomem (wsp);
    }
  else if (wsp->ws_flags & WRDSF_GETVAR)
    value = wsp->ws_getvar (str, i, wsp->ws_closure);
  else if (wsp->ws_flags & WRDSF_UNDEF)
    {
      wsp->ws_errno = WRDSE_UNDEF;
      if (wsp->ws_flags & WRDSF_SHOWERR)
	wordsplit_perror (wsp);
      return 1;
    }
  else
    {
      if (wsp->ws_flags & WRDSF_KEEPUNDEF)
	value = NULL;
      else
	value = "";
    }

  /* FIXME: handle defstr */
  (void) defstr;

  if (value)
    {
      if (flg & _WSNF_QUOTE)
	{
	  if (wsnode_new (wsp, &newnode))
	    return 1;
	  wsnode_insert (wsp, newnode, *ptail, 0);
	  *ptail = newnode;
	  newnode->flags = _WSNF_WORD | _WSNF_NOEXPAND | flg;
	  newnode->v.word = strdup (value);
	  if (!newnode->v.word)
	    return _wsplt_nomem (wsp);
	}
      else if (*value == 0)
	{
	  /* Empty string is a special case */
	  if (wsnode_new (wsp, &newnode))
	    return 1;
	  wsnode_insert (wsp, newnode, *ptail, 0);
	  *ptail = newnode;
	  newnode->flags = _WSNF_NULL;
	}
      else
	{
	  struct wordsplit ws;
	  int i;

	  ws.ws_delim = wsp->ws_delim;
	  if (wordsplit (value, &ws,
			 WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_DELIM | WRDSF_WS))
	    {
	      wordsplit_free (&ws);
	      return 1;
	    }
	  for (i = 0; i < ws.ws_wordc; i++)
	    {
	      if (wsnode_new (wsp, &newnode))
		return 1;
	      wsnode_insert (wsp, newnode, *ptail, 0);
	      *ptail = newnode;
	      newnode->flags = _WSNF_WORD |
		_WSNF_NOEXPAND |
		(i + 1 < ws.ws_wordc ? (flg & ~_WSNF_JOIN) : flg);
	      newnode->v.word = strdup (ws.ws_wordv[i]);
	      if (!newnode->v.word)
		return _wsplt_nomem (wsp);
	    }
	  wordsplit_free (&ws);
	}
    }
  else if (wsp->ws_flags & WRDSF_KEEPUNDEF)
    {
      size_t size = *pend - start + 1;

      if (wsnode_new (wsp, &newnode))
	return 1;
      wsnode_insert (wsp, newnode, *ptail, 0);
      *ptail = newnode;
      newnode->flags = _WSNF_WORD | _WSNF_NOEXPAND | flg;
      newnode->v.word = malloc (size + 1);
      if (!newnode->v.word)
	return _wsplt_nomem (wsp);
      memcpy (newnode->v.word, start, size);
      newnode->v.word[size] = 0;
    }
  else
    {
      if (wsnode_new (wsp, &newnode))
	return 1;
      wsnode_insert (wsp, newnode, *ptail, 0);
      *ptail = newnode;
      newnode->flags = _WSNF_NULL;
    }
  return 0;
}
Example #11
0
static int
format_checkpoint_string (FILE *fp, size_t len,
			  const char *input, bool do_write,
			  unsigned cpn)
{
  const char *opstr = do_write ? gettext ("write") : gettext ("read");
  char uintbuf[UINTMAX_STRSIZE_BOUND];
  char *cps = STRINGIFY_BIGINT (cpn, uintbuf);
  const char *ip;

  static char *argbuf = NULL;
  static size_t arglen = 0;
  char *arg = NULL;

  if (!input)
    {
      if (do_write)
	/* TRANSLATORS: This is a "checkpoint of write operation",
	 *not* "Writing a checkpoint".
	 E.g. in Spanish "Punto de comprobaci@'on de escritura",
	 *not* "Escribiendo un punto de comprobaci@'on" */
	input = gettext ("Write checkpoint %u");
      else
	/* TRANSLATORS: This is a "checkpoint of read operation",
	 *not* "Reading a checkpoint".
	 E.g. in Spanish "Punto de comprobaci@'on de lectura",
	 *not* "Leyendo un punto de comprobaci@'on" */
	input = gettext ("Read checkpoint %u");
    }

  for (ip = input; *ip; ip++)
    {
      if (*ip == '%')
	{
	  if (*++ip == '{')
	    {
	      arg = getarg (ip, &ip, &argbuf, &arglen);
	      if (!arg)
		{
		  fputc ('%', fp);
		  fputc (*ip, fp);
		  len += 2;
		  continue;
		}
	    }
	  switch (*ip)
	    {
	    case 'c':
	      len += format_checkpoint_string (fp, len, def_format, do_write,
					       cpn);
	      break;

	    case 'u':
	      fputs (cps, fp);
	      len += strlen (cps);
	      break;

	    case 's':
	      fputs (opstr, fp);
	      len += strlen (opstr);
	      break;

	    case 'd':
	      len += fprintf (fp, "%.0f", compute_duration ());
	      break;

	    case 'T':
	      {
		const char **fmt = checkpoint_total_format, *fmtbuf[3];
		struct wordsplit ws;
		compute_duration ();

		if (arg)
		  {
		    ws.ws_delim = ",";
		    if (wordsplit (arg, &ws, WRDSF_NOVAR | WRDSF_NOCMD |
				           WRDSF_QUOTE | WRDSF_DELIM))
		      ERROR ((0, 0, _("cannot split string '%s': %s"),
			      arg, wordsplit_strerror (&ws)));
		    else
		      {
			int i;

			for (i = 0; i < ws.ws_wordc; i++)
			  fmtbuf[i] = ws.ws_wordv[i];
			for (; i < 3; i++)
			  fmtbuf[i] = NULL;
			fmt = fmtbuf;
		      }
		  }
		len += format_total_stats (fp, fmt, ',', 0);
		if (arg)
		  wordsplit_free (&ws);
	      }
	      break;

	    case 't':
	      {
		struct timeval tv;
		struct tm *tm;
		const char *fmt = arg ? arg : "%c";

		gettimeofday (&tv, NULL);
		tm = localtime (&tv.tv_sec);
		len += fprintftime (fp, fmt, tm, 0, tv.tv_usec * 1000);
	      }
	      break;

	    case '*':
	      {
		long w = arg ? strtol (arg, NULL, 10) : getwidth (fp);
		for (; w > len; len++)
		  fputc (' ', fp);
	      }
	      break;

	    default:
	      fputc ('%', fp);
	      fputc (*ip, fp);
	      len += 2;
	      break;
	    }
	  arg = NULL;
	}
      else
	{
	  fputc (*ip, fp);
	  if (*ip == '\r')
	    {
	      len = 0;
	      tty_cleanup = 1;
	    }
	  else
	    len++;
	}
    }
  fflush (fp);
  return len;
}
Example #12
0
/* Learns a new lexicon.  */
static Lexicon *
learn (Ustr const *fname)
{
  Lexicon *rv;
  AUstr words;
  Ustr *line;
  FILE *lexicon;

  /* Initialize variables.  */
  rv = malloc (sizeof (Lexicon));
  if (!rv)
    abort ();
  austr_init (&words);
  line = ustr_dup_empty ();
  if (!line)
    abort ();
  lexicon = fopen (ustr_cstr (fname), "r");
  if (!lexicon)
    {
      perror ("galcry-backend, lexicon, learn");
      abort ();
    }

  /* Clear histogram.  */
  memset ((void *) rv, 0, sizeof (Lexicon));

  /* Learn words.  */
  while (errno = 0, ustr_sc_del (&line), ustr_io_getline (&line, lexicon))
    {
      unsigned int i;

      /* Skip comment lines.  */
      if (ustr_cstr (line)[0] == '#')
	continue;

      wordsplit (&words, line);
      for (i = 0; i < austr_length (&words); ++i)
	{
	  unsigned int l;
	  char const *s;

	  l = ustr_len (austr_i (&words, i));
	  if (l < 2)
	    continue;
	  s = ustr_cstr (austr_i (&words, i));

	  {
	    unsigned int i;
	    unsigned int c0, c1, c2;
	    c0 = 0;
	    c1 = (s[0] % 32) % 27;
	    ++rv->histogram2[c0][c1];
	    for (i = 1; i < l + 1; ++i)
	      {
		c2 = (s[i] % 32) % 27;
		++rv->histogram3[c0][c1][c2];
		++rv->histogram2[c1][c2];
		c0 = c1;
		c1 = c2;
	      }
	  }
	}
    }
  if (errno != 0)
    {
      perror ("galcry-backend, lexicon, learn, read");
      abort ();
    }

  fclose (lexicon);
  ustr_sc_free (&line);
  austr_deinit (&words);
  return rv;
}