home *** CD-ROM | disk | FTP | other *** search
/ HomeWare 14 / HOMEWARE14.bin / archive / au116_4s.arj / WHATIS.CPP < prev   
C/C++ Source or Header  |  1994-03-08  |  9KB  |  350 lines

  1. // WHATIS.CPP                                 1          1    6666
  2. // Dave Harris                                11         11   6
  3. // Compiled using Borland C++ ver 3.1       1 1        1 1   6666
  4. // 03-03-94                                  1     ..   1   6   6
  5. //                                           11111 .. 11111  666
  6. ////////////////////////////////////////////////////////////////////////
  7.  
  8. #include "au.hpp"      // Globally used defines
  9.  
  10. /*********************************************************************/
  11. /* Define Statements */
  12. /*********************/
  13.  
  14. #define PROGRAM "WHATIS"  // Name of module
  15.  
  16. /*********************************************************************/
  17. /* Structures */
  18. /**************/
  19.  
  20. typedef struct TYPE
  21. {
  22.     char *desc;
  23.     int  errorlevel;      // errorlevel to return
  24.     BYTE color;
  25.     char nocase;
  26.     unsigned char action[100];
  27. } TYPE;
  28.  
  29. #define START      255     // Goto byte
  30. #define END       254     // Goto byte from end
  31. #define MATCH1      253     // match a byte
  32. #define MATCH2      252     // match a word
  33.  
  34. /*********************************************************************/
  35. /* Global Variables */
  36. /********************/
  37.  
  38. /* Command line/Config options */
  39.  
  40. typedef struct
  41. {
  42.     TYPE *type;
  43.     int type_number;     // current element of type being added to
  44.     int action_pos;
  45.     int errorlevel;
  46. } WHATIS_INFO;
  47.  
  48. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  49. static int whatis(AU *au, char *file_name)
  50. {
  51.     HANDLE file;
  52.     int i,pos;
  53.     WHATIS_INFO *in = (WHATIS_INFO *)au->info;
  54.  
  55.     au->number_processed++;
  56.     file = au_open(au, file_name);
  57.     for (i=0; i<=in->type_number; i++)
  58.     {
  59.         pos = 0;
  60.         for(EVER)
  61.         {
  62.             switch(in->type[i].action[pos++])
  63.             {
  64.             case 0:
  65.                 au_printf_c(au, in->type[i].color, "%-*s  %s\n", FILE_SIZE,
  66.                             file_name, in->type[i].desc, in->type[i].desc);
  67.                 in->errorlevel = in->type[i].errorlevel;
  68.                 close(file);
  69.                 return 0;
  70.             case START:
  71.                 lseek(file, *(long *)(in->type[i].action+pos), SEEK_SET);
  72.                 pos+=4;
  73.                 break;
  74.             case END:
  75.                 lseek(file, *(long *)(in->type[i].action+pos), SEEK_END);
  76.                 pos+=4;
  77.                 break;
  78.             case MATCH1:
  79.             {
  80.                 char ch1 = my_getc_1(file);
  81.                 char ch2 = in->type[i].action[pos++];
  82.                 if (in->type[i].nocase)
  83.                 {
  84.                     ch1 = toupper(ch1);
  85.                     ch2 = toupper(ch2);
  86.                 }
  87.                 if (ch1 != ch2)
  88.                     goto around;
  89.                 break;
  90.             }
  91.             case MATCH2:
  92.                 break;
  93.             }
  94.         }
  95. around: ;
  96.     }
  97.     close(file);
  98.     au_printf_c(au, 7, "%-*s  other\n", FILE_SIZE, file_name);
  99.     return 0;
  100. }
  101. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  102. static long read_number(AU *au, char *string, char *cfg_file, int cfg_line)
  103. {
  104.     long number=0;
  105.     int  pos=0;
  106.     int  negate = FALSE;
  107.     int  i;
  108.  
  109.     if (string[0]=='-')
  110.     {
  111.         negate = TRUE;
  112.         pos = 1;
  113.     }
  114.     if (string[pos]=='0')
  115.     {
  116.         if (toupper(string[pos+1]) == 'X')    // Hex
  117.         {
  118.             for (i=pos+2; i<strlen(string); i++)
  119.             {
  120.                 string[i] = toupper(string[i]);
  121.                 number*=16;
  122.                 if (string[i]>='A' && string[i]<='F')
  123.                     number+=string[i]-'A'+10;
  124.                 else if (string[i]>='0' && string[i]<='9')
  125.                     number+=string[i]-'0';
  126.                 else
  127.                 {
  128.                     au_printf_error(au, "Bad hex constant in file %s: line %d",
  129.                            cfg_file, cfg_line);
  130.                     exit(0);
  131.                 }
  132.             }
  133.         }
  134.         else                                         // Octal
  135.         {
  136.             for (i=pos+1; i<strlen(string); i++)
  137.             {
  138.                 number*=8;
  139.                 if (string[i]>='0' && string[i]<='7')
  140.                     number+=string[i]-'0';
  141.                 else
  142.                 {
  143.                     au_printf_error(au, "Bad octal constant in line %d", cfg_line);
  144.                     exit(0);
  145.                 }
  146.             }
  147.         }
  148.     }
  149.     else                                            // Decimal
  150.     {
  151.         for (i=pos; i<strlen(string); i++)
  152.         {
  153.             number*=10;
  154.             if (string[i]>='0' && string[i]<='9')
  155.                 number+=string[i]-'0';
  156.             else
  157.             {
  158.                 au_printf_error(au, "Bad Decimal constant in line %d", cfg_line);
  159.                 exit(0);
  160.             }
  161.         }
  162.     }
  163.     if (negate)
  164.         return -number;
  165.     else
  166.         return number;
  167. }
  168. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  169. static void ReadCFGInfo(AU *au, HANDLE file, char *cfg_file, int *cfg_line)
  170. {
  171.    char string[200],
  172.         string2[200],
  173.         string3[200];
  174.  
  175.    int len;
  176.    int i;
  177.    WHATIS_INFO *in = (WHATIS_INFO *)au->info;
  178.  
  179.    for(EVER)
  180.    {
  181.  
  182.       if (get_file_line(au, file, string)==EOF)
  183.          break;
  184.       (*cfg_line)++;
  185.  
  186. /////////////////////////////////////////
  187. around:
  188.       ltrim(string);
  189.       if (string[0] == '\0')
  190.          continue;
  191.  
  192.       if (string[0] == '\"')
  193.       {
  194.          len = strlen(string);
  195.          string[0] = ' ';
  196.          for (i=1; i<len; i++)
  197.          {
  198.             if (string[i]=='\"')
  199.             {
  200.                string[i] = ' ';
  201.                goto around;
  202.             }
  203.             in->type[in->type_number].action[in->action_pos++] = MATCH1;
  204.             in->type[in->type_number].action[in->action_pos++] = string[i];
  205.             string[i] = ' ';  // so ltrim will hack it off
  206.          }
  207.          au_printf_error(au, "\" missing in line %d", *cfg_line);
  208.          exit(0);
  209.       }
  210.  
  211.       split_string(string, string2);
  212.       switch (toupper(string2[1]) << 8 | toupper(string2[0]))
  213.       {
  214.          case 'BE':                                          // Begin
  215.             return;
  216.          case 'TY':                                          // Type
  217.          {
  218.             int len,i;
  219.  
  220.             in->type_number++;
  221.             in->action_pos = 0;
  222.             in->type[in->type_number].color = 7;
  223.             if (string[0]!='\"')
  224.             {
  225.                au_printf_error(au, "\" missing in line %d", *cfg_line);
  226.                exit(0);
  227.             }
  228.             string[0]=' ';
  229.             len=strlen(string);
  230.             for (i=1 ; i<len ; i++)
  231.             {
  232.                if (string[i]=='\"')
  233.                {
  234.                   string[i] = ' ';
  235.                   goto bp1;
  236.                }
  237.                string2[i-1] = string[i];
  238.                string[i] = ' ';
  239.             }
  240.             au_printf_error(au, "\" missing in line %d", *cfg_line);
  241.             exit(0);
  242. bp1:
  243.             string2[i-1] = '\0';
  244.             in->type[in->type_number].desc =
  245.                       (char *)au_malloc(au, strlen(string2)+1);
  246.             strcpy(in->type[in->type_number].desc,string2);
  247.             goto around;
  248.          }
  249.          case 'ST':
  250.             split_string(string, string2);
  251.             in->type[in->type_number].action[in->action_pos++] = START;
  252.             *(long *)(in->type[in->type_number].action+in->action_pos) =
  253.                 read_number(au, string2, cfg_file, *cfg_line);
  254.             in->action_pos+=4;
  255.             goto around;
  256.          case 'EN':
  257.             split_string(string, string2);
  258.             in->type[in->type_number].action[in->action_pos++] = END;
  259.             *(long *)(in->type[in->type_number].action+in->action_pos) =
  260.                 read_number(au, string2, cfg_file, *cfg_line);
  261.             in->action_pos+=4;
  262.             goto around;
  263.          case 'ER':
  264.             split_string(string, string2);
  265.             in->type[in->type_number].errorlevel =
  266.                 read_number(au, string2, cfg_file, *cfg_line);
  267.             goto around;
  268.          case 'CO':
  269.             split_string(string, string2);
  270.             in->type[in->type_number].color =
  271.                 read_number(au, string2, cfg_file, *cfg_line) % 16;
  272.             goto around;
  273.          case 'NO':
  274.             in->type[in->type_number].nocase = TRUE;
  275.             goto around;
  276.          default:                 /* number of some sort probably */
  277.          {
  278.             long number;
  279.  
  280.             number = read_number(au, string2, cfg_file, *cfg_line);
  281.             if (number <= 255)
  282.             {
  283.                in->type[in->type_number].action[in->action_pos++] = MATCH1;
  284.                in->type[in->type_number].action[in->action_pos++] = number;
  285.             }
  286.             else
  287.             {
  288.                in->type[in->type_number].action[in->action_pos++] = MATCH2;
  289.                *(int *)(in->type[in->type_number].action[in->action_pos+=2]) = number;
  290.             }
  291.             goto around;
  292.          }           /* End Default */
  293.       }         /* End Switch */
  294.    }         /* End For */
  295. }
  296. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  297. static BYTE parse_comm_line(AU *au, char option, char *cur_argv,
  298.                             PARSE_TYPE type)
  299. {
  300.     Unused(cur_argv);
  301.  
  302.     switch (type)
  303.     {
  304.     case PARSE_PARAM_OPTION:
  305.         switch (option)
  306.         {
  307.         case '?':
  308.             au_standard_opt_header(au, "Whatis", NULL);
  309.             exit (0);
  310.         default:
  311.             au_invalid_option(au, PROGRAM, option);
  312.         }
  313.         return TRUE;
  314.     }
  315.     return FALSE;
  316. }
  317. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  318. int main_whatis(AU *au, int argc, char *argv[])
  319. {
  320.     int retCode;
  321.     WHATIS_INFO *in;
  322.  
  323.     in = (WHATIS_INFO *)au_malloc(au, sizeof(WHATIS_INFO));
  324.     memset(in, '\0', sizeof(WHATIS_INFO));
  325.     au->info = in;
  326.  
  327.     in->type = (TYPE *)au_malloc(au, sizeof(TYPE)*200);
  328.     if (in->type == NULL)
  329.     {
  330.         au_printf_error(au, "Out of Memory\n");
  331.         exit(1);
  332.     }
  333.     in->type_number = -1;
  334.  
  335.     ReadGlobalCFGInfo(au, au->cfg_file, PROGRAM, ReadCFGInfo);
  336.     generic_parse_comm_line(au, argc, argv, parse_comm_line);
  337.     process_files(au, whatis);
  338.  
  339.     if (!au->no_extra)
  340.     {
  341.         au_printf_c(au, 15, "\nFiles Processed = %d\n", au->number_processed);
  342.         au_printf_c(au, 15, "Exiting with errorlevel %d\n", in->errorlevel);
  343.     }
  344.  
  345.     retCode = in->errorlevel;
  346.     free(in->type);
  347.     return retCode;
  348. }
  349.  
  350.