2 * Copyright (c) 1992, Brian Berliner and Jeff Polk
3 * Copyright (c) 1989-1992, Brian Berliner
4 * Copyright (c) 2001, Tony Hoyle
5 * Copyright (c) 2004, Derek R. Price & Ximbiot <http://ximbiot.com>
7 * You may distribute under the terms of the GNU General Public License as
8 * specified in the README file that comes with the CVS source distribution.
10 * Query CVS/Entries from server
16 static int ls_proc (int argc, char **argv, char *xwhere, char *mwhere,
17 char *mfile, int shorten, int local, char *mname,
20 static const char *const ls_usage[] =
22 "Usage: %s %s [-e | -l] [-RP] [-r rev] [-D date] [path...]\n",
23 "\t-d\tShow dead revisions (with tag when specified).\n",
24 "\t-e\tDisplay in CVS/Entries format.\n",
25 "\t-l\tDisplay all details.\n",
26 "\t-P\tPrune empty directories.\n",
27 "\t-R\tList recursively.\n",
28 "\t-r rev\tShow files with revision or tag.\n",
29 "\t-D date\tShow files from date.\n",
30 "(Specify the --help global option for a list of other help options)\n",
34 static bool entries_format;
35 static bool long_format;
36 static char *show_tag;
37 static char *show_date;
39 static char *created_dir;
40 static bool tag_validated;
42 static bool ls_prune_dirs;
43 static char *regexp_match;
45 static bool show_dead_revs;
50 ls (int argc, char **argv)
55 is_rls = strcmp (cvs_cmd_name, "rls") == 0;
60 entries_format = false;
64 tag_validated = false;
66 ls_prune_dirs = false;
67 show_dead_revs = false;
71 while ((c = getopt (argc, argv,
73 server_active ? "qdelr:D:PR" :
74 #endif /* SERVER_SUPPORT */
85 "`%s ls -q' is deprecated. Please use the global `-q' option instead.",
92 #endif /* SERVER_SUPPORT */
94 show_dead_revs = true;
97 entries_format = true;
106 show_date = Make_Date (optarg);
109 ls_prune_dirs = true;
123 if (entries_format && long_format)
125 error (0, 0, "`-e' & `-l' are mutually exclusive.");
131 #ifdef CLIENT_SUPPORT
132 if (current_parsed_root->isremote)
134 /* We're the local client. Fire up the remote server. */
139 if (is_rls ? !(supported_request ("rlist") || supported_request ("ls"))
140 : !supported_request ("list"))
141 error (1, 0, "server does not support %s", cvs_cmd_name);
143 if (quiet && !supported_request ("global-list-quiet"))
156 option_with_arg ("-r", show_tag);
158 client_senddate (show_date);
165 for (i = 0; i < argc; i++)
167 if (supported_request ("rlist"))
168 send_to_server ("rlist\012", 0);
170 /* For backwards compatibility with CVSNT... */
171 send_to_server ("ls\012", 0);
175 /* Setting this means, I think, that any empty directories created
176 * by the server will be deleted by the client. Since any dirs
177 * created at all by ls should remain empty, this should cause any
178 * dirs created by the server for the ls command to be deleted.
180 client_prune_dirs = 1;
182 /* I explicitly decide not to send contents here. We *could* let
183 * the user pull status information with this command, but why
184 * don't they just use update or status?
186 send_files (argc, argv, !recurse, 0, SEND_NO_CONTENTS);
187 send_file_names (argc, argv, SEND_EXPAND_WILD);
188 send_to_server ("list\012", 0);
191 err = get_responses_and_close ();
203 for (i = 0; i < argc; i++)
205 char *mod = xstrdup (argv[i]);
208 for (p=strchr (mod,'\\'); p; p=strchr (p,'\\'))
211 p = strrchr (mod,'/');
212 if (p && (strchr (p,'?') || strchr (p,'*')))
220 /* Frontends like to do 'ls -q /', so we support it explicitly.
222 if (!strcmp (mod,"/"))
227 err += do_module (db, mod, MISC, "Listing",
228 ls_proc, NULL, 0, 0, 0, 0, NULL);
235 /* should be ".", but do_recursion()
236 fails this: assert ( strstr ( repository, "/./" ) == NULL ); */
237 char *topmod = xstrdup ("");
238 err += do_module (db, topmod, MISC, "Listing",
239 ls_proc, NULL, 0, 0, 0, 0, NULL);
245 ls_proc (argc + 1, argv - 1, NULL, NULL, NULL, 0, 0, NULL, NULL);
252 struct long_format_data
260 ls_print (Node *p, void *closure)
264 struct long_format_data *data = p->data;
265 cvs_output_tagged ("text", data->header);
267 cvs_output_tagged ("date", data->time);
269 cvs_output_tagged ("text", data->footer);
270 cvs_output_tagged ("newline", NULL);
273 cvs_output (p->data, 0);
280 ls_print_dir (Node *p, void *closure)
282 static bool printed = false;
284 if (recurse && !(ls_prune_dirs && list_isempty (p->data)))
286 /* Keep track of whether we've printed. If we have, then put a blank
287 * line before directory headers, to separate the header from the
288 * listing of the previous directory.
291 cvs_output ("\n", 1);
295 if (!strcmp (p->key, ""))
298 cvs_output (p->key, 0);
299 cvs_output (":\n", 2);
301 walklist (p->data, ls_print, NULL);
308 * Delproc for a node containing a struct long_format_data as data.
311 long_format_data_delproc (Node *n)
313 struct long_format_data *data = (struct long_format_data *)n->data;
314 if (data->header) free (data->header);
315 if (data->time) free (data->time);
316 if (data->footer) free (data->footer);
322 /* A delproc for a List Node containing a List *. */
326 dellist ((List **)&p->data);
332 * Add a file to our list of data to print for a directory.
336 ls_fileproc (void *callerdat, struct file_info *finfo)
342 const char *filename;
346 #ifdef FILENAMES_CASE_INSENSITIVE
347 re_set_syntax (REG_ICASE|RE_SYNTAX_EGREP);
349 re_set_syntax (RE_SYNTAX_EGREP);
351 if ((regex_err = re_comp (regexp_match)) != NULL)
353 error (1, 0, "bad regular expression passed to 'ls': %s",
356 if (re_exec (finfo->file) == 0)
357 return 0; /* no match */
360 vers = Version_TS (finfo, NULL, show_tag, show_date, 1, 0);
361 /* Skip dead revisions unless specifically requested to do otherwise.
362 * We also bother to check for long_format so we can print the state.
364 if (vers->vn_rcs && (!show_dead_revs || long_format))
365 isdead = RCS_isdead (finfo->rcs, vers->vn_rcs);
368 if (!vers->vn_rcs || (!show_dead_revs && isdead))
374 p = findnode (callerdat, finfo->update_dir);
377 /* This only occurs when a complete path to a file is specified on the
378 * command line. Put the file in the root list.
380 filename = finfo->fullname;
382 /* Add update_dir node. */
383 p = findnode (callerdat, ".");
387 p->key = xstrdup (".");
388 p->data = getlist ();
389 p->delproc = ls_delproc;
390 addnode (callerdat, p);
394 filename = finfo->file;
399 char *outdate = entries_time (RCS_getrevtime (finfo->rcs, vers->vn_rcs,
401 n->data = Xasprintf ("/%s/%s/%s/%s/%s%s\n",
402 filename, vers->vn_rcs,
403 outdate, vers->options,
404 show_tag ? "T" : "", show_tag ? show_tag : "");
407 else if (long_format)
409 struct long_format_data *out =
410 xmalloc (sizeof (struct long_format_data));
411 out->header = Xasprintf ("%-5.5s",
412 vers->options[0] != '\0' ? vers->options
414 /* FIXME: Do we want to mimc the real `ls' command's date format? */
415 out->time = gmformat_time_t (RCS_getrevtime (finfo->rcs, vers->vn_rcs,
417 out->footer = Xasprintf (" %-9.9s%s %s%s", vers->vn_rcs,
418 strlen (vers->vn_rcs) > 9 ? "+" : " ",
419 show_dead_revs ? (isdead ? "dead " : " ")
423 n->delproc = long_format_data_delproc;
426 n->data = Xasprintf ("%s\n", filename);
428 addnode (p->data, n);
437 * Add this directory to the list of data to be printed for a directory and
438 * decide whether to tell the recursion processor whether to continue
442 ls_direntproc (void *callerdat, const char *dir, const char *repos,
443 const char *update_dir, List *entries)
448 /* Due to the way we called start_recursion() from ls_proc() with a single
449 * argument at a time, we can assume that if we don't yet have a parent
450 * directory in DIRS then this directory should be processed.
453 if (strcmp (dir, "."))
455 /* Search for our parent directory. */
457 parent = xmalloc (strlen (update_dir) - strlen (dir) + 1);
458 strncpy (parent, update_dir, strlen (update_dir) - strlen (dir));
459 parent[strlen (update_dir) - strlen (dir)] = '\0';
460 strip_trailing_slashes (parent);
461 p = findnode (callerdat, parent);
468 /* Push this dir onto our parent directory's listing. */
472 n->data = Xasprintf ("D/%s////\n", dir);
473 else if (long_format)
475 struct long_format_data *out =
476 xmalloc (sizeof (struct long_format_data));
477 out->header = xstrdup ("d--- ");
478 out->time = gmformat_time_t (unix_time_stamp (repos));
479 out->footer = Xasprintf ("%12s%s%s", "",
480 show_dead_revs ? " " : "", dir);
482 n->delproc = long_format_data_delproc;
485 n->data = Xasprintf ("%s\n", dir);
487 addnode (p->data, n);
492 /* Create a new list for this directory. */
494 p->key = xstrdup (strcmp (update_dir, ".") ? update_dir : "");
495 p->data = getlist ();
496 p->delproc = ls_delproc;
497 addnode (callerdat, p);
499 /* Create a local directory and mark it as needing deletion. This is
500 * the behavior the recursion processor relies upon, a la update &
506 if (show_tag == NULL && show_date == NULL)
508 ParseTag (&show_tag, &show_date, &nonbranch);
513 created_dir = xstrdup (update_dir);
515 make_directory (dir);
516 Create_Admin (dir, update_dir, repos, show_tag, show_date,
518 Subdir_Register (entries, NULL, dir);
521 /* Tell do_recursion to keep going. */
532 /* Clean up tags, dates, and dirs if we created this directory.
535 ls_dirleaveproc (void *callerdat, const char *dir, int err,
536 const char *update_dir, List *entries)
538 if (created_dir && !strcmp (created_dir, update_dir))
542 if (show_tag) free (show_tag);
543 if (show_date) free (show_date);
544 show_tag = show_date = NULL;
548 (void)CVS_CHDIR ("..");
549 if (unlink_file_dir (dir))
550 error (0, errno, "Failed to remove directory `%s'",
552 Subdir_Deregister (entries, NULL, dir);
563 ls_proc (int argc, char **argv, char *xwhere, char *mwhere, char *mfile,
564 int shorten, int local, char *mname, char *msg)
577 error (0, 0, "Listing module: `%s'",
578 strcmp (mname, "") ? mname : ".");
580 repository = xmalloc (strlen (current_parsed_root->directory)
582 + (mfile == NULL ? 0 : strlen (mfile) + 1)
584 (void)sprintf (repository, "%s/%s", current_parsed_root->directory,
586 where = xmalloc (strlen (argv[0])
587 + (mfile == NULL ? 0 : strlen (mfile) + 1)
589 (void)strcpy (where, argv[0]);
591 /* If mfile isn't null, we need to set up to do only part of the
599 /* If the portion of the module is a path, put the dir part on
602 if ((cp = strrchr (mfile, '/')) != NULL)
605 (void)strcat (repository, "/");
606 (void)strcat (repository, mfile);
607 (void)strcat (where, "/");
608 (void)strcat (where, mfile);
612 /* take care of the rest */
613 path = Xasprintf ("%s/%s", repository, mfile);
616 /* directory means repository gets the dir tacked on */
617 (void)strcpy (repository, path);
618 (void)strcat (where, "/");
619 (void)strcat (where, mfile);
630 /* cd to the starting repository */
631 if (CVS_CHDIR (repository) < 0)
633 error (0, errno, "cannot chdir to %s", repository);
645 which = W_LOCAL | W_REPOS;
648 if (show_tag || show_date || show_dead_revs)
651 if (show_tag != NULL && !tag_validated)
653 tag_check_valid (show_tag, argc - 1, argv + 1, local, 0, repository,
655 tag_validated = true;
658 /* Loop on argc so that we are guaranteed that any directory passed to
659 * ls_direntproc should be processed if its parent is not yet in DIRS.
663 List *dirs = getlist ();
664 err = start_recursion (ls_fileproc, NULL, ls_direntproc,
665 ls_dirleaveproc, dirs, 0, NULL, local, which, 0,
666 CVS_LOCK_READ, where, 1, repository);
667 walklist (dirs, ls_print_dir, NULL);
672 for (i = 1; i < argc; i++)
674 List *dirs = getlist ();
675 err = start_recursion (ls_fileproc, NULL, ls_direntproc,
676 NULL, dirs, 1, argv + i, local, which, 0,
677 CVS_LOCK_READ, where, 1, repository);
678 walklist (dirs, ls_print_dir, NULL);
683 if (!(which & W_LOCAL)) free (repository);
684 if (where) free (where);