-
Notifications
You must be signed in to change notification settings - Fork 0
/
shebang.c
198 lines (170 loc) · 4.98 KB
/
shebang.c
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
/* $Id: shebang.c,v 1.1.1.1 2002/01/29 23:26:56 diego Exp $ */
#include <config.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#define _GNU_SOURCE
#include <getopt.h>
#include "shebang.h"
static const int MAXARGS = 100;
static const int COUNT = 1;
static const int DONT_CLOSE_ON_EXEC = 0;
static char buf[MAXBUFSIZE] = { '\0' };
static char sb_cmd[MAXBUFSIZE] = { '\0' };
static const char* SHORT_OPTS = "k:u:V";
static struct option options[] = {
{"checkargs", required_argument, NULL, 'k'},
{"usage", required_argument, NULL, 'u'},
{"version", no_argument, NULL, 'V'},
{0, 0, 0, 0}
};
void usage(void) {
fprintf(stderr, "usage: %s { -V|--version | [-k|--checkargs N] "
"[-u|--usage STR] cmd [OPTIONS]... @! }\n", SHEBANGNAME);
fprintf(stderr, "*** IMPORTANT NOTICE: %s is not intended for "
"command line use ***\n", SHEBANGNAME);
exit(1);
}
void copyright(void) {
printf("%s Version %s\n", SHEBANGNAME, SB_VERSION);
printf("Copyright (C) 2001-2013 by Diego Torres Milano "
"<[email protected]>\n");
printf("All rights reserved. See COPYING for details.\n");
exit(0);
}
int main(int argc, char* argv[]) {
int i;
char* sb_usage = NULL;
int sb_checkargs = -1;
char* sb_script;
int sb_optind = 1;
char* sb_eval_name;
FILE* sb_eval;
FILE* sb_eval_pipe;
int sb_opt_argc = MAXARGS;
char* sb_opt_argv0;
char* sb_opt_argv[MAXARGS];
int sb_argc = MAXARGS;
char* sb_argv0;
char* sb_argv[MAXARGS];
const char* sb_input;
int fd;
char c;
int gnabehs = 0;
int gnabehs_pos_in_argv = -1;
#ifndef NDEBUG
printf("shebang:\n");
printf("arguments:\n");
for (i=0; i<argc; i++) {
printf("\t%d:%s\n", i, argv[i]);
}
#endif
if ( argc < 2 ) {
usage();
}
strncpy(buf, argv[0], sizeof(buf));
for (sb_argc=1; sb_argc < argc; sb_argc++) {
if (strstr(argv[sb_argc], GNABEHS) != NULL) {
gnabehs_pos_in_argv = sb_argc;
}
strcat(buf, WSTR);
strncat(buf, argv[sb_argc], sizeof(buf));
}
if ( str2argv(buf, &sb_opt_argc, sb_opt_argv, &sb_opt_argv0, &gnabehs) < 0 ) {
Fatal("str2argv");
}
/* this is used by getopt_long to stop processing options after first non-option found */
if (setenv("POSIXLY_CORRECT", "1", 1) == -1) {
Fatal("Cannot set environment");
}
while ( (c = getopt_long(sb_opt_argc, sb_opt_argv, SHORT_OPTS,
options, NULL)) != -1 ) {
#ifndef NDEBUG
printf("c='%c' optarg='%s'\n", c, optarg);
#endif
switch ( c ) {
case 'k':
/* checkargs */
sb_checkargs = strtol(optarg, (char **)NULL, 10);
fprintf(stderr, "%s: WARNING: Not implemented yet.\n", SHEBANGNAME);
break;
case 'u':
/* usage */
sb_usage = strdup(optarg);
fprintf(stderr, "%s: WARNING: Not implemented yet.\n", SHEBANGNAME);
break;
case 'V':
/* version */
copyright();
case ':':
case '?':
usage();
default:
Fatal("getopt (%c)", c);
}
}
if (!gnabehs || gnabehs_pos_in_argv == -1) {
Fatal("Couldn't find the end of shebang arguments delimited by '%s'", GNABEHS);
}
#ifndef NDEBUG
printf("optind=%d argc=%d sb_argc=%d sb_opt_argc=%d gnabehs_pos_in_argv=%d\n", optind, argc,
sb_argc, sb_opt_argc, gnabehs_pos_in_argv);
#endif
if (optind < sb_opt_argc) {
#ifndef NDEBUG
fprintf(stderr, "copying sb_opt_argv[%d] to sb_cmd\n", optind);
fprintf(stderr, " %s\n", sb_opt_argv[optind]);
#endif
strcpy(sb_cmd, sb_opt_argv[optind]);
for (i=optind+1; i < sb_opt_argc; i++) {
#ifndef NDEBUG
fprintf(stderr, "concatenating i=%d\n", i);
#endif
strncat(sb_cmd, WSTR, 1);
strncat(sb_cmd, sb_opt_argv[i], sizeof(buf));
}
if (argc > gnabehs_pos_in_argv+1) {
sb_input = strdup(argv[gnabehs_pos_in_argv+1]);
strncat(sb_cmd, WSTR, 1);
strncat(sb_cmd, sb_input, sizeof(buf));
}
else {
sb_input = "";
}
for (i=gnabehs_pos_in_argv+2; i < argc; i++) {
strncat(sb_cmd, WSTR, 1);
strncat(sb_cmd, argv[i], sizeof(buf));
}
#ifndef NDEBUG
fprintf(stderr, "We should execute the command in buf='%s'\n", sb_cmd);
fprintf(stderr, "with input from %s\n", sb_input);
#endif
}
else {
fprintf(stderr, "%s: ERROR: It seems that you forgot to specify the command to run\n", SHEBANGNAME);
usage();
}
if ( argc < 3 ) {
usage();
}
assert(argc >= 2);
sb_argc = sizeof(sb_argv)/sizeof(char*);
if (str2argv(sb_cmd, &sb_argc, sb_argv, &sb_argv0, NULL) < 0) {
Fatal("str2argv");
}
#ifndef NDEBUG
fprintf(stderr, "execvp '%s", sb_argv0);
for (i=0; i < sb_argc; i++) {
fprintf(stderr, " %s", sb_argv[i]);
}
fprintf(stderr, "'\n");
#endif
execvp(sb_argv0, sb_argv);
Fatal("exec");
return 0;
}