main
process.c
1/* Copyright (c) 1985 Ceriel J.H. Jacobs */
2
3#include "in_all.h"
4#include "process.h"
5#include "commands.h"
6#include "display.h"
7#include "prompt.h"
8#include "getline.h"
9#include "getcomm.h"
10#include "main.h"
11#include "options.h"
12#include "output.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
60 static char *dummies[3];
61 long arg;
62
63 if (!(nfiles = n)) {
64 /*
65 * Input from pipe
66 */
67 currentfile = "standard-input";
68 /*
69 * Take care that *(filenames - 1) and *(filenames + 1) are 0
70 */
71 filenames = &dummies[1];
72 d_clean();
73 do_clean();
74 } else {
75 filenames = argv;
76 (void)nextfile(0);
77 }
78 *--argv = 0;
79 if (startcomm) {
80 n = getcomm(&arg);
81 if (commands[n].c_flags & NEEDS_SCREEN) {
82 redraw(0);
83 }
84 do_comm(n, arg);
85 startcomm = 0;
86 }
87 redraw(1);
88 if (setjmp(SetJmpBuf)) {
89 nflush();
90 redraw(1);
91 }
92 DoneSetJmp = 1;
93 for (;;) {
94 interrupt = 0;
95 n = getcomm(&arg);
96 do_comm(n, arg);
97 }
98}
99
100/*
101 * Get the next file the user asks for.
102 */
103
104int
105nextfile(int n)
106{
107 int i;
108
109 if ((i = filecount + n) >= nfiles || i < 0) {
110 return 1;
111 }
112 filecount = i;
113 visitfile(filenames[i]);
114 return 0;
115}