NeoMutt  2020-08-07-1-gab41a1
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 "path.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...
 
void path_init (struct ConfigSet *cs)
 Register the Path config type. More...
 

Variables

char * HomeDir
 User's home directory. 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 56 of file path.c.

57 {
58  if (!path || (*path == '\0'))
59  return NULL;
60 
61  char buf[PATH_MAX] = { 0 };
62  mutt_str_copy(buf, path, sizeof(buf));
63 
64  mutt_path_tilde(buf, sizeof(buf), HomeDir);
65  mutt_path_tidy(buf, is_dir);
66 
67  return mutt_str_dup(buf);
68 }
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:375
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:721
+ 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 73 of file path.c.

74 {
75  const char **str = (const char **) var;
76  if (!*str)
77  return;
78 
79  FREE(var);
80 }
#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 85 of file path.c.

87 {
88  /* Store empty paths as NULL */
89  if (value && (value[0] == '\0'))
90  value = NULL;
91 
92  if (!value && (cdef->type & DT_NOT_EMPTY))
93  {
94  mutt_buffer_printf(err, _("Option %s may not be empty"), cdef->name);
96  }
97 
98  int rc = CSR_SUCCESS;
99 
100  if (var)
101  {
102  if (mutt_str_equal(value, (*(char **) var)))
104 
105  if (cdef->validator)
106  {
107  rc = cdef->validator(cs, cdef, (intptr_t) value, err);
108 
109  if (CSR_RESULT(rc) != CSR_SUCCESS)
110  return rc | CSR_INV_VALIDATOR;
111  }
112 
113  path_destroy(cs, var, cdef);
114 
115  char *str = path_tidy(value, cdef->type & DT_PATH_DIR);
116  if (!str)
117  rc |= CSR_SUC_EMPTY;
118 
119  *(char **) var = str;
120  }
121  else
122  {
123  if (cdef->type & DT_INITIAL_SET)
124  FREE(&cdef->initial);
125 
126  cdef->type |= DT_INITIAL_SET;
127  cdef->initial = IP mutt_str_dup(value);
128  }
129 
130  return rc;
131 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:876
#define DT_NOT_EMPTY
Empty strings are not allowed.
Definition: types.h:46
#define CSR_RESULT(x)
Definition: set.h:52
intptr_t initial
Initial value.
Definition: set.h:66
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:375
#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
const char * name
User-visible name.
Definition: set.h:63
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate a config variable.
Definition: set.h:78
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:38
#define CSR_SUC_EMPTY
Value is empty/unset.
Definition: set.h:42
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
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:73
#define IP
Definition: set.h:54
#define FREE(x)
Definition: memory.h:40
#define CSR_SUC_NO_CHANGE
The value hasn&#39;t changed.
Definition: set.h:44
static char * path_tidy(const char *path, bool is_dir)
Tidy a path for storage.
Definition: path.c:56
+ Here is the call graph for this function:
+ Here is the caller 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 136 of file path.c.

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

158 {
159  char *str = (char *) value;
160 
161  /* Store empty paths as NULL */
162  if (str && (str[0] == '\0'))
163  value = 0;
164 
165  if ((value == 0) && (cdef->type & DT_NOT_EMPTY))
166  {
167  mutt_buffer_printf(err, _("Option %s may not be empty"), cdef->name);
169  }
170 
171  if (mutt_str_equal((const char *) value, (*(char **) var)))
173 
174  int rc;
175 
176  if (cdef->validator)
177  {
178  rc = cdef->validator(cs, cdef, value, err);
179 
180  if (CSR_RESULT(rc) != CSR_SUCCESS)
181  return rc | CSR_INV_VALIDATOR;
182  }
183 
184  path_destroy(cs, var, cdef);
185 
186  str = path_tidy(str, cdef->type & DT_PATH_DIR);
187  rc = CSR_SUCCESS;
188  if (!str)
189  rc |= CSR_SUC_EMPTY;
190 
191  *(const char **) var = str;
192  return rc;
193 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:876
#define DT_NOT_EMPTY
Empty strings are not allowed.
Definition: types.h:46
#define CSR_RESULT(x)
Definition: set.h:52
#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
const char * name
User-visible name.
Definition: set.h:63
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate a config variable.
Definition: set.h:78
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:38
#define CSR_SUC_EMPTY
Value is empty/unset.
Definition: set.h:42
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
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:73
#define CSR_SUC_NO_CHANGE
The value hasn&#39;t changed.
Definition: set.h:44
static char * path_tidy(const char *path, bool is_dir)
Tidy a path for storage.
Definition: path.c:56
+ Here is the call graph for this function:
+ Here is the caller 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 198 of file path.c.

200 {
201  const char *str = *(const char **) var;
202 
203  return (intptr_t) str;
204 }
+ Here is the caller graph for this function:

◆ 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 209 of file path.c.

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

◆ path_init()

void path_init ( struct ConfigSet cs)

Register the Path config type.

Parameters
csConfig items

Definition at line 248 of file path.c.

249 {
250  const struct ConfigSetType cst_path = {
251  "path",
256  NULL, // string_plus_equals
257  NULL, // string_minus_equals
258  path_reset,
259  path_destroy,
260  };
261  cs_register_type(cs, DT_PATH, &cst_path);
262 }
Type definition for a config item.
Definition: set.h:88
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:85
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:136
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:198
#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:156
static void path_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
Destroy a Path - Implements ConfigSetType::destroy()
Definition: path.c:73
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:209
bool cs_register_type(struct ConfigSet *cs, unsigned int type, const struct ConfigSetType *cst)
Register a type of config item.
Definition: set.c:259
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ HomeDir

char* HomeDir

User's home directory.

Definition at line 49 of file mutt_globals.h.