ocaml: Error on compiler warnings.
[libguestfs.git] / fish / glob.c
1 /* guestfish - the filesystem interactive shell
2  * Copyright (C) 2009-2010 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, size_t argc, char ***globs, int *posn, int *count, int *r);
32
33 int
34 run_glob (const char *cmd, size_t 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   size_t i;
50   int r = 0;
51
52   if (argc < 1) {
53     fprintf (stderr, _("use 'glob command [args...]'\n"));
54     return -1;
55   }
56
57   /* This array will record the current execution position
58    * in the Cartesian product.
59    * NB. globs[0], posn[0], count[0] are ignored.
60    */
61   for (i = 1; i < argc; ++i)
62     posn[i] = 0;
63   for (i = 1; i < argc; ++i)
64     globs[i] = NULL;
65
66   for (i = 1; i < argc; ++i) {
67     char **pp;
68
69     /* Only if it begins with '/' can it possibly be a globbable path. */
70     if (argv[i][0] == '/') {
71       pp = guestfs_glob_expand (g, argv[i]);
72       if (pp == NULL) {         /* real error in glob_expand */
73         fprintf (stderr, _("glob: guestfs_glob_expand call failed: %s\n"),
74                  argv[i]);
75         goto error0;
76       }
77
78       /* If there were no matches, then we add a single element list
79        * containing just the original argv[i] string.
80        */
81       if (pp[0] == NULL) {
82         char **pp2;
83
84         pp2 = realloc (pp, sizeof (char *) * 2);
85         if (pp2 == NULL) {
86           perror ("realloc");
87           free (pp);
88           goto error0;
89         }
90         pp = pp2;
91
92         pp[0] = strdup (argv[i]);
93         if (pp[0] == NULL) {
94           perror ("strdup");
95           free (pp);
96           goto error0;
97         }
98         pp[1] = NULL;
99       }
100     }
101     /* Doesn't begin with '/' */
102     else {
103       pp = malloc (sizeof (char *) * 2);
104       if (pp == NULL) {
105         perror ("malloc");
106         goto error0;
107       }
108       pp[0] = strdup (argv[i]);
109       if (pp[0] == NULL) {
110         perror ("strdup");
111         free (pp);
112         goto error0;
113       }
114       pp[1] = NULL;
115     }
116
117     globs[i] = pp;
118     count[i] = count_strings (pp);
119   }
120
121   /* Issue the commands. */
122   glob_issue (argv[0], argc, globs, posn, count, &r);
123
124   /* Free resources. */
125  error0:
126   for (i = 1; i < argc; ++i)
127     if (globs[i])
128       free_strings (globs[i]);
129   return r;
130 }
131
132 static void
133 glob_issue (char *cmd, size_t argc,
134             char ***globs, int *posn, int *count,
135             int *r)
136 {
137   size_t i;
138   char *argv[argc+1];
139
140   argv[0] = cmd;
141   argv[argc] = NULL;
142
143  again:
144   for (i = 1; i < argc; ++i)
145     argv[i] = globs[i][posn[i]];
146
147   if (issue_command (argv[0], &argv[1], NULL) == -1)
148     *r = -1;                    /* ... but don't exit */
149
150   for (i = argc-1; i >= 1; --i) {
151     posn[i]++;
152     if (posn[i] < count[i])
153       break;
154     posn[i] = 0;
155   }
156   if (i == 0)                   /* All done. */
157     return;
158
159   goto again;
160 }