home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / xc-4.1 / part01 / xcdial.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-13  |  6.5 KB  |  391 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 <string.h>
  8. #include <ctype.h>
  9. #include <sys/types.h>
  10. #include <termio.h>
  11. #include <fcntl.h>
  12. #include "xc.h"
  13.  
  14. #define NAME    25    /* display for system name */
  15. #define NUMBER    22    /*      "        "  phone number */
  16. #define BPS          5    /*      "        "  speed */
  17. #define PROTO     3    /*      "        "  protocol */
  18. #define SCRIPT    14    /*      "        "  script name */
  19.  
  20. static FILE *dirf;
  21. static long pages[57];    /* offsets into phonefile */
  22. static short dirnum, thispage, lastpage;
  23. static char *last_nbr = NIL(char);
  24. static char format[46];
  25. extern short s_flag;
  26. extern void cl_end(), newbmask();
  27.  
  28. struct {
  29.     char *speed;
  30. } speed[] = {
  31.     "300",
  32.     "600",
  33.     "1200",
  34.     "2400",
  35.     "4800",
  36.     "9600",
  37. #ifdef B19200
  38.     "19200",
  39. #endif
  40. #ifdef B38400
  41.     "38400",
  42.     "57600",
  43. #endif
  44.     NIL(char)
  45. };
  46.  
  47. struct {
  48.     char *proto;
  49. } proto[] = {
  50.     "8N1",
  51.     "7E2",
  52.     "7O2",
  53.     NIL(char)
  54. };
  55.  
  56. /*    show a single, formatted dialdir entry.
  57.     check its format integrity as we go
  58. */
  59. static void 
  60. showentry(choice, entry)
  61. short choice;
  62. char *entry;
  63. {
  64.     char name[NAME +1], num[NUMBER +1],
  65.         bps[BPS +1], prot[PROTO+1], script[SCRIPT +1];
  66.     char *s;
  67.     int i, j;
  68.  
  69.     s = entry;
  70.  
  71.     /* get phone number */
  72.     while (isspace(*s))
  73.         s++;
  74.  
  75.     for (i=0; i < NUMBER && !isspace(*s); i++, s++)
  76.         num[i] = *s;
  77.     num[i]='\0';
  78.  
  79.     /* get name */
  80.     while (!isspace(*s))
  81.         s++;
  82.     while (isspace(*s))
  83.         s++;
  84.     for (i=0; i < NAME && *s != '\t' && *s != '\n'; i++, s++)
  85.         name[i] = *s;
  86.     name[i] = '\0';
  87.  
  88.     /* get bps */
  89.     memset(bps,0,BPS+1);
  90.     if (s = strstr(entry, "BPS=")){
  91.         s += 4;
  92.         for (i=0; i < BPS && isdigit(*s); i++, s++)
  93.             bps[i] = *s;
  94.         for (i = 0, j = 0 ; speed[i].speed ; i++)
  95.                 if (!strcmp(bps,speed[i].speed))
  96.                     j++;
  97.         if (!j){
  98.             beep();
  99.             sprintf(Msg,"Invalid BPS= for '%s'",name);
  100.             S;
  101.             return;
  102.         }
  103.     }
  104.  
  105.     memset(prot,0,PROTO+1);
  106.     if (s = strstr(entry, "PROTO=")){
  107.         s += 6;
  108.         for (i=0; i < PROTO && isalnum(*s); i++, s++)
  109.             prot[i] = *s;
  110.         prot[i]='\0';
  111.         uc_word(prot);
  112.         for (i=0, j=0; proto[i].proto; i++)
  113.             if (!strcmp(prot,proto[i].proto))
  114.                 j++;
  115.         if (!j){
  116.             beep();
  117.             sprintf(Msg,"Invalid PROT= for '%s'",name);
  118.             S;
  119.             return;
  120.         }
  121.     }
  122.  
  123.     memset(script,0, SCRIPT+1);
  124.     if (s = strstr(entry, "SCRIPT=")){
  125.         s += 7;
  126.         for (i=0; i < SCRIPT && !isspace(*s); i++, s++)
  127.             script[i] = *s;
  128.     }
  129.     fprintf(tfp, format, choice, name, num, script, bps, prot);
  130. }
  131.  
  132. /* scroll directory at current filepos */
  133. static void
  134. scroll_dir()
  135. {
  136.     short i;
  137.     char buf[120];
  138.  
  139.     mode(OLDMODE);
  140.     ttgoto(4, 0);
  141.     cur_off();
  142.     cl_end();
  143.  
  144.     fseek(dirf, pages[thispage], 0),
  145.     dirnum = thispage * (LI - 6);
  146.     for (i=0; i < LI - 6; i++){
  147.         if (!fgets(buf, 120, dirf)){
  148.             lastpage = thispage;
  149.             break;
  150.         }
  151.         showentry(++dirnum, buf);
  152.     }
  153.  
  154.     pages[thispage + 1] = ftell(dirf);
  155.     if (!fgets(buf, 120, dirf))
  156.         lastpage = thispage;
  157.     cur_on();
  158.     mode(NEWMODE);
  159. }
  160.  
  161. /* Dial a phone number, using proper format and delay. */
  162. void
  163. xcdial(s)
  164. char *s;
  165. {
  166.     char buffer[SM_BUFF];
  167.  
  168.     if (last_nbr)
  169.         free(last_nbr);
  170.  
  171.     last_nbr = strdup(s);
  172.  
  173.     sprintf(buffer, DIALSTR, s);
  174.     send_string(buffer);
  175. }
  176.  
  177. static
  178. parse_entry(buf)
  179. char *buf;
  180. {
  181.     int i;
  182.     char *s, *t, *nbr, bps[BPS+1], prot[PROTO+1];
  183.  
  184.     if (s = strchr(buf,'\n'))
  185.         *s = '\0';
  186.  
  187.     if (s = strstr(buf, "BPS=")){
  188.         s += 4;
  189.         for (i=0; i < BPS && isdigit(*s); i++, s++)
  190.             bps[i] = *s;
  191.         bps[i]='\0';
  192.         if (!mrate(bps)){
  193.             S0("Invalid BPS=");
  194.             return FAILURE;
  195.         }
  196.     }
  197.  
  198.     if (s = strstr(buf, "PROTO=")){
  199.         s += 6;
  200.         for (i=0; i < PROTO && isalnum(*s); i++, s++)
  201.             prot[i] = *s;
  202.         prot[i]='\0';
  203.         uc_word(prot);
  204.         if (!xc_setproto(prot)){
  205.             S0("Invalid PROTO=");
  206.             return FAILURE;
  207.         }
  208.     }
  209.  
  210.     cls();
  211.     sprintf(Msg,"Calling %s",buf);
  212.     S;
  213.  
  214.     if (s = strstr(buf, "PREFIX="))
  215.         s += 7,
  216.         send_string("\r"),
  217.         send_string(s),
  218.         send_string("\r"),
  219.         s -= 7,
  220.         *s = '\0',
  221.         sleep(1);
  222.  
  223.     while (isspace(*buf) && *buf)
  224.         buf++;
  225.  
  226.     if (!(*buf))
  227.         return FAILURE;
  228.  
  229.     for (nbr = buf; !isspace(*buf) && *buf; buf++)
  230.         ;
  231.  
  232.     *buf = '\0';
  233.     xcdial(nbr);
  234.  
  235.     if (s = strstr(++buf, "SCRIPT=")){
  236.         s += 7;
  237.         t = s;
  238.         while (*t && !isspace(*t))
  239.             t++;
  240.         *t = '\0';
  241.         sprintf(ddsname,"%s",s);
  242.         s_flag = linkflag = TRUE;
  243.     }
  244.     return SUCCESS;
  245. }
  246.  
  247. static
  248. dial_entry(choice)
  249. short choice;
  250. {
  251.     char buf[120];
  252.  
  253.     if (!choice)
  254.         return FAILURE;
  255.     rewind(dirf);
  256.     while (choice--){
  257.         if (!fgets(buf, 120, dirf)){
  258.             S0("Nonexistent entry");
  259.             return FAILURE;
  260.         }
  261.     }
  262.     return (parse_entry(buf));
  263. }
  264.  
  265. static
  266. man_dial()
  267. {
  268.     ttgoto(LI-1, 0);
  269.     cl_end();
  270.     fputs("Number to dial: ",tfp);
  271.     getline();
  272.     if (!line[0])
  273.         return FAILURE;
  274.     return (parse_entry(line));
  275. }
  276.  
  277. dial_dir()
  278. {
  279.     int i, c;
  280.     char buf[5];
  281.  
  282.     if (!(dirf = openfile(phonefile))){
  283.         sprintf(Msg,"Phonelist '%s' not found",phonefile);
  284.         S;
  285.         return FAILURE;
  286.     }
  287.  
  288.     dirnum = thispage = 0;
  289.     lastpage = -1;
  290.     cls();
  291.     drawline(0, 0, CO);
  292.     ttgoto(1,(CO-strlen(phonefile))/2 -1);
  293.     show(-1,phonefile);
  294.     drawline(2, 0, CO);
  295.     ttgoto(3, 0);
  296.     sprintf(format,"     %%-%ds %%%ds %%-%ds %%%ds %%%ds%*s\n\r",
  297.         NAME, NUMBER, SCRIPT, BPS, PROTO,
  298.         CO-NAME-NUMBER-BPS-PROTO-SCRIPT-7, "");
  299.     sprintf(Msg, format, "NAME", "NUMBER", "SCRIPT", "BPS", "PRO");
  300.     show(-1,Msg);
  301.     sprintf(format,"%%3d - %%-%ds %%%ds %%-%ds %%%ds %%%ds\n\r",
  302.         NAME, NUMBER, SCRIPT, BPS, PROTO);
  303.     scroll_dir();
  304.     for (;;){
  305.         ttgoto(LI-1, 0);
  306.         fputs(
  307.         "==>     [#] Dial Entry   [M]anual Dial   [X]it   [N]ext   [P]revious",            tfp);
  308.         ttgoto(LI-1, 4);
  309.         while (1){
  310.             c = toupper(fgetc(stdin));
  311.             if (c == BS)
  312.                 continue;
  313.             if (c == 'N' || c == '\n' || c == ' '){
  314.                 if (thispage > (int)((1000/(LI-6))-1) || thispage == lastpage)
  315.                     S0("Last page");
  316.                 else
  317.                     thispage++,
  318.                     scroll_dir();
  319.                 break;
  320.             }
  321.             else if (c == 'P' && dirnum > 1){
  322.                 if (!thispage)
  323.                     S0("First page");
  324.                 else
  325.                     thispage--,
  326.                     scroll_dir();
  327.                 break;
  328.             }
  329.             else if (c == 'X'){
  330.                 cls();
  331.                 fclose(dirf);
  332.                 return FAILURE;
  333.             }
  334.             else if (c == 'M'){
  335.                 if (man_dial()){
  336.                     fclose(dirf);
  337.                     reterm = TRUE;
  338.                     return SUCCESS;
  339.                 }
  340.                 reterm = FALSE;
  341.                 break;
  342.             }
  343.             else if (isdigit(c)){
  344.                 buf[0] = c;
  345.                 fputc(c,tfp);
  346.                 for (i=1; i<4; ++i){
  347.                     buf[i] = getchar();
  348.                     if (buf[i]==BS){
  349.                         if (i>0)
  350.                             fputs("\b \b",tfp),
  351.                             i -= 2;
  352.                         else
  353.                             i = -1;
  354.                         continue;
  355.                     }
  356.                     fputc(buf[i],tfp);
  357.                     if (buf[i]=='\n' || buf[i]=='\r')
  358.                         break;
  359.                 }
  360.                 if (!i){
  361.                     reterm = FALSE;
  362.                     break;
  363.                 }
  364.                 buf[++i] = '\0';
  365.                 if (dial_entry(atoi(buf))){
  366.                     fclose(dirf);
  367.                     reterm = TRUE;
  368.                     return SUCCESS;
  369.                 }
  370.                 reterm = FALSE;
  371.                 break;
  372.             }
  373.         }
  374.     }
  375. }
  376.  
  377. redial()
  378. {
  379.     char *s;
  380.  
  381.     if (!last_nbr){
  382.         S1("REDIAL FAILURE");
  383.         return -1;
  384.     }
  385.  
  386.     s = strdup(last_nbr);
  387.     xcdial(s);
  388.     free(s);
  389.     return SUCCESS;
  390. }
  391.