error handling and proper death function
This commit is contained in:
parent
eb72d24b5b
commit
580e2fa5be
@ -1,6 +1,8 @@
|
|||||||
#ifndef _EDITOR_CONFIG_H_
|
#ifndef _EDITOR_CONFIG_H_
|
||||||
#define _EDITOR_CONFIG_H_
|
#define _EDITOR_CONFIG_H_
|
||||||
|
|
||||||
|
#define FRONTEND_NCURSES
|
||||||
|
|
||||||
#define TABSIZE 4 // Tab size as used in render
|
#define TABSIZE 4 // Tab size as used in render
|
||||||
#define MAX_LINE 1024 // maximum line length on screen
|
#define MAX_LINE 1024 // maximum line length on screen
|
||||||
#define PGK_DELTA 15 // Step of jump
|
#define PGK_DELTA 15 // Step of jump
|
||||||
|
32
src/die.h
Normal file
32
src/die.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#ifndef _HELPER_DIE_H_
|
||||||
|
#define _HELPER_DIE_H_
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#ifdef FRONTEND_NCURSES
|
||||||
|
#include <ncurses.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
AOK,
|
||||||
|
GENERIC_ERR,
|
||||||
|
BAD_FILE,
|
||||||
|
MALLOC_ERR,
|
||||||
|
REALLOC_ERR,
|
||||||
|
BAD_PNTR
|
||||||
|
} DeathStatus;
|
||||||
|
|
||||||
|
void die (const char *message, DeathStatus sn)
|
||||||
|
{
|
||||||
|
#ifdef FRONTEND_NCURSES
|
||||||
|
erase();
|
||||||
|
refresh();
|
||||||
|
endwin();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (sn) perror(message);
|
||||||
|
exit(sn);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -1,8 +1,18 @@
|
|||||||
#ifndef _FBUFFER_H_
|
#ifndef _FBUFFER_H_
|
||||||
#define _FBUFFER_H_
|
#define _FBUFFER_H_
|
||||||
|
|
||||||
#ifndef _XOPEN_SOURCE
|
#ifndef _XOPEN_SOURCE
|
||||||
#define _XOPEN_SOURCE /* See feature_test_macros(7) */
|
#define _XOPEN_SOURCE /* See feature_test_macros(7) */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <wchar.h>
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
#include "die.h"
|
||||||
|
|
||||||
/* Row structure, defines actual and
|
/* Row structure, defines actual and
|
||||||
* render chars, actual and render size
|
* render chars, actual and render size
|
||||||
* and difference between render and
|
* and difference between render and
|
||||||
@ -43,11 +53,7 @@ int isUtf (int c);
|
|||||||
int isCont (int c);
|
int isCont (int c);
|
||||||
int isStart (int c);
|
int isStart (int c);
|
||||||
|
|
||||||
#include "config.h"
|
/* Start of function definitions */
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <wchar.h>
|
|
||||||
|
|
||||||
void bufInit (FileBuffer *b)
|
void bufInit (FileBuffer *b)
|
||||||
{
|
{
|
||||||
@ -60,13 +66,13 @@ void rowAddLast (FileBuffer *b, char *s, int len)
|
|||||||
{
|
{
|
||||||
/* Extend the block of memory containing the lines */
|
/* Extend the block of memory containing the lines */
|
||||||
Row *newr = realloc(b->rw, (b->rownum + 1) * sizeof(Row));
|
Row *newr = realloc(b->rw, (b->rownum + 1) * sizeof(Row));
|
||||||
//if (newr == NULL) termDie("realloc in rowAddLast");
|
if (newr == NULL) die("realloc in rowAddLast", REALLOC_ERR);
|
||||||
b->rw = newr;
|
b->rw = newr;
|
||||||
|
|
||||||
/* Allocate memory for the line and copy it
|
/* Allocate memory for the line and copy it
|
||||||
* at the current row number */
|
* at the current row number */
|
||||||
b->rw[b->rownum].chars = malloc(len + 1);
|
b->rw[b->rownum].chars = malloc(len + 1);
|
||||||
//if (b->rw[b->rownum].chars == NULL) termDie("malloc in rowAddLast chars");
|
if (b->rw[b->rownum].chars == NULL) die("malloc in rowAddLast chars", MALLOC_ERR);
|
||||||
memcpy(b->rw[b->rownum].chars, s, len);
|
memcpy(b->rw[b->rownum].chars, s, len);
|
||||||
b->rw[b->rownum].chars[len] = '\0';
|
b->rw[b->rownum].chars[len] = '\0';
|
||||||
b->rw[b->rownum].size = len;
|
b->rw[b->rownum].size = len;
|
||||||
@ -81,9 +87,9 @@ void rowAddChar (Row *rw, char c, int pos)
|
|||||||
|
|
||||||
/* extend the string */
|
/* extend the string */
|
||||||
rw->size++;
|
rw->size++;
|
||||||
char *t = realloc(rw->chars, rw->size + 1);
|
char *tmp = realloc(rw->chars, rw->size + 1);
|
||||||
//if (t == NULL) termDie("realloc in rowAddchar");
|
if (tmp == NULL) die("realloc in rowAddchar", REALLOC_ERR);
|
||||||
rw->chars = t;
|
rw->chars = tmp;
|
||||||
|
|
||||||
/* make space for the new char */
|
/* make space for the new char */
|
||||||
memcpy(&rw->chars[pos + 1], &rw->chars[pos], (rw->size + 1) - (pos + 1));
|
memcpy(&rw->chars[pos + 1], &rw->chars[pos], (rw->size + 1) - (pos + 1));
|
||||||
@ -131,7 +137,7 @@ void rowAddRow (FileBuffer *b, int pos, int cur) // WIP; TO DOCUMENT
|
|||||||
Row nex = EROW;
|
Row nex = EROW;
|
||||||
/* allocate a buffer */
|
/* allocate a buffer */
|
||||||
char *s = malloc(len + 1);
|
char *s = malloc(len + 1);
|
||||||
//if (s == NULL) termDie("malloc in rowAddRow s");
|
if (s == NULL) die("malloc in rowAddRow s", MALLOC_ERR);
|
||||||
/* copy the contents of the pos row after the cursor into the buffer */
|
/* copy the contents of the pos row after the cursor into the buffer */
|
||||||
memcpy(s, &b->rw[pos].chars[cur], len);
|
memcpy(s, &b->rw[pos].chars[cur], len);
|
||||||
s[len] = '\0';
|
s[len] = '\0';
|
||||||
@ -142,7 +148,7 @@ void rowAddRow (FileBuffer *b, int pos, int cur) // WIP; TO DOCUMENT
|
|||||||
/* MAKE THE SPLIT INTO TWO LINES */
|
/* MAKE THE SPLIT INTO TWO LINES */
|
||||||
/* shrink the line at pos */
|
/* shrink the line at pos */
|
||||||
char *p = realloc(b->rw[pos].chars, cur + 1);
|
char *p = realloc(b->rw[pos].chars, cur + 1);
|
||||||
//if (p == NULL) termDie("realloc in rowAddRow");
|
if (p == NULL) die("realloc in rowAddRow", REALLOC_ERR);
|
||||||
b->rw[pos].chars = p;
|
b->rw[pos].chars = p;
|
||||||
/* and terminate it with null like a good boi */
|
/* and terminate it with null like a good boi */
|
||||||
b->rw[pos].chars[cur] = '\0';
|
b->rw[pos].chars[cur] = '\0';
|
||||||
@ -173,7 +179,7 @@ void rowCpy (Row *to, Row *from) // WIP
|
|||||||
rowFree(to);
|
rowFree(to);
|
||||||
/* Allocate space for the new string */
|
/* Allocate space for the new string */
|
||||||
to->chars = (char*) malloc(strlen(from->chars) + 1);
|
to->chars = (char*) malloc(strlen(from->chars) + 1);
|
||||||
//if (to->chars == NULL) termDie("malloc in rowCpy");
|
if (to->chars == NULL) die("malloc in rowCpy", MALLOC_ERR);
|
||||||
/* And update the size */
|
/* And update the size */
|
||||||
to->size = from->size;
|
to->size = from->size;
|
||||||
/* Then copy the chars from the source row to the destination row */
|
/* Then copy the chars from the source row to the destination row */
|
||||||
@ -187,7 +193,7 @@ void rowAppendString (Row *rw, char *s, int len)
|
|||||||
{
|
{
|
||||||
/* reallocate the row to accomodate for the added string */
|
/* reallocate the row to accomodate for the added string */
|
||||||
char *temp = realloc(rw->chars, rw->size + len + 1);
|
char *temp = realloc(rw->chars, rw->size + len + 1);
|
||||||
//if (temp == NULL) termDie("realloc in rowAppendString");
|
if (temp == NULL) die("realloc in rowAppendString", REALLOC_ERR);
|
||||||
rw->chars = temp;
|
rw->chars = temp;
|
||||||
|
|
||||||
memcpy(&rw->chars[rw->size], s, len);
|
memcpy(&rw->chars[rw->size], s, len);
|
||||||
@ -209,7 +215,7 @@ void rowDeleteRow (FileBuffer *b, int pos)
|
|||||||
b->rownum--;
|
b->rownum--;
|
||||||
rowFree(&b->rw[b->rownum]);
|
rowFree(&b->rw[b->rownum]);
|
||||||
Row *temp = realloc(b->rw, sizeof(Row) * b->rownum);
|
Row *temp = realloc(b->rw, sizeof(Row) * b->rownum);
|
||||||
//if (temp == NULL) termDie("realloc in rowDeleteRow");
|
if (temp == NULL) die("realloc in rowDeleteRow", REALLOC_ERR);
|
||||||
b->rw = temp;
|
b->rw = temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,7 +251,7 @@ void updateRender (Row *rw)
|
|||||||
/* Render is long as size with the added tab spaces - 1
|
/* Render is long as size with the added tab spaces - 1
|
||||||
* (we already count for the \t as a char) */
|
* (we already count for the \t as a char) */
|
||||||
rw->render = malloc(rw->size + tabs * (TABSIZE - 1) + 1);
|
rw->render = malloc(rw->size + tabs * (TABSIZE - 1) + 1);
|
||||||
//if (rw->render == NULL) termDie ("malloc in updateRender");
|
if (rw->render == NULL) die ("malloc in updateRender", MALLOC_ERR);
|
||||||
|
|
||||||
/* put all the characters (substituing all special chars)
|
/* put all the characters (substituing all special chars)
|
||||||
* into the render buffer */
|
* into the render buffer */
|
||||||
|
28
src/ste.c
28
src/ste.c
@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
#include "fbuffer.h"
|
#include "fbuffer.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
#include "die.h"
|
||||||
|
|
||||||
/* defines */
|
/* defines */
|
||||||
#define CTRL(k) ((k) & 0x1f) // Control mask modifier
|
#define CTRL(k) ((k) & 0x1f) // Control mask modifier
|
||||||
@ -74,8 +75,6 @@ static inline void lnMove (int y, int x);
|
|||||||
|
|
||||||
/* Terminal operations */
|
/* Terminal operations */
|
||||||
static void termInit (void);
|
static void termInit (void);
|
||||||
static void termExit (void);
|
|
||||||
static void termDie (char *s);
|
|
||||||
|
|
||||||
/* file operations */
|
/* file operations */
|
||||||
static void fileOpen (char *filename);
|
static void fileOpen (char *filename);
|
||||||
@ -129,7 +128,7 @@ int main (int argc, char *argv[])
|
|||||||
/* Wait for an event (keypress) */
|
/* Wait for an event (keypress) */
|
||||||
switch (c = getch()) {
|
switch (c = getch()) {
|
||||||
case (CTRL('q')):
|
case (CTRL('q')):
|
||||||
termExit();
|
die("", AOK);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case (KEY_MOVE_UP):
|
case (KEY_MOVE_UP):
|
||||||
@ -210,7 +209,7 @@ int main (int argc, char *argv[])
|
|||||||
|
|
||||||
/* If by chance i find myself here be sure
|
/* If by chance i find myself here be sure
|
||||||
* end curses mode and clenaup */
|
* end curses mode and clenaup */
|
||||||
termExit();
|
die("", AOK);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -268,23 +267,6 @@ int decimalSize (int n)
|
|||||||
return l + 1;
|
return l + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void termExit (void)
|
|
||||||
{
|
|
||||||
erase();
|
|
||||||
refresh();
|
|
||||||
endwin();
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void termDie (char *s)
|
|
||||||
{
|
|
||||||
erase();
|
|
||||||
refresh();
|
|
||||||
endwin();
|
|
||||||
perror(s);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ----------------------------- term operations -------------------------------- */
|
/* ----------------------------- term operations -------------------------------- */
|
||||||
|
|
||||||
void drawScreen ()
|
void drawScreen ()
|
||||||
@ -327,7 +309,7 @@ void drawLines (void)
|
|||||||
lnMove(i, 0);
|
lnMove(i, 0);
|
||||||
|
|
||||||
/* Draw the line matcing render memory */
|
/* Draw the line matcing render memory */
|
||||||
if (&rows.rw[ln] == NULL) termDie("drawlines NULL");
|
if (&rows.rw[ln] == NULL) die("drawlines NULL", BAD_PNTR);
|
||||||
if (rows.rw[ln].r_size > t.cur.off_x) {
|
if (rows.rw[ln].r_size > t.cur.off_x) {
|
||||||
|
|
||||||
start = t.cur.off_x;
|
start = t.cur.off_x;
|
||||||
@ -382,7 +364,7 @@ void drawBar (char *s)
|
|||||||
void fileOpen (char *filename)
|
void fileOpen (char *filename)
|
||||||
{
|
{
|
||||||
FILE *fp = fopen(filename, "r");
|
FILE *fp = fopen(filename, "r");
|
||||||
if (fp == NULL) termDie("Cannot open file");
|
if (fp == NULL) die("Cannot open file", BAD_FILE);
|
||||||
|
|
||||||
/* Init the linebuffer */
|
/* Init the linebuffer */
|
||||||
char *line = NULL;
|
char *line = NULL;
|
||||||
|
Loading…
Reference in New Issue
Block a user