#include <stdbool.h>
#include <string.h>
+#define ULAS_PATHMAX 4096
+
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
// configurable tokens
#define ULAS_TOK_COMMENT ';'
-#define ULAS_TOK_DIRECTIVE_BEGIN '#'
+// start of as directives such as .org
+#define ULAS_TOK_ASDIR_BEGIN '.'
+// start of preprocessor directives such as #define or #include
+#define ULAS_TOK_PREPROC_BEGIN '#'
// format macros
#define ULAS_FMT(f, fmt) \
extern FILE *ulaserr;
struct ulas_config {
+ // argv represents file names
char **argv;
int argc;
+ char *output_path;
+
bool verbose;
};
int ulas_main(struct ulas_config cfg);
+char *ulas_strndup(const char *src, size_t n);
+
#endif
#define ULAS_OPTS "hvV"
// args with value
-#define ULAS_OPTS_ARG ""
+#define ULAS_OPTS_ARG "o:"
-#define ULAS_HELP(a, desc) printf("\t%s\t%s\n", (a), desc);
+#define ULAS_HELP(a, desc) printf("\t-%s\t%s\n", (a), desc);
void ulas_help(void) {
printf("%s\n", ULAS_NAME);
- printf("Usage %s [-%s]\n\n", ULAS_NAME, ULAS_OPTS);
- ULAS_HELP("-h", "display this help and exit");
- ULAS_HELP("-V", "display version info and exit");
- ULAS_HELP("-v", "verbose output");
+ printf("Usage %s [-%s] [-o=path] [input]\n\n", ULAS_NAME, ULAS_OPTS);
+ ULAS_HELP("h", "display this help and exit");
+ ULAS_HELP("V", "display version info and exit");
+ ULAS_HELP("v", "verbose output");
+ ULAS_HELP("o=path", "Output file");
}
void ulas_version(void) { printf("%s version %s\n", ULAS_NAME, ULAS_VER); }
case 'v':
cfg->verbose = true;
break;
+ case 'o':
+ cfg->output_path = ulas_strndup(optarg, ULAS_PATHMAX);
+ break;
case '?':
break;
default:
#include "ulas.h"
+#include <errno.h>
+#include <string.h>
FILE *ulasin = NULL;
FILE *ulasout = NULL;
return cfg;
}
+char *ulas_strndup(const char *src, size_t n) {
+ size_t len = MIN(strlen(src), n);
+ char *dst = malloc(len);
+ strncpy(dst, src, len);
+ return dst;
+}
+
int ulas_main(struct ulas_config cfg) {
+ if (cfg.output_path) {
+ ulasout = fopen(cfg.output_path, "we");
+ if (!ulasout) {
+ fprintf(ulaserr, "%s: %s\n", cfg.output_path, strerror(errno));
+ free(cfg.output_path);
+ return -1;
+ }
+ }
+
ulas_init(cfg);
+
+ if (cfg.output_path) {
+ fclose(ulasout);
+ free(cfg.output_path);
+ return -1;
+ }
+
return 0;
}