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_ #ifndef _HELPER_DIE_H_
#define _HELPER_DIE_H_ #define _HELPER_DIE_H_
#include "config.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h>
#ifdef FRONTEND_NCURSES #ifdef FRONTEND_NCURSES
#include <ncurses.h> #include <ncurses.h>
#endif #endif
typedef enum { void die (const char *message, int err)
AOK,
GENERIC_ERR,
BAD_FILE,
SAVE_ERR,
MALLOC_ERR,
REALLOC_ERR,
BAD_PNTR
} DeathStatus;
void die (const char *message, DeathStatus sn)
{ {
#ifdef FRONTEND_NCURSES #ifdef FRONTEND_NCURSES
erase(); erase();
@ -26,9 +19,20 @@ void die (const char *message, DeathStatus sn)
endwin(); endwin();
#endif #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); perror(message);
exit(sn); exit(errno);
}
exit(-1);
} }
#endif #endif

@ -66,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) die("realloc in rowAddLast", REALLOC_ERR); if (newr == NULL) die("realloc in rowAddLast", 0);
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) 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); 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;
@ -88,7 +88,7 @@ void rowAddChar (Row *rw, char c, int pos)
/* extend the string */ /* extend the string */
rw->size++; rw->size++;
char *tmp = realloc(rw->chars, rw->size + 1); 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; rw->chars = tmp;
/* make space for the new char */ /* make space for the new char */
@ -137,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) 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 */ /* 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';
@ -148,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) die("realloc in rowAddRow", REALLOC_ERR); if (p == NULL) die("realloc in rowAddRow", 0);
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';
@ -179,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) die("malloc in rowCpy", MALLOC_ERR); if (to->chars == NULL) die("malloc in rowCpy", 0);
/* 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 */
@ -193,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) die("realloc in rowAppendString", REALLOC_ERR); if (temp == NULL) die("realloc in rowAppendString", 0);
rw->chars = temp; rw->chars = temp;
memcpy(&rw->chars[rw->size], s, len); memcpy(&rw->chars[rw->size], s, len);
@ -215,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) die("realloc in rowDeleteRow", REALLOC_ERR); if (temp == NULL) die("realloc in rowDeleteRow", 0);
b->rw = temp; b->rw = temp;
} }
@ -251,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) die ("malloc in updateRender", MALLOC_ERR); if (rw->render == NULL) die ("malloc in updateRender", 0);
/* put all the characters (substituing all special chars) /* put all the characters (substituing all special chars)
* into the render buffer */ * into the render buffer */

@ -108,7 +108,7 @@ int main (int argc, char *argv[])
bufInit(&rows); bufInit(&rows);
/* Try to open the file */ /* 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]); fileOpen(argv[1]);
if(snprintf(t.filename, FILENAME_MAX_LENGTH, "%s", argv[1]) >= FILENAME_MAX_LENGTH) if(snprintf(t.filename, FILENAME_MAX_LENGTH, "%s", argv[1]) >= FILENAME_MAX_LENGTH)
t.state |= NAME_TRUNCATED; t.state |= NAME_TRUNCATED;
@ -131,21 +131,20 @@ int main (int argc, char *argv[])
if (t.state & NEW_FILE) { if (t.state & NEW_FILE) {
if (t.state & NAME_TRUNCATED) { if (t.state & NAME_TRUNCATED) {
if (remove(argv[1]) == -1) if (remove(argv[1]) == -1)
die("Could not delete file (argv)", BAD_FILE); die("Could not delete file (argv)", EEXIST);
} else { } else {
if (remove(t.filename) == -1) 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 { } else {
die(NULL, AOK); die(NULL, 0);
} }
} else { } else {
if (t.state & NAME_TRUNCATED) if (t.state & NAME_TRUNCATED)
fileSave(argv[1]); fileSave(argv[1]);
else else
fileSave(t.filename); fileSave(t.filename);
die(NULL, AOK); die(NULL, 0);
} }
break; break;
@ -228,7 +227,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 */
die(NULL, AOK); die(NULL, 0);
return 0; return 0;
} }
@ -328,7 +327,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) die("drawlines NULL", BAD_PNTR); if (&rows.rw[ln] == NULL) die("drawlines NULL", EFAULT);
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;
@ -387,7 +386,7 @@ void fileOpen (char *filename)
t.state |= NEW_FILE; t.state |= NEW_FILE;
fd = fopen(filename, "a+"); fd = fopen(filename, "a+");
if (fd == NULL) 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 */ /* Check if the file is empty first */
@ -422,7 +421,7 @@ void fileSave (char *filename)
{ {
FILE *fd = fopen(filename, "w"); FILE *fd = fopen(filename, "w");
if (fd == NULL) if (fd == NULL)
die("Cannot open file (save)", SAVE_ERR); die("Could not save file", 0);
for(int i = 0; i < rows.rownum; i++) for(int i = 0; i < rows.rownum; i++)
fprintf(fd, "%s\n", rows.rw[i].chars); fprintf(fd, "%s\n", rows.rw[i].chars);
fclose(fd); fclose(fd);

Loading…
Cancel
Save