1 /* Implementation for "cvs watch add", "cvs watchers", and related commands
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details. */
18 const char *const watch_usage[] =
20 "Usage: %s %s {on|off|add|remove} [-lR] [-a <action>]... [<path>]...\n",
21 "on/off: turn on/off read-only checkouts of files\n",
22 "add/remove: add or remove notification on actions\n",
23 "-l (on/off/add/remove): Local directory only, not recursive\n",
24 "-R (on/off/add/remove): Process directories recursively (default)\n",
25 "-a (add/remove): Specify what actions, one of\n",
26 " edit,unedit,commit,all,none (defaults to all, multiple -a\n",
27 " options are permitted)\n",
28 "(Specify the --help global option for a list of other help options)\n",
32 static struct addremove_args the_args;
35 watch_modify_watchers (file, what)
37 struct addremove_args *what;
39 char *curattr = fileattr_get0 (file, "_watchers");
47 size_t mynewattr_size;
50 int add_unedit_pending;
51 int add_commit_pending;
52 int remove_edit_pending;
53 int remove_unedit_pending;
54 int remove_commit_pending;
55 int add_tedit_pending;
56 int add_tunedit_pending;
57 int add_tcommit_pending;
60 who_len = strlen (who);
62 /* Look for current watcher types for this user. */
68 if (strncmp (who, p, who_len) == 0
71 /* Found this user. */
72 mycurattr = p + who_len + 1;
80 if (mycurattr != NULL)
82 mycurattr = xstrdup (mycurattr);
83 p = strchr (mycurattr, ',');
88 /* Now copy mycurattr to mynewattr, making the requisite modifications.
89 Note that we add a dummy '+' to the start of mynewattr, to reduce
90 special cases (but then we strip it off when we are done). */
92 mynewattr_size = sizeof "+edit+unedit+commit+tedit+tunedit+tcommit";
93 if (mycurattr != NULL)
94 mynewattr_size += strlen (mycurattr);
95 mynewattr = xmalloc (mynewattr_size);
98 add_edit_pending = what->adding && what->edit;
99 add_unedit_pending = what->adding && what->unedit;
100 add_commit_pending = what->adding && what->commit;
101 remove_edit_pending = !what->adding && what->edit;
102 remove_unedit_pending = !what->adding && what->unedit;
103 remove_commit_pending = !what->adding && what->commit;
104 add_tedit_pending = what->add_tedit;
105 add_tunedit_pending = what->add_tunedit;
106 add_tcommit_pending = what->add_tcommit;
108 /* Copy over existing watch types, except those to be removed. */
112 pend = strchr (p, '+');
115 pend = p + strlen (p);
121 /* Process this item. */
122 if (pend - p == 4 && strncmp ("edit", p, 4) == 0)
124 if (!remove_edit_pending)
125 strcat (mynewattr, "+edit");
126 add_edit_pending = 0;
128 else if (pend - p == 6 && strncmp ("unedit", p, 6) == 0)
130 if (!remove_unedit_pending)
131 strcat (mynewattr, "+unedit");
132 add_unedit_pending = 0;
134 else if (pend - p == 6 && strncmp ("commit", p, 6) == 0)
136 if (!remove_commit_pending)
137 strcat (mynewattr, "+commit");
138 add_commit_pending = 0;
140 else if (pend - p == 5 && strncmp ("tedit", p, 5) == 0)
142 if (!what->remove_temp)
143 strcat (mynewattr, "+tedit");
144 add_tedit_pending = 0;
146 else if (pend - p == 7 && strncmp ("tunedit", p, 7) == 0)
148 if (!what->remove_temp)
149 strcat (mynewattr, "+tunedit");
150 add_tunedit_pending = 0;
152 else if (pend - p == 7 && strncmp ("tcommit", p, 7) == 0)
154 if (!what->remove_temp)
155 strcat (mynewattr, "+tcommit");
156 add_tcommit_pending = 0;
162 /* Copy over any unrecognized watch types, for future
164 mp = mynewattr + strlen (mynewattr);
166 strncpy (mp, p, pend - p);
167 *(mp + (pend - p)) = '\0';
170 /* Set up for next item. */
174 /* Add in new watch types. */
175 if (add_edit_pending)
176 strcat (mynewattr, "+edit");
177 if (add_unedit_pending)
178 strcat (mynewattr, "+unedit");
179 if (add_commit_pending)
180 strcat (mynewattr, "+commit");
181 if (add_tedit_pending)
182 strcat (mynewattr, "+tedit");
183 if (add_tunedit_pending)
184 strcat (mynewattr, "+tunedit");
185 if (add_tcommit_pending)
186 strcat (mynewattr, "+tcommit");
192 fileattr_modify (curattr,
194 mynewattr[0] == '\0' ? NULL : mynewattr + 1,
197 /* If the attribute is unchanged, don't rewrite the attribute file. */
198 if (!((curattr_new == NULL && curattr == NULL)
199 || (curattr_new != NULL
201 && strcmp (curattr_new, curattr) == 0)))
205 if (curattr_new != NULL)
211 if (mycurattr != NULL)
213 if (mynewattr != NULL)
217 static int addremove_fileproc PROTO ((void *callerdat,
218 struct file_info *finfo));
221 addremove_fileproc (callerdat, finfo)
223 struct file_info *finfo;
225 watch_modify_watchers (finfo->file, &the_args);
231 static int addremove_filesdoneproc PROTO ((void *, int, const char *,
232 const char *, List *));
235 addremove_filesdoneproc (callerdat, err, repository, update_dir, entries)
238 const char *repository;
239 const char *update_dir;
242 if (the_args.setting_default)
243 watch_modify_watchers (NULL, &the_args);
247 static int watch_addremove PROTO ((int argc, char **argv));
250 watch_addremove (argc, argv)
264 while ((c = getopt (argc, argv, "+lRa:")) != -1)
276 if (strcmp (optarg, "edit") == 0)
278 else if (strcmp (optarg, "unedit") == 0)
280 else if (strcmp (optarg, "commit") == 0)
282 else if (strcmp (optarg, "all") == 0)
288 else if (strcmp (optarg, "none") == 0)
313 #ifdef CLIENT_SUPPORT
314 if (current_parsed_root->isremote)
321 /* FIXME: copes poorly with "all" if server is extended to have
322 new watch types and client is still running an old version. */
324 option_with_arg ("-a", "edit");
326 option_with_arg ("-a", "unedit");
328 option_with_arg ("-a", "commit");
329 if (!the_args.edit && !the_args.unedit && !the_args.commit)
330 option_with_arg ("-a", "none");
332 send_files (argc, argv, local, 0, SEND_NO_CONTENTS);
333 send_file_names (argc, argv, SEND_EXPAND_WILD);
334 send_to_server (the_args.adding ?
335 "watch-add\012" : "watch-remove\012",
337 return get_responses_and_close ();
339 #endif /* CLIENT_SUPPORT */
341 the_args.setting_default = (argc <= 0);
343 lock_tree_for_write (argc, argv, local, W_LOCAL, 0);
345 err = start_recursion (addremove_fileproc, addremove_filesdoneproc,
346 (DIRENTPROC) NULL, (DIRLEAVEPROC) NULL, NULL,
347 argc, argv, local, W_LOCAL, 0, CVS_LOCK_NONE,
348 (char *) NULL, 1, (char *) NULL);
355 watch_add (argc, argv)
360 return watch_addremove (argc, argv);
364 watch_remove (argc, argv)
369 return watch_addremove (argc, argv);
379 if (strcmp (argv[1], "on") == 0)
383 return watch_on (argc, argv);
385 else if (strcmp (argv[1], "off") == 0)
389 return watch_off (argc, argv);
391 else if (strcmp (argv[1], "add") == 0)
395 return watch_add (argc, argv);
397 else if (strcmp (argv[1], "remove") == 0)
401 return watch_remove (argc, argv);
408 static const char *const watchers_usage[] =
410 "Usage: %s %s [-lR] [files...]\n",
411 "\t-l\tProcess this directory only (not recursive).\n",
412 "\t-R\tProcess directories recursively.\n",
413 "(Specify the --help global option for a list of other help options)\n",
417 static int watchers_fileproc PROTO ((void *callerdat,
418 struct file_info *finfo));
421 watchers_fileproc (callerdat, finfo)
423 struct file_info *finfo;
428 them = fileattr_get0 (finfo->file, "_watchers");
432 cvs_output (finfo->fullname, 0);
437 cvs_output ("\t", 1);
438 while (*p != '>' && *p != '\0')
442 /* Only happens if attribute is misformed. */
443 cvs_output ("\n", 1);
447 cvs_output ("\t", 1);
450 while (*p != '+' && *p != ',' && *p != '\0')
454 cvs_output ("\n", 1);
463 cvs_output ("\t", 1);
465 cvs_output ("\n", 1);
473 watchers (argc, argv)
481 usage (watchers_usage);
484 while ((c = getopt (argc, argv, "+lR")) != -1)
496 usage (watchers_usage);
503 #ifdef CLIENT_SUPPORT
504 if (current_parsed_root->isremote)
512 send_files (argc, argv, local, 0, SEND_NO_CONTENTS);
513 send_file_names (argc, argv, SEND_EXPAND_WILD);
514 send_to_server ("watchers\012", 0);
515 return get_responses_and_close ();
517 #endif /* CLIENT_SUPPORT */
519 return start_recursion (watchers_fileproc, (FILESDONEPROC) NULL,
520 (DIRENTPROC) NULL, (DIRLEAVEPROC) NULL, NULL,
521 argc, argv, local, W_LOCAL, 0, CVS_LOCK_READ,
522 (char *) NULL, 1, (char *) NULL);