fish docs: Be consistent about using I<-..> for options.
[libguestfs.git] / fish / glob.c
1 /* guestfish - the filesystem interactive shell
2  * Copyright (C) 2009 Red Hat Inc.
3  *
4  * This program 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 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program 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 License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "fish.h"
27
28 /* A bit tricky because in the case where there are multiple
29  * paths we have to perform a Cartesian product.
30  */
31 static void glob_issue (char *cmd, int argc, char ***globs, int *posn, int *count, int *r);
32
33 int
34 do_glob (const char *cmd, int argc, char *argv[])
35 {
36   /* For 'glob cmd foo /s* /usr/s*' this could be:
37    *
38    * (globs[0]) globs[1]  globs[1]  globs[2]
39    * (cmd)      foo       /sbin     /usr/sbin
40    *                      /srv      /usr/share
41    *                      /sys      /usr/src
42    *
43    * and then we call every combination (ie. 1x3x3) of
44    * argv[1-].
45    */
46   char **globs[argc];
47   int posn[argc];
48   int count[argc];
49   int i, r = 0;
50
51   if (argc < 1) {
52     fprintf (stderr, _("use 'glob command [args...]'\n"));
53     return -1;
54   }
55
56   /* This array will record the current execution position
57    * in the Cartesian product.
58    * NB. globs[0], posn[0], count[0] are ignored.
59    */
60   for (i = 1; i < argc; ++i)
61     posn[i] = 0;
62   for (i = 1; i < argc; ++i)
63     globs[i] = NULL;
64
65   for (i = 1; i < argc; ++i) {
66     char **pp;
67
68     /* Only if it begins with '/' can it possibly be a globbable path. */
69     if (argv[i][0] == '/') {
70       pp = guestfs_glob_expand (g, argv[i]);
71       if (pp == NULL) {         /* real error in glob_expand */
72         fprintf (stderr, _("glob: guestfs_glob_expand call failed: %s\n"),
73                  argv[i]);
74         goto error0;
75       }
76
77       /* If there were no matches, then we add a single element list
78        * containing just the original argv[i] string.
79        */
80       if (pp[0] == NULL) {
81         char **pp2;
82
83         pp2 = realloc (pp, sizeof (char *) * 2);
84         if (pp2 == NULL) {
85           perror ("realloc");
86           free (pp);
87           goto error0;
88         }
89         pp = pp2;
90
91         pp[0] = strdup (argv[i]);
92         if (pp[0] == NULL) {
93           perror ("strdup");
94           free (pp);
95           goto error0;
96         }
97         pp[1] = NULL;
98       }
99     }
100     /* Doesn't begin with '/' */
101     else {
102       pp = malloc (sizeof (char *) * 2);
103       if (pp == NULL) {
104         perror ("malloc");
105         goto error0;
106       }
107       pp[0] = strdup (argv[i]);
108       if (pp[0] == NULL) {
109         perror ("strdup");
110         free (pp);
111         goto error0;
112       }
113       pp[1] = NULL;
114     }
115
116     globs[i] = pp;
117     count[i] = count_strings (pp);
118   }
119
120   /* Issue the commands. */
121   glob_issue (argv[0], argc, globs, posn, count, &r);
122
123   /* Free resources. */
124  error0:
125   for (i = 1; i < argc; ++i)
126     if (globs[i])
127       free_strings (globs[i]);
128   return r;
129 }
130
131 static void
132 glob_issue (char *cmd, int argc,
133             char ***globs, int *posn, int *count,
134             int *r)
135 {
136   int i;
137   char *argv[argc+1];
138
139   argv[0] = cmd;
140   argv[argc] = NULL;
141
142  again:
143   printf ("%s", argv[0]);
144   for (i = 1; i < argc; ++i) {
145     argv[i] = globs[i][posn[i]];
146     printf (" %s", argv[i]);
147   }
148   printf ("\n");
149
150   if (issue_command (argv[0], &argv[1], NULL) == -1)
151     *r = -1;                    /* ... but don't exit */
152
153   for (i = argc-1; i >= 1; --i) {
154     posn[i]++;
155     if (posn[i] < count[i])
156       break;
157     posn[i] = 0;
158   }
159   if (i == 0)                   /* All done. */
160     return;
161
162   goto again;
163 }