NeoMutt  2020-09-25
Teaching an old dog new tricks
DOXYGEN
path.c File Reference

Type representing a path. More...

#include "config.h"
#include <stddef.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include "mutt/lib.h"
#include "set.h"
#include "types.h"
+ Include dependency graph for path.c:

Go to the source code of this file.

Functions

static char * path_tidy (const char *path, bool is_dir)
 Tidy a path for storage. More...
 
static void path_destroy (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
 Destroy a Path - Implements ConfigSetType::destroy() More...
 
static int path_string_set (const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
 Set a Path by string - Implements ConfigSetType::string_set() More...
 
static int path_string_get (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
 Get a Path as a string - Implements ConfigSetType::string_get() More...
 
static int path_native_set (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
 Set a Path config item by string - Implements ConfigSetType::native_set() More...
 
static intptr_t path_native_get (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
 Get a string from a Path config item - Implements ConfigSetType::native_get() More...
 
static int path_reset (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
 Reset a Path to its initial value - Implements ConfigSetType::reset() More...
 

Variables

char * HomeDir
 User's home directory. More...
 
const struct ConfigSetType cst_path
 Config type representing a path. More...
 

Detailed Description

Type representing a path.

Authors
  • Richard Russon

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file path.c.

Function Documentation

◆ path_tidy()

static char* path_tidy ( const char *  path,
bool  is_dir 
)
static

Tidy a path for storage.

Parameters
pathPath to be tidied
is_dirIs the path a directory?
Return values
ptrTidy path

Expand ~ and remove junk like /./

Note
The caller must free the returned string

Definition at line 55 of file path.c.

56 {
57  if (!path || (*path == '\0'))
58  return NULL;
59 
60  char buf[PATH_MAX] = { 0 };
61  mutt_str_copy(buf, path, sizeof(buf));
62 
63  mutt_path_tilde(buf, sizeof(buf), HomeDir);
64  mutt_path_tidy(buf, is_dir);
65 
66  return mutt_str_dup(buf);
67 }
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:370
bool mutt_path_tidy(char *buf, bool is_dir)
Remove unnecessary parts of a path.
Definition: path.c:165
char * HomeDir
User&#39;s home directory.
Definition: mutt_globals.h:49
bool mutt_path_tilde(char *buf, size_t buflen, const char *homedir)
Expand &#39;~&#39; in a path.
Definition: path.c:223
#define PATH_MAX
Definition: mutt.h:44
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:716
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ path_destroy()

static void path_destroy ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef 
)
static

Destroy a Path - Implements ConfigSetType::destroy()

Definition at line 72 of file path.c.

73 {
74  const char **str = (const char **) var;
75  if (!*str)
76  return;
77 
78  FREE(var);
79 }
#define FREE(x)
Definition: memory.h:40
+ Here is the caller graph for this function:

◆ path_string_set()

static int path_string_set ( const struct ConfigSet cs,
void *  var,
struct ConfigDef cdef,
const char *  value,
struct Buffer err 
)
static

Set a Path by string - Implements ConfigSetType::string_set()

Definition at line 84 of file path.c.

86 {
87  /* Store empty paths as NULL */
88  if (value && (value[0] == '\0'))
89  value = NULL;
90 
91  if (!value && (cdef->type & DT_NOT_EMPTY))
92  {
93  mutt_buffer_printf(err, _("Option %s may not be empty"), cdef->name);
95  }
96 
97  int rc = CSR_SUCCESS;
98 
99  if (var)
100  {
101  if (mutt_str_equal(value, (*(char **) var)))
103 
104  if (cdef->validator)
105  {
106  rc = cdef->validator(cs, cdef, (intptr_t) value, err);
107 
108  if (CSR_RESULT(rc) != CSR_SUCCESS)
109  return rc | CSR_INV_VALIDATOR;
110  }
111 
112  path_destroy(cs, var, cdef);
113 
114  char *str = path_tidy(value, cdef->type & DT_PATH_DIR);
115  if (!str)
116  rc |= CSR_SUC_EMPTY;
117 
118  *(char **) var = str;
119  }
120  else
121  {
122  if (cdef->type & DT_INITIAL_SET)
123  FREE(&cdef->initial);
124 
125  cdef->type |= DT_INITIAL_SET;
126  cdef->initial = IP mutt_str_dup(value);
127  }
128 
129  return rc;
130 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:871
#define IP
Definition: set.h:54
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:38
#define DT_NOT_EMPTY
Empty strings are not allowed.
Definition: types.h:46
intptr_t initial
Initial value.
Definition: set.h:66
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:370
#define DT_INITIAL_SET
Config item must have its initial value freed.
Definition: types.h:81
#define _(a)
Definition: message.h:28
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
#define DT_PATH_DIR
Path is a directory.
Definition: types.h:53
#define CSR_RESULT(x)
Definition: set.h:52
#define CSR_SUC_EMPTY
Value is empty/unset.
Definition: set.h:42
#define CSR_SUC_NO_CHANGE
The value hasn&#39;t changed.
Definition: set.h:44
const char * name
User-visible name.
Definition: set.h:63
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate a config variable.
Definition: set.h:78
unsigned int type
Variable type, e.g. DT_STRING.
Definition: set.h:64
static void path_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
Destroy a Path - Implements ConfigSetType::destroy()
Definition: path.c:72
#define FREE(x)
Definition: memory.h:40
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
static char * path_tidy(const char *path, bool is_dir)
Tidy a path for storage.
Definition: path.c:55
+ Here is the call graph for this function:

◆ path_string_get()

static int path_string_get ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
struct Buffer result 
)
static

Get a Path as a string - Implements ConfigSetType::string_get()

Definition at line 135 of file path.c.

137 {
138  const char *str = NULL;
139 
140  if (var)
141  str = *(const char **) var;
142  else
143  str = (char *) cdef->initial;
144 
145  if (!str)
146  return CSR_SUCCESS | CSR_SUC_EMPTY; /* empty path */
147 
148  mutt_buffer_addstr(result, str);
149  return CSR_SUCCESS;
150 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
intptr_t initial
Initial value.
Definition: set.h:66
#define CSR_SUC_EMPTY
Value is empty/unset.
Definition: set.h:42
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
+ Here is the call graph for this function:

◆ path_native_set()

static int path_native_set ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
intptr_t  value,
struct Buffer err 
)
static

Set a Path config item by string - Implements ConfigSetType::native_set()

Definition at line 155 of file path.c.

157 {
158  char *str = (char *) value;
159 
160  /* Store empty paths as NULL */
161  if (str && (str[0] == '\0'))
162  value = 0;
163 
164  if ((value == 0) && (cdef->type & DT_NOT_EMPTY))
165  {
166  mutt_buffer_printf(err, _("Option %s may not be empty"), cdef->name);
168  }
169 
170  if (mutt_str_equal((const char *) value, (*(char **) var)))
172 
173  int rc;
174 
175  if (cdef->validator)
176  {
177  rc = cdef->validator(cs, cdef, value, err);
178 
179  if (CSR_RESULT(rc) != CSR_SUCCESS)
180  return rc | CSR_INV_VALIDATOR;
181  }
182 
183  path_destroy(cs, var, cdef);
184 
185  str = path_tidy(str, cdef->type & DT_PATH_DIR);
186  rc = CSR_SUCCESS;
187  if (!str)
188  rc |= CSR_SUC_EMPTY;
189 
190  *(const char **) var = str;
191  return rc;
192 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:871
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:38
#define DT_NOT_EMPTY
Empty strings are not allowed.
Definition: types.h:46
#define _(a)
Definition: message.h:28
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
#define DT_PATH_DIR
Path is a directory.
Definition: types.h:53
#define CSR_RESULT(x)
Definition: set.h:52
#define CSR_SUC_EMPTY
Value is empty/unset.
Definition: set.h:42
#define CSR_SUC_NO_CHANGE
The value hasn&#39;t changed.
Definition: set.h:44
const char * name
User-visible name.
Definition: set.h:63
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate a config variable.
Definition: set.h:78
unsigned int type
Variable type, e.g. DT_STRING.
Definition: set.h:64
static void path_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
Destroy a Path - Implements ConfigSetType::destroy()
Definition: path.c:72
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
static char * path_tidy(const char *path, bool is_dir)
Tidy a path for storage.
Definition: path.c:55
+ Here is the call graph for this function:

◆ path_native_get()

static intptr_t path_native_get ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
struct Buffer err 
)
static

Get a string from a Path config item - Implements ConfigSetType::native_get()

Definition at line 197 of file path.c.

199 {
200  const char *str = *(const char **) var;
201 
202  return (intptr_t) str;
203 }

◆ path_reset()

static int path_reset ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
struct Buffer err 
)
static

Reset a Path to its initial value - Implements ConfigSetType::reset()

Definition at line 208 of file path.c.

210 {
211  int rc = CSR_SUCCESS;
212 
213  const char *str = path_tidy((const char *) cdef->initial, cdef->type & DT_PATH_DIR);
214  if (!str)
215  rc |= CSR_SUC_EMPTY;
216 
217  if (mutt_str_equal(str, (*(char **) var)))
218  {
219  FREE(&str);
220  return rc | CSR_SUC_NO_CHANGE;
221  }
222 
223  if (cdef->validator)
224  {
225  rc = cdef->validator(cs, cdef, cdef->initial, err);
226 
227  if (CSR_RESULT(rc) != CSR_SUCCESS)
228  {
229  FREE(&str);
230  return rc | CSR_INV_VALIDATOR;
231  }
232  }
233 
234  path_destroy(cs, var, cdef);
235 
236  if (!str)
237  rc |= CSR_SUC_EMPTY;
238 
239  *(const char **) var = str;
240  return rc;
241 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:871
intptr_t initial
Initial value.
Definition: set.h:66
#define DT_PATH_DIR
Path is a directory.
Definition: types.h:53
#define CSR_RESULT(x)
Definition: set.h:52
#define CSR_SUC_EMPTY
Value is empty/unset.
Definition: set.h:42
#define CSR_SUC_NO_CHANGE
The value hasn&#39;t changed.
Definition: set.h:44
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate a config variable.
Definition: set.h:78
unsigned int type
Variable type, e.g. DT_STRING.
Definition: set.h:64
static void path_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
Destroy a Path - Implements ConfigSetType::destroy()
Definition: path.c:72
#define FREE(x)
Definition: memory.h:40
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
static char * path_tidy(const char *path, bool is_dir)
Tidy a path for storage.
Definition: path.c:55
+ Here is the call graph for this function:

Variable Documentation

◆ HomeDir

char* HomeDir

User's home directory.

Definition at line 49 of file mutt_globals.h.

◆ cst_path

const struct ConfigSetType cst_path
Initial value:
= {
"path",
NULL,
NULL,
}
static int path_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a Path by string - Implements ConfigSetType::string_set()
Definition: path.c:84
static int path_string_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a Path as a string - Implements ConfigSetType::string_get()
Definition: path.c:135
static intptr_t path_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get a string from a Path config item - Implements ConfigSetType::native_get()
Definition: path.c:197
#define DT_PATH
a path to a file/directory
Definition: types.h:36
static int path_native_set(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set a Path config item by string - Implements ConfigSetType::native_set()
Definition: path.c:155
static void path_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
Destroy a Path - Implements ConfigSetType::destroy()
Definition: path.c:72
static int path_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a Path to its initial value - Implements ConfigSetType::reset()
Definition: path.c:208

Config type representing a path.

Definition at line 246 of file path.c.