Commit 7cf7cfb8 authored by mbionchi's avatar mbionchi

add splash screen, add getopt parsing, probably other stuff ????

parent 7a3ae89c
bin_PROGRAMS = telnetsite
telnetsite_SOURCES = main.c anim.c anim.h site.c site.h data.c data.h scroll.c scroll.h render.c render.h log.c log.h
telnetsite_SOURCES = main.c anim.c anim.h site.c site.h splash.c splash.h data.c data.h scroll.c scroll.h render.c render.h log.c log.h
telnetsite_LDADD = ${ncurses_LIBS}
AM_CFLAGS = -Isrc/
......@@ -22,6 +22,8 @@
#include <dirent.h>
#include <curses.h>
char *binary_name;
struct line {
char *line;
struct line *prev, *next;
......
......@@ -16,9 +16,41 @@
*/
#include "site.h"
#include "splash.h"
#include "data.h"
#include <getopt.h>
#include <stdlib.h>
enum { SITE_INDEX, SPLASH_INDEX };
int main(int argc, char **argv) {
exit(site(argc, argv));
binary_name = argv[0];
char *splash_path = NULL,
*site_path = NULL;
static struct option options[] = {
{"site", required_argument, NULL, SITE_INDEX},
{"splash", required_argument, NULL, SPLASH_INDEX},
{NULL, 0, NULL, 0}
};
int opt = getopt_long_only(argc, argv, "", options, NULL);
while (opt != -1) {
switch (opt) {
case SITE_INDEX:
site_path = optarg;
break;
case SPLASH_INDEX:
splash_path = optarg;
break;
}
opt = getopt_long_only(argc, argv, "", options, NULL);
}
if (!site_path) {
fprintf(stderr, "Usage: %s --site <path-to-dir> [--splash <path-to-file>]\n", binary_name);
exit(1);
}
if (splash_path) {
splash(splash_path);
}
site(site_path);
}
......@@ -62,14 +62,9 @@ void winch_handler(int signo) {
* TODO:
* - error handling everywhere please
*/
int site(int argc, char **argv) {
void site(char *path) {
signal(SIGWINCH, winch_handler);
if (argc < 2) {
fprintf(stderr, "Usage: %s <path-to-site.d>\n", argv[0]);
exit(EXIT_FAILURE);
}
struct window index_window;
struct window separator_window;
struct window content_window;
......@@ -77,19 +72,19 @@ int site(int argc, char **argv) {
int selected_index = 0;
int scroll = 0;
size_t n_sections;
DIR *dir = opendir(argv[1]);
DIR *dir = opendir(path);
if (dir == NULL) {
fprintf(stderr, "[E] %s:%s:%u: %s: %s\n", argv[0], __FILE__, __LINE__, strerror(errno), argv[1]);
fprintf(stderr, "[E] %s:%s:%u: %s: %s\n", binary_name, __FILE__, __LINE__, strerror(errno), path);
exit(1);
}
struct section **sections = read_sections(dir, argv[1], 1, &n_sections);
struct section **sections = read_sections(dir, path, 1, &n_sections);
closedir(dir);
FILE *fp = fopen(sections[selected_index]->filename, "r");
if (fp != NULL) {
content_window.content.n_raw = read_nlines(fp, &content_window.content.raw);
fclose(fp);
} else {
fprintf(stderr, "[W] %s:%s:%u: %s: %s\n", argv[0], __FILE__, __LINE__, strerror(errno), sections[selected_index]->filename);
fprintf(stderr, "[W] %s:%s:%u: %s: %s\n", binary_name, __FILE__, __LINE__, strerror(errno), sections[selected_index]->filename);
content_window.content.n_raw = gen_err_opening(&content_window.content.raw);
}
initscr();
......@@ -255,7 +250,7 @@ int site(int argc, char **argv) {
content_window.content.n_raw = read_nlines(fp, &content_window.content.raw);
fclose(fp);
} else {
fprintf(stderr, "[W] %s:%s:%u: %s: %s\n", argv[0], __FILE__, __LINE__, strerror(errno), sections[selected_index]->filename);
fprintf(stderr, "[W] %s:%s:%u: %s: %s\n", binary_name, __FILE__, __LINE__, strerror(errno), sections[selected_index]->filename);
content_window.content.n_raw = gen_err_opening(&content_window.content.raw);
}
......@@ -284,5 +279,4 @@ int site(int argc, char **argv) {
delwin(separator_window.window);
delwin(content_window.window);
endwin();
return 0;
}
......@@ -18,6 +18,6 @@
#ifndef _TELNET_SITE_SITE_H
#define _TELNET_SITE_SITE_H
int site(int argc, char **argv);
void site(char *site_path);
#endif
#include "splash.h"
#include "data.h"
#include "render.h"
#include "anim.h"
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
void splash(char *path) {
signal(SIGWINCH, SIG_IGN);
struct window splash_window;
FILE *fp = fopen(path, "r");
if (fp != NULL) {
splash_window.content.n_raw = read_nlines(fp, &splash_window.content.raw);
fclose(fp);
} else {
fprintf(stderr, "[W] %s:%s:%u: %s: %s\n", binary_name, __FILE__, __LINE__, strerror(errno), path);
splash_window.content.n_raw = gen_err_opening(&splash_window.content.raw);
}
splash_window.window = initscr();
cbreak();
halfdelay(1);
noecho();
curs_set(0);
splash_window.cols = COLS;
splash_window.rows = LINES;
splash_window.scroll = 0;
splash_window.content.anim_refs = NULL;
splash_window.content.n_formatted = flow_nlines(splash_window.content.raw,
splash_window.content.n_raw,
&splash_window.content.formatted,
splash_window.cols);
render_ncontent(&splash_window);
refresh();
wrefresh(splash_window.window);
int ch = getch();
while (ch == ERR) {
anim_tick(&splash_window);
ch = getch();
}
free_content(&splash_window);
free_anim_refs(&splash_window);
clear();
refresh();
endwin();
return;
}
/*
* This file is part of telnet-site.
*
* telnet-site 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.
*
* telnet-site 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 telnet-site. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef _TELNET_SPLASH_SITE_H
#define _TELNET_SPLASH_SITE_H
void splash(char *path);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment