1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - - makelst
3   File:     searcharg.c
4 
5   Copyright 2006-2009 Nintendo.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2009-06-04#$
14   $Rev: 10698 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <getopt.h>
21 
22 #include "searcharg.h"
23 
24 
25 extern const unsigned long SDK_DATE_OF_LATEST_FILE;
26 
27 int    modc;
28 char** modv;
29 char** dll_argv;
30 char** static_argv;
31 char** header_argv;
32 int    dll_index    = 0;
33 int    static_index = 0;
34 int    header_index = 0;
35 char*  output_fname = NULL;
36 
37 
38 
SA_Usage(void)39 void SA_Usage( void)
40 {
41     fprintf( stderr, "Development Tool - makelst - Make \"C\" source file\n");
42     fprintf( stderr, "Build %lu\n\n", SDK_DATE_OF_LATEST_FILE);
43     fprintf( stderr, "Usage: makelst [-o output-file] [--static static-files ...] [--dll dll-files ...] [--header header-files ...]\n\n");
44     exit( 1);
45 }
46 
47 
48 /* Convert -dll A B C ...  to -dll A -dll B -dll C ...*/
SA_modify(int argc,char * argv[])49 void SA_modify( int argc, char* argv[])
50 {
51     int i=0;
52     int j=0;
53     char* opt_name;
54     modv = malloc( 4 * (argc * 3));
55 
56     for( ; i<argc; ) {
57         if( (strcmp( argv[i], "--dll") == 0) ||
58             (strcmp( argv[i], "--static") == 0) ||
59             (strcmp( argv[i], "--header") == 0)) { //Insert option
60 
61             opt_name = argv[i++];
62             if( argc <= i) { break;}
63             while( strncmp( argv[i], "-", 1) != 0) {
64                 modv[j++] = opt_name;
65                 modv[j++] = argv[i++];
66                 if( argc <= i) { break;}
67             }
68         }else{ //Ordinary support
69             modv[j++] = argv[i++];
70         }
71     }
72     modc = j;
73 //    printf( "modc = %d\n", modc);
74 }
75 
76 
77 /*Parse the argument*/
SA_searchopt(int argc,char * argv[])78 void SA_searchopt( int argc, char* argv[])
79 {
80     int n;
81     struct option optionInfo[] = {
82         { "static" , required_argument, NULL, 's'},
83         { "dll"    , required_argument, NULL, 'l'},
84         { "header" , required_argument, NULL, 'H'},
85         { NULL, 0, NULL, 0}
86     };
87 
88     if( argc <= 1) {
89         SA_Usage();
90     }
91 
92     /*Convert argument*/
93     SA_modify( argc, argv);
94 
95     dll_argv    = malloc( 4 * argc);
96     static_argv = malloc( 4 * argc);
97     header_argv = malloc( 4 * argc);
98 
99     while( (n = getopt_long( modc, modv, "do:h", &optionInfo[0], NULL))
100            != -1)
101     {
102         switch( n) {
103         case 'd':
104 //          dbg_print_flag = 1;
105           break;
106         case 'o':
107           if( output_fname != NULL) {
108               fprintf( stderr, "ERROR! redefined output filename.\n");
109               SA_Usage();
110           }
111           output_fname = optarg;
112           break;
113         case 's': // "--static"
114           static_argv[static_index++] = optarg;
115           break;
116         case 'l': // "--dll"
117           dll_argv[dll_index++] = optarg;
118           break;
119         case 'H': // "--header"
120           header_argv[header_index++] = optarg;
121           break;
122         case 'h':
123           SA_Usage();
124           break;
125         default: // '?'
126           SA_Usage();
127           break;
128         }
129     }
130 
131     /* When the static file is not specified */
132     if( static_index == 0) {
133         fprintf( stderr, "ERROR! no input static file(s).\n");
134         SA_Usage();
135     }
136     /* When the DLL is not specified */
137     if( dll_index == 0) {
138         fprintf( stderr, "ERROR! no input dll file(s).\n");
139         SA_Usage();
140     }
141     /* When there are elements that are not options */
142     if( optind != modc) {
143         SA_Usage();
144     }
145 }
146 
147