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