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