1 /* This program is free software; you can redistribute it and/or modify
2 it under the terms of the GNU General Public License as published by
3 the Free Software Foundation; either version 2, or (at your option)
6 This program is distributed in the hope that it will be useful,
7 but WITHOUT ANY WARRANTY; without even the implied warranty of
8 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 GNU General Public License for more details. */
15 Original Author: athan@morgan.com <Andrew C. Athan> 2/1/94
16 Modified By: vdemarco@bou.shl.com
18 This package was written to support the NEXTSTEP concept of
19 "wrappers." These are essentially directories that are to be
20 treated as "files." This package allows such wrappers to be
21 "processed" on the way in and out of CVS. The intended use is to
22 wrap up a wrapper into a single tar, such that that tar can be
23 treated as a single binary file in CVS. To solve the problem
24 effectively, it was also necessary to be able to prevent rcsmerge
25 application at appropriate times.
28 Format of wrapper file ($CVSROOT/CVSROOT/cvswrappers or .cvswrappers)
30 wildcard [option value][option value]...
32 where option is one of
33 -m update methodology value: MERGE or COPY
34 -k default -k rcs option to use on import or add
36 and value is a single-quote delimited value.
39 *.nib -f 'gunzipuntar' -t 'targzip' -m 'COPY'
48 WrapMergeMethod mergeMethod;
51 static WrapperEntry **wrap_list=NULL;
52 static WrapperEntry **wrap_saved_list=NULL;
54 static int wrap_size=0;
55 static int wrap_count=0;
56 static int wrap_tempcount=0;
58 /* FIXME: the relationship between wrap_count, wrap_tempcount,
59 * wrap_saved_count, and wrap_saved_tempcount is not entirely clear;
60 * it is certainly suspicious that wrap_saved_count is never set to a
61 * value other than zero! If the variable isn't being used, it should
62 * be removed. And in general, we should describe how temporary
63 * vs. permanent wrappers are implemented, and then make sure the
64 * implementation is actually doing that.
66 * Right now things seem to be working, but that's no guarantee there
67 * isn't a bug lurking somewhere in the murk.
70 static int wrap_saved_count=0;
72 static int wrap_saved_tempcount=0;
74 #define WRAPPER_GROW 8
76 void wrap_add_entry PROTO((WrapperEntry *e,int temp));
77 void wrap_kill PROTO((void));
78 void wrap_kill_temp PROTO((void));
79 void wrap_free_entry PROTO((WrapperEntry *e));
80 void wrap_free_entry_internal PROTO((WrapperEntry *e));
81 void wrap_restore_saved PROTO((void));
85 /* FIXME-reentrancy: if we do a multithreaded server, will need to
86 move this to a per-connection data structure, or better yet
87 think about a cleaner solution. */
88 static int wrap_setup_already_done = 0;
91 if (wrap_setup_already_done != 0)
94 wrap_setup_already_done = 1;
96 if (!current_parsed_root->isremote)
100 file = xmalloc (strlen (current_parsed_root->directory)
101 + sizeof (CVSROOTADM)
102 + sizeof (CVSROOTADM_WRAPPER)
104 /* Then add entries found in repository, if it exists. */
105 (void) sprintf (file, "%s/%s/%s", current_parsed_root->directory, CVSROOTADM,
109 wrap_add_file(file,0);
114 /* Then add entries found in home dir, (if user has one) and file
116 homedir = get_homedir ();
117 /* If we can't find a home directory, ignore ~/.cvswrappers. This may
118 make tracking down problems a bit of a pain, but on the other
119 hand it might be obnoxious to complain when CVS will function
120 just fine without .cvswrappers (and many users won't even know what
124 char *file = strcat_filename_onto_homedir (homedir, CVSDOTWRAPPER);
127 wrap_add_file (file, 0);
132 /* FIXME: calling wrap_add() below implies that the CVSWRAPPERS
133 * environment variable contains exactly one "wrapper" -- a line
136 * FILENAME_PATTERN FLAG OPTS [ FLAG OPTS ...]
138 * This may disagree with the documentation, which states:
141 * A whitespace-separated list of file name patterns that CVS
142 * should treat as wrappers. *Note Wrappers::.
144 * Does this mean the environment variable can hold multiple
145 * wrappers lines? If so, a single call to wrap_add() is
149 /* Then add entries found in CVSWRAPPERS environment variable. */
150 wrap_add (getenv (WRAPPER_ENV), 0);
153 #ifdef CLIENT_SUPPORT
154 /* Send -W arguments for the wrappers to the server. The command must
155 be one that accepts them (e.g. update, import). */
161 for (i = 0; i < wrap_count + wrap_tempcount; ++i)
163 if (wrap_list[i]->tocvsFilter != NULL
164 || wrap_list[i]->fromcvsFilter != NULL)
165 /* For greater studliness we would print the offending option
166 and (more importantly) where we found it. */
168 -t and -f wrapper options are not supported remotely; ignored");
169 if (wrap_list[i]->mergeMethod == WRAP_COPY)
170 /* For greater studliness we would print the offending option
171 and (more importantly) where we found it. */
173 -m wrapper option is not supported remotely; ignored");
174 send_to_server ("Argument -W\012Argument ", 0);
175 send_to_server (wrap_list[i]->wildCard, 0);
176 send_to_server (" -k '", 0);
177 if (wrap_list[i]->rcsOption != NULL)
178 send_to_server (wrap_list[i]->rcsOption, 0);
180 send_to_server ("kv", 0);
181 send_to_server ("'\012", 0);
184 #endif /* CLIENT_SUPPORT */
186 #if defined(SERVER_SUPPORT) || defined(CLIENT_SUPPORT)
187 /* Output wrapper entries in the format of cvswrappers lines.
189 * This is useful when one side of a client/server connection wants to
190 * send its wrappers to the other; since the receiving side would like
191 * to use wrap_add() to incorporate the wrapper, it's best if the
192 * entry arrives in this format.
194 * The entries are stored in `line', which is allocated here. Caller
197 * If first_call_p is nonzero, then start afresh. */
199 wrap_unparse_rcs_options (line, first_call_p)
203 /* FIXME-reentrancy: we should design a reentrant interface, like
204 a callback which gets handed each wrapper (a multithreaded
205 server being the most concrete reason for this, but the
206 non-reentrant interface is fairly unnecessary/ugly). */
212 if (i >= wrap_count + wrap_tempcount) {
217 *line = xmalloc (strlen (wrap_list[i]->wildCard)
220 + (wrap_list[i]->rcsOption != NULL ?
221 strlen (wrap_list[i]->rcsOption) : 2)
223 + 1); /* leave room for '\0' */
225 strcpy (*line, wrap_list[i]->wildCard);
226 strcat (*line, " -k '");
227 if (wrap_list[i]->rcsOption != NULL)
228 strcat (*line, wrap_list[i]->rcsOption);
230 strcat (*line, "kv");
235 #endif /* SERVER_SUPPORT || CLIENT_SUPPORT */
238 * Remove fmt str specifier other than %% or %s. And allow
239 * only max_s %s specifiers
242 wrap_clean_fmt_str(char *fmt, int max_s)
245 if (fmt[0] == '%' && fmt[1])
250 if (fmt[1] == 's' && max_s > 0)
262 * Open a file and read lines, feeding each line to a line parser. Arrange
263 * for keeping a temporary list of wrappers at the end, if the "temp"
267 wrap_add_file (file, temp)
273 size_t line_allocated = 0;
275 wrap_restore_saved ();
279 fp = CVS_FOPEN (file, "r");
282 if (!existence_error (errno))
283 error (0, errno, "cannot open %s", file);
286 while (getline (&line, &line_allocated, fp) >= 0)
287 wrap_add (line, temp);
291 error (0, errno, "cannot read %s", file);
292 if (fclose (fp) == EOF)
293 error (0, errno, "cannot close %s", file);
301 wrap_free_entry(wrap_list[--wrap_count]);
307 WrapperEntry **temps=wrap_list+wrap_count;
309 while(wrap_tempcount)
310 wrap_free_entry(temps[--wrap_tempcount]);
317 wrap_free_entry_internal(e);
322 wrap_free_entry_internal(e)
327 free (e->tocvsFilter);
328 if (e->fromcvsFilter)
329 free (e->fromcvsFilter);
344 wrap_list=wrap_saved_list;
345 wrap_count=wrap_saved_count;
346 wrap_tempcount=wrap_saved_tempcount;
348 wrap_saved_list=NULL;
350 wrap_saved_tempcount=0;
354 wrap_add (line, isTemp)
363 if (!line || line[0] == '#')
366 memset (&e, 0, sizeof(e));
368 /* Search for the wild card */
369 while (*line && isspace ((unsigned char) *line))
372 *line && !isspace ((unsigned char) *line);
381 e.wildCard=xstrdup(temp);
385 /* Search for the option */
386 while(*line && *line!='-')
395 /* Search for the filter commandline */
396 for(++line;*line && *line!='\'';++line);
400 for(temp=++line;*line && (*line!='\'' || line[-1]=='\\');++line)
403 /* This used to "break;" (ignore the option) if there was a
404 single character between the single quotes (I'm guessing
405 that was accidental). Now it "break;"s if there are no
406 characters. I'm not sure either behavior is particularly
407 necessary--the current options might not require ''
408 arguments, but surely some future option legitimately
409 might. Also I'm not sure that ignoring the option is a
410 swift way to handle syntax errors in general. */
418 /* Before this is reenabled, need to address the problem in
420 http://ximbiot.com/cvs/cvshome/docs/infowrapper.html). */
422 "-t/-f wrappers not supported by this version of CVS");
425 free(e.fromcvsFilter);
426 /* FIXME: error message should say where the bad value
428 e.fromcvsFilter=expand_path (temp, "<wrapper>", 0);
429 if (!e.fromcvsFilter)
430 error (1, 0, "Correct above errors first");
433 /* Before this is reenabled, need to address the problem in
435 http://ximbiot.com/cvs/cvshome/docs/infowrapper.html). */
437 "-t/-f wrappers not supported by this version of CVS");
441 /* FIXME: error message should say where the bad value
443 e.tocvsFilter=expand_path (temp, "<wrapper>", 0);
445 error (1, 0, "Correct above errors first");
448 if(*temp=='C' || *temp=='c')
449 e.mergeMethod=WRAP_COPY;
451 e.mergeMethod=WRAP_MERGE;
456 e.rcsOption = strcmp (temp, "kv") ? xstrdup (temp) : NULL;
466 wrap_add_entry(&e, isTemp);
470 wrap_add_entry(e, temp)
475 if(wrap_count+wrap_tempcount>=wrap_size){
476 wrap_size += WRAPPER_GROW;
477 wrap_list = (WrapperEntry **) xrealloc ((char *) wrap_list,
479 sizeof (WrapperEntry *));
482 if(!temp && wrap_tempcount){
483 for(x=wrap_count+wrap_tempcount-1;x>=wrap_count;--x)
484 wrap_list[x+1]=wrap_list[x];
487 x=(temp ? wrap_count+(wrap_tempcount++):(wrap_count++));
488 wrap_list[x]=(WrapperEntry *)xmalloc(sizeof(WrapperEntry));
492 /* Return 1 if the given filename is a wrapper filename */
494 wrap_name_has (name,has)
498 int x,count=wrap_count+wrap_tempcount;
502 if (CVS_FNMATCH (wrap_list[x]->wildCard, name, 0) == 0){
505 temp=wrap_list[x]->tocvsFilter;
508 temp=wrap_list[x]->fromcvsFilter;
511 temp = wrap_list[x]->rcsOption;
524 static WrapperEntry *wrap_matching_entry PROTO ((const char *));
526 static WrapperEntry *
527 wrap_matching_entry (name)
530 int x,count=wrap_count+wrap_tempcount;
533 if (CVS_FNMATCH (wrap_list[x]->wildCard, name, 0) == 0)
535 return (WrapperEntry *)NULL;
538 /* Return the RCS options for FILENAME in a newly malloc'd string. If
539 ASFLAG, then include "-k" at the beginning (e.g. "-kb"), otherwise
540 just give the option itself (e.g. "b"). */
542 wrap_rcsoption (filename, asflag)
543 const char *filename;
546 WrapperEntry *e = wrap_matching_entry (filename);
549 if (e == NULL || e->rcsOption == NULL || (*e->rcsOption == '\0'))
552 buf = xmalloc (strlen (e->rcsOption) + 3);
556 strcat (buf, e->rcsOption);
560 strcpy (buf, e->rcsOption);
566 wrap_tocvs_process_file(fileName)
567 const char *fileName;
569 WrapperEntry *e=wrap_matching_entry(fileName);
570 static char *buf = NULL;
573 if(e==NULL || e->tocvsFilter==NULL)
578 buf = cvs_temp_name ();
580 args = xmalloc (strlen (e->tocvsFilter)
584 wrap_clean_fmt_str(e->tocvsFilter, 2);
585 sprintf (args, e->tocvsFilter, fileName, buf);
587 run_exec(RUN_TTY, RUN_TTY, RUN_TTY, RUN_NORMAL|RUN_REALLY );
594 wrap_merge_is_copy (fileName)
595 const char *fileName;
597 WrapperEntry *e=wrap_matching_entry(fileName);
598 if(e==NULL || e->mergeMethod==WRAP_MERGE)
605 wrap_fromcvs_process_file(fileName)
606 const char *fileName;
609 WrapperEntry *e=wrap_matching_entry(fileName);
611 if(e==NULL || e->fromcvsFilter==NULL)
614 args = xmalloc (strlen (e->fromcvsFilter)
615 + strlen (fileName));
617 wrap_clean_fmt_str(e->fromcvsFilter, 1);
618 sprintf (args, e->fromcvsFilter, fileName);
620 run_exec(RUN_TTY, RUN_TTY, RUN_TTY, RUN_NORMAL );