1 /****************************************************************************
2 *
3 *  This code is Public Domain.
4 *
5 *  ========================================================================
6 *
7 * Description:  JWasm top level module
8 *
9 ****************************************************************************/
10 
11 #include <signal.h>
12 
13 #include "globals.h"
14 #include "msgtext.h"
15 #include "cmdline.h"
16 #include "input.h" /* GetFNamePart() */
17 
18 #if defined(__UNIX__) || defined(__CYGWIN__) || defined(__DJGPP__)
19 
20 #define WILDCARDS 0
21 #define CATCHBREAK 0
22 
23 #else
24 
25 #define WILDCARDS 1
26 #ifdef __POCC__
27 #define CATCHBREAK 0
28 #else
29 #define CATCHBREAK 1
30 #endif
31 
32 #endif
33 
34 #if WILDCARDS
35 
36  #ifdef __UNIX__
37   #include <unistd.h>
38  #else
39   #include <io.h>
40  #endif
41 #endif
42 
43 #ifdef TRMEM
44 void tm_Init( void );
45 void tm_Fini( void );
46 #endif
47 
genfailure(int signo)48 static void genfailure( int signo )
49 /*********************************/
50 {
51 #if CATCHBREAK
52     if (signo != SIGBREAK)
53 #else
54     if (signo != SIGTERM)
55 #endif
56         EmitError( GENERAL_FAILURE );
57     close_files();
58     exit( EXIT_FAILURE );
59 }
60 
main(int argc,char ** argv)61 int main( int argc, char **argv )
62 /*******************************/
63 {
64     char    *pEnv;
65     int     numArgs = 0;
66     int     numFiles = 0;
67     int     rc = 0;
68 #if WILDCARDS
69     /* v2.11: _findfirst/next/close() handle, should be of type intptr_t.
70      * since this type isn't necessarily defined, type long is used as substitute.
71      */
72     long    fh;
73     const char *pfn;
74     int     dirsize;
75     struct  _finddata_t finfo;
76     char    fname[FILENAME_MAX];
77 #endif
78 
79 #if 0 //def DEBUG_OUT    /* DebugMsg() cannot be used that early */
80     int i;
81     for ( i = 1; i < argc; i++ ) {
82         printf("argv[%u]=>%s<\n", i, argv[i] );
83     }
84 #endif
85 
86 #ifdef TRMEM
87     tm_Init();
88 #endif
89 
90     pEnv = getenv( "JWASM" );
91     if ( pEnv == NULL )
92         pEnv = "";
93     argv[0] = pEnv;
94 
95 #ifndef DEBUG_OUT
96     signal(SIGSEGV, genfailure);
97 #endif
98 
99 #if CATCHBREAK
100     signal(SIGBREAK, genfailure);
101 #else
102     signal(SIGTERM, genfailure);
103 #endif
104 
105     /* ParseCmdLine() returns NULL if no source file name has been found (anymore) */
106     while ( ParseCmdline( (const char **)argv, &numArgs ) ) {
107         numFiles++;
108         write_logo();
109 #if WILDCARDS
110         if ((fh = _findfirst( Options.names[ASM], &finfo )) == -1 ) {
111             DebugMsg(("main: _findfirst(%s) failed\n", Options.names[ASM] ));
112             EmitErr( CANNOT_OPEN_FILE, Options.names[ASM], ErrnoStr() );
113             break;
114         }
115         /* v2.12: _splitpath()/_makepath() removed */
116         //_splitpath( Options.names[ASM], drv, dir, NULL, NULL );
117         //DebugMsg(("main: _splitpath(%s): drv=\"%s\" dir=\"%s\"\n", Options.names[ASM], drv, dir ));
118         pfn = GetFNamePart( Options.names[ASM] );
119         dirsize = pfn - Options.names[ASM];
120         memcpy( fname, Options.names[ASM], dirsize );
121         do {
122             /* v2.12: _splitpath()/_makepath() removed */
123             //_makepath( fname, drv, dir, finfo.name, NULL );
124             //DebugMsg(("main: _makepath(\"%s\", \"%s\", \"%s\")=\"%s\"\n", drv, dir, finfo.name, fname ));
125             strcpy( &fname[dirsize], finfo.name );
126             DebugMsg(("main: fname=%s\n", fname ));
127             rc = AssembleModule( fname );  /* assemble 1 module */
128         } while ( ( _findnext( fh, &finfo ) != -1 ) );
129         _findclose( fh );
130 #else
131         rc = AssembleModule( Options.names[ASM] );
132 #endif
133     };
134     CmdlineFini();
135     if ( numArgs == 0 ) {
136         write_logo();
137         printf( "%s", MsgGetEx( MSG_USAGE ) );
138     } else if ( numFiles == 0 )
139         EmitError( NO_FILENAME_SPECIFIED );
140 
141 #ifdef TRMEM
142     tm_Fini();
143 #endif
144 
145     DebugMsg(("main: exit, return code=%u\n", 1 - rc ));
146     return( 1 - rc ); /* zero if no errors */
147 }
148