aboutsummaryrefslogtreecommitdiff
path: root/pd/src/s_entry.c
blob: e81e45c6dae54d806a6553859f7827c48ab9a6bd (plain)
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
/* In MSW, this is all there is to pd; the rest sits in a "pdlib" dll so
that externs can link back to functions defined in pd. */

#include <stdio.h>

int sys_main(int argc, char **argv);

#ifdef MSW
#include <windows.h>
#include <stdio.h>
#include <malloc.h>

#define MAXARGS 1024
#define MAXARGLEN 1024

/* jsarlo { */
int tokenizeCommandLineString(char *clString, char **tokens)
{
    int i, charCount = 0;
    int tokCount= 0;
    int quoteOpen = 0;

    for (i = 0; i < (int)strlen(clString); i++)
    {
        if (clString[i] == '"')
        {
            quoteOpen = !quoteOpen;
        }
        else if (clString[i] == ' ' && !quoteOpen)
        {
            tokens[tokCount][charCount] = 0;
            tokCount++;
            charCount = 0;
        }
        else
        {
            tokens[tokCount][charCount] = clString[i];
            charCount++;
        }
    }
    tokens[tokCount][charCount] = 0;
    tokCount++;
    return tokCount;
}

int WINAPI WinMain(HINSTANCE hInstance, 
                               HINSTANCE hPrevInstance,
                               LPSTR lpCmdLine,
                               int nCmdShow)
{
    int i, argc;
    char *argv[MAXARGS];

     __try
    {
        for (i = 0; i < MAXARGS; i++)
        {
            argv[i] = (char *)malloc(MAXARGLEN * sizeof(char));
        }
        GetModuleFileName(NULL, argv[0], MAXARGLEN);
        argc = tokenizeCommandLineString(lpCmdLine, argv + 1) + 1;
        sys_main(argc, argv);
        for (i = 0; i < MAXARGS; i++)
        {
            free(argv[i]);
        }
    }
    __finally
    {
        printf("caught an exception; stopping\n");
    }
}

/* } jsarlo */

#else /* not MSW */
int main(int argc, char **argv)
{
    return (sys_main(argc, argv));
}
#endif