home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / programs / system / csh / src / main.c < prev    next >
C/C++ Source or Header  |  1995-02-27  |  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.         printf("          - %s\n",shellctr);
  314.         if (e2)
  315.         printf("          - %s\n",shellres);
  316.         if (e3)
  317.         printf("          - %s\n",shellthere);
  318.         printf("\n         and reboot your system after removing the files.\n");
  319.         printf("         Never copy ENV: to ENVARC: !!\n\n");
  320.         Delay(150L);
  321.     }
  322.     }
  323.  
  324.     /* would be a nice idea to have a flag that tells if this is
  325.        a full shell or just executing some commands and the leaving */
  326.  
  327.     for (i = 1; i < argc; ++i) {
  328.         if (*argv[i]=='-') {
  329.             char *str=argv[i]+1;
  330.             for( ; *str; str++ ) {
  331.                 switch( *str) {
  332.                 case 'c':
  333.                 case 'C': execute( compile_av( argv, i+1, argc, ' ',*str=='C'));
  334.                           main_exit(Lastresult); break;
  335.                 case 'k': set_var(LEVEL_SET,v_nobreak,"1"); break;
  336. #ifdef DO_ACS_KLUDGE
  337.                 case 'K': acs_kludge = 1; break;
  338. #endif
  339.                 case 'v': Verbose=1; set_var(LEVEL_SET,v_verbose,"hs"); break;
  340.                 case 'b': oldtaskpri=Myprocess->pr_Task.tc_Node.ln_Pri;
  341.                           SetTaskPri( &Myprocess->pr_Task, -1 ); break;
  342.                 case 'f': oldtaskpri=Myprocess->pr_Task.tc_Node.ln_Pri;
  343.                           SetTaskPri( &Myprocess->pr_Task,  1 ); break;
  344.                 case 'a': o_nowindow= o_noraw= 1;
  345.                           set_var( LEVEL_SET, v_hilite, "" ); break;
  346. #if 0
  347.                 case 'm': unset_var(LEVEL_SET,v_nomatch); break;
  348. #endif
  349.                 case 'm': set_var(LEVEL_SET,v_nomatch,"1"); break;
  350.                 case 't': o_nowindow= o_vt100= o_nofastscr= 1;
  351.                           set_var( LEVEL_SET, v_hilite, "b" /*"r"*/ );
  352.                           set_var( LEVEL_SET, v_noreq, "1" );
  353.                           set_var( LEVEL_SET, "_terminal", "1" );
  354.                           set_var( LEVEL_ALIAS, "cls", "e -n ^[[0\\;0H^[[J" );
  355.                           break;
  356.                 case 's': DOSBase->dl_Root->rn_Flags |= RNF_WILDSTAR;
  357.                           break;
  358.                 case 'w': /* just don't use window pointer */
  359.                           o_nowindow = 1;
  360.                           break;
  361.                 case 'V': /* strict VT100 mode */
  362.                           o_vt100 = 1;
  363.                           break;
  364.                 }
  365.             }
  366.         } else {
  367.             sprintf (Inline, "source %s",argv[i]);
  368.             execute (Inline);
  369.         }
  370.     }
  371.  
  372.     /* we should do "main_exit(Lastresult);" here if 
  373.        csh was called with some files to source ... */
  374.  
  375. #if 0
  376.     if( fh=Open( "CONSOLE:" , MODE_NEWFILE) ) {
  377.         if (Console != ((struct FileHandle *)(4*fh))->fh_Type)
  378.             Write(fh,"Funny Console!\n",15);
  379.         else
  380.             Write(fh,"Real Console!\n",14);
  381.         Close(fh);
  382.     }
  383.     else
  384.         printf("No Console!\n");
  385.  
  386.     Write(Input(),"Input()!\n",9);
  387.     Write(Output(),"Output()!\n",10);
  388. #endif
  389.  
  390.     if( !IsInteractive(Input()) ) {
  391.         main_exit( Lastresult );
  392.     }
  393.  
  394. #ifdef DO_ACS_KLUDGE
  395.     if (acs_kludge>0) {
  396.         BPTR bfh = Open("*", MODE_OLDFILE);
  397.         struct FileHandle *fh;
  398.         long xrslt,xioerr;
  399.         if (bfh != NULL) {
  400.             fh = BADDR(bfh);
  401.             if (fh->fh_Type != NULL) {
  402.                 xrslt = DoPkt( (void *)fh->fh_Type, ACTION_CHANGE_SIGNAL, fh->fh_Arg1, (long)&(Myprocess->pr_MsgPort), NULL,NULL,NULL);
  403.                 /* also possible instead of fh->fh_Type: Myprocess->pr_ConsoleTask */
  404.                 xioerr = IoErr();
  405.                 acs_oldport = (struct MsgPort *)xioerr;
  406.                 printf("ACS_1: DoPkt()=%ld, fh_Type=%ld, ConTask=%ld, pr_MsgPort=%ld, IoErr()=%ld\n",xrslt,(long)fh->fh_Type,(long)Myprocess->pr_ConsoleTask,(long)&(Myprocess->pr_MsgPort),xioerr);
  407.             }
  408.             Close(bfh);
  409.         }
  410.         acs_kludge = 2;
  411.     }
  412. #endif
  413.  
  414.     for (;;) {
  415.         if (breakcheck())
  416.             while (WaitForChar(Input(), 100L) || CHARSWAIT( stdin ))
  417.                 gets(Inline);
  418.         clearerr(stdin);  /* prevent acidental quit */
  419.         /*
  420.            AMK: new position of breakreset() before exec_every(),
  421.                 old position caused enforcer hits
  422.                 (do 'set _every "set cols = $(wincols)"' and
  423.                  then abort (^c) a command)
  424.         */
  425.         breakreset();
  426.         exec_every();
  427.  
  428.         { /* AMK TEST BEGIN */
  429.             char *old;
  430.             char pwd[256];
  431.  
  432.             /* no requesters when no disk is inserted */
  433.             Myprocess->pr_WindowPtr = (APTR)(-1);
  434.  
  435. #if 1
  436.             if (NameFromLock(Myprocess->pr_CurrentDir, pwd, 255L)) {
  437.                 if( !(old=get_var(LEVEL_SET, v_cwd)) )
  438.                     old="";
  439.                 if (strcmp(pwd,old)) {
  440.                     set_var(LEVEL_SET, v_lcd, old);
  441.                     set_cwd();
  442.                 }
  443.             }
  444. #else
  445.             if (!NameFromLock(Myprocess->pr_CurrentDir, pwd, 255L)) {
  446.                 fprintf(stderr,"csh.main: NameFromLock() failed\n");
  447.                 strcpy(pwd,"<unknown>");
  448.             }
  449.             if( !(old=get_var(LEVEL_SET, v_cwd)) )
  450.                 old="";
  451.             if (strcmp(pwd,old)) {
  452.                 set_var(LEVEL_SET, v_lcd, old);
  453.                 set_cwd();
  454.             }
  455. #endif
  456.             /*update_sys_var(v_cwd);*/
  457.  
  458.             Myprocess->pr_WindowPtr = o_noreq ? (APTR) -1L : 0L/*Mywindow*/;
  459.  
  460.         } /* AMK TEST BEGIN */
  461.  
  462.         update_sys_var(v_titlebar);
  463.         update_sys_var(v_prompt);
  464.         /*
  465.            AMK: old position of breakreset(), causes enforcer hits
  466.                 (do 'set _every "set cols = $(wincols)"' and
  467.                  then abort (^c) a command)
  468.         */
  469.         breakreset();
  470. #if RAW_CONSOLE
  471.         if (Quit || !rawgets(Inline, disable ? "_ " : trueprompt)) main_exit(0);
  472. #else
  473.         printf("%s", disable ? "_ " : trueprompt);
  474.         fflush(stdout);
  475.         if (Quit || !gets(Inline)) main_exit(0);
  476. #endif
  477.         breakreset();
  478.  
  479. #if 1
  480.         /* start/stop timer only if non-empty command line */
  481.  
  482.         if (*Inline && strlen(Inline)>0) {
  483.             exectimer(0);
  484.             ExecRC = exec_command(Inline);
  485.             exectimer(1);
  486.         }
  487. #else
  488.         /* start/stop timer for every command  */
  489.  
  490.         exectimer(0);
  491.         if (*Inline) ExecRC=exec_command(Inline);
  492.         exectimer(1);
  493. #endif
  494.     }
  495. }
  496.  
  497.  
  498. void
  499. main_exit(n)
  500. {
  501.     int i;
  502.     char buf[10];
  503.  
  504.     /* AMK: OS20-GetVar replaces ARP-Getenv */
  505.     GetVar(shellctr,buf,10L,GVF_GLOBAL_ONLY|GVF_BINARY_VAR);
  506.     i=atoi(buf);
  507.     sprintf(buf,"%d",i-1);
  508.     /* AMK: OS20-SetVar replaces ARP-Setenv */
  509.     SetVar(shellctr, buf, -1L, GVF_GLOBAL_ONLY|GVF_BINARY_VAR);
  510.  
  511. #ifdef DO_ACS_KLUDGE
  512.     if (acs_kludge>0) {
  513.         BPTR bfh = Open("*", MODE_OLDFILE);
  514.         struct FileHandle *fh;
  515.         long xrslt,xioerr;
  516.         if (bfh != NULL) {
  517.             fh = BADDR(bfh);
  518.             if (fh->fh_Type != NULL) {
  519.                 xrslt = DoPkt( (void *)fh->fh_Type, ACTION_CHANGE_SIGNAL, fh->fh_Arg1, (long)acs_oldport, NULL,NULL,NULL);
  520.                 /* also possible instead of fh->fh_Type: Myprocess->pr_ConsoleTask */
  521.                 xioerr = IoErr();
  522.                 printf("ACS_2: DoPkt()=%ld, fh_Type=%ld, ConTask=%ld, oldport=%ld, IoErr()=%ld\n",xrslt,(long)fh->fh_Type,(long)Myprocess->pr_ConsoleTask,(long)acs_oldport,xioerr);
  523.             }
  524.             Close(bfh);
  525.         }
  526.         acs_kludge = 0;
  527.     }
  528. #endif
  529.  
  530.     if( !nowintitle && oldtitle )
  531.         SetWindowTitles(Mywindow,oldtitle,(char *)-1);
  532.     if( oldtaskpri != -999 )
  533.         SetTaskPri(&Myprocess->pr_Task,oldtaskpri);
  534.  
  535.     if (Mywindow)
  536.         Myprocess->pr_WindowPtr = old_WindowPtr;
  537.  
  538.     for (i=1; i<MAXMYFILES; i++) myclose(i);
  539.  
  540.     /* no effect if AnzMenus==0 (no menus in use) */
  541.     remove_menu();
  542.  
  543. #ifdef MULTIUSER_SUPPORT
  544.     /* LILJA: Added for multiuser-support */
  545.     if (muBase) CloseLibrary((struct Library *)muBase);
  546. #endif
  547.  
  548.     if (PatternBase) CloseLibrary( PatternBase );
  549.     CloseLibrary((struct Library *)GadToolsBase);
  550.     CloseLibrary((struct Library *)AslBase);
  551.     CloseLibrary((struct Library *)IntuitionBase);
  552.     CloseLibrary((struct Library *)GfxBase);
  553.     exit(n);
  554. }
  555.  
  556.  
  557. static void
  558. exectimer(int stop)
  559. {
  560.     struct DateStamp dss;
  561.     static long lasttime;
  562.     long time;
  563.  
  564.     DateStamp(&dss);
  565.     time=dss.ds_Minute*6000+2*dss.ds_Tick;   /* 2 = 100/TickPerSec   */
  566.     if( stop )
  567.         ExecTimer=time-lasttime;
  568.     else
  569.         lasttime=time;
  570. }
  571.  
  572.  
  573. /* print break message only once */
  574. static BOOL dobreak_output = TRUE;
  575.  
  576. int
  577. breakcheck()
  578. {
  579.     return !o_nobreak && SetSignal(0L,0L) & SIGBREAKF_CTRL_C;
  580. }
  581.  
  582. void
  583. breakreset()
  584. {
  585.     /* We reset CTRL_E here for GMD's foreach() hack ... */
  586.     /* Why not just reset CTRL_F also -- for completeness? :) */
  587.  
  588.     SetSignal(0L, SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D|SIGBREAKF_CTRL_E);
  589.     dobreak_output = TRUE;
  590. }
  591.  
  592. dobreak()
  593. {
  594.     if (breakcheck()) {
  595.         if (dobreak_output) {
  596.             printf("^C\n");
  597.             dobreak_output = FALSE;
  598.         }
  599.         return(1);
  600.     }
  601.     return(0);
  602. }
  603.  
  604. /* this routine causes manx to use this Chk_Abort() rather than it's own */
  605. /* otherwise it resets our ^C when doing any I/O (even when Enable_Abort */
  606. /* is zero).  Since we want to check for our own ^C's             */
  607.  
  608. long
  609. Chk_Abort()
  610. {
  611.     return(0);
  612. }
  613.  
  614. void
  615. _wb_parse()
  616. {
  617. }
  618.  
  619. do_howmany()
  620. {
  621.     char buf[10];
  622.  
  623.     /* AMK: OS20-GetVar replaces ARP-Getenv */
  624.     GetVar(shellctr, buf, 10L, GVF_GLOBAL_ONLY|GVF_BINARY_VAR);
  625.     printf("Shell(s) running: %s\n",buf);
  626.     return 0;
  627. }
  628.  
  629. static void set_kickversion(void)
  630. {
  631.     struct Library *verlib;
  632.     static char verstr[10];
  633.     if (verlib = OpenLibrary("version.library",0)) {
  634.         sprintf(verstr,"%d",verlib->lib_Version);
  635.         CloseLibrary(verlib);
  636.     }
  637.     else
  638.         sprintf(verstr,"%d",SysBase->LibNode.lib_Version);
  639.     set_var(LEVEL_SET,"_kick",verstr);
  640. }
  641.  
  642. #if 1
  643. static struct Window *
  644. getwindow(void)
  645. {
  646.     struct Window *win = NULL;
  647.  
  648.     if( o_nowindow )
  649.         return NULL;
  650.  
  651.     if( IsInteractive(Output()) )
  652.         Write(Output(),"",1);     /*    make window appear */
  653.  
  654.     if( isconsole(Output()) && Output() ) {
  655.         if ( ((struct FileHandle *)BADDR(Output()))->fh_Type ) {
  656.             struct InfoData *infodata;
  657.             infodata=(void *)SAllocMem((long)sizeof(struct InfoData),MEMF_CLEAR|MEMF_PUBLIC);
  658.             if (DoPkt((void *)((struct FileHandle *)BADDR(Output()))->fh_Type,ACTION_DISK_INFO,((LONG)infodata)>>2,NULL,NULL,NULL,NULL)) {
  659.                 win=(struct Window *)infodata->id_VolumeNode;
  660.             }
  661.             FreeMem(infodata,sizeof(struct InfoData));
  662.         }
  663.         if( win==NULL )
  664.             o_nowindow=1;
  665.     }
  666.  
  667.     newwidth();
  668.  
  669.     return win;
  670. }
  671. #else
  672. static struct Window *
  673. getwindow(void)
  674. {
  675.     struct Window *win = NULL;
  676.  
  677.     if( o_nowindow )
  678.         return NULL;
  679.     if( isconsole(Output()))
  680.         Write(Output(),"",1);     /*    make window appear */
  681.     if( Myprocess->pr_ConsoleTask ) {
  682.         struct InfoData *infodata;
  683.         infodata=(void *)SAllocMem((long)sizeof(struct InfoData),MEMF_CLEAR|MEMF_PUBLIC);
  684.         if (DoPkt((void *)Myprocess->pr_ConsoleTask,ACTION_DISK_INFO,((LONG)infodata)>>2,NULL,NULL,NULL,NULL)) {
  685.             win=(struct Window *)infodata->id_VolumeNode;
  686.         }
  687.         FreeMem(infodata,sizeof(struct InfoData));
  688.         if( win==NULL )
  689.             o_nowindow=1;
  690.     }
  691.     newwidth();
  692.     return win;
  693. }
  694. #endif
  695.  
  696. #ifdef LATTICE
  697.  
  698. int
  699. setenv( char *var, char *val )
  700. {
  701.     char *buf=salloc(strlen(var)+strlen(val)+10);
  702.     sprintf(buf, "%s=%s", var, val );
  703.     Free(buf);
  704.     return putenv(buf);
  705. }
  706.  
  707. __regargs int __chkabort(void) { return(0); }
  708.  
  709. #endif
  710.  
  711. #ifdef _DCC
  712. void
  713. swapmem(void *_s1, void *_s2, size_t _n)
  714. {
  715.     char t, *s1=_s1, *s2=_s2;
  716.     int  i;
  717.  
  718.     for( i=0; i<_n; i++ )
  719.         t = *s1; *s1++ = *s2; *s2++=t;
  720. }
  721.  
  722. int
  723. setenv( char *var, char *val )
  724. {
  725.     BPTR fh;
  726.     char buf[40];
  727.     sprintf(buf,"ENV:%s",var );
  728.  
  729.     if( fh=Open(buf,MODE_NEWFILE) ) {
  730.         Write(fh,val,strlen(val));
  731.         Close(fh);
  732.     }
  733. }
  734.  
  735. int
  736. rand()
  737. {
  738.     return 0;
  739. }
  740.  
  741. _waitwbmsg() {return 0;};
  742.  
  743. #endif
  744.