home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / LharcUUCP0_22.lha / LharcUUCP / source / RCS / ConfigFiles.c,v next >
Encoding:
Text File  |  1993-12-15  |  12.0 KB  |  441 lines

  1. head    0.4;
  2. access;
  3. symbols;
  4. locks
  5.         simons:0.4; strict;
  6. comment @ * @;
  7.  
  8.  
  9. 0.4
  10. date    93.12.15.20.08.37;      author simons;  state Exp;
  11. branches;
  12. next    0.3;
  13.  
  14. 0.3
  15. date    93.12.11.14.22.18;      author simons;  state Exp;
  16. branches;
  17. next    0.2;
  18.  
  19. 0.2
  20. date    93.12.11.14.16.44;      author simons;  state Exp;
  21. branches;
  22. next    0.1;
  23.  
  24. 0.1
  25. date    93.12.11.14.00.27;      author simons;  state Exp;
  26. branches;
  27. next    ;
  28.  
  29.  
  30. desc
  31. @A set of subroutines designed to handle config file parsing.
  32. @
  33.  
  34.  
  35. 0.4
  36. log
  37. @The buffer GetConfigEntry() returns now isn't static anymore!!
  38. @
  39. text
  40. @/*
  41.  *      $Filename: ConfigFiles.c $
  42.  *      $Revision: 0.3 $
  43.  *      $Date: 1993/12/11 14:22:18 $
  44.  *
  45.  *      Copyright (C) 1993 by Peter Simons <simons@@peti.GUN.de>
  46.  *
  47.  *      This program is free software; you can redistribute it and/or
  48.  *      modify it under the terms of the GNU General Public License as
  49.  *      published by the Free Software Foundation; either version 2 of
  50.  *      the License, or (at your option) any later version.
  51.  *
  52.  *      This program is distributed in the hope that it will be useful,
  53.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  54.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  55.  *      General Public License for more details.
  56.  *
  57.  *      You should have received a copy of the GNU General Public License
  58.  *      along with this program; if not, write to the Free Software
  59.  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  60.  *
  61.  *
  62.  *      $Id: ConfigFiles.c,v 0.3 1993/12/11 14:22:18 simons Exp simons $
  63.  *
  64.  * ------------------------------- log history ----------------------------
  65.  * $Log: ConfigFiles.c,v $
  66.  * Revision 0.3  1993/12/11  14:22:18  simons
  67.  * Added file-locking.
  68.  *
  69.  * Revision 0.2  1993/12/11  14:16:44  simons
  70.  * Removed the netsupport.library specific stuff and added LoadFile().
  71.  *
  72.  * Revision 0.1  1993/12/11  14:00:27  simons
  73.  * Taken from the netsupport.library. Several changes will be necessary
  74.  * to make it work with LharcUUCP.
  75.  */
  76.  
  77.  
  78. /**************************************************************************
  79.  *                                                                        *
  80.  * SEKTION: Labels, Macros, Switches, Structures                          *
  81.  *                                                                        *
  82.  **************************************************************************/
  83.  
  84. /************************************* Includes ***********/
  85. #include <string.h>
  86. #include <stdlib.h>
  87.  
  88. #include <dos/dos.h>
  89. #include <proto/dos.h>
  90.  
  91. /************************************* Defines ************/
  92.  
  93. /************************************* Prototypes *********/
  94. char * GetConfigEntry(char * , char * , char * );
  95. char * LoadUUConfig(void);
  96. char * GetKeyword(char * , char * );
  97. char * GetKeywordLine(char * , char * );
  98. char * InterpretKeywordLine(char * );
  99. char * LoadFile(char * );
  100.  
  101. void LockFile(char *);
  102. int FileIsLocked(char *);
  103. void UnLockFile(char *);
  104. void UnLockFiles(void);
  105.  
  106. /************************************* global variables ***/
  107.  
  108. static const char __RCSId[] = "$Id: ConfigFiles.c,v 0.3 1993/12/11 14:22:18 simons Exp simons $";
  109.  
  110. /**************************************************************************
  111.  *                                                                        *
  112.  * SEKTION: main functions                                                *
  113.  *                                                                        *
  114.  **************************************************************************/
  115.  
  116. char *GetConfigEntry(char *filename, char *keyword, char *def)
  117. {
  118.         char *parameter, *filebuffer;
  119.  
  120.         /* If no filename is given, we assume the UUCP main config
  121.          * file.
  122.          */
  123.  
  124.         if (filename) {
  125.                 LockFile(filename);
  126.                 filebuffer = LoadFile(filename);
  127.                 UnLockFile(filename);
  128.         }
  129.         else
  130.                 filebuffer = LoadUUConfig();
  131.  
  132.  
  133.         /* If the file has been loaded successfully, find the keyword
  134.          * and returen the parameter.
  135.          */
  136.  
  137.         if (filebuffer) {
  138.                 if (parameter = GetKeywordLine(keyword, filebuffer))
  139.                         parameter = InterpretKeywordLine(parameter);
  140.  
  141.                 free(filebuffer);   /* free filebuffer */
  142.         }
  143.  
  144.         return (parameter) ? parameter : def;
  145. }
  146.  
  147.  
  148.  
  149. /**************************************************************************
  150.  *                                                                        *
  151.  * SEKTION: internal functions                                            *
  152.  *                                                                        *
  153.  **************************************************************************/
  154.  
  155. char *LoadUUConfig(void)
  156. {
  157.         char *filebuffer;
  158.  
  159.  
  160.         LockFile("S:UUConfig");
  161.         filebuffer = LoadFile("S:UUConfig");
  162.         UnLockFile("S:UUConfig");
  163.  
  164.         if (!filebuffer) {
  165.                 LockFile("UULIB:Config");
  166.                 filebuffer = LoadFile("UULIB:Config");
  167.                 UnLockFile("UULIB:Config");
  168.         }
  169.  
  170.         return filebuffer;
  171. }
  172.  
  173.  
  174. char * GetKeyword(char *keyword, char *buffer)
  175. {
  176.         char *parameter;
  177.  
  178.         if (parameter = GetKeywordLine(keyword, buffer))
  179.                 parameter = InterpretKeywordLine(parameter);
  180.  
  181.  
  182.         /* If GetKeywordLine() didn't find the keyword, return a pointer
  183.          * to the provided default. */
  184.  
  185.         if (!(*parameter))
  186.                 parameter = &keyword[strlen(keyword)+1];
  187.  
  188.         return parameter;
  189. }
  190.  
  191.  
  192. char * GetKeywordLine(char *keyword, char *buffer)
  193. {
  194.         int keywdlen;
  195.  
  196.         if (!(keywdlen = strlen(keyword)))
  197.                 return NULL;            /* no keyword provided */
  198.  
  199.         while (strnicmp(keyword, buffer, keywdlen)) {
  200.                 while(*buffer != '\0' && *buffer != '\n')
  201.                         buffer++;       /* search for '\0' or '\n' */
  202.  
  203.                 if (!(*buffer))         /* if '\0', break loop */
  204.                         break;
  205.  
  206.                 buffer++;       /* skip '\n' and compare next line */
  207.         }
  208.  
  209.         return (*buffer) ? buffer : NULL;
  210. }
  211.  
  212. char * InterpretKeywordLine(char *keywordline)
  213. {
  214.         char *parameter;
  215.         int i;
  216.  
  217.         while (*keywordline != ' ' && *keywordline != '\t')
  218.                 keywordline++;  /* skip keyword */
  219.  
  220.         while (*keywordline == ' ' || *keywordline == '\t')
  221.                 keywordline++;  /* skip leading blanks or tabs */
  222.  
  223.         for (i = 0; keywordline[i] != '\n' && keywordline[i] != '\0'; i++)
  224.                 ;               /* find end of line */
  225.  
  226.         while (keywordline[i-1] == ' ' || keywordline[i-1] == '\t')
  227.                 i--;            /* remove trailing blanks or tabs */
  228.  
  229.         if (parameter = malloc(i+1)) {
  230.                 parameter[i] = '\0';    /* send end of string */
  231.                 while (i--)             /* copy string to keywordline */
  232.                         parameter[i] = keywordline[i];
  233.         }
  234.  
  235.         return parameter;
  236. }
  237.  
  238.  
  239.  /*
  240.   * LoadFile() -- determines the length of a given file, allocates the
  241.   * appropiate buffer and actually loads the file. If everything works,
  242.   * the address of the buffer is returned and the length of the file is
  243.   * available via IoErr().
  244.   *
  245.   * ATTENTION: A zero-byte is appended to the loaded file, to enable the
  246.   * usage of standard C string routines on the contents.
  247.   *
  248.   * The buffer should be freed after usage using free().
  249.   */
  250.  
  251. char *LoadFile(char *filename)
  252. {
  253.         BPTR fh, fh2;
  254.         struct FileInfoBlock *fib;
  255.         char *memblock, *filebuffer = NULL;
  256.         BOOL isfile = FALSE;
  257.         int readlen;
  258.  
  259.         if (fh = Lock(filename, ACCESS_READ)) {
  260.                 if (fib = malloc(sizeof(struct FileInfoBlock))) {
  261.                         if (Examine(fh, fib)) {
  262.                                 if (fh2 = OpenFromLock(fh)) {
  263.                                         isfile = TRUE;
  264.                                         if (memblock = malloc(fib->fib_Size + 1)) {
  265.                                                 if ((readlen = Read(fh2, memblock, fib->fib_Size)) != -1L) {
  266.                                                         memblock[readlen] = '\0';
  267.                                                         filebuffer = memblock;
  268.                                                         SetIoErr(readlen);
  269.                                                 }
  270.                                                 else
  271.                                                         free(memblock);
  272.                                         }
  273.                                         Close(fh2);
  274.                                 }
  275.                         }
  276.                         free(fib);
  277.                 }
  278.                 if (!(isfile))
  279.                         UnLock(fh);
  280.         }
  281.  
  282.         return filebuffer;
  283. }
  284.  
  285. @
  286.  
  287.  
  288. 0.3
  289. log
  290. @Added file-locking.
  291. @
  292. text
  293. @d3 2
  294. a4 2
  295.  *      $Revision: 0.2 $
  296.  *      $Date: 1993/12/11 14:16:44 $
  297. d23 1
  298. a23 1
  299.  *      $Id: ConfigFiles.c,v 0.2 1993/12/11 14:16:44 simons Exp simons $
  300. d27 3
  301. d69 1
  302. a69 1
  303. static const char __RCSId[] = "$Id: ConfigFiles.c,v 0.2 1993/12/11 14:16:44 simons Exp simons $";
  304. d79 1
  305. a79 8
  306.         char *parameter = NULL, *filebuffer;
  307.         static char *GCEntryBuffer = NULL;
  308.  
  309.  
  310.         /* Free the buffer we used last time */
  311.  
  312.         if (GCEntryBuffer)
  313.                 free(GCEntryBuffer);
  314. a80 1
  315.  
  316. d105 1
  317. a105 8
  318.         if (parameter) {        /* track buffer for later usage */
  319.                 GCEntryBuffer = parameter;
  320.                 return parameter;
  321.         }
  322.         else {                  /* no buffer allocated--return default */
  323.                 GCEntryBuffer = NULL;
  324.                 return def;
  325.         }
  326. @
  327.  
  328.  
  329. 0.2
  330. log
  331. @Removed the netsupport.library specific stuff and added LoadFile().
  332. @
  333. text
  334. @d3 2
  335. a4 2
  336.  *      $Revision: 0.1 $
  337.  *      $Date: 1993/12/11 14:00:27 $
  338. d23 1
  339. a23 1
  340.  *      $Id: ConfigFiles.c,v 0.1 1993/12/11 14:00:27 simons Exp simons $
  341. d27 3
  342. d59 5
  343. d66 1
  344. a66 1
  345. static const char __RCSId[] = "$Id: ConfigFiles.c,v 0.1 1993/12/11 14:00:27 simons Exp simons $";
  346. d90 2
  347. a91 1
  348.         if (filename)
  349. d93 2
  350. d132 7
  351. a138 1
  352.         if (!(filebuffer = LoadFile("S:UUConfig")))
  353. d140 2
  354. @
  355.  
  356.  
  357. 0.1
  358. log
  359. @Taken from the netsupport.library. Several changes will be necessary
  360. to make it work with LharcUUCP.
  361. @
  362. text
  363. @d3 2
  364. a4 2
  365.  *      $Revision: 1.1 $
  366.  *      $Date: 1993/12/11 13:59:29 $
  367. d23 1
  368. a23 1
  369.  *      $Id: ConfigFiles.c,v 1.1 1993/12/11 13:59:29 simons Exp simons $
  370. d27 3
  371. a29 3
  372.  * Revision 1.1  1993/12/11  13:59:29  simons
  373.  * Initial revision
  374.  *
  375. d41 1
  376. a41 2
  377. #include <proto/utility.h>
  378. #include <exec/types.h>
  379. d43 2
  380. a44 3
  381. #include "libraries/netsupport.h"
  382. #include "NetSupportLibrary.h"
  383. #include "MemoryHandling.h"
  384. d49 8
  385. a56 2
  386. #include "ConfigFiles.h"
  387. #include "SupportRoutines.h"
  388. d58 1
  389. a58 1
  390. static const char __RCSId[] = "$Id: ConfigFiles.c,v 1.1 1993/12/11 13:59:29 simons Exp simons $";
  391. d62 1
  392. a62 1
  393.  * SEKTION: Public library functions                                      *
  394. d66 1
  395. a66 3
  396. STRPTR __asm GetConfigEntry(register __a0 STRPTR filename,
  397.                             register __a1 STRPTR keyword,
  398.                             register __a2 STRPTR def)
  399. d69 1
  400. a69 1
  401.         void *GCEntryBuffer = (GetStaticBuffer())->nspsb_GCEntryBuffer;
  402. d75 1
  403. a75 1
  404.                 NSPFreeMemPooled(GCEntryBuffer);
  405. d88 1
  406. a88 1
  407.         /* If the file has be loaded successfully, find the keyword
  408. d96 1
  409. a96 1
  410.                 NSPFreeMemPooled(filebuffer);   /* free filebuffer */
  411. d111 1
  412. a111 1
  413. /*************************************************************************
  414. d113 1
  415. a113 1
  416.  * SEKTION: Internal library functions                                    *
  417. d117 1
  418. a117 1
  419. char * __asm LoadUUConfig(void)
  420. d128 1
  421. a128 2
  422. char * __asm GetKeyword(register __a0 char *keyword,
  423.                         register __a1 char *buffer)
  424. d146 1
  425. a146 2
  426. char * __asm GetKeywordLine(register __a0 char *keyword,
  427.                             register __a1 char *buffer)
  428. a147 1
  429.         struct UtilityBase *UtilityBase = NetSupportBase->nsp_UtilityBase;
  430. d153 1
  431. a153 1
  432.         while (Strnicmp(keyword, buffer, keywdlen)) {
  433. d166 1
  434. a166 1
  435. char * __asm InterpretKeywordLine(register __a0 char *keywordline)
  436. d183 1
  437. a183 1
  438.         if (parameter = NSPAllocMemPooled(i+1, 0L)) {
  439. d192 46
  440. @
  441.