home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / AP / JED / JED097-1.TAR / jed / src / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-12  |  5.9 KB  |  319 lines

  1. /*
  2.  *  Copyright (c) 1992, 1994 John E. Davis  (davis@amy.tch.harvard.edu)
  3.  *  All Rights Reserved.
  4.  */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <setjmp.h>
  8. #ifdef unix
  9. # ifdef SYSV
  10. #   include <sys/types.h>
  11. #   include <fcntl.h>
  12. # endif
  13. # include <sys/file.h>
  14. #endif
  15. #ifdef msdos
  16. # include <io.h>
  17. # if !defined(__WATCOMC__)
  18. #   include <dir.h>
  19.     extern unsigned _stklen = 10000U;
  20. # else
  21. #   include <direct.h>
  22. # endif
  23. #endif
  24.  
  25. #ifndef O_RDWR
  26. #ifndef VMS
  27. # include <fcntl.h>
  28. #endif
  29. #endif
  30.  
  31. #include "config.h"
  32. #include "file.h"
  33. #include "buffer.h"
  34. #include "display.h"
  35. #include "sysdep.h"
  36. #include "sig.h"
  37. #include "misc.h"
  38. #include "keymap.h"
  39. #include "screen.h"
  40. #include "ledit.h"
  41. #include "slang.h"
  42. #include "search.h"
  43. #include "text.h"
  44. #include "hooks.h"
  45.  
  46. char Jed_Root_Dir[256];
  47.  
  48. /* a hook to parse some command line args. */
  49. int (*X_Argc_Argv_Hook)(int, char **);
  50.  
  51. typedef struct
  52. {
  53.    jmp_buf b;
  54. } jmp_buf_struct;
  55.  
  56. extern jmp_buf_struct Jump_Buffer, *Jump_Buffer_Ptr;
  57.  
  58. int Batch = 0;
  59.  
  60. char *get_cwd(void)
  61. {
  62.    static char cwd[256];
  63.  
  64. #if defined (sequent)
  65.    if(!getwd(cwd)) return "";
  66. #else 
  67. #if defined (__EMX__)
  68.    _getcwd2(cwd, 254);               /* includes drive specifier */
  69. #else
  70.    getcwd(cwd, 254);               /* djggp includes drive specifier */
  71. #endif
  72. #endif
  73. #ifndef VMS
  74.    slash2slash(cwd);
  75.    fixup_dir(cwd);
  76. #endif
  77.    return(cwd);
  78. }
  79.  
  80. #if 0
  81. void load_init_file(void)
  82. {
  83.    FILE *fp;
  84. #ifdef unix
  85.    char *file = ".jedrc";
  86. #else
  87.    char *file = "jed.rc";
  88. #endif
  89.    char initfile[256], *home;
  90.  
  91.    home = getenv("JED_HOME");
  92. #ifndef VMS
  93.    if ((home != NULL) || (NULL != (home = getenv("HOME"))))
  94.      {
  95.     strcpy(initfile, home);
  96.     fixup_dir(initfile);
  97.      }
  98.    else initfile[0] = 0;
  99. #else
  100.    if (home == NULL) home = "SYS$LOGIN:";
  101.    strcpy(initfile, home);
  102. #endif
  103.  
  104.    strcat(initfile, file);
  105.    if ((fp = fopen(initfile,"r")) == NULL) return;
  106.    fclose(fp);
  107.    SLang_load_file(initfile);
  108. }
  109. #endif 
  110.  
  111. void (*X_Init_Global_Structures_Hook)(void);
  112.  
  113. int Main_Argc;
  114. char **Main_Argv;
  115.  
  116. int main(int argc, char **argv)
  117. {
  118.    char *cmd_hook = "command_line_hook", *jl, *jr;
  119.    int i, fd;
  120.    
  121. #ifdef __EMX__
  122.    _response(&argc, &argv);
  123.    _wildcard(&argc, &argv);
  124. #endif
  125.  
  126. #ifdef msdos
  127.    define_word("0-9a-zA-Z\200-\232\240-\245\341-\353");
  128. #else
  129.    define_word("0-9a-zA-Z\277-\326\330-\336\340-\366\370-\376");
  130. #endif
  131.  
  132.    tt_get_terminfo();
  133.  
  134.    /* If this hook is defined, let it peel off what ever arguments
  135.       it wants.  It should return the number of remaining arguments */
  136.    if (X_Argc_Argv_Hook != NULL) 
  137.      {
  138.     i = (*X_Argc_Argv_Hook)(argc, argv) - 1;
  139.     argv[i] = argv[0];
  140.     argc -= i;
  141.     argv += i;
  142.      }
  143.    
  144.    if (argc > 1)
  145.      {
  146.     if (!strcmp(argv[1], "-batch"))
  147.       {
  148.          Batch = 1;
  149.          SLang_Traceback = 1;
  150.       }
  151.     else if (!strcmp(argv[1], "-script"))
  152.       {
  153.          Batch = 2;
  154.          SLang_Traceback = 1;
  155.       }
  156.      }
  157.    
  158.  
  159. #ifdef unix
  160. #ifdef __GO32__
  161.    fd = 2;
  162. #else
  163.    if (Batch) fd = 2;
  164.    else 
  165.      {
  166.     if ((fd = open("/dev/tty", O_RDWR)) >= 0)
  167.       {
  168.          dup2(fd, 2);  /* tty uses 2 as the descriptor */
  169.       }
  170.     else fd = 2;
  171.       /* exit_error("Unable to open /dev/tty.", 0); */
  172.    
  173.      }
  174. #endif
  175. #else
  176.    fd = 2;                   /* 2 is stderr, assume it is ok */
  177. #endif
  178.  
  179.    
  180.    init_tty();
  181.    
  182.    init_display(1);         /* sets up virtual screen */
  183.  
  184.    Jump_Buffer_Ptr = &Jump_Buffer;
  185.    
  186.    /* incase something goes wrong before we even get started... */
  187.    if (setjmp(Jump_Buffer_Ptr->b) != 0)
  188.      {
  189.     /* hmm.... just exit I guess */
  190.     exit_error("main: Fatal Error", 0);
  191.     return -1;
  192.      }
  193.  
  194.  
  195.    /* This order here is crucial! */
  196.  
  197.    init_keymaps();
  198.    
  199.  
  200.    if (NULL == (CBuf = make_buffer()))
  201.      {
  202.     exit_error("main: Allocation Failure", 0);
  203.      }
  204.    CLine = NULL;
  205.    strcpy(CBuf->name, "*scratch*");
  206.    *CBuf->file = 0;
  207.    
  208.    strcpy(CBuf->dir, get_cwd());
  209.    
  210.    init_minibuffer();
  211.  
  212.    set_file_modes();
  213.    
  214.    /* what if someone pipes something to jed, allow it */
  215.    
  216.    if (!isatty(0))   /* 1 if stdin is a terminal, 0 otherwise */
  217.      {
  218.     set_buffer("*stdin*");
  219.     read_file_pointer(fileno(stdin));
  220.     bob();
  221.     fclose(stdin);
  222.     dup2(fd, 0);    
  223.      }
  224.    
  225.    if (CLine == NULL) make_line(25);
  226.    Point = 0;
  227.    
  228.    window_buffer(CBuf);
  229.    if (!Batch) tt_cls();
  230. #ifndef VMS
  231. #ifndef msdos
  232. #if !defined (__GO32__) && !defined (__os2__)
  233.    init_signals();
  234. #endif
  235. #endif
  236. #endif
  237.  
  238. #ifdef VMS
  239.    jr = "JED_ROOT:";
  240. #else
  241.    jr = (char *) getenv("JED_ROOT");
  242. #endif
  243.    
  244. #ifdef JED_ROOT
  245.    if ((jr == NULL) && (file_status(JED_ROOT) == 2))
  246.      {
  247.     jr = JED_ROOT;
  248.      }
  249. #endif
  250.  
  251.    if (jr != NULL) 
  252.      {
  253.     strcpy(Jed_Root_Dir, jr);
  254.     strcpy(Jed_Library, jr);
  255. #ifndef VMS
  256.        fixup_dir(Jed_Library);
  257.     strcat(Jed_Library, "lib");
  258. #else
  259.     strcat(Jed_Library, "[lib]");
  260. #endif
  261.      }
  262.    
  263.    jl = (char *) getenv("JED_LIBRARY");
  264.     
  265.    if (jl == NULL)
  266.      {
  267.     if (*Jed_Library == 0)
  268.       {
  269.          jl = extract_file(argv[0]);
  270.          i = (int) (jl - argv[0]);
  271.          strncpy(Jed_Library, argv[0], i);
  272.          Jed_Library[i] = 0;
  273.       }
  274.      }
  275.    else strcpy(Jed_Library, jl);
  276.  
  277.    
  278.    SLang_load_file("site");
  279.    
  280.    /* set up command line args */
  281.    Main_Argc = argc;
  282.    Main_Argv = argv;
  283.    
  284.    if (Batch == 2)
  285.      {
  286.     if (argc > 2)
  287.       SLang_load_file(argv[2]);
  288.      }
  289.    else
  290.      {     
  291.     if (SLang_is_defined(cmd_hook))
  292.       {
  293.          SLang_run_hooks(cmd_hook, NULL, NULL);
  294.       }
  295.     else if (!SLang_Error && (argc > 2))
  296.       {
  297.          find_file_in_window(argv[2]);
  298.       }
  299.      }
  300.    
  301.     
  302.  
  303.    /* after we have possible loaded key definitions, we can fix up
  304.     the minibuffer map. This way user definitions are used. */
  305.    
  306.    if (NULL == (Mini_Map = SLang_create_keymap("Mini_Map", Global_Map))) exit_error("main: malloc error", 0);
  307.    SLang_undefine_key("^M", Mini_Map);
  308.    SLang_define_key1("^M", (VOID *) exit_minibuffer, SLKEY_F_INTRINSIC, Mini_Map);
  309.    SLang_define_key1("^I", (VOID *) mini_complete, SLKEY_F_INTRINSIC, Mini_Map);
  310.    SLang_define_key1(" ", (VOID *) mini_complete, SLKEY_F_INTRINSIC, Mini_Map);
  311.    The_MiniBuffer->keymap = Mini_Map;
  312.    
  313.    /* edit_loop */
  314.    if (!Batch) jed();  /* never returns */
  315.    
  316.    exit_error ("Batch End with no exit", 0);
  317.    return(-1);
  318. }
  319.