summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2022-04-19 19:43:48 -0400
committerJakob Kaivo <jkk@ung.org>2022-04-19 19:43:48 -0400
commita63233cd95c5a7ac9a61774f121e3e83488cafa8 (patch)
tree43766ca8ddc3e42be02752a5b4bf726bfcecfb96
parent5f87ff233faf60581255b919debb2dfa0ce1f0c9 (diff)
merge everything into more.c
-rw-r--r--Makefile1
-rw-r--r--io.c86
-rw-r--r--more.c40
-rw-r--r--more.h20
4 files changed, 26 insertions, 121 deletions
diff --git a/Makefile b/Makefile
index 926fcc7..8f8f64a 100644
--- a/Makefile
+++ b/Makefile
@@ -23,7 +23,6 @@ install: $(BINDIR)/more
cp $(BINDIR)/more $(DESTDIR)/bin
$(BINDIR)/more: $(OBJDIR)/more.o
-$(OBJDIR)/more.o: $(SRCDIR)/more.h
$(OBJDIR)/more.o: $(SRCDIR)/more.c
$(CC) $(CFLAGS) -o $@ -c $(SRCDIR)/more.c
diff --git a/io.c b/io.c
deleted file mode 100644
index 6d6c328..0000000
--- a/io.c
+++ /dev/null
@@ -1,86 +0,0 @@
-#define _XOPEN_SOURCE 700
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <unistd.h>
-
-#include "more.h"
-
-ssize_t more_getline(struct more_file *mf, size_t lineno)
-{
- if (mf->nlines <= lineno && mf->nlines != 0) {
- fsetpos(mf->f, &(mf->tlines[mf->nlines - 1]));
- getline(&(mf->buf), &(mf->nbuf), mf->f);
- }
-
- while (mf->nlines <= lineno) {
- mf->nlines++;
- mf->tlines = realloc(mf->tlines, mf->nlines * sizeof(*mf->tlines));
- mf->bytepos = realloc(mf->bytepos, mf->nlines * sizeof(*mf->bytepos));
-
- fgetpos(mf->f, &(mf->tlines[mf->nlines - 1]));
-
- if (getline(&(mf->buf), &(mf->nbuf), mf->f) == -1) {
- return -1;
- }
-
- if (mf->nlines > 1) {
- mf->bytepos[mf->nlines - 1] = mf->bytepos[mf->nlines - 2] + strlen(mf->buf);
- } else {
- mf->bytepos[0] = 0;
- }
-
- if (mf->backing != mf->f) {
- fgetpos(mf->backing, &(mf->tlines[mf->nlines - 1]));
- fputs(mf->buf, mf->backing);
- }
- }
-
- fsetpos(mf->backing, &(mf->tlines[lineno]));
- return getline(&(mf->buf), &(mf->nbuf), mf->backing);
-}
-
-struct more_file more_open(const char *path)
-{
- struct more_file mf = {
- .f = stdin,
- };
-
- if (strcmp(path, "-")) {
- mf.f = fopen(path, "r");
- if (!mf.f) {
- fprintf(stderr, "more: %s: %s\r\n", path, strerror(errno));
- return mf;
- }
- }
-
- fpos_t pos;
- if (fgetpos(mf.f, &pos) != 0) {
- mf.backing = tmpfile();
- } else {
- mf.backing = mf.f;
- struct stat st;
- fstat(fileno(mf.f), &st);
- mf.nbytes = st.st_size;
- }
- mf.path = strdup(path);
-
- return mf;
-}
-
-void more_close(struct more_file *mf)
-{
- if (mf->backing != mf->f) {
- fclose(mf->backing);
- }
-
- if (mf->f != stdin) {
- fclose(mf->f);
- }
-
- free(mf->tlines);
- free(mf->buf);
- free(mf->path);
-}
diff --git a/more.c b/more.c
index 8028263..08aea79 100644
--- a/more.c
+++ b/more.c
@@ -14,10 +14,24 @@
#include <unistd.h>
#include <wordexp.h>
-#include "more.h"
+struct more_file {
+ struct more_file *next;
+ struct more_file *prev;
+ FILE *f;
+ FILE *backing;
+ size_t topline;
+ fpos_t *tlines;
+ size_t nlines;
+ size_t mark[26];
+ size_t nbytes;
+ size_t *bytepos;
+ char *buf;
+ size_t nbuf;
+ char *path;
+};
-static int more_clear = 0;
static int retval = 0;
+static int more_clear = 0;
static WINDOW *more_win = NULL;
static WINDOW *more_status = NULL;
@@ -32,7 +46,7 @@ enum {
static int more(const char *);
-ssize_t more_getline(struct more_file *mf, size_t lineno)
+static ssize_t more_getline(struct more_file *mf, size_t lineno)
{
if (mf->nlines <= lineno && mf->nlines != 0) {
fsetpos(mf->f, &(mf->tlines[mf->nlines - 1]));
@@ -66,7 +80,7 @@ ssize_t more_getline(struct more_file *mf, size_t lineno)
return getline(&(mf->buf), &(mf->nbuf), mf->backing);
}
-struct more_file more_open(const char *path)
+static struct more_file more_open(const char *path)
{
struct more_file mf = {
.f = stdin,
@@ -94,7 +108,7 @@ struct more_file more_open(const char *path)
return mf;
}
-void more_close(struct more_file *mf)
+static void more_close(struct more_file *mf)
{
if (mf->backing != mf->f) {
fclose(mf->backing);
@@ -126,7 +140,7 @@ static void more_scroll(struct more_file *mf, int count, int multiple)
{
int by = count ? count * multiple : multiple;
- if (abs(by) >= LINES && more_clear) {
+ if (more_clear) {
clear();
}
@@ -195,12 +209,10 @@ static void more_help(void)
fprintf(f, "\n");
fprintf(f, "Scrolling Commands: Prefix with a count for number of lines, otherwise as shown\n");
- fprintf(f, "f ^F forward one screen\n");
- fprintf(f, "b ^B backward one screen\n");
- fprintf(f, "j <space> <newline> forward one line\n");
- fprintf(f, "k backward one line\n");
- fprintf(f, "d ^D forward one half screen\n");
- fprintf(f, "u ^U backward one half screen\n");
+ fprintf(f, "%-20s%-30s%-10s\n", " ", "forward", "backward");
+ fprintf(f, "%-20s%-30s%-10s\n", "one line", "j <space> <newline>", "k");
+ fprintf(f, "%-20s%-30s%-10s\n", "one half screen", "d ^D", "u ^U");
+ fprintf(f, "%-20s%-30s%-10s\n", "one screen", "f ^F", "b ^B");
fprintf(f, "\n");
fprintf(f, "Jumping Commands\n");
@@ -680,13 +692,13 @@ int main(int argc, char *argv[])
cbreak();
noecho();
+ atexit(more_exit);
+
more_win = newwin(LINES - 1, 0, 0, 0);
scrollok(more_win, TRUE);
more_status = newwin(1, 0, LINES - 1, 0);
touchwin(stdscr);
- atexit(more_exit);
-
if (more_clear) {
clear();
}
diff --git a/more.h b/more.h
deleted file mode 100644
index 5fcd9a2..0000000
--- a/more.h
+++ /dev/null
@@ -1,20 +0,0 @@
-#define _XOPEN_SOURCE 700
-#include <stdio.h>
-
-struct more_file {
- FILE *f;
- FILE *backing;
- size_t topline;
- fpos_t *tlines;
- size_t nlines;
- size_t mark[26];
- size_t nbytes;
- size_t *bytepos;
- char *buf;
- size_t nbuf;
- char *path;
-};
-
-struct more_file more_open(const char *path);
-void more_close(struct more_file *mf);
-ssize_t more_getline(struct more_file *mf, size_t lineno);