home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 019.lha / Aterm / Phone.c < prev    next >
C/C++ Source or Header  |  1986-11-10  |  13KB  |  549 lines

  1. /*************************************************************************
  2.  * Business first... This code is NOT to be used for commercial purposes.
  3.  * You are granted a limited license to distribute it on a not-for-profit
  4.  * basis by any means, including, but not limited to, paper, magnetic media,
  5.  * or telecommunications. We have no objection to its appearance on a 
  6.  * commercial communications network provided it may be obtained for no
  7.  * cost over and above the standard connect time charges.
  8.  *  In addition, we would appreciate it if anyone modifying this code
  9.  * would attempt to get the modifications back to us so that we can
  10.  * keep some semblance of continuity of versions.
  11.  *
  12.  * Jeff Lydiatt
  13.  * Larry Phillips, Compuserve, 76703,4322
  14.  *
  15. **   File:  phone.c
  16. **      contains phone library manipulation
  17. **
  18. **   Functions:
  19. **    do_phlib() - presents and processes phone library requester
  20. **    init_req() - initializes phone library requester structures
  21. **    sav_phlib() - writes current phone library to disk file
  22. **    load_phlib() - loads phone library from disk file
  23. **    dial() - calls number from phone library
  24. **
  25. *************************************************************************/
  26. #include <exec/types.h>
  27. #include <stdio.h>
  28. #include <intuition/intuition.h>
  29. #include <intuition/intuitionbase.h>
  30. #include <functions.h>
  31. #include "ConsoleIO.h"
  32. #include "SerialIO.h"
  33.  
  34. /*********************************************************************
  35. *      Structures to define Requester and it's gadgets
  36. *********************************************************************/
  37.  
  38. static struct IntuiText ph_req_txt3 = {
  39.     3,1,        /* front pen, back */
  40.     JAM1,        /* draw mode */
  41.     140,0,        /* relative start */
  42.     NULL,        /* font */
  43.     (UBYTE *) " Phone Library ", /* text */
  44.     NULL        /* chain */
  45.     };
  46.  
  47. static struct IntuiText ph_req_txt2 = {
  48.     3,1,        /* front pen, back */
  49.     JAM1,        /* draw mode */
  50.     48,30,        /* relative start */
  51.     NULL,        /* font */
  52.     (UBYTE *) "Name:", /* text */
  53.     &ph_req_txt3    /* chain */
  54.     };
  55.  
  56. static struct IntuiText ph_req_txt1 = {
  57.     3,1,        /* front pen, back */
  58.     JAM1,        /* draw mode */
  59.     218,30,        /* relative start */
  60.     NULL,        /* font */
  61.     (UBYTE *) "Number:", /* text */
  62.     &ph_req_txt2    /* chain */
  63.     };
  64.  
  65. static struct IntuiText l_txt = {
  66.     2,3,        /* front pen, back */
  67.     JAM1,        /* draw mode */
  68.     2,1,        /* relative start */
  69.     NULL,        /* font */
  70.     (UBYTE *) "Load", /* text */
  71.     NULL        /* chain */
  72.     };
  73.  
  74. static struct IntuiText s_txt = {
  75.     2,3,        /* front pen, back */
  76.     JAM1,        /* draw mode */
  77.     2,1,        /* relative start */
  78.     NULL,        /* font */
  79.     (UBYTE *) "Save", /* text */
  80.     NULL        /* chain */
  81.     };
  82.  
  83. static struct IntuiText c_txt = {
  84.     2,3,        /* front pen, back */
  85.     JAM1,        /* draw mode */
  86.     2,1,        /* relative start */
  87.     NULL,        /* font */
  88.     (UBYTE *) "Quit", /* text */
  89.     NULL        /* chain */
  90.     };
  91.  
  92. static struct IntuiText d_txt = {
  93.     2,0,        /* front pen, back */
  94.     JAM1,        /* draw mode */
  95.     2,1,        /* relative start */
  96.     NULL,        /* font */
  97.     (UBYTE *) "DIAL", /* text */
  98.     NULL        /* chain */
  99.     };
  100.  
  101. static struct IntuiText ph_nm_txt = {
  102.     2,1,        /* front pen, back */
  103.     JAM1,        /* draw mode */
  104.     -48,0,        /* relative start */
  105.     NULL,        /* font */
  106.     (UBYTE *) "File: ", /* text */
  107.     NULL        /* chain */
  108.     };
  109.  
  110. static struct IntuiText pfx_txt = {
  111.     2,1,        /* front pen, back */
  112.     JAM1,        /* draw mode */
  113.     -96,0,        /* relative start */
  114.     NULL,        /* font */
  115.     (UBYTE *) "Dial Prefix:", /* text */
  116.     NULL        /* chain */
  117.     };
  118.  
  119. #define Fil_Nam_Sz 128
  120. #define Bbs_Ent_Sz 21
  121. #define MAX_NUM 10
  122.  
  123. static UBYTE fil_nam[Fil_Nam_Sz] = "s:Term.Phn";
  124. static UBYTE pfx_nam[Bbs_Ent_Sz] = "ATDT";
  125. static UBYTE undo_buf[Fil_Nam_Sz];
  126. static UBYTE bbs_name[MAX_NUM][Bbs_Ent_Sz+1];
  127. static UBYTE bbs_num[MAX_NUM][Bbs_Ent_Sz+1];
  128.  
  129. static struct StringInfo ph_nm_buf = {
  130.     fil_nam,    /* buffer */
  131.     undo_buf,    /* undo buffer */
  132.     0,        /* buf pos */
  133.     Fil_Nam_Sz,    /* Max chars */
  134.     0,        /* first disp pos */
  135.     0,        /* Undo pos */
  136.     13,        /*  current char count */
  137.     0,0,0,
  138.     NULL,NULL
  139.     };
  140.  
  141. static struct StringInfo pfx_buf = {
  142.     pfx_nam,    /* buffer */
  143.     undo_buf,    /* undo buffer */
  144.     0,        /* buf pos */
  145.     Bbs_Ent_Sz,    /* Max chars */
  146.     0,        /* first disp pos */
  147.     0,        /* Undo pos */
  148.     4,        /*  current char count */
  149.     0,0,0,
  150.     NULL,NULL
  151.     };
  152.  
  153. static struct StringInfo bbs_name_s [MAX_NUM];
  154.  
  155. static struct StringInfo bbs_num_s [MAX_NUM];
  156.  
  157. static SHORT bord_cords[] = 
  158.     {0,0,35,0,35,10,0,10,0,0};
  159.  
  160. static struct Border bord = {
  161.     0,-1,        /* left, top */
  162.     2,1,JAM1,
  163.     5,bord_cords,
  164.     NULL
  165.     };
  166.  
  167. /*   Macros for identifying gadgets   */
  168.  
  169. #define C_GAD        200
  170. #define S_GAD        201
  171. #define L_GAD        202
  172. #define PH_NM_GAD    203
  173. #define PFX_GAD        204
  174.  
  175. #define UP_C_GAD (gad->GadgetID == C_GAD)
  176. #define UP_S_GAD (gad->GadgetID == S_GAD)
  177. #define UP_L_GAD (gad->GadgetID == L_GAD)
  178. #define UP_PH_NM_GAD (gad->GadgetID == PH_NM_GAD)
  179. #define UP_PFX_GAD (gad->GadgetID == PFX_GAD)
  180. #define UP_DIAL_GAD ((gad->GadgetID % 3) == 0)
  181. #define UP_NAME_GAD ((gad->GadgetID % 3) == 1)
  182. #define UP_NUM_GAD ((gad->GadgetID % 3) == 2)
  183.  
  184. #define GAD_LINE (gad->GadgetID / 3)
  185.  
  186.  
  187. static struct Gadget d_gad [MAX_NUM];
  188.  
  189. static struct Gadget nam_gad [MAX_NUM];
  190.  
  191. static struct Gadget num_gad [MAX_NUM];
  192.  
  193. static struct Gadget pfx_gad = {
  194.     &d_gad[0],    /* chain to next */
  195.     100,22,        /* left, top */
  196.     160,11,        /* width, height */
  197.     GADGHCOMP,    /* flags */
  198.     RELVERIFY,
  199.     REQGADGET |
  200.     STRGADGET,    /* gadget type */
  201.     NULL, NULL,    /* render ptrs */
  202.     &pfx_txt,    /* gadget label */
  203.     NULL,        /* exclude */
  204.     (APTR)&pfx_buf,    /* string buffers */
  205.     PFX_GAD,        /* gadget id */
  206.     NULL        /* user data */
  207.     };
  208.  
  209. static struct Gadget c_gad = {
  210.     &pfx_gad,    /* chain to next */
  211.     330,10,        /* left, top */
  212.     35,11,        /* width, height */
  213.     GADGHCOMP,    /* flags */
  214.     RELVERIFY |
  215.     ENDGADGET,
  216.     REQGADGET |
  217.     BOOLGADGET,    /* gadget type */
  218.     (APTR)&bord, NULL,    /* render ptrs */
  219.     &c_txt,        /* gadget label */
  220.     NULL,        /* exclude */
  221.     NULL,
  222.     C_GAD,        /* gadget id */
  223.     NULL        /* user data */
  224.     };
  225.  
  226. static struct Gadget s_gad = {
  227.     &c_gad,        /* chain to next */
  228.     288,10,        /* left, top */
  229.     35,11,        /* width, height */
  230.     GADGHCOMP,    /* flags */
  231.     RELVERIFY,    /* activation */
  232.     REQGADGET |
  233.     BOOLGADGET,    /* gadget type */
  234.     (APTR)&bord, NULL,    /* render ptrs */
  235.     &s_txt,        /* gadget label */
  236.     NULL,        /* exclude */
  237.     NULL,
  238.     S_GAD,        /* gadget id */
  239.     NULL        /* user data */
  240.     };
  241.  
  242. static struct Gadget l_gad = {
  243.     &s_gad,        /* chain to next */
  244.     245,10,        /* left, top */
  245.     35,11,        /* width, height */
  246.     GADGHCOMP,    /* flags */
  247.     RELVERIFY,    /* activation */
  248.     REQGADGET |
  249.     BOOLGADGET,    /* gadget type */
  250.     (APTR)&bord, NULL,    /* render ptrs */
  251.     &l_txt,        /* gadget label */
  252.     NULL,        /* exclude */
  253.     NULL,
  254.     L_GAD,        /* gadget id */
  255.     NULL        /* user data */
  256.     };
  257.  
  258. static struct Gadget ph_nm_gad = {
  259.     &l_gad,        /* chain to next */
  260.     60,10,        /* left, top */
  261.     160,11,        /* width, height */
  262.     GADGHCOMP,    /* flags */
  263.     RELVERIFY,
  264.     REQGADGET |
  265.     STRGADGET,    /* gadget type */
  266.     NULL, NULL,    /* render ptrs */
  267.     &ph_nm_txt,    /* gadget label */
  268.     NULL,        /* exclude */
  269.     (APTR)&ph_nm_buf,    /* string buffers */
  270.     PH_NM_GAD,        /* gadget id */
  271.     NULL        /* user data */
  272.     };
  273.  
  274. static struct Requester ph_req = {
  275.     NULL,        /* backwards chain */
  276.     30,10,        /* left, top */
  277.     400,160,    /* width, height */
  278.     NULL,NULL,    /* pointer relatives */
  279.     &ph_nm_gad,    /* first gadget */
  280.     NULL,        /* border */
  281.     &ph_req_txt1,    /* Requester text */
  282.     NULL,        /* flags */
  283.     1,        /* backfill */
  284.     NULL,        /* clip req */
  285.     NULL,        /*  bit map */
  286.     NULL        /* bit map */
  287.     };
  288.  
  289. /********************************************************************
  290. *   Function to handle PhoneLib Requester                           *
  291. ********************************************************************/
  292.  
  293.  
  294.  
  295. static init_req()
  296. {
  297.     int i;
  298.     USHORT g_num = 0;
  299.  
  300.     for (i = 0;i < MAX_NUM;i++) {
  301.         d_gad[i].NextGadget = &nam_gad[i];
  302.         d_gad[i].LeftEdge = 2;
  303.         d_gad[i].TopEdge = 38 + (11 * i);
  304.         d_gad[i].Width = 35;
  305.         d_gad[i].Height = 11;;
  306.         d_gad[i].Flags = GADGHCOMP;
  307.         d_gad[i].Activation = RELVERIFY | ENDGADGET;
  308.         d_gad[i].GadgetType = REQGADGET | BOOLGADGET;
  309.         d_gad[i].GadgetRender = (APTR)&bord;
  310.         d_gad[i].SelectRender = NULL;
  311.         d_gad[i].GadgetText = &d_txt;
  312.         d_gad[i].MutualExclude = NULL;
  313.         d_gad[i].SpecialInfo = NULL;
  314.         d_gad[i].GadgetID = g_num++;
  315.         d_gad[i].UserData = NULL;
  316.         nam_gad[i].NextGadget = &num_gad[i];
  317.         nam_gad[i].LeftEdge = 40;
  318.         nam_gad[i].TopEdge = 39 + (11 * i);
  319.         nam_gad[i].Width = 160;
  320.         nam_gad[i].Height = 11;;
  321.         nam_gad[i].Flags = GADGHCOMP;
  322.         nam_gad[i].Activation = RELVERIFY;
  323.         nam_gad[i].GadgetType = REQGADGET | STRGADGET;
  324.         nam_gad[i].GadgetRender = NULL;
  325.         nam_gad[i].SelectRender = NULL;
  326.         nam_gad[i].GadgetText = NULL;
  327.         nam_gad[i].MutualExclude = NULL;
  328.         nam_gad[i].SpecialInfo = (APTR)&bbs_name_s[i];
  329.         nam_gad[i].GadgetID = g_num++;
  330.         nam_gad[i].UserData = NULL;
  331.         num_gad[i].NextGadget = &d_gad[i]+1;
  332.         num_gad[i].LeftEdge = 210;
  333.         num_gad[i].TopEdge = 39 + (11 * i);
  334.         num_gad[i].Width = 160;
  335.         num_gad[i].Height = 11;;
  336.         num_gad[i].Flags = GADGHCOMP;
  337.         num_gad[i].Activation = RELVERIFY;
  338.         num_gad[i].GadgetType = REQGADGET | STRGADGET;
  339.         num_gad[i].GadgetRender = NULL;
  340.         num_gad[i].SelectRender = NULL;
  341.         num_gad[i].GadgetText = NULL;
  342.         num_gad[i].MutualExclude = NULL;
  343.         num_gad[i].SpecialInfo = (APTR)&bbs_num_s[i];
  344.         num_gad[i].GadgetID = g_num++;
  345.         num_gad[i].UserData = NULL;
  346.         bbs_name_s[i].Buffer = (UBYTE *)&bbs_name[i];
  347.         bbs_name_s[i].UndoBuffer = undo_buf;
  348.         bbs_name_s[i].BufferPos = 0;
  349.         bbs_name_s[i].MaxChars = Bbs_Ent_Sz;
  350.         bbs_name_s[i].DispPos = 0;
  351.         bbs_name_s[i].UndoPos = 0;
  352.         bbs_name_s[i].NumChars = 0;
  353.         bbs_name_s[i].DispCount = 0;
  354.         bbs_name_s[i].CLeft = 0;
  355.         bbs_name_s[i].CTop = 0;
  356.         bbs_name_s[i].LayerPtr = NULL;
  357.         bbs_name_s[i].LongInt = 0;
  358.         bbs_name_s[i].AltKeyMap = NULL;
  359.         bbs_num_s[i].Buffer = (UBYTE *)&bbs_num[i];
  360.         bbs_num_s[i].UndoBuffer = undo_buf;
  361.         bbs_num_s[i].BufferPos = 0;
  362.         bbs_num_s[i].MaxChars = Bbs_Ent_Sz;
  363.         bbs_num_s[i].DispPos = 0;
  364.         bbs_num_s[i].UndoPos = 0;
  365.         bbs_num_s[i].NumChars = 0;
  366.         bbs_num_s[i].DispCount = 0;
  367.         bbs_num_s[i].CLeft = 0;
  368.         bbs_num_s[i].CTop = 0;
  369.         bbs_num_s[i].LayerPtr = NULL;
  370.         bbs_num_s[i].LongInt = 0;
  371.         bbs_num_s[i].AltKeyMap = NULL;
  372.         *bbs_name[i] = '\0';
  373.         *bbs_num[i] = '\0';
  374.     }
  375.     num_gad[MAX_NUM-1].NextGadget = NULL;
  376.     return(0);
  377. }
  378.  
  379. static int sav_phlib()
  380. {
  381.     int i;
  382.     FILE *fptr;
  383.  
  384.  
  385.     if ((fptr = fopen(fil_nam,"w")) == NULL)
  386.         return(1);    /*  NOTE: file cannot open */
  387.  
  388.     if ((fputs(pfx_nam,fptr) == EOF) ||
  389.         (fputc('\n',fptr) == EOF)) {
  390.         fclose(fptr);
  391.         return(1);
  392.     }
  393.     
  394.     for (i = 0;i<MAX_NUM;i++) {
  395.         if ((fputs(bbs_name[i],fptr) == EOF) ||
  396.             (fputc('\n',fptr) == EOF)) {
  397.             fclose(fptr);
  398.             return(1);
  399.         }
  400.         if ((fputs(bbs_num[i],fptr) == EOF) ||
  401.             (fputc('\n',fptr) == EOF)) {
  402.             fclose(fptr);
  403.             return(1);
  404.         }
  405.     }
  406.     fclose(fptr);
  407.     return(0);
  408. }
  409.         
  410.  
  411. static int load_phlib(menu_act)
  412. int    menu_act;
  413. {
  414.     int i, j;
  415.     int error = 0;
  416.     FILE *fptr;
  417.  
  418.     for (i = 0;i < MAX_NUM; ++i) {
  419.         *bbs_name[i] = '\0';
  420.         *bbs_num[i] = '\0';
  421.     }
  422.  
  423.     if ((fptr = fopen(fil_nam,"r")) == NULL) {
  424.         error = TRUE;
  425.     }
  426.  
  427.     if (!error) {
  428.         if (fgets(pfx_nam,Bbs_Ent_Sz+1,fptr) == NULL) 
  429.             pfx_nam[0] = '\0';
  430.         else {
  431.             j = strlen(pfx_nam);
  432.             if (j) pfx_nam[j-1] = '\0';
  433.             for (i = 0;i<MAX_NUM;i++) {
  434.                 if ((fgets(bbs_name[i],Bbs_Ent_Sz+1,fptr) == NULL) ||
  435.                     (fgets(bbs_num[i],Bbs_Ent_Sz+1,fptr) == NULL)) {
  436.                     bbs_name[i][0] = '\0';
  437.                     bbs_num[i][0] = '\0';
  438.                     i = MAX_NUM + 1;
  439.                 }
  440.                 j = strlen(bbs_name[i]);
  441.                 if (j) bbs_name[i][j-1] = '\0';    /*  note: drop \n */
  442.                 j = strlen(bbs_num[i]);
  443.                 if (j) bbs_num[i][j-1] = '\0';    /*  note: drop \n */
  444.             }
  445.         }
  446.         fclose(fptr);
  447.     }
  448.     return(0);
  449. }
  450.  
  451. static void dial_str( str )
  452. char *str;
  453. {
  454.     while( *str )
  455.            SerIOPut( *str++ );
  456. }
  457.         
  458. static void dial(ent)
  459.     int ent;
  460. {
  461.     char Fname[27];
  462.  
  463.     strcpy(Fname, "s:");
  464.     strcat(Fname,bbs_name[ent]);
  465.  
  466.     if (*bbs_name[ent] != ' ')
  467.       {
  468.         if (LoadKeys(&Fname[2]))
  469.           {
  470.             PutString("\n\x9b33m");
  471.             PutString(bbs_name[ent]);
  472.             PutString(" Function Keys Loaded\n\x9bm");
  473.           }
  474.         else if (LoadKeys(&Fname[0]))
  475.           {
  476.             PutString("\n\x9b33m");
  477.             PutString(bbs_name[ent]);
  478.             PutString(" Function Keys Loaded\n\x9bm");
  479.           }
  480.       }
  481.     
  482.     PutString("\x9b33mCalling ");
  483.     PutString(bbs_name[ent]);
  484.     PutString("\n\x9bm");
  485.  
  486.     dial_str(pfx_nam);
  487.     dial_str(bbs_num[ent]);
  488.     dial_str("\x0d");
  489. }
  490.  
  491. void do_phlib( mywindow )
  492. struct Window *mywindow;
  493. {
  494.     static short init_ok = 0;
  495.     int looping = TRUE;
  496.     short dirty = 0;
  497.     struct IntuiMessage *mes;
  498.     struct Gadget *gad;
  499.     ULONG class;
  500.     int i;
  501.  
  502.  
  503.     if (!init_ok) {
  504.         init_req();
  505.         load_phlib(0);
  506.         init_ok = 1;
  507.     }
  508.  
  509.     if (Request(&ph_req,mywindow) != 1) {
  510.         PutString("Requester Failed\n");
  511.         return;
  512.     }
  513.  
  514.     while (looping) {
  515.        (VOID) Wait( 1L << mywindow->UserPort->mp_SigBit );
  516.     
  517.        while ( mes = (struct IntuiMessage *)GetMsg(mywindow->UserPort) )
  518.          {
  519.         class = mes->Class;
  520.         gad = (struct Gadget *)mes->IAddress;
  521.         (VOID) ReplyMsg(mes);
  522.         if ( class == GADGETUP)
  523.           switch( gad->GadgetID )
  524.           {
  525.             case C_GAD: looping = FALSE; break;
  526.             case S_GAD:
  527.             if (sav_phlib() == 1) {
  528.                 EndRequest(&ph_req,mywindow);
  529.                 PutString("Save of Phone Library failed\n");
  530.                 looping = FALSE;
  531.             }
  532.             break;
  533.             case L_GAD:
  534.             (void)load_phlib(1);
  535.             RefreshGadgets(&ph_nm_gad,mywindow,&ph_req);
  536.             break;
  537.             case PH_NM_GAD:
  538.             case PFX_GAD:   break;
  539.             default: 
  540.             if UP_DIAL_GAD {
  541.                i = GAD_LINE;
  542.                dial( i );
  543.                looping = FALSE;
  544.             }
  545.         } /* end switch */
  546.        } /* end while ( mes = */
  547.     }   /*  end while */
  548. }
  549.