home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3598 / xcdial.c next >
Encoding:
C/C++ Source or Header  |  1991-07-11  |  6.3 KB  |  364 lines

  1. /*    xcdial.c -- dialing directory module for XC
  2.     This file uses 4-character tabstops
  3.     Author: Steve Manes 8/26/88
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <ctype.h>
  8. #include <sys/types.h>
  9. #include <fcntl.h>
  10. #include "xc.h"
  11.  
  12. #define MAXNAME        27    /* maximum display for system name */
  13. #define MAXNUMBER    20    /*      "          "        "  phone number */
  14. #define MAXPROTOCOL     9    /*      "          "        "  protocol string */
  15. #define MAXSCRIPT    14    /*      "          "        "  script name */
  16.  
  17. static FILE *dirf;
  18. static long pages[57];    /* offsets into phonefile */
  19. static short dirnum, thispage, lastpage;
  20. static char *last_nbr = NULLS;
  21. extern short s_flag;
  22. extern void cl_end(), newbmask();
  23.  
  24. /*    showentry(dirnum, entry)
  25.     show a single, formatted dialdir entry.
  26.     check its format integrity as we go
  27. */
  28. static void showentry(choice, entry)
  29. short choice;
  30. char *entry;
  31. {
  32.     char name[MAXNAME +1], num[MAXNUMBER +1], f[30],
  33.         protocol[MAXPROTOCOL +1], script[MAXSCRIPT +1];
  34.     char *s;
  35.     short i, j = 0;
  36.  
  37.     s = entry;
  38.  
  39.     /* get phone number */
  40.     while (isspace(*s))
  41.         s++;
  42.  
  43.     for (i=0; i < MAXNUMBER && !isspace(*s); i++)
  44.         num[j++] = *s++;
  45.     num[j] = '\0';
  46.  
  47.     /* get name */
  48.     while (!isspace(*s))
  49.         s++;
  50.     while (isspace(*s))
  51.         s++;
  52.     j=0;
  53.     for (i=0; i < MAXNAME && *s != '\t'; i++)
  54.         name[j++] = *s++;
  55.     name[j] = '\0';
  56.     s = strchr(name,'\n');
  57.     if (s)
  58.         *s = '\0';
  59.  
  60.     /* get protocol */
  61.     if (s = strstr(entry, "BPS=")) {
  62.         s += 4;
  63.         j=0;
  64.         for (i=0; i < 6 && isdigit(*s); i++)
  65.             protocol[j++] = *s++;
  66.         protocol[j] = '\0';
  67.         strcat(protocol, "/");
  68.     }
  69.     else
  70.         sprintf(protocol,"????%c",'/');
  71.  
  72.     if (s = strstr(entry, "BITS=")) {
  73.         s += 5;
  74.         switch (*s) {
  75.             case '7':
  76.                 strcat(protocol, "7/");
  77.                 break;
  78.             case '8':
  79.                 strcat(protocol, "8/");
  80.                 break;
  81.             default:
  82.                 beep();
  83.                 sprintf(Msg,"Invalid BITS= for '%s'",name);
  84.                 S;
  85.                 strcat(protocol,"8/N");
  86.                 return;
  87.         }
  88.     }
  89.     else
  90.         strcat(protocol,"?/");
  91.  
  92.     if (*s == '7')
  93.         strcat(protocol, "E");
  94.     else
  95.         strcat(protocol, "N");
  96.  
  97.     if (s = strstr(entry, "SCRIPT=")) {
  98.         s += 7;
  99.         j=0;
  100.         for (i=0; i < MAXSCRIPT && !isspace(*s); i++)
  101.             script[j++] = *s++;
  102.         script[j] = '\0';
  103.  
  104.     }
  105.     else
  106.         script[0] = '\0';
  107.  
  108.     sprintf(f,"%%3d - %%-%ds %%%ds %%%ds %%-%ds\n",
  109.         MAXNAME, MAXNUMBER, MAXPROTOCOL, MAXSCRIPT);
  110.     fprintf(tfp, f, choice, name, num, protocol, script);
  111. }
  112.  
  113. /*    scroll_dir()
  114.     scroll directory at current filepos
  115. */
  116. static void scroll_dir()
  117. {
  118.     short i;
  119.     char buff[121];
  120.  
  121.     ttgoto(4, 0);
  122.     cur_off();
  123.     cl_end();
  124.  
  125.     fseek(dirf, pages[thispage], 0),
  126.     dirnum = thispage * (LI - 6);
  127.     for (i=0; i < LI - 6; i++) {
  128.         if (fgets(buff, 120, dirf) == NULLS) {
  129.             lastpage = thispage;
  130.             break;
  131.         }
  132.         showentry(++dirnum, buff);
  133.     }
  134.  
  135.     pages[thispage + 1] = ftell(dirf);
  136.     if (fgets(buff, 120, dirf) == NULLS)
  137.         lastpage = thispage;
  138.     cur_on();
  139. }
  140.  
  141. /* Dial a phone number, using proper format and delay. */
  142. void xcdial(s)
  143. char *s;
  144. {
  145.     char buffer[SM_BUFF];
  146.  
  147.     if (last_nbr)
  148.         free(last_nbr);
  149.  
  150.     last_nbr = strdup(s);
  151.  
  152.     sprintf(buffer, DIALSTR, s);
  153.     send_slowly(buffer);
  154. }
  155.  
  156. static void parse_entry(buf)
  157. char *buf;
  158. {
  159.     char *s, *t, *nbr;
  160.  
  161.     if (s = strstr(buf, "BPS=")) {
  162.         s += 4;
  163.         if (mrate(s) < 0) {
  164.             show(0,"Invalid BPS=");
  165.             return;
  166.         }
  167.     }
  168.     if (s = strstr(buf, "BITS=")) {
  169.         s += 5;
  170.         switch (*s) {
  171.             case '7':
  172.                 if (bitmask == 0xff)
  173.                     newbmask();
  174.                 break;
  175.             case '8':
  176.                 if (bitmask == 0x7f)
  177.                      newbmask();
  178.                 break;
  179.             default:
  180.                 show(0,"Invalid BITS=");
  181.                 return;
  182.         }
  183.     }
  184.  
  185.     cls();
  186.     if ((s = strstr(buf, "PREFIX=")) != NULLS) {
  187.         s += 7;
  188.         send_slowly("\r");
  189.         send_slowly(s);
  190.         send_slowly("\r");
  191.         s -= 7;
  192.         *s = '\0';
  193.         sleep(2);
  194.     }
  195.     sprintf(Msg,"Calling %s",buf);
  196.     S;
  197.  
  198.     while (isspace(*buf) && *buf)
  199.         buf++;
  200.  
  201.     if (!(*buf))
  202.         return;
  203.  
  204.     for (nbr = buf; !isspace(*buf) && *buf; buf++)
  205.         ;
  206.  
  207.     *buf = '\0';
  208.     intdel(TRUE);
  209.     xcdial(nbr);
  210.  
  211.     if (s = strstr(++buf, "SCRIPT=")) {
  212.         s += 7;
  213.         t = s;
  214.         while (!isspace(*t) && *t != '\0')
  215.             t++;
  216.         *t = '\0';
  217.         sprintf(ddsname,"%s",s);
  218.         s_flag = linkflag = TRUE;
  219.     }
  220.     intdel(FALSE);
  221. }
  222.  
  223. dial_dir()
  224. {
  225.     int i, c;
  226.     char buff[5], f[SM_BUFF];
  227.  
  228.     if ((dirf = openfile(phonefile)) == NULLF) {
  229.         sprintf(Msg,"Phonelist '%s' not found",phonefile);
  230.         S;
  231.         return FAILURE;
  232.     }
  233.  
  234.     dirnum = thispage = 0;
  235.     lastpage = -1;
  236.     cls();
  237.     drawline(0, 0, CO);
  238.     ttgoto(1, 23);
  239.     show(-1," D I A L I N G   D I R E C T O R Y ");
  240.     drawline(2, 0, CO);
  241.     ttgoto(3, 0);
  242.     sprintf(f,"     %%-%ds %%%ds %%%ds %%-%ds%*s\n",
  243.         MAXNAME, MAXNUMBER, MAXPROTOCOL, MAXSCRIPT,
  244.         CO-MAXNAME-MAXNUMBER-MAXPROTOCOL-MAXSCRIPT-8, "");
  245.     sprintf(Msg, f, "NAME", "NUMBER", "PROTOCOL", "SCRIPT");
  246.     show(-1,Msg);
  247.     scroll_dir();
  248.     while (1) {
  249.         ttgoto(LI-1, 0);
  250.         fprintf(tfp,
  251.         "==>     [#] Dial Entry   [M]anual Dial   [X]it   [N]ext   [P]revious");
  252.         ttgoto(LI-1, 4);
  253.         while (1) {
  254.             c = toupper(dgetch());
  255.             if (c == '\b')
  256.                 continue;
  257.             if (c == 'N' || c == '\n' || c == ' ') {
  258.                 if (thispage > (int)((1000/(LI-6))-1) || thispage == lastpage)
  259.                     show(0,"Last page");
  260.                 else
  261.                     thispage++,
  262.                     scroll_dir();
  263.                 break;
  264.             }
  265.             else if (c == 'P' && dirnum > 1) {
  266.                 if (thispage == 0)
  267.                     show(0,"First page");
  268.                 else
  269.                     thispage--,
  270.                     scroll_dir();
  271.                 break;
  272.             }
  273.             else if (c == 'X') {
  274.                 cls();
  275.                 fclose(dirf);
  276.                 return FAILURE;
  277.             }
  278.             else if (c == 'M') {
  279.                 if (man_dial()) {
  280.                     fclose(dirf);
  281.                     reterm = TRUE;
  282.                     return SUCCESS;
  283.                 }
  284.                 reterm = FALSE;
  285.                 break;
  286.             }
  287.             else if (isdigit(c)) {
  288.                 buff[0] = c;
  289.                 fputc(c,tfp);
  290.                 for (i=1; i<4; ++i) {
  291.                     buff[i] = getchar();
  292.                     if (buff[i]=='\b') {
  293.                         if (i>0)
  294.                             fprintf(tfp,"\b \b"),
  295.                             i -= 2;
  296.                         else
  297.                             i = -1;
  298.                         continue;
  299.                     }
  300.                     fputc(buff[i],tfp);
  301.                     if (buff[i]=='\n' || buff[i]=='\r')
  302.                         break;
  303.                 }
  304.                 if (i == 0) {
  305.                     reterm = FALSE;
  306.                     break;
  307.                 }
  308.                 buff[++i] = '\0';
  309.                 if (dial_entry(atoi(buff))) {
  310.                     fclose(dirf);
  311.                     reterm = TRUE;
  312.                     return SUCCESS;
  313.                 }
  314.                 reterm = FALSE;
  315.                 break;
  316.             }
  317.         }
  318.     }
  319. }
  320. static dial_entry(choice)
  321. short choice;
  322. {
  323.     char buff[121];
  324.  
  325.     if (choice == 0)
  326.         return FAILURE;
  327.     rewind(dirf);
  328.     while (choice--) {
  329.         if (fgets(buff, 120, dirf) == NULLS) {
  330.             show(0,"Nonexistent entry");
  331.             return FAILURE;
  332.         }
  333.     }
  334.     parse_entry(buff);
  335.     return SUCCESS;
  336. }
  337.  
  338. static man_dial()
  339. {
  340.     ttgoto(LI-1, 0);
  341.     cl_end();
  342.     fprintf(tfp,"Number to dial: ");
  343.     getline();
  344.     if (!line[0])
  345.         return FAILURE;
  346.     parse_entry(line);
  347.     return SUCCESS;
  348. }
  349.  
  350. redial()
  351. {
  352.     char *s;
  353.  
  354.     if (last_nbr == NULLS) {
  355.         show(1,"REDIAL FAILURE");
  356.         return -1;
  357.     }
  358.  
  359.     s = strdup(last_nbr);
  360.     xcdial(s);
  361.     free(s);
  362.     return 0;
  363. }
  364.