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