Line data Source code
1 : /* This file is part of GNU cflow
2 : Copyright (C) 1997, 2005, 2007, 2010 Sergey Poznyakoff
3 :
4 : GNU cflow is free software; you can redistribute it and/or modify
5 : it under the terms of the GNU General Public License as published by
6 : the Free Software Foundation; either version 3 of the License, or
7 : (at your option) any later version.
8 :
9 : GNU cflow is distributed in the hope that it will be useful,
10 : but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : GNU General Public License for more details.
13 :
14 : You should have received a copy of the GNU General Public
15 : License along with GNU cflow; if not, write to the Free Software
16 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 : MA 02110-1301 USA */
18 :
19 : #include <cflow.h>
20 : #include <parser.h>
21 : #include <sys/stat.h>
22 : #include <ctype.h>
23 : #include <argcv.h>
24 :
25 : #ifndef LOCAL_RC
26 : # define LOCAL_RC ".cflowrc"
27 : #endif
28 :
29 : static void
30 0 : expand_argcv(int *argc_ptr, char ***argv_ptr, int argc, char **argv)
31 : {
32 : int i;
33 :
34 0 : *argv_ptr = xrealloc(*argv_ptr,
35 0 : (*argc_ptr + argc + 1) * sizeof **argv_ptr);
36 0 : for (i = 0; i <= argc; i++)
37 0 : (*argv_ptr)[*argc_ptr + i] = argv[i];
38 0 : *argc_ptr += argc;
39 0 : }
40 :
41 : /* Parse rc file
42 : */
43 : void
44 23 : parse_rc(int *argc_ptr, char ***argv_ptr, char *name)
45 : {
46 : struct stat st;
47 : FILE *rcfile;
48 : int size;
49 : char *buf, *p;
50 :
51 23 : if (stat(name, &st))
52 46 : return;
53 0 : buf = malloc(st.st_size+1);
54 0 : if (!buf) {
55 0 : error(0, 0, _("not enough memory to process rc file"));
56 0 : return;
57 : }
58 0 : rcfile = fopen(name, "r");
59 0 : if (!rcfile) {
60 0 : error(0, errno, _("cannot open `%s'"), name);
61 0 : return;
62 : }
63 0 : size = fread(buf, 1, st.st_size, rcfile);
64 0 : buf[size] = 0;
65 0 : fclose(rcfile);
66 :
67 0 : for (p = strtok(buf, "\n"); p; p = strtok(NULL, "\n")) {
68 : int argc;
69 : char **argv;
70 :
71 0 : argcv_get(p, "", "#", &argc, &argv);
72 0 : expand_argcv(argc_ptr, argv_ptr, argc, argv);
73 0 : free(argv);
74 : }
75 0 : free(buf);
76 : }
77 :
78 : /* Process the value of the environment variable CFLOW_OPTIONS
79 : * and of the rc file.
80 : * Split the value into words and add them between (*ARGV_PTR)[0] and
81 : * (*ARGV_PTR[1]) modifying *ARGC_PTR accordingly.
82 : * NOTE: Since sourcerc() is not meant to take all SH command line processing
83 : * burden, only word splitting is performed and no kind of expansion
84 : * takes place.
85 : */
86 : void
87 23 : sourcerc(int *argc_ptr, char ***argv_ptr)
88 : {
89 : char *env;
90 23 : int xargc = 1;
91 : char **xargv;
92 :
93 23 : xargv = xmalloc(2*sizeof *xargv);
94 23 : xargv[0] = **argv_ptr;
95 23 : xargv[1] = NULL;
96 :
97 23 : env = getenv("CFLOW_OPTIONS");
98 23 : if (env) {
99 : int argc;
100 : char **argv;
101 :
102 0 : argcv_get(env, "", "#", &argc, &argv);
103 0 : expand_argcv(&xargc, &xargv, argc, argv);
104 0 : free(argv);
105 : }
106 :
107 23 : env = getenv("CFLOWRC");
108 23 : if (env)
109 0 : parse_rc(&xargc, &xargv, env);
110 : else {
111 23 : char *home = getenv("HOME");
112 23 : if (home) {
113 23 : int len = strlen(home);
114 23 : char *buf = malloc(len + sizeof(LOCAL_RC)
115 23 : + (home[len-1] != '/') );
116 23 : if (!buf)
117 0 : return;
118 23 : strcpy(buf, home);
119 23 : if (home[len-1] != '/')
120 23 : buf[len++] = '/';
121 23 : strcpy(buf+len, LOCAL_RC);
122 23 : parse_rc(&xargc, &xargv, buf);
123 23 : free(buf);
124 : }
125 : }
126 :
127 23 : if (xargc > 1) {
128 0 : expand_argcv(&xargc, &xargv, *argc_ptr-1, *argv_ptr+1);
129 0 : *argc_ptr = xargc;
130 0 : *argv_ptr = xargv;
131 : }
132 : }
133 :
134 :
135 :
|