new: locale implementation and signal handler

This commit is contained in:
ngn 2024-08-01 02:02:13 +03:00
parent 33e59c6644
commit 4bb4496bdc
4 changed files with 104 additions and 17 deletions

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-07-31 23:21+0300\n"
"POT-Creation-Date: 2024-08-01 01:59+0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -72,56 +72,69 @@ msgstr ""
msgid "Please answer with y/n"
msgstr ""
#: src/main.c:74
#: src/main.c:48
msgid "Received a segfault"
msgstr ""
#: src/main.c:93
msgid "Failed to access the root directory, are you running as root?"
msgstr ""
#: src/main.c:76
#: src/main.c:95
#, fuzzy, c-format
msgid "Bad root directory (%s): %s"
msgstr "Havuz dizini kullanmak başarısız oldu: %s"
#: src/main.c:84
#: src/main.c:103
#, c-format
msgid "Bad datadir (%s): %s"
msgstr ""
#: src/main.c:89
#: src/main.c:108
#, c-format
msgid "Bad tmpdir (%s): %s"
msgstr ""
#: src/main.c:96
#: src/main.c:115
#, fuzzy
msgid "Failed to add pool "
msgstr "Havuzları sekronize etmek başarısız oldu: %s"
#: src/main.c:109
#: src/main.c:127
msgid "Failed to lock, matt is already running"
msgstr ""
#: src/main.c:131
#, fuzzy
msgid "Failed to lock, are you root?"
msgstr "Havuzları sekronize etmek başarısız oldu: %s"
#: src/main.c:142
msgid "Command not found: "
msgstr ""
#: src/main.c:111
#: src/main.c:144
#, c-format
msgid "MatterLinux package manager (version %s)"
msgstr ""
#: src/main.c:112
#: src/main.c:145
msgid "Usage: "
msgstr ""
#: src/main.c:114
#: src/main.c:147
msgid "Here is a list of available commands:"
msgstr ""
#: src/main.c:120
#: src/main.c:153
msgid "To list different options, use commands with "
msgstr ""
#: src/main.c:121
#: src/main.c:154
msgid "Here is a list of available global options:"
msgstr ""
#: src/main.c:124 src/main.c:125 src/cmd/info.c:42 src/cmd/info.c:43
#: src/main.c:157 src/main.c:158 src/cmd/info.c:42 src/cmd/info.c:43
#: src/cmd/info.c:44 src/cmd/install.c:25 src/cmd/install.c:26
#: src/cmd/list.c:13 src/cmd/list.c:14 src/cmd/remove.c:20 src/cmd/remove.c:21
#: src/cmd/update.c:29
@ -129,7 +142,7 @@ msgstr ""
msgid " "
msgstr ""
#: src/main.c:127
#: src/main.c:160
msgid ""
"Licensed under GPLv3, see https://www.gnu.org/licenses/ for more information"
msgstr ""

28
src/lock.c Normal file
View File

@ -0,0 +1,28 @@
#include "lock.h"
#include <fcntl.h>
#include <libmp/util.h>
#include <string.h>
#include <unistd.h>
lock_st lock(lm_ctx_t *ctx) {
if (NULL == ctx->data)
return LOCK_ERROR;
char lock_file[strlen(ctx->data) + 10];
join(lock_file, ctx->data, "lock");
if (exists(lock_file))
return ALREADY_LOCKED;
if (creat(lock_file, 0600) == -1)
return LOCK_ERROR;
return LOCK_SUCCESS;
}
void unlock(lm_ctx_t *ctx) {
if (NULL == ctx->data)
return;
char lock_file[strlen(ctx->data) + 10];
join(lock_file, ctx->data, "lock");
unlink(lock_file);
}

11
src/lock.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include <libmp/all.h>
typedef enum lock_state {
ALREADY_LOCKED = 1,
LOCK_SUCCESS = 2,
LOCK_ERROR = 3,
} lock_st;
lock_st lock(lm_ctx_t *ctx);
void unlock(lm_ctx_t *ctx);

View File

@ -26,6 +26,8 @@
#include <libmp/ctx.h>
#include <libmp/error.h>
#include <libmp/util.h>
#include <locale.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
@ -33,10 +35,28 @@
#include "cmd.h"
#include "config.h"
#include "intl.h"
#include "lock.h"
#include "log.h"
#include "util.h"
lm_ctx_t ctx;
void handler(int sig) {
unlock(&ctx);
printf("\e[?25h\n");
if (SIGSEGV == sig)
error(_("Received a segfault"));
exit(1);
}
int main(int argc, char *argv[], char *envp[]) {
signal(SIGSEGV, handler);
signal(SIGINT, handler);
setbuf(stdout, NULL);
setlocale(LC_ALL, "");
textdomain("matt");
cmd_t commands[] = {
{.name = "info", .desc = "show information about a single package", .func = cmd_info },
{.name = "list", .desc = "list all the installed packages", .func = cmd_list },
@ -51,7 +71,6 @@ int main(int argc, char *argv[], char *envp[]) {
args_t *args = NULL;
bool ret = false;
config_t config;
lm_ctx_t ctx;
set_env(envp);
lm_ctx_init(&ctx);
@ -100,11 +119,25 @@ int main(int argc, char *argv[], char *envp[]) {
}
for (int i = 0; i < sizeof(commands) / sizeof(cmd_t); i++) {
if (eq(commands[i].name, args->list[0].value)) {
if (!eq(commands[i].name, args->list[0].value))
continue;
switch (lock(&ctx)) {
case ALREADY_LOCKED:
error(_("Failed to lock, matt is already running"));
goto end;
case LOCK_ERROR:
error(_("Failed to lock, are you root?"));
goto end;
default:
break;
}
ret = commands[i].func(&ctx, &config, args);
goto end;
}
}
error(_("Command not found: " FG_BOLD "%s"), args->list[0].value);
help:
@ -130,6 +163,8 @@ help:
goto end;
end:
unlock(&ctx);
free(full_datadir);
free(full_tmpdir);