summaryrefslogtreecommitdiff
path: root/command_line/C++_Parser/Src/Library/ParserFace.c
blob: a005eceb73f9b765de7267b6db85bfd01c237e4d (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
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
#include "parser.h"

const char *failedCallback;
jmp_buf exit_plugin;
ParseOptsType parseopts;

static CWResult SetupParamBlock(CWPluginContext context) {
    CWResult result;
    int x;
    Handle h;
    PCmdLineEnvir cle;

    memset(&parseopts, 0, sizeof(parseopts));
    parseopts.context = context;

    result = CWSecretGetNamedPreferences(context, "CmdLine Environment", &h);
    if (result)
        return result;

    cle = **((PCmdLineEnvir **) h);
    parseopts.underIDE = cle.underIDE;
    parseopts.ioRows = cle.rows;
    parseopts.ioCols = cle.cols;

    result = CWParserGetCommandLine(context, &parseopts.args);
    if (result)
        return result;

    result = CWParserGetToolInfo(context, &parseopts.toolVersion);
    if (result)
        return result;

    result = CWParserGetTargetInfo(context, &parseopts.cpu, &parseopts.os);
    if (result)
        return result;

    result = CWParserGetPanels(context, &parseopts.numPanels, &parseopts.panelNames);
    if (result)
        return result;

    result = CWParserGetPlugins(context, &parseopts.numPlugins, &parseopts.plugins);
    if (result)
        return result;

    parseopts.passingArgs = 0;
    for (x = 0; x < parseopts.numPlugins; x++) {
        if (parseopts.plugins[x].storeCommandLine)
            parseopts.passingArgs = 1;
    }

    return 0;
}

static CWResult SetupOptions(CWPluginContext context) {
    if (!pTool) {
        return 2;
    } else {
        SetupParserToolOptions();
        return 0;
    }
}

static CWResult Parse(CWPluginContext context) {
    CWResult result;

    parseopts.success = 1;
    parseopts.currentSegment = 1;
    parseopts.currentOverlayGroup = 0;
    parseopts.currentOverlay = 0;

    Arg_InitToolArgs(&linkargs);
    Arg_InitToolArgs(&prelinkargs);
    Arg_InitToolArgs(&postlinkargs);

    if (pTool->PreParse)
        parseopts.success &= pTool->PreParse();

    Arg_Init(parseopts.args->argc, parseopts.args->argv);
    parseopts.noOptions = Arg_IsEmpty();
    parseopts.success &= Options_Parse(Options_GetOptions(), OFLAGS_1) && !parseopts.hadErrors;
    Arg_Reset();

    result = Parser_StorePanels(context);
    if (result)
        return result;

    if (pTool->MidParse && parseopts.success)
        parseopts.success &= pTool->MidParse();

    result = Parser_StorePanels(context);
    if (result)
        return result;

    if (parseopts.showHelp && parseopts.success)
        parseopts.success &= Options_DisplayHelp();

    Arg_Reset();
    if (parseopts.success)
        parseopts.success &= Options_Parse(Options_GetOptions(), 0) && !parseopts.hadErrors;

    if (pTool->PostParse && parseopts.success)
        parseopts.success &= pTool->PostParse();

    Arg_Terminate();

    return (parseopts.success && !parseopts.hadErrors) ? cwNoErr : cwErrRequestFailed;
}

Handle Parser_FindPrefPanel(const char *name) {
    int idx;
    Handle h;

    for (idx = 0; idx < pTool->numPrefDataPanels; idx++) {
        if (!ustrcmp(name, pTool->prefDataPanels[idx].name)) {
            h = NewHandle(pTool->prefDataPanels[idx].size);
            if (!h)
                return NULL;

            HLock(h);
            memcpy(*h, pTool->prefDataPanels[idx].ptr, pTool->prefDataPanels[idx].size);
            HUnlock(h);
            return h;
        }
    }

    return NULL;
}

CWResult Parser_StorePanels(CWPluginContext context) {
    int idx;
    CWResult result;
    const char *name;
    Handle h;

    for (idx = 0; idx < parseopts.numPanels; idx++) {
        name = parseopts.panelNames[idx];
        h = Parser_FindPrefPanel(name);
        if (h) {
            result = CWParserSetNamedPreferences(parseopts.context, name, h);
            if (result) {
                CLPReportError(CLPStr68, name);
                return result;
            }
        }
    }

    return cwNoErr;
}

static CWResult StoreResults(CWPluginContext context) {
    int idx; // r25
    CWResult result;
    CWCommandLineArgs args;
    UInt32 id; // r8
    UInt32 lg; // r10

    result = Parser_StorePanels(context);
    if (result)
        return result;

    for (idx = 0; idx < parseopts.numPlugins; idx++) {
        if (parseopts.plugins[idx].plugintype == CWDROPINLINKERTYPE && parseopts.plugins[idx].storeCommandLine) {
            if (parseopts.plugins[idx].dropinflags & isPreLinker) {
                Arg_ToolArgsForPlugin(&prelinkargs, &args);
            } else if (parseopts.plugins[idx].dropinflags & isPostLinker) {
                Arg_ToolArgsForPlugin(&postlinkargs, &args);
            } else {
                Arg_ToolArgsForPlugin(&linkargs, &args);
            }
            result = CWParserStoreCommandLineForPlugin(parseopts.context, idx, &args);
            if (result)
                return result;
        } else if (parseopts.plugins[idx].storeCommandLine) {
            id = parseopts.plugins[idx].plugintype;
            lg = parseopts.plugins[idx].language;
            fprintf(stderr,
                    "*** No support for %c%c%c%c/%c%c%c%c tool\n",
                    (id & 0xFF000000) >> 24,
                    (id & 0x00FF0000) >> 16,
                    (id & 0x0000FF00) >> 8,
                    (id & 0x000000FF),
                    (lg & 0xFF000000) >> 24,
                    (lg & 0x00FF0000) >> 16,
                    (lg & 0x0000FF00) >> 8,
                    (lg & 0x000000FF)
                    );
        }
    }

    return cwNoErr;
}

CWPLUGIN_ENTRY (CWParser_GetDropInFlags)(const DropInFlags **flags, SInt32 *flagsSize) {
    static const DropInFlags sFlags = {
            kCurrentDropInFlagsVersion,
            CWDROPINPARSERTYPE,
            7,
            0,
            'Seep',
            12
    };
    *flags = &sFlags;
    *flagsSize = sizeof(sFlags);
    return 0;
}

CWPLUGIN_ENTRY (CWParser_GetDropInName)(const char **dropinName) {
    static const char *sDropInName = "Command-Line Parser";
    *dropinName = sDropInName;
    return 0;
}

CWPLUGIN_ENTRY (CWParser_GetDisplayName)(const char **displayName) {
    static const char *sDisplayName = "Command-Line Parser";
    *displayName = sDisplayName;
    return 0;
}

CWPLUGIN_ENTRY (CWParser_GetPanelList)(const CWPanelList **panelList) {
    static CWPanelList sPanelList = {
            kCurrentCWFamilyListVersion,
            0,
            0
    };
    *panelList = &sPanelList;
    return 0;
}

CWPLUGIN_ENTRY (CWParser_GetTargetList)(const CWTargetList **targetList) {
    static CWDataType sCPU = '****';
    static CWDataType sOS = '****';
    static CWTargetList sTargetList = {
            kCurrentCWTargetListVersion,
            1,
            &sCPU,
            1,
            &sOS
    };
    *targetList = &sTargetList;
    return 0;
}

CWPLUGIN_ENTRY (CWParser_GetVersionInfo)(const VersionInfo **versioninfo) {
    static const VersionInfo vi = {
            1, 1, 0, 0
    };
    *versioninfo = &vi;
    return 0;
}

CWPLUGIN_ENTRY (Parser_SupportsPlugin)(const struct CLPluginInfo *pluginfo, CWDataType cpu, CWDataType os, Boolean *isSupported) {
    *isSupported = ParserToolMatchesPlugin(pluginfo->plugintype, pluginfo->language, cpu, os);
    return 0;
}

CWPLUGIN_ENTRY (Parser_SupportsPanels)(int numPanels, const char **panelNames, Boolean *isSupported) {
    *isSupported = ParserToolHandlesPanels(numPanels, panelNames);
    return 0;
}

CWPLUGIN_ENTRY (parser_main)(CWPluginContext context) {
    CWResult result;
    SInt32 request;

    CWGetPluginRequest(context, &request);
    result = setjmp(exit_plugin);
    if (result == 0) {
        switch (request) {
            case reqInitialize:
                result = 0;
                break;
            case 0:
                break;
            case 1:
                result = SetupParamBlock(context);
                if (!result)
                    result = Parser_StorePanels(context);
                break;
            case 2:
                result = SetupParamBlock(context);
                if (!result) {
                    result = SetupOptions(context);
                    if (!result) {
                        result = Parse(context);
                        if (!result)
                            result = StoreResults(context);
                    }
                }
                break;
        }
    } else {
        if (failedCallback && result != 1)
            fprintf(stderr, "Unexpected error in %s [%d]\n", failedCallback, result);
    }
    CWDonePluginRequest(context, result);

    return result;
}