aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

process.c (1913B)


      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 
     14 jmp_buf SetJmpBuf;
     15 int     DoneSetJmp;
     16 int     stdf;
     17 int     filecount;
     18 char  **filenames;
     19 char   *currentfile;
     20 long    maxpos;
     21 
     22 static int nfiles; /* number of filenames on command line */
     23 
     24 /*
     25  * visit a file, file name is "fn"
     26  */
     27 
     28 void
     29 visitfile(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 
     56 void
     57 processfiles(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 
    103 int
    104 nextfile(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 }