home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 411b.lha / dme_1.42 / src.LZH / src / main.c < prev    next >
C/C++ Source or Header  |  1990-08-20  |  19KB  |  907 lines

  1.  
  2. /*
  3.  * MAIN.C
  4.  *
  5.  *    (C)Copyright 1987 by Matthew Dillon, All Rights Reserved.
  6.  *
  7.  */
  8.  
  9. #include "defs.h"
  10. #include <libraries/dos.h>
  11. #include <libraries/dosextens.h>
  12. #include <workbench/startup.h>
  13. #include <workbench/workbench.h>
  14.  
  15. typedef struct Process        PROC;
  16. typedef struct WBStartup    WBS;
  17. typedef struct DiskObject   DISKOBJ;
  18.  
  19. #define IDCMPFLAGS   CLOSEWINDOW|NEWSIZE|RAWKEY|MOUSEBUTTONS|ACTIVEWINDOW|MOUSEMOVE|MENUPICK
  20.  
  21. extern void GeometryToNW();
  22. extern WIN *OpenWindow();
  23. extern void *OpenLibrary();
  24. extern void *GetDiskObject();
  25.  
  26. struct NewWindow Nw = {
  27.    0, 1, 0  , 0  , -1, -1,  /*    width, height filled in by program */
  28.    IDCMPFLAGS,
  29.    ACTIVATE|WINDOWSIZING|WINDOWDRAG|WINDOWDEPTH|WINDOWCLOSE|NOCAREREFRESH|RMBTRAP,
  30.    NULL, NULL, (ubyte *)"   WAIT   ",
  31.    NULL, NULL,
  32.    32, 32, -1, -1,
  33.    WBENCHSCREEN
  34. };
  35.  
  36. short Sharedrefs;
  37. short Oldtlen = 999;      /*  Old Title Length      */
  38. struct MsgPort *Sharedport;
  39. static DISKOBJ *Do;
  40. WBS    *Wbs;
  41.  
  42. short Xsize,  Ysize;        /* font character sizes        */
  43. short Rows,  Columns;        /* character rows/cols available       */
  44. short Xbase,  Ybase;        /* offset pixel base for display       */
  45. short XTbase,YTbase;        /* used for text display           */
  46. short Xpixs,  Ypixs;        /* actual # X/Y pixels available       */
  47. short Mx, My;
  48.  
  49. ubyte *av[8];
  50. char Quitflag;
  51. char Overide;
  52. char Wdisable = 1;        /* Disable icon save               */
  53. char MShowTitle, MForceTitle;
  54.  
  55. PORT *IPCPort;
  56. PORT *IPCRPort;
  57. long Mask;
  58.  
  59. struct IntuitionBase *IntuitionBase;
  60. struct GfxBase *GfxBase;
  61. struct Library *IconBase;
  62.  
  63. extern int Enable_Abort;
  64.  
  65. static char *Ffile;
  66.  
  67. wbmain(wbs)
  68. WBS *wbs;
  69. {
  70.     return(main(0, wbs));
  71. }
  72.  
  73. main(mac, mav)
  74. int mac;
  75. char **mav;
  76. {
  77.     char nf, ni;        /*    # files on command line     */
  78.     char notdone;        /*    for endless loop        */
  79.     char iawm = 0;        /*    overide mouse buttons        */
  80.     char dontwait = 0;        /*    don't wait for a message    */
  81.     short i;
  82.     short Code;
  83.     PROC *proc = (PROC *)FindTask(NULL);
  84.     BPTR origlock;
  85.  
  86.     fclose(stdin);
  87.     /*fclose(stdout);*/     /*  assume person will run >nil:    */
  88.     fclose(stderr);         /*  close stderr & console ref. */
  89.     origlock = CurrentDir(DupLock((BPTR)proc->pr_CurrentDir));
  90.  
  91.     NewList((LIST *)&DBase);
  92.     NewList((LIST *)&PBase);
  93.     IntuitionBase = OpenLibrary("intuition.library", 0);
  94.     GfxBase = OpenLibrary("graphics.library", 0);
  95.     if (IntuitionBase == (void *)NULL || GfxBase == (void *)NULL)
  96.     exiterr("cannot open intuition or graphics library");
  97.  
  98.     init_command();
  99.  
  100.     String  = (char *)malloc(1);        /*  initialize scanf variable   */
  101.     *String = 0;
  102.  
  103.     if (mac == 0) {             /*  WORKBENCH STARTUP   */
  104.     Wdisable = 0;        /*  allow icon save    */
  105.     Wbs = (WBS *)mav;
  106.     IconBase = OpenLibrary("icon.library", 0);
  107.     if (IconBase == NULL)
  108.         exiterr("unable to open icon library");
  109.     }
  110.  
  111. #if AREXX
  112.     {
  113.     mountrequest(0);
  114.     openrexx();     /*   do this after the last possible call to exiterr() */
  115.     mountrequest(1);
  116.     }
  117. #endif
  118.     resethash();
  119.  
  120.     if (Wbs) {
  121.     if (Wbs->sm_ArgList[0].wa_Lock) {
  122.         BPTR savelock = CurrentDir((BPTR)Wbs->sm_ArgList[0].wa_Lock);
  123.         if (Do = GetDiskObject(Wbs->sm_ArgList[0].wa_Name)) {
  124.         ops(Do->do_ToolTypes, 1);
  125.         FreeDiskObject(Do);
  126.         }
  127.         CurrentDir(savelock);
  128.     }
  129.     nf = Wbs->sm_NumArgs - 1;
  130.     mac = 99;
  131.     } else {
  132.     nf = ops(mav+1, 0);
  133.     }
  134.  
  135.     for (ni = 0, i = 1; i < mac; ++i) {
  136.     char *str;
  137.     DISKOBJ *dso;
  138.     if (Wbs) {
  139.         if (i > nf)
  140.         break;
  141.         str = Wbs->sm_ArgList[i].wa_Name;
  142.         UnLock(CurrentDir(DupLock((BPTR)Wbs->sm_ArgList[i].wa_Lock)));
  143.         if (dso = GetDiskObject(Wbs->sm_ArgList[i].wa_Name)) {
  144.         ops(dso->do_ToolTypes, 1);
  145.         FreeDiskObject(dso);
  146.         }
  147.     } else {
  148.         str = mav[i];
  149.         if (*str == '-')
  150.         continue;
  151.     }
  152.     do_newwindow();
  153.     ++ni;
  154.     av[0] = (ubyte *)"newfile";
  155.     av[1] = (ubyte *)str;
  156.     do_edit();
  157.     MForceTitle = 1;
  158.     window_title();
  159.     }
  160.     if (nf == 0)                    /* no files to edit */
  161.     do_newwindow();
  162.  
  163.     mountrequest(0);
  164.     av[0] = NULL;
  165.     av[1] = (ubyte *)"s:.edrc";
  166.     do_source();
  167.     av[0] = NULL;
  168.     av[1] = (ubyte *)((Ffile) ? Ffile : ".edrc");
  169.     do_source();
  170.     mountrequest(1);
  171.     {                /*    1.29c    */
  172.     ED *ep;
  173.     ED *eb = Ep;
  174.     if (eb) {
  175.         for (ep = (ED *)eb->Node.mln_Succ; ep->Node.mln_Succ; ep = (ED *)ep->Node.mln_Succ) {
  176.         ep->Tabstop = eb->Tabstop;
  177.         ep->Margin  = eb->Margin;
  178.         ep->Insertmode = eb->Insertmode;
  179.         ep->IgnoreCase = eb->IgnoreCase;
  180.         ep->Wordwrap   = eb->Wordwrap;
  181.         if (eb->Font) {
  182.             ep->Font = eb->Font;
  183.             ++eb->Font->tf_Accessors;
  184.         }
  185.         }
  186.     }
  187.     }
  188.     title("DME V1.42 \251Copyright 1988-1990 by Matthew Dillon,  All Rights Reserved             ");
  189.     Mask |= 1 << Ep->Win->UserPort->mp_SigBit;
  190. loop:
  191.     if (!Ep->iconmode)
  192.     text_cursor(1);
  193.     for (notdone = 1; !Quitflag && notdone;) {
  194.     char mmove = 0;
  195.     short mqual;
  196.  
  197.     if (!Ep->iconmode)
  198.         window_title();
  199.     if (dontwait) {
  200.         --dontwait;
  201.     } else {
  202.         Wait(Mask);
  203.     }
  204.  
  205.     /*
  206.      *  NOTE: due to operation of breakcheck(), the userport signal
  207.      *  may not be set even if there are messages pending.
  208.      *
  209.      *  NOTE2: CheckPort() requires dres.library, which will be loaded
  210.      *  if IPCPort exists because the IPC needs it also.
  211.      */
  212.  
  213.     {
  214.         IMESS *im;
  215.         while (im = (IMESS *)GetMsg(Ep->Win->UserPort)) {
  216.         Msgchk = 1;
  217.         Abortcommand = 0;
  218.         Code = im->Code;
  219.         if (im->IDCMPWindow != Ep->Win) {
  220.             Overide = 0;
  221.             if (Comlinemode)
  222.             escapecomlinemode();
  223.             text_sync();
  224.             MShowTitle = 0;
  225.             if (!Ep->iconmode)
  226.             window_title();
  227.             if (text_switch(im->IDCMPWindow) == 0) {
  228.             ReplyMsg((MSG *)im);
  229.             continue;
  230.             }
  231.         }
  232.         Mx = im->MouseX;
  233.         My = im->MouseY;
  234.         switch(im->Class) {
  235.         case NEWSIZE:
  236.             if (!Ep->iconmode) {
  237.             if (Comlinemode)
  238.                 escapecomlinemode();
  239.             set_window_params();
  240.             if (!text_sync())
  241.                 text_redisplay();
  242.             text_cursor(1);
  243.             }
  244.             break;
  245.         case MOUSEBUTTONS:
  246.             switch(Code) {
  247.             case SELECTDOWN:
  248.             case MENUDOWN:
  249.             if (Ep->iconmode || iawm) {
  250.                 uniconify();
  251.                 break;
  252.             }
  253.             Forbid();
  254.             Ep->Win->Flags |= REPORTMOUSE;
  255.             Permit();
  256.             uniconify();
  257.             text_cursor(0);
  258.             keyctl(NULL, im->Code|0x80, im->Qualifier);
  259.             text_cursor(1);
  260.             break;
  261.             case SELECTUP:
  262.             case MENUUP:
  263.             Forbid();
  264.             Ep->Win->Flags &= ~REPORTMOUSE;
  265.             Permit();
  266.             break;
  267.             }
  268.             break;
  269.         case RAWKEY:
  270.             if ((im->Code & 0x80) == 0) {
  271.             /*  Handled in command interpreter.
  272.             if (Ep->iconmode) {
  273.                 uniconify();
  274.                 break;
  275.             }
  276.             */
  277.             text_cursor(0);
  278.             keyctl(im, im->Code, im->Qualifier);
  279.             text_cursor(1);
  280.             }
  281.             break;
  282.         case MENUPICK:
  283.             {
  284.             char *str = menu_cmd(im);
  285.             if (str) {
  286.                 str = strcpy(malloc(strlen(str)+1), str);
  287.                 text_cursor(0);
  288.                 do_command(str);
  289.                 free(str);
  290.                 text_cursor(1);
  291.             }
  292.             }
  293.             break;
  294.         case CLOSEWINDOW:
  295.             if (Comlinemode)
  296.             escapecomlinemode();
  297.             text_sync();
  298.             notdone = 0;
  299.             break;
  300.         case ACTIVEWINDOW:
  301.             if (!Ep->iconmode)
  302.             iawm = 1;
  303.             break;
  304.         case MOUSEMOVE:
  305.             mmove = 1;
  306.             mqual = im->Qualifier;
  307.             break;
  308.         }
  309.         if (im)
  310.             ReplyMsg((MSG *)im);
  311.         if (notdone == 0 || Quitflag) {
  312.             dontwait = 2;
  313.             goto boom;
  314.         }
  315.         }
  316.     }
  317.  
  318.     iawm = 0;
  319.     if (mmove) {
  320.         uniconify();
  321.         mmove = 0;
  322.         text_cursor(0);
  323.         keyctl(NULL, QMOVE, mqual);
  324.         text_cursor(1);
  325.     }
  326.     closesharedwindow(NULL);
  327.     }
  328. boom:
  329.     text_sync();
  330.     if (Ep->Modified && !Overide) {
  331.     uniconify();
  332.     Overide = 1;
  333.     title("*** File has been modified ***");
  334.     Quitflag = 0;
  335.     goto loop;
  336.     }
  337.     SetWindowTitles(Ep->Win, "", (char *)-1);
  338.     {
  339.     WIN *win = Ep->Win;
  340.     text_uninit();
  341.     closesharedwindow(win);
  342.     }
  343.     if (Ep) {
  344.     Quitflag = 0;
  345.     if (!Ep->iconmode)
  346.         set_window_params();
  347.     text_load();
  348.     MShowTitle = 0;
  349.     goto loop;
  350.     }
  351.     closesharedwindow(NULL);
  352. #if AREXX
  353.     closerexx();
  354. #endif
  355.     UnLock(CurrentDir(origlock));
  356.     if (IPCRPort)
  357.     DeletePort(IPCRPort);
  358.     if (IconBase)
  359.     CloseLibrary(IconBase);
  360.     if (GfxBase)
  361.     CloseLibrary(GfxBase);
  362.     if (IntuitionBase)
  363.     CloseLibrary(IntuitionBase);
  364.     IconBase = GfxBase = IntuitionBase = 0;
  365.     dealloc_hash();
  366.     return(0);
  367. }
  368.  
  369. void
  370. do_iconify()
  371. {
  372.     text_sync();
  373.     if (!Comlinemode)
  374.     iconify();
  375. }
  376.  
  377. void
  378. do_tomouse()
  379. {
  380.     text_position((Mx-Xbase)/Xsize, (My-Ybase)/Ysize);
  381. }
  382.  
  383. void
  384. iconify()
  385. {
  386.     WIN *newwin;
  387.     ED *ep = Ep;
  388.     WIN *win = ep->Win;
  389.  
  390.     if (!ep->iconmode) {
  391.     ep->Winx      = win->LeftEdge;
  392.     ep->Winy      = win->TopEdge;
  393.     ep->Winwidth  = win->Width;
  394.     ep->Winheight = win->Height;
  395.     Nw.Height = 10;
  396.     Nw.Width  = 20 + 5*8 + strlen(Ep->Name)*8;
  397.     Nw.LeftEdge= ep->IWinx;
  398.     Nw.TopEdge = ep->IWiny;
  399.     if (Nw.LeftEdge + Nw.Width > win->WScreen->Width)
  400.         Nw.LeftEdge = win->WScreen->Width - Nw.Width;
  401.     if (Nw.TopEdge + Nw.Height > win->WScreen->Height)
  402.         Nw.TopEdge = win->WScreen->Height - Nw.Height;
  403.     Nw.Title = ep->Wtitle;
  404.     Nw.Flags &= ~(WINDOWSIZING|WINDOWDEPTH|ACTIVATE);
  405.     Nw.Flags |= BORDERLESS;
  406.     Nw.DetailPen = ep->BGPen;
  407.     Nw.BlockPen  = ep->FGPen;
  408.     if (win->Flags & WINDOWACTIVE)      /*  KTS */
  409.         Nw.Flags |= ACTIVATE;
  410.     sprintf(ep->Wtitle, "%s     ", Ep->Name);
  411.     if (newwin = opensharedwindow(&Nw)) {
  412.         closesharedwindow(win);
  413.         Nw.BlockPen = -1;
  414.         Nw.Flags |= WINDOWSIZING|WINDOWDEPTH;
  415.         Nw.Flags &= ~BORDERLESS;
  416.         ep->iconmode = 1;
  417.         ep->Win = newwin;
  418.     }
  419.     }
  420. }
  421.  
  422. void
  423. uniconify()
  424. {
  425.     ED *ep = Ep;
  426.     WIN *win = ep->Win;
  427.     WIN *newwin;
  428.     RP *rp;
  429.  
  430.     if (ep->iconmode) {
  431.     ep->IWinx = win->LeftEdge;
  432.     ep->IWiny = win->TopEdge;
  433.     Nw.LeftEdge = ep->Winx;
  434.     Nw.TopEdge  = ep->Winy;
  435.     Nw.Width    = ep->Winwidth;
  436.     Nw.Height   = ep->Winheight;
  437.     Nw.Title    = ep->Wtitle;
  438.     Nw.DetailPen = ep->BGPen;
  439.     Nw.BlockPen  = ep->FGPen;
  440.  
  441.     if (newwin = opensharedwindow(&Nw)) {
  442.         closesharedwindow(win);
  443.         win= ep->Win = newwin;
  444.         rp = win->RPort;
  445.  
  446.         menu_strip(win);
  447.         if (ep->Font)
  448.         SetFont(rp, ep->Font);
  449.         set_window_params();
  450.         if (!text_sync())
  451.         text_redisplay();
  452.         text_cursor(1);
  453.         MShowTitle = 0;
  454.         window_title();
  455.         ep->iconmode = 0;
  456.     }
  457.     }
  458. }
  459.  
  460.  
  461. void
  462. do_newwindow()
  463. {
  464.     WIN *win;
  465.  
  466.     if (Ep)
  467.     text_sync();
  468.     Nw.Title = (ubyte *)"    OK    ";
  469.  
  470.     if (text_init(Ep, NULL, &Nw)) {
  471.     if (win = opensharedwindow(&Nw)) {
  472.         menu_strip(win);
  473.         Ep->Win = win;
  474.         set_window_params();
  475.         text_load();
  476.     } else {
  477.         text_uninit();
  478.     }
  479.     }
  480. }
  481.  
  482. /*
  483.  *  openwindow with geometry specification.  Negative number specify
  484.  *  relative-right / relative-left (leftedge & topedge), or relative-width /
  485.  *  relative height (width & height).
  486.  *
  487.  *    <leftedge><topedge><width><height>
  488.  *
  489.  *  Example:    +10+10-20-20    Open window centered on screen 10 pixels
  490.  *                from the border on all sides.
  491.  */
  492.  
  493. void
  494. do_openwindow()
  495. {
  496.     WIN *win;
  497.  
  498.     if (Ep)
  499.     text_sync();
  500.     Nw.Title = (ubyte *)"    OK    ";
  501.  
  502.     if (text_init(Ep, NULL, &Nw)) {
  503.     GeometryToNW(av[1], &Nw);
  504.     if (win = opensharedwindow(&Nw)) {
  505.         menu_strip(win);
  506.         Ep->Win = win;
  507.         set_window_params();
  508.         text_load();
  509.     } else {
  510.         text_uninit();
  511.     }
  512.     }
  513. }
  514.  
  515.  
  516. WIN *
  517. TOpenWindow(nw)
  518. struct NewWindow *nw;
  519. {
  520.     WIN *win;
  521.  
  522.     while ((win = OpenWindow(nw)) == NULL) {
  523.     if (nw->Width < 50 || nw->Height < 50)
  524.         break;
  525.     nw->Width -= 10;
  526.     nw->Height-= 10;
  527.     }
  528.     return(win);
  529. }
  530.  
  531.  
  532. WIN *
  533. opensharedwindow(nw)
  534. struct NewWindow *nw;
  535. {
  536.     WIN *win;
  537.  
  538.     if (Sharedport)
  539.     nw->IDCMPFlags = NULL;
  540.     else
  541.     nw->IDCMPFlags = IDCMPFLAGS;
  542.     win = TOpenWindow(nw);
  543.     if (win) {
  544.     if (Sharedport) {
  545.         win->UserPort = Sharedport;
  546.         ModifyIDCMP(win, IDCMPFLAGS);
  547.     } else {
  548.         Sharedport = win->UserPort;
  549.     }
  550.     ++Sharedrefs;
  551.     SetAPen(win->RPort, nw->DetailPen);
  552.     RectFill(win->RPort, win->BorderLeft, win->BorderTop, win->Width - win->BorderRight - 1, win->Height - win->BorderBottom - 1);
  553.     SetAPen(win->RPort, nw->BlockPen);
  554.     }
  555.     return(win);
  556. }
  557.  
  558. void
  559. closesharedwindow(win)
  560. WIN *win;
  561. {
  562.     static WIN *wunlink;
  563.     char notoktoclosenow = 0;
  564.  
  565.     if (win) {
  566.     SetWindowTitles(win, "", (char *)-1);
  567.     ClearMenuStrip(win);
  568.     Forbid();
  569.     win->UserPort = NULL;
  570.     ModifyIDCMP(win, GADGETUP);     /* NEVER occurs */
  571.  
  572.     notoktoclosenow = 1;
  573.  
  574.     Permit();
  575.     if (notoktoclosenow) {
  576.         win->UserData = (char *)wunlink;
  577.         wunlink = win;
  578.     } else {
  579.         CloseWindow(win);
  580.     }
  581.     --Sharedrefs;
  582.     } else {
  583.     if (Sharedrefs == 0 && Sharedport) {
  584.         DeletePort(Sharedport);
  585.         Sharedport = NULL;
  586.     }
  587.     for (win = wunlink; win; win = wunlink) {
  588.         wunlink = (WIN *)win->UserData;
  589.         CloseWindow(win);
  590.     }
  591.     wunlink = NULL;
  592.     }
  593. }
  594.  
  595.  
  596. getyn(text)
  597. char *text;
  598. {
  599.     int result;
  600.     ITEXT *body, *pos, *neg;
  601.  
  602.     body = (ITEXT *)AllocMem(sizeof(ITEXT), 0);
  603.     pos  = (ITEXT *)AllocMem(sizeof(ITEXT), 0);
  604.     neg  = (ITEXT *)AllocMem(sizeof(ITEXT), 0);
  605.     clrmem(body, sizeof(ITEXT));
  606.     clrmem(pos , sizeof(ITEXT));
  607.     clrmem(neg , sizeof(ITEXT));
  608.     body->BackPen = pos->BackPen = neg->BackPen = 1;
  609.     body->DrawMode= pos->DrawMode= neg->DrawMode= AUTODRAWMODE;
  610.     body->LeftEdge = 10;
  611.     body->TopEdge  = 12;
  612.     body->IText    = (ubyte *)text;
  613.     pos->LeftEdge = AUTOLEFTEDGE;
  614.     pos->TopEdge = AUTOTOPEDGE;
  615.     pos->IText = (ubyte *)"OK";
  616.     neg->LeftEdge = AUTOLEFTEDGE;
  617.     neg->TopEdge = AUTOTOPEDGE;
  618.     neg->IText = (ubyte *)"CANCEL";
  619.     result = AutoRequest(Ep->Win,body,pos,neg,0,0,320,58);
  620.     FreeMem(body, sizeof(ITEXT));
  621.     FreeMem(pos , sizeof(ITEXT));
  622.     FreeMem(neg , sizeof(ITEXT));
  623.     return(result);
  624. }
  625.  
  626. void
  627. title(buf)
  628. char *buf;
  629. {
  630.     SetWindowTitles(Ep->Win, buf, (char *)-1);
  631.     Oldtlen = 999;
  632.     MShowTitle = 3;
  633. }
  634.  
  635. void
  636. window_title()
  637. {
  638.     int len, maxlen;
  639.  
  640.     if (memoryfail) {
  641.     title(" -- NO MEMORY -- ");
  642.     memoryfail = 0;
  643.     text_redisplay();
  644.     }
  645.     if (MForceTitle) {
  646.     MShowTitle = 0;
  647.     MForceTitle = 0;
  648.     }
  649.     if (MShowTitle) {
  650.     --MShowTitle;
  651.     return;
  652.     }
  653.     {
  654.     char *mod;
  655.     FONT *oldfont;
  656.     ED *ep = Ep;
  657.     WIN *win = ep->Win;
  658.     RP *rp = win->RPort;
  659.  
  660.     mod = (ep->Modified) ? " (modified)" : "          ";
  661.     sprintf(ep->Wtitle, "%3ld/%-3ld %3ld %s%s  ", text_lineno(), text_lines(), text_colno()+1, text_name(), mod);
  662.     if (!text_imode())
  663.         strcat(ep->Wtitle, "Ovr ");
  664.     len = strlen(ep->Wtitle);
  665.     if (len < Columns && Columns < 128) {
  666.         setmem(ep->Wtitle+len, Columns - len + 1, ' ');
  667.         ep->Wtitle[Columns + 1] = 0;
  668.     }
  669.  
  670.     /*
  671.      *  Update title
  672.      */
  673.  
  674.     oldfont = win->RPort->Font;
  675.     SetFont(rp, win->WScreen->RastPort.Font);
  676.  
  677.     win->Title = ep->Wtitle;
  678.     SetAPen(rp, ep->BGPen);
  679.     SetBPen(rp, ep->FGPen);
  680.     Move(rp, 30, rp->Font->tf_Baseline+1);
  681.     maxlen = (win->Width-96)/rp->Font->tf_XSize;
  682.     if (maxlen < 0)
  683.         maxlen = 0;
  684.     if (len > maxlen)
  685.         len = Oldtlen = maxlen;
  686.     if (Oldtlen > maxlen)
  687.         Oldtlen = maxlen;
  688.     Text(rp, ep->Wtitle, len);      /*  No flash                    */
  689.     while (Oldtlen - len >= (int)sizeof(Space)) {
  690.         Text(rp, Space, sizeof(Space));
  691.         Oldtlen -= sizeof(Space);
  692.     }
  693.     if (Oldtlen - len > 0)
  694.         Text(rp, Space, Oldtlen - len);
  695.     Oldtlen = len;            /*  Oldtlen might have been <    */
  696.     SetAPen(rp, ep->FGPen);
  697.     SetBPen(rp, ep->BGPen);
  698.  
  699.     SetFont(rp, oldfont);
  700.     }
  701. }
  702.  
  703. void
  704. set_window_params()
  705. {
  706.     ED *ep = Ep;
  707.     WIN *win = ep->Win;
  708.     RP    *rp = win->RPort;
  709.  
  710.     Xsize = rp->Font->tf_XSize;
  711.     Ysize = rp->Font->tf_YSize;
  712.     Xbase = win->BorderLeft;
  713.     Ybase = win->BorderTop;
  714.     Xpixs   = win->Width - win->BorderRight - Xbase;
  715.     Ypixs   = win->Height- win->BorderBottom- Ybase;
  716.     Columns = Xpixs / Xsize;
  717.     Rows    = Ypixs / Ysize;
  718.     XTbase  =  Xbase;
  719.     YTbase  =  Ybase + rp->Font->tf_Baseline;
  720.     SetAPen(rp, ep->FGPen);
  721.     SetBPen(rp, ep->BGPen);
  722. }
  723.  
  724. void
  725. exiterr(str)
  726. char *str;
  727. {
  728.     if (Output()) {
  729.     Write(Output(),str,strlen(str));
  730.     Write(Output(),"\n",1);
  731.     }
  732.     exit(1);
  733. }
  734.  
  735.  
  736. /*
  737.  *  Check break by scanning pending messages in the I stream for a ^C.
  738.  *  Msgchk forces a check, else the check is only made if the signal is
  739.  *  set in the I stream (the signal is reset).
  740.  */
  741.  
  742. breakcheck()
  743. {
  744.     IMESS *im;
  745.     WIN *win = Ep->Win;
  746.     struct List *list = &win->UserPort->mp_MsgList;
  747.  
  748.     if (Msgchk || (SetSignal(0,0) & (1<<win->UserPort->mp_SigBit))) {
  749.     Msgchk = 0;
  750.     SetSignal(0,1<<win->UserPort->mp_SigBit);
  751.  
  752.     im = (IMESS *)list->lh_Head;
  753.     Forbid();
  754.     for (; im != (IMESS *)&list->lh_Tail; im = (IMESS *)im->ExecMessage.mn_Node.ln_Succ) {
  755.         if (im->Class == RAWKEY && (im->Qualifier & 0xFB) == 0x08 &&
  756.         im->Code == CtlC) {
  757.  
  758.         Permit();
  759.         SetSignal(SIGBREAKF_CTRL_C,SIGBREAKF_CTRL_C);
  760.         return(1);
  761.         }
  762.     }
  763.     Permit();
  764.     }
  765.     return(0);
  766. }
  767.  
  768. void
  769. breakreset()
  770. {
  771.     SetSignal(0, SIGBREAKF_CTRL_C);
  772. }
  773.  
  774. /*
  775.  *  resize cols rows
  776.  */
  777.  
  778. void
  779. do_resize()
  780. {
  781.     WIN *win = Ep->Win;
  782.     int cols = atoi(av[1]);
  783.     int rows = atoi(av[2]);
  784.     short width = (cols*win->RPort->Font->tf_XSize) + win->BorderLeft + win->BorderRight;
  785.     short height= (rows*win->RPort->Font->tf_YSize) + win->BorderTop + win->BorderBottom;
  786.  
  787.     if (width < 16 || height < 16 ||
  788.     width > win->WScreen->Width - win->LeftEdge ||
  789.     height > win->WScreen->Height - win->TopEdge) {
  790.     title ("window too big (try moving to upper left corner and retrying)");
  791.     return;
  792.     }
  793.     SizeWindow(win, width - win->Width, height - win->Height);
  794.     Delay(50*2);    /* wait 2 seconds */
  795. }
  796.  
  797. ops(av, iswb)
  798. char **av;
  799. {
  800.     short nonops;
  801.     short i;
  802.     long val;
  803.     char *str;
  804.  
  805.     for (i = nonops = 0; str = av[i]; ++i) {
  806.     if (iswb) {
  807.         if (strncmp(str, "ARG", 3) == 0) {
  808.         while (*str && *str != '-')
  809.             ++str;
  810.         }
  811.     }
  812.     if (*str == '-') {
  813.         val = atoi(str+2);
  814.         switch(str[1]) {
  815.         case 'f':
  816.         Ffile = str+2;
  817.         break;
  818.         case 'b':
  819.         /*SizeOveride = 1;*/
  820.         break;
  821.         case 't':
  822.         /*Nwtopedge = val;*/
  823.         break;
  824.         case 'l':
  825.         /*Nwleftedge= val;*/
  826.         break;
  827.         case 'w':
  828.         /*SizeOveride = 1;*/
  829.         /*Nwwidth   = val;*/
  830.         break;
  831.         case 'h':
  832.         /*SizeOveride = 1;*/
  833.         /*Nwheight  = val;*/
  834.         break;
  835.         }
  836.     } else {
  837.         ++nonops;
  838.     }
  839.     }
  840.     return((int)nonops);
  841. }
  842.  
  843. /*
  844.  *  Convert geometry to nw params.
  845.  */
  846.  
  847. char *
  848. geoskip(ptr, pval, psgn)
  849. char *ptr;
  850. int *pval;
  851. int *psgn;
  852. {
  853.     if (*ptr == '-')
  854.     *psgn = -1;
  855.     else
  856.     *psgn = 1;
  857.     if (*ptr == '-' || *ptr == '+')
  858.     ++ptr;
  859.     *pval = atoi(ptr);
  860.     while (*ptr >= '0' && *ptr <= '9')
  861.     ++ptr;
  862.     return(ptr);
  863. }
  864.  
  865. void
  866. GeometryToNW(geo, nw)
  867. char *geo;
  868. struct NewWindow *nw;
  869. {
  870.     int n;
  871.     int sign;
  872.     struct Screen scr;
  873.  
  874.     GetScreenData(&scr, sizeof(scr), WBENCHSCREEN, NULL);
  875.  
  876.     if (*geo) {
  877.     geo = geoskip(geo, &n, &sign);
  878.     if (sign > 0)
  879.         nw->LeftEdge = n;
  880.     else
  881.         nw->LeftEdge = scr.Width - n;
  882.     }
  883.     if (*geo) {
  884.     geo = geoskip(geo, &n, &sign);
  885.     if (sign > 0)
  886.         nw->TopEdge = n;
  887.     else
  888.         nw->TopEdge = scr.Height - n;
  889.     }
  890.     if (*geo) {
  891.     geo = geoskip(geo, &n, &sign);
  892.     if (sign > 0)
  893.         nw->Width = n;
  894.     else
  895.         nw->Width = scr.Width - nw->LeftEdge - n;
  896.     }
  897.     if (*geo) {
  898.     geo = geoskip(geo, &n, &sign);
  899.     if (sign > 0)
  900.         nw->Height = n;
  901.     else
  902.         nw->Height = scr.Height - nw->TopEdge - n;
  903.     }
  904. }
  905.  
  906.  
  907.