summaryrefslogtreecommitdiffstats
path: root/tools/lint/configuration.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-07-23 09:41:28 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-07-23 11:00:43 +0000
commitd0b1bae8c5c70c5d06f3dcecc450a75e7f7cb5af (patch)
tree7ea7c5e622a5d7c9c989057a1eca8954c4d7fefb /tools/lint/configuration.c
parentInitial commit. (diff)
downloadlibyang3-d0b1bae8c5c70c5d06f3dcecc450a75e7f7cb5af.tar.xz
libyang3-d0b1bae8c5c70c5d06f3dcecc450a75e7f7cb5af.zip
Adding upstream version 3.1.0+dfsg.upstream/3.1.0+dfsg
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/lint/configuration.c')
-rw-r--r--tools/lint/configuration.c125
1 files changed, 125 insertions, 0 deletions
diff --git a/tools/lint/configuration.c b/tools/lint/configuration.c
new file mode 100644
index 0000000..e3db668
--- /dev/null
+++ b/tools/lint/configuration.c
@@ -0,0 +1,125 @@
+/**
+ * @file configuration.c
+ * @author Michal Vasko <mvasko@cesnet.cz>
+ * @brief yanglint configuration
+ *
+ * Copyright (c) 2017 CESNET, z.s.p.o.
+ *
+ * This source code is licensed under BSD 3-Clause License (the "License").
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://opensource.org/licenses/BSD-3-Clause
+ */
+
+#include "configuration.h"
+
+#include <errno.h>
+#include <pwd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "linenoise/linenoise.h"
+
+#include "common.h"
+
+/* Yanglint home (appended to ~/) */
+#define YL_DIR ".yanglint"
+
+char *
+get_yanglint_dir(void)
+{
+ int ret;
+ struct passwd *pw;
+ char *user_home, *yl_dir;
+
+ if (!(pw = getpwuid(getuid()))) {
+ YLMSG_E("Determining home directory failed (%s).", strerror(errno));
+ return NULL;
+ }
+ user_home = pw->pw_dir;
+
+ yl_dir = malloc(strlen(user_home) + 1 + strlen(YL_DIR) + 1);
+ if (!yl_dir) {
+ YLMSG_E("Memory allocation failed (%s).", strerror(errno));
+ return NULL;
+ }
+ sprintf(yl_dir, "%s/%s", user_home, YL_DIR);
+
+ ret = access(yl_dir, R_OK | X_OK);
+ if (ret == -1) {
+ if (errno == ENOENT) {
+ /* directory does not exist */
+ YLMSG_W("Configuration directory \"%s\" does not exist, creating it.", yl_dir);
+ if (mkdir(yl_dir, 00700)) {
+ if (errno != EEXIST) {
+ /* parallel execution, yay */
+ YLMSG_E("Configuration directory \"%s\" cannot be created (%s).", yl_dir, strerror(errno));
+ free(yl_dir);
+ return NULL;
+ }
+ }
+ } else {
+ YLMSG_E("Configuration directory \"%s\" exists but cannot be accessed (%s).", yl_dir, strerror(errno));
+ free(yl_dir);
+ return NULL;
+ }
+ }
+
+ return yl_dir;
+}
+
+void
+load_config(void)
+{
+ char *yl_dir, *history_file;
+
+ if ((yl_dir = get_yanglint_dir()) == NULL) {
+ return;
+ }
+
+ history_file = malloc(strlen(yl_dir) + 9);
+ if (!history_file) {
+ YLMSG_E("Memory allocation failed (%s).", strerror(errno));
+ free(yl_dir);
+ return;
+ }
+
+ sprintf(history_file, "%s/history", yl_dir);
+ if (access(history_file, F_OK) && (errno == ENOENT)) {
+ YLMSG_W("No saved history.");
+ } else if (linenoiseHistoryLoad(history_file)) {
+ YLMSG_E("Failed to load history.");
+ }
+
+ free(history_file);
+ free(yl_dir);
+}
+
+void
+store_config(void)
+{
+ char *yl_dir, *history_file;
+
+ if ((yl_dir = get_yanglint_dir()) == NULL) {
+ return;
+ }
+
+ history_file = malloc(strlen(yl_dir) + 9);
+ if (!history_file) {
+ YLMSG_E("Memory allocation failed (%s).", strerror(errno));
+ free(yl_dir);
+ return;
+ }
+
+ sprintf(history_file, "%s/history", yl_dir);
+ if (linenoiseHistorySave(history_file)) {
+ YLMSG_E("Failed to save history.");
+ }
+
+ free(history_file);
+ free(yl_dir);
+}