home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d8xx / d834 / pinfocom.lha / PInfoCom / pinfocom-3.0.lha / amiga_script.c < prev    next >
C/C++ Source or Header  |  1992-11-04  |  18KB  |  796 lines

  1. /* amiga_script.c
  2.  *
  3.  *  ``pinfocom'' -- a portable Infocom Inc. data file interpreter.
  4.  *  Copyright (C) 1987-1992  InfoTaskForce
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public License as published by
  8.  *  the Free Software Foundation; either version 2 of the License, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; see the file COPYING.  If not, write to the
  18.  *  Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. /*
  22.  * $Header$
  23.  */
  24.  
  25. #ifndef _AMIGA_GLOBAL_H
  26. #include "amiga_global.h"
  27. #endif    /* !_AMIGA_GLOBAL_H */
  28.  
  29.     /* ScriptPadCleanup():
  30.      *
  31.      *    Free auxilary buffers for transcript file
  32.      *    line padding.
  33.      */
  34.  
  35. VOID
  36. ScriptPadCleanup()
  37. {
  38.     if(ScriptPadLen)
  39.     {
  40.         FreeMem(ScriptPadLen,sizeof(int) * ScriptPadWidth / 2);
  41.  
  42.         ScriptPadLen = NULL;
  43.     }
  44.  
  45.     if(ScriptPadSkip)
  46.     {
  47.         FreeMem(ScriptPadSkip,sizeof(int) * ScriptPadWidth / 2);
  48.  
  49.         ScriptPadSkip = NULL;
  50.     }
  51.  
  52.     if(ScriptPadBuffer)
  53.     {
  54.         FreeMem(ScriptPadBuffer,ScriptPadWidth);
  55.  
  56.         ScriptPadBuffer = NULL;
  57.     }
  58.  
  59.     ScriptPadWidth = 0;
  60. }
  61.  
  62.     /* ScriptPadSetup(int Width):
  63.      *
  64.      *    Allocate auxilary buffers for transcript file
  65.      *    line padding.
  66.      */
  67.  
  68. Bool
  69. ScriptPadSetup(int Width)
  70. {
  71.         /* Free previously allocated buffers. */
  72.  
  73.     ScriptPadCleanup();
  74.  
  75.         /* Remember line width. */
  76.  
  77.     ScriptPadWidth = Width;
  78.  
  79.         /* Allocate the buffers. */
  80.  
  81.     if(ScriptPadLen = (int *)AllocMem(sizeof(int) * ScriptPadWidth / 2,MEMF_ANY))
  82.     {
  83.         if(ScriptPadSkip = (int *)AllocMem(sizeof(int) * ScriptPadWidth / 2,MEMF_ANY))
  84.         {
  85.             if(ScriptPadBuffer = (char *)AllocMem(ScriptPadWidth,MEMF_ANY))
  86.                 return(TRUE);
  87.         }
  88.     }
  89.  
  90.         /* No success, clean up first. */
  91.  
  92.     ScriptPadCleanup();
  93.  
  94.         /* Return failure. */
  95.  
  96.     return(FALSE);
  97. }
  98.  
  99.     /* ScriptPad(const char *Line,int Len):
  100.      *
  101.      *    Pad a line to be written to the transcript file.
  102.      */
  103.  
  104. Bool
  105. ScriptPad(const char *Line,int Len)
  106. {
  107.     int    Index        = 0,
  108.         Start        = 0,
  109.         Size        = 0,
  110.         MaxWidth    = ScriptPadWidth,
  111.         i        = 0;
  112.  
  113.         /* Include leading spaces. */
  114.  
  115.     while(i < Len && Line[i] == ' ')
  116.     {
  117.         Size++;
  118.  
  119.         i++;
  120.     }
  121.  
  122.         /* Run down the line... */
  123.  
  124.     while(i < Len)
  125.     {
  126.             /* Count the characters. */
  127.  
  128.         while(i < Len && Line[i] != ' ')
  129.         {
  130.             Size++;
  131.  
  132.             i++;
  133.         }
  134.  
  135.             /* Remember word position and length. */
  136.  
  137.         ScriptPadLen[Index]    = Size;
  138.         ScriptPadSkip[Index]    = Start;
  139.  
  140.             /* Skip the spaces in between. */
  141.  
  142.         while(i < Len && Line[i] == ' ')
  143.             i++;
  144.  
  145.             /* Reduce remaining space. */
  146.     
  147.         MaxWidth -= Size;
  148.  
  149.             /* Proceed to the next word. */
  150.  
  151.         Index++;
  152.  
  153.         Start = i;
  154.  
  155.         Size = 0;
  156.     }
  157.  
  158.         /* Any text to output? */
  159.  
  160.     if(Index > 1)
  161.     {
  162.         int Count = Index - 1,j,k;
  163.  
  164.         i = j = 0;
  165.  
  166.             /* Process all the words. */
  167.  
  168.         do
  169.         {
  170.                 /* Produce the spaces in between. */
  171.  
  172.             if(i)
  173.             {
  174.                 k = (MaxWidth * i) / Count - (MaxWidth * (i - 1)) / Count;
  175.  
  176.                 while(k--)
  177.                     ScriptPadBuffer[j++] = ' ';
  178.             }
  179.  
  180.                 /* Transfer the characters. */
  181.  
  182.             for(k = 0 ; k < ScriptPadLen[i] ; k++)
  183.                 ScriptPadBuffer[j++] = Line[ScriptPadSkip[i] + k];
  184.  
  185.                 /* Proceed to the next word. */
  186.  
  187.             i++;
  188.         }
  189.         while(--Index);
  190.  
  191.             /* Write the line. */
  192.  
  193.         return(ScriptWrite(ScriptPadBuffer,ScriptPadWidth));
  194.     }
  195.     else
  196.         return(ScriptWrite(Line,Len));
  197. }
  198.  
  199.     /* ScriptWrite(const char *Buffer,int Width):
  200.      *
  201.      *    Write a string to the transcript file,
  202.      *    adding a new-line character.
  203.      */
  204.  
  205. Bool
  206. ScriptWrite(const char *Buffer,const int Width)
  207. {
  208.         /* Are we still to continue scripting? */
  209.  
  210.     if(ScriptAborted)
  211.         return(FALSE);
  212.     else
  213.     {
  214.         if(Width)
  215.         {
  216.                 /* Flush the current line buffer contents to the transcript
  217.                  * file, close it if the action fails.
  218.                  */
  219.  
  220.             if(!fwrite(Buffer,Width,1,ScriptFile))
  221.             {
  222.                 scr_putmesg("Error writing to transcript file",TRUE);
  223.  
  224.                 ScriptAborted = TRUE;
  225.  
  226.                 return(FALSE);
  227.             }
  228.         }
  229.  
  230.         if(!fwrite("\n",1,1,ScriptFile))
  231.         {
  232.             scr_putmesg("Error writing to transcript file",TRUE);
  233.  
  234.             ScriptAborted = TRUE;
  235.  
  236.             return(FALSE);
  237.         }
  238.  
  239.         return(TRUE);
  240.     }
  241. }
  242.  
  243.     /* ScriptCreateGadgets():
  244.      *
  245.      *    Create the gadgets for the printer control panel.
  246.      */
  247.  
  248. struct Gadget *
  249. ScriptCreateGadgets(struct Gadget **GadgetArray,struct Gadget **GadgetList,const APTR VisualInfo,UWORD *Width,UWORD *Height,const struct Screen *Screen)
  250. {
  251.         /* The gadget title labels. */
  252.  
  253.     STATIC STRPTR GadgetLabels[] =
  254.     {
  255.         "Transcript output file or device",
  256.         "Transcript page width",
  257.         "Save",
  258.         "Cancel",
  259.         "Select a file..."
  260.     };
  261.  
  262.         /* A bunch of local variables. */
  263.  
  264.     struct Gadget        *Gadget;
  265.     struct NewGadget     NewGadget;
  266.     WORD             Counter = 0;
  267.  
  268.         /* Zero the template. */
  269.  
  270.     memset(&NewGadget,0,sizeof(struct NewGadget));
  271.  
  272.         /* Create the root info. */
  273.  
  274.     if(Gadget = CreateContext(GadgetList))
  275.     {
  276.         WORD    ButtonWidth,
  277.             StringWidth,
  278.             MaxWidth,
  279.             Temp,
  280.             i;
  281.  
  282.             /* Determine the longest string/integer gadget label. */
  283.  
  284.         MaxWidth = 0;
  285.  
  286.         for(i = SCRIPTGAD_PRINTER_STRING ; i <= SCRIPTGAD_PRINTER_WIDTH ; i++)
  287.         {
  288.             if((Temp = 2 * INTERWIDTH + TextLength((struct RastPort *)&Screen -> RastPort,GadgetLabels[i],strlen(GadgetLabels[i]))) > MaxWidth)
  289.                 MaxWidth = Temp;
  290.         }
  291.  
  292.             /* Store the longest label width. */
  293.  
  294.         StringWidth = MaxWidth;
  295.  
  296.             /* Determine the longest button gadget label. */
  297.  
  298.         MaxWidth = 0;
  299.  
  300.         for(i = SCRIPTGAD_PRINTER_ACCEPT ; i <= SCRIPTGAD_PRINTER_SELECT ; i++)
  301.         {
  302.             if((Temp = 2 * INTERWIDTH + TextLength((struct RastPort *)&Screen -> RastPort,GadgetLabels[i],strlen(GadgetLabels[i]))) > MaxWidth)
  303.                 MaxWidth = Temp;
  304.         }
  305.  
  306.             /* Store the longest label width. */
  307.  
  308.         ButtonWidth = MaxWidth;
  309.  
  310.             /* Are the three buttons in a row longer
  311.              * than the string/integer gadget? If so,
  312.              * adjust the string/integer gadget width.
  313.              */
  314.  
  315.         if(3 * ButtonWidth + 2 * INTERWIDTH > StringWidth)
  316.             StringWidth = 3 * ButtonWidth + 2 * INTERWIDTH;
  317.  
  318.             /* Determine window size. */
  319.  
  320.         *Width    = Screen -> WBorLeft + INTERWIDTH + StringWidth + INTERWIDTH + Screen -> WBorRight;
  321.         *Height    = Screen -> WBorTop + Screen -> Font -> ta_YSize + 1 + 2 * (2 * INTERHEIGHT + Screen -> Font -> ta_YSize) + 3 * (Screen -> Font -> ta_YSize + 6) + 2 * INTERHEIGHT + Screen -> WBorBottom;
  322.  
  323.             /* Set up for gadget creation. */
  324.  
  325.         NewGadget . ng_GadgetText    = GadgetLabels[SCRIPTGAD_PRINTER_STRING];
  326.         NewGadget . ng_TextAttr        = Screen -> Font;
  327.         NewGadget . ng_VisualInfo    = VisualInfo;
  328.         NewGadget . ng_GadgetID        = Counter;
  329.         NewGadget . ng_Flags        = PLACETEXT_ABOVE;
  330.         NewGadget . ng_LeftEdge        = Screen -> WBorLeft + INTERWIDTH;
  331.         NewGadget . ng_TopEdge        = Screen -> WBorTop + Screen -> Font -> ta_YSize + 1 + 2 * INTERHEIGHT + Screen -> Font -> ta_YSize;
  332.         NewGadget . ng_Width        = StringWidth;
  333.         NewGadget . ng_Height        = Screen -> Font -> ta_YSize + 6;
  334.  
  335.         GadgetArray[Counter++] = Gadget = CreateGadget(STRING_KIND,Gadget,&NewGadget,
  336.             GTST_MaxChars,255,
  337.         TAG_DONE);
  338.  
  339.         NewGadget . ng_GadgetText    = GadgetLabels[SCRIPTGAD_PRINTER_WIDTH];
  340.         NewGadget . ng_GadgetID        = Counter;
  341.         NewGadget . ng_TopEdge        = NewGadget . ng_TopEdge + NewGadget . ng_Height + 2 * INTERHEIGHT + Screen -> Font -> ta_YSize;
  342.  
  343.         GadgetArray[Counter++] = Gadget = CreateGadget(INTEGER_KIND,Gadget,&NewGadget,TAG_DONE);
  344.  
  345.         NewGadget . ng_GadgetText    = GadgetLabels[SCRIPTGAD_PRINTER_ACCEPT];
  346.         NewGadget . ng_GadgetID        = Counter;
  347.         NewGadget . ng_Flags        = NULL;
  348.         NewGadget . ng_TopEdge        = NewGadget . ng_TopEdge + NewGadget . ng_Height + INTERHEIGHT;
  349.         NewGadget . ng_Width        = ButtonWidth;
  350.  
  351.         GadgetArray[Counter++] = Gadget = CreateGadget(BUTTON_KIND,Gadget,&NewGadget,TAG_DONE);
  352.  
  353.         NewGadget . ng_GadgetText    = GadgetLabels[SCRIPTGAD_PRINTER_CANCEL];
  354.         NewGadget . ng_GadgetID        = Counter;
  355.         NewGadget . ng_LeftEdge        = (*Width) - (Screen -> WBorLeft + INTERWIDTH + NewGadget . ng_Width);
  356.  
  357.         GadgetArray[Counter++] = Gadget = CreateGadget(BUTTON_KIND,Gadget,&NewGadget,TAG_DONE);
  358.  
  359.         NewGadget . ng_GadgetText    = GadgetLabels[SCRIPTGAD_PRINTER_SELECT];
  360.         NewGadget . ng_GadgetID        = Counter;
  361.         NewGadget . ng_LeftEdge        = ((*Width) - NewGadget . ng_Width) / 2;
  362.  
  363.         GadgetArray[Counter] = Gadget = CreateGadget(BUTTON_KIND,Gadget,&NewGadget,TAG_DONE);
  364.     }
  365.  
  366.     return(Gadget);
  367. }
  368.  
  369.     /* ScriptGetPrinterName(STRPTR PrinterName,int *PrinterWidth):
  370.      *
  371.      *    Open the printer control panel.
  372.      */
  373.  
  374. Bool
  375. ScriptGetPrinterName(STRPTR PrinterName,int *PrinterWidth)
  376. {
  377.     STATIC char     FileName[MAX_FILENAME_LENGTH];
  378.  
  379.     struct Gadget    *GadgetArray[SCRIPTGAD_PRINTER_SELECT + 1],
  380.             *GadgetList;
  381.     struct Window    *PrinterWindow;
  382.     UWORD         Width,
  383.              Height;
  384.     char        *Index;
  385.     Bool         Result = FALSE;
  386.  
  387.         /* Get the path part of the file name,
  388.          * check if ends in a null-character,
  389.          * if so, it's probably just a device name.
  390.          */
  391.  
  392.     if(Index = PathPart(PrinterName))
  393.     {
  394.             /* If it has a file name attached, copy it to the filename buffer. */
  395.  
  396.         if(*Index)
  397.             strcpy(FileName,PrinterName);
  398.         else
  399.         {
  400.             int    ExtensionLen,
  401.                 NameLen,
  402.                 i;
  403.             Bool    GotIt = FALSE;
  404.  
  405.                 /* Use the story file name to start. */
  406.  
  407.             strcpy(FileName,gflags . filenm);
  408.  
  409.                 /* Determine name length. */
  410.  
  411.             NameLen = strlen(FileName);
  412.  
  413.                 /* Try to find a matching game
  414.                  * file name extension.
  415.                  */
  416.  
  417.             for(i = 0 ; !GotIt && StoryExtensions[i] ; i++)
  418.             {
  419.                     /* Is the game file name long enough
  420.                      * to hold an extension?
  421.                      */
  422.  
  423.                 if((ExtensionLen = strlen(StoryExtensions[i])) > NameLen)
  424.                 {
  425.                         /* Does the extension match? */
  426.  
  427.                     if(!Stricmp(&FileName[NameLen - ExtensionLen],StoryExtensions[i]))
  428.                     {
  429.                             /* Add new file name extension. */
  430.  
  431.                         strcpy(&FileName[NameLen - ExtensionLen],SCRIPT_EXT);
  432.  
  433.                             /* We're done now. */
  434.  
  435.                         GotIt = TRUE;
  436.                     }
  437.                 }
  438.             }
  439.  
  440.                 /* If we didn't succeed in adding
  441.                  * a new file name extension, just
  442.                  * attach the default.
  443.                  */
  444.  
  445.             if(!GotIt)
  446.             {
  447.                     /* Strip any existing extension. */
  448.  
  449.                 for(i = NameLen - 1 ; i >= 0 ; i--)
  450.                 {
  451.                     if(FileName[i] == '.')
  452.                     {
  453.                         FileName[i] = 0;
  454.  
  455.                         break;
  456.                     }
  457.                 }
  458.  
  459.                     /* Attach the default script
  460.                      * file name extension.
  461.                      */
  462.  
  463.                 strcat(FileName,SCRIPT_EXT);
  464.             }
  465.         }
  466.     }
  467.  
  468.         /* Create the gadgets for the window. */
  469.  
  470.     if(ScriptCreateGadgets(&GadgetArray[0],&GadgetList,VisualInfo,&Width,&Height,Window -> WScreen))
  471.     {
  472.             /* Open the window. */
  473.  
  474.         if(PrinterWindow = OpenWindowTags(NULL,
  475.             WA_Top,            Window -> TopEdge + (Window -> Height - Height) / 2,
  476.             WA_Left,        Window -> LeftEdge + (Window -> Width - Width) / 2,
  477.             WA_Title,        "Save transcript file",
  478.             WA_Width,        Width,
  479.             WA_Height,        Height,
  480.             WA_IDCMP,        IDCMP_VANILLAKEY | IDCMP_CLOSEWINDOW | STRINGIDCMP | BUTTONIDCMP,
  481.             WA_Activate,        TRUE,
  482.             WA_CloseGadget,        TRUE,
  483.             WA_DragBar,        TRUE,
  484.             WA_DepthGadget,        TRUE,
  485.             WA_RMBTrap,        TRUE,
  486.             WA_CustomScreen,    Window -> WScreen,
  487.         TAG_DONE))
  488.         {
  489.             struct IntuiMessage    *Massage;
  490.             ULONG             Class;
  491.             struct Gadget        *Gadget;
  492.  
  493.             Bool             Terminated = FALSE;
  494.             STRPTR             Buffer;
  495.  
  496.                 /* Add the gadgets and render them. */
  497.  
  498.             AddGList(PrinterWindow,GadgetList,(UWORD)-1,(UWORD)-1,NULL);
  499.             RefreshGList(GadgetList,PrinterWindow,NULL,(UWORD)-1);
  500.             GT_RefreshWindow(PrinterWindow,NULL);
  501.  
  502.                 /* Set the string gadget (output file name). */
  503.  
  504.             GT_SetGadgetAttrs(GadgetArray[SCRIPTGAD_PRINTER_STRING],PrinterWindow,NULL,
  505.                 GTST_String,    PrinterName,
  506.             TAG_DONE);
  507.  
  508.                 /* Set the integer gadget (output file width). */
  509.  
  510.             GT_SetGadgetAttrs(GadgetArray[SCRIPTGAD_PRINTER_WIDTH],PrinterWindow,NULL,
  511.                 GTIN_Number,    *PrinterWidth,
  512.             TAG_DONE);
  513.  
  514.                 /* Activate the file/device name gadget. */
  515.  
  516.             ActivateGadget(GadgetArray[SCRIPTGAD_PRINTER_STRING],PrinterWindow,NULL);
  517.  
  518.                 /* A handy shortcut. */
  519.  
  520.             Buffer = GT_STRING(GadgetArray[SCRIPTGAD_PRINTER_STRING]);
  521.  
  522.                 /* Enter input loop. */
  523.  
  524.             do
  525.             {
  526.                     /* Wait for input. */
  527.  
  528.                 WaitPort(PrinterWindow -> UserPort);
  529.  
  530.                     /* Process all incoming messages. */
  531.  
  532.                 while(Massage = GT_GetIMsg(PrinterWindow -> UserPort))
  533.                 {
  534.                         /* Remember input class and gadget. */
  535.  
  536.                     Class    = Massage -> Class;
  537.                     Gadget    = (struct Gadget *)Massage -> IAddress;
  538.  
  539.                         /* Return the input event message. */
  540.  
  541.                     GT_ReplyIMsg(Massage);
  542.  
  543.                         /* Which kind of message did we receive? */
  544.  
  545.                     switch(Class)
  546.                     {
  547.                             /* Activate a string gadget? */
  548.  
  549.                         case IDCMP_VANILLAKEY:        ActivateGadget(GadgetArray[SCRIPTGAD_PRINTER_STRING],PrinterWindow,NULL);
  550.                                         break;
  551.  
  552.                             /* Close the window? */
  553.  
  554.                         case IDCMP_CLOSEWINDOW:        Terminated = TRUE;
  555.                                         break;
  556.  
  557.                             /* A button has been pressed? */
  558.  
  559.                         case IDCMP_GADGETUP:        switch(Gadget -> GadgetID)
  560.                                         {
  561.                                                 /* Activate the printer width gadget? */
  562.  
  563.                                             case SCRIPTGAD_PRINTER_STRING:ActivateGadget(GadgetArray[SCRIPTGAD_PRINTER_WIDTH],PrinterWindow,NULL);
  564.                                                         break;
  565.  
  566.                                                 /* Accept current settings. */
  567.  
  568.                                             case SCRIPTGAD_PRINTER_ACCEPT:if(Buffer[0])
  569.                                                         {
  570.                                                                 /* Copy the new printer output file/device name. */
  571.  
  572.                                                             strcpy(PrinterName,(char *)Buffer);
  573.  
  574.                                                                 /* Quit the loop the next time. */
  575.  
  576.                                                             Terminated = Result = TRUE;
  577.  
  578.                                                                 /* Get current printer width, don't make it too small. */
  579.  
  580.                                                             if((*PrinterWidth = GT_INTEGER(GadgetArray[SCRIPTGAD_PRINTER_WIDTH])) < MIN_PRINTER_COLUMNS)
  581.                                                                 *PrinterWidth = MIN_PRINTER_COLUMNS;
  582.                                                         }
  583.  
  584.                                                         break;
  585.  
  586.                                                 /* Select a new output file. */
  587.  
  588.                                             case SCRIPTGAD_PRINTER_SELECT:
  589.  
  590.                                                             /* Block window input. */
  591.  
  592.                                                         ConBlockWindow(PrinterWindow);
  593.  
  594.                                                         strcpy(TempBuffer,FileName);
  595.  
  596.                                                             /* Extract the path name. */
  597.  
  598.                                                         if(Index = PathPart(TempBuffer))
  599.                                                             *Index = 0;
  600.  
  601.                                                             /* If no path name is given, supply the
  602.                                                              * current directory.
  603.                                                              */
  604.  
  605.                                                         if(!TempBuffer[0])
  606.                                                         {
  607.                                                                 /* Try to obtain the current directory name.
  608.                                                                  * If this fails, leave the path name empty.
  609.                                                                  */
  610.  
  611.                                                             if(!GetCurrentDirName(TempBuffer,MAX_FILENAME_LENGTH))
  612.                                                                 TempBuffer[0] = 0;
  613.                                                         }
  614.  
  615.                                                             /* Request the file name. */
  616.  
  617.                                                         if(AslRequestTags(GameFileRequest,
  618.                                                             ASL_Window,    PrinterWindow,
  619.                                                             ASL_Dir,    TempBuffer,
  620.                                                             ASL_File,    FilePart(FileName),
  621.                                                             ASL_FuncFlags,    FILF_SAVE,
  622.                                                             ASL_Hail,    "Select transcript file to save",
  623.                                                             ASL_Pattern,    "~(#?.info)",
  624.                                                         TAG_DONE))
  625.                                                         {
  626.                                                                 /* Did we get a file name? */
  627.  
  628.                                                             if(GameFileRequest -> rf_File[0])
  629.                                                             {
  630.                                                                     /* Copy the drawer name. */
  631.  
  632.                                                                 strcpy(TempBuffer,(char *)GameFileRequest -> rf_Dir);
  633.  
  634.                                                                     /* Add the file name. */
  635.  
  636.                                                                 if(AddPart(TempBuffer,GameFileRequest -> rf_File,MAX_FILENAME_LENGTH))
  637.                                                                 {
  638.                                                                         /* Update the file name string. */
  639.  
  640.                                                                     GT_SetGadgetAttrs(GadgetArray[SCRIPTGAD_PRINTER_STRING],PrinterWindow,NULL,
  641.                                                                         GTST_String,TempBuffer,
  642.                                                                     TAG_DONE);
  643.  
  644.                                                                         /* Remember the file name. */
  645.  
  646.                                                                     strcpy(FileName,TempBuffer);
  647.                                                                 }
  648.                                                             }
  649.                                                         }
  650.  
  651.                                                         ConUnblockWindow(PrinterWindow);
  652.  
  653.                                                         break;
  654.  
  655.                                                 /* Cancel the requester. */
  656.  
  657.                                             case SCRIPTGAD_PRINTER_CANCEL:Terminated = TRUE;
  658.                                                         break;
  659.                                         }
  660.  
  661.                                         break;
  662.                     }
  663.  
  664.                     if(Terminated)
  665.                         break;
  666.                 }
  667.             }
  668.             while(!Terminated);
  669.  
  670.                 /* Close the printer control window. */
  671.  
  672.             CloseWindow(PrinterWindow);
  673.         }
  674.  
  675.             /* Free the gadgets. */
  676.  
  677.         FreeGadgets(GadgetList);
  678.     }
  679.  
  680.     return(Result);
  681. }
  682.  
  683.     /* ScriptSplitLine(char *Line,int Len,const Bool ReturnPrompt):
  684.      *
  685.      *    Split a hunk of text into neat little pieces.
  686.      */
  687.  
  688. char *
  689. ScriptSplitLine(char *Line,int Len,const Bool ReturnPrompt)
  690. {
  691.     int     Count,
  692.          Space;
  693.  
  694.         /* Process & chop the text. */
  695.  
  696.     do
  697.     {
  698.             /* Does the entire line fit? */
  699.  
  700.         if(Len <= ScriptWidth)
  701.         {
  702.             if(ReturnPrompt)
  703.                 return(Line);
  704.             else
  705.                 ScriptWrite(Line,Count = Len);
  706.         }
  707.         else
  708.         {
  709.                 /* Reset the counters. */
  710.  
  711.             Space = Count = 0;
  712.  
  713.                 /* Determine number of
  714.                  * characters to fit and
  715.                  * the last space to use
  716.                  * for text-wrapping.
  717.                  */
  718.  
  719.             while(Count < Len && Count + 1 < ScriptWidth)
  720.             {
  721.                 Count++;
  722.  
  723.                     /* Remember last space. */
  724.  
  725.                 if(Line[Count] == ' ')
  726.                     Space = Count;
  727.             }
  728.  
  729.                 /* Return remainder. */
  730.  
  731.             if(Count == Len)
  732.             {
  733.                 if(ReturnPrompt)
  734.                     return(Line);
  735.                 else
  736.                 {
  737.                     if(Line[Count - 1] == ' ')
  738.                         ScriptWrite(Line,Count - 1);
  739.                     else
  740.                         ScriptWrite(Line,Count);
  741.                 }
  742.             }
  743.             else
  744.             {
  745.                     /* Are we to pad lines? */
  746.  
  747.                 if(FillLines && ScriptPadWidth)
  748.                 {
  749.                     if(Line[Count - 1] == ' ')
  750.                     {
  751.                         if(!ScriptPad(Line,Count - 1))
  752.                             break;
  753.                     }
  754.                     else
  755.                     {
  756.                         if(!ScriptPad(Line,Space))
  757.                             break;
  758.                         else
  759.                             Count = Space + 1;
  760.                     }
  761.                 }
  762.                 else
  763.                 {
  764.                         /* Write the line. */
  765.  
  766.                     if(Line[Count - 1] == ' ')
  767.                     {
  768.                         if(!ScriptWrite(Line,Count - 1))
  769.                             break;
  770.                     }
  771.                     else
  772.                     {
  773.                         if(!ScriptWrite(Line,Space))
  774.                             break;
  775.                         else
  776.                             Count = Space + 1;
  777.                     }
  778.                 }
  779.             }
  780.  
  781.                 /* Move up. */
  782.  
  783.             Line += Count;
  784.         }
  785.  
  786.             /* Reduce remaining length. */
  787.  
  788.         Len -= Count;
  789.     }
  790.     while(Len > 0);
  791.  
  792.         /* Return blank line. */
  793.  
  794.     return("");
  795. }
  796.