die now uses errno instead of a totally custom and non-standard error system

master
Alessandro Mauri 5 years ago
parent 83cdd65c71
commit c98095dd3f
  1. 30
      src/die.h
  2. 18
      src/fbuffer.h
  3. 19
      src/ste.c

@ -1,24 +1,17 @@
#ifndef _HELPER_DIE_H_
#define _HELPER_DIE_H_
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#ifdef FRONTEND_NCURSES
#include <ncurses.h>
#endif
typedef enum {
AOK,
GENERIC_ERR,
BAD_FILE,
SAVE_ERR,
MALLOC_ERR,
REALLOC_ERR,
BAD_PNTR
} DeathStatus;
void die (const char *message, DeathStatus sn)
void die (const char *message, int err)
{
#ifdef FRONTEND_NCURSES
erase();
@ -26,9 +19,20 @@ void die (const char *message, DeathStatus sn)
endwin();
#endif
if (sn && message != NULL)
if (message == NULL)
exit(0);
if (err > 131)
err = 1;
if (err) {
printf("\n%s:%s", message, strerror(err));
exit(err);
} else {
perror(message);
exit(sn);
exit(errno);
}
exit(-1);
}
#endif

@ -66,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) die("realloc in rowAddLast", REALLOC_ERR);
if (newr == NULL) die("realloc in rowAddLast", 0);
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) die("malloc in rowAddLast chars", MALLOC_ERR);
if (b->rw[b->rownum].chars == NULL) die("malloc in rowAddLast chars", 0);
memcpy(b->rw[b->rownum].chars, s, len);
b->rw[b->rownum].chars[len] = '\0';
b->rw[b->rownum].size = len;
@ -88,7 +88,7 @@ void rowAddChar (Row *rw, char c, int pos)
/* extend the string */
rw->size++;
char *tmp = realloc(rw->chars, rw->size + 1);
if (tmp == NULL) die("realloc in rowAddchar", REALLOC_ERR);
if (tmp == NULL) die("realloc in rowAddchar", 0);
rw->chars = tmp;
/* make space for the new char */
@ -137,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) die("malloc in rowAddRow s", MALLOC_ERR);
if (s == NULL) die("malloc in rowAddRow s", 0);
/* copy the contents of the pos row after the cursor into the buffer */
memcpy(s, &b->rw[pos].chars[cur], len);
s[len] = '\0';
@ -148,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) die("realloc in rowAddRow", REALLOC_ERR);
if (p == NULL) die("realloc in rowAddRow", 0);
b->rw[pos].chars = p;
/* and terminate it with null like a good boi */
b->rw[pos].chars[cur] = '\0';
@ -179,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) die("malloc in rowCpy", MALLOC_ERR);
if (to->chars == NULL) die("malloc in rowCpy", 0);
/* And update the size */
to->size = from->size;
/* Then copy the chars from the source row to the destination row */
@ -193,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) die("realloc in rowAppendString", REALLOC_ERR);
if (temp == NULL) die("realloc in rowAppendString", 0);
rw->chars = temp;
memcpy(&rw->chars[rw->size], s, len);
@ -215,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) die("realloc in rowDeleteRow", REALLOC_ERR);
if (temp == NULL) die("realloc in rowDeleteRow", 0);
b->rw = temp;
}
@ -251,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) die ("malloc in updateRender", MALLOC_ERR);
if (rw->render == NULL) die ("malloc in updateRender", 0);
/* put all the characters (substituing all special chars)
* into the render buffer */

@ -108,7 +108,7 @@ int main (int argc, char *argv[])
bufInit(&rows);
/* Try to open the file */
if (argc < 2) die("File not found", BAD_FILE);
if (argc < 2) die("File not found", ENOENT);
fileOpen(argv[1]);
if(snprintf(t.filename, FILENAME_MAX_LENGTH, "%s", argv[1]) >= FILENAME_MAX_LENGTH)
t.state |= NAME_TRUNCATED;
@ -131,21 +131,20 @@ int main (int argc, char *argv[])
if (t.state & NEW_FILE) {
if (t.state & NAME_TRUNCATED) {
if (remove(argv[1]) == -1)
die("Could not delete file (argv)", BAD_FILE);
die("Could not delete file (argv)", EEXIST);
} else {
if (remove(t.filename) == -1)
die("Could not delete file (stored filename)", BAD_FILE);
die("Could not delete file (stored filename)", EEXIST);
}
die("File not modified", AOK);
} else {
die(NULL, AOK);
die(NULL, 0);
}
} else {
if (t.state & NAME_TRUNCATED)
fileSave(argv[1]);
else
fileSave(t.filename);
die(NULL, AOK);
die(NULL, 0);
}
break;
@ -228,7 +227,7 @@ int main (int argc, char *argv[])
/* If by chance i find myself here be sure
* end curses mode and clenaup */
die(NULL, AOK);
die(NULL, 0);
return 0;
}
@ -328,7 +327,7 @@ void drawLines (void)
lnMove(i, 0);
/* Draw the line matcing render memory */
if (&rows.rw[ln] == NULL) die("drawlines NULL", BAD_PNTR);
if (&rows.rw[ln] == NULL) die("drawlines NULL", EFAULT);
if (rows.rw[ln].r_size > t.cur.off_x) {
start = t.cur.off_x;
@ -387,7 +386,7 @@ void fileOpen (char *filename)
t.state |= NEW_FILE;
fd = fopen(filename, "a+");
if (fd == NULL)
die("Could not open file: permission denied", BAD_FILE);
die("Could not open file", 0);
}
/* Check if the file is empty first */
@ -422,7 +421,7 @@ void fileSave (char *filename)
{
FILE *fd = fopen(filename, "w");
if (fd == NULL)
die("Cannot open file (save)", SAVE_ERR);
die("Could not save file", 0);
for(int i = 0; i < rows.rownum; i++)
fprintf(fd, "%s\n", rows.rw[i].chars);
fclose(fd);

Loading…
Cancel
Save