Blender V5.3
autoexec.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include <cstdlib>
13#include <cstring>
14
15#include "DNA_userdef_types.h"
16
17#include "BLI_fnmatch.hh"
18#include "BLI_path_utils.hh"
19#include "BLI_string.hh"
20
21#include "BKE_autoexec.hh" /* own include */
22#include "BKE_global.hh"
23
24namespace blender {
25
29static const char *autoexec_dirpath_resolve(const char *path,
30 const bool canonicalize,
31 const bool strip_filename,
32 char *dirpath,
33 const size_t dirpath_maxncpy)
34{
35 if (!(canonicalize || strip_filename)) {
36 return path;
37 }
38
39 const size_t path_len = BLI_strncpy_rlen(dirpath, path, dirpath_maxncpy);
40 /* A path ending with a slash is already a directory & has no file name to strip,
41 * canonicalizing removes the slash so this must be checked first. */
42 const bool is_dirpath = path_len && BLI_path_slash_is_native_compat(dirpath[path_len - 1]);
43 const bool do_strip_filename = strip_filename && !is_dirpath;
44
45 if (canonicalize) {
46 BLI_path_canonicalize_native(dirpath, dirpath_maxncpy);
47 if (!do_strip_filename) {
48 /* Canonicalizing removed the trailing slash. */
49 BLI_path_slash_ensure(dirpath, dirpath_maxncpy);
50 }
51 }
52 if (do_strip_filename) {
53 /* Truncating at the file name leaves the trailing slash. */
54 *const_cast<char *>(BLI_path_basename(dirpath)) = '\0';
55 }
56 return dirpath;
57}
58
59bool BKE_autoexec_match_unchecked(const char *path,
60 const bool canonicalize,
61 const bool strip_filename)
62{
63 /* The caller must never pass in an empty path. */
64 BLI_assert(path[0] != '\0');
65
66 bPathCompare *path_cmp;
67
68 char dirpath_buf[FILE_MAX];
69 const char *dirpath = autoexec_dirpath_resolve(
70 path, canonicalize, strip_filename, dirpath_buf, sizeof(dirpath_buf));
71
72#ifdef WIN32
73 const int fnmatch_flags = FNM_CASEFOLD;
74#else
75 const int fnmatch_flags = 0;
76#endif
77
78 for (path_cmp = U.autoexec_paths.first(); path_cmp; path_cmp = path_cmp->next) {
79 if (path_cmp->path[0] == '\0') {
80 /* pass */
81 }
82 else if (path_cmp->flag & USER_PATHCMP_GLOB) {
83 if (fnmatch(path_cmp->path, dirpath, fnmatch_flags) == 0) {
84 return true;
85 }
86 }
87 else if (BLI_path_ncmp(path_cmp->path, dirpath, strlen(path_cmp->path)) == 0) {
88 return true;
89 }
90 }
91
92 return false;
93}
94
95bool BKE_autoexec_match(const char *path, const bool canonicalize, const bool strip_filename)
96{
97 /* Auto-execution must be enabled by the preference or trusted for this session. */
99
100 return BKE_autoexec_match_unchecked(path, canonicalize, strip_filename);
101}
102
103bool BKE_autoexec_default_trust_source(const char *path, const AutoExec_Params &params)
104{
105 if (!params.skip_overrides) {
106 if (G.autoexec_override.has_value()) {
107 /* The command line forced auto-execution on or off, excluded paths don't apply. */
108 return *G.autoexec_override;
109 }
110 if (U.flag & USER_SCRIPT_AUTOEXEC_DISABLE) {
111 /* Nothing is trusted by default, the user may still opt-in. */
112 return false;
113 }
114 }
115
116 return !BKE_autoexec_match_unchecked(path, params.canonicalize, params.strip_filename);
117}
118
119} // namespace blender
#define BLI_path_ncmp
#define FILE_MAX
BMesh Iterators.
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
BLI_INLINE bool BLI_path_slash_is_native_compat(const char ch)
Global G
Definition blender.cc:57
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
int BLI_path_canonicalize_native(char *path, int path_maxncpy)
bool BKE_autoexec_match_unchecked(const char *path, bool canonicalize, bool strip_filename)
Definition autoexec.cc:59
char char size_t BLI_strncpy_rlen(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
int BLI_path_slash_ensure(char *path, size_t path_maxncpy) ATTR_NONNULL(1)
static const char * autoexec_dirpath_resolve(const char *path, const bool canonicalize, const bool strip_filename, char *dirpath, const size_t dirpath_maxncpy)
Definition autoexec.cc:29
@ G_FLAG_SCRIPT_AUTOEXEC
bool BKE_autoexec_default_trust_source(const char *path, const AutoExec_Params &params)
Definition autoexec.cc:103
@ USER_SCRIPT_AUTOEXEC_DISABLE
bool BKE_autoexec_match(const char *path, bool canonicalize, bool strip_filename)
Definition autoexec.cc:95
struct bPathCompare * next