home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / cpm / pcpursut / pisrc.ark / MENU.C < prev    next >
Text File  |  1988-07-14  |  8KB  |  322 lines

  1. /**********************************************************
  2. * menu.c - contains the menus for selecting city 
  3. * and bbs
  4. ********************************************************/
  5.  
  6. #include "stdio.h"
  7. #include "cpcp.h"
  8.  
  9. #define NUMLINES    13        /* number of lines in menu for city */
  10.                             /* limited by letters a-z as choices */
  11. #define ESC    27
  12. #define TAB    '\t'
  13. #define WIDTH    30        /* width for each city name */
  14. #define BACKSPACE    0x08
  15. #define DEL    0x7f
  16.  
  17. extern struct cityrec *cities;
  18. extern char *pad();
  19.  
  20. /* get pointer to city that has been chosen from list 
  21. * return null if ESC (exit) was selected
  22. * supports 2 page list for up to 52 cities
  23. */
  24. struct cityrec *citymenu(citylist)
  25. struct cityrec *citylist;    /* city list to use for menu */
  26. {
  27.     static struct cityrec *leftptr;
  28.     static struct cityrec *rtptr;
  29.     static struct cityrec *tempptr;
  30.     static int page_num = 0;
  31.     static int line_num;
  32.     static int i;
  33.     static char last_let;        /* last valid letter in the list */
  34.     static char c;
  35.     static char *(mesg[10]);
  36.  
  37.     line_num = 0;
  38.     last_let = 'a';        /* start at beginning of the list */
  39.  
  40.     /* get pointers to left and right side of list */
  41.     rtptr = leftptr = citylist;
  42.     for (i = 0; i < NUMLINES; i++)
  43.         if (rtptr)
  44.             rtptr = rtptr->nextcity;
  45.  
  46.     mesg[0] = "CITY SELECTION";
  47.     mesg[1] = NULL;
  48.     makehdr(mesg);
  49.     
  50.     while (line_num < NUMLINES) {
  51.         if (leftptr) {
  52.             printf("     %c-%s    ", line_num + 'A', pad(leftptr->cityname, WIDTH));
  53.             last_let++;
  54.             leftptr = leftptr->nextcity;;
  55.         }
  56.         if (rtptr) {
  57.             printf("%c-%s\n", line_num + 'A' + NUMLINES, pad(rtptr->cityname, WIDTH));
  58.             last_let++;
  59.             rtptr = rtptr->nextcity;;
  60.         }
  61.         else
  62.             printf("\n");
  63.         line_num++;
  64.     }
  65.  
  66.     printf("\n");
  67.     if (page_num)
  68.         printf("     Press the <TAB> key to see the first page of cities.\n");
  69.     else if (rtptr)
  70.         printf("     Press the <TAB> key to see second page of cities.\n");
  71.     printf("     Press the <ESC> key to disconnect and abort this program.\n");
  72.     /* check for more cites that can be displayed on two pages */
  73.     if (rtptr && page_num) {
  74.         printf("        !!!  To many cities in database !!!!\n");
  75.     }
  76.  
  77.     printf("\n     Enter the letter beside the");
  78.     printf("\n     city of your choice: -> ");
  79.  
  80.  
  81.     do {
  82.         c = tolower(getch());
  83.     } while (!((c >= 'a' && c < last_let) || ((c == TAB) && !page_num && rtptr) 
  84.             || ((c == TAB) && page_num) || (c == ESC)));
  85.  
  86.     if ( c >= 'a' && c < last_let)
  87.         putchar(toupper(c));
  88.  
  89.     /* check if page switch */
  90.     if (c == TAB) {
  91.         if (page_num) {
  92.             return (-1);
  93.         }
  94.         else {
  95.             page_num++;
  96.             tempptr = citymenu(rtptr);
  97.             page_num--;
  98.  
  99.             /* check if we want to put page 1 up again */
  100.             if (tempptr == -1)
  101.                 return(citymenu(citylist));
  102.             else
  103.                 return (tempptr);
  104.         }
  105.     }
  106.     else if (c == ESC) 
  107.         return (NULL);
  108.     else {
  109.         tempptr = citylist;
  110.         for (i = 0; i < c - 'a'; i++)
  111.             tempptr = tempptr->nextcity;
  112.         return (tempptr);
  113.     }
  114. }
  115.  
  116.     
  117.  
  118. /* display menu for bbs selection ,
  119.  * return NULL if Escape pressed and support 2 pages
  120.  *  */
  121. struct bbs_rec *bbsmenu(bbslist, titlestring, area_code)
  122. struct bbs_rec *bbslist;
  123. char *titlestring;
  124. char *area_code;
  125. {
  126.  
  127.  
  128.     static struct  bbs_rec *leftptr;
  129.     static struct  bbs_rec *rtptr;
  130.     static struct  bbs_rec *tempptr;
  131.     static int page_num = 0;
  132.     static int line_num;
  133.     static int i;
  134.     static char last_let;        /* last valid letter in the list */
  135.     static char c;
  136.     static char *sptr;
  137.     static char *(mesg[10]);
  138.     static char *string;
  139.     char str[80];
  140.  
  141.     string = str;
  142.     line_num = 0;
  143.     last_let = 'a';        /* start at beginning of the list */
  144.  
  145.     /* get pointers to left and right side of list */
  146.     rtptr = leftptr = bbslist;
  147.     for (i = 0; i < NUMLINES; i++)
  148.         if (rtptr)
  149.             rtptr = rtptr->next_bbs;
  150.  
  151.     sprintf(string, "BBS list for %s.", titlestring);
  152.     mesg[0] = string;
  153.     mesg[1] = NULL;
  154.     makehdr(mesg);
  155.     
  156.     printf("\n");
  157.     while (line_num < NUMLINES) {
  158.         if (leftptr) {
  159.             printf("     %c-%s    ", line_num + 'A', pad(leftptr->bbs_name, WIDTH));
  160.             last_let++;
  161.             leftptr = leftptr->next_bbs;;
  162.         }
  163.  
  164.         if (rtptr) {
  165.             printf("%c-%s\n", line_num + 'A' + NUMLINES, pad(rtptr->bbs_name, WIDTH));
  166.             last_let++;
  167.             rtptr = rtptr->next_bbs;;
  168.         }
  169.         else
  170.             printf("\n");
  171.         line_num++;
  172.     }
  173.  
  174.     printf("\n");
  175.     if (page_num)
  176.         printf("     Press the <TAB> key to see the first page of bbs's.\n");
  177.     else if (rtptr)
  178.         printf("     Press the <TAB> key to see second page of bbs's.\n");
  179.     printf("     Press the <ESC> key to return to Cities Menu.\n");
  180.     printf("     Press the </> key for manual dial entry.\n");
  181.  
  182.     /* check for more cites that can be displayed on two pages */
  183.     if (rtptr && page_num) {
  184.         printf("        !!!  To many phone numbers in database for this city!!!!\n");
  185.     }
  186.  
  187.     /* prompt for selection */
  188.     printf("\n     Enter the letter beside the                      \n");
  189.     printf("     bbs of your choice -> ");
  190.  
  191.  
  192.  
  193.     do {
  194.         c = tolower(getch());
  195.     } while (!((c >= 'a' && c < last_let) || ((c == TAB) && !page_num && rtptr) 
  196.             || ((c == TAB) && page_num) || (c == ESC) || (c == '/')));
  197.  
  198.     if ( c >= 'a' && c < last_let)
  199.         putchar(toupper(c));
  200.  
  201.     /* check if page switch */
  202.     if (c == TAB) {
  203.         if (page_num) {
  204.             return (-1);
  205.         }
  206.         else {
  207.             page_num++;
  208.             tempptr = bbsmenu(rtptr, titlestring, area_code);
  209.             page_num--;
  210.  
  211.             /* check if we want to put page 1 up again */
  212.             if (tempptr == -1)
  213.                 return(bbsmenu(bbslist, titlestring, area_code));
  214.             else
  215.                 return (tempptr);
  216.         }
  217.     }
  218.     else if (c == ESC) 
  219.         return (NULL);
  220.     else if (c == '/') {
  221.         if (tempptr = mdial())
  222.             return(tempptr);
  223.         else
  224.             return(bbsmenu(bbslist, titlestring, area_code)); /* <- recursive so watch stack */
  225.     }
  226.     else {
  227.         tempptr = bbslist;
  228.         for (i = 0; i < c - 'a'; i++)
  229.             tempptr = tempptr->next_bbs;
  230.         return (tempptr);
  231.     }
  232.  
  233. }
  234.  
  235.  
  236. /* prompt user for manual dial number */
  237. struct bbs_rec *mdial()
  238. {
  239.     static char areacode[4] =  "???";
  240.     static int count;
  241.     static int c;
  242.     static char *(mesg[10]);
  243.     static struct bbs_rec bbs;
  244.     static char *prompt = "Enter number to be dialed -> ";
  245.     static char temp[15];
  246.  
  247.     count = 0;
  248.     mesg[0] = "Manual Dial Mode";
  249.     mesg[1] = " ";
  250.     mesg[2] = "Press Escape to abort or Return to enter Number";
  251.     mesg[3] = NULL;
  252.     makehdr(mesg);
  253.  
  254.  
  255.     
  256.     printf("\n");
  257.     printf(prompt);
  258.     printf("%3s-", areacode);
  259.     printf("___-____\015");
  260.     printf(prompt);
  261.     printf("%3s-", areacode);
  262.     strncpy(bbs.bbs_number, areacode, 3);
  263.  
  264.     count = 3;
  265.     for (;;) {
  266.         c = getch();    
  267.         if (c == ESC)
  268.             return (NULL);
  269.         else if (c == '\015' && count == 10) {
  270.             bbs.bbs_name = "Manual Dial";
  271.             bbs.next_bbs = NULL;
  272.             if (!strncmp(areacode, &(bbs.bbs_number[0]), 3)) {
  273.                 strncpy(&(bbs.bbs_number[0]), &(bbs.bbs_number[3]), 7);
  274.                 bbs.bbs_number[7] = NULL;
  275.             }
  276.             else bbs.bbs_number[10] = NULL;
  277.                 
  278.             /* check for 1 at beginning of area code */
  279.             printf("\n\n     Add 1 to begining of area code? ");
  280.             c = getch();
  281.             if (toupper(c) == 'Y') {
  282.                 strcpy(temp, bbs.bbs_number);
  283.                 strcpy(bbs.bbs_number, "1");
  284.                 strcat(bbs.bbs_number, temp);
  285.             }
  286.  
  287.             printf("\n\n     1 - 300\n");
  288.             printf("     2 - 1200\n");
  289.             printf("     3 - 2400\n");
  290.             printf("Enter baud rate (1-3) -> ");
  291.             do {
  292.                 c = getch();
  293.             } while ((c < '1') && (c > '3'));
  294.             putchar(c);
  295.             bbs.bbs_baud = c - '0'; 
  296.             return(&bbs);
  297.         } else if (isdigit(c) && count < 10) {
  298.             bbs.bbs_number[count] = c;
  299.             putchar(c);
  300.             count++;
  301.             if ((count == 3) || (count == 6))
  302.                 putchar('-');
  303.         } else if (c == BACKSPACE || c == DEL) {
  304.             if (count == 0)
  305.                 ;
  306.             else if ((count == 3) || (count == 6)) {
  307.                 putchar(BACKSPACE);
  308.                 putchar(BACKSPACE);
  309.                 putchar('_');
  310.                 putchar(BACKSPACE);
  311.                 count--;
  312.             } else {
  313.                 putchar(BACKSPACE);
  314.                 putchar('_');
  315.                 putchar(BACKSPACE);
  316.                 count--;
  317.             }
  318.         }
  319.     }
  320.     
  321. }
  322.