error handling and proper death function

master
Alessandro Mauri 5 years ago
parent eb72d24b5b
commit 580e2fa5be
  1. 2
      src/config.h
  2. 32
      src/die.h
  3. 38
      src/fbuffer.h
  4. 28
      src/ste.c

@ -1,6 +1,8 @@
#ifndef _EDITOR_CONFIG_H_
#define _EDITOR_CONFIG_H_
#define FRONTEND_NCURSES
#define TABSIZE 4 // Tab size as used in render
#define MAX_LINE 1024 // maximum line length on screen
#define PGK_DELTA 15 // Step of jump

@ -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_
#define _FBUFFER_H_
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE /* See feature_test_macros(7) */
#endif
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <wchar.h>
#include "config.h"
#include "die.h"
/* Row structure, defines actual and
* render chars, actual and render size
* and difference between render and
@ -43,11 +53,7 @@ int isUtf (int c);
int isCont (int c);
int isStart (int c);
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <wchar.h>
/* Start of function definitions */
void bufInit (FileBuffer *b)
{
@ -60,13 +66,13 @@ void rowAddLast (FileBuffer *b, char *s, int len)
{
/* Extend the block of memory containing the lines */
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;
/* Allocate memory for the line and copy it
* at the current row number */
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);
b->rw[b->rownum].chars[len] = '\0';
b->rw[b->rownum].size = len;
@ -81,9 +87,9 @@ void rowAddChar (Row *rw, char c, int pos)
/* extend the string */
rw->size++;
char *t = realloc(rw->chars, rw->size + 1);
//if (t == NULL) termDie("realloc in rowAddchar");
rw->chars = t;
char *tmp = realloc(rw->chars, rw->size + 1);
if (tmp == NULL) die("realloc in rowAddchar", REALLOC_ERR);
rw->chars = tmp;
/* make space for the new char */
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;
/* allocate a buffer */
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 */
memcpy(s, &b->rw[pos].chars[cur], len);
s[len] = '\0';
@ -142,7 +148,7 @@ void rowAddRow (FileBuffer *b, int pos, int cur) // WIP; TO DOCUMENT
/* MAKE THE SPLIT INTO TWO LINES */
/* shrink the line at pos */
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;
/* and terminate it with null like a good boi */
b->rw[pos].chars[cur] = '\0';
@ -173,7 +179,7 @@ void rowCpy (Row *to, Row *from) // WIP
rowFree(to);
/* Allocate space for the new string */
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 */
to->size = from->size;
/* 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 */
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;
memcpy(&rw->chars[rw->size], s, len);
@ -209,7 +215,7 @@ void rowDeleteRow (FileBuffer *b, int pos)
b->rownum--;
rowFree(&b->rw[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;
}
@ -245,7 +251,7 @@ void updateRender (Row *rw)
/* Render is long as size with the added tab spaces - 1
* (we already count for the \t as a char) */
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)
* into the render buffer */

@ -10,6 +10,7 @@
#include "fbuffer.h"
#include "config.h"
#include "die.h"
/* defines */
#define CTRL(k) ((k) & 0x1f) // Control mask modifier
@ -74,8 +75,6 @@ static inline void lnMove (int y, int x);
/* Terminal operations */
static void termInit (void);
static void termExit (void);
static void termDie (char *s);
/* file operations */
static void fileOpen (char *filename);
@ -129,7 +128,7 @@ int main (int argc, char *argv[])
/* Wait for an event (keypress) */
switch (c = getch()) {
case (CTRL('q')):
termExit();
die("", AOK);
break;
case (KEY_MOVE_UP):
@ -210,7 +209,7 @@ int main (int argc, char *argv[])
/* If by chance i find myself here be sure
* end curses mode and clenaup */
termExit();
die("", AOK);
return 0;
}
@ -268,23 +267,6 @@ int decimalSize (int n)
return l + 1;
}
void termExit (void)
{
erase();
refresh();
endwin();
exit(0);
}
void termDie (char *s)
{
erase();
refresh();
endwin();
perror(s);
exit(1);
}
/* ----------------------------- term operations -------------------------------- */
void drawScreen ()
@ -327,7 +309,7 @@ void drawLines (void)
lnMove(i, 0);
/* 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) {
start = t.cur.off_x;
@ -382,7 +364,7 @@ void drawBar (char *s)
void fileOpen (char *filename)
{
FILE *fp = fopen(filename, "r");
if (fp == NULL) termDie("Cannot open file");
if (fp == NULL) die("Cannot open file", BAD_FILE);
/* Init the linebuffer */
char *line = NULL;

Loading…
Cancel
Save