home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / RAUSM120.ZIP / READCTL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-08  |  11.7 KB  |  319 lines

  1. #include "rausm.h"
  2. #include <time.h>
  3. #include <io.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8.  
  9. void _Err_Handler(char *file, int line, char *date, char *time, int errlevel)
  10. {
  11.     if (logfp != NULL)
  12.         fprintf(logfp, "\nAbort: %s(%d) %s %s\r\n", strupr(file), line, date, time);
  13.     fprintf(stderr, "\nAbort: %s(%d) %s %s\r\n", strupr(file), line, date, time);
  14.     exit(errlevel);
  15. }
  16.  
  17. static FILE *fp;
  18. static char *p;
  19.  
  20. static char *SkipSpace(void)
  21. {
  22.     while ((*p == ' ' || *p == '\t') && *p != '\0' && *p != '\n') p++;
  23.     if (*p == '\0' || *p == '\n') return NULL;
  24.     return p;
  25. }
  26.  
  27. static int TokenType(char *s)
  28. {
  29.     if (stricmp(s, "LogFile") == 0) return 1;
  30.     if (stricmp(s, "UserBbsPath") == 0) return 2;
  31.     if (stricmp(s, "ReportOnly") == 0) return 3;
  32.     if (stricmp(s, "UploadCredit") == 0) return 4;
  33.     if (stricmp(s, "UploadKCredit") == 0) return 5;
  34.     if (stricmp(s, "DownloadCredit") == 0) return 6;
  35.     if (stricmp(s, "DownloadKCredit") == 0) return 7;
  36.     if (stricmp(s, "MessageCredit") == 0) return 8;
  37.     if (stricmp(s, "CallOnCredit") == 0) return 9;
  38.     if (stricmp(s, "SysOpCredit") == 0) return 10;
  39.     if (stricmp(s, "LoginDayCredit") == 0) return 11;
  40.     if (stricmp(s, "ProtectFlag") == 0) return 12;
  41.     if (stricmp(s, "MaxSecurity") == 0) return 13;
  42.     if (stricmp(s, "MinSecurity") == 0) return 14;
  43.     if (stricmp(s, "BaseCredit") == 0) return 15;
  44.     if (stricmp(s, "Security") == 0) return 16;
  45.     if (stricmp(s, "ListFile") == 0) return 17;
  46.     if (stricmp(s, "Hidden") == 0) return 18;
  47.     if (stricmp(s, "SortFile") == 0) return 19;
  48.     return 0;
  49. }
  50.  
  51. static char *GetToken(void)
  52. {
  53.     static char name[512];
  54.     char *p2;
  55.  
  56.     p2 = name;
  57.     while (*p != ' ' && *p != '\r' && *p != '\n' && *p != '\t' && *p != '\0')
  58.         *p2++ = *p++;
  59.     *p2 = '\0';
  60.     return &name[0];
  61. }
  62.  
  63. void ReadCTL(char *path)
  64. {
  65.     char str[1024];
  66.     char name[80];
  67.     int line = 0;
  68.     unsigned int table[] = { 0x01, 0x02, 0x04, 0x08,
  69.                              0x10, 0x20, 0x40, 0x80
  70.                            };
  71.     time_t beg;
  72.     SortList    *stail, *stemp;
  73.     Hidden      *htail, *htemp;
  74.     int i;
  75.  
  76.     logfp = NULL;
  77.     UserBbsPath[0] = '\0';
  78.     ListName[0] = '\0';
  79.     ReportOnly = 0;
  80.     UploadCredit = 0L;
  81.     UploadKCredit = 0L;
  82.     DownloadCredit = 0L;
  83.     DownloadKCredit = 0L;
  84.     MessageCredit = 0L;
  85.     CallOnCredit = 0L;
  86.     SysOpCredit = 0L;
  87.     BaseCredit = 0L;
  88.     LoginDayCredit = 0L;
  89.     memset(UserFlag, '\0', 4);
  90.     MaxSecurity = 0U;
  91.     MinSecurity = 0U;
  92.     SecCount = 0;
  93.     HiddenHead = NULL;
  94.     SortHead = NULL;
  95.     htail = NULL;
  96.     stail = NULL;
  97.  
  98.     if (access("RAUSM.CTL", 0) == 0)
  99.         strcpy(name, "RAUSM.CTL");
  100.     else {
  101.         char drive[_MAX_DRIVE];
  102.         char dir[_MAX_DIR];
  103.         char file[_MAX_FNAME];
  104.         char ext[_MAX_EXT];
  105.  
  106.         _splitpath(path, drive, dir, file, ext);
  107.         _makepath(name, drive, dir, "RAUSM", ".CTL");
  108.     }
  109.  
  110.     printf(" ■ Reading control file [%s].\n", name);
  111.     if ((fp = fopen(name, "rt")) == NULL) {
  112.         fprintf(stderr, "Failed to open control file [%s].\n", name);
  113.         ERR_EXIT(1);
  114.     }
  115.  
  116.     while (1) {
  117.         line++;
  118.         fgets(str, 1024, fp);
  119.         if (feof(fp)) break;
  120.         p = str;
  121.         if (SkipSpace() == NULL || *p == ';') continue;
  122.         strcpy(name, GetToken());
  123.         if (SkipSpace() == NULL || *p == ';') {
  124.             if (logfp != NULL)
  125.                 fprintf(logfp, " ! %s setup error at line %d.\n", name, line);
  126.             fprintf(stderr, " ! %s setup error at line %d.\n", name, line);
  127.             continue;
  128.         }
  129.         switch (TokenType(name)) {
  130.             case 1:     // LogFile
  131.                 strcpy(name, GetToken());
  132.                 if ((logfp = fopen(name, "at")) == NULL)
  133.                     fprintf(stderr, " ! Failed to open %s.\n", name);
  134.                 if (logfp != NULL) {
  135.                     time(&beg);
  136.                     strcpy(name, ctime(&beg));
  137.                     name[strlen(name)-1] = '\0';
  138.                     fprintf(logfp, " ------ %s ------\n", name);
  139.                     fprintf(logfp, " ■ RAUSM %s fire up.\n", Version);
  140.                 }
  141.                 break;
  142.             case 2:     // UserBBSPath
  143.                 strcpy(UserBbsPath, GetToken());
  144.                 break;
  145.             case 3:    // ReportOnly
  146.                 strcpy(name, GetToken());
  147.                 if (stricmp(name, "Yes") == 0)
  148.                     ReportOnly = 1;
  149.                 else if (stricmp(name, "No") == 0)
  150.                     ReportOnly = 0;
  151.                 else {
  152.                     if (logfp != NULL)
  153.                         fprintf(logfp, " ! Definition error at line %d.\n", line);
  154.                     fprintf(stderr, " ! Definition error at line %d.\n", line);
  155.                     continue;
  156.                 }
  157.                 break;
  158.             case 4:    // UploadCredit
  159.                 strcpy(name, GetToken());
  160.                 UploadCredit = atol(name);
  161.                 break;
  162.             case 5:    // UploadKCredit
  163.                 strcpy(name, GetToken());
  164.                 UploadKCredit = atol(name);
  165.                 break;
  166.             case 6:    // DownloadCredit
  167.                 strcpy(name, GetToken());
  168.                 DownloadCredit = atol(name);
  169.                 break;
  170.             case 7:    // DownloadKCredit
  171.                 strcpy(name, GetToken());
  172.                 DownloadKCredit = atol(name);
  173.                 break;
  174.             case 8:    // MessageCredit
  175.                 strcpy(name, GetToken());
  176.                 MessageCredit = atol(name);
  177.                 break;
  178.             case 9:    // CallOnCredit
  179.                 strcpy(name, GetToken());
  180.                 CallOnCredit = atol(name);
  181.                 break;
  182.             case 10:    // SysOpCredit
  183.                 strcpy(name, GetToken());
  184.                 SysOpCredit = atol(name);
  185.                 break;
  186.             case 11:    // LoginDayCredit
  187.                 strcpy(name, GetToken());
  188.                 LoginDayCredit = atol(name);
  189.                 break;
  190.             case 12:    // ProtectFlag
  191.                 strcpy(name, GetToken());
  192.                 UserFlag[toupper(name[0])-'A'] |= table[name[1]-'1'];
  193.                 break;
  194.             case 13:    // MaxSecurity
  195.                 strcpy(name, GetToken());
  196.                 MaxSecurity = (unsigned int)atol(name);
  197.                 break;
  198.             case 14:    // MinSecurity
  199.                 strcpy(name, GetToken());
  200.                 MinSecurity = (unsigned int)atol(name);
  201.                 break;
  202.             case 15:    // BaseCredit
  203.                 strcpy(name, GetToken());
  204.                 BaseCredit = atol(name);
  205.                 break;
  206.             case 16:    // Security
  207.                 if (SecCount == 200) {
  208.                     if (logfp != NULL)
  209.                         fprintf(logfp, " ! Security too many definition.\n");
  210.                     fprintf(stderr, " ! Security too many definition.\n");
  211.                     continue;
  212.                 }
  213.                 strcpy(name, GetToken());
  214.                 Security[SecCount].Security = (unsigned int)atol(name);
  215.                 if (SkipSpace() == NULL || *p == ';') {
  216.                     if (logfp != NULL)
  217.                         fprintf(logfp, " ! Security definition error at line %d.\n", line);
  218.                     fprintf(stderr, " ! Security definition error at line %d.\n", line);
  219.                     continue;
  220.                 }
  221.                 strcpy(name, GetToken());
  222.                 Security[SecCount++].Credit = atol(name);
  223.                 break;
  224.             case 17:     //  ListFile
  225.                 strcpy(ListName, GetToken());
  226.                 break;
  227.             case 18:    // Hidden
  228.                 if (*p != '\"') {
  229.                     if (logfp != NULL)
  230.                         fprintf(logfp, " ! Hidden name definition error at line %d.\n", line);
  231.                     fprintf(stderr, " ! Hidden name definition error at line %d.\n", line);
  232.                     continue;
  233.                 }
  234.                 p++;
  235.                 i = 0;
  236.                 while (*p != '\"' && *p != '\0') {
  237.                     if (*p == '\\')
  238.                         name[i++] = *p++;
  239.                     name[i++] = *p++;
  240.                 }
  241.                 name[i] = '\0';
  242.                 if (*p == '\0') {
  243.                     if (logfp != NULL)
  244.                         fprintf(logfp, " ! Hidden name definition error at line %d.\n", line);
  245.                     fprintf(stderr, " ! Hidden name definition error at line %d.\n", line);
  246.                     continue;
  247.                 }
  248.                 htemp = (Hidden *)malloc(sizeof(Hidden));
  249.                 if (htemp == NULL) {
  250.                     if (logfp != NULL)
  251.                         fprintf(logfp, " ! Memory not enough at line %d.\n", line);
  252.                     fprintf(stderr, " ! Memory not enough at line %d.\n", line);
  253.                     ERR_EXIT(1);
  254.                 }
  255.                 htemp->name = (char *)malloc(sizeof(char) * (strlen(name)+1));
  256.                 if (htemp->name == NULL) {
  257.                     if (logfp != NULL)
  258.                         fprintf(logfp, " ! Memory not enough at line %d.\n", line);
  259.                     fprintf(stderr, " ! Memory not enough at line %d.\n", line);
  260.                     ERR_EXIT(1);
  261.                 }
  262.                 strcpy(htemp->name, name);
  263.                 htemp->next = NULL;
  264.                 if (htail == NULL)
  265.                     HiddenHead = htemp;
  266.                 else
  267.                     htail->next = htemp;
  268.                 htail = htemp;
  269.                 break;
  270.             case 19:    // SortFile
  271.                 strcpy(name, GetToken());
  272.                 stemp = (SortList *)malloc(sizeof(SortList));
  273.                 if (stemp == NULL) {
  274.                     if (logfp != NULL)
  275.                         fprintf(logfp, " ! Memory not enough at line %d.\n", line);
  276.                     fprintf(stderr, " ! Memory not enough at line %d.\n", line);
  277.                     ERR_EXIT(1);
  278.                 }
  279.                 stemp->SourceName = (char *)malloc(sizeof(char) * (strlen(name)+1));
  280.                 if (stemp->SourceName == NULL) {
  281.                     if (logfp != NULL)
  282.                         fprintf(logfp, " ! Memory not enough at line %d.\n", line);
  283.                     fprintf(stderr, " ! Memory not enough at line %d.\n", line);
  284.                     ERR_EXIT(1);
  285.                 }
  286.                 strcpy(stemp->SourceName, name);
  287.                 if (SkipSpace() == NULL || *p == ';') {
  288.                     if (logfp != NULL)
  289.                         fprintf(logfp, " ! Security definition error at line %d.\n", line);
  290.                     fprintf(stderr, " ! Security definition error at line %d.\n", line);
  291.                     continue;
  292.                 }
  293.                 strcpy(name, GetToken());
  294.                 stemp->TargetName = (char *)malloc(sizeof(char) * (strlen(name)+1));
  295.                 if (stemp->TargetName == NULL) {
  296.                     if (logfp != NULL)
  297.                         fprintf(logfp, " ! Memory not enough at line %d.\n", line);
  298.                     fprintf(stderr, " ! Memory not enough at line %d.\n", line);
  299.                     ERR_EXIT(1);
  300.                 }
  301.                 strcpy(stemp->TargetName, name);
  302.                 stemp->Next = NULL;
  303.                 if (stail == NULL)
  304.                     SortHead = stemp;
  305.                 else
  306.                     stail->Next = stemp;
  307.                 stail = stemp;
  308.                 break;
  309.             default:
  310.                 printf(" ! Unknown control file token (%s) at line %d.\n",
  311.                                               name, line);
  312.                 break;
  313.         }
  314.     }
  315.     fclose(fp);
  316.     return;
  317. }
  318.  
  319.