/* $Id: main.c,v 1.80 2024/12/31 20:50:11 tom Exp $ */

#include <signal.h>
#if !defined(_WIN32) || defined(__MINGW32__)
#include <unistd.h>		/* for _exit() */
#else
#include <stdlib.h>		/* for _exit() */
#endif

#include "defs.h"

#ifdef HAVE_MKSTEMP
# define USE_MKSTEMP 1
#elif defined(HAVE_FCNTL_H)
# define USE_MKSTEMP 1
# include <fcntl.h>		/* for open(), O_EXCL, etc. */
#else
# define USE_MKSTEMP 0
#endif

#ifndef W_OK
#define W_OK 02
#endif

#if USE_MKSTEMP
#include <sys/types.h>
#include <sys/stat.h>

typedef struct _my_tmpfiles
{
    struct _my_tmpfiles *next;
    char *name;
}
MY_TMPFILES;

static MY_TMPFILES *my_tmpfiles;
#endif /* USE_MKSTEMP */

char dflag;
char dflag2;
char gflag;
char iflag;
char lflag;
static char oflag;
char rflag;
char sflag;
char tflag;
char vflag;

const char *symbol_prefix;
const char *myname = "yacc";

int lineno;
int outline;

static char default_file_prefix[] = "y";
static int explicit_file_name;

static char *file_prefix = default_file_prefix;

char *code_file_name;
char *input_file_name;
size_t input_file_name_len = 0;
char *defines_file_name;
char *externs_file_name;

static char *graph_file_name;
static char *output_file_name;
static char *verbose_file_name;

FILE *action_file;	/*  a temp file, used to save actions associated    */
			/*  with rules until the parser is written          */
FILE *code_file;	/*  y.code.c (used when the -r option is specified) */
FILE *defines_file;	/*  y.tab.h                                         */
FILE *externs_file;	/*  y.tab.i                                         */
FILE *input_file;	/*  the input file                                  */
FILE *output_file;	/*  y.tab.c                                         */
FILE *text_file;	/*  a temp file, used to save text until all        */
			/*  symbols have been defined                       */
FILE *union_file;	/*  a temp file, used to save the union             */
			/*  definition until all symbol have been           */
			/*  defined                                         */
FILE *verbose_file;	/*  y.output                                        */
FILE *graph_file;	/*  y.dot                                           */

Value_t nitems;
Value_t nrules;
Value_t nsyms;
Value_t ntokens;
Value_t nvars;

Value_t start_symbol;
char **symbol_name;
char **symbol_pname;
Value_t *symbol_value;
Value_t *symbol_prec;
char *symbol_assoc;

int pure_parser;
int token_table;
int error_verbose;

#if defined(YYBTYACC)
Value_t *symbol_pval;
char **symbol_destructor;
char **symbol_type_tag;
int locations = 0;	/* default to no position processing */
int backtrack = 0;	/* default is no backtracking */
char *initial_action = NULL;
#endif

int exit_code;

Value_t *ritem;
Value_t *rlhs;
Value_t *rrhs;
Value_t *rprec;
Assoc_t *rassoc;
Value_t **derives;
char *nullable;

/*
 * Since fclose() is called via the signal handler, it might die.  Don't loop
 * if there is a problem closing a file.
 */
#define DO_CLOSE(fp) \
	if (fp != NULL) { \
	    FILE *use = fp; \
	    fp = NULL; \
	    fclose(use); \
	}

static int got_intr = 0;

void
done(int k)
{
    DO_CLOSE(input_file);
    DO_CLOSE(output_file);
    if (iflag)
	DO_CLOSE(externs_file);
    if (rflag)
	DO_CLOSE(code_file);

    DO_CLOSE(action_file);
    DO_CLOSE(defines_file);
    DO_CLOSE(graph_file);
    DO_CLOSE(text_file);
    DO_CLOSE(union_file);
    DO_CLOSE(verbose_file);

    if (got_intr)
	_exit(EXIT_FAILURE);

#ifdef NO_LEAKS
    DO_FREE(input_file_name);

    if (rflag)
	DO_FREE(code_file_name);

    if (dflag && !dflag2)
	DO_FREE(defines_file_name);

    if (iflag)
	DO_FREE(externs_file_name);

    if (oflag)
	DO_FREE(output_file_name);

    if (vflag)
	DO_FREE(verbose_file_name);

    if (gflag)
	DO_FREE(graph_file_name);

    lr0_leaks();
    lalr_leaks();
    mkpar_leaks();
    mstring_leaks();
    output_leaks();
    reader_leaks();
#endif

    exit(k);
}

static void
onintr(int sig GCC_UNUSED)
{
    got_intr = 1;
    done(EXIT_FAILURE);
}

static void
set_signals(void)
{
#ifdef SIGINT
    if (signal(SIGINT, SIG_IGN) != SIG_IGN)
	signal(SIGINT, onintr);
#endif
#ifdef SIGTERM
    if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
	signal(SIGTERM, onintr);
#endif
#ifdef SIGHUP
    if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
	signal(SIGHUP, onintr);
#endif
}

#define SIZEOF(v) (sizeof(v) / sizeof((v)[0]))

/*
 * Long options are provided only as a compatibility aid for scripters.
 */
/* *INDENT-OFF* */
static const struct {
    const char long_opt[16];
    const char yacc_arg;
    const char yacc_opt;
} long_opts[] = {
    { "defines",     1, 'H' },
    { "file-prefix", 1, 'b' },
    { "graph",       0, 'g' },
    { "help",        0, 'h' },
    { "name-prefix", 1, 'p' },
    { "no-lines",    0, 'l' },
    { "output",      1, 'o' },
    { "version",     0, 'V' }
};
/* *INDENT-ON* */

/*
 * Usage-message is designed for 80 columns, with some unknowns.  Account for
 * those in the maximum width so that the usage message uses no relocatable
 * pointers.
 */
#define USAGE_COLS (80 + sizeof(DEFINES_SUFFIX) + sizeof(OUTPUT_SUFFIX))

static void
usage(void)
{
    /* *INDENT-OFF* */
    static const char msg[][USAGE_COLS] =
    {
	{ "  -b file_prefix        set filename prefix (default \"y.\")" },
	{ "  -B                    create a backtracking parser" },
	{ "  -d                    write definitions (" DEFINES_SUFFIX ")" },
	{ "  -h                    print this help-message" },
	{ "  -H defines_file       write definitions to defines_file" },
	{ "  -i                    write interface (y.tab.i)" },
	{ "  -g                    write a graphical description" },
	{ "  -l                    suppress #line directives" },
	{ "  -L                    enable position processing, e.g., \"%locations\"" },
	{ "  -o output_file        (default \"" OUTPUT_SUFFIX "\")" },
	{ "  -p symbol_prefix      set symbol prefix (default \"yy\")" },
	{ "  -P                    create a reentrant parser, e.g., \"%pure-parser\"" },
	{ "  -r                    produce separate code and table files (y.code.c)" },
	{ "  -s                    suppress #define's for quoted names in %token lines" },
	{ "  -t                    add debugging support" },
	{ "  -v                    write description (y.output)" },
	{ "  -V                    show version information and exit" },
    };
    /* *INDENT-ON* */
    unsigned n;

    fflush(stdout);
    fprintf(stderr, "Usage: %s [options] filename\n", myname);

    fprintf(stderr, "\nOptions:\n");
    for (n = 0; n < SIZEOF(msg); ++n)
    {
	fprintf(stderr, "%s\n", msg[n]);
    }

    fprintf(stderr, "\nLong options:\n");
    for (n = 0; n < SIZEOF(long_opts); ++n)
    {
	fprintf(stderr, "  --%-20s-%c\n",
		long_opts[n].long_opt,
		long_opts[n].yacc_opt);
    }

    exit(EXIT_FAILURE);
}

static void
invalid_option(const char *option)
{
    fprintf(stderr, "invalid option: %s\n", option);
    usage();
}

static void
setflag(int ch)
{
    switch (ch)
    {
    case 'B':
#if defined(YYBTYACC)
	backtrack = 1;
#else
	unsupported_flag_warning("-B", "reconfigure with --enable-btyacc");
#endif
	break;

    case 'd':
	dflag = 1;
	dflag2 = 0;
	break;

    case 'g':
	gflag = 1;
	break;

    case 'i':
	iflag = 1;
	break;

    case 'l':
	lflag = 1;
	break;

    case 'L':
#if defined(YYBTYACC)
	locations = 1;
#else
	unsupported_flag_warning("-L", "reconfigure with --enable-btyacc");
#endif
	break;

    case 'P':
	pure_parser = 1;
	break;

    case 'r':
	rflag = 1;
	break;

    case 's':
	sflag = 1;
	break;

    case 't':
	tflag = 1;
	break;

    case 'v':
	vflag = 1;
	break;

    case 'V':
	printf("%s - %s\n", myname, VERSION);
	exit(EXIT_SUCCESS);

    case 'y':
	/* noop for bison compatibility. byacc is already designed to be posix
	 * yacc compatible. */
	break;

    default:
	usage();
    }
}

static void
getargs(int argc, char *argv[])
{
    int i;
#ifdef HAVE_GETOPT
    int ch;
#endif

    /*
     * Map bison's long-options into yacc short options.
     */
    for (i = 1; i < argc; ++i)
    {
	char *a = argv[i];

	if (!strncmp(a, "--", 2))
	{
	    char *eqls;
	    size_t lc;
	    size_t len;

	    if ((len = strlen(a)) == 2)
		break;

	    if ((eqls = strchr(a, '=')) != NULL)
	    {
		len = (size_t)(eqls - a);
		if (len == 0 || eqls[1] == '\0')
		    invalid_option(a);
	    }

	    for (lc = 0; lc < SIZEOF(long_opts); ++lc)
	    {
		if (!strncmp(long_opts[lc].long_opt, a + 2, len - 2))
		{
		    if (eqls != NULL && !long_opts[lc].yacc_arg)
			invalid_option(a);
		    *a++ = '-';
		    *a++ = long_opts[lc].yacc_opt;
		    *a = '\0';
		    if (eqls)
		    {
			while ((*a++ = *++eqls) != '\0') /* empty */ ;
		    }
		    break;
		}
	    }
	    if (!strncmp(a, "--", 2))
		invalid_option(a);
	}
    }

#ifdef HAVE_GETOPT
    if (argc > 0)
	myname = argv[0];

    while ((ch = getopt(argc, argv, "Bb:dghH:ilLo:Pp:rstVvy")) != -1)
    {
	switch (ch)
	{
	case 'b':
	    file_prefix = optarg;
	    break;
	case 'h':
	    usage();
	    break;
	case 'H':
	    dflag = dflag2 = 1;
	    defines_file_name = optarg;
	    break;
	case 'o':
	    output_file_name = optarg;
	    explicit_file_name = 1;
	    break;
	case 'p':
	    symbol_prefix = optarg;
	    break;
	default:
	    setflag(ch);
	    break;
	}
    }
    if ((i = optind) < argc)
    {
	/* getopt handles "--" specially, while we handle "-" specially */
	if (!strcmp(argv[i], "-"))
	{
	    if ((i + 1) < argc)
		usage();
	    input_file = stdin;
	    return;
	}
    }
#else
    char *s;
    int ch;

    if (argc > 0)
	myname = argv[0];

    for (i = 1; i < argc; ++i)
    {
	s = argv[i];
	if (*s != '-')
	    break;
	switch (ch = *++s)
	{
	case '\0':
	    input_file = stdin;
	    if (i + 1 < argc)
		usage();
	    return;

	case '-':
	    ++i;
	    goto no_more_options;

	case 'b':
	    if (*++s)
		file_prefix = s;
	    else if (++i < argc)
		file_prefix = argv[i];
	    else
		usage();
	    continue;

	case 'H':
	    dflag = dflag2 = 1;
	    if (*++s)
		defines_file_name = s;
	    else if (++i < argc)
		defines_file_name = argv[i];
	    else
		usage();
	    continue;

	case 'o':
	    if (*++s)
		output_file_name = s;
	    else if (++i < argc)
		output_file_name = argv[i];
	    else
		usage();
	    explicit_file_name = 1;
	    continue;

	case 'p':
	    if (*++s)
		symbol_prefix = s;
	    else if (++i < argc)
		symbol_prefix = argv[i];
	    else
		usage();
	    continue;

	default:
	    setflag(ch);
	    break;
	}

	for (;;)
	{
	    switch (ch = *++s)
	    {
	    case '\0':
		goto end_of_option;

	    default:
		setflag(ch);
		break;
	    }
	}
      end_of_option:;
    }

  no_more_options:

#endif /* HAVE_GETOPT */
    if (i + 1 != argc)
	usage();
    input_file_name_len = strlen(argv[i]);
    input_file_name = TMALLOC(char, input_file_name_len + 1);
    NO_SPACE(input_file_name);
    strcpy(input_file_name, argv[i]);
}

void *
allocate(size_t n)
{
    void *p;

    p = NULL;
    if (n)
    {
	p = CALLOC(1, n);
	NO_SPACE(p);
    }
    return (p);
}

#define CREATE_FILE_NAME(dest, suffix) \
	dest = alloc_file_name(len, suffix)

static char *
alloc_file_name(size_t len, const char *suffix)
{
    char *result = TMALLOC(char, len + strlen(suffix) + 1);
    if (result == NULL)
	on_error();
    strcpy(result, file_prefix);
    strcpy(result + len, suffix);
    return result;
}

static char *
find_suffix(char *name, const char *suffix)
{
    size_t len = strlen(name);
    size_t slen = strlen(suffix);
    if (len >= slen)
    {
	name += len - slen;
	if (strcmp(name, suffix) == 0)
	    return name;
    }
    return NULL;
}

static void
create_file_names(void)
{
    size_t len;
    const char *defines_suffix;
    const char *externs_suffix;
    char *suffix;

    suffix = NULL;
    defines_suffix = DEFINES_SUFFIX;
    externs_suffix = EXTERNS_SUFFIX;

    /* compute the file_prefix from the user provided output_file_name */
    if (output_file_name != NULL)
    {
	if (!(suffix = find_suffix(output_file_name, OUTPUT_SUFFIX))
	    && (suffix = find_suffix(output_file_name, ".c")))
	{
	    defines_suffix = ".h";
	    externs_suffix = ".i";
	}
    }

    if (suffix != NULL)
    {
	len = (size_t)(suffix - output_file_name);
	file_prefix = TMALLOC(char, len + 1);
	NO_SPACE(file_prefix);
	strncpy(file_prefix, output_file_name, len)[len] = 0;
    }
    else
	len = strlen(file_prefix);

    /* if "-o filename" was not given */
    if (output_file_name == NULL)
    {
	oflag = 1;
	CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
    }

    if (rflag)
    {
	CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
    }
    else
	code_file_name = output_file_name;

    if (dflag && !dflag2)
    {
	if (explicit_file_name)
	{
	    char *xsuffix;
	    defines_file_name = strdup(output_file_name);
	    if (defines_file_name == NULL)
		on_error();
	    /* does the output_file_name have a known suffix */
	    xsuffix = strrchr(output_file_name, '.');
	    if (xsuffix != NULL &&
		(!strcmp(xsuffix, ".c") ||	/* good, old-fashioned C */
		 !strcmp(xsuffix, ".C") ||	/* C++, or C on Windows */
		 !strcmp(xsuffix, ".cc") ||	/* C++ */
		 !strcmp(xsuffix, ".cxx") ||	/* C++ */
		 !strcmp(xsuffix, ".cpp")))	/* C++ (Windows) */
	    {
		strncpy(defines_file_name, output_file_name,
			(size_t) (xsuffix - output_file_name + 1));
		defines_file_name[xsuffix - output_file_name + 1] = 'h';
		defines_file_name[xsuffix - output_file_name + 2] = 0;
	    }
	    else
	    {
		fprintf(stderr, "%s: suffix of output file name %s"
			" not recognized, no -d file generated.\n",
			myname, output_file_name);
		dflag = 0;
		free(defines_file_name);
		defines_file_name = NULL;
	    }
	}
	else
	{
	    CREATE_FILE_NAME(defines_file_name, defines_suffix);
	}
    }

    if (iflag)
    {
	CREATE_FILE_NAME(externs_file_name, externs_suffix);
    }

    if (vflag)
    {
	CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
    }

    if (gflag)
    {
	CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
    }

    if (suffix != NULL)
    {
	FREE(file_prefix);
    }
}

#if USE_MKSTEMP
static void
close_tmpfiles(void)
{
    while (my_tmpfiles != NULL)
    {
	MY_TMPFILES *next = my_tmpfiles->next;

	(void)chmod(my_tmpfiles->name, 0644);
	(void)unlink(my_tmpfiles->name);

	free(my_tmpfiles->name);
	free(my_tmpfiles);

	my_tmpfiles = next;
    }
}

#ifndef HAVE_MKSTEMP
static int
my_mkstemp(char *temp)
{
    int fd;
    char *dname;
    char *fname;
    char *name;

    /*
     * Split-up to use tempnam, rather than tmpnam; the latter (like
     * mkstemp) is unusable on Windows.
     */
    if ((fname = strrchr(temp, '/')) != 0)
    {
	dname = strdup(temp);
	dname[++fname - temp] = '\0';
    }
    else
    {
	dname = 0;
	fname = temp;
    }
    if ((name = tempnam(dname, fname)) != 0)
    {
	fd = open(name, O_CREAT | O_EXCL | O_RDWR);
	strcpy(temp, name);
    }
    else
    {
	fd = -1;
    }

    if (dname != 0)
	free(dname);

    return fd;
}
#define mkstemp(s) my_mkstemp(s)
#endif

#endif

/*
 * tmpfile() should be adequate, except that it may require special privileges
 * to use, e.g., MinGW and Windows 7 where it tries to use the root directory.
 */
static FILE *
open_tmpfile(const char *label)
{
#define MY_FMT "%s/%.*sXXXXXX"
    FILE *result;
#if USE_MKSTEMP
    const char *tmpdir;
    char *name;

    if (((tmpdir = getenv("TMPDIR")) == NULL || access(tmpdir, W_OK) != 0) &&
	((tmpdir = getenv("TEMP")) == NULL || access(tmpdir, W_OK) != 0))
    {
#ifdef P_tmpdir
	tmpdir = P_tmpdir;
#else
	tmpdir = "/tmp";
#endif
	if (access(tmpdir, W_OK) != 0)
	    tmpdir = ".";
    }

    /* The size of the format is guaranteed to be longer than the result from
     * printing empty strings with it; this calculation accounts for the
     * string-lengths as well.
     */
    name = malloc(strlen(tmpdir) + sizeof(MY_FMT) + strlen(label));

    result = NULL;
    if (name != NULL)
    {
	int fd;
	const char *mark;

	mode_t save_umask = umask(0177);

	if ((mark = strrchr(label, '_')) == NULL)
	    mark = label + strlen(label);

	sprintf(name, MY_FMT, tmpdir, (int)(mark - label), label);
	fd = mkstemp(name);
	if (fd >= 0
	    && (result = fdopen(fd, "w+")) != NULL)
	{
	    MY_TMPFILES *item;

	    if (my_tmpfiles == NULL)
	    {
		atexit(close_tmpfiles);
	    }

	    item = NEW(MY_TMPFILES);
	    NO_SPACE(item);

	    item->name = name;
	    NO_SPACE(item->name);

	    item->next = my_tmpfiles;
	    my_tmpfiles = item;
	}
	else
	{
	    FREE(name);
	}
	(void)umask(save_umask);
    }
#else
    result = tmpfile();
#endif

    if (result == NULL)
	open_error(label);
    return result;
#undef MY_FMT
}

static void
open_files(void)
{
    create_file_names();

    if (input_file == NULL)
    {
	input_file = fopen(input_file_name, "r");
	if (input_file == NULL)
	    open_error(input_file_name);
    }

    action_file = open_tmpfile("action_file");
    text_file = open_tmpfile("text_file");

    if (vflag)
    {
	verbose_file = fopen(verbose_file_name, "w");
	if (verbose_file == NULL)
	    open_error(verbose_file_name);
    }

    if (gflag)
    {
	graph_file = fopen(graph_file_name, "w");
	if (graph_file == NULL)
	    open_error(graph_file_name);
	fprintf(graph_file, "digraph %s {\n", file_prefix);
	fprintf(graph_file, "\tedge [fontsize=10];\n");
	fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
	fprintf(graph_file, "\torientation=landscape;\n");
	fprintf(graph_file, "\trankdir=LR;\n");
	fprintf(graph_file, "\t/*\n");
	fprintf(graph_file, "\tmargin=0.2;\n");
	fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
	fprintf(graph_file, "\tratio=auto;\n");
	fprintf(graph_file, "\t*/\n");
    }

    if (dflag || dflag2)
    {
	defines_file = fopen(defines_file_name, "w");
	if (defines_file == NULL)
	    open_error(defines_file_name);
	union_file = open_tmpfile("union_file");
    }

    if (iflag)
    {
	externs_file = fopen(externs_file_name, "w");
	if (externs_file == NULL)
	    open_error(externs_file_name);
    }

    output_file = fopen(output_file_name, "w");
    if (output_file == NULL)
	open_error(output_file_name);

    if (rflag)
    {
	code_file = fopen(code_file_name, "w");
	if (code_file == NULL)
	    open_error(code_file_name);
    }
    else
	code_file = output_file;
}

int
main(int argc, char *argv[])
{
    SRexpect = -1;
    RRexpect = -1;
    exit_code = EXIT_SUCCESS;

    set_signals();
    getargs(argc, argv);
    open_files();
    reader();
    lr0();
    lalr();
    make_parser();
    graph();
    finalize_closure();
    verbose();
    output();
    done(exit_code);
    /*NOTREACHED */
}
