xref: /openbsd/gnu/usr.bin/cvs/src/cvsrc.c (revision 07ea8d15)
1 /*
2  *    Copyright (c) 1993 david d zuhn
3  *
4  *    written by david d `zoo' zuhn while at Cygnus Support
5  *
6  *    You may distribute under the terms of the GNU General Public License
7  *    as specified in the README file that comes with the CVS 1.4 kit.
8  *
9  */
10 
11 
12 #include "cvs.h"
13 #include "getline.h"
14 
15 /* this file is to be found in the user's home directory */
16 
17 #ifndef	CVSRC_FILENAME
18 #define	CVSRC_FILENAME	".cvsrc"
19 #endif
20 char cvsrc[] = CVSRC_FILENAME;
21 
22 #define	GROW	10
23 
24 extern char *strtok ();
25 
26 /* Read cvsrc, processing options matching CMDNAME ("cvs" for global
27    options, and update *ARGC and *ARGV accordingly.  */
28 
29 void
30 read_cvsrc (argc, argv, cmdname)
31     int *argc;
32     char ***argv;
33     char *cmdname;
34 {
35     char *homedir;
36     char *homeinit;
37     FILE *cvsrcfile;
38 
39     char *line;
40     int line_length;
41     size_t line_chars_allocated;
42 
43     char *optstart;
44 
45     int command_len;
46     int found = 0;
47 
48     int i;
49 
50     int new_argc;
51     int max_new_argv;
52     char **new_argv;
53 
54     /* don't do anything if argc is -1, since that implies "help" mode */
55     if (*argc == -1)
56 	return;
57 
58     /* setup the new options list */
59 
60     new_argc = 1;
61     max_new_argv = (*argc) + GROW;
62     new_argv = (char **) xmalloc (max_new_argv * sizeof (char*));
63     new_argv[0] = xstrdup ((*argv)[0]);
64 
65     /* determine filename for ~/.cvsrc */
66 
67     homedir = get_homedir ();
68     if (!homedir)
69 	return;
70 
71     homeinit = (char *) xmalloc (strlen (homedir) + strlen (cvsrc) + 10);
72     strcpy (homeinit, homedir);
73     strcat (homeinit, "/");
74     strcat (homeinit, cvsrc);
75 
76     /* if it can't be read, there's no point to continuing */
77 
78     if (!isreadable (homeinit))
79     {
80 	free (homeinit);
81 	return;
82     }
83 
84     /* now scan the file until we find the line for the command in question */
85 
86     line = NULL;
87     line_chars_allocated = 0;
88     command_len = strlen (cmdname);
89     cvsrcfile = open_file (homeinit, "r");
90     while ((line_length = getline (&line, &line_chars_allocated, cvsrcfile))
91 	   >= 0)
92     {
93 	/* skip over comment lines */
94 	if (line[0] == '#')
95 	    continue;
96 
97 	/* stop if we match the current command */
98 	if (!strncmp (line, cmdname, command_len)
99 	    && isspace (*(line + command_len)))
100 	{
101 	    found = 1;
102 	    break;
103 	}
104     }
105 
106     fclose (cvsrcfile);
107 
108     if (found)
109     {
110 	/* skip over command in the options line */
111 	optstart = strtok (line + command_len, "\t \n");
112 
113 	while (optstart)
114 	{
115 	    new_argv [new_argc] = xstrdup (optstart);
116 	    new_argv [new_argc+1] = NULL;
117 	    new_argc += 1;
118 
119 	    if (new_argc >= max_new_argv)
120 	    {
121 		char **tmp_argv;
122 		max_new_argv += GROW;
123 		tmp_argv = (char **) xmalloc (max_new_argv * sizeof (char*));
124 		for (i = 0; i <= new_argc; i++)
125 		    tmp_argv[i] = new_argv[i];
126 		free(new_argv);
127 		new_argv = tmp_argv;
128 	    }
129 	    optstart = strtok (NULL, "\t \n");
130 	}
131     }
132 
133     if (line != NULL)
134 	free (line);
135 
136     /* now copy the remaining arguments */
137 
138     for (i=1; i < *argc; i++)
139     {
140 	new_argv [new_argc] = (*argv)[i];
141 	new_argc += 1;
142     }
143 
144     *argc = new_argc;
145     *argv = new_argv;
146 
147     free (homeinit);
148     return;
149 }
150