struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / sdas / asxxsrc / sdas.c
blob8ec4521ebeb012dda101617341080dc09717f1fc
1 /* sdas.c
3 Copyright (C) 2009-2010 Borut Razem
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any
8 later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #ifndef _WIN32
19 #include <libgen.h>
20 #endif
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <ctype.h>
26 #include "sdas.h"
28 #define NELEM(x) (sizeof (x) / sizeof (x)[0])
31 static int sdas = -1;
32 static enum sdas_target_e target = TARGET_ID_UNKNOWN;
35 static char
36 *program_name (char *path)
38 #ifdef _WIN32
39 static char fname[_MAX_FNAME];
41 _splitpath (path, NULL, NULL, fname, NULL);
42 return fname;
43 #else
44 return basename (path);
45 #endif
49 static void
50 check_init(void)
52 if (sdas == -1)
54 fprintf(stderr, "sdas_init not called!\n");
55 exit (1);
60 void
61 sdas_init (char *path)
63 struct tgt_s {
64 char *str;
65 enum sdas_target_e target;
66 } tgt[] = {
67 { "gb", TARGET_ID_GB },
68 { "z80", TARGET_ID_Z80 },
69 { "8051", TARGET_ID_8051 },
70 { "390", TARGET_ID_DS390 },
71 { "6808", TARGET_ID_6808 },
72 { "stm8", TARGET_ID_STM8 },
73 { "pdk13", TARGET_ID_PDK13 },
74 { "pdk14", TARGET_ID_PDK14 },
75 { "pdk15", TARGET_ID_PDK15 },
76 { "pdk16", TARGET_ID_PDK16 },
78 int i = NELEM (tgt);
80 char *progname = program_name (path);
81 #if _WIN32
82 /* convert it to lower case:
83 on DOS and Windows 9x the file name in argv[0] is uppercase */
84 char *p;
86 for (p = progname; '\0' != *p; ++p)
87 *p = tolower (*p);
88 #endif
89 if ((sdas = (strncmp(progname, "sdas", 4) == 0)) != 0)
91 for (i = 0; i < NELEM (tgt); ++i)
93 if (strstr(progname, tgt[i].str))
95 target = tgt[i].target;
96 break;
100 /* diagnostic message */
101 if (getenv ("SDAS_DIAG"))
103 printf ("sdas path: %s\n", path);
104 printf ("is sdas: %d\n", sdas);
105 if (sdas)
106 printf ("sdas target: %s\n", (i >= NELEM (tgt)) ? "unknown" : tgt[i].str);
112 is_sdas(void)
114 check_init();
115 return sdas;
119 enum sdas_target_e
120 get_sdas_target(void)
122 check_init();
123 return target;
126 void
127 set_sdas_target(enum sdas_target_e newtarget)
129 target = newtarget;
133 is_sdas_target_z80_like(void)
135 check_init();
136 return target == TARGET_ID_Z80 || target == TARGET_ID_GB;
141 is_sdas_target_8051_like(void)
143 check_init();
144 return target == TARGET_ID_8051 || target == TARGET_ID_DS390;
148 is_sdas_target_stm8(void)
150 check_init();
151 return target == TARGET_ID_STM8;
155 is_sdas_target_pdk(void)
157 check_init();
158 return target == TARGET_ID_PDK13 ||
159 target == TARGET_ID_PDK14 ||
160 target == TARGET_ID_PDK15;