summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2022-04-19 19:57:36 -0400
committerJakob Kaivo <jkk@ung.org>2022-04-19 19:57:36 -0400
commit66171c2ef0e4348a9f3929d1f1b3cea0b5098c83 (patch)
treef64ca77018cf24ea6e42c905a407f25ad241846a
parentb11c0350874c49b91eb0875f2b418a9130d39b4e (diff)
change to a linked list for files to visit
-rw-r--r--more.c122
1 files changed, 62 insertions, 60 deletions
diff --git a/more.c b/more.c
index d89f8fc..19184b8 100644
--- a/more.c
+++ b/more.c
@@ -54,7 +54,6 @@ struct more_file {
char *path;
};
-static int retval = 0;
static int more_clear = 0;
static WINDOW *more_win = NULL;
static WINDOW *more_status = NULL;
@@ -68,8 +67,6 @@ enum {
CTRL_U = 0x15,
};
-static int more(const char *);
-
static ssize_t more_getline(struct more_file *mf, size_t lineno)
{
if (mf->nlines <= lineno && mf->nlines != 0) {
@@ -104,32 +101,29 @@ static ssize_t more_getline(struct more_file *mf, size_t lineno)
return getline(&(mf->buf), &(mf->nbuf), mf->backing);
}
-static struct more_file more_open(const char *path)
+static int more_open(struct more_file *mf)
{
- 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;
+ mf->f = stdin;
+
+ if (strcmp(mf->path, "-")) {
+ mf->f = fopen(mf->path, "r");
+ if (!mf->f) {
+ fprintf(stderr, "more: %s: %s\r\n", mf->path, strerror(errno));
+ return 1;
}
}
fpos_t pos;
- if (fgetpos(mf.f, &pos) != 0) {
- mf.backing = tmpfile();
+ if (fgetpos(mf->f, &pos) != 0) {
+ mf->backing = tmpfile();
} else {
- mf.backing = mf.f;
+ mf->backing = mf->f;
struct stat st;
- fstat(fileno(mf.f), &st);
- mf.nbytes = st.st_size;
+ fstat(fileno(mf->f), &st);
+ mf->nbytes = st.st_size;
}
- mf.path = strdup(path);
- return mf;
+ return 0;
}
static void more_close(struct more_file *mf)
@@ -144,7 +138,6 @@ static void more_close(struct more_file *mf)
free(mf->tlines);
free(mf->buf);
- free(mf->path);
}
static void more_refresh(struct more_file *mf)
@@ -259,7 +252,7 @@ static void more_help(void)
fclose(f);
- more(path);
+ //more(path);
remove(path);
}
@@ -298,7 +291,7 @@ static struct more_file more_invoke_editor(struct more_file *mf)
waitpid(pid, NULL, 0);
- ret = more_open(path);
+ //ret = more_open(path);
ret.topline = line;
free(path);
} else {
@@ -341,6 +334,7 @@ static struct more_file more_examine(struct more_file *mf)
strcpy(filename, mf->path);
}
+ /*
struct more_file ret = more_open(filename);
if (ret.f == NULL) {
wprintw(more_status, "%s: %s", filename, strerror(errno));
@@ -350,26 +344,26 @@ static struct more_file more_examine(struct more_file *mf)
more_close(mf);
more_refresh(&ret);
return ret;
+ */
+ return *mf;
}
-static int more(const char *path)
+static int more(struct more_file *mf)
{
- struct more_file mf = more_open(path);
-
- if (mf.f == NULL) {
- retval = 1;
+ if (more_open(mf) != 0) {
+ /* FIXME: try next file */
return 1;
}
- more_refresh(&mf);
+ more_refresh(mf);
wrefresh(more_win);
int count = 0;
- while (mf.f) {
+ while (mf->f) {
int at_eof = 0; //(mf.topline + LINES >= mf.nlines);
wmove(more_status, 0, 0);
if (at_eof) {
- wprintw(more_status, "%s <EOF>", mf.path);
+ wprintw(more_status, "%s <EOF>", mf->path);
}
int c = wgetch(more_status);
wclear(more_status);
@@ -392,7 +386,7 @@ static int more(const char *path)
if (count == 0) {
count = LINES - 1;
}
- more_scroll(&mf, count, 1);
+ more_scroll(mf, count, 1);
break;
case 'b':
@@ -400,7 +394,7 @@ static int more(const char *path)
if (count == 0) {
count = LINES - 1;
}
- more_scroll(&mf, count, -1);
+ more_scroll(mf, count, -1);
break;
case ' ':
@@ -411,11 +405,11 @@ static int more(const char *path)
if (at_eof) {
return 1;
}
- more_scroll(&mf, count, 1);
+ more_scroll(mf, count, 1);
break;
case 'k':
- more_scroll(&mf, count, -1);
+ more_scroll(mf, count, -1);
break;
case 'd':
@@ -423,7 +417,7 @@ static int more(const char *path)
if (at_eof) {
return 1;
}
- more_scroll(&mf, count, (LINES - 1) / 2);
+ more_scroll(mf, count, (LINES - 1) / 2);
break;
case 's':
@@ -431,21 +425,21 @@ static int more(const char *path)
return 1;
}
count = count ? count : 1;
- more_scroll(&mf, LINES - 1 + count, 1);
+ more_scroll(mf, LINES - 1 + count, 1);
break;
case 'u':
case CTRL_U:
- more_scroll(&mf, count, -(LINES - 1) / 2);
+ more_scroll(mf, count, -(LINES - 1) / 2);
break;
case 'G':
if (count == 0) {
- count = mf.nbytes;
+ count = mf->nbytes;
}
/* FALLTHRU */
case 'g':
- more_scroll(&mf, count - mf.topline, 1);
+ more_scroll(mf, count - mf->topline, 1);
break;
case 'R':
@@ -453,15 +447,15 @@ static int more(const char *path)
/* FALLTHRU */
case 'r':
case CTRL_L:
- more_refresh(&mf);
+ more_refresh(mf);
break;
case 'm':
- mark(&mf);
+ mark(mf);
break;
case '\'':
- jump(&mf);
+ jump(mf);
break;
case '/':
@@ -490,15 +484,15 @@ static int more(const char *path)
switch (c2) {
case 'e':
- mf = more_examine(&mf);
+ more_examine(mf);
break;
case 'n':
- more_close(&mf);
+ more_close(mf);
return count ? count : 1;
case 'p':
- more_close(&mf);
+ more_close(mf);
return count ? -count : -1;
case 't':
@@ -514,12 +508,12 @@ static int more(const char *path)
break;
case 'v':
- mf = more_invoke_editor(&mf);
+ more_invoke_editor(mf);
break;
case '=':
case CTRL_G:
- wprintw(more_status, "%s; File %d/%d; Line %zd; Byte %zd/%zd; %zd%%", mf.path, 0, 0, mf.topline, mf.bytepos[mf.topline], mf.nbytes, 100 * mf.bytepos[mf.topline] / mf.nbytes);
+ wprintw(more_status, "%s; File %d/%d; Line %zd; Byte %zd/%zd; %zd%%", mf->path, 0, 0, mf->topline, mf->bytepos[mf->topline], mf->nbytes, 100 * mf->bytepos[mf->topline] / mf->nbytes);
break;
case 'Z':
@@ -553,7 +547,7 @@ static int more(const char *path)
wrefresh(more_win);
}
- more_close(&mf);
+ more_close(mf);
return 0;
}
@@ -727,22 +721,30 @@ int main(int argc, char *argv[])
clear();
}
- if (optind >= argc) {
- more("-");
- }
+ struct more_file *head = NULL;
+ do {
+ struct more_file *mf = calloc(1, sizeof(*mf));
+ if (!mf) {
+ perror("more");
+ return 1;
+ }
+ mf->path = argv[optind];
- int min = optind;
- while (optind < argc) {
- int next = more(argv[optind]);
- optind += next;
- if (optind < min) {
- optind = min;
+ if (head == NULL) {
+ head = mf;
+ } else {
+ struct more_file *c = head;
+ while (c->next != NULL) {
+ c = c->next;
+ }
+ c->next = mf;
+ mf->prev = c;
}
- }
+ } while (argv[optind++]);
(void)fastexit;
(void)ignorecase;
(void)backspace;
- return retval;
+ return more(head);
}