166 lines
3.0 KiB
C
166 lines
3.0 KiB
C
|
/*
|
||
|
* libglacier.c - Backend C library for Glacier
|
||
|
*
|
||
|
* This file is part of Glacier.
|
||
|
*
|
||
|
* Glacier is free software: you can redistribute it and/or modify it under the terms of the
|
||
|
* GNU General Public License as published by the Free Software Foundation, either
|
||
|
* version 3 of the License, or (at your option) any later version.
|
||
|
*
|
||
|
* Glacier is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||
|
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License along with Glacier. If
|
||
|
* not, see <https://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
|
||
|
#include <color.h>
|
||
|
#include <libconfig.h>
|
||
|
#include <stdbool.h>
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
/* Global variables */
|
||
|
|
||
|
config_t cfg;
|
||
|
config_setting_t *setting;
|
||
|
const char str;
|
||
|
int GLACIER_ALLOW_SERVICES;
|
||
|
char GLACIER_ALLOWED_LICENSES;
|
||
|
|
||
|
const char *runtime_files[] = { "/etc/glacier.cfg", "/etc/glacier/call-hooks", "/etc/make.conf" };
|
||
|
|
||
|
/*
|
||
|
* infolog
|
||
|
*
|
||
|
* DESCRIPTION: Output a stylized info message.
|
||
|
* PARAMETERS: char MSG[]
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
void
|
||
|
infolog(char MSG[])
|
||
|
{
|
||
|
if (MSG == NULL) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
printf(COL_BLUE "[i]" COL_RESET " %s\n", MSG);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* warnlog
|
||
|
*
|
||
|
* DESCRIPTION: Output a stylized warning message.
|
||
|
* Parameters: char MSG[]
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
void
|
||
|
warnlog(char MSG[])
|
||
|
{
|
||
|
if (MSG == NULL) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
printf(COL_YELLOW "[!]" COL_RESET " %s\n", MSG);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* errlog
|
||
|
*
|
||
|
* DESCRIPTION: Output a stylized error message.
|
||
|
* PARAMETERS: char MSG[]
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
void
|
||
|
errlog(char MSG[])
|
||
|
{
|
||
|
if (MSG == NULL) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
fprintf(stderr, COL_RED "[x]" COL_RESET " %s\n", MSG);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* runtime_exists
|
||
|
*
|
||
|
* DESCRIPTION: Check if necesary runtime files exist.
|
||
|
* PARAMETERS: None.
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
void
|
||
|
runtime_exists()
|
||
|
{
|
||
|
int f;
|
||
|
|
||
|
for (f = 0; f < 3; f++) {
|
||
|
if (access(runtime_files[f], F_OK) == 0) {
|
||
|
printf("%s exists\n", runtime_files[f]);
|
||
|
} else {
|
||
|
printf("%s does not exist\n", runtime_files[f]);
|
||
|
}
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* init_config
|
||
|
*
|
||
|
* DESCRIPTION: Initialize libconfig.
|
||
|
* PARAMETERS: None.
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
int
|
||
|
init_config()
|
||
|
{
|
||
|
config_init(&cfg);
|
||
|
|
||
|
if (! config_read_file(&cfg, "glacier.cfg")) {
|
||
|
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
|
||
|
config_error_line(&cfg), config_error_text(&cfg));
|
||
|
config_destroy(&cfg);
|
||
|
return(EXIT_FAILURE);
|
||
|
}
|
||
|
|
||
|
infolog("Initialized libconfig");
|
||
|
}
|
||
|
|
||
|
int
|
||
|
die_config()
|
||
|
{
|
||
|
config_destroy(&cfg);
|
||
|
infolog("Destroyed libconfig");
|
||
|
return(EXIT_SUCCESS);
|
||
|
}
|
||
|
|
||
|
void
|
||
|
load_setting_from_config()
|
||
|
{
|
||
|
if(config_lookup_bool(&cfg, "GLACIER_ALLOW_SERVICES", &GLACIER_ALLOW_SERVICES))
|
||
|
printf("Services allowed: %s\n", GLACIER_ALLOW_SERVICES ? "true" : "false");
|
||
|
else
|
||
|
warnlog("GLACIER_SERVICES_ALLOWED is not defined in glacier.cfg.");
|
||
|
|
||
|
}
|
||
|
|
||
|
int
|
||
|
main(int argc, char *argv[])
|
||
|
{
|
||
|
errlog("Unknown option.");
|
||
|
printf(COL_RED "[x]" COL_RESET " usage: %s [-h] [-v] [-f] [-u] [-x] [-fl] [-ul] [-d] [-s] PKG\n", argv[0]);
|
||
|
runtime_exists();
|
||
|
init_config();
|
||
|
load_setting_from_config();
|
||
|
die_config();
|
||
|
}
|