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