1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - tools - defval
3   File:     defval.c
4 
5   Copyright 2005-2008 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:: 2008-05-23#$
14   $Rev: 6187 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 #include        "defval.h"
18 
19 extern const unsigned long SDK_DATE_OF_LATEST_FILE;
20 
21 static void usage(const char *argv0);
22 static BOOL defval(const char *file_in, const char *file_out);
23 static BOOL isComment = FALSE;
24 
25 /*---------------------------------------------------------------------------*
26   Name:         Main
27 
28   Description:  defval main function
29                 Processes options and calls the main processing function.
30 
31   Arguments:    argc   Argument count
32                 argv   Argument
33 
34   Returns:      0           Ended normally
35                 Non-0  Error
36  *---------------------------------------------------------------------------*/
main(int argc,char * argv[])37 int main(int argc, char *argv[])
38 {
39     int     n, fargc;
40     char  **fargv;
41     BOOL    result;
42 
43     //
44     // Option analysis
45     //
46     while ((n = getopt(argc, argv, "D:M:Cdh")) != -1)
47     {
48         switch (n)
49         {
50         case 'D':
51             (void)add_dvalue_by_equality(optarg);
52             break;
53 
54         case 'M':
55             if (!add_dvalue_from_file(optarg))
56             {
57                 fprintf(stderr, "Stop.\n");
58                 return 1;
59             }
60             break;
61 
62         case 'C':
63             isComment = TRUE;          // Enabled # comments at the beginning of lines
64             break;
65 
66         case 'd':
67             SetDebugMode(TRUE);
68             break;
69 
70         case 'h':
71         case '?':
72         case ':':
73         default:
74             usage(argv[0]);
75         }
76     }
77 
78     //
79     // Start processing
80     //
81     fargc = argc - optind;
82     fargv = argv + optind;
83 
84     switch (fargc)
85     {
86     case 0:
87         result = defval("-", "-");
88         break;
89 
90     case 1:
91         result = defval(fargv[0], "-");
92         break;
93 
94     case 2:
95     default:
96         result = defval(fargv[0], fargv[1]);
97         break;
98     }
99     return result ? 0 : 1;
100 }
101 
usage(const char * argv0)102 static void usage(const char *argv0)
103 {
104     char   *appname;
105 
106     UnpackFileName(argv0, NULL, &appname, NULL);        // Get command name
107 
108     fprintf(stderr,
109             "TWL-SDK Development Tool - %s - filter to support user defines\n"
110             "Build %lu\n\n"
111             "Usage: %s [-DNAME=VALUE...] [-MDEFINES_FILE] [-C] INFILE OUTFILE\n\n",
112             appname, SDK_DATE_OF_LATEST_FILE, appname);
113 
114     free(appname);
115     exit(2);
116 }
117 
118 /*---------------------------------------------------------------------------*
119   Name:         Main
120 
121   Description:  defval main function
122                 Processes options and calls the main processing function.
123 
124   Arguments:    file_in  Input file
125                 file_out Output file
126 
127   Returns:      TRUE        Ended successfully
128                 FALSE  Error
129  *---------------------------------------------------------------------------*/
defval(const char * file_in,const char * file_out)130 static BOOL defval(const char *file_in, const char *file_out)
131 {
132     BOOL    result;
133     FILE   *fp_in;
134     FILE   *fp_out;
135     char   *buffer;
136     int     buffer_size;
137 
138     DebugPrintf("file_in=[%s] file_out=[%s]\n", file_in, file_out);
139 
140     result = FALSE;
141     buffer = NULL;
142 
143     if (NULL == (fp_in = Fopen(file_in, "rb")) || NULL == (fp_out = Fopen(file_out, "wb")))
144     {
145         goto escape;
146     }
147 
148     while (NULL != Fgets(&buffer, &buffer_size, fp_in))
149     {
150         DebugPrintf("buffer=[%s] buffer_size=[%d]\n", buffer, buffer_size);
151 
152         if (isComment && buffer[0] == '#')      // comment processing
153         {
154             continue;
155         }
156 
157         if (!puts_modified_dvalue(fp_out, buffer))
158         {
159             fprintf(stderr, "Error: Cannot write %s.\n", file_out);
160             goto escape;
161         }
162     }
163     result = TRUE;
164 
165   escape:
166     Free((void **)&buffer);
167     Fclose(fp_in);
168     Fclose(fp_out);
169 
170     return result;
171 }
172