summaryrefslogtreecommitdiff
path: root/libmailutils/mailcap/finder.c
blob: b303412a8dfd3cac6586893f1b4f6d451c56cf18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999-2020 Free Software Foundation, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   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 library.  If not, see
   <http://www.gnu.org/licenses/>. */

#include <config.h>
#include <stdlib.h>
#include <unistd.h>
#include <mailutils/sys/mailcap.h>
#include <mailutils/iterator.h>

static int
finder_init (mu_mailcap_finder_t finder,
	     int flags,
	     struct mu_mailcap_selector_closure *sc,
	     struct mu_mailcap_error_closure *ec,
	     char **file_names);

int
mu_mailcap_finder_create (mu_mailcap_finder_t *pfinder, int flags,
			  struct mu_mailcap_selector_closure *sc,
			  struct mu_mailcap_error_closure *ec,
			  char **file_names)
{
  int rc;
  mu_mailcap_finder_t finder;

  if (!pfinder)
    return MU_ERR_OUT_PTR_NULL;
  if (!file_names)
    return EINVAL;
  finder = calloc (1, sizeof *finder);
  if (!finder)
    return ENOMEM;
  rc = finder_init (finder, flags, sc, ec, file_names);
  if (rc)
    mu_mailcap_finder_destroy (&finder);
  else
    *pfinder = finder;
  return rc;
}

static int
finder_init (mu_mailcap_finder_t finder,
	     int flags,
	     struct mu_mailcap_selector_closure *sc,
	     struct mu_mailcap_error_closure *ec,
	     char **file_names)
{
  int rc;
  size_t i;

  rc = mu_mailcap_create (&finder->mcp);
  if (rc)
    return rc;
  mu_mailcap_set_flags (finder->mcp, flags);
  if (sc)
    {
      rc = mu_mailcap_set_selector (finder->mcp, sc);
      if (rc)
	return rc;
    }
  if (ec)
    {
      rc = mu_mailcap_set_error (finder->mcp, ec);
      if (rc)
	return rc;
    }

  for (i = 0; file_names[i]; i++)
    {
      if (access (file_names[i], F_OK))
	continue;
      mu_mailcap_parse_file (finder->mcp, file_names[i]);
    }

  return 0;
}

void
mu_mailcap_finder_destroy (mu_mailcap_finder_t *pfinder)
{
  if (pfinder && *pfinder)
    {
      mu_mailcap_finder_t finder = *pfinder;
      mu_iterator_destroy (&finder->itr);
      mu_mailcap_destroy (&finder->mcp);
      free (finder);
      *pfinder = NULL;
    }
}

int
mu_mailcap_finder_next_match (mu_mailcap_finder_t finder,
			      mu_mailcap_entry_t *ret_entry)
{
  int rc;

  if (!finder)
    return EINVAL;
  if (!ret_entry)
    return MU_ERR_OUT_PTR_NULL;
  if (!finder->itr)
    {
      rc = mu_mailcap_get_iterator (finder->mcp, &finder->itr);
      if (rc == 0)
	rc = mu_iterator_first (finder->itr);
    }
  else
    rc = mu_iterator_next (finder->itr);

  if (rc)
    return rc;

  if (mu_iterator_is_done (finder->itr))
    return MU_ERR_NOENT;

  return mu_iterator_current (finder->itr, (void**) ret_entry);
}

Return to:

Send suggestions and report system problems to the System administrator.