1 /*
2  * This file is part of RGBDS.
3  *
4  * Copyright (c) 1997-2019, Carsten Sorensen and RGBDS contributors.
5  *
6  * SPDX-License-Identifier: MIT
7  */
8 
9 /* Declarations that all modules use, as well as `main` and related */
10 #ifndef RGBDS_LINK_MAIN_H
11 #define RGBDS_LINK_MAIN_H
12 
13 #include <stdint.h>
14 #include <stdbool.h>
15 #include <stdio.h>
16 
17 #include "helpers.h"
18 
19 /* Variables related to CLI options */
20 extern bool isDmgMode;
21 extern char       *linkerScriptName;
22 extern char const *mapFileName;
23 extern char const *symFileName;
24 extern char const *overlayFileName;
25 extern char const *outputFileName;
26 extern uint8_t padValue;
27 extern uint16_t scrambleROMX;
28 extern uint8_t scrambleWRAMX;
29 extern uint8_t scrambleSRAM;
30 extern bool is32kMode;
31 extern bool beVerbose;
32 extern bool isWRA0Mode;
33 extern bool disablePadding;
34 
35 struct FileStackNode {
36 	struct FileStackNode *parent;
37 	/* Line at which the parent context was exited; meaningless for the root level */
38 	uint32_t lineNo;
39 
40 	enum {
41 		NODE_REPT,
42 		NODE_FILE,
43 		NODE_MACRO,
44 	} type;
45 	union {
46 		char *name; /* NODE_FILE, NODE_MACRO */
47 		struct { /* NODE_REPT */
48 			uint32_t reptDepth;
49 			uint32_t *iters;
50 		};
51 	};
52 };
53 
54 /* Helper macro for printing verbose-mode messages */
55 #define verbosePrint(...)   do { \
56 					if (beVerbose) \
57 						fprintf(stderr, __VA_ARGS__); \
58 				} while (0)
59 
60 /**
61  * Dump a file stack to stderr
62  * @param node The leaf node to dump the context of
63  */
64 char const *dumpFileStack(struct FileStackNode const *node);
65 
66 void warning(struct FileStackNode const *where, uint32_t lineNo,
67 	     char const *fmt, ...) format_(printf, 3, 4);
68 
69 void error(struct FileStackNode const *where, uint32_t lineNo,
70 	   char const *fmt, ...) format_(printf, 3, 4);
71 
72 _Noreturn void fatal(struct FileStackNode const *where, uint32_t lineNo,
73 		     char const *fmt, ...) format_(printf, 3, 4);
74 
75 /**
76  * Opens a file if specified, and aborts on error.
77  * @param fileName The name of the file to open; if NULL, no file will be opened
78  * @param mode The mode to open the file with
79  * @return A pointer to a valid FILE structure, or NULL if fileName was NULL
80  */
81 FILE *openFile(char const *fileName, char const *mode);
82 
83 #define closeFile(file) do { \
84 				FILE *tmp = file; \
85 				if (tmp) \
86 					fclose(tmp); \
87 			} while (0)
88 
89 #endif /* RGBDS_LINK_MAIN_H */
90