aruu

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

main.c (3002B)


      1 /* copyright (c) 1985 ceriel J.H. jacobs */
      2 
      3 #include "main.h"
      4 #include "commands.h"
      5 #include "display.h"
      6 #include "in_all.h"
      7 #include "options.h"
      8 #include "output.h"
      9 #include "process.h"
     10 #include "prompt.h"
     11 #include "paths.h"
     12 #include "term.h"
     13 #include "wexec.h"
     14 
     15 int   nopipe;
     16 char *progname;
     17 int   interrupt;
     18 int   no_tty;
     19 
     20 static int  initialize(int x);
     21 static void sigquit(int signo);
     22 #ifdef SIGTSTP
     23 static void suspsig(int signo);
     24 #endif
     25 
     26 int
     27 main(int argc, char **argv)
     28 {
     29   char **av;
     30 
     31   (void)setlocale(LC_CTYPE, "");
     32   if (!isatty(1)) {
     33     no_tty = 1;
     34   }
     35   argv[argc] = 0;
     36   progname   = argv[0];
     37   if ((av = readoptions(argv)) == (char **)0 || initialize(*av ? 1 : 0)) {
     38     if (no_tty) {
     39       close(1);
     40       (void)dup(2);
     41     }
     42     putline("Usage: ");
     43     putline(argv[0]);
     44     putline(" [-c] [-u] [-n] [-q] [-number] [+command] [file ... ]\n");
     45     flush();
     46     exit(1);
     47   }
     48   if (no_tty) {
     49     *--av = "cat";
     50     wexecve_self(ARUU_PATH_BIN "/cat", av, (char *const[]){NULL});
     51   } else
     52     processfiles(argc - (av - argv), av);
     53   (void)quit();
     54   /* NOTREACHED */
     55 }
     56 
     57 /*
     58  * open temporary file for reading and writing
     59  * panic if it fails
     60  */
     61 
     62 /*
     63  * collect initializing stuff here
     64  */
     65 
     66 static int
     67 initialize(int x)
     68 {
     69   if (!(nopipe = x)) {
     70     /*
     71      * reading from pipe
     72      */
     73     if (isatty(0)) {
     74       return 1;
     75     }
     76     stdf = dup(0); /* duplicate file descriptor of input */
     77     if (no_tty)
     78       return 0;
     79     /*
     80      * make sure standard input is from the terminal
     81      */
     82     (void)close(0);
     83     if (open("/dev/tty", O_RDONLY, 0) != 0) {
     84       putline("Couldn't open terminal\n");
     85       flush();
     86       exit(1);
     87     }
     88   }
     89   if (no_tty)
     90     return 0;
     91   /*
     92    * handle signals
     93    * catch QUIT, DELETE and ^z
     94    */
     95   (void)signal(SIGQUIT, SIG_IGN);
     96   (void)signal(SIGINT, catchdel);
     97   ini_terminal();
     98 #ifdef SIGTSTP
     99   if (signal(SIGTSTP, SIG_IGN) == SIG_DFL) {
    100     (void)signal(SIGTSTP, suspsig);
    101   }
    102 #endif
    103   (void)signal(SIGQUIT, sigquit);
    104   return 0;
    105 }
    106 
    107 void
    108 catchdel(int signo)
    109 {
    110   (void)signo;
    111   (void)signal(SIGINT, catchdel);
    112   interrupt = 1;
    113 }
    114 
    115 static void
    116 sigquit(int signo)
    117 {
    118   (void)signo;
    119   quit();
    120 }
    121 
    122 #ifdef SIGTSTP
    123 
    124 /*
    125  * we had a SIGTSTP signal
    126  * suspend, by a call to this routine
    127  */
    128 
    129 void
    130 suspend(void)
    131 {
    132   nflush();
    133   resettty();
    134   (void)signal(SIGTSTP, SIG_DFL);
    135   (void)kill(0, SIGTSTP);
    136   /*
    137    * we are not here anymore ...
    138    *
    139    *
    140    *
    141    * but we arive here ...
    142    */
    143   inittty();
    144   putline(TI);
    145   flush();
    146   (void)signal(SIGTSTP, suspsig);
    147 }
    148 
    149 /*
    150  * SIGTSTP signal handler
    151  * just indicate that we had one, ignore further ones and return
    152  */
    153 
    154 static void
    155 suspsig(int signo)
    156 {
    157   (void)signo;
    158 
    159   suspend();
    160   if (DoneSetJmp)
    161     longjmp(SetJmpBuf, 1);
    162 }
    163 #endif
    164 
    165 /*
    166  * quit : called on exit
    167  * i bet you guessed that much
    168  */
    169 
    170 int
    171 quit(void)
    172 {
    173   clrbline();
    174   resettty();
    175   flush();
    176   exit(0);
    177 }
    178 
    179 /*
    180  * exit, but nonvoluntarily
    181  * at least tell the user why
    182  */
    183 
    184 void
    185 panic(char *s)
    186 {
    187   putline("\a\a\a\r\n");
    188   putline(s);
    189   putline("\r\n");
    190   quit();
    191 }