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