home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / util / csh-5.39.lha / Csh / src / main.c < prev    next >
C/C++ Source or Header  |  1994-10-01  |  18KB  |  744 lines

  1. /*
  2.  * MAIN.C
  3.  *
  4.  * Matthew Dillon, 24 Feb 1986
  5.  * (c)1986 Matthew Dillon     9 October 1986
  6.  *
  7.  * Version 2.07M by Steve Drew 10-Sep-87
  8.  * Version 4.01A by Carlo Borreo & Cesare Dieni 17-Feb-90
  9.  * Version 5.00L by U. Dominik Mueller 17-Feb-91
  10.  * Version 5.20L by Andreas M. Kirchwitz (Fri, 13 Mar 1992)
  11.  *
  12.  */
  13.  
  14. #include "shell.h"
  15.  
  16.  
  17. #define CSH_VER "5"
  18. #define CSH_REV "39"
  19.  
  20.  
  21. static struct Window *getwindow(void);
  22. static void exectimer(int stop);
  23. static void set_kickversion(void);
  24. char shellcompiled[]="Compiled: "__DATE__" "__TIME__" with "COMPILER"\n";
  25. #if 1
  26. char shellname[]    ="Csh "CSH_VER"."CSH_REV" (public release)";
  27. #else
  28. char shellname[]    ="Csh "CSH_VER"."CSH_REV" (BETA)";
  29. #endif
  30. char shellversion[] =CSH_VER""CSH_REV;
  31. char shellvers[]    =CSH_VER"."CSH_REV;
  32. char shellv   []    ="\0$VER: csh "CSH_VER"."CSH_REV" "__AMIGADATE__"";
  33. char shellctr []    ="CshCounter";
  34. char shellres []    ="CshResident";
  35. char shellthere[]   ="CshLoggedIn";
  36.  
  37. char *oldtitle = NULL;
  38. char trueprompt[100];
  39. char Inline[260];
  40. extern struct ExecBase *SysBase;    /* standard fare....*/
  41. extern struct DosLibrary *DOSBase;    /* more standard fare.... */
  42. struct IntuitionBase *IntuitionBase;
  43. struct GfxBase *GfxBase;
  44. struct Library *GadToolsBase;
  45. struct Library *AslBase;        /* AMK: Asl-FileRequester replaces ARP */
  46. struct Library *BattClockBase;
  47.  
  48. struct Window *old_WindowPtr = NULL;
  49. int    oldtaskpri = -999;
  50. BPTR   OldCin;
  51. void   *PatternBase;
  52. BOOL   nologout = FALSE;
  53. BOOL   nowintitle = FALSE;
  54. struct MsgPort *acs_oldport = NULL;    /* old MsgPort */
  55.  
  56. #ifdef DO_ACS_KLUDGE
  57. /*
  58.  * - New option "-K" (Kludge) for KingCON/ToolManager, sends DOS-Paket
  59.  *    ACTION_CHANGE_SIGNAL.  Don't use this option if you have no problems.
  60.  */
  61. int    acs_kludge = 0;            /* ACTION_CHANGE_SIGNAL kludge */
  62. #endif
  63.  
  64. #ifdef MULTIUSER_SUPPORT
  65. struct muBase  *muBase;     /* LILJA: Added for multiuser-support */
  66. #endif
  67.  
  68. static char *defset[]={
  69.     v_histnum,  "0",
  70.     v_titlebar, shellname,
  71.     v_hist,     "50",
  72.     v_lasterr,  "0",
  73.     v_stat,     "0",
  74.     v_path,     "RAM:,RAM:c,df0:c,df1:c,sys:system,csh:,s:",
  75.     v_rxpath,   "REXX:",
  76.     v_scroll,   "2",
  77.     v_minrows,  "34",
  78.     v_hilite,   "c7",
  79.     v_lcd,      "",
  80.     v_qcd,      "csh:csh-qcd",
  81.     v_insert,   "1",
  82.     v_abbrev,   "5",
  83.     "_terminal","",
  84.     "_man",     "csh:csh.doc",
  85.     "_version", shellversion,
  86. /*    v_nomatch,  "1",*/
  87.     v_prghash,  "csh:csh-prgs",
  88.     NULL,       NULL
  89. };
  90.  
  91. static char *defalias[]={
  92.     "cls",  "echo -n ^l",
  93.     "dswap","cd $_lcd",
  94.     "exit", "endcli;quit",
  95.     "cdir", "%q cd $q; cls; dir",
  96.     "q",    "quit",
  97.     "rx",   "RX",
  98.     "manlist", "search -nl $_man \"    \"",
  99.     NULL,   NULL
  100. };
  101.  
  102. struct MsgPort *Console=(struct MsgPort *)-1;
  103. long ExecTimer, ExecRC;
  104.  
  105. #ifdef __SASC
  106. long __stack = 17500L;
  107. #endif
  108.  
  109. main(int argc, char *argv[])
  110. {
  111.     static ROOT locals;
  112.     BPTR fh;
  113.     int i, login=0;
  114.     char buf[10];
  115.     BOOL nologin=FALSE,nocshrc=FALSE,noclrmenu=FALSE;
  116. #ifdef AZTEC_C
  117.     extern int Enable_Abort;
  118.     Enable_Abort = 0;
  119. #endif
  120.  
  121.     MyMem=salloc(4);
  122.  
  123.     if( argc==0 ) {              /* Prevent starting from workbench */
  124.         Delay(60);
  125.         exit(0);
  126.     }
  127.  
  128.     Myprocess = (struct Process *)FindTask(NULL);
  129.     OldCin    = Myprocess->pr_CIS;
  130.     Mycli     = Cli();
  131. /*    Mycli     = (struct CommandLineInterface *)((long)Myprocess->pr_CLI << 2);*/
  132.  
  133.     init_mbase();
  134.     push_locals( &locals );
  135.     initmap();
  136.  
  137.     if (!SysBase || SysBase->LibNode.lib_Version<37) {
  138.         printf("Sorry, you'll need at least V37 to run CSH.\n");
  139.         exit(20);
  140.     }
  141.  
  142.     if (!DOSBase) {
  143.         printf("No dos library\n");
  144.         exit(20);
  145.     }
  146.  
  147.     /* we do not work without it ... */
  148.     DOSBase->dl_Root->rn_Flags |= RNF_WILDSTAR;
  149.  
  150.     if (!(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",37L))) {
  151.         printf("No graphics library\n");
  152.         exit(20);
  153.     }
  154.  
  155.     if (!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",37L))) {
  156.         printf("No intuition library\n");
  157.         CloseLibrary((struct Library *)GfxBase);
  158.         exit(20);
  159.     }
  160.  
  161.     if (!(AslBase=OpenLibrary("asl.library",37L))) {
  162.         printf("No asl library\n");
  163.         CloseLibrary((struct Library *)IntuitionBase);
  164.         CloseLibrary((struct Library *)GfxBase);
  165.         exit(20);
  166.     }
  167.  
  168.     if (!(GadToolsBase=OpenLibrary("gadtools.library",37L))) {
  169.         printf("No gadtools library\n");
  170.         CloseLibrary((struct Library *)AslBase);
  171.         CloseLibrary((struct Library *)IntuitionBase);
  172.         CloseLibrary((struct Library *)GfxBase);
  173.         exit(20);
  174.     }
  175.  
  176. #ifdef MULTIUSER_SUPPORT
  177.     /* LILJA: Added for multiuser-support */
  178.     muBase = (struct muBase *) OpenLibrary("multiuser.library",39);
  179. #endif
  180.  
  181.     PatternBase = NULL; /* OpenLibrary("pattern.library",5L); */
  182.     BattClockBase = OpenResource(BATTCLOCKNAME);
  183.  
  184.     set_var(LEVEL_SET,"_kick2x",(SysBase->LibNode.lib_Version>=37)?"1":"0");
  185.     set_var(LEVEL_SET,"_kick3x",(SysBase->LibNode.lib_Version>=39)?"1":"0");
  186.  
  187.     set_kickversion();
  188.  
  189.     if( !IsInteractive(Input())) {
  190.         o_bground=1;
  191.         Mycli->cli_Background=DOSTRUE;
  192.     } else
  193.         Mycli->cli_Background=DOSFALSE;
  194.  
  195.     set_var( LEVEL_SET, v_bground, (o_bground) ? "1" : "0" );
  196.  
  197.     /* We don't really need "CONSOLE:", maybe good for stderr ... */
  198.     if( fh=Open( "CONSOLE:" , MODE_NEWFILE) ) {
  199.         Console= ((struct FileHandle *)(4*fh))->fh_Type;
  200.         Close(fh);
  201.     }
  202.  
  203.     Forbid();
  204.     i=0;
  205.     /* AMK: OS20-GetVar replaces ARP-Getenv, SetVar replaces Setenv */
  206.     if ( GetVar(shellres  ,buf,10,GVF_GLOBAL_ONLY|GVF_BINARY_VAR) >= 0L)
  207.         o_resident=1;
  208.     if ( GetVar(shellthere,buf,10,GVF_GLOBAL_ONLY|GVF_BINARY_VAR) <  0L)
  209.         login=TRUE, SetVar(shellthere,"1",-1L,GVF_GLOBAL_ONLY|GVF_BINARY_VAR);
  210.     if ( GetVar(shellctr  ,buf,10,GVF_GLOBAL_ONLY|GVF_BINARY_VAR) >= 0L)
  211.         i=atoi(buf);
  212.     sprintf(buf, "%d", i+1);
  213.     /* AMK: OS20-SetVar replaces ARP-Setenv */
  214.     SetVar(shellctr, buf, -1L, GVF_GLOBAL_ONLY|GVF_BINARY_VAR);
  215.     Permit();
  216.  
  217. #ifdef AZTEC_C
  218.     stdin->_flags    |= _IONBF;    /* make sure we're set as an unbuffered tty */
  219.     stdout->_flags    |= _IONBF;    /* in case of redirection in .login */
  220.     Close( (BPTR)_devtab[2].fd);
  221.     _devtab[2].mode |= O_STDIO;
  222.     _devtab[2].fd = _devtab[1].fd;    /* set stderr to Output() otherwise */
  223.                     /* don't work with aux driver */
  224. #else
  225.     /* if( setvbuf( stdout,NULL,_IOLBF,BUFSIZ )) exit(20); */
  226.     /* setnbf( stdout ); */
  227.     /* Close( _ufbs[2] );*/
  228.     /*_ufbs[2]=_ufbs[1]; */
  229.     /* setnbf( stderr ); */
  230. #endif
  231.  
  232.     sprintf(buf,"%ld",Myprocess->pr_TaskNum);
  233.     set_var(LEVEL_SET, "_clinumber", buf);
  234.  
  235.     seterr(0);
  236.     if (Myprocess->pr_CurrentDir == NULL)
  237.         execute("cd :");
  238.     set_cwd();
  239.  
  240.     o_nowindow= 1;
  241.  
  242.     set_var(LEVEL_SET,v_prompt, (IsInteractive(Input())) ? "%c%p> ":"");
  243.  
  244.     for( i=0; defset[i]; i+=2 )
  245.         set_var( LEVEL_SET, defset[i], defset[i+1] );
  246.     for( i=0; defalias[i]; i+=2 )
  247.         set_var( LEVEL_ALIAS, defalias[i], defalias[i+1] );
  248.  
  249.     o_nowindow= 0;
  250.  
  251.     for (i = 1; i < argc; ++i) {
  252.         if (*argv[i]=='-' && (index(argv[i],'a') || index(argv[i],'t')))
  253.             o_nowindow=1;
  254.         if (*argv[i]=='-' && index(argv[i],'n'))
  255.             nologin = TRUE;
  256.         if (*argv[i]=='-' && index(argv[i],'N'))
  257.             nocshrc = TRUE;
  258.         if (*argv[i]=='-' && index(argv[i],'L'))
  259.             nologout = TRUE;
  260.         if (*argv[i]=='-' && index(argv[i],'M'))
  261.             noclrmenu = TRUE;
  262.         if (*argv[i]=='-' && index(argv[i],'W'))
  263.             nowintitle = TRUE;
  264.         if (*argv[i]=='-' && index(argv[i],'R'))
  265.             o_noraw = 1;
  266.         if (*argv[i]=='-' && index(argv[i],'w'))
  267.             o_nowindow = 1;
  268.         if (*argv[i]=='-' && (index(argv[i],'C') || index(argv[i],'c'))) {
  269.             noclrmenu  = TRUE;
  270.             nowintitle = TRUE;
  271.         }
  272.     }
  273.  
  274.     if (Mycli->cli_Background) {
  275.         o_noraw    = 1;
  276.         o_nowindow = 1;
  277.         o_vt100    = 1;
  278.     }
  279.  
  280.     if( !o_nowindow && (Mywindow=getwindow()) && IsInteractive(Input())) {
  281.         old_WindowPtr = Myprocess->pr_WindowPtr;
  282.         Myprocess->pr_WindowPtr = 0L/*Mywindow*/;
  283.         newwidth();
  284.         if (!nowintitle)
  285.             oldtitle=(char *)(Mywindow->Title);
  286.         if (!noclrmenu) {
  287.             /* clear menus, even if we are not the owner */
  288.             set_menu();
  289.         }
  290.     }
  291.  
  292.     if (login && !nologin) {
  293.         /*printf("we're the first csh today, mon ami!\n");*/
  294.         if( exists("S:.login"))
  295.             execute("source S:.login");
  296.     }
  297.  
  298.     if (!nocshrc) {
  299.         if( exists("S:.cshrc"))
  300.             execute("source S:.cshrc");
  301.     }
  302.  
  303.     {
  304.     char nam1[40],nam2[40],nam3[40];
  305.     BOOL e1,e2,e3;
  306.     strcpy(nam1,"ENVARC:"); strcat(nam1,shellctr);   e1=exists(nam1);
  307.     strcpy(nam2,"ENVARC:"); strcat(nam2,shellres);   e2=exists(nam2);
  308.     strcpy(nam3,"ENVARC:"); strcat(nam3,shellthere); e3=exists(nam3);
  309.  
  310.     if (e1 || e2 || e3) {
  311.         printf("\nWARNING: please remove the following files from ENVARC:\n\n");
  312.         if (e1)
  313.         pr