home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / gigop806.zip / COMPILE.CPP < prev    next >
Text File  |  1994-08-04  |  7KB  |  303 lines

  1. #include <conio.h>
  2. #include <ctype.h>
  3. #include <direct.h>
  4. #include <dos.h>
  5. #include <fcntl.h>
  6. #include <io.h>
  7. #include <share.h>
  8. #include <stdarg.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <sys\stat.h>
  13.  
  14. #define addbackslash(x)  if (x[0]) if (x[strlen(x)-1]!='\\') strcat(x,"\\")
  15.  
  16.  
  17. #ifdef __OS2__
  18.  #define DW unsigned short
  19. #else
  20.  #define DW unsigned int
  21. #endif
  22.  
  23.   #define INBUFSIZE 32000
  24.   #define OUTBUFSIZE 8192
  25.  
  26.   #define bumpby 35
  27.  
  28. // #define messy    This define shows us what it's compiling
  29.  
  30. char defaultdir[256] = ".\\";
  31. char defaultnodelist[256] = "NODELIST";
  32. char workbuf[256];
  33. int append=0;
  34.  
  35. int inhandle=0;
  36. char *inbuf=NULL;
  37. int insize=0,inptr=0,inread=0;
  38.  
  39. FILE *outfile=NULL;
  40.    char namebuf[256];
  41.  
  42.   static char *wb1=&workbuf[1];
  43.   static char *wb4=&workbuf[4];
  44.   static char *wb5=&workbuf[5];
  45.   static char *wb7=&workbuf[7];
  46.   static char *wb8=&workbuf[8];
  47.  
  48.   static DW uZONE=65535u;
  49.   static DW uHOST=65534u;
  50.  
  51.  
  52.  
  53. void myatoi(char *p)
  54. {
  55.  DW u=0;
  56.  while ((*p)!=',')
  57.   u=(DW) (u*10-'0'+(*p++));
  58.  if ((u&uHOST)==uHOST) // also covers uZONE
  59.   {
  60.   cprintf("Skipping a node using 65534 or 65535 as an address.\r\n");
  61.   u=0;
  62.   }
  63.  fwrite(&u,2,1,outfile);
  64. }
  65.  
  66. /*
  67. char nextchar(void)
  68. {
  69.  if (inptr>=inread)
  70.    {
  71.     inptr=0;
  72.     inbuf[0]=0;
  73.     inread=read(inhandle,inbuf,insize);
  74.     if (!inread) return 0;
  75.    }
  76.  return(inbuf[inptr++]);
  77. }
  78. */
  79. #define prepnextchar(); if (inptr>=inread) {inptr=0;inbuf[0]=0;inread=read(inhandle,inbuf,insize);}
  80. #define nextchar() inbuf[inptr++]
  81.  
  82. void nextline(char *workbuf)
  83. {
  84.  register char ch;
  85. runagain:
  86.  *workbuf=0;
  87.  prepnextchar();
  88.  *workbuf=nextchar();
  89.  if (!*workbuf) return;
  90.  if (*workbuf==13) return;
  91.  if (*workbuf==27) return;
  92.  if (*workbuf==10) return;
  93.  if (*workbuf==';')
  94.    {
  95.     do
  96.      {prepnextchar();ch=nextchar();}
  97.     while ((ch!=0) && (ch!='\n'));
  98.     goto runagain;
  99.    }
  100.  if (*workbuf==26) {*workbuf=0;return;}
  101.  do  //first string + ,
  102.    {workbuf++;
  103.     prepnextchar();
  104.     *workbuf=ch=nextchar();}
  105.  while (*workbuf!=',');
  106.  
  107.  do  // second char + ,
  108.    { workbuf++;
  109.      prepnextchar();
  110.      *workbuf=nextchar();}
  111.  while (*workbuf!=',');
  112.  
  113.  inptr+=bumpby;
  114.  if (inptr>=inread) inptr=inread;
  115.  
  116.  do
  117.    {prepnextchar();
  118.     ch=nextchar();}
  119.     while ((ch!=0) && (ch!='\n'));
  120.  return;
  121. }
  122. int why=0;
  123. void parselines(void)
  124. {
  125. //  register DW value;
  126.  
  127. /*
  128.     Hub,        4
  129.     Pvt,        4
  130.     Zone,       5
  131.     Host,       5
  132.     Region,     7
  133.     */
  134.  
  135. newline:
  136.   nextline(workbuf);
  137.   switch (*workbuf)
  138.    {
  139.     case 0   : return;
  140.  
  141.     case ',' : myatoi(wb1);
  142.                #ifdef messy
  143.                  gotoxy(12,why);cprintf("%4u",atoi(wb1));
  144.                #endif
  145.                goto newline;
  146.  
  147.     case 'H' : if (workbuf[1]=='u') goto Hub; // we only want HOST here
  148.                if (workbuf[2]=='l') goto priv; // DOWN? make it private
  149.                #ifdef messy
  150.                  gotoxy(7,why);cprintf("%4u",atoi(wb5));
  151.                #endif
  152.                fwrite(&uHOST,2,1,outfile);
  153.                myatoi(wb5);goto newline;
  154.  
  155.     Hub      : myatoi(wb4);goto newline;
  156.  
  157. //    case 'p' :
  158.     priv     :
  159.     case 'D' :  // down
  160.     case 'P' : myatoi(wb4); goto newline;
  161.  
  162.     case 'R' : fwrite(&uHOST,2,1,outfile);
  163.                #ifdef messy
  164.                  gotoxy(7,why);cprintf("%4u",atoi(wb7));
  165.                #endif
  166.                myatoi(wb7);goto newline;
  167.  
  168.     case 'Z' : fwrite(&uZONE,2,1,outfile);
  169.                #ifdef messy
  170.                  gotoxy(1,why);cprintf("%4u",atoi(wb5));
  171.                #else
  172.                 cprintf("\r\nZone %u",atoi(wb5));
  173.                #endif
  174.                myatoi(wb5);goto newline;
  175.  
  176.    }
  177.   goto newline;
  178. }
  179. void parsenodelist(void)
  180. {  int ibsize;
  181.   inhandle=sopen(defaultnodelist, O_BINARY|O_RDONLY, SH_DENYNO, S_IREAD);
  182.   if (inhandle<1)
  183.    {
  184.     cprintf("Could not open nodelist file %s.\r\n",defaultnodelist);
  185.     exit(1);
  186.    }
  187.  
  188.  
  189.   if (append)
  190.     outfile= fopen("NODELIST.GIG", "ab");
  191.   else
  192.     outfile= fopen("NODELIST.GIG", "wb");
  193.   if (!outfile)
  194.    {
  195.     cprintf("Could not open output index file %s.\r\n",workbuf);
  196.     exit(1);
  197.    }
  198.  
  199.   if (setvbuf(outfile, NULL, _IOFBF, OUTBUFSIZE) != 0)
  200.      cprintf("Failed to set up output buffer\r\n");
  201.  
  202.   ibsize=INBUFSIZE;
  203.  loop:
  204.   if (ibsize<256)
  205.    {
  206.     cprintf("Failed to set up input buffer.\r\n");
  207.     fclose(outfile);
  208.     exit(1);
  209.    }
  210.   inbuf=(char *) calloc(1,ibsize);
  211.   if (!inbuf) {ibsize-=128;goto loop;}
  212.   insize=ibsize;inptr=0,inread=0;
  213. #ifdef messy
  214.   why = wherey();
  215. #endif
  216.   parselines();
  217.  
  218.   close(inhandle);
  219.   fclose(outfile);
  220. }
  221.  
  222. char *getnodelist(char *dir, char *name)
  223. {
  224. /*  char *p; */
  225. DIR *dirp;
  226. char buf[255];
  227. unsigned long d_date=0,d_time=0;
  228. struct dirent *direntp;
  229.  
  230.    int valuemode=0;
  231.    strcpy(namebuf,dir);
  232.    addbackslash(namebuf);
  233.    strcat(namebuf,name);
  234.    if (!strchr(name,'.')) {valuemode=1;strcat(namebuf,".*");}
  235.    buf[0]=0; d_time=d_date=0;
  236.  
  237. dirp = opendir(namebuf); 
  238. if (dirp == NULL) {
  239.    fprintf(stderr,"Could not opendir: %s\r\n",namebuf);
  240. } else {
  241.    for (;;) {
  242.       direntp = readdir(dirp);
  243.       if (direntp==NULL) break;
  244.       if ((direntp->d_date > d_date) && (direntp->d_time > d_time))
  245.            strcpy(buf,direntp->d_name);
  246.     }
  247.    closedir(dirp);
  248. } /* endif */
  249.  
  250.    if (!buf[0])
  251.      {
  252.       cprintf("No nodelist files found.\r\n");
  253.       exit(1);
  254.      }
  255.    strcpy(namebuf,dir);
  256.    addbackslash(namebuf);
  257.    strcat(namebuf,buf);
  258.    return namebuf;
  259. }
  260.  
  261. void main(int argc, char *argv[])
  262. {
  263. //  clrscr();
  264.   if (argc > 1)
  265.    {
  266.     if (strcmp(argv[1],"/?")==NULL)
  267.       {
  268.   cputs("\r\n\r\n");
  269.   cprintf("Nodelist Index Builder for GIGO vsn "__DATE__"\r\n");
  270.   cprintf("Usage:  COMPILE [path] [nodelistname] [/Append]\r\n"
  271.                  " Note that PATH and NODELISTNAME _must_ be specified\r\n"
  272.                  " seperately for this program to work.  It's quick, dirty,\r\n"
  273.                  " and way fast!  /APPEND may be specified if you need to\r\n"
  274.          " run this for more than one nodelist file.  Sample:\r\n"
  275.                  "   C> COMPILE .\ NODELIST\r\n"
  276.          "   C> COMPILE .\ EGGNET /A\r\n");
  277.      return;
  278.       }
  279.     strcpy(defaultdir,argv[1]);
  280.      }
  281.   addbackslash(defaultdir);
  282.   if (argc > 2)
  283.     strcpy(defaultnodelist,argv[2]);
  284.   if (argc > 3)
  285.     for (int index=3; index < argc; index++)
  286.      {
  287.        if (strnicmp(argv[index],"/A",2)==NULL)
  288.          append=1;
  289.      }
  290.   strcpy(defaultnodelist,getnodelist(defaultdir,defaultnodelist));
  291.   cprintf("Nodelist Index Builder for GIGO vsn "__DATE__"\r\n");
  292.   cprintf("Type %s /? for help info.\r\n",argv[0]);
  293.   cprintf(       "\r\nOptions selected:\r\n"
  294.                  "  Directory:  %s\r\n"
  295.                  "  Nodelist:   %s\r\n"
  296.                  "  Append:     %s\r\n\n",defaultdir,defaultnodelist,append?"Yes":"No (Overwriting)");
  297.  
  298.  if (!append)
  299.   unlink("NODELIST.GIG");
  300.  parsenodelist();
  301.  cprintf("\r\n");
  302. }
  303.