root/lang/objective-cplusplus/i3/trunk/src/Common.cc @ 37492

Revision 37492, 11.1 kB (checked in by saturday06, 3 years ago)

... surpress . . . .warning . ..

  • Property svn:executable set to *
Line 
1#include <mil/PrecompiledHeaders.h>
2#include "Common.h"
3
4#include <mil/StaticData.cc>
5
6#ifdef HAVE_LIBPOPT
7#include <popt.h>
8namespace i3 {
9
10int usage(poptContext optCon, int exitcode, const char *error, const char *addl) {
11    poptPrintUsage(optCon, stdout, 0);
12    if (error) {
13        fprintf(stdout, "%s: %s0", error, addl);
14    }
15    return exitcode;
16}
17
18int execute_popt(int argc, char** argv) {
19    int c;            /* used for argument parsing */
20    int i = 0;        /* used for tracking options */
21    int speed = 0;    /* used in argument parsing to set speed */
22    int raw = 0;      /* raw mode? */
23    int version = 0;
24    int j;
25    char buf[BUFSIZ + 1];
26    poptContext optCon;   /* context for parsing command-line options */
27
28    struct poptOption optionsTable[] = {
29        /*{ "longname", "shortname", argInfo,      *arg,   int val, description, argment description} */
30        { "version",  'v',         0,            &version, 0,     "version" },
31        { "bps",      'b',         POPT_ARG_INT, &speed,   0,     "signaling rate in bits-per-second", "BPS" },
32        { "crnl",     'c',         0,            0,      'c',     "expand cr characters to cr/lf sequences" },
33        { "hwflow",   'h',         0,            0,      'h',     "use hardware (RTS/CTS) flow control" },
34        { "noflow",   'n',         0,            0,      'n',     "use no flow control" },
35        { "raw",      'r',         0,            &raw,   0,       "don't perform any character conversions" },
36        { "swflow",   's',         0,            0,      's',     "use software (XON/XOF) flow control" } ,
37        POPT_AUTOHELP
38        { "aswflow",   'x',        0,            0,      'x',     "XXXXXXXXXXXXXXXXXXXXXX" } ,
39        { NULL, 0, 0, NULL, 0 }
40    };
41    /*
42      $ a.out --help
43
44      Usage: a.out [OPTIONS]*
45      -b, --bps=BPS   signaling rate in bits-per-second
46      -c, --crnl      expand cr characters to cr/lf sequences
47      -h, --hwflow    use hardware (RTS/CTS) flow control
48      -n, --noflow    use no flow control
49      -r, --raw       don't perform any character conversions
50      -s, --swflow    use software (XON/XOF) flow control
51
52      Help options
53      -?, --help      Show this help message
54      --usage         Display brief usage message
55    */
56
57    const char** argv2 = const_cast<const char**>(argv);
58    optCon = poptGetContext(NULL, argc, argv2, optionsTable, 0);
59    poptSetOtherOptionHelp(optCon, "[OPTIONS]* <port>");
60
61    if (argc < 2) {
62        //poptPrintUsage(optCon, stdout, 0);
63        //return exit_status::exit_when_special_argument();
64        return 0;
65    }
66
67    // Now do options processing, get portname
68    while ((c = poptGetNextOpt(optCon)) >= 0) {
69        switch (c) {
70        case 'c':
71            buf[i++] = 'c';
72            break;
73        case 'h':
74            buf[i++] = 'h';
75            break;
76        case 's':
77            buf[i++] = 's';
78            break;
79        case 'n':
80            buf[i++] = 'n';
81            break;
82        }
83    }
84
85    const char* portname = poptGetArg(optCon);
86
87    if (version) {
88        fprintf(stdout, PACKAGE_NAME " version " PACKAGE_VERSION "\n");
89        goto normal_exit;
90    }
91
92
93//     if ((portname == NULL) || !(poptPeekArg(optCon) == NULL)) {
94//         usage(optCon, 1, "Specify a single port", ".e.g., /dev/cua0\n", fp);
95//     }
96    if (c < -1) {
97        /* an error occurred during option processing */
98        fprintf(stderr, "%s: %s\n",
99                poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
100                poptStrerror(c));
101        goto error_exit;
102    }
103//     fprintf(stdout, "Options  chosen: ");
104//     for (j = 0; j < i ; j++) {
105//         fprintf(stdout, "-%c ", buf[j]);
106//     }
107//     if (raw) {
108//         fprintf(stdout, "-r ");
109//     }
110//     if (speed) {
111//         fprintf(stdout, "-b %d ", speed);
112//     }
113//     fprintf(stdout, "\nPortname chosen: %s\n", portname);
114
115    poptFreeContext(optCon);
116    return 0;
117
118normal_exit:
119    poptFreeContext(optCon);
120    exit(EXIT_SUCCESS);
121
122error_exit:
123    poptFreeContext(optCon);
124    return EXIT_FAILURE;
125
126    /*
127      struct arg_lit  *list    = arg_lit0("lL",NULL,                      gettext("list files"));
128      struct arg_lit  *recurse = arg_lit0("R",NULL,                       gettext("recurse through subdirectories"));
129      struct arg_int  *repeat  = arg_int0("k","scalar",NULL,              gettext("define scalar value k (default is 3)"));
130      struct arg_str  *defines = arg_strn("D","define","MACRO",0,argc+2,  gettext("macro definitions"));
131      struct arg_file *outfile = arg_file0("o",NULL,"<output>",           gettext("output file (default is \"-\")"));
132      struct arg_lit  *verbose = arg_lit0("v","verbose,debug",            gettext("verbose messages"));
133      struct arg_lit  *help    = arg_lit0(NULL,"help",                    gettext("print this help and exit"));
134      struct arg_lit  *version = arg_lit0(NULL,"version",                 gettext("print version information and exit"));
135      //struct arg_file *infiles = arg_filen(NULL,NULL,NULL,1,argc+2,       "input file(s)");
136      struct arg_end  *end     = arg_end(20);
137      //void* argtable[] = {list,recurse,repeat,defines,outfile,verbose,help,version,infiles,end};
138      void* argtable[] = {list,recurse,repeat,defines,outfile,verbose,help,version,end};
139      int nerrors = 0;
140
141
142      // verify the argtable[] entries were allocated sucessfully
143      if (arg_nullcheck(argtable) != 0)
144      {
145      // NULL entries were detected, some allocations must have failed
146      fprintf(stdout, PACKAGE_NAME ": insufficient memory\n");
147      goto error_exit;
148      }
149
150      // set any command line default values prior to parsing
151      repeat->ival[0]=3;
152      outfile->filename[0]="-";
153
154      // Parse the command line as defined by argtable[]
155      nerrors = arg_parse(argc,argv,argtable);
156
157      // special case: '--help' takes precedence over error reporting
158      if (help->count > 0)
159      {
160      arg_print_syntax(stdout,argtable,"\n");
161      arg_print_glossary(stdout,argtable,"  %-25s %s\n");
162      goto normal_exit;
163      }
164
165      // special case: '--version' takes precedence error reporting
166      if (version->count > 0)
167      {
168      fprintf(stdout, PACKAGE_NAME " version " PACKAGE_VERSION "\n");
169      goto normal_exit;
170      }
171
172      // If the parser returned any errors then display them and exit
173      if (nerrors > 0)
174      {
175      // Display the error details contained in the arg_end struct.
176      arg_print_errors(stdout, end, PACKAGE_NAME);
177      goto error_exit;
178      }
179
180      // normal case: take the command line options at face value
181      //    exitcode = mymain(list->count, recurse->count, repeat->ival[0],
182      //                      defines->sval, defines->count,
183      //                      outfile->filename[0], verbose->count,
184      //                      infiles->filename, infiles->count);
185
186
187      arg_freetable(argtable, _countof(argtable));
188      return 0;
189      normal_exit:
190      arg_freetable(argtable, _countof(argtable));
191      return exit_status::exit_when_special_argument();
192      error_exit:
193      arg_freetable(argtable, _countof(argtable));
194    */
195    return EXIT_FAILURE;
196}
197}
198#else
199namespace i3 {
200int execute_popt(int argc, char** argv) {
201    return 0;
202}
203}
204#endif
205
206namespace i3 {
207int init_common_global_data(int argc, char** argv) {
208    int result = execute_popt(argc, argv);
209    srand(clock());
210    return result;
211}
212
213// http://www.gnu.org/software/gettext/FAQ.html#windows_setenv
214int my_setenv (const char * name, const char * value) {
215    size_t namelen = strlen(name);
216    size_t valuelen = (value == NULL ? 0 : strlen(value));
217
218    // On Woe32, each process has two copies of the environment variables,
219    // one managed by the OS and one managed by the C library. We set
220    // the value in both locations, so that other software that looks in
221    // one place or the other is guaranteed to see the value. Even if it's
222    // a bit slow. See also
223    // <http://article.gmane.org/gmane.comp.gnu.mingw.user/8272>
224    // <http://article.gmane.org/gmane.comp.gnu.mingw.user/8273>
225    // <http://www.cygwin.com/ml/cygwin/1999-04/msg00478.html>
226#if (defined(MIL_OS_WINDOWS) && !defined(_WIN32_WCE)) || defined(__CYGWIN__)
227    if (!SetEnvironmentVariableA(name, value)) {
228        return -1;
229    }
230#endif
231
232#if defined(HAVE_PUTENV)
233    char* buffer = (char*)malloc(namelen+1+valuelen+1);
234    if (!buffer) {
235        return -1; // no need to set errno = ENOMEM
236    }
237    memcpy(buffer,name,namelen);
238    if (value != NULL) {
239        buffer[namelen] = '=';
240        memcpy(buffer+namelen+1,value,valuelen);
241        buffer[namelen+1+valuelen] = 0;
242    } else {
243        buffer[namelen] = 0;
244    }
245#ifdef _MSC_VER
246    return _putenv(buffer);
247#else
248    return putenv(buffer);
249#endif
250
251#elif defined(HAVE_SETENV)
252    return setenv(name,value,1);
253#else
254    // Uh oh, neither putenv() nor setenv() ...
255    return -1;
256#endif
257}
258
259}
260
261#if defined(MIL_OS_WINDOWS) || defined(MIL_GUI_WINDOWS)
262#ifdef _MSC_VER
263#ifndef _WIN32_WCE
264#pragma comment(lib, "psapi.lib")
265#pragma comment(lib, "shlwapi.lib")
266#pragma comment(lib, "comctl32.lib")
267#else
268#pragma comment(lib, "aygshell.lib")
269#endif
270
271// http://msdn.microsoft.com/en-us/library/bb531404.aspx
272// swap _M_IX86 and _M_X64
273#ifdef UNICODE
274#if defined _M_X64
275#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
276#elif defined _M_IX86
277#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
278#elif defined _M_IA64
279#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'\"")
280#else
281#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
282#endif
283#endif
284
285#ifdef _M_X64
286#define I3_ARCH_DIR "x64"
287#elif defined(_M_IX86)
288#define I3_ARCH_DIR "ix86"
289#elif defined(_M_ALPHA)
290#define I3_ARCH_DIR "alpha"
291#elif defined(_SH3_)
292#define I3_ARCH_DIR "sh3"
293#elif defined(_M_IA64)
294#define I3_ARCH_DIR "ia64"
295#elif defined(_MIPS_)
296#define I3_ARCH_DIR "mips"
297#elif defined(_ARM_)
298#define I3_ARCH_DIR "arm"
299#else
300#define I3_ARCH_DIR "unknown"
301#endif
302
303#if defined(_MT) && defined(_DLL) && defined(_DEBUG)
304#define I3_WINDOWS_RUNTIME "-MDd"
305#elif defined(_MT) && defined(_DLL)
306#define I3_WINDOWS_RUNTIME "-MD"
307#elif defined(_MT)  && defined(_DEBUG)
308#define I3_WINDOWS_RUNTIME "-MTd"
309#elif defined(_MT)
310#define I3_WINDOWS_RUNTIME "-MT"
311#else
312#error
313#endif
314
315#ifdef ENABLE_NLS
316#pragma comment(lib, I3_ARCH_DIR "/libintl" I3_WINDOWS_RUNTIME ".lib")
317#endif
318
319#endif
320
321namespace i3 {
322HINSTANCE hInstance = NULL;
323Dll dll = {};
324}
325
326#endif
327
Note: See TracBrowser for help on using the browser.