Subversion Repositories Kolibri OS

Rev

Rev 6536 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6536 Rev 6664
1
/*
1
/*
2
 * crt1.c
2
 * crt1.c
3
 * This file has no copyright assigned and is placed in the Public Domain.
3
 * This file has no copyright assigned and is placed in the Public Domain.
4
 * This file is a part of the mingw-runtime package.
4
 * This file is a part of the mingw-runtime package.
5
 * No warranty is given; refer to the file DISCLAIMER within the package.
5
 * No warranty is given; refer to the file DISCLAIMER within the package.
6
 *
6
 *
7
 * Source code for the startup proceedures used by all programs. This code
7
 * Source code for the startup proceedures used by all programs. This code
8
 * is compiled to make crt1.o, which should be located in the library path.
8
 * is compiled to make crt1.o, which should be located in the library path.
9
 *
9
 *
10
 */
10
 */
11
 
11
 
12
/* Hide the declaration of _fmode with dllimport attribute in stdlib.h to
12
/* Hide the declaration of _fmode with dllimport attribute in stdlib.h to
13
   avoid problems with older GCC. */
13
   avoid problems with older GCC. */
14
 
14
 
15
#include 
15
#include 
16
 
16
 
17
struct app_hdr
17
struct app_hdr
18
{
18
{
19
    char  banner[8];
19
    char  banner[8];
20
    int   version;
20
    int   version;
21
    int   start;
21
    int   start;
22
    int   iend;
22
    int   iend;
23
    int   memsize;
23
    int   memsize;
24
    int   stacktop;
24
    int   stacktop;
25
    char  *cmdline;
25
    char  *cmdline;
26
    char  *path;
26
    char  *path;
27
    int    __subsystem__;
27
    int    __subsystem__;
28
};
28
};
29
 
29
 
30
extern void init_reent();
30
extern void init_global_reent();
31
extern void init_stdio();
31
extern void init_stdio();
32
extern void __init_conio();
32
extern void __init_conio();
33
extern void __fini_conio();
33
extern void __fini_conio();
34
 
34
 
35
extern void tls_init(void);
35
extern void tls_init(void);
36
extern int main (int, char **, char **);
36
extern int main (int, char **, char **);
37
 
37
 
38
/* NOTE: The code for initializing the _argv, _argc, and environ variables
38
/* NOTE: The code for initializing the _argv, _argc, and environ variables
39
 *       has been moved to a separate .c file which is included in both
39
 *       has been moved to a separate .c file which is included in both
40
 *       crt1.c and dllcrt1.c. This means changes in the code don't have to
40
 *       crt1.c and dllcrt1.c. This means changes in the code don't have to
41
 *       be manually synchronized, but it does lead to this not-generally-
41
 *       be manually synchronized, but it does lead to this not-generally-
42
 *       a-good-idea use of include. */
42
 *       a-good-idea use of include. */
43
 
43
 
44
char* __appenv;
44
char* __appenv;
45
int   __appenv_size;
45
int   __appenv_size;
46
 
46
 
47
char * __libc_getenv(const char *name)
47
char * __libc_getenv(const char *name)
48
{
48
{
49
    return NULL;
49
    return NULL;
50
}
50
}
51
 
51
 
52
static int split_cmdline(char *cmdline, char **argv)
52
static int split_cmdline(char *cmdline, char **argv)
53
{
53
{
54
    enum quote_state
54
    enum quote_state
55
    {
55
    {
56
        QUOTE_NONE,         /* no " active in current parm       */
56
        QUOTE_NONE,         /* no " active in current parm       */
57
        QUOTE_DELIMITER,    /* " was first char and must be last */
57
        QUOTE_DELIMITER,    /* " was first char and must be last */
58
        QUOTE_STARTED       /* " was seen, look for a match      */
58
        QUOTE_STARTED       /* " was seen, look for a match      */
59
    };
59
    };
60
 
60
 
61
    enum quote_state state;
61
    enum quote_state state;
62
    unsigned int argc;
62
    unsigned int argc;
63
    char *p = cmdline;
63
    char *p = cmdline;
64
    char *new_arg, *start;
64
    char *new_arg, *start;
65
 
65
 
66
    argc = 0;
66
    argc = 0;
67
 
67
 
68
    for(;;)
68
    for(;;)
69
    {
69
    {
70
        /* skip over spaces and tabs */
70
        /* skip over spaces and tabs */
71
        if ( *p )
71
        if ( *p )
72
        {
72
        {
73
            while (*p == ' ' || *p == '\t')
73
            while (*p == ' ' || *p == '\t')
74
                ++p;
74
                ++p;
75
        }
75
        }
76
 
76
 
77
        if (*p == '\0')
77
        if (*p == '\0')
78
            break;
78
            break;
79
 
79
 
80
        state = QUOTE_NONE;
80
        state = QUOTE_NONE;
81
        if( *p == '\"' )
81
        if( *p == '\"' )
82
        {
82
        {
83
            p++;
83
            p++;
84
            state = QUOTE_DELIMITER;
84
            state = QUOTE_DELIMITER;
85
        }
85
        }
86
        new_arg = start = p;
86
        new_arg = start = p;
87
        for (;;)
87
        for (;;)
88
        {
88
        {
89
            if( *p == '\"' )
89
            if( *p == '\"' )
90
            {
90
            {
91
                p++;
91
                p++;
92
                if( state == QUOTE_NONE )
92
                if( state == QUOTE_NONE )
93
                {
93
                {
94
                    state = QUOTE_STARTED;
94
                    state = QUOTE_STARTED;
95
                }
95
                }
96
                else
96
                else
97
                {
97
                {
98
                    state = QUOTE_NONE;
98
                    state = QUOTE_NONE;
99
                }
99
                }
100
                continue;
100
                continue;
101
            }
101
            }
102
 
102
 
103
            if( *p == ' ' || *p == '\t' )
103
            if( *p == ' ' || *p == '\t' )
104
            {
104
            {
105
                if( state == QUOTE_NONE )
105
                if( state == QUOTE_NONE )
106
                {
106
                {
107
                    break;
107
                    break;
108
                }
108
                }
109
            }
109
            }
110
 
110
 
111
            if( *p == '\0' )
111
            if( *p == '\0' )
112
                break;
112
                break;
113
 
113
 
114
            if( *p == '\\' )
114
            if( *p == '\\' )
115
            {
115
            {
116
                if( p[1] == '\"' )
116
                if( p[1] == '\"' )
117
                {
117
                {
118
                    ++p;
118
                    ++p;
119
                    if( p[-2] == '\\' )
119
                    if( p[-2] == '\\' )
120
                    {
120
                    {
121
                        continue;
121
                        continue;
122
                    }
122
                    }
123
                }
123
                }
124
            }
124
            }
125
            if( argv )
125
            if( argv )
126
            {
126
            {
127
                *(new_arg++) = *p;
127
                *(new_arg++) = *p;
128
            }
128
            }
129
            ++p;
129
            ++p;
130
        };
130
        };
131
 
131
 
132
        if( argv )
132
        if( argv )
133
        {
133
        {
134
            argv[ argc ] = start;
134
            argv[ argc ] = start;
135
            ++argc;
135
            ++argc;
136
 
136
 
137
            /*
137
            /*
138
              The *new = '\0' is req'd in case there was a \" to "
138
              The *new = '\0' is req'd in case there was a \" to "
139
              translation. It must be after the *p check against
139
              translation. It must be after the *p check against
140
              '\0' because new and p could point to the same char
140
              '\0' because new and p could point to the same char
141
              in which case the scan would be terminated too soon.
141
              in which case the scan would be terminated too soon.
142
            */
142
            */
143
 
143
 
144
            if( *p == '\0' )
144
            if( *p == '\0' )
145
            {
145
            {
146
                *new_arg = '\0';
146
                *new_arg = '\0';
147
                break;
147
                break;
148
            }
148
            }
149
            *new_arg = '\0';
149
            *new_arg = '\0';
150
            ++p;
150
            ++p;
151
        }
151
        }
152
        else
152
        else
153
        {
153
        {
154
            ++argc;
154
            ++argc;
155
            if( *p == '\0' )
155
            if( *p == '\0' )
156
            {
156
            {
157
                break;
157
                break;
158
            }
158
            }
159
            ++p;
159
            ++p;
160
        }
160
        }
161
    }
161
    }
162
 
162
 
163
    return argc;
163
    return argc;
164
};
164
};
165
 
165
 
166
void  __attribute__((noreturn))
166
void  __attribute__((noreturn))
167
__libc_init (void)
167
__libc_init (void)
168
{
168
{
169
    struct   app_hdr *header = NULL;
169
    struct   app_hdr *header = NULL;
170
    int retval = 0;
170
    int retval = 0;
171
 
171
 
172
    char **argv;
172
    char **argv;
173
    int    argc;
173
    int    argc;
174
 
174
 
175
    tls_init();
175
    tls_init();
176
    init_reent();
176
    init_global_reent();
177
    init_stdio();
177
    init_stdio();
178
 
178
 
179
    if(header->__subsystem__ == 3)
179
    if(header->__subsystem__ == 3)
180
        __init_conio();
180
        __init_conio();
181
 
181
 
182
    if( header->cmdline[0] != 0)
182
    if( header->cmdline[0] != 0)
183
    {
183
    {
184
        argc = split_cmdline(header->cmdline, NULL) + 1;
184
        argc = split_cmdline(header->cmdline, NULL) + 1;
185
        argv = alloca((argc+1)*sizeof(char*));
185
        argv = alloca((argc+1)*sizeof(char*));
186
        argv[0] = header->path;
186
        argv[0] = header->path;
187
 
187
 
188
        split_cmdline(header->cmdline, argv + 1);
188
        split_cmdline(header->cmdline, argv + 1);
189
    }
189
    }
190
    else
190
    else
191
    {
191
    {
192
        argc = 1;
192
        argc = 1;
193
        argv = alloca((argc+1)*sizeof(char*));
193
        argv = alloca((argc+1)*sizeof(char*));
194
        argv[0] = header->path;
194
        argv[0] = header->path;
195
    }
195
    }
196
    argv[argc] = NULL;
196
    argv[argc] = NULL;
197
 
197
 
198
    retval = main(argc, argv, NULL);
198
    retval = main(argc, argv, NULL);
199
done:
199
done:
200
    if(header->__subsystem__ == 3)
200
    if(header->__subsystem__ == 3)
201
        __fini_conio();
201
        __fini_conio();
202
 
202
 
203
    exit (retval);
203
    exit (retval);
204
}
204
}