1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
|
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
#include <stdio.h>
#include <process.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
typedef char bool;
#define false 0
#define true (!false)
bool silent = false;
bool shared = false;
bool export_all = false;
enum mode_t { mCompile, mLink, mInstall };
enum output_type_t { otGeneral, otObject, otProgram, otStaticLibrary, otDynamicLibrary };
#ifdef __EMX__
# define SHELL_CMD "sh"
# define CC "gcc"
# define GEN_EXPORTS "emxexp"
# define DEF2IMPLIB_CMD "emximp"
# define SHARE_SW "-Zdll -Zmtd"
# define USE_OMF true
# define TRUNCATE_DLL_NAME
# define DYNAMIC_LIB_EXT "dll"
# define EXE_EXT ".exe"
# if USE_OMF
/* OMF is the native format under OS/2 */
# define STATIC_LIB_EXT "lib"
# define OBJECT_EXT "obj"
# define LIBRARIAN "emxomfar"
# else
/* but the alternative, a.out, can fork() which is sometimes necessary */
# define STATIC_LIB_EXT "a"
# define OBJECT_EXT "o"
# define LIBRARIAN "ar"
# endif
#endif
typedef struct {
char *arglist[1024];
int num_args;
enum mode_t mode;
enum output_type_t output_type;
char *output_name;
char *stub_name;
char *tmp_dirs[1024];
int num_tmp_dirs;
char *obj_files[1024];
int num_obj_files;
} cmd_data_t;
void parse_args(int argc, char *argv[], cmd_data_t *cmd_data);
bool parse_long_opt(char *arg, cmd_data_t *cmd_data);
bool parse_short_opt(char *arg, cmd_data_t *cmd_data);
bool parse_input_file_name(char *arg, cmd_data_t *cmd_data);
bool parse_output_file_name(char *arg, cmd_data_t *cmd_data);
void post_parse_fixup(cmd_data_t *cmd_data);
bool explode_static_lib(char *lib, cmd_data_t *cmd_data);
int execute_command(cmd_data_t *cmd_data);
char *shell_esc(const char *str);
void cleanup_tmp_dirs(cmd_data_t *cmd_data);
void generate_def_file(cmd_data_t *cmd_data);
char *nameof(char *fullpath);
char *truncate_dll_name(char *path);
int main(int argc, char *argv[])
{
int rc;
cmd_data_t cmd_data;
memset(&cmd_data, 0, sizeof(cmd_data));
cmd_data.mode = mCompile;
cmd_data.output_type = otGeneral;
parse_args(argc, argv, &cmd_data);
rc = execute_command(&cmd_data);
if (rc == 0 && cmd_data.stub_name) {
fopen(cmd_data.stub_name, "w");
}
cleanup_tmp_dirs(&cmd_data);
return rc;
}
void parse_args(int argc, char *argv[], cmd_data_t *cmd_data)
{
int a;
char *arg;
bool argused;
for (a=1; a < argc; a++) {
arg = argv[a];
argused = false;
if (arg[0] == '-') {
if (arg[1] == '-') {
argused = parse_long_opt(arg + 2, cmd_data);
} else if (arg[1] == 'o' && a+1 < argc) {
cmd_data->arglist[cmd_data->num_args++] = arg;
arg = argv[++a];
argused = parse_output_file_name(arg, cmd_data);
} else {
argused = parse_short_opt(arg + 1, cmd_data);
}
} else {
argused = parse_input_file_name(arg, cmd_data);
}
if (!argused) {
cmd_data->arglist[cmd_data->num_args++] = arg;
}
}
post_parse_fixup(cmd_data);
}
bool parse_long_opt(char *arg, cmd_data_t *cmd_data)
{
char *equal_pos = strchr(arg, '=');
char var[50];
char value[500];
if (equal_pos) {
strncpy(var, arg, equal_pos - arg);
var[equal_pos - arg] = 0;
strcpy(value, equal_pos + 1);
} else {
strcpy(var, arg);
}
if (strcmp(var, "silent") == 0) {
silent = true;
} else if (strcmp(var, "mode") == 0) {
if (strcmp(value, "compile") == 0) {
cmd_data->mode = mCompile;
cmd_data->output_type = otObject;
}
if (strcmp(value, "link") == 0) {
cmd_data->mode = mLink;
}
if (strcmp(value, "install") == 0) {
cmd_data->mode = mInstall;
}
} else if (strcmp(var, "shared") == 0) {
shared = true;
} else if (strcmp(var, "export-all") == 0) {
export_all = true;
} else {
return false;
}
return true;
}
bool parse_short_opt(char *arg, cmd_data_t *cmd_data)
{
if (strcmp(arg, "export-dynamic") == 0) {
return true;
}
if (strcmp(arg, "module") == 0) {
return true;
}
if (strcmp(arg, "Zexe") == 0) {
return true;
}
if (strcmp(arg, "avoid-version") == 0) {
return true;
}
return false;
}
bool parse_input_file_name(char *arg, cmd_data_t *cmd_data)
{
char *ext = strrchr(arg, '.');
char *name = strrchr(arg, '/');
int pathlen;
char *newarg;
if (!ext) {
return false;
}
ext++;
if (name == NULL) {
name = strrchr(arg, '\\');
if (name == NULL) {
name = arg;
} else {
name++;
}
} else {
name++;
}
pathlen = name - arg;
if (strcmp(ext, "lo") == 0) {
newarg = (char *)malloc(strlen(arg) + 10);
strcpy(newarg, arg);
strcpy(newarg + (ext - arg), OBJECT_EXT);
cmd_data->arglist[cmd_data->num_args++] = newarg;
cmd_data->obj_files[cmd_data->num_obj_files++] = newarg;
return true;
}
if (strcmp(ext, "la") == 0) {
newarg = (char *)malloc(strlen(arg) + 10);
strcpy(newarg, arg);
newarg[pathlen] = 0;
strcat(newarg, ".libs/");
if (strncmp(name, "lib", 3) == 0) {
name += 3;
}
strcat(newarg, name);
ext = strrchr(newarg, '.') + 1;
if (shared && cmd_data->mode == mInstall) {
strcpy(ext, DYNAMIC_LIB_EXT);
newarg = truncate_dll_name(newarg);
} else {
strcpy(ext, STATIC_LIB_EXT);
}
cmd_data->arglist[cmd_data->num_args++] = newarg;
return true;
}
if (strcmp(ext, "c") == 0) {
if (cmd_data->stub_name == NULL) {
cmd_data->stub_name = (char *)malloc(strlen(arg) + 4);
strcpy(cmd_data->stub_name, arg);
strcpy(strrchr(cmd_data->stub_name, '.') + 1, "lo");
}
}
if (strcmp(name, CC) == 0 || strcmp(name, CC EXE_EXT) == 0) {
if (cmd_data->output_type == otGeneral) {
cmd_data->output_type = otObject;
}
}
return false;
}
bool parse_output_file_name(char *arg, cmd_data_t *cmd_data)
{
char *name = strrchr(arg, '/');
char *ext = strrchr(arg, '.');
char *newarg = NULL, *newext;
int pathlen;
if (name == NULL) {
name = strrchr(arg, '\\');
if (name == NULL) {
name = arg;
} else {
name++;
}
} else {
name++;
}
if (!ext) {
cmd_data->stub_name = arg;
cmd_data->output_type = otProgram;
newarg = (char *)malloc(strlen(arg) + 5);
strcpy(newarg, arg);
strcat(newarg, EXE_EXT);
cmd_data->arglist[cmd_data->num_args++] = newarg;
cmd_data->output_name = newarg;
return true;
}
ext++;
pathlen = name - arg;
if (strcmp(ext, "la") == 0) {
cmd_data->stub_name = arg;
cmd_data->output_type = shared ? otDynamicLibrary : otStaticLibrary;
newarg = (char *)malloc(strlen(arg) + 10);
mkdir(".libs", 0);
strcpy(newarg, ".libs/");
if (strncmp(arg, "lib", 3) == 0) {
arg += 3;
}
strcat(newarg, arg);
newext = strrchr(newarg, '.') + 1;
strcpy(newext, shared ? DYNAMIC_LIB_EXT : STATIC_LIB_EXT);
#ifdef TRUNCATE_DLL_NAME
if (shared) {
newarg = truncate_dll_name(newarg);
}
#endif
cmd_data->arglist[cmd_data->num_args++] = newarg;
cmd_data->output_name = newarg;
return true;
}
if (strcmp(ext, "lo") == 0) {
cmd_data->stub_name = arg;
cmd_data->output_type = otObject;
newarg = (char *)malloc(strlen(newarg) + 2);
strcpy(newarg, arg);
ext = strrchr(newarg, '.') + 1;
strcpy(ext, OBJECT_EXT);
cmd_data->arglist[cmd_data->num_args++] = newarg;
cmd_data->output_name = newarg;
return true;
}
return false;
}
void post_parse_fixup(cmd_data_t *cmd_data)
{
int a;
char *arg;
char *ext;
if (cmd_data->output_type == otStaticLibrary && cmd_data->mode == mLink) {
/* We do a real hatchet job on the args when making a static library
* removing all compiler switches & any other cruft that ar won't like
* We also need to explode any libraries listed
*/
for (a=0; a < cmd_data->num_args; a++) {
arg = cmd_data->arglist[a];
if (arg) {
ext = strrchr(arg, '.');
if (ext) {
ext++;
}
if (arg[0] == '-') {
cmd_data->arglist[a] = NULL;
if (strcmp(arg, "-rpath") == 0 && a+1 < cmd_data->num_args) {
cmd_data->arglist[a+1] = NULL;
}
if (strcmp(arg, "-R") == 0 && a+1 < cmd_data->num_args) {
cmd_data->arglist[a+1] = NULL;
}
if (strcmp(arg, "-version-info") == 0 && a+1 < cmd_data->num_args) {
cmd_data->arglist[a+1] = NULL;
}
if (strcmp(arg, "-o") == 0) {
a++;
}
}
if (strcmp(arg, CC) == 0 || strcmp(arg, CC EXE_EXT) == 0) {
cmd_data->arglist[a] = LIBRARIAN " cr";
}
if (ext) {
if (strcmp(ext, "h") == 0 || strcmp(ext, "c") == 0) {
/* ignore source files, they don't belong in a library */
cmd_data->arglist[a] = NULL;
}
if (strcmp(ext, STATIC_LIB_EXT) == 0) {
cmd_data->arglist[a] = NULL;
explode_static_lib(arg, cmd_data);
}
}
}
}
}
if (cmd_data->output_type == otDynamicLibrary) {
for (a=0; a < cmd_data->num_args; a++) {
arg = cmd_data->arglist[a];
if (arg) {
if (strcmp(arg, "-rpath") == 0 && a+1 < cmd_data->num_args) {
cmd_data->arglist[a] = NULL;
cmd_data->arglist[a+1] = NULL;
}
}
}
if (export_all) {
generate_def_file(cmd_data);
}
}
#if USE_OMF
if (cmd_data->output_type == otObject ||
cmd_data->output_type == otProgram ||
cmd_data->output_type == otDynamicLibrary) {
cmd_data->arglist[cmd_data->num_args++] = "-Zomf";
}
#endif
if (shared && (cmd_data->output_type == otObject || cmd_data->output_type == otDynamicLibrary)) {
cmd_data->arglist[cmd_data->num_args++] = SHARE_SW;
}
}
int execute_command(cmd_data_t *cmd_data)
{
int target = 0;
char *command;
int a, total_len = 0;
char *args[4];
for (a=0; a < cmd_data->num_args; a++) {
if (cmd_data->arglist[a]) {
total_len += strlen(cmd_data->arglist[a]) + 1;
}
}
command = (char *)malloc( total_len );
command[0] = 0;
for (a=0; a < cmd_data->num_args; a++) {
if (cmd_data->arglist[a]) {
strcat(command, cmd_data->arglist[a]);
strcat(command, " ");
}
}
command[strlen(command)-1] = 0;
if (!silent) {
puts(command);
}
cmd_data->num_args = target;
cmd_data->arglist[cmd_data->num_args] = NULL;
command = shell_esc(command);
args[0] = SHELL_CMD;
args[1] = "-c";
args[2] = command;
args[3] = NULL;
return spawnvp(P_WAIT, args[0], args);
}
char *shell_esc(const char *str)
{
char *cmd;
unsigned char *d;
const unsigned char *s;
cmd = (char *)malloc(2 * strlen(str) + 1);
d = (unsigned char *)cmd;
s = (const unsigned char *)str;
for (; *s; ++s) {
if (*s == '"' || *s == '\\') {
*d++ = '\\';
}
*d++ = *s;
}
*d = '\0';
return cmd;
}
bool explode_static_lib(char *lib, cmd_data_t *cmd_data)
{
char tmpdir[1024];
char savewd[1024];
char cmd[1024];
char *name;
DIR *dir;
struct dirent *entry;
strcpy(tmpdir, lib);
strcat(tmpdir, ".exploded");
mkdir(tmpdir, 0);
cmd_data->tmp_dirs[cmd_data->num_tmp_dirs++] = strdup(tmpdir);
getcwd(savewd, sizeof(savewd));
if (chdir(tmpdir) != 0)
return false;
strcpy(cmd, LIBRARIAN " x ");
name = strrchr(lib, '/');
if (name) {
name++;
} else {
name = lib;
}
strcat(cmd, "../");
strcat(cmd, name);
system(cmd);
chdir(savewd);
dir = opendir(tmpdir);
while ((entry = readdir(dir)) != NULL) {
if (entry->d_name[0] != '.') {
strcpy(cmd, tmpdir);
strcat(cmd, "/");
strcat(cmd, entry->d_name);
cmd_data->arglist[cmd_data->num_args++] = strdup(cmd);
}
}
closedir(dir);
return true;
}
void cleanup_tmp_dir(char *dirname)
{
DIR *dir;
struct dirent *entry;
char fullname[1024];
dir = opendir(dirname);
if (dir == NULL)
return;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_name[0] != '.') {
strcpy(fullname, dirname);
strcat(fullname, "/");
strcat(fullname, entry->d_name);
remove(fullname);
}
}
rmdir(dirname);
}
void cleanup_tmp_dirs(cmd_data_t *cmd_data)
{
int d;
for (d=0; d < cmd_data->num_tmp_dirs; d++) {
cleanup_tmp_dir(cmd_data->tmp_dirs[d]);
}
}
void generate_def_file(cmd_data_t *cmd_data)
{
char def_file[1024];
char implib_file[1024];
char *ext;
FILE *hDef;
char *export_args[1024];
int num_export_args = 0;
char *cmd;
int cmd_size = 0;
int a;
if (cmd_data->output_name) {
strcpy(def_file, cmd_data->output_name);
strcat(def_file, ".def");
hDef = fopen(def_file, "w");
if (hDef != NULL) {
fprintf(hDef, "LIBRARY %s INITINSTANCE\n", nameof(cmd_data->output_name));
fprintf(hDef, "EXPORTS\n");
fclose(hDef);
for (a=0; a < cmd_data->num_obj_files; a++) {
cmd_size += strlen(cmd_data->obj_files[a]) + 1;
}
cmd_size += strlen(GEN_EXPORTS) + strlen(def_file) + 3;
cmd = (char *)malloc(cmd_size);
strcpy(cmd, GEN_EXPORTS);
for (a=0; a < cmd_data->num_obj_files; a++) {
strcat(cmd, " ");
strcat(cmd, cmd_data->obj_files[a] );
}
strcat(cmd, ">>");
strcat(cmd, def_file);
puts(cmd);
export_args[num_export_args++] = SHELL_CMD;
export_args[num_export_args++] = "-c";
export_args[num_export_args++] = cmd;
export_args[num_export_args++] = NULL;
spawnvp(P_WAIT, export_args[0], export_args);
cmd_data->arglist[cmd_data->num_args++] = strdup(def_file);
/* Now make an import library for the dll */
num_export_args = 0;
export_args[num_export_args++] = DEF2IMPLIB_CMD;
export_args[num_export_args++] = "-o";
strcpy(implib_file, ".libs/");
strcat(implib_file, cmd_data->stub_name);
ext = strrchr(implib_file, '.');
if (ext)
*ext = 0;
strcat(implib_file, ".");
strcat(implib_file, STATIC_LIB_EXT);
export_args[num_export_args++] = implib_file;
export_args[num_export_args++] = def_file;
spawnvp(P_WAIT, export_args[0], export_args);
}
}
}
/* returns just a file's name without path or extension */
char *nameof(char *fullpath)
{
char buffer[1024];
char *ext;
char *name = strrchr(fullpath, '/');
if (name == NULL) {
name = strrchr(fullpath, '\\');
}
if (name == NULL) {
name = fullpath;
} else {
name++;
}
strcpy(buffer, name);
ext = strrchr(buffer, '.');
if (ext) {
*ext = 0;
return strdup(buffer);
}
return name;
}
char *truncate_dll_name(char *path)
{
/* Cut DLL name down to 8 characters after removing any mod_ prefix */
char *tmppath = strdup(path);
char *newname = strrchr(tmppath, '/') + 1;
char *ext = strrchr(tmppath, '.');
int len;
if (ext == NULL)
return tmppath;
len = ext - newname;
if (strncmp(newname, "mod_", 4) == 0) {
strcpy(newname, newname + 4);
len -= 4;
}
if (len > 8) {
strcpy(newname + 8, strchr(newname, '.'));
}
return tmppath;
}
|