home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / shell / csh531s.lha / main.c < prev    next >
C/C++ Source or Header  |  1993-06-26  |  12KB  |  504 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 "31"
  19.  
  20.  
  21. static struct Window *getwindow(void);
  22. static void exectimer(int stop);
  23. char shellcompiled[]="Compiled: "__DATE__" "__TIME__" with "COMPILER"\n";
  24. char shellname[]    ="Csh "CSH_VER"."CSH_REV" (public release)";
  25. char shellversion[] =CSH_VER""CSH_REV;
  26. char shellvers[]    =CSH_VER"."CSH_REV;
  27. char shellv   []    ="\0$VER: csh "CSH_VER"."CSH_REV" ("__DATE__" "__TIME__")";
  28. char shellctr []    ="CshCounter";
  29. char shellres []    ="CshResident";
  30. char shellthere[]   ="CshLoggedIn";
  31.  
  32. char *oldtitle;
  33. char trueprompt[100];
  34. char Inline[260];
  35. extern struct ExecBase *SysBase;    /* standard fare....*/
  36. struct DosLibrary *DOSBase;        /* more standard fare.... */
  37. struct IntuitionBase *IntuitionBase;
  38. struct GfxBase *GfxBase;
  39. struct Library *GadToolsBase;
  40. struct Library *AslBase;        /* AMK: Asl-FileRequester replaces ARP */
  41. struct Window *Win;
  42. int    oldtaskpri=-999;
  43. BPTR   OldCin;
  44. void   *PatternBase;
  45.  
  46. static char *defset[]={
  47.     v_histnum,  "0",
  48.     v_titlebar, shellname,
  49.     v_hist,     "50",
  50.     v_lasterr,  "0",
  51.     v_stat,     "0",
  52.     v_path,     "RAM:,RAM:c,df0:c,df1:c,sys:system,csh:,s:",
  53.     v_rxpath,   "REXX:",
  54.     v_scroll,   "2",
  55.     v_minrows,  "34",
  56.     v_hilite,   "c7",
  57.     v_lcd,      "",
  58.     v_qcd,      "csh:csh-qcd",
  59.     v_insert,   "1",
  60.     "_terminal","",
  61.     "_man",     "csh:csh.doc",
  62.     "_version", shellversion,
  63.     NULL,       NULL
  64. };
  65.  
  66. static char *defalias[]={
  67.     "cls",  "echo -n ^l",
  68.     "dswap","cd $_lcd",
  69.     "exit", "endcli;quit",
  70.     "cdir", "%q cd $q; cls; dir",
  71.     "q",    "quit",
  72.     "rx",   "RX",
  73.     "manlist", "search -nl $_man \"    \"",
  74.     NULL,   NULL
  75. };
  76.  
  77. struct MsgPort *Console=(struct MsgPort *)-1;
  78. long ExecTimer, ExecRC;
  79.  
  80. #ifdef __SASC
  81. long __stack = 15000L;
  82. #endif
  83.  
  84. main(int argc, char *argv[])
  85. {
  86.     static ROOT locals;
  87.     BPTR fh;
  88.     int i, login=0;
  89.     char buf[10];
  90.     BOOL nologin=FALSE,nocshrc=FALSE;
  91. #ifdef AZTEC_C
  92.     extern int Enable_Abort;
  93.     Enable_Abort = 0;
  94. #endif
  95.  
  96.     MyMem=salloc(4);
  97.  
  98.     if( argc==0 ) {              /* Prevent starting from workbench */
  99.         Delay(60);
  100.         exit(0);
  101.     }
  102.  
  103.     Myprocess = (struct Process *)FindTask(NULL);
  104.     OldCin    = Myprocess->pr_CIS;
  105.     Mycli     = Cli();
  106. /*    Mycli     = (struct CommandLineInterface *)((long)Myprocess->pr_CLI << 2);*/
  107.  
  108.     init_mbase();
  109.     push_locals( &locals );
  110.     initmap();
  111.  
  112.     if (!SysBase || SysBase->LibNode.lib_Version<37) {
  113.         printf("Sorry, you'll need at least V37 to run CSH.\n");
  114.         exit(20);
  115.     }
  116.  
  117.     if (!DOSBase) {
  118.         printf("No dos library\n");
  119.         exit(20);
  120.     }
  121.  
  122.     /* we do not work without it ... */
  123.     DOSBase->dl_Root->rn_Flags |= RNF_WILDSTAR;
  124.  
  125.     if (!(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",37L))) {
  126.         printf("No graphics library\n");
  127.         exit(20);
  128.     }
  129.  
  130.     if (!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",37L))) {
  131.         printf("No intuition library\n");
  132.         CloseLibrary((struct Library *)GfxBase);
  133.         exit(20);
  134.     }
  135.  
  136.     if (!(AslBase=OpenLibrary("asl.library",37L))) {
  137.         printf("No asl library\n");
  138.         CloseLibrary((struct Library *)IntuitionBase);
  139.         CloseLibrary((struct Library *)GfxBase);
  140.         exit(20);
  141.     }
  142.  
  143.     if (!(GadToolsBase=OpenLibrary("gadtools.library",37L))) {
  144.         printf("No gadtools library\n");
  145.         CloseLibrary((struct Library *)AslBase);
  146.         CloseLibrary((struct Library *)IntuitionBase);
  147.         CloseLibrary((struct Library *)GfxBase);
  148.         exit(20);
  149.     }
  150.  
  151.     PatternBase = OpenLibrary("pattern.library",5L);
  152.  
  153.     set_var(LEVEL_SET,"_kick2x",(SysBase->LibNode.lib_Version>=37)?"1":"0");
  154.     set_var(LEVEL_SET,"_kick3x",(SysBase->LibNode.lib_Version>=39)?"1":"0");
  155.  
  156.     if( !IsInteractive(Input())) {
  157.         o_bground=1;
  158.         Mycli->cli_Background=DOSTRUE;
  159.     } else
  160.         Mycli->cli_Background=DOSFALSE;
  161.  
  162.     set_var( LEVEL_SET, v_bground, (o_bground) ? "1" : "0" );
  163.  
  164.     if( fh=Open( "CONSOLE:" , MODE_NEWFILE) ) {
  165.         Console= ((struct FileHandle *)(4*fh))->fh_Type;
  166.         Close(fh);
  167.     }
  168.  
  169.     Forbid();
  170.     i=0;
  171.     /* AMK: OS20-GetVar replaces ARP-Getenv, SetVar replaces Setenv */
  172.     if ( GetVar(shellres  ,buf,10,GVF_GLOBAL_ONLY|GVF_BINARY_VAR) >= 0L)
  173.         o_resident=1;
  174.     if ( GetVar(shellthere,buf,10,GVF_GLOBAL_ONLY|GVF_BINARY_VAR) <  0L)
  175.         login=TRUE, SetVar(shellthere,"1",-1L,GVF_GLOBAL_ONLY|GVF_BINARY_VAR);
  176.     if ( GetVar(shellctr  ,buf,10,GVF_GLOBAL_ONLY|GVF_BINARY_VAR) >= 0L)
  177.         i=atoi(buf);
  178.     sprintf(buf, "%d", i+1);
  179.     /* AMK: OS20-SetVar replaces ARP-Setenv */
  180.     SetVar(shellctr, buf, -1L, GVF_GLOBAL_ONLY|GVF_BINARY_VAR);
  181.     Permit();
  182.  
  183. #ifdef AZTEC_C
  184.     stdin->_flags    |= _IONBF;    /* make sure we're set as an unbuffered tty */
  185.     stdout->_flags    |= _IONBF;    /* in case of redirection in .login */
  186.     Close( (BPTR)_devtab[2].fd);
  187.     _devtab[2].mode |= O_STDIO;
  188.     _devtab[2].fd = _devtab[1].fd;    /* set stderr to Output() otherwise */
  189.                     /* don't work with aux driver */
  190. #else
  191.     /* if( setvbuf( stdout,NULL,_IOLBF,BUFSIZ )) exit(20); */
  192.     /* setnbf( stdout ); */
  193.     /* Close( _ufbs[2] );*/
  194.     /*_ufbs[2]=_ufbs[1]; */
  195.     /* setnbf( stderr ); */
  196. #endif
  197.  
  198.     sprintf(buf,"%ld",Myprocess->pr_TaskNum);
  199.     set_var(LEVEL_SET, "_clinumber", buf);
  200.  
  201.     seterr(0);
  202.     if (Myprocess->pr_CurrentDir == NULL)
  203.         execute("cd :");
  204.     set_cwd();
  205.  
  206.     o_nowindow= 1;
  207.  
  208.     set_var(LEVEL_SET,v_prompt, (isconsole(Input())) ? "%c%p> ":"");
  209.     set_var(LEVEL_SET,v_nomatch,"1");
  210.  
  211.     for( i=0; defset[i]; i+=2 )
  212.         set_var( LEVEL_SET, defset[i], defset[i+1] );
  213.     for( i=0; defalias[i]; i+=2 )
  214.         set_var( LEVEL_ALIAS, defalias[i], defalias[i+1] );
  215.  
  216.     o_nowindow= 0;
  217.  
  218.     for (i = 1; i < argc; ++i) {
  219.         if (*argv[i]=='-' && (index(argv[i],'a') || index(argv[i],'t')))
  220.             o_nowindow=1;
  221.         if (*argv[i]=='-' && index(argv[i],'n'))
  222.             nologin=1;
  223.         if (*argv[i]=='-' && index(argv[i],'N'))
  224.             nocshrc=1;
  225.     }
  226.  
  227.     if( !o_nowindow && (Win=getwindow()) && isconsole(Input())) {
  228.         oldtitle=(char *)(Win->Title);
  229.         set_menu();
  230.     }
  231.  
  232.     if (login && !nologin) {
  233.         /*printf("we're the first csh today, mon ami!\n");*/
  234.         if( exists("S:.login"))
  235.             execute("source S:.login");
  236.     }
  237.  
  238.     if (!nocshrc) {
  239.         if( exists("S:.cshrc"))
  240.             execute("source S:.cshrc");
  241.     }
  242.  
  243.     {
  244.     char nam1[40],nam2[40],nam3[40];
  245.     BOOL e1,e2,e3;
  246.     strcpy(nam1,"ENVARC:"); strcat(nam1,shellctr);   e1=exists(nam1);
  247.     strcpy(nam2,"ENVARC:"); strcat(nam2,shellres);   e2=exists(nam2);
  248.     strcpy(nam3,"ENVARC:"); strcat(nam3,shellthere); e3=exists(nam3);
  249.  
  250.     if (e1 || e2 || e3) {
  251.         printf("\nWARNING: please remove the following files from ENVARC:\n\n");
  252.         if (e1)
  253.         printf("          - %s\n",shellctr);
  254.         if (e2)
  255.         printf("          - %s\n",shellres);
  256.         if (e3)
  257.         printf("          - %s\n",shellthere);
  258.         printf("\n         and reboot your system after removing the files.\n");
  259.         printf("         Never copy ENV: to ENVARC: !!\n\n");
  260.         Delay(150L);
  261.     }
  262.     }
  263.  
  264.     for (i = 1; i < argc; ++i) {
  265.         if (*argv[i]=='-') {
  266.             char *str=argv[i]+1;
  267.             for( ; *str; str++ ) {
  268.                 switch( *str) {
  269.                 case 'c':
  270.                 case 'C': execute( compile_av( argv, i+1, argc, ' ',*str=='C'));
  271.                           main_exit(Lastresult); break;
  272.                 case 'k': set_var(LEVEL_SET,v_nobreak,"1"); break;
  273.                 case 'v': Verbose=1; set_var(LEVEL_SET,v_verbose,"hs"); break;
  274.                 case 'b': oldtaskpri=Myprocess->pr_Task.tc_Node.ln_Pri;
  275.                           SetTaskPri( &Myprocess->pr_Task, -1 ); break;
  276.                 case 'f': oldtaskpri=Myprocess->pr_Task.tc_Node.ln_Pri;
  277.                           SetTaskPri( &Myprocess->pr_Task,  1 ); break;
  278.                 case 'a': o_nowindow= o_noraw= 1;
  279.                           set_var( LEVEL_SET, v_hilite, "" ); break;
  280.                 case 't': o_nowindow= o_vt100= o_nofastscr= 1;
  281.                           set_var( LEVEL_SET, v_hilite, "r" );
  282.                           set_var( LEVEL_SET, v_noreq, "1" );
  283.                           set_var( LEVEL_SET, "_terminal", "1" );
  284.                           set_var( LEVEL_ALIAS, "cls", "e -n ^[[0\\;0H^[[J" );
  285.                           break;
  286.                 case 's': DOSBase->dl_Root->rn_Flags |= RNF_WILDSTAR;
  287.                           break;
  288.                 }
  289.             }
  290.         } else {
  291.             sprintf (Inline, "source %s",argv[i]);
  292.             execute (Inline);
  293.         }
  294.     }
  295.  
  296.     if( !isconsole(Input()))
  297.         main_exit( Lastresult );
  298.  
  299.     for (;;) {
  300.         if (breakcheck())
  301.             while (WaitForChar(Input(), 100L) || CHARSWAIT( stdin ))
  302.                 gets(Inline);
  303.         clearerr(stdin);  /* prevent acidental quit */
  304.         exec_every();
  305.         { /* AMK TEST BEGIN */
  306.             char *old;
  307.             char pwd[256];
  308.             NameFromLock(Myprocess->pr_CurrentDir, pwd, 255L);
  309.             if( !(old=get_var(LEVEL_SET, v_cwd)) )
  310.                 old="";
  311.             if (strcmp(pwd,old)) {
  312.                 set_var(LEVEL_SET, v_lcd, old);
  313.                 set_cwd();
  314.             }
  315.             /*update_sys_var(v_cwd);*/
  316.         } /* AMK TEST BEGIN */
  317.         update_sys_var(v_titlebar);
  318.         update_sys_var(v_prompt);
  319.         breakreset();
  320. #if RAW_CONSOLE
  321.         if (Quit || !rawgets(Inline, disable ? "_ " : trueprompt)) main_exit(0);
  322. #else
  323.         printf("%s", disable ? "_ " : trueprompt);
  324.         fflush(stdout);
  325.         if (Quit || !gets(Inline)) main_exit(0);
  326. #endif
  327.         breakreset();
  328.         exectimer(0);
  329.         if (*Inline) ExecRC=exec_command(Inline);
  330.         exectimer(1);
  331.     }
  332. }
  333.  
  334.  
  335. void
  336. main_exit(n)
  337. {
  338.     int i;
  339.     char buf[10];
  340.  
  341.     /* AMK: OS20-GetVar replaces ARP-Getenv */
  342.     GetVar(shellctr,buf,10L,GVF_GLOBAL_ONLY|GVF_BINARY_VAR);
  343.     i=atoi(buf);
  344.     sprintf(buf,"%d",i-1);
  345.     /* AMK: OS20-SetVar replaces ARP-Setenv */
  346.     SetVar(shellctr, buf, -1L, GVF_GLOBAL_ONLY|GVF_BINARY_VAR);
  347.  
  348.     if( oldtitle )
  349.         SetWindowTitles(Win,oldtitle,(char *)-1);
  350.     if( oldtaskpri != -999 )
  351.         SetTaskPri(&Myprocess->pr_Task,oldtaskpri);
  352.     for (i=1; i<MAXMYFILES; i++) myclose(i);
  353.     remove_menu();
  354.     if( PatternBase ) CloseLibrary( PatternBase );
  355.     CloseLibrary((struct Library *)GadToolsBase);
  356.     CloseLibrary((struct Library *)AslBase);
  357.     CloseLibrary((struct Library *)IntuitionBase);
  358.     CloseLibrary((struct Library *)GfxBase);
  359.     exit(n);
  360. }
  361.  
  362.  
  363. static void
  364. exectimer(int stop)
  365. {
  366.     struct DateStamp dss;
  367.     static long lasttime;
  368.     long time;
  369.  
  370.     DateStamp(&dss);
  371.     time=dss.ds_Minute*6000+2*dss.ds_Tick;   /* 2 = 100/TickPerSec   */
  372.     if( stop )
  373.         ExecTimer=time-lasttime;
  374.     else
  375.         lasttime=time;
  376. }
  377.  
  378.  
  379. /* print break message only once */
  380. static BOOL dobreak_output = TRUE;
  381.  
  382. int
  383. breakcheck()
  384. {
  385.     return !o_nobreak && SetSignal(0L,0L) & SIGBREAKF_CTRL_C;
  386. }
  387.  
  388. void
  389. breakreset()
  390. {
  391.     SetSignal(0L, SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D);
  392.     dobreak_output = TRUE;
  393. }
  394.  
  395. dobreak()
  396. {
  397.     if (breakcheck()) {
  398.         if (dobreak_output) {
  399.             printf("^C\n");
  400.             dobreak_output = FALSE;
  401.         }
  402.         return(1);
  403.     }
  404.     return(0);
  405. }
  406.  
  407. /* this routine causes manx to use this Chk_Abort() rather than it's own */
  408. /* otherwise it resets our ^C when doing any I/O (even when Enable_Abort */
  409. /* is zero).  Since we want to check for our own ^C's             */
  410.  
  411. long
  412. Chk_Abort()
  413. {
  414.     return(0);
  415. }
  416.  
  417. void
  418. _wb_parse()
  419. {
  420. }
  421.  
  422. do_howmany()
  423. {
  424.     char buf[10];
  425.  
  426.     /* AMK: OS20-GetVar replaces ARP-Getenv */
  427.     GetVar(shellctr, buf, 10L, GVF_GLOBAL_ONLY|GVF_BINARY_VAR);
  428.     printf("Shell(s) running: %s\n",buf);
  429.     return 0;
  430. }
  431.  
  432. static struct Window *
  433. getwindow(void)
  434. {
  435.     struct Window *win = NULL;
  436.  
  437.     if( o_nowindow )
  438.         return NULL;
  439.     if( isconsole(Output()))
  440.         Write(Output(),"",1);     /*    make window appear */
  441.     if( Myprocess->pr_ConsoleTask ) {
  442.         struct InfoData *infodata;
  443.         infodata=(void *)SAllocMem((long)sizeof(struct InfoData),MEMF_CLEAR|MEMF_PUBLIC);
  444.         if (DoPkt((void *)Myprocess->pr_ConsoleTask,ACTION_DISK_INFO,((LONG)infodata)>>2,NULL,NULL,NULL,NULL)) {
  445.             win=(struct Window *)infodata->id_VolumeNode;
  446.         }
  447.         FreeMem(infodata,sizeof(struct InfoData));
  448.         if( win==NULL )
  449.             o_nowindow=1;
  450.     }
  451.     newwidth();
  452.     return win;
  453. }
  454.  
  455.  
  456. #ifdef LATTICE
  457.  
  458. int
  459. setenv( char *var, char *val )
  460. {
  461.     char *buf=salloc(strlen(var)+strlen(val)+10);
  462.     sprintf(buf, "%s=%s", var, val );
  463.     Free(buf);
  464.     return putenv(buf);
  465. }
  466.  
  467. __regargs int __chkabort(void) { return(0); }
  468.  
  469. #endif
  470.  
  471. #ifdef _DCC
  472. void
  473. swapmem(void *_s1, void *_s2, size_t _n)
  474. {
  475.     char t, *s1=_s1, *s2=_s2;
  476.     int  i;
  477.  
  478.     for( i=0; i<_n; i++ )
  479.         t=*s1; *s1++=*s2; *s2++=t;
  480. }
  481.  
  482. int
  483. setenv( char *var, char *val )
  484. {
  485.     BPTR fh;
  486.     char buf[40];
  487.     sprintf(buf,"ENV:%s",var );
  488.  
  489.     if( fh=Open(buf,MODE_NEWFILE) ) {
  490.         Write(fh,val,strlen(val));
  491.         Close(fh);
  492.     }
  493. }
  494.  
  495. int
  496. rand()
  497. {
  498.     return 0;
  499. }
  500.  
  501. _waitwbmsg() {return 0;};
  502.  
  503. #endif
  504.