1/* Copyright (c) 1985 Ceriel J.H. Jacobs */
2
3#include "process.h"
4#include "commands.h"
5#include "display.h"
6#include "getcomm.h"
7#include "getline.h"
8#include "in_all.h"
9#include "main.h"
10#include "options.h"
11#include "output.h"
12#include "prompt.h"
13
14jmp_buf SetJmpBuf;
15int DoneSetJmp;
16int stdf;
17int filecount;
18char **filenames;
19char *currentfile;
20long maxpos;
21
22static int nfiles; /* Number of filenames on command line */
23
24/*
25 * Visit a file, file name is "fn".
26 */
27
28void
29visitfile(char *fn)
30{
31 struct stat statbuf;
32
33 if (stdf > 0) {
34 /*
35 * Close old input file
36 */
37 (void)close(stdf);
38 }
39 currentfile = fn;
40 if ((stdf = open(fn, O_RDONLY, 0)) < 0) {
41 error(": could not open");
42 maxpos = 0;
43 } else { /* Get size for percentage in prompt */
44 (void)fstat(stdf, &statbuf);
45 maxpos = statbuf.st_size;
46 }
47 do_clean();
48 d_clean();
49}
50
51/*
52 * process the input files, one by one.
53 * If there is none, input is from a pipe.
54 */
55
56void
57processfiles(int n, char **argv)
58{
59 static char *dummies[3];
60 long arg;
61
62 if (!(nfiles = n)) {
63 /*
64 * Input from pipe
65 */
66 currentfile = "standard-input";
67 /*
68 * Take care that *(filenames - 1) and *(filenames + 1) are 0
69 */
70 filenames = &dummies[1];
71 d_clean();
72 do_clean();
73 } else {
74 filenames = argv;
75 (void)nextfile(0);
76 }
77 *--argv = 0;
78 if (startcomm) {
79 n = getcomm(&arg);
80 if (commands[n].c_flags & NEEDS_SCREEN) {
81 redraw(0);
82 }
83 do_comm(n, arg);
84 startcomm = 0;
85 }
86 redraw(1);
87 if (setjmp(SetJmpBuf)) {
88 nflush();
89 redraw(1);
90 }
91 DoneSetJmp = 1;
92 for (;;) {
93 interrupt = 0;
94 n = getcomm(&arg);
95 do_comm(n, arg);
96 }
97}
98
99/*
100 * Get the next file the user asks for.
101 */
102
103int
104nextfile(int n)
105{
106 int i;
107
108 if ((i = filecount + n) >= nfiles || i < 0) {
109 return 1;
110 }
111 filecount = i;
112 visitfile(filenames[i]);
113 return 0;
114}