chiark / gitweb /
Initial revision
[ssr] / StraySrc / Utilities / c / submake
1 /*
2  * submake.c
3  *
4  * Handle recursive makes
5  *
6  * © 1998 Straylight/Edgeware
7  */
8
9 /*----- Licensing note ----------------------------------------------------*
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2, or (at your option)
14  * any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, write to the Free Software Foundation,
23  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  */
25
26 /*----- Header files ------------------------------------------------------*/
27
28 #include <stddef.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "swis.h"
34 #include "swiv.h"
35
36 #include "alloc.h"
37 #include "glob.h"
38
39 /*----- Main code ---------------------------------------------------------*/
40
41 typedef struct submk_ctx {
42   char **p;
43   size_t i;
44   size_t sz;
45 } submk_ctx;
46
47 static void submk_add(const char *p, void *ctx)
48 {
49   int len = _swi(OS_FSControl, _inr(0, 5) | _return(5), 37, p, 0, 0, 0, 0);
50   char *q = xmalloc(1 - len);
51   submk_ctx *sx = ctx;
52
53   _swi(OS_FSControl, _inr(0, 5), 37, p, q, 0, 0, 1 - len);
54   if (sx->i == sx->sz) {
55     sx->sz += 256;
56     sx->p = xrealloc(sx->p, sx->sz * sizeof(sx->p[0]));
57   }
58   sx->p[sx->i++] = q;
59 }
60
61 static char *nstrcpy(char *d, const char *s)
62 {
63   while ((*d++ = *s++) != 0)
64     ;
65   return (d - 1);
66 }
67
68 int main(int argc, char *argv[])
69 {
70   char prefix[1024];
71   int is_prefix = 1;
72   char *tail;
73   submk_ctx sx;
74
75   /* --- Remember the original DDEUtils prefix --- */
76
77   {
78     int len;
79     if (_swix(OS_ReadVarVal, _inr(0, 4) | _out(2),
80               "Prefix$Dir", prefix, sizeof(prefix), 0, 0,
81               &len))
82       is_prefix = 0;
83     else
84       prefix[len] = 0;
85   }
86
87   /* --- Initialise the context --- */
88
89   sx.sz = 256;
90   sx.p = xmalloc(sx.sz * sizeof(sx.p[0]));
91   sx.i = 0;
92
93   /* --- Mangle the command line arguments --- */
94
95   {
96     int i;
97
98     /* --- Expand wildcarded makefile names --- */
99
100     for (i = 1; i < argc && strcmp(argv[i], "--"); i++) {
101       if (glob(argv[i], submk_add, &sx) == 0)
102         submk_add(argv[i], &sx);
103     }
104
105     /* --- Take the rest of the arguments --- *
106      *
107      * This is one of those times that C shows its Unix roots most
108      * irritatingly.  I've got to grab all the carefully separated
109      * arguments, and stick them all back together.
110      */
111
112     if (i >= argc)
113       tail = "";
114     else {
115       size_t sz = 0;
116       int j;
117       char *p;
118
119       i++;
120       for (j = i; j < argc; j++)
121         sz += strlen(argv[j]) + 1;
122       tail = xmalloc(sz + 1);
123       p = tail;
124       *p++ = ' ';
125       p = nstrcpy(p, argv[i++]);
126       for (j = i; j < argc; j++) {
127         *p++ = ' ';
128         p = nstrcpy(p, argv[j]);
129       }
130       *p = 0;
131     }
132   }
133
134   /* --- Now start work on making things --- */
135
136   {
137     int i, e;
138     char cmdbuf[1024];
139
140     if (is_prefix)
141       _swi(XDDEUtils_Prefix, _in(0), 0);
142     for (i = 0; i < sx.i; i++) {
143       sprintf(cmdbuf, "amu -desktop -f %s%s", sx.p[i], tail);
144       printf("submake: Making%s with `%s'\n", tail, sx.p[i]);
145       if ((e = system(cmdbuf)) != 0) {
146         printf("submake: Failed%s with `%s' [%i]\n", tail, sx.p[i], e);
147         exit(1);
148       }
149       printf("submake: Successfully made%s with `%s'\n", tail, sx.p[i]);
150     }
151   }
152
153   /* --- Restore the original prefix --- */
154
155   if (is_prefix)
156     _swi(XDDEUtils_Prefix, _in(0), prefix);
157
158   /* --- That's it: return --- */
159
160   return (0);
161 }