Move output and path handling code outside main.

Temporarily add static to translation strings to allow compilation to
work.
This commit is contained in:
Érico Rolim 2021-02-22 01:53:44 -03:00
parent d8b6432c6b
commit fdf2b43b34
7 changed files with 95 additions and 70 deletions

View File

@ -9,7 +9,7 @@ bindir = $(PREFIX)/bin
all: ep
ep: ep.c git.c
ep: ep.c out.c path.c git.c
install: ep
install -m755 $< $(bindir)/ep

68
ep.c
View File

@ -21,31 +21,6 @@
#define PROMPT " ➜ "
/* function to print string */
FILE *out;
static inline void p(const char *s) { fputs(s, out); }
/* function to log stuff */
enum log_level_value { DEBUG, INFO, WARN, ERROR };
const enum log_level_value log_level = ERROR;
FILE *outerr;
static void e(enum log_level_value l, const char *s, int errcode)
{
if (l < log_level) {
return;
}
if (errcode) {
perror(s);
} else {
fputs(s, outerr);
fputs("\n", outerr);
}
}
/* print current dir in fish style */
const int fish_style_dir = 1;
int main(int argc, char **argv)
{
setlocale(LC_ALL, "");
@ -88,48 +63,7 @@ int main(int argc, char **argv)
p(" ");
}
/* deal with printing current path */
char *pwd = getcwd(NULL, 0);
char *rpwd = NULL;
if (!pwd || !(rpwd = realpath(pwd, NULL))) {
/* getcwd or realpath failed */
p(unknowndir);
} else {
/* strip HOME out if possible */
if (home) {
size_t l = strlen(home);
if (!strncmp(home, rpwd, l)) {
/* found HOME in pwd */
p("~");
rpwd += l;
if (*rpwd) {
if (fish_style_dir) {
/* short and sweet way of malloc-ing enough memory */
char *frpwd = strdup(rpwd);
/* rpwd starts with a slash */
const char *c = rpwd, *co;
char *n = frpwd;
for (; c; co = c, c = strchr(co+1, '/')) {
*n++ = '/';
*n++ = *(c+1);
}
/* copy last path completely */
strcpy(--n, co+1);
p(frpwd);
} else {
p(rpwd);
}
}
} else {
/* HOME wasn't in rpwd */
p(rpwd);
}
} else {
/* HOME is unset */
p(rpwd);
}
}
print_pwd(home);
/* git status */
pthread_join(git_handle, NULL);

13
ep.h
View File

@ -1,6 +1,19 @@
#ifndef EP_H
#define EP_H
#include <stdio.h>
/* from out.c */
extern FILE *out, *outerr;
enum log_level_value { DEBUG, INFO, WARN, ERROR };
extern const enum log_level_value log_level;
void p(const char *);
void e(enum log_level_value, const char *, int);
/* from path.c */
extern const int fish_style_dir;
void print_pwd(const char *);
/* from git.c */
extern char *git_branch_name;
void *get_git_branch_name(void *);

24
out.c Normal file
View File

@ -0,0 +1,24 @@
#include <stdio.h>
#include "ep.h"
/* function to print string */
FILE *out;
void p(const char *s) { fputs(s, out); }
/* function to log stuff */
const enum log_level_value log_level = ERROR;
FILE *outerr;
void e(enum log_level_value l, const char *s, int errcode)
{
if (l < log_level) {
return;
}
if (errcode) {
perror(s);
} else {
fputs(s, outerr);
fputs("\n", outerr);
}
}

54
path.c Normal file
View File

@ -0,0 +1,54 @@
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "ep.h"
#include "info_strings.h"
/* print current dir in fish style */
const int fish_style_dir = 1;
void print_pwd(const char *home)
{
char *pwd = getcwd(NULL, 0);
char *rpwd = NULL;
if (!pwd || !(rpwd = realpath(pwd, NULL))) {
/* getcwd or realpath failed */
p(unknowndir);
} else {
/* strip HOME out if possible */
if (home) {
size_t l = strlen(home);
if (!strncmp(home, rpwd, l)) {
/* found HOME in pwd */
p("~");
rpwd += l;
if (*rpwd) {
if (fish_style_dir) {
/* short and sweet way of malloc-ing enough memory */
char *frpwd = strdup(rpwd);
/* rpwd starts with a slash */
const char *c = rpwd, *co;
char *n = frpwd;
for (; c; co = c, c = strchr(co+1, '/')) {
*n++ = '/';
*n++ = *(c+1);
}
/* copy last path completely */
strcpy(--n, co+1);
p(frpwd);
} else {
p(rpwd);
}
}
} else {
/* HOME wasn't in rpwd */
p(rpwd);
}
} else {
/* HOME is unset */
p(rpwd);
}
}
}

View File

@ -1 +1 @@
const char *unknowndir = "[unknown]";
static const char *unknowndir = "[unknown]";

View File

@ -1 +1 @@
const char *unknowndir = "[desconhecido]";
static const char *unknowndir = "[desconhecido]";