blob: 5bda4ed3436fca9ac7fed00f8703491a1d7fbf46 [file] [log] [blame]
Radek Krejcied5acc52019-04-25 15:57:04 +02001/**
2 * @file configuration.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief yanglint configuration
5 *
6 * Copyright (c) 2017 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejcied5acc52019-04-25 15:57:04 +020015#include "configuration.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020016
17#include <errno.h>
18#include <pwd.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <unistd.h>
24
25#include "linenoise/linenoise.h"
Radek Krejcied5acc52019-04-25 15:57:04 +020026
27/* Yanglint home (appended to ~/) */
28#define YL_DIR ".yanglint"
29
30char *
31get_yanglint_dir(void)
32{
33 int ret;
34 struct passwd *pw;
35 char *user_home, *yl_dir;
36
37 if (!(pw = getpwuid(getuid()))) {
38 fprintf(stderr, "Determining home directory failed (%s).\n", strerror(errno));
39 return NULL;
40 }
41 user_home = pw->pw_dir;
42
43 yl_dir = malloc(strlen(user_home) + 1 + strlen(YL_DIR) + 1);
44 if (!yl_dir) {
45 fprintf(stderr, "Memory allocation failed (%s).\n", strerror(errno));
46 return NULL;
47 }
48 sprintf(yl_dir, "%s/%s", user_home, YL_DIR);
49
50 ret = access(yl_dir, R_OK | X_OK);
51 if (ret == -1) {
52 if (errno == ENOENT) {
53 /* directory does not exist */
54 fprintf(stdout, "Configuration directory \"%s\" does not exist, creating it.\n", yl_dir);
55 if (mkdir(yl_dir, 00700)) {
56 fprintf(stderr, "Configuration directory \"%s\" cannot be created (%s).\n", yl_dir, strerror(errno));
57 free(yl_dir);
58 return NULL;
59 }
60 } else {
61 fprintf(stderr, "Configuration directory \"%s\" exists but cannot be accessed (%s).\n", yl_dir, strerror(errno));
62 free(yl_dir);
63 return NULL;
64 }
65 }
66
67 return yl_dir;
68}
69
70void
71load_config(void)
72{
73 char *yl_dir, *history_file;
74 if ((yl_dir = get_yanglint_dir()) == NULL) {
75 return;
76 }
77
78 history_file = malloc(strlen(yl_dir) + 9);
79 if (!history_file) {
80 fprintf(stderr, "Memory allocation failed (%s).\n", strerror(errno));
81 free(yl_dir);
82 return;
83 }
84
85 sprintf(history_file, "%s/history", yl_dir);
86 if (access(history_file, F_OK) && (errno == ENOENT)) {
87 fprintf(stdout, "No saved history.\n");
88 } else if (linenoiseHistoryLoad(history_file)) {
89 fprintf(stderr, "Failed to load history.\n");
90 }
91
92 free(history_file);
93 free(yl_dir);
94}
95
96void
97store_config(void)
98{
99 char *yl_dir, *history_file;
100
101 if ((yl_dir = get_yanglint_dir()) == NULL) {
102 return;
103 }
104
105 history_file = malloc(strlen(yl_dir) + 9);
106 if (!history_file) {
107 fprintf(stderr, "Memory allocation failed (%s).\n", strerror(errno));
108 free(yl_dir);
109 return;
110 }
111
112 sprintf(history_file, "%s/history", yl_dir);
113 if (linenoiseHistorySave(history_file)) {
114 fprintf(stderr, "Failed to save history.\n");
115 }
116
117 free(history_file);
118 free(yl_dir);
119}