home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume32 / xbbs / part06 / bbscqust.c < prev    next >
C/C++ Source or Header  |  1992-09-08  |  11KB  |  320 lines

  1. #include <string.h>
  2. #include <ctype.h>
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <sys/locking.h>
  6. #include <sys/stat.h>
  7. #include "bbscdef.h"
  8. struct stat statbuf;
  9. char QST_FILE[50];
  10. /*
  11.     The following questionnaire commands are available for usage:
  12.     Command           Description
  13.       !               Output the rest of the line and wait for an input
  14.       ^               Same as '!' except strlen of input must be > 0
  15.       @               Output the rest of the line and send a \n\r
  16.       &               Same as '@' without a terminal \n\r
  17.       $               Input line
  18.       *               Same as '$' except strlen of input must be > 0
  19.       %               Output \n\r
  20.       Y || y          Output line and set flag true if answer is a Y or y
  21.       N || n          Output line and set flag true if answer is an N or n
  22.       {               Start conditional response based on flag = true
  23.       }               End conditional response
  24.       /               Exit questionnaire - normally used as a conditional
  25.                       statement
  26.  
  27.       The COMMAND is in column one of line
  28. */
  29. question(typeofqst)
  30. int typeofqst;
  31. {
  32.         int result, fds, var;
  33.         char *ptr;
  34.         var = FALSE;
  35.     result=change_qst(0, typeofqst);
  36.         if( result != 0)
  37.         {
  38.                 portsout("\n\rThere is NO questionnaire today!\n\r");
  39.                 return;
  40.         }
  41.         if((inbuf=fopen(QST_FILE,"r")) == NULL)
  42.         {
  43.                 portsout("\n\rThat questionnaire was not found -- notify the sysop!\n\r");
  44.                 return;
  45.         }
  46.         if((otbuf=fopen(ANSWER,"a+")) == NULL)
  47.         {
  48.                 portsout("\n\rError opening answer file!\n\r");
  49.                 exit (1);
  50.         }
  51.         fds = fileno(otbuf);
  52.         rewind (otbuf);
  53.         locking(fds, LK_LOCK, 0L);
  54.     lseek(fds, 0L, 2);
  55.         fprintf(otbuf,"%s\n","***********************************************");
  56.         fprintf(otbuf, "%s %s    %s %s %s\n", w_fname, w_lname, week, date, ttime);
  57. next_qloop:
  58.         while ((fgets(buf128, 80, inbuf)) != NULL )
  59.         {
  60.                 strip(buf128);
  61.                 if(buf128[0] == '$')
  62.                 {
  63.                         portsout("\n\r? ");
  64.                         portsin(buf128,80);
  65.                         fprintf(otbuf, "%s\n", buf128);
  66.                         continue;
  67.                 }
  68.                 if(buf128[0] == '*')
  69.                 {
  70.             while (1) {
  71.                                portsout("\n\r? ");
  72.                                portsin(buf128,80);
  73.                 if( strlen(buf128) > 0 )
  74.                     break;
  75.                 portsout("\n\rInput is required.");
  76.             }
  77.             
  78.                            fprintf(otbuf, "%s\n", buf128);
  79.                         continue;
  80.                 }
  81.                 if(buf128[0] == '!')
  82.                 {
  83.                         ptr = buf128 + 1;
  84.                         portsout(CRLF);
  85.                         portsout(ptr);
  86.                         portsout(" ? ");
  87.                         portsin(buf128,80);
  88.                         fprintf(otbuf, "%s\n", buf128);
  89.                         continue;
  90.                 }
  91.                 if(buf128[0] == '^')
  92.                 {
  93.                         ptr = buf128 + 1;
  94.                         portsout(CRLF);
  95.                         portsout(ptr);
  96.                         portsout(" ? ");
  97.             while(1) {
  98.                             portsin(buf128,80);
  99.                 if( strlen(buf128) > 0 )
  100.                     break;
  101.                 portsout("\n\rInput is required.");
  102.                                portsout("\n\r? ");
  103.             }
  104.                         fprintf(otbuf, "%s\n", buf128);
  105.                         continue;
  106.                 }
  107.                 if(buf128[0] == '@')
  108.                 {
  109.                         ptr = buf128 + 1;
  110.                         portsout(CRLF);
  111.                         portsout(ptr);
  112.                         portsout(CRLF);
  113.                         continue;
  114.                 }
  115.                 if(buf128[0] == '&')
  116.                 {
  117.                         ptr = buf128 + 1;
  118.                         portsout(CRLF);
  119.                         portsout(ptr);
  120.                         continue;
  121.                 }
  122.                 if(buf128[0] == '%' )
  123.                 {
  124.                         portsout(CRLF);
  125.                         continue;
  126.                 }
  127.                 if(buf128[0] == 'y' || buf128[0] == 'Y')
  128.                 {
  129.                         ptr = buf128 + 1;
  130.                         portsout(CRLF);
  131.                         portsout(ptr);
  132.                         portsout(" ? ");
  133.                         portsin(buf128,1);
  134.                         if(buf128[0] == 'Y' || buf128[0] == 'y') var = TRUE;
  135.                         else var = FALSE;
  136.                         continue;
  137.                 }
  138.                 if(buf128[0] == 'n' || buf128[0] == 'N')
  139.                 {
  140.                         ptr = buf128 + 1;
  141.                         portsout(CRLF);
  142.                         portsout(ptr);
  143.                         portsout(" ? ");
  144.                         portsin(buf128,1);
  145.                         if(buf128[0] == 'N' || buf128[0] == 'n') var = TRUE;
  146.                         else var = FALSE;
  147.                         continue;
  148.                 }
  149.                 if(buf128[0] == '/')
  150.                 {
  151.                         rewind(otbuf);
  152.                         locking(fds, LK_UNLCK, 0L);
  153.                         fclose(inbuf);
  154.                         fclose(otbuf);
  155.                         return;
  156.                 }
  157.                 if(buf128[0] == '{' && var == TRUE ) continue;
  158.                 if(buf128[0] == '{')
  159.                 {
  160.                         while ((fgets(buf128, 80, inbuf)) != NULL )
  161.                         {
  162.                                 strip(buf128);
  163.                                 if(buf128[0] == '}')
  164.                                 goto next_qloop;
  165.                         }
  166.                         rewind(otbuf);
  167.                         locking(fds, LK_UNLCK, 0L);
  168.                         fclose(inbuf);
  169.                         fclose(otbuf);
  170.                         return;
  171.                 }
  172.                 if(buf128[0] == '}') continue;
  173.         }
  174.         rewind(otbuf);
  175.         locking(fds, LK_UNLCK, 0L);
  176.         fclose(inbuf);
  177.         fclose(otbuf);
  178. }
  179.  
  180.  
  181. int             set_yet_q = TRUE;
  182. extern int      user_priv;
  183.  
  184.  
  185. int change_qst( type, typeofqst ) int type, typeofqst;
  186. {
  187.         FILE           *fpt, *fopen();
  188.         char           *fgets(), *getenv();
  189.         char            choice[4];
  190. #ifndef SYSV
  191.         char            dir_priv_ascii[7];
  192. #endif
  193. #ifdef SYSV
  194.         char            dir_priv_ascii[20];
  195. #endif
  196.  
  197.         char           *buf_ptr;
  198.         int             line_cnt, ret, i;
  199.         int             index_value, ptr;
  200.         int             length;
  201.  
  202.  
  203. do_again:
  204.         strcpy(buf128, QUESTION);
  205.  
  206.         if ((fpt = fopen(buf128, "r")) == NULL) {
  207.                 portsout("\n\rError Opening Questionnaire List: Notify Sysop!\n\r");
  208.                 return (-1);
  209.         }
  210.         if(!type) {
  211.         portsout("\n\r    Questionnaire     Description                                      \n\r");
  212.         portsout("    ============= ========================================== \n\r");
  213.         }
  214.  
  215.         line_cnt = 0;
  216.         while (fpt) {
  217.         zfl(f_lines[line_cnt], 81);
  218.                 if ((fgets(f_lines[line_cnt], 80, fpt)) == NULL) {
  219.                         if (line_cnt == 0) {
  220.                                 portsout("\n\rEOF Unexpected in Questionnaire List: Notify Sysop!\n\r");
  221.                                 return (-1);
  222.                         }
  223.                         break;  /* if not 1st line */
  224.                 }               /* end of if ((fgets)) */
  225.                 if (line_cnt > 0) {
  226.                         length = strlen(f_lines[line_cnt]);
  227.                         length -= 57;
  228.                         if(length > 6)
  229.                                 length = 6;
  230.                         substr(f_lines[line_cnt], dir_priv_ascii, 57, length);
  231.                         dir_priv[line_cnt] = atoi(dir_priv_ascii);
  232.                         if (dir_priv[line_cnt] > user_priv)
  233.                                 goto next_read;
  234.                         strcpy(who_am_i, f_lines[line_cnt]);
  235.                         buf_ptr = who_am_i;
  236.                         buf_ptr += 56;
  237.                         for (ptr = 0; ptr < 6; ptr++)
  238.                                 *buf_ptr++ = ' ';
  239.             *buf_ptr='\0';
  240.                         sprintf(buf128, "%2d) %s", line_cnt, who_am_i);
  241.                         if(!type) {
  242.                         strip(buf128);
  243.                         term_space(buf128);
  244.                         portsout(buf128);
  245.                         portsout("\n\r");
  246.                         }
  247.                 }
  248. next_read:
  249.                 ++line_cnt;
  250.         }                       /* end of while (fpt) */
  251.         if (line_cnt <= 1)
  252.                 return;
  253.         if (set_yet_q && !type) {
  254.                 portsout(CRLF);
  255.     if(!typeofqst)
  256.                 portsout(" Q) Quit to Previous Menu");
  257.         }
  258.         if(!type)portsout(CRLF);
  259.         fclose(fpt);
  260.         if(!type)portsout(CRLF);
  261.  
  262.         while (1) {
  263.                 if(!type) {
  264.                 portsout("Enter Selection ===> ");
  265.         if(!typeofqst)
  266.                    portsin_cmp(choice, 2, "Qq");
  267.         else
  268.             portsin(choice, 2);
  269.                 portsout(CRLF);
  270.                 *choice = toupper(*choice);
  271.  
  272.                 if (*choice == 'Q' && set_yet_q && !typeofqst)
  273.                         return (-1);
  274.  
  275.  
  276.                 index_value = atoi(choice);
  277.                 }
  278.                 else index_value = type;
  279.                 if (index_value > 0 && index_value < line_cnt) {
  280.                         if (dir_priv[index_value] <= user_priv) {
  281.                                 parse2arg(f_lines[index_value]);
  282.                                 set_yet_q = TRUE;
  283.                                 return (0);
  284.                         }
  285.                 }
  286.                 if( type != 0 ) {
  287.                         type = 0;
  288.                         portsout("\n\rInvalid Questionnaire request!\n\r");
  289.                         goto do_again;
  290.                 }
  291.         }
  292. }
  293.  
  294.  
  295.  
  296.  
  297.  
  298. parse2arg(string)
  299.         char           *string;
  300. {
  301.  
  302.         register char  *file_ptr, *xptr;
  303.         register int    i;
  304.  
  305.         strcpy(QST_FILE, ORGPATH);
  306.         file_ptr = (QST_FILE + strlen(QST_FILE));
  307.  
  308.         i = 0;
  309.         while (string[i] != ' ') {
  310.                 *file_ptr = string[i];
  311.                 ++file_ptr;
  312.                 ++i;
  313.         }
  314.         *file_ptr = '\0';
  315.     strcpy(ANSWER, QST_FILE);
  316.     strcat(QST_FILE, ".qst");
  317.     strcat(ANSWER, ".ans");
  318.  
  319. }
  320.