home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d034 / less.lha / Less / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-09-03  |  4.9 KB  |  305 lines

  1. /*
  2.  * Entry point, initialization, miscellaneous routines.
  3.  */
  4.  
  5. #include "less.h"
  6. #include "position.h"
  7. #include <setjmp.h>
  8.  
  9. public int      ispipe;
  10. public jmp_buf  main_loop;
  11. public char *   first_cmd;
  12. public char *   every_first_cmd;
  13. public int      new_file;
  14. public int      is_tty;
  15. public char     current_file[128];
  16. public int ac;
  17. public char **av;
  18. public int curr_ac;
  19. #if EDITOR
  20. public char *   editor;
  21. #endif
  22.  
  23. extern int file;
  24. extern int nbufs;
  25. extern int sigs;
  26. extern int quit_at_eof;
  27. extern int p_nbufs, f_nbufs;
  28. extern int back_scroll;
  29. extern int top_scroll;
  30. extern int sc_height;
  31.  
  32.  
  33. /*
  34.  * Edit a new file.
  35.  * Filename "-" means standard input.
  36.  * No filename means the "current" file, from the command line.
  37.  */
  38.     public void
  39. edit(filename)
  40.     char *filename;
  41. {
  42.     register int f;
  43.     char message[100];
  44.     static int any_edited = 0;
  45.     static int hold_scroll = 0;
  46.  
  47.     if (filename == NULL || *filename == '\0')
  48.     {
  49.         if (curr_ac >= ac)
  50.         {
  51.             error("No current file");
  52.             return;
  53.         }
  54.         filename = av[curr_ac];
  55.     }
  56.     if (strcmp(filename, "-") == 0)
  57.         f = 0;  /* Standard input */
  58.     else if ((f = open(filename, 0)) < 0)
  59.     {
  60. #ifdef amiga
  61.         sprintf(message, "Cannot open %s", filename);
  62. #else
  63.         sprintf(message, "Cannot open %.*s", error_width()-13, filename);
  64. #endif
  65.         if (any_edited)
  66.             error(message);
  67.         else
  68.         {
  69.             puts(message);
  70. #ifdef amiga
  71.             flush();
  72.             /* wait two seconds for him to read message */
  73.             Delay(100L);
  74. #endif
  75.             hold_scroll = 1;
  76.         }
  77.         return;
  78.     }
  79.  
  80.     if (isatty(f))
  81.     {
  82.         /*
  83.          * Not really necessary to call this an error,
  84.          * but if the control terminal (for commands)
  85.          * and the input file (for data) are the same,
  86.          * we get weird results at best.
  87.          */
  88.         error("Can't take input from a terminal");
  89.         if (f > 0)
  90.             close(f);
  91.         return;
  92.     }
  93.  
  94.     /*
  95.      * Close the current input file and set up to use the new one.
  96.      */
  97.     if (file > 0)
  98.         close(file);
  99.     new_file = 1;
  100.     strcpy(current_file, filename);
  101.     ispipe = (f == 0);
  102.     file = f;
  103.     ch_init( (ispipe) ? p_nbufs : f_nbufs );
  104.     init_mark();
  105.     if (every_first_cmd != NULL)
  106.         first_cmd = every_first_cmd;
  107.     if (is_tty)
  108.     {
  109.         any_edited = 1;
  110.         if (hold_scroll)
  111.         {
  112.             /*
  113.              * Before erasing the screen contents,
  114.              * display the file name and ask for a keystroke.
  115.              */
  116.             error(filename);
  117.             hold_scroll = 0;
  118.         }
  119.         if (first_cmd == NULL || *first_cmd == '\0')
  120.         {
  121.             /* 
  122.              * Display the first screen. 
  123.              */
  124.             jump_back(1);
  125.         } else
  126.         {
  127.             /* 
  128.              * The first_cmd will hopefully redisplay the
  129.              * screen, so we need not display anything yet.
  130.              * Indicate there is nothing yet on the screen. 
  131.              */
  132.             pos_clear();
  133.         }
  134.     }
  135. }
  136.  
  137. /*
  138.  * Edit the next file in the command line list.
  139.  */
  140.     public void
  141. next_file(n)
  142.     int n;
  143. {
  144.     if (curr_ac + n >= ac)
  145.     {
  146.         if (quit_at_eof)
  147.             quit();
  148.         error("No (N-th) next file");
  149.     } else
  150.         edit(av[curr_ac += n]);
  151. }
  152.  
  153. /*
  154.  * Edit the previous file in the command line list.
  155.  */
  156.     public void
  157. prev_file(n)
  158.     int n;
  159. {
  160.     if (curr_ac - n < 0)
  161.         error("No (N-th) previous file");
  162.     else
  163.         edit(av[curr_ac -= n]);
  164. }
  165.  
  166. /*
  167.  * Copy a file directly to standard output.
  168.  * Used if standard output is not a tty.
  169.  */
  170.     static void
  171. cat_file()
  172. {
  173.     register int c;
  174.  
  175.     while ((c = ch_forw_get()) != EOF)
  176.         putc(c);
  177.     flush();
  178. }
  179.  
  180. /*
  181.  * Entry point.
  182.  */
  183. main(argc, argv)
  184.     int argc;
  185.     char *argv[];
  186. {
  187.     char *getenv();
  188.  
  189.  
  190.     /*
  191.      * Process command line arguments and LESS environment arguments.
  192.      * Command line arguments override environment arguments.
  193.      */
  194.     init_option();
  195.     scan_option(getenv("LESS"));
  196.     argv++;
  197.     while ( (--argc > 0) && 
  198.       (argv[0][0] == '-' || argv[0][0] == '+') && 
  199.       argv[0][1] != '\0')
  200.         scan_option(*argv++);
  201.  
  202. #if EDITOR
  203.     editor = getenv("EDITOR");
  204.     if (editor == NULL || *editor == '\0')
  205.         editor = EDIT_PGM;
  206. #endif
  207.  
  208.     /*
  209.      * Set up list of files to be examined.
  210.      */
  211.     ac = argc;
  212.     av = argv;
  213.     curr_ac = 0;
  214.  
  215.     /*
  216.      * Set up terminal, etc.
  217.      */
  218.     is_tty = isatty(1);
  219.     if (!is_tty)
  220.     {
  221.         /*
  222.          * Output is not a tty.
  223.          * Just copy the input file(s) to output.
  224.          */
  225.         if (ac < 1)
  226.         {
  227.             edit("-");
  228.             cat_file();
  229.         } else
  230.         {
  231.             do
  232.             {
  233.                 edit((char *)NULL);
  234.                 if (file >= 0)
  235.                     cat_file();
  236.             } while (++curr_ac < ac);
  237.         }
  238.         exit(0);
  239.     }
  240.  
  241.     raw_mode(1);
  242.     get_term();
  243.     open_getc();
  244.     init();
  245.  
  246.     if (back_scroll < 0)
  247.     {
  248.         /* {{ KLUDGE }} */
  249.         back_scroll = sc_height-1;
  250.         if (top_scroll)
  251.             back_scroll--;
  252.     }
  253.  
  254.     if (setjmp(main_loop))
  255.         quit();
  256.     init_signals();
  257.  
  258.     /*
  259.      * Select the first file to examine.
  260.      */
  261.     if (ac < 1)
  262.         edit("-");      /* Standard input */
  263.     else 
  264.     {
  265.         /*
  266.          * Try all the files named as command arguments.
  267.          * We are simply looking for one which can be
  268.          * opened without error.
  269.          */
  270.         do
  271.         {
  272.             edit((char *)NULL);
  273.             if (file >= 0)
  274.                 /* We can open this file. */
  275.                 break;
  276.             putc('\n');  flush();
  277.         } while (++curr_ac < ac);
  278.     }
  279.  
  280.     if (file >= 0)
  281.         commands();
  282.     quit();
  283. }
  284.  
  285. /*
  286.  * Exit the program.
  287.  */
  288.     public void
  289. quit()
  290. {
  291.     /*
  292.      * Put cursor at bottom left corner, clear the line,
  293.      * reset the terminal modes, and exit.
  294.      */
  295.     lower_left();
  296.     clear_eol();
  297.     deinit();
  298.     flush();
  299.     raw_mode(0);
  300. #ifdef amiga
  301.     ttclose();
  302. #endif
  303.     exit(0);
  304. }
  305.