All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
nwmain.h
Go to the documentation of this file.
1 /**********************************************************************
2  * File: nwmain.h
3  * Description: Tool to declare main, making windows invisible.
4  * Author: Ray Smith
5  * Created: Fri Sep 07 13:27:50 MDT 1995
6  *
7  * (C) Copyright 1995, Hewlett-Packard Co.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 #ifndef RUNMAIN_H
21 #define RUNMAIN_H
22 
23 #include "host.h"
24 #include "params.h"
25 
26 #define DECLARE_MAIN(ARGC,ARGV)\
27 STRING_VAR(init_config_file,"config","Config file to read on startup");\
28 REALLY_DECLARE_MAIN(ARGC,ARGV)
29 
30 #define DECLARE_MAIN_CONFIG(ARGC,ARGV,NAME)\
31 STRING_VAR(init_config_file,NAME,"Config file to read on startup");\
32 REALLY_DECLARE_MAIN(ARGC,ARGV)
33 
34 #ifndef __UNIX__
35 
36 #define REALLY_DECLARE_MAIN(ARGC,ARGV)\
37 \
38 /**********************************************************************\
39 * parse_args\
40 *\
41 * Turn a list of args into a new list of args with each separate\
42 * whitespace spaced string being an arg.\
43 **********************************************************************/\
44 \
45 inT32 parse_args( /*refine arg list*/\
46 inT32 argc, /*no of input args*/\
47 char *argv[], /*input args*/\
48 char *arglist[] /*output args*/\
49 )\
50 {\
51  inT32 argcount; /*converted argc*/\
52  char *testchar; /*char in option string*/\
53  inT32 arg; /*current argument*/\
54 \
55  argcount=0; /*no of options*/\
56  for (arg=0;arg<argc;arg++)\
57  {\
58  testchar=argv[arg]; /*start of arg*/\
59  do\
60  {\
61  while (*testchar\
62  && (*testchar==' ' || *testchar=='"' || *testchar=='\n' || *testchar=='\t'))\
63  testchar++; /*skip white space*/\
64  if (*testchar)\
65  {\
66  arglist[argcount++]=testchar; /*new arg*/\
67  do\
68  {\
69  for (testchar++;*testchar\
70  && *testchar!=' ' && *testchar!='"' && *testchar!='\n' && *testchar!='\t';\
71  testchar++); /*skip to white space*/\
72  }\
73  while (*testchar=='"' && testchar[1]!=' ' && testchar[1]!='\0' && testchar[1]!='\n' && testchar[1]!='\t');\
74  if (*testchar)\
75  *testchar++='\0'; /*turn to separate args*/\
76  }\
77  }\
78  while (*testchar);\
79  }\
80  return argcount; /*new number of args*/\
81 }\
82 \
83 inT32 global_exit_code;\
84 inT32 real_main(inT32,const char**);\
85 \
86 inT32 run_main( /*the main thread*/\
87 CWinApp* theapp /*arguments*/\
88 )\
89 {\
90  char **argv;\
91  char *argsin[2];\
92  inT32 argc;\
93  inT32 exit_code;\
94  \
95  argsin[0]=strdup(theapp->m_pszExeName);\
96  argsin[1]=strdup(theapp->m_lpCmdLine);\
97 /*allocate memory for the args. There can never be more than half*/\
98 /*the total number of characters in the arguments.*/\
99  argv=(char**)malloc(((strlen(argsin[0])+strlen(argsin[1]))/2+1)*sizeof(char*));\
100 \
101 /*now construct argv as it should be for C.*/\
102  argc=parse_args(2,argsin,argv);\
103 \
104 /*call main(argc,argv) here*/\
105  exit_code=real_main(argc,(const char **)argv);\
106 \
107 \
108 /*now get rid of the main app window*/\
109  if (theapp!=NULL && theapp->m_pMainWnd!=NULL)\
110  PostMessage(theapp->m_pMainWnd->m_hWnd,WM_QUIT,0,0);\
111  free(argsin[0]);\
112  free(argsin[1]);\
113  free(argv);\
114  global_exit_code=exit_code;\
115  return exit_code;\
116 }\
117 \
118 inT32 real_main(inT32 ARGC,const char* ARGV[])\
119 
120 #else
121 
122 #define REALLY_DECLARE_MAIN(ARGC,ARGV)\
123 \
124 /**********************************************************************\
125 * parse_args\
126 *\
127 * Turn a list of args into a new list of args with each separate\
128 * whitespace spaced string being an arg.\
129 **********************************************************************/\
130 \
131 inT32 parse_args( /*refine arg list*/\
132 inT32 argc, /*no of input args*/\
133 char *argv[], /*input args*/\
134 char *arglist[] /*output args*/\
135 )\
136 {\
137  inT32 argcount; /*converted argc*/\
138  char *testchar; /*char in option string*/\
139  inT32 arg; /*current argument*/\
140 \
141  argcount=0; /*no of options*/\
142  for (arg=0;arg<argc;arg++)\
143  {\
144  testchar=argv[arg]; /*start of arg*/\
145  do\
146  {\
147  while (*testchar\
148  && (*testchar==' ' || *testchar=='"' || *testchar=='\n' || *testchar=='\t'))\
149  testchar++; /*skip white space*/\
150  if (*testchar)\
151  {\
152  arglist[argcount++]=testchar; /*new arg*/\
153  do\
154  {\
155  for (testchar++;*testchar\
156  && *testchar!=' ' && *testchar!='"' && *testchar!='\n' && *testchar!='\t';\
157  testchar++); /*skip to white space*/\
158  }\
159  while (*testchar=='"' && testchar[1]!=' ' && testchar[1]!='\0' && testchar[1]!='\n' && testchar[1]!='\t');\
160  if (*testchar)\
161  *testchar++='\0'; /*turn to separate args*/\
162  }\
163  }\
164  while (*testchar);\
165  }\
166  return argcount; /*new number of args*/\
167 }\
168 \
169 inT32 main(inT32 ARGC,const char* ARGV[])\
170 
171 #endif
172 
173 #else
174 #error "NOT allowed to include nwmain.h or runmain.h twice!!"
175 #endif