blob: 095d2d0685730695d27dc96bc7dab4ed68b8f93f [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
15#include "config.h"
16
17#include <stdlib.h>
18#include <stdio.h>
19#include <string.h>
20#include <errno.h>
21#include <unistd.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24#include <pwd.h>
25
26#include "configuration.h"
27#include "./linenoise/linenoise.h"
28
29/* Yanglint home (appended to ~/) */
30#define YL_DIR ".yanglint"
31
32char *
33get_yanglint_dir(void)
34{
35 int ret;
36 struct passwd *pw;
37 char *user_home, *yl_dir;
38
39 if (!(pw = getpwuid(getuid()))) {
40 fprintf(stderr, "Determining home directory failed (%s).\n", strerror(errno));
41 return NULL;
42 }
43 user_home = pw->pw_dir;
44
45 yl_dir = malloc(strlen(user_home) + 1 + strlen(YL_DIR) + 1);
46 if (!yl_dir) {
47 fprintf(stderr, "Memory allocation failed (%s).\n", strerror(errno));
48 return NULL;
49 }
50 sprintf(yl_dir, "%s/%s", user_home, YL_DIR);
51
52 ret = access(yl_dir, R_OK | X_OK);
53 if (ret == -1) {
54 if (errno == ENOENT) {
55 /* directory does not exist */
56 fprintf(stdout, "Configuration directory \"%s\" does not exist, creating it.\n", yl_dir);
57 if (mkdir(yl_dir, 00700)) {
58 fprintf(stderr, "Configuration directory \"%s\" cannot be created (%s).\n", yl_dir, strerror(errno));
59 free(yl_dir);
60 return NULL;
61 }
62 } else {
63 fprintf(stderr, "Configuration directory \"%s\" exists but cannot be accessed (%s).\n", yl_dir, strerror(errno));
64 free(yl_dir);
65 return NULL;
66 }
67 }
68
69 return yl_dir;
70}
71
72void
73load_config(void)
74{
75 char *yl_dir, *history_file;
76 if ((yl_dir = get_yanglint_dir()) == NULL) {
77 return;
78 }
79
80 history_file = malloc(strlen(yl_dir) + 9);
81 if (!history_file) {
82 fprintf(stderr, "Memory allocation failed (%s).\n", strerror(errno));
83 free(yl_dir);
84 return;
85 }
86
87 sprintf(history_file, "%s/history", yl_dir);
88 if (access(history_file, F_OK) && (errno == ENOENT)) {
89 fprintf(stdout, "No saved history.\n");
90 } else if (linenoiseHistoryLoad(history_file)) {
91 fprintf(stderr, "Failed to load history.\n");
92 }
93
94 free(history_file);
95 free(yl_dir);
96}
97
98void
99store_config(void)
100{
101 char *yl_dir, *history_file;
102
103 if ((yl_dir = get_yanglint_dir()) == NULL) {
104 return;
105 }
106
107 history_file = malloc(strlen(yl_dir) + 9);
108 if (!history_file) {
109 fprintf(stderr, "Memory allocation failed (%s).\n", strerror(errno));
110 free(yl_dir);
111 return;
112 }
113
114 sprintf(history_file, "%s/history", yl_dir);
115 if (linenoiseHistorySave(history_file)) {
116 fprintf(stderr, "Failed to save history.\n");
117 }
118
119 free(history_file);
120 free(yl_dir);
121}