home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / BBS / MISC / XSRC_117.ZIP / XMENU.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-25  |  11.2 KB  |  327 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*   XBBS SOURCE CODE copyright (c) 1990 by M. Kimes                        */
  4. /*   All Rights Reserved                                                    */
  5. /*                                                                          */
  6. /*    For complete details of the licensing restrictions, please refer      */
  7. /*    to the License agreement, which is published in its entirety in       */
  8. /*    the in the file LICENSE.XBS.                                          */
  9. /*                                                                          */
  10. /*    USE OF THIS FILE IS SUBJECT TO THE RESTRICTIONS CONTAINED IN THE      */
  11. /*    XBBS LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF            */
  12. /*    THIS AGREEMENT IN ANY OF THE AFOREMENTIONED FILES, OR IF YOU DO       */
  13. /*    NOT HAVE THESE FILES, YOU SHOULD IMMEDIATELY CONTACT M. KIMES         */
  14. /*    AT THE ADDRESS LISTED BELOW.  IN NO EVENT SHOULD YOU PROCEED TO USE   */
  15. /*    THIS FILE WITHOUT HAVING ACCEPTED THE TERMS OF THE XBBS LICENSING     */
  16. /*    AGREEMENT, OR SUCH OTHER AGREEMENT AS YOU ARE ABLE TO REACH WITH      */
  17. /*    M. KIMES                                                              */
  18. /*                                                                          */
  19. /*                                                                          */
  20. /* You can contact M. Kimes at the following address:                       */
  21. /*                                                                          */
  22. /* M. Kimes                         1:380/16.0@FidoNet                      */
  23. /* 542 Merrick                      (318)222-3455 data                      */
  24. /* Shreveport, LA  71104                                                    */
  25. /*                                                                          */
  26. /*                                                                          */
  27. /* Please feel free to contact me at any time to share your comments about  */
  28. /* my software and/or licensing policies.                                   */
  29. /*                                                                          */
  30. /*--------------------------------------------------------------------------*/
  31. /****************************************************************************/
  32. /*                      XMenu.C (Menu handler)                              */
  33. /*                                                                          */
  34. /* Menu format (flat ASCII file):                                           */
  35. /*      First line:    Menu ID (not used by XBBS)                           */
  36. /*      Second line:   Prompt for this menu                                 */
  37. /*      Third line:    Help prompt # in XBBS.TXT (or 0) for this menu       */
  38. /*      Fourth line:   Color for prompt this menu                           */
  39. /*      Fifth line:    Prompt for this entry                                */
  40. /*      Sixth line:    Key for this entry                                   */
  41. /*      Seventh line:  Action for this entry                                */
  42. /*      Eighth line:   Security masks for this entry                        */
  43. /*      Ninth line:    Bitmask for this entry                               */
  44. /*      Tenth line:    Age mask for this entry                              */
  45. /*      Eleventh line: Misc data for this entry                             */
  46. /*      Twelvth line:  Color for this entry                                 */
  47. /*      Thirteenth line...nth line: Repeat from fifth                       */
  48. /* There can be a maximum of 50 entries per menu                            */
  49. /* If a line is blank, it must still be included                            */
  50. /* This sorta-ASCII format should make it easy for anyone to write a menu   */
  51. /* editor or generator even in QuickBASIC or XBASIC.  There will be a slight*/
  52. /* sacrifice in speed when XBBS first loads the menu; should be negligible. */
  53. /****************************************************************************/
  54.  
  55. #include "msg.h"
  56. #include "xext.h"
  57.  
  58.     struct _menu {
  59.         char prompt[81];
  60.         char key;
  61.         char action;
  62.         char data[133];
  63.         char color;
  64.     };
  65.  
  66.  
  67. char pascal do_hot (struct _menu **m,char num,char *hotone);
  68. char pascal do_item (struct _menu **m,char which);
  69.  
  70.  
  71.  
  72. void pascal xmenu (char *menuname) {
  73.  
  74.     struct _menu *m[50];
  75.     char num,x,keyin;
  76.     int handle,test;
  77.     register int y;
  78.     char s[133];
  79.     char *p;
  80.     char prompt[81];
  81.     word mhelpnum;
  82.     char promptcolor;
  83.     char hotone;
  84.     struct _color {
  85.         bit fore:   3;
  86.         bit flash:  1;
  87.         bit back:   3;
  88.         bit hi:     1;
  89.     };
  90.     union __COLOR__ {
  91.         struct _color c;
  92.         char cl;
  93.     } cr;
  94.  
  95.     for(y=0;y<50;y++) m[y]=NULL;
  96.  
  97. ReStart:
  98.  
  99.     for(y=0;y<50;y++) {
  100.         if(m[y])ffree(m[y]);
  101.         m[y]=NULL;
  102.     }
  103.  
  104.     handle=oopen(menuname,O_RDONLY | O_BINARY | O_DENYWRITE);
  105.     if(handle==-1) goto OutAHere;
  106.     num=0;
  107.  
  108.     if(!fgetsx(s,133,handle)) {
  109.         cclose(handle);
  110.         goto OutAHere;
  111.     }
  112.     if(!fgetsx(s,133,handle)) {
  113.         cclose(handle);
  114.         goto OutAHere;
  115.     }
  116.     s[80]=0;
  117.     stripcr(s);
  118.     strcpy(prompt,s);
  119.     if(!fgetsx(s,133,handle)) {
  120.         cclose(handle);
  121.         goto OutAHere;
  122.     }
  123.     mhelpnum=(word)atol(s);
  124.     if(!fgetsx(s,133,handle)) {
  125.         cclose(handle);
  126.         goto OutAHere;
  127.     }
  128.     promptcolor=(char)atoi(s);
  129.  
  130.     while(num<50 && !eof(handle)) {     /* Load up to 50 menu items */
  131. Continue:
  132.         if(m[num]==NULL) {
  133.             m[num]=(struct _menu *)mmalloc(sizeof(struct _menu));
  134.             if(m[num]==NULL) break;
  135.         }
  136.         if(!fgetsx(s,133,handle)) break;
  137.         stripcr(s);
  138.         s[79]=0;
  139.         strcpy(m[num]->prompt,s);
  140.         x=strlen(m[num]->prompt);
  141.         if(m[num]->prompt[x-1]==';') {
  142.             m[num]->prompt[x-1]=0;
  143.         }
  144.         else strcat(m[num]->prompt,"\n");
  145.         if(!fgetsx(s,133,handle)) break;
  146.         stripcr(s);
  147.         m[num]->key=toupper(*s);
  148.         if(!fgetsx(s,133,handle)) break;
  149.         m[num]->action=(char)atoi(s);
  150.    /* Security checking for this item */
  151.         if(!fgetsx(s,133,handle)) break;
  152.         s[132]=0;
  153.         stripcr(s);
  154.         p=strtok(s," ");
  155.         if(p) {
  156.             keyin=0;
  157.             do {
  158.                 if(keyin==10) break;
  159.                 if((word)atol(p)>user.stat[keyin]) goto Continue;
  160.                 keyin++;
  161.             } while(p=strtok(0," "));
  162.         }
  163.         if(!fgetsx(s,133,handle)) break;
  164.         test=(word)atol(s);
  165.         if(test) {
  166.             for(y=0;y<16;y++) {
  167.                 if(test & (1<<y)) {
  168.                     if(!(user.attr2 & (1<<y))) {
  169.                         goto Continue;
  170.                     }
  171.                 }
  172.             }
  173.         }
  174.         if(!fgetsx(s,133,handle)) break;
  175.         test=atoi(s);
  176.         if(test) {
  177.             if(test<0) {
  178.                 if(age>(char)test) continue;
  179.             }
  180.             else {
  181.                 if(age<(char)test) continue;
  182.             }
  183.         }
  184.    /* End security checking--if he got here, he has access */
  185.         if(!fgetsx(s,133,handle)) break;
  186.         s[132]=0;
  187.         stripcr(s);
  188.         strcpy(m[num]->data,s);
  189.         if(!fgetsx(s,133,handle)) break;
  190.         m[num]->color=(char)atoi(s);
  191.         num++;
  192.     }
  193.     cclose(handle);
  194.     if(!num) {
  195. OutAHere:
  196.         for(y=0;y<50;y++) {
  197.             if(m[y]) ffree(m[y]);
  198.         }
  199.         return;
  200.     }
  201. Loop:
  202.     cls();
  203.     pauser=0;
  204.     if(m[0]->key!=0 && !user.cold) {
  205.         keyin=do_hot(m,num,&hotone);
  206.         if(keyin==255) goto OutAHere;
  207.         if(keyin==1) {
  208.             menuname=m[hotone]->data;
  209.             goto ReStart;
  210.         }
  211.         if(keyin!=0) goto Loop;
  212.     }
  213.     if(m[0]->key==0) {
  214.         keyin=do_item(m,0);
  215.         if(keyin==255) goto OutAHere;
  216.         if(keyin==1) {
  217.             menuname=m[0]->data;
  218.             goto ReStart;
  219.         }
  220.     }
  221.     for(x=0;x<num;x++) {
  222.  
  223.         if(m[x]->color && user.graphics) {
  224.  
  225.                 /* Translate 1-byte color code to ANSI string.  Note that a 0
  226.                    byte skips sending a string (so common-colored prompts
  227.                    need not waste time resending the same old string).  Also
  228.                    note that, therefore, one color combination is unavailable:
  229.                    dim black-on-black.  Somehow I imagine you'll be able to
  230.                    live without it.
  231.                    See union __COLOR__ definition above for how the bits
  232.                    in the char are used.
  233.                    Note that while I used a union, similar results can be
  234.                    obtained by bit-shifting or selective and/or/not usage.
  235.                 */
  236.  
  237.             char tempfore[8];
  238.             char tempback[8];
  239.  
  240.             cr.cl=m[x]->color;
  241.             if(cr.c.hi) strcpy(s,"\x1b[0;1;");
  242.             else strcpy(s,"\x1b[0;2;");
  243.             if(cr.c.flash) strcat(s,"5;");
  244.             sprintf(tempfore,"%u;",cr.c.fore+30);
  245.             sprintf(tempback,"%um",cr.c.back+40);
  246.             strcat(s,tempfore);
  247.             strcat(s,tempback);
  248.             printm(s);
  249.         }
  250.  
  251.         if(*m[x]->prompt) printm(convertstring(m[x]->prompt));
  252.         if(m[0]->key!=0 && !user.cold) {
  253.             keyin=do_hot(m,num,&hotone);
  254.             if(keyin==255) goto OutAHere;
  255.             if(keyin==1) {
  256.                 menuname=m[hotone]->data;
  257.                 goto ReStart;
  258.             }
  259.             if(keyin!=0) goto Loop;
  260.         }
  261.         if(x<num) {
  262.             if(m[x+1]->key==0) {
  263.                 keyin=do_item(m,x+1);
  264.                 if(keyin==255) goto OutAHere;
  265.                 if(keyin==1) {
  266.                     menuname=m[x+1]->data;
  267.                     goto ReStart;
  268.                 }
  269.             }
  270.         }
  271.     }
  272.     if(promptcolor && user.graphics) {
  273.             /* Translate 1-byte color code to ANSI */
  274.  
  275.         char tempfore[8];
  276.         char tempback[8];
  277.  
  278.         cr.cl=promptcolor;
  279.         if(cr.c.hi) strcpy(s,"\x1b[0;1;");
  280.         else strcpy(s,"\x1b[0;2;");
  281.         if(cr.c.flash) strcat(s,"5;");
  282.         sprintf(tempfore,"%u;",cr.c.fore+30);
  283.         sprintf(tempback,"%um",cr.c.back+40);
  284.         strcat(s,tempfore);
  285.         strcat(s,tempback);
  286.         printm(s);
  287.     }
  288.     gprintf(0,"%s ",convertstring(prompt));
  289. ReLoop:
  290.     helpnum=mhelpnum;
  291.     keyin=toupper(*genin(1,0,1,1,ALLL));
  292.     helpnum=0;
  293.     if(keyin==0) goto Loop;
  294.     for(x=0;x<num;x++) {
  295.         if(keyin==m[x]->key) {
  296.             keyin=do_item(m,x);
  297.             if(keyin==1) {
  298.                 menuname=m[x]->data;
  299.                 goto ReStart;
  300.             }
  301.             if(keyin==255) goto OutAHere;
  302.             goto Loop;
  303.         }
  304.     }
  305.     printm(BACKSPACE);
  306.     goto ReLoop;
  307. }
  308.  
  309.  
  310. char pascal do_hot (struct _menu **m,char num,char *hotone) {
  311.  
  312.     char keyin;
  313.     register int x;
  314.  
  315.     keyin=toupper(inkey());  /* Check for hot keypress */
  316.     if(keyin) {
  317.         for(x=0;x<num;x++) {
  318.             if(keyin==m[x]->key) {
  319.                 *hotone=(char)x;
  320.                 return do_item(m,x);
  321.             }
  322.         }
  323.     }
  324.     return 0;
  325. }
  326.  
  327.