File: rc.c

package info (click to toggle)
grep-dctrl 1.3a
  • links: PTS
  • area: main
  • in suites: potato
  • size: 660 kB
  • ctags: 403
  • sloc: ansic: 4,004; sh: 358; makefile: 226; sed: 93
file content (151 lines) | stat: -rw-r--r-- 3,801 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*   grep-dctrl - grep Debian control files
     Copyright (C) 1999  Antti-Juhani Kaijanaho
  
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
  
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details. 
  
     You should have received a copy of the GNU General Public License
     along with this program; see the file COPYING.  If not, write to
     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
  
     The author can be reached via mail at (ISO 8859-1 charset for the city)
        Antti-Juhani Kaijanaho
        Helvintie 2 e as 9
        FIN-40500 JYVSKYL
        FINLAND
        EUROPE
     and via electronic mail from
        gaia@iki.fi
     If you have a choice, use the email address; it is more likely to
     stay current.

*/

#include <assert.h>
#include <errno.h>
#include <publib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "msg.h"
#include "rc.h"
#include "strutil.h"

static char *
fnqualify_xalloc (const char * fname)
{
  char * rv = 0;
  size_t rv_len = 0;
  int actual_len = 0;

  while (actual_len >= rv_len)
    {
      rv = xrealloc (rv, rv_len += 64);
      actual_len = fnqualify (rv, fname, rv_len);
    }
  
  return rv;
}

const char *
find_ifile_by_exename (const char * exename, const char * rcfname)
{
  static const char * default_rcfiles [] = {
    /* Order is significant: earlier files override later ones. */
    "~/.grep-dctrlrc",
    SYSCONF "/grep-dctrl.rc",
    0 };
  int i;
  const char * rv = 0;
  char * fname;
  int lineno;
  FILE * f;
  
  assert (exename != 0);
  
  if (rcfname == 0)
    {
      for (i = 0; rv == 0 && default_rcfiles [i] != 0; i++)
        rv = find_ifile_by_exename (exename, default_rcfiles [i]);
      return rv;
    }
  
  assert (rcfname != 0);

  fname = fnqualify_xalloc (rcfname);

  message (L_INFORMATIONAL, _("reading config file"), fname);

  f = fopen (fname, "r");
  if (f == 0)
    {
      message (L_INFORMATIONAL, strerror (errno), fname);
      return 0;
    }

  lineno = 0;
  while (1)
    {
      static char * line = 0;
      char * line_exe;
      char * line_ifile;

      /* If this is not the first call, line may be non-null and must
         be freed.  It must be freed on all non-first iterations,
         too. */
      free (line);

      line = xgetaline (f);
      ++lineno;
      if (line == 0)
        {
          rv = 0;
          break;
        }

      chop_comment (line, '#');

      if (left_trimmed (line) [0] == 0)
        continue;

      line_exe = strtok (line, " \t");
      if (line_exe == 0)
        {
          line_message (L_IMPORTANT, _("syntax error: need a executable name"),
                        fname, lineno);
          continue;
        }

      line_ifile = strtok (0, " \t");
      if (line_ifile == 0)
        {
          line_message (L_IMPORTANT,
                        _("syntax error: need an input file name"),
                        fname, lineno);
          continue;
        }

      message (L_INFORMATIONAL, _("considering executable name"), line_exe);
      if (strcmp (exename, line_exe) == 0)
        {
          message (L_INFORMATIONAL, _("yes, will use executable name"), line_exe);
          rv = line_ifile;
          message (L_INFORMATIONAL, _("default input file"), rv);
          break;
        }
    }

  fclose (f);
  free (fname);
  return rv;

}