home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / git-4.3 / git-4 / git-4.3.11 / src / configure.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  3.6 KB  |  195 lines

  1. /* configure.c -- Configuration files management functions.  */
  2.  
  3. /* Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /* Written by Tudor Hulubei and Andrei Pitis.  */
  20.  
  21.  
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25.  
  26. #ifdef HAVE_STDLIB_H
  27. #include <stdlib.h>
  28. #else /* !HAVE_STDLIB_H */
  29. #include "ansi_stdlib.h"
  30. #endif /* !HAVE_STDLIB_H */
  31.  
  32. #include <stdio.h>
  33.  
  34. #include <sys/types.h>
  35. #include "file.h"
  36. #include <ctype.h>
  37.  
  38. #ifdef HAVE_UNISTD_H
  39. #include <unistd.h>
  40. #endif /* HAVE_UNISTD_H */
  41.  
  42. #include "xstring.h"
  43. #include "tty.h"
  44. #include "configure.h"
  45. #include "misc.h"
  46.  
  47.  
  48. static FILE *fileptr;
  49. static int   sectionptr;
  50. static char  line[MAXLINE];
  51.  
  52.  
  53. int
  54. configuration_init(file_name)
  55.     char *file_name;
  56. {
  57.     if (file_name == NULL)
  58.     return 0;
  59.  
  60.     fileptr = fopen(file_name, "r");
  61.  
  62.     if (fileptr == NULL)
  63.     return 0;
  64.  
  65.     sectionptr = -1;
  66.     return 1;
  67. }
  68.  
  69.  
  70. void
  71. configuration_end()
  72. {
  73.     if (fileptr)
  74.     fclose(fileptr);
  75. }
  76.  
  77.  
  78. static int
  79. configuration_getline()
  80. {
  81.     int c;
  82.     size_t len;
  83.     char *comment;
  84.  
  85.     if (fgets(line, MAXLINE, fileptr) == NULL)
  86.     return 0;
  87.  
  88.     if ((len = strlen(line)) == MAXLINE - 1)
  89.     {
  90.     fprintf(stderr, "%s: configuration: line too long. Truncated.\n",
  91.         program);
  92.  
  93.     /* Search the end of this big line.  */
  94.     for (;;)
  95.     {
  96.         c = fgetc(fileptr);
  97.         if (c == '\n' || c == EOF)
  98.         break;
  99.     }
  100.     }
  101.  
  102.     if ((comment = strchr(line, ICS)))
  103.     *comment = 0;
  104.     else
  105.     if (line[len - 1] == '\n')
  106.         line[len - 1] = 0;
  107.  
  108.     return 1;
  109. }
  110.  
  111.  
  112. int
  113. configuration_section(section_name)
  114.     char *section_name;
  115. {
  116.     fseek(fileptr, 0, SEEK_SET);
  117.  
  118.     while (configuration_getline())
  119.     if (strcmp(section_name, line) == 0)
  120.         return sectionptr = ftell(fileptr);
  121.  
  122.     return sectionptr = -1;
  123. }
  124.  
  125.  
  126. void
  127. configuration_getvarinfo(var_name, dest, fields, seek)
  128.     char *var_name, **dest;
  129.     int fields, seek;
  130. {
  131.     int fld;
  132.     char buf[MAXLINE], *ptr, *tmp;
  133.  
  134.     if (seek)
  135.     fseek(fileptr, sectionptr, SEEK_SET);
  136.  
  137.     if (fields == 1)
  138.     *dest = 0;
  139.     else
  140.     memset((char *)dest, 0, fields * sizeof(char *));
  141.  
  142.     while (configuration_getline() && *line)
  143.     {
  144.     *buf = 0;
  145.     sscanf(line, "%s", buf);
  146.  
  147.     if (seek == NO_SEEK)
  148.         buf[32] = 0;        /* Just in case... */
  149.  
  150.     if (!is_print(*buf))
  151.         return;
  152.  
  153.     if (seek == NO_SEEK || strcmp(var_name, buf) == 0)
  154.     {
  155.         if ((ptr = strchr(line, IAS)) && *++ptr)
  156.         {
  157.         for (dest[0] = ptr, fld = 1; *ptr && fld < fields; ptr++)
  158.             if (*ptr == IFS)
  159.             {
  160.             *ptr = 0;
  161.             if (*(ptr + 1) && *(ptr + 1) != IFS)
  162.                 dest[fld] = ptr + 1;
  163.             fld++;
  164.             }
  165.         if ((ptr = strchr(ptr, IFS)))
  166.             *ptr = 0;
  167.         }
  168.  
  169.         for (fld = 0; fld < fields; fld++)
  170.         if (dest[fld])
  171.         {
  172.             while (isspace(*dest[fld]))
  173.             dest[fld]++;
  174.  
  175.             tmp = dest[fld] + strlen(dest[fld]) - 1;
  176.  
  177.             while (isspace(*tmp) && tmp >= dest[fld])
  178.             tmp--;
  179.  
  180.             *(tmp + 1) = 0;
  181.  
  182.             if (dest[fld][0] == 0)
  183.             dest[fld] = NULL;
  184.         }
  185.  
  186.         if (seek == NO_SEEK)
  187.         strcpy(var_name, buf);
  188.         return;
  189.     }
  190.     }
  191.  
  192.     if (seek == NO_SEEK)
  193.     *var_name = 0;
  194. }
  195.