Skip to content
This repository has been archived by the owner on Aug 15, 2019. It is now read-only.

Fix unused result #12

Merged
merged 2 commits into from
Sep 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,22 @@ void enable_raw_mode(e_context* ctx) {
}


/* sigh */
void write_wrapped(int file, const char* str, int len) {
ssize_t x = write(file, str, len);
(void) x;
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disclaimer: I'm not a C programmer in what so ever way. ;-)
Wouldn't be nice to check for return value of write and name the function following your naming convention of e_foobar?

void e_write(int file, const char* str, int len) {
    if (write(file, str, len) != len) {
        fprintf(stderr, "Cannot write to file %i with error: %s\n", file, strerror(errno));
    }
}

Copy link
Owner Author

@hellerve hellerve Sep 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your concern here. A lot of the time in C we don’t do all of the error checking we could do because we assume that some calls never fail—in e I also don’t check for failures of malloc/realloc for instance. I’m also not so sure about fprintfing the error here, especially after the last call to write failed and considering that the user could redirect stderr, leading to weird artifacts on the screen (in the best case).

As per the naming convention: the e_<function> convention is only really important for exported functions (as in, functions that are in the header) I feel, but I would agree that it’s less of a code smell to follow it regardless. I shall update it in the next commit.


void e_die(const char* s) {
write(STDOUT_FILENO, "\x1b[2J\x1b[?47l\x1b""8", 12);
write_wrapped(STDOUT_FILENO, "\x1b[2J\x1b[?47l\x1b""8", 12);
perror(s);
exit(1);
}


void e_exit() {
write(STDOUT_FILENO, "\x1b[2J\x1b[?47l\x1b""8", 12);
write_wrapped(STDOUT_FILENO, "\x1b[2J\x1b[?47l\x1b""8", 12);
exit(0);
}

Expand Down Expand Up @@ -211,7 +218,7 @@ void e_clear_screen(e_context* ctx) {
ansi_append(&ab, buf, strlen(buf));
ansi_append(&ab, "?25h", 4);

write(STDOUT_FILENO, ab.b, ab.len);
write_wrapped(STDOUT_FILENO, ab.b, ab.len);
ab_free(&ab);
}

Expand Down Expand Up @@ -1211,7 +1218,7 @@ e_context* e_setup() {
e_context* ctx = malloc(sizeof(e_context));
if (tcgetattr(STDIN_FILENO, &ctx->orig) == -1) e_die("tcgetattr");

write(STDOUT_FILENO, "\x1b""7\x1b[?47h", 8);
write_wrapped(STDOUT_FILENO, "\x1b""7\x1b[?47h", 8);
e_get_win_size(ctx);
enable_raw_mode(ctx);
ctx->rx = 0;
Expand Down
16 changes: 8 additions & 8 deletions src/syntax.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ void syntax_read_extensions(syntax* c, FILE* f, char* line) {
regcomp(reg, line, REG_EXTENDED);

while (fpeek(f) == ' ' || fpeek(f) == '\t') {
fgets(line, MAX_LINE_WIDTH, f);
ln = strlen(line)-1;
line[ln] = '\0'; // replace newline
reg = realloc(reg, sizeof(regex_t) * ++regl);
err = regcomp(&reg[regl-1], strtriml(line), REG_EXTENDED);
if (err) exit(err);
if (fgets(line, MAX_LINE_WIDTH, f)) {
ln = strlen(line)-1;
line[ln] = '\0'; // replace newline
reg = realloc(reg, sizeof(regex_t) * ++regl);
err = regcomp(&reg[regl-1], strtriml(line), REG_EXTENDED);
if (err) exit(err);
}
}
c->matchlen = regl;
c->filematch = reg;
Expand All @@ -51,8 +52,7 @@ void syntax_read_pattern(syntax* c, FILE* f, char* key, char* value) {
err = regcomp(&pat->pattern, value, REG_EXTENDED);
if (err) exit(err);

if (fpeek(f) == ' ' || fpeek(f) == '\t') {
fgets(line, MAX_LINE_WIDTH, f);
if ((fpeek(f) == ' ' || fpeek(f) == '\t') && fgets(line, MAX_LINE_WIDTH, f)) {
char* l = strtriml(line);
ln = strlen(l)-1;
memmove(l+1, l, ln);
Expand Down
2 changes: 1 addition & 1 deletion src/syntax.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "colors.h"
#include "util.h"

#define MAX_LINE_WIDTH 512
#define MAX_LINE_WIDTH 1025

typedef struct pattern {
regex_t pattern;
Expand Down