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

  1. /* $Revision Header * Header built automatically - do not edit! *************
  2.  *
  3.  *    (C) Copyright 1990 by Olaf 'Olsen' Barthel & MXM
  4.  *
  5.  *    Name .....: TermPhone.c
  6.  *    Created ..: Monday 21-Jan-91 20:12
  7.  *    Revision .: 0
  8.  *
  9.  *    Date            Author          Comment
  10.  *    =========       ========        ====================
  11.  *    21-Jan-91       Olsen           Created this file!
  12.  *
  13.  * $Revision Header ********************************************************/
  14.  
  15. #include "TermGlobal.h"
  16.  
  17.     /* SetPrefToDefaults():
  18.      *
  19.      *    Initialize configuration with default values.
  20.      */
  21.  
  22. VOID
  23. SetPrefToDefaults(struct Configuration *Config,UBYTE *PathBuffer)
  24. {
  25.     if(!PathBuffer)
  26.         PathBuffer = "ENVARC:term";
  27.  
  28.     strcpy(Config -> DefaultStorage,PathBuffer);
  29.  
  30.     /* Serial Preferences. */
  31.  
  32.     Config -> BaudRate        = 2400;
  33.     Config -> BitsPerChar        = 8;
  34.     Config -> Parity        = PARITY_NONE;
  35.     Config -> StopBits        = 1;
  36.     Config -> Handshaking        = HANDSHAKING_XONXOFF;
  37.     Config -> Duplex        = DUPLEX_FULL;
  38.     Config -> HighSpeed        = FALSE;
  39.     Config -> BreakLength        = 250000;
  40.     Config -> UnitNumber        = 0;
  41.  
  42.     strcpy(Config -> SerialDevice,SERIALNAME);
  43.  
  44.     /* Modem Preferences. */
  45.  
  46.     Config -> RedialDelay        = 2;
  47.     Config -> DialRetries        = 10;
  48.     Config -> DialTimeout        = 60;
  49.     Config -> ConnectAutoBaud    = FALSE;
  50.  
  51.     strcpy(Config -> ModemInit,    "AT\\r");
  52.     strcpy(Config -> ModemExit,    "");
  53.     strcpy(Config -> ModemHangup,    "~~~~~~~~+++~~~~~~ATH0\\r");
  54.     strcpy(Config -> DialPrefix,    "~~ATX3DP");
  55.     strcpy(Config -> NoCarrier,    "NO CARRIER");
  56.     strcpy(Config -> Connect,    "CONNECT");
  57.     strcpy(Config -> Voice,        "VOICE");
  58.     strcpy(Config -> Ring,        "RING");
  59.     strcpy(Config -> Busy,        "BUSY");
  60.  
  61.     /* Transfer Preferences. */
  62.  
  63.     strcpy(Config -> Protocol,"xprzmodem.library");
  64.  
  65.     /* Macro Preferences. */
  66.  
  67.     strcpy(LastMacros,PathBuffer);
  68.     AddPart(LastMacros,"Macros.term",256);
  69.  
  70.     strcpy(Config -> MacroFile,LastMacros);
  71.  
  72.     /* Screen Preferences. */
  73.  
  74.     Config -> DisplayMode        = ModeID[0];
  75.     Config -> MakeScreenPublic    = TRUE;
  76.  
  77.     /* Terminal Preferences. */
  78.  
  79.     Config -> CaptureFilter        = TRUE;
  80.     Config -> DestructiveBackspace    = TRUE;
  81.     Config -> AudibleBell        = TRUE;
  82.     Config -> VisibleBell        = FALSE;
  83.     Config -> EightyColumns        = FALSE;
  84.     Config -> SendCR        = CR_ASCR;
  85.     Config -> SendLF        = LF_ASLF;
  86.     Config -> ColourMode        = COLOUR_AMIGA;
  87.     Config -> Emulation        = EMULATION_ANSIVT100;
  88.     Config -> Font            = FONT_TOPAZ;
  89. }
  90.  
  91.     /* CreatePhoneList():
  92.      *
  93.      *    Turn the array of pointers to phonebook entries into
  94.      *    a linked standard Amiga List (gadtools needs this).
  95.      */
  96.  
  97. struct List *
  98. CreatePhoneList()
  99. {
  100.     struct List    *PhoneList;
  101.     struct Node    *PhoneNode;
  102.     LONG         i;
  103.  
  104.     if(PhoneList = (struct List *)AllocMem(sizeof(struct List),MEMF_PUBLIC|MEMF_CLEAR))
  105.     {
  106.         NewList(PhoneList);
  107.  
  108.         if(Phonebook && NumPhoneEntries)
  109.         {
  110.             if(PhoneNode = (struct Node *)AllocMem(NumPhoneEntries * sizeof(struct Node),MEMF_PUBLIC|MEMF_CLEAR))
  111.             {
  112.                 for(i = 0 ; i < NumPhoneEntries ; i++)
  113.                 {
  114.                     PhoneNode[i] . ln_Name    = Phonebook[i] -> Name;
  115.  
  116.                     AddTail(PhoneList,&PhoneNode[i]);
  117.                 }
  118.  
  119.                 return(PhoneList);
  120.             }
  121.         }
  122.         else
  123.             return(PhoneList);
  124.  
  125.         FreeMem(PhoneList,sizeof(struct List));
  126.     }
  127.  
  128.     return(NULL);
  129. }
  130.  
  131.     /* DeletePhoneList(struct List *PhoneList):
  132.      *
  133.      *    Delete the entries listed in the Amiga List
  134.      *    created by the routine above.
  135.      */
  136.  
  137. VOID
  138. DeletePhoneList(struct List *PhoneList)
  139. {
  140.     if(PhoneList)
  141.     {
  142.         if(NumPhoneEntries)
  143.             FreeMem(PhoneList -> lh_Head,NumPhoneEntries * sizeof(struct Node));
  144.  
  145.         FreeMem(PhoneList,sizeof(struct List));
  146.     }
  147. }
  148.  
  149.     /* Compare(struct PhoneEntry **A,struct PhoneEntry **B):
  150.      *
  151.      *    Comparison subroutine required by the QuickSort
  152.      *    call below.     
  153.      */
  154.  
  155. STATIC LONG
  156. Compare(struct PhoneEntry **A,struct PhoneEntry **B)
  157. {
  158.     return(StrCmp((*A) -> Name,(*B) -> Name));
  159. }
  160.  
  161.     /* SortPhoneEntries():
  162.      *
  163.      *    Sorts the current phone list array in ascending order.
  164.      */
  165.  
  166. VOID
  167. SortPhoneEntries()
  168. {
  169.     qsort(Phonebook,NumPhoneEntries,sizeof(struct PhoneEntry *),Compare);
  170. }
  171.  
  172.     /* RemPhoneEntry(LONG Num):
  173.      *
  174.      *    Remove a given entry from the phone book.
  175.      */
  176.  
  177. VOID
  178. RemPhoneEntry(LONG Num)
  179. {
  180.     struct PhoneEntry    *Entry;
  181.     LONG             i;
  182.  
  183.     Entry = Phonebook[Num];
  184.  
  185.     for(i = Num ; i < NumPhoneEntries ; i++)
  186.         Phonebook[i] = Phonebook[i + 1];
  187.  
  188.     Phonebook[NumPhoneEntries--] = NULL;
  189.  
  190.     FreeMem(Entry,sizeof(struct PhoneEntry));
  191. }
  192.  
  193.     /* NewPhoneEntry():
  194.      *
  195.      *    Create a new phone book entry with default values and
  196.      *    add it to the array. Expand the phone book if necessary.
  197.      */
  198.  
  199. BYTE
  200. NewPhoneEntry()
  201. {
  202.     struct PhoneEntry    **PrivatePhonebook;
  203.     LONG             PrivatePhoneSize;
  204.     LONG             i;
  205.  
  206.         /* The phone book is filled `to the brim', so let's expand
  207.          * it.
  208.          */
  209.  
  210.     if(NumPhoneEntries + 1 > PhoneSize)
  211.     {
  212.             /* Allocate another phone book. */
  213.  
  214.         if(PrivatePhonebook = CreatePhonebook(PhoneSize + 1,&PrivatePhoneSize,FALSE))
  215.         {
  216.                 /* Copy the data. */
  217.  
  218.             if(Phonebook && PhoneSize)
  219.             {
  220.                 for(i = 0 ; i < PhoneSize ; i++)
  221.                     PrivatePhonebook[i] = Phonebook[i];
  222.  
  223.                     /* Remove the old phonebook. */
  224.  
  225.                 DeletePhonebook(Phonebook,PhoneSize,FALSE);
  226.             }
  227.  
  228.                 /* Assign the new pointers. */
  229.  
  230.             Phonebook = PrivatePhonebook;
  231.             PhoneSize = PrivatePhoneSize;
  232.         }
  233.         else
  234.             return(FALSE);
  235.     }
  236.  
  237.         /* Allocate another entry and add it to the phone book. */
  238.  
  239.     if(Phonebook[NumPhoneEntries] = (struct PhoneEntry *)AllocMem(sizeof(struct PhoneEntry),MEMF_PUBLIC|MEMF_CLEAR))
  240.     {
  241.         strcpy(Phonebook[NumPhoneEntries] -> Name,"-- Unused Entry --");
  242.  
  243.         CopyMem(&Config,&Phonebook[NumPhoneEntries] -> Config,sizeof(struct Configuration));
  244.  
  245.             /* These are the german default values. */
  246.  
  247.         Phonebook[NumPhoneEntries] -> PayPerUnit[0] = 23;
  248.         Phonebook[NumPhoneEntries] -> PayPerUnit[1] = 23;
  249.  
  250.         Phonebook[NumPhoneEntries] -> SecPerUnit[0] =  6 * 60;
  251.         Phonebook[NumPhoneEntries] -> SecPerUnit[1] = 12 * 60;
  252.  
  253.         Phonebook[NumPhoneEntries] -> TimeOfDay[0] =  48;
  254.         Phonebook[NumPhoneEntries] -> TimeOfDay[1] = 108;
  255.  
  256.         NumPhoneEntries++;
  257.  
  258.         return(TRUE);
  259.     }
  260.  
  261.     return(FALSE);
  262. }
  263.  
  264.     /* CreatePhonebook(LONG Size,LONG *AllocSize,BYTE CreateEntries):
  265.      *
  266.      *    Create a new phone entry array (so-called phone book).
  267.      */
  268.  
  269. struct PhoneEntry **
  270. CreatePhonebook(LONG Size,LONG *AllocSize,BYTE CreateEntries)
  271. {
  272.     struct PhoneEntry **PhoneEntry = NULL;
  273.  
  274.     if(Size)
  275.     {
  276.             /* Round the number of phone entries to a
  277.              * multiple of eight.
  278.              */
  279.  
  280.         *AllocSize = (((Size + 7) >> 3) << 3);
  281.  
  282.             /* Create the list of pointers. */
  283.  
  284.         if(PhoneEntry = (struct PhoneEntry **)AllocMem(*AllocSize * sizeof(struct PhoneEntry *),MEMF_PUBLIC|MEMF_CLEAR))
  285.         {
  286.                 /* And create some entries if necessary. */
  287.  
  288.             if(CreateEntries)
  289.             {
  290.                 LONG i;
  291.  
  292.                 for(i = 0 ; i < Size ; i++)
  293.                 {
  294.                     if(!(PhoneEntry[i] = (struct PhoneEntry *)AllocMem(sizeof(struct PhoneEntry),MEMF_PUBLIC|MEMF_CLEAR)))
  295.                     {
  296.                         LONG j;
  297.  
  298.                         for(j = 0 ; j < i ; j++)
  299.                             FreeMem(PhoneEntry[j],sizeof(struct PhoneEntry));
  300.  
  301.                         FreeMem(PhoneEntry,*AllocSize * sizeof(struct PhoneEntry *));
  302.  
  303.                         return(NULL);
  304.                     }
  305.                 }
  306.             }
  307.         }
  308.     }
  309.  
  310.     return(PhoneEntry);
  311. }
  312.  
  313.     /* DeletePhonebook(struct PhoneEntry **PhoneBook,LONG Size,BYTE FreeEntries):
  314.      *
  315.      *    Deallocates a given phone book and its entries if necessary.
  316.      */
  317.  
  318. VOID
  319. DeletePhonebook(struct PhoneEntry **PhoneBook,LONG Size,BYTE FreeEntries)
  320. {
  321.     if(FreeEntries)
  322.     {
  323.         LONG i;
  324.  
  325.         for(i = 0 ; i < Size ; i++)
  326.         {
  327.             if(PhoneBook[i])
  328.                 FreeMem(PhoneBook[i],sizeof(struct PhoneEntry));
  329.         }
  330.     }
  331.  
  332.     FreeMem(PhoneBook,Size * sizeof(struct PhoneEntry *));
  333. }
  334.  
  335.     /* SavePhonebook(UBYTE *Name):
  336.      *
  337.      *    Save the current phone book to a disk file.
  338.      */
  339.  
  340. BYTE
  341. SavePhonebook(UBYTE *Name)
  342. {
  343.     struct IFFHandle    *Handle;
  344.     BYTE             Success = FALSE;
  345.  
  346.     if(Phonebook && NumPhoneEntries)
  347.     {
  348.         if(Handle = (struct IFFHandle *)AllocIFF())
  349.         {
  350.             if(Handle -> iff_Stream = Open(Name,MODE_NEWFILE))
  351.             {
  352.                 InitIFFasDOS(Handle);
  353.  
  354.                 if(!OpenIFF(Handle,IFFF_WRITE))
  355.                 {
  356.                     if(!PushChunk(Handle,'TERM',ID_CAT,IFFSIZE_UNKNOWN))
  357.                     {
  358.                         if(!PushChunk(Handle,'TERM',ID_FORM,IFFSIZE_UNKNOWN))
  359.                         {
  360.                             if(!PushChunk(Handle,0,'VERS',IFFSIZE_UNKNOWN))
  361.                             {
  362.                                 struct TermInfo TermInfo;
  363.  
  364.                                 TermInfo . Version    = TermVersion;
  365. /*                                TermInfo . Revision    = TermRevision;*/
  366.                                 TermInfo . Revision    = 9;
  367.  
  368.                                 if(WriteChunkBytes(Handle,&TermInfo,sizeof(struct TermInfo)) == sizeof(struct TermInfo))
  369.                                 {
  370.                                     if(PopChunk(Handle))
  371.                                         Success = FALSE;
  372.                                     else
  373.                                     {
  374.                                         if(!PushChunk(Handle,0,'DIAL',IFFSIZE_UNKNOWN))
  375.                                         {
  376.                                             if(WriteChunkBytes(Handle,&NumPhoneEntries,sizeof(LONG)) == sizeof(LONG))
  377.                                             {
  378.                                                 if(PopChunk(Handle))
  379.                                                     Success = FALSE;
  380.                                                 else
  381.                                                 {
  382.                                                     if(PopChunk(Handle))
  383.                                                         Success = FALSE;
  384.                                                     else
  385.                                                     {
  386.                                                         LONG i;
  387.  
  388.                                                         for(i = 0 ; i < NumPhoneEntries ; i++)
  389.                                                         {
  390.                                                             if(!PushChunk(Handle,'TERM',ID_FORM,IFFSIZE_UNKNOWN))
  391.                                                             {
  392.                                                                 if(!PushChunk(Handle,0,'PREF',IFFSIZE_UNKNOWN))
  393.                                                                 {
  394.                                                                     if(WriteChunkBytes(Handle,Phonebook[i],sizeof(struct PhoneEntry)) != sizeof(struct PhoneEntry))
  395.                                                                         break;
  396.                                                                     else
  397.                                                                         Success = TRUE;
  398.  
  399.                                                                     if(PopChunk(Handle))
  400.                                                                     {
  401.                                                                         Success = FALSE;
  402.                                                                         break;
  403.                                                                     }
  404.                                                                 }
  405.  
  406.                                                                 if(PopChunk(Handle))
  407.                                                                 {
  408.                                                                     Success = FALSE;
  409.                                                                     break;
  410.                                                                 }
  411.                                                             }
  412.                                                         }
  413.                                                     }
  414.                                                 }
  415.                                             }
  416.                                         }
  417.                                     }
  418.                                 }
  419.                             }
  420.                         }
  421.  
  422.                         if(PopChunk(Handle))
  423.                             Success = FALSE;
  424.                     }
  425.  
  426.                     CloseIFF(Handle);
  427.                 }
  428.  
  429.                 Close(Handle -> iff_Stream);
  430.             }
  431.  
  432.             FreeIFF(Handle);
  433.         }
  434.  
  435.         if(Success)
  436.             SetProtection(Name,FIBF_EXECUTE);
  437.         else
  438.             DeleteFile(Name);
  439.     }
  440.  
  441.     return(Success);
  442. }
  443.  
  444.     /* OldLoadPhonebook(UBYTE *Name):
  445.      *
  446.      *    Restore a phone book from a disk file (old version).
  447.      */
  448.  
  449. STATIC BYTE
  450. OldLoadPhonebook(UBYTE *Name)
  451. {
  452.     struct PhoneEntry    **PrivatePhonebook;
  453.     LONG             PrivatePhoneSize;
  454.     LONG             Size,i;
  455.     struct IFFHandle    *Handle;
  456.     BYTE             Success = FALSE;
  457.     struct StoredProperty    *Prop;
  458.     struct TermInfo        *TermInfo;
  459.  
  460.     if(Handle = AllocIFF())
  461.     {
  462.         if(Handle -> iff_Stream = Open(Name,MODE_OLDFILE))
  463.         {
  464.             InitIFFasDOS(Handle);
  465.  
  466.             if(!OpenIFF(Handle,IFFF_READ))
  467.             {
  468.                 if(!PropChunks(Handle,&VersionProps[0],1))
  469.                 {
  470.                     if(!StopChunk(Handle,'TERM','DIAL'))
  471.                     {
  472.                         if(!ParseIFF(Handle,IFFPARSE_SCAN))
  473.                         {
  474.                             if(Prop = FindProp(Handle,'TERM','VERS'))
  475.                             {
  476.                                 TermInfo = (struct TermInfo *)Prop -> sp_Data;
  477.  
  478.                                 if(TermInfo -> Version == TermVersion && TermInfo -> Revision <= TermRevision && TermInfo -> Revision >= 6)
  479.                                 {
  480.                                     if(ReadChunkRecords(Handle,&Size,sizeof(LONG),1))
  481.                                     {
  482.                                         if(Size)
  483.                                         {
  484.                                             if(PrivatePhonebook = CreatePhonebook(Size,&PrivatePhoneSize,TRUE))
  485.                                             {
  486.                                                 for(i = 0 ; i < Size ; i++)
  487.                                                 {
  488.                                                     SetPrefToDefaults(&PrivatePhonebook[i] -> Config,NULL);
  489.  
  490.                                                     if(!ReadChunkRecords(Handle,PrivatePhonebook[i],sizeof(struct PhoneEntry),1))
  491.                                                     {
  492.                                                         if(i)
  493.                                                             Size = i - 1;
  494.                                                         else
  495.                                                             Size = 0;
  496.  
  497.                                                         break;
  498.                                                     }
  499.                                                 }
  500.  
  501.                                                 if(Size)
  502.                                                 {
  503.                                                     if(Phonebook)
  504.                                                         DeletePhonebook(Phonebook,PhoneSize,TRUE);
  505.  
  506.                                                     Phonebook = PrivatePhonebook;
  507.                                                     PhoneSize = PrivatePhoneSize;
  508.  
  509.                                                     NumPhoneEntries = Size;
  510.  
  511.                                                     Success = TRUE;
  512.                                                 }
  513.                                                 else
  514.                                                 {
  515.                                                     DeletePhonebook(PrivatePhonebook,PrivatePhoneSize,TRUE);
  516.                                                     Success = FALSE;
  517.                                                 }
  518.  
  519.                                                 FreeSubList();
  520.                                             }
  521.                                         }
  522.                                     }
  523.                                 }
  524.                             }
  525.                         }
  526.                     }
  527.                 }
  528.  
  529.                 CloseIFF(Handle);
  530.             }
  531.  
  532.             Close(Handle -> iff_Stream);
  533.         }
  534.  
  535.         FreeIFF(Handle);
  536.     }
  537.  
  538.     return(Success);
  539. }
  540.  
  541.     /* LoadPhonebook(UBYTE *Name):
  542.      *
  543.      *    Restore a phone book from a disk file.
  544.      */
  545.  
  546. BYTE
  547. LoadPhonebook(UBYTE *Name)
  548. {
  549.     STATIC ULONG Stops[6] =
  550.     {
  551.         'TERM','VERS',
  552.         'TERM','DIAL',
  553.         'TERM','PREF'
  554.     };
  555.  
  556.     struct PhoneEntry    **PrivatePhonebook;
  557.     LONG             PrivatePhoneSize = 0;
  558.     LONG             Size = 0,i;
  559.     struct IFFHandle    *Handle;
  560.     BYTE             Success = FALSE;
  561.     struct ContextNode    *Chunk;
  562.     BYTE             UseOld = FALSE;
  563.  
  564.     if(Handle = AllocIFF())
  565.     {
  566.         if(Handle -> iff_Stream = Open(Name,MODE_OLDFILE))
  567.         {
  568.             InitIFFasDOS(Handle);
  569.  
  570.             if(!OpenIFF(Handle,IFFF_READ))
  571.             {
  572.                 if(!StopChunks(Handle,&Stops[0],3))
  573.                 {
  574.                     while(!ParseIFF(Handle,IFFPARSE_SCAN))
  575.                     {
  576.                         Chunk = CurrentChunk(Handle);
  577.  
  578.                         if(Chunk -> cn_ID == 'VERS')
  579.                         {
  580.                             struct TermInfo TermInfo;
  581.  
  582.                             if(ReadChunkBytes(Handle,&TermInfo,sizeof(struct TermInfo)) == sizeof(struct TermInfo))
  583.                             {
  584.                                 if(TermInfo . Version != TermVersion || TermInfo . Revision > 9 || TermInfo . Revision < 6)
  585.                                     break;
  586.  
  587.                                 if(TermInfo . Revision < 9)
  588.                                 {
  589.                                     UseOld = TRUE;
  590.  
  591.                                     break;
  592.                                 }
  593.                             }
  594.                             else
  595.                                 break;
  596.                         }
  597.  
  598.                         if(Chunk -> cn_ID == 'DIAL')
  599.                         {
  600.                             if(ReadChunkBytes(Handle,&Size,sizeof(LONG)) == sizeof(LONG))
  601.                             {
  602.                                 i = 0;
  603.  
  604.                                 if(!(PrivatePhonebook = CreatePhonebook(Size,&PrivatePhoneSize,TRUE)))
  605.                                     break;
  606.                             }
  607.                             else
  608.                                 break;
  609.                         }
  610.  
  611.                         if(Chunk -> cn_ID == 'PREF')
  612.                         {
  613.                             if(Size && i < Size)
  614.                             {
  615.                                 SetPrefToDefaults(&PrivatePhonebook[i] -> Config,NULL);
  616.  
  617.                                 if(ReadChunkBytes(Handle,PrivatePhonebook[i],sizeof(struct PhoneEntry)) == sizeof(struct PhoneEntry))
  618.                                     i++;
  619.                                 else
  620.                                 {
  621.                                     if(i)
  622.                                         Size = i - 1;
  623.                                     else
  624.                                         Size = 0;
  625.  
  626.                                     break;
  627.                                 }
  628.                             }
  629.                         }
  630.                     }
  631.  
  632.                     if(Size)
  633.                     {
  634.                         if(Phonebook)
  635.                             DeletePhonebook(Phonebook,PhoneSize,TRUE);
  636.  
  637.                         Phonebook = PrivatePhonebook;
  638.                         PhoneSize = PrivatePhoneSize;
  639.  
  640.                         NumPhoneEntries = Size;
  641.  
  642.                         Success = TRUE;
  643.                     }
  644.                     else
  645.                     {
  646.                         if(PrivatePhoneSize)
  647.                         {
  648.                             DeletePhonebook(PrivatePhonebook,PrivatePhoneSize,TRUE);
  649.                             Success = FALSE;
  650.                         }
  651.                     }
  652.  
  653.                     FreeSubList();
  654.                 }
  655.  
  656.                 CloseIFF(Handle);
  657.             }
  658.  
  659.             Close(Handle -> iff_Stream);
  660.         }
  661.  
  662.         FreeIFF(Handle);
  663.     }
  664.  
  665.     if(UseOld)
  666.         Success = OldLoadPhonebook(Name);
  667.  
  668.     return(Success);
  669. }
  670.