home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d092 / less.lha / Less / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-08-22  |  6.9 KB  |  401 lines

  1. /* Entry point, initialization, micellaneous routines.
  2.  */
  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.  
  18. #ifdef amiga
  19. /* max items and chars that *.c can expand to */
  20. #define MAXTEMPLATES 100
  21. #define MAXARGVCHARS 1000
  22. public char    local_buffer[MAXARGVCHARS];
  23. public char    *local_av[MAXTEMPLATES];
  24. #endif
  25. public char **av;
  26. public int curr_ac;
  27. #if EDITOR
  28. public char *   editor;
  29. #endif
  30.  
  31. extern int file;
  32. extern int nbufs;
  33. extern int sigs;
  34. extern int quit_at_eof;
  35. extern int p_nbufs, f_nbufs;
  36. extern int back_scroll;
  37. extern int top_scroll;
  38. extern int sc_height;
  39.  
  40. /*
  41.  * Edit a new file.
  42.  * Filename "-" means standard input.
  43.  * No filename means the "current" file, from the command line.
  44.  */
  45.     public void
  46. edit(filename)
  47.     char *filename;
  48. {
  49.     register int f;
  50.     char message[100];
  51.     static int any_edited = 0;
  52.     static int hold_scroll = 0;
  53.  
  54.     if (filename == NULL || *filename == '\0')
  55.     {
  56.         if (curr_ac >= ac)
  57.         {
  58.             error("No current file");
  59.             return;
  60.         }
  61.         filename = av[curr_ac];
  62.     }
  63.  
  64.  
  65.     if (strcmp(filename, "-") == 0)
  66.         f = 0;  /* Standard input */
  67.     else if ((f = open(filename, 0)) < 0)
  68.     {
  69. #ifdef amiga
  70.         sprintf(message, "Cannot open %s", filename);
  71. #else
  72.         sprintf(message, "Cannot open %.*s", error_width()-13, filename);
  73. #endif
  74.         if (any_edited)
  75.             error(message);
  76.         else
  77.         {
  78.             puts(message);
  79. #ifdef amiga
  80.             flush();
  81.             /* wait two seconds for him to read message */
  82.             Delay(100L);
  83. #endif
  84.             hold_scroll = 1;
  85.         }
  86.         return;
  87.     }
  88.  
  89.     if (isatty(f))
  90.     {
  91.         /*
  92.          * Not really necessary to call this an error,
  93.          * but if the control terminal (for commands)
  94.          * and the input file (for data) are the same,
  95.          * we get weird results at best.
  96.          */
  97.         error("Can't take input from a terminal");
  98.         if (f > 0)
  99.             close(f);
  100.         return;
  101.     }
  102.  
  103.     /*
  104.      * Close the current input file and set up to use the new one.
  105.      */
  106.     if (file > 0)
  107.         close(file);
  108.     new_file = 1;
  109.     strcpy(current_file, filename);
  110. #ifndef amiga
  111.     ispipe = (f == 0);
  112. #endif
  113.     file = f;
  114.     ch_init( (ispipe) ? p_nbufs : f_nbufs );
  115.     init_mark();
  116.     if (every_first_cmd != NULL)
  117.         first_cmd = every_first_cmd;
  118.     if (is_tty)
  119.     {
  120.         any_edited = 1;
  121.         if (hold_scroll)
  122.         {
  123.             /*
  124.              * Before erasing the screen contents,
  125.              * display the file name and ask for a keystroke.
  126.              */
  127.             error(filename);
  128.             hold_scroll = 0;
  129.         }
  130.         if (first_cmd == NULL || *first_cmd == '\0')
  131.         {
  132.             /* 
  133.              * Display the first screen. 
  134.              */
  135.             jump_back(1);
  136.         } else
  137.         {
  138.             /* 
  139.              * The first_cmd will hopefully redisplay the
  140.              * screen, so we need not display anything yet.
  141.              * Indicate there is nothing yet on the screen. 
  142.              */
  143.             pos_clear();
  144.         }
  145.     }
  146. }
  147.  
  148. /*
  149.  * Edit the next file in the command line list.
  150.  */
  151.     public void
  152. next_file(n)
  153.     int n;
  154. {
  155.     if (curr_ac + n >= ac)
  156.     {
  157.         if (quit_at_eof)
  158.             quit();
  159.         error("No (N-th) next file");
  160.     } else
  161.         edit(av[curr_ac += n]);
  162. }
  163.  
  164. /*
  165.  * Edit the previous file in the command line list.
  166.  */
  167.     public void
  168. prev_file(n)
  169.     int n;
  170. {
  171.     if (curr_ac - n < 0)
  172.         error("No (N-th) previous file");
  173.     else
  174.         edit(av[curr_ac -= n]);
  175. }
  176.  
  177. /*
  178.  * Copy a file directly to standard output.
  179.  * Used if standard output is not a tty.
  180.  */
  181.     static void
  182. cat_file()
  183. {
  184.     register int c;
  185.  
  186.     while ((c = ch_forw_get()) != EOF)
  187.         putc(c);
  188.     flush();
  189. }
  190.  
  191. #ifdef amiga
  192.  
  193. /* Bob Leivian  4/28/87 fudge up things so it will work
  194.    when called from Work Bench */
  195. char argvbuf[80];
  196. int called_from_WB = 0;
  197. long old_dir;
  198.  
  199. #include "workbench/startup.h"
  200.  
  201. /* ignore AZTECs wb stuff */
  202. _wb_parse(ignore, ignore2)
  203. char *ignore;
  204. char *ignore2;
  205. {
  206.     return;
  207.  
  208. /* this requires the workbench disk --
  209.   ignore all environment variables for now */
  210. char * getenv(ignore)
  211. {
  212.     return NULL;
  213. }
  214. #endif
  215.  
  216.  
  217. /*
  218.  * Entry point.
  219.  */
  220. main(argc, argv)
  221.     int argc;
  222.     char *argv[];
  223. {
  224.     char *getenv();
  225.     int i,j;
  226.  
  227. #ifdef amiga
  228. /* if we were called from the workbench we will have no args
  229.    but a pointer to the WB struct, get the filename from this structure */
  230. if(argc == 0) {
  231.    struct WBStartup *WBmsg;
  232.    struct WBArg *p;
  233.  
  234.    /* the argv is really the work bench structure */
  235.    WBmsg = (struct WBStartup *) argv;
  236.    p = WBmsg->sm_ArgList;
  237.  
  238.    p++;  /* ignore first parm */
  239.    strcpy(argvbuf, p->wa_Name);
  240.  
  241.    argc = 2;
  242.    argv[0] = "less";
  243.    argv[1] = argvbuf;        /* fake up the args now */
  244.  
  245.    /* we have to set up this icons current directory  (and release it later) */
  246.    called_from_WB++;
  247.    old_dir = CurrentDir(p->wa_Lock);
  248.  
  249. }
  250. #endif
  251.  
  252.     /*
  253.      * Process command line arguments and LESS environment arguments.
  254.      * Command line arguments override environment arguments.
  255.      */
  256.     init_option();
  257.     
  258.     scan_option(getenv("LESS"));
  259.  
  260.     argv++;
  261.     while ( (--argc > 0) && 
  262.       (argv[0][0] == '-' || argv[0][0] == '+') && 
  263.       argv[0][1] != '\0')
  264.         scan_option(*argv++);
  265.  
  266. #if EDITOR
  267.     editor = getenv("EDITOR");
  268.     if (editor == NULL || *editor == '\0')
  269.         editor = EDIT_PGM;
  270. #endif
  271.  
  272.     /*
  273.      * Set up list of files to be examined.
  274.      */
  275.     ac = argc;
  276.     av = argv;
  277. #ifdef amiga
  278.     /* CLI doesn't expand templates link U*ix so we need to do it
  279.            'by hand' */
  280.  
  281.     for (i=0, j=0; i<ac; i++) {
  282.        int temp = 0;
  283.        char *p;
  284.        char *scdir();
  285.  
  286.        if(index(av[i], '*') || index(av[i], '?')) {
  287.  
  288.           /* this is a template it needs to be expanded */
  289.               while( (p = scdir(av[i])) && (j < MAXTEMPLATES)) {
  290.          strcpy(&local_buffer[temp], p);
  291.          local_av[j++] = &local_buffer[temp];
  292.          temp += (strlen(&local_buffer[temp]) +1);
  293.          if(temp > MAXARGVCHARS) break;
  294.           }
  295.        } else
  296.           local_av[j++] = av[i];
  297.     }
  298.     av = local_av;
  299.     ac = j;
  300. #endif
  301.     curr_ac = 0;
  302.  
  303.     /*
  304.      * Set up terminal, etc.
  305.      */
  306.     is_tty = isatty(1);
  307. #ifdef amiga
  308.     if (!is_tty && !called_from_WB)
  309. #else
  310.     if (!is_tty)
  311. #endif
  312.     {
  313.         /*
  314.          * Output is not a tty.
  315.          * Just copy the input file(s) to output.
  316.          */
  317.         if (ac < 1)
  318.         {
  319.             edit("-");
  320.             cat_file();
  321.         } else
  322.         {
  323.             do
  324.             {
  325.                 edit((char *)NULL);
  326.                 if (file >= 0)
  327.                     cat_file();
  328.             } while (++curr_ac < ac);
  329.         }
  330.         exit(0);
  331.     }
  332.  
  333.     raw_mode(1);
  334.     get_term();
  335.     open_getc();
  336.     init();
  337.  
  338.     if (back_scroll < 0)
  339.     {
  340.         /* {{ KLUDGE }} */
  341.         back_scroll = sc_height-1;
  342.         if (top_scroll)
  343.             back_scroll--;
  344.     }
  345.  
  346.     if (setjmp(main_loop))
  347.         quit();
  348.     init_signals();
  349.  
  350.     /*
  351.      * Select the first file to examine.
  352.      */
  353.     if (ac < 1)
  354.         edit("-");      /* Standard input */
  355.     else 
  356.     {
  357.         /*
  358.          * Try all the files named as command arguments.
  359.          * We are simply looking for one which can be
  360.          * opened without error.
  361.          */
  362.         do
  363.         {
  364.             edit((char *)NULL);
  365.             if (file >= 0)
  366.                 /* We can open this file. */
  367.                 break;
  368.             putc('\n');  flush();
  369.         } while (++curr_ac < ac);
  370.     }
  371.  
  372.     if (file >= 0)
  373.         commands();
  374.     quit();
  375. }
  376.  
  377. /*
  378.  * Exit the program.
  379.  */
  380.     public void
  381. quit()
  382. {
  383.     /*
  384.      * Put cursor at bottom left corner, clear the line,
  385.      * reset the terminal modes, and exit.
  386.      */
  387.     lower_left();
  388.     clear_eol();
  389.     deinit();
  390.     flush();
  391.     raw_mode(0);
  392. #ifdef amiga
  393.     ttclose();
  394.  
  395.     if(called_from_WB)
  396.         CurrentDir(old_dir);
  397. #endif
  398.     exit(0);
  399. }
  400.