Fix stdin support
This commit is contained in:
parent
ab10a9c217
commit
f853dabe7f
1
Makefile
1
Makefile
@ -22,6 +22,7 @@ $(ODIR)/%.o: %.c $(DEPS)
|
||||
$(CC) -c -o $@ $< $(CFLAGS) $(LIB)
|
||||
|
||||
$(NAME): $(OBJ)
|
||||
@echo "--don't forget to set $(NAME)'s version--"
|
||||
$(CC) -o $(BINDIR)/$@ $^ $(CFLAGS) $(LIB)
|
||||
|
||||
.PHONY: prep
|
||||
|
@ -2,9 +2,10 @@
|
||||
#define ARG_H
|
||||
#include <stdbool.h>
|
||||
|
||||
#define LAT_VERSION "0.4.0"
|
||||
#define LAT_VERSION "0.7.3"
|
||||
|
||||
struct config {
|
||||
bool stdin;
|
||||
bool process;
|
||||
bool color;
|
||||
bool lines;
|
||||
|
@ -1,7 +1,8 @@
|
||||
#ifndef FILE_H
|
||||
#define FILE_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct filedata readfile(FILE *fp);
|
||||
struct filedata readfile(FILE *fp, bool isstdin);
|
||||
#endif
|
||||
|
@ -5,7 +5,7 @@
|
||||
#include "types.h"
|
||||
#include "util.h"
|
||||
|
||||
struct filedata readfile(FILE *fp) {
|
||||
struct filedata readfile(FILE *fp, bool isstdin) {
|
||||
struct filedata f;
|
||||
|
||||
f.lc = 0;
|
||||
@ -15,6 +15,38 @@ struct filedata readfile(FILE *fp) {
|
||||
f.buf = NULL;
|
||||
f.lines = NULL;
|
||||
|
||||
if (isstdin) {
|
||||
size_t bufsize = 1024;
|
||||
f.buf = malloc(bufsize);
|
||||
if (f.buf == NULL)
|
||||
die("malloc");
|
||||
|
||||
char c;
|
||||
while (fread(&c, 1, 1, fp) > 0) {
|
||||
if (f.buflen == bufsize - 1) {
|
||||
bufsize *= 2;
|
||||
|
||||
char *new_buf = realloc(f.buf, bufsize);
|
||||
if (new_buf == NULL)
|
||||
die("realloc");
|
||||
|
||||
f.buf = new_buf;
|
||||
}
|
||||
f.buf[f.buflen++] = c;
|
||||
}
|
||||
|
||||
if (f.buflen < bufsize - 1) {
|
||||
char *new_buf = realloc(f.buf, f.buflen + 1);
|
||||
if (new_buf == NULL)
|
||||
die("realloc");
|
||||
|
||||
f.buf = new_buf;
|
||||
}
|
||||
f.buf[f.buflen] = '\0';
|
||||
|
||||
return f;
|
||||
}
|
||||
|
||||
// expects to be at beginning of file
|
||||
fseek(fp, 0, SEEK_END);
|
||||
f.buflen = ftell(fp);
|
||||
|
14
src/main.c
14
src/main.c
@ -21,7 +21,7 @@ void run(FILE *fp, char *filename, bool tty) {
|
||||
const char *reset = conf.color ? RESET : "";
|
||||
|
||||
struct filedata f;
|
||||
f = readfile(fp);
|
||||
f = readfile(fp, conf.stdin);
|
||||
|
||||
if (tty) {
|
||||
char *addon = f.binary ? "<binary>" : "";
|
||||
@ -40,7 +40,7 @@ void run(FILE *fp, char *filename, bool tty) {
|
||||
for (int i = 0; i < f.lc; i++) {
|
||||
if (conf.lines) {
|
||||
char *padding = linepad(linecount, f.lc);
|
||||
printf("%s%s%d:%s %s\n", grey, padding, i, reset, f.lines[i].buf);
|
||||
printf("%s%s%d:%s %s\n", grey, padding, i + 1, reset, f.lines[i].buf);
|
||||
free(padding);
|
||||
linecount++;
|
||||
} else {
|
||||
@ -60,14 +60,12 @@ void run(FILE *fp, char *filename, bool tty) {
|
||||
float rounded;
|
||||
char *format = formatbytes(f.buflen, &rounded);
|
||||
|
||||
// char *cnewline = c == '\n' ? "" : "\n";
|
||||
char *cnewline = "";
|
||||
fprintf(stderr, "\r%s%s%.2f %s%s\r\n", cnewline, invert_t, rounded, format,
|
||||
reset);
|
||||
fprintf(stderr, "\r%s%.2f %s%s\r\n", invert_t, rounded, format, reset);
|
||||
}
|
||||
}
|
||||
|
||||
void initconf(void) {
|
||||
conf.stdin = false;
|
||||
conf.process = true;
|
||||
conf.color = true;
|
||||
conf.lines = true;
|
||||
@ -98,10 +96,12 @@ int main(int argc, char *argv[]) {
|
||||
if (conf.has_read_stdin)
|
||||
clearstdin();
|
||||
conf.has_read_stdin = true;
|
||||
conf.stdin = true;
|
||||
run(stdin, "stdin", tty);
|
||||
continue;
|
||||
}
|
||||
|
||||
conf.stdin = false;
|
||||
FILE *fp = fopen(argv[i], "rb");
|
||||
if (fp == NULL)
|
||||
die(argv[i]);
|
||||
@ -114,9 +114,11 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
|
||||
if (offset == argc) {
|
||||
conf.stdin = true;
|
||||
run(stdin, "stdin", tty);
|
||||
}
|
||||
} else {
|
||||
conf.stdin = true;
|
||||
run(stdin, "stdin", tty); // for piped-input or repl-like behavior
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user