home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d534 / term.lha / Term / Source.LZH / UpdateConfig.c < prev    next >
C/C++ Source or Header  |  1991-07-06  |  10KB  |  476 lines

  1. /* $Revision Header * Header built automatically - do not edit! *************
  2.  *
  3.  *    (C) Copyright 1990 by Olaf 'Olsen' Barthel & MXM
  4.  *
  5.  *    Name .....: UpdateConfig.c
  6.  *    Created ..: Saturday 23-Mar-91 22:07
  7.  *    Revision .: 0
  8.  *
  9.  *    Date            Author          Comment
  10.  *    =========       ========        ====================
  11.  *    23-Mar-91       Olsen           Created this file!
  12.  *
  13.  * $Revision Header ********************************************************/
  14.  
  15. struct TermInfo
  16. {
  17.     UWORD    Version;
  18.     UWORD    Revision;
  19. };
  20.  
  21. extern LONG TermVersion,TermRevision;
  22. extern UBYTE TermName[],TermDate[],TermString[];
  23.  
  24. struct Configuration
  25. {
  26.     /* Serial Preferences. */
  27.  
  28.     ULONG    BaudRate;
  29.     BYTE    BitsPerChar;
  30.     BYTE    Parity;
  31.     BYTE    StopBits;
  32.     BYTE    Handshaking;
  33.     BYTE    Duplex;
  34.     BYTE    HighSpeed;
  35.     ULONG    BreakLength;
  36.     UBYTE    SerialDevice[40];
  37.     LONG    UnitNumber;
  38.  
  39.     /* Modem Preferences. */
  40.  
  41.     UBYTE    ModemInit[80];
  42.     UBYTE     ModemExit[80];
  43.     UBYTE    DialPrefix[80];
  44.     LONG    RedialDelay;
  45.     LONG    DialRetries;
  46.     LONG    DialTimeout;
  47.     BYTE    ConnectAutoBaud;
  48.     UBYTE    NoCarrier[16];
  49.     UBYTE    Connect[16];
  50.     UBYTE    Error[16];
  51.     UBYTE    Voice[16];
  52.     UBYTE    Ring[16];
  53.     UBYTE    Busy[16];
  54.     UBYTE    Okay[16];
  55.  
  56.     /* Transfer Preferences. */
  57.  
  58.     UBYTE    Protocol[40];
  59.  
  60.     /* Startup macro. */
  61.  
  62.     UBYTE    StartupMacro[256];
  63.  
  64.     /* Macro Preferences. */
  65.  
  66.     UBYTE    MacroFile[256];
  67.  
  68.     /* Screen Preferences. */
  69.  
  70.     ULONG    DisplayMode;
  71.     UWORD    Colours[16];
  72.     BYTE    MakeScreenPublic;
  73.     BYTE    ShanghaiWindows;
  74.  
  75.     /* Terminal Preferences. */
  76.  
  77.     BYTE    CaptureFilter;
  78.     BYTE    DestructiveBackspace;
  79.     BYTE    AudibleBell;
  80.     BYTE    VisibleBell;
  81.     BYTE    EightyColumns;
  82.     BYTE    SendCR;
  83.     BYTE    SendLF;
  84.     BYTE    ColourMode;
  85.     BYTE    Emulation;
  86.     BYTE    Font;
  87. };
  88.  
  89.     /* A phone book entry. */
  90.  
  91. struct PhoneEntry
  92. {
  93.     UBYTE            Name[40];
  94.     UBYTE            Number[40];
  95.     UBYTE            Password[40];
  96.  
  97.     LONG            PayPerUnit;
  98.     LONG            MinPerUnit;
  99.  
  100.     struct Configuration    Config;
  101. };
  102.  
  103. struct NewConfiguration
  104. {
  105.     /* Serial Preferences. */
  106.  
  107.     ULONG    BaudRate;
  108.     BYTE    BitsPerChar;
  109.     BYTE    Parity;
  110.     BYTE    StopBits;
  111.     BYTE    Handshaking;
  112.     BYTE    Duplex;
  113.     BYTE    HighSpeed;
  114.     ULONG    BreakLength;
  115.     UBYTE    SerialDevice[40];
  116.     LONG    UnitNumber;
  117.  
  118.     /* Modem Preferences. */
  119.  
  120.     UBYTE    ModemInit[80];
  121.     UBYTE     ModemExit[80];
  122.     UBYTE     ModemHangup[80];
  123.     UBYTE    DialPrefix[80];
  124.     LONG    RedialDelay;
  125.     LONG    DialRetries;
  126.     LONG    DialTimeout;
  127.     BYTE    ConnectAutoCapture;
  128.     BYTE    ConnectAutoBaud;
  129.     BYTE    LogActions;
  130.     UBYTE    NoCarrier[16];
  131.     UBYTE    Connect[16];
  132.     UBYTE    Voice[16];
  133.     UBYTE    Ring[16];
  134.     UBYTE    Busy[16];
  135.  
  136.     /* Transfer Preferences. */
  137.  
  138.     UBYTE    Protocol[40];
  139.  
  140.     /* Startup macro. */
  141.  
  142.     UBYTE    StartupMacro[256];
  143.  
  144.     /* Macro Preferences. */
  145.  
  146.     UBYTE    MacroFile[256];
  147.  
  148.     /* Screen Preferences. */
  149.  
  150.     ULONG    DisplayMode;
  151.     UWORD    Colours[16];
  152.     BYTE    MakeScreenPublic;
  153.     BYTE    ShanghaiWindows;
  154.  
  155.     /* Terminal Preferences. */
  156.  
  157.     BYTE    CaptureFilter;
  158.     BYTE    DestructiveBackspace;
  159.     BYTE    AudibleBell;
  160.     BYTE    VisibleBell;
  161.     BYTE    EightyColumns;
  162.     BYTE    DisableBlinking;
  163.     BYTE    SendCR;
  164.     BYTE    SendLF;
  165.     BYTE    ColourMode;
  166.     BYTE    Emulation;
  167.     BYTE    Font;
  168.  
  169.     /* Emulation control. */
  170.  
  171.     BYTE    CursorApp;
  172.     BYTE    FontScale;
  173.     BYTE    JumpScroll;
  174.     BYTE    AutoWrap;
  175.     BYTE    CursorWrap;
  176.     BYTE    NewLine;
  177.     BYTE    InsertChar;
  178.     BYTE    NumApp;
  179.  
  180.     /* Path Preferences. */
  181.  
  182.     UBYTE    DefaultStorage[256];
  183.  
  184.     UBYTE    TextUploadPath[256];
  185.     UBYTE    TextDownloadPath[256];
  186.  
  187.     UBYTE    ASCIIUploadPath[256];
  188.     UBYTE    ASCIIDownloadPath[256];
  189.  
  190.     UBYTE    BinaryUploadPath[256];
  191.     UBYTE    BinaryDownloadPath[256];
  192.  
  193.     UBYTE    CapturePath[256];
  194.     UBYTE    LogFile[256];
  195.     UBYTE    Editor[256];
  196.     UBYTE    BeepSound[256];
  197. };
  198.  
  199. LONG __saveds            Main(VOID);
  200. BYTE                WriteIFFData(UBYTE *Name,APTR Data,LONG Size,ULONG Type);
  201. BYTE                ReadIFFData(UBYTE *Name,APTR Data,LONG Size,ULONG Type);
  202. struct NewConfiguration *    Convert(struct Configuration *Src);
  203.  
  204. struct ExecBase        *SysBase;
  205. struct DosLibrary    *DOSBase;
  206. struct Library        *IFFParseBase;
  207.  
  208. LONG __saveds
  209. Main()
  210. {
  211.     SysBase = *(struct ExecBase **)4;
  212.  
  213.     if(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library",36))
  214.     {
  215.         if(IFFParseBase = (struct Library *)OpenLibrary("iffparse.library",0))
  216.         {
  217.             UBYTE **ArgArray;
  218.  
  219.             if(ArgArray = (UBYTE **)AllocVec(sizeof(UBYTE *),MEMF_PUBLIC | MEMF_CLEAR))
  220.             {
  221.                 struct RDArgs *ArgsPtr;
  222.  
  223.                 if(ArgsPtr = (struct RDArgs *)ReadArgs("File",(LONG *)ArgArray,NULL))
  224.                 {
  225.                     if(ArgArray[0])
  226.                     {
  227.                         struct Configuration Config;
  228.  
  229.                         if(ReadIFFData(ArgArray[0],&Config,sizeof(struct Configuration),'PREF'))
  230.                         {
  231.                             if(!WriteIFFData(ArgArray[0],Convert(&Config),sizeof(struct NewConfiguration),'PREF'))
  232.                                 Printf("\33[1mUpdateConfig:\33[0m Couldn't save file \"%s\"!\a\n",ArgArray[0]);
  233.                         }
  234.                         else
  235.                             Printf("\33[1mUpdateConfig:\33[0m Couldn't load file \"%s\"!\a\n",ArgArray[0]);
  236.                     }
  237.  
  238.                     FreeArgs(ArgsPtr);
  239.                 }
  240.  
  241.                 FreeVec(ArgArray);
  242.             }
  243.  
  244.             CloseLibrary(IFFParseBase);
  245.         }
  246.  
  247.         CloseLibrary(DOSBase);
  248.     }
  249.  
  250.     return(RETURN_OK);
  251. }
  252.  
  253.     /* WriteIFFData(UBYTE *Name,APTR Data,LONG Size,ULONG Type):
  254.      *
  255.      *    Write data to an IFF file (via iffparse.library).
  256.      */
  257.  
  258. BYTE
  259. WriteIFFData(UBYTE *Name,APTR Data,LONG Size,ULONG Type)
  260. {
  261.     struct IFFHandle    *Handle;
  262.     BYTE             Success = FALSE;
  263.  
  264.         /* Allocate a handle. */
  265.  
  266.     if(Handle = AllocIFF())
  267.     {
  268.             /* Open an output stream. */
  269.  
  270.         if(Handle -> iff_Stream = Open(Name,MODE_NEWFILE))
  271.         {
  272.                 /* Tell iffparse that this is
  273.                  * a DOS handle.
  274.                  */
  275.  
  276.             InitIFFasDOS(Handle);
  277.  
  278.                 /* Open the handle for writing. */
  279.  
  280.             if(!OpenIFF(Handle,IFFF_WRITE))
  281.             {
  282.                     /* Push outmost chunk onto stack. */
  283.  
  284.                 if(!PushChunk(Handle,'TERM','FORM',IFFSIZE_UNKNOWN))
  285.                 {
  286.                         /* Add a version identifier. */
  287.  
  288.                     if(!PushChunk(Handle,0,'VERS',IFFSIZE_UNKNOWN))
  289.                     {
  290.                         struct TermInfo TermInfo;
  291.  
  292.                         TermInfo . Version    = TermVersion;
  293.                         TermInfo . Revision    = TermRevision;
  294.  
  295.                             /* Write the version data. */
  296.  
  297.                         if(WriteChunkBytes(Handle,&TermInfo,sizeof(struct TermInfo)) == sizeof(struct TermInfo))
  298.                         {
  299.                                 /* Pop the version chunk, i.e. write it to the file. */
  300.  
  301.                             if(PopChunk(Handle))
  302.                                 Success = FALSE;
  303.                             else
  304.                             {
  305.                                     /* Push the real data chunk on the stack. */
  306.  
  307.                                 if(!PushChunk(Handle,0,Type,IFFSIZE_UNKNOWN))
  308.                                 {
  309.                                         /* Write the data. */
  310.  
  311.                                     if(WriteChunkBytes(Handle,Data,Size) == Size)
  312.                                         Success = TRUE;
  313.  
  314.                                             /* Pop the data chunk. */
  315.  
  316.                                     if(PopChunk(Handle))
  317.                                         Success = FALSE;
  318.                                 }
  319.                                 else
  320.                                     Success = FALSE;
  321.                             }
  322.                         }
  323.                         else
  324.                             Success = FALSE;
  325.                     }
  326.  
  327.                         /* Seems that we're done, now try to pop the FORM chunk
  328.                          * and return.
  329.                          */
  330.  
  331.                     if(PopChunk(Handle))
  332.                         Success = FALSE;
  333.                 }
  334.  
  335.                     /* Close the handle (flush any pending data). */
  336.  
  337.                 CloseIFF(Handle);
  338.             }
  339.  
  340.                 /* Close the DOS handle itself. */
  341.  
  342.             Close(Handle -> iff_Stream);
  343.         }
  344.  
  345.             /* And free the IFF handle. */
  346.  
  347.         FreeIFF(Handle);
  348.     }
  349.  
  350.     if(Success)
  351.         SetProtection(Name,FIBF_EXECUTE);
  352.  
  353.     return(Success);
  354. }
  355.  
  356.     /* ReadIFFData(UBYTE *Name,APTR Data,LONG Size,ULONG Type):
  357.      *
  358.      *    Read data from a `TERM' FORM chunk contained in an IFF file.
  359.      */
  360.  
  361. BYTE
  362. ReadIFFData(UBYTE *Name,APTR Data,LONG Size,ULONG Type)
  363. {
  364.     struct IFFHandle    *Handle;
  365.     BYTE             Success = FALSE;
  366.  
  367.     if(Handle = AllocIFF())
  368.     {
  369.         if(Handle -> iff_Stream = Open(Name,MODE_OLDFILE))
  370.         {
  371.             InitIFFasDOS(Handle);
  372.  
  373.             if(!OpenIFF(Handle,IFFF_READ))
  374.             {
  375.                     /* The following line tells iffparse to stop at the
  376.                      * very beginning of a `Type' chunk contained in a
  377.                      * `TERM' FORM chunk.
  378.                      */
  379.  
  380.                 if(!StopChunk(Handle,'TERM',Type))
  381.                 {
  382.                         /* Parse the file... */
  383.  
  384.                     if(!ParseIFF(Handle,IFFPARSE_SCAN))
  385.                     {
  386.                             /* The file read pointer is positioned
  387.                              * just in front of the first data
  388.                              * to be read, so let's don't disappoint
  389.                              * iffparse and read it.
  390.                              */
  391.  
  392.                         if(ReadChunkBytes(Handle,Data,Size) == Size)
  393.                             Success = TRUE;
  394.                     }
  395.                 }
  396.  
  397.                 CloseIFF(Handle);
  398.             }
  399.  
  400.             Close(Handle -> iff_Stream);
  401.         }
  402.  
  403.         FreeIFF(Handle);
  404.     }
  405.  
  406.     return(Success);
  407. }
  408.  
  409.     /* Convert(struct PhoneEntry *Src):
  410.      *
  411.      *    Convert a phonebook entry.
  412.      */
  413.  
  414. struct NewConfiguration *
  415. Convert(struct Configuration *Src)
  416. {
  417.     STATIC struct NewConfiguration Dst;
  418.  
  419.     Dst . BaudRate            = Src -> BaudRate;
  420.     Dst . BitsPerChar        = Src -> BitsPerChar;
  421.     Dst . Parity            = Src -> Parity;
  422.     Dst . StopBits            = Src -> StopBits;
  423.     Dst . Handshaking        = Src -> Handshaking;
  424.     Dst . Duplex            = Src -> Duplex;
  425.     Dst . HighSpeed            = Src -> HighSpeed;
  426.     Dst . BreakLength        = Src -> BreakLength;
  427.  
  428.     strcpy(Dst . SerialDevice,Src -> SerialDevice);
  429.  
  430.     Dst . UnitNumber            = Src -> UnitNumber;
  431.  
  432.     strcpy(Dst . ModemInit,Src -> ModemInit);
  433.     strcpy(Dst . ModemExit,Src -> ModemExit);
  434.     strcpy(Dst . ModemHangup,"~~~~~~+++~~ATH0\\r");
  435.     strcpy(Dst . DialPrefix,Src -> DialPrefix);
  436.  
  437.     Dst . RedialDelay        = Src -> RedialDelay;
  438.     Dst . DialRetries        = Src -> DialRetries;
  439.     Dst . DialTimeout        = Src -> DialTimeout;
  440.     Dst . ConnectAutoBaud        = Src -> ConnectAutoBaud;
  441.  
  442.     strcpy(Dst . NoCarrier,Src -> NoCarrier);
  443.     strcpy(Dst . Connect,Src -> Connect);
  444.     strcpy(Dst . Voice,Src -> Voice);
  445.     strcpy(Dst . Ring,Src -> Ring);
  446.     strcpy(Dst . Busy,Src -> Busy);
  447.  
  448.     strcpy(Dst . Protocol,Src -> Protocol);
  449.  
  450.     strcpy(Dst . StartupMacro,Src -> StartupMacro);
  451.  
  452.     strcpy(Dst . MacroFile,Src -> MacroFile);
  453.  
  454.     Dst . DisplayMode        = Src -> DisplayMode;
  455.  
  456.     CopyMem(Src -> Colours,Dst . Colours,sizeof(UWORD) * 16);
  457.  
  458.     Dst . MakeScreenPublic        = Src -> MakeScreenPublic;
  459.     Dst . ShanghaiWindows        = Src -> ShanghaiWindows;
  460.  
  461.     Dst . CaptureFilter        = Src -> CaptureFilter;
  462.     Dst . DestructiveBackspace    = Src -> DestructiveBackspace;
  463.     Dst . AudibleBell        = Src -> AudibleBell;
  464.     Dst . VisibleBell        = Src -> VisibleBell;
  465.     Dst . EightyColumns        = Src -> EightyColumns;
  466.     Dst . SendCR            = Src -> SendCR;
  467.     Dst . SendLF            = Src -> SendLF;
  468.     Dst . ColourMode        = Src -> ColourMode;
  469.     Dst . Emulation            = Src -> Emulation;
  470.     Dst . Font            = Src -> Font;
  471.  
  472.     strcpy(Dst . DefaultStorage,"ENVARC:sys");
  473.  
  474.     return(&Dst);
  475. }
  476.