home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / c / plstsrc / edit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-31  |  14.1 KB  |  413 lines

  1. /************************************************************************
  2. *************************************************************************
  3. ** NOTE:                                                               **
  4. **   This Module is part of the ATARI ST Implementation of Parselst    **
  5. **   It should NOT be used for general reference. Please read the com- **
  6. **   plete Disclaimer at the TOP of PARSELST.C.                        **
  7. **   Peter Glasmacher at 1:143/9  Ph (408)985-1538                     **
  8. *************************************************************************
  9. *************************************************************************/
  10.  
  11. /*----------------------------------------------------------------------*
  12. *                              Nodelist Parser                          *
  13. *                                                                       *
  14. *              This module was originally written by Bob Hartman        *
  15. *                       Sysop of FidoNet node 1:132/101                 *
  16. *                                                                       *
  17. * Spark Software, 427-3 Amherst St, CS 2032, Suite 232, Nashua, NH 03061*
  18. *                                                                       *
  19. * This program source code is being released with the following         *
  20. * provisions:                                                           *
  21. *                                                                       *
  22. * 1.  You are  free to make  changes to this source  code for use on    *
  23. *     your own machine,  however,  altered source files may not be      *
  24. *     distributed without the consent of Spark Software.                *
  25. *                                                                       *
  26. * 2.  You may distribute "patches"  or  "diff" files for any changes    *
  27. *     that you have made, provided that the "patch" or "diff" files are *
  28. *     also sent to Spark Software for inclusion in future releases of   *
  29. *     the entire package.   A "diff" file for the source archives may   *
  30. *     also contain a compiled version,  provided it is  clearly marked  *
  31. *     as not  being created  from the original source code.             *
  32. *     No other  executable  versions may be  distributed without  the   *
  33. *     consent of Spark Software.                                        *
  34. *                                                                       *
  35. * 3.  You are free to include portions of this source code in any       *
  36. *     program you develop, providing:  a) Credit is given to Spark      *
  37. *     Software for any code that may is used, and  b) The resulting     *
  38. *     program is free to anyone wanting to use it, including commercial *
  39. *     and government users.                                             *
  40. *                                                                       *
  41. * 4.  There is  NO  technical support  available for dealing with this  *
  42. *     source code, or the accompanying executable files.  This source   *
  43. *     code  is provided as is, with no warranty expressed or implied    *
  44. *     (I hate legalease).   In other words, if you don't know what to   *
  45. *     do with it,  don't use it,  and if you are brave enough to use it,*
  46. *      you're on your own.                                              *
  47. *                                                                       *
  48. * Spark Software may be contacted by modem at (603) 888-8179            *
  49. * (node 1:132/101)                                                      *
  50. * on the public FidoNet network, or at the address given above.         *
  51. *                                                                       *
  52. *-----------------------------------------------------------------------*/
  53.  
  54. #include    <stdio.h>
  55. #include    <string.h>
  56. #include    <stdlib.h>
  57. #include    <ext.h>
  58. #include    <ctype.h>
  59.  
  60. #include "types.h"
  61. #include "pl.h"
  62.  
  63. /*----------------------------------------------------------------------*
  64. * -rev 04-16-87  (abbreviated)                                          *
  65. *  The CRC-16 routines used by XMODEM, YMODEM, and ZMODEM               *
  66. *  are also in this file, a fast table driven macro version             *
  67. *-----------------------------------------------------------------------*/
  68.  
  69. /*----------------------------------------------------------------------*
  70. * crctab calculated by Mark G. Mendel, Network Systems Corporation      *
  71. *-----------------------------------------------------------------------*/
  72. static unsigned short crctab[256] = {
  73.     0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108,
  74.     0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, 0x1231, 0x0210,
  75.     0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, 0x9339, 0x8318, 0xb37b,
  76.     0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, 0x2462, 0x3443, 0x0420, 0x1401,
  77.     0x64e6, 0x74c7, 0x44a4, 0x5485, 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee,
  78.     0xf5cf, 0xc5ac, 0xd58d, 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6,
  79.     0x5695, 0x46b4, 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d,
  80.     0xc7bc, 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
  81.     0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, 0x5af5, 
  82.     0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, 0xdbfd, 0xcbdc,
  83.     0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, 0x6ca6, 0x7c87, 0x4ce4,
  84.     0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, 0xedae, 0xfd8f, 0xcdec, 0xddcd,
  85.     0xad2a, 0xbd0b, 0x8d68, 0x9d49, 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13,
  86.     0x2e32, 0x1e51, 0x0e70, 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 
  87.     0x9f59, 0x8f78, 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e,
  88.     0xe16f, 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
  89.     0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, 0x02b1, 
  90.     0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, 0xb5ea, 0xa5cb, 
  91.     0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, 0x34e2, 0x24c3, 0x14a0,
  92.     0x0481, 0x7466, 0x6447, 0x5424, 0x4405, 0xa7db, 0xb7fa, 0x8799, 0x97b8,
  93.     0xe75f, 0xf77e, 0xc71d, 0xd73c, 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657,
  94.     0x7676, 0x4615, 0x5634, 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9,
  95.     0xb98a, 0xa9ab, 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882,
  96.     0x28a3, 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
  97.     0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, 0xfd2e,
  98.     0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, 0x7c26, 0x6c07,
  99.     0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, 0xef1f, 0xff3e, 0xcf5d,
  100.     0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, 0x6e17, 0x7e36, 0x4e55, 0x5e74,
  101.     0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
  102. };
  103.  
  104. /*
  105.  * updcrc macro derived from article Copyright (C) 1986 Stephen Satchell. 
  106.  *  NOTE: First argument must be in range 0 to 255.
  107.  *        Second argument is referenced twice.
  108.  * 
  109.  * Programmers may incorporate any or all code into their programs, 
  110.  * giving proper credit within the source. Publication of the 
  111.  * source routines is permitted so long as proper credit is given 
  112.  * to Stephen Satchell, Satchell Evaluations and Chuck Forsberg, 
  113.  * Omen Technology.
  114.  */
  115.  
  116. #define updcrc(cp, crc) ( crctab[((crc >> 8) & 255) ^ cp] ^ (crc << 8))
  117.  
  118. char     old_name[13] = "NODELIST.*";
  119. char     new_name[13] = "NODELIST.";
  120. char     diff_name[13] = "NODEDIFF.*";
  121. FILE     *old_file, *diff_file, *new_file;
  122. unsigned official_crc = 0;
  123. unsigned calc_crc = 0;   /* Until I find out how to do the CRC */
  124.  
  125. unsigned crcstr (char *buf, unsigned crc)
  126. {
  127. char *p;
  128.  
  129.    p = buf;
  130.    fix_up (p);
  131.  
  132.    while (*p) {
  133.       crc = updcrc (*p++, crc);
  134.    }
  135.  
  136.    return (crc);
  137. }
  138.  
  139. void fix_up (char *buf)
  140. {
  141. char  *p;
  142.  
  143.    p = buf + strlen (buf) - 1;
  144.    if (*p == '\n')
  145.       --p;
  146.    if (*p == '\r')
  147.       --p;
  148.    while (*p == ' ')
  149.       --p;
  150.    strcpy (++p, "\r\n");
  151.  
  152.    /* Now get rid of rotten chars */
  153.    p = buf;
  154.    while (*p)    {
  155.       if (*p >= 128)
  156.          *p = '?';
  157.       ++p;
  158.    }
  159. }
  160.  
  161. void edit (void)                         /* Add NODEDIFF to NODELIST */
  162. {
  163. int results;
  164.  
  165.    get_old_name (old_name);
  166.    if (!get_diff_name (diff_name))   {
  167.       results = open_infiles (old_name, diff_name);
  168.    }  else   {
  169.       results = 1;
  170.    }
  171.    if (results == 0)
  172.       add_files ();
  173.    close_files ();
  174.    return;
  175. }
  176.  
  177. void add_files (void)             /* Actually put the new lines in here */
  178. {
  179. char  old_line[256];
  180. char  diff_line[256];
  181. char  *pnt_line;
  182. char  first_char;
  183. char  *diff_result;
  184. char  *old_result;
  185. char  *ptr;
  186. int   i, j, k;
  187. int   first_diff;
  188.  
  189.    first_diff = 1;
  190.  
  191.    pnt_line = (char *) malloc (256);
  192.  
  193.    while ((diff_result = fgets (diff_line, 256, diff_file)) != NULL)    {
  194.       strcpy (pnt_line, diff_line);
  195.       first_char = diff_line[0];
  196.  
  197.       switch (first_char)   {
  198.          case ';':
  199.             continue;
  200.  
  201.          case 'D':
  202.             j = atoi (pnt_line + 1);
  203.             for (i = 0; i < j; i++)    {
  204.                old_result = fgets (old_line, 256, old_file);
  205.             }
  206.             break;
  207.  
  208.          case 'A':
  209.             j = atoi (pnt_line + 1);
  210.             for (i = 0; i < j; i++)    {
  211.                diff_result = fgets (diff_line, 256, diff_file);
  212.                if (!first_diff)
  213.                   calc_crc = crcstr (diff_line, calc_crc);
  214.                else   {
  215.                   first_diff = 0;
  216.                   ptr = strrchr (diff_line, ':');
  217.                   if (ptr != NULL)    {
  218.                      ++ptr;
  219.                      official_crc = atoi (ptr);
  220.                   }
  221.                }
  222.                k = fputs (diff_line, new_file);
  223.             }
  224.             break;
  225.  
  226.          case 'C':
  227.             j = atoi (pnt_line + 1);
  228.             for (i = 0; i < j; i++)      {
  229.                diff_result = fgets (old_line, 256, old_file);
  230.                calc_crc = crcstr (old_line, calc_crc);
  231.                k = fputs (old_line, new_file);
  232.                }
  233.             break;
  234.  
  235.          default:
  236.             printf ("\n\tI don't understand this line.\n");
  237.             break;
  238.          }
  239.       }
  240.  
  241.    if (calc_crc != official_crc)
  242.       {
  243.       fputs (";A This nodelist does not have the proper CRC!\r\n", new_file);
  244.       printf ("This nodelist does not have the proper CRC %u/%u!\n",
  245.          calc_crc, official_crc);
  246.       printf ("Please check out the problem and correct it.\n\n");
  247.       }
  248.  
  249.    fprintf (new_file, "%c", '\032');
  250.  
  251.    return;
  252. }
  253.  
  254. void close_files (void)
  255. {
  256.    if (old_file)
  257.       fclose (old_file);
  258.    if (new_file)
  259.       fclose (new_file);
  260.    if (new_file)
  261.       fclose (diff_file);
  262.  
  263.    return;
  264. }
  265.  
  266. open_infiles (char *nodelist, char *difflist)
  267. {
  268. char           first_line[256], *result;
  269. char           temp[11];
  270. int            a_number, i, j;
  271. struct ffblk   c_file;
  272.  
  273.    i = findfirst (new_name,&c_file,0);
  274.    if (i == 0)
  275.       {
  276.       printf ("%s already exists.\n", new_name);
  277.       return (1);
  278.       }
  279.    if ((old_file = fopen (nodelist, "rb"))==0)    {
  280.       printf ("Error opening old nodelist file!\n");
  281.       return (1);
  282.       }
  283.    if ((diff_file = fopen (difflist, "rb"))==0)
  284.       {
  285.       printf ("Error opening nodediff file!\n");
  286.       return (1);
  287.       }
  288.    result = fgets (first_line, 256, diff_file);
  289.    for (i = 0; i < 80; i++)
  290.       if (first_line[i] == ':')
  291.          j = i;
  292.    for (i = 0; i < 10; i++)
  293.       temp[i] = first_line[i + j + 1];
  294.    official_crc = atoi (temp);
  295.    j -= 5;
  296.    for (i = 0; i < 5; i++)
  297.       temp[i] = first_line[i + j];
  298.    temp[5] = '\0';
  299.    j = atoi (temp);
  300.    result = strrchr (nodelist, '.');
  301.    result++;
  302.    i = atoi (result);
  303.    if (i != j)
  304.       {
  305.       printf ("NodeDiff doesn't match to existing Nodelist file! Skipping...\n");
  306.       return (1);
  307.       }
  308.    new_file = fopen (new_name, "wb");
  309.    if (new_file == NULL)
  310.       {
  311.       printf ("Can't open new nodelist file!\n");
  312.       return (1);
  313.       }
  314.    printf ("Applying '%s' to '%s'\n", difflist, nodelist);
  315.    return (0);
  316. }
  317.  
  318. void get_old_name (char *temp_name)
  319. {
  320. char           *temp;
  321. int             i, j, k;
  322. struct ffblk   c_file;
  323.  
  324.    temp = (char *)malloc (20);
  325.  
  326.    j = findfirst (temp_name,&c_file,0);
  327.    while (j == 0)  {
  328.       temp = strrchr (c_file.ff_name, '.');
  329.       temp++;
  330.       if (isdigit (*temp) && isdigit (*(temp+1)) && isdigit (*(temp+2)))     {
  331.          i = atoi (temp);
  332.          strcpy (old_name, c_file.ff_name);
  333.          break;
  334.       } else     {
  335.          j = findnext (&c_file);
  336.       }
  337.    }
  338.  
  339.    while (j == 0)   {
  340.       j = findnext (&c_file);
  341.       temp = strrchr (c_file.ff_name, '.');
  342.       if (temp)   {
  343.          temp++;
  344.          if (isdigit (*temp) &&
  345.              isdigit (*(temp+1)) && 
  346.              isdigit (*(temp+2)))      {
  347.             k = atoi (temp);
  348.  
  349.             if ((i < k && (k - i) < 300) ||
  350.                 (k < i && (k - i) > 300))     {
  351.                i = k;
  352.                strcpy (old_name, c_file.ff_name);
  353.             }
  354.          }
  355.       }
  356.       
  357.    }
  358.    return;
  359. }
  360.  
  361. get_diff_name (char *temp_name)
  362. {
  363. char           *temp;
  364. int             i, j, k;
  365. struct ffblk   c_file;
  366.  
  367.    temp = (char *)malloc (80);
  368.    if ((j = findfirst (temp_name,&c_file,0))!=0)      {
  369.       return (1);
  370.    }
  371.  
  372.    while (j == 0)      {
  373.       temp = strrchr (c_file.ff_name, '.');
  374.       if (temp)   {
  375.          temp++;
  376.          if (isdigit (*temp) && 
  377.              isdigit (*(temp+1)) && 
  378.              isdigit (*(temp+2)))     {
  379.             i = atoi (temp);
  380.             strcpy (diff_name, c_file.ff_name);
  381.             break;
  382.          } else  {
  383.             j = findnext (&c_file);
  384.          }
  385.       }
  386.    }
  387.  
  388.    if (j != 0)
  389.       return (1);
  390.  
  391.    while (j == 0)  {
  392.       j = findnext (&c_file);
  393.       temp = strrchr (c_file.ff_name, '.');
  394.       if (temp)   {
  395.          temp++;
  396.          if (isdigit (*temp) && 
  397.              isdigit (*(temp+1)) && 
  398.              isdigit (*(temp+2)))     {
  399.             k = atoi (temp);
  400.             if ((i < k && (k - i) < 300) ||
  401.                 (k < i && (k - i) > 300))  {
  402.                i = k;
  403.                strcpy (diff_name, c_file.ff_name);
  404.             }
  405.          }
  406.       }
  407.    }
  408.    temp = strrchr (diff_name, '.');
  409.    temp++;
  410.    strcat (new_name, temp);
  411.    return (0);
  412. }
  413.