_nc_tracing = (1 << debug_level) - 1;
set_trace_level(debug_level);
main(int argc, char *argv[]){    char my_tmpname[PATH_MAX];    int v_opt = -1, debug_level;    int smart_defaults = TRUE;    char *termcap;    ENTRY *qp;    int this_opt, last_opt = '?';    int outform = F_TERMINFO;	/* output format */    int sortmode = S_TERMINFO;	/* sort_mode */    int fd;    int width = 60;    bool formatted = FALSE;	/* reformat complex strings? */    int numbers = 0;		/* format "%'char'" to/from "%{number}" */    bool infodump = FALSE;	/* running as captoinfo? */    bool capdump = FALSE;	/* running as infotocap? */    bool forceresolve = FALSE;	/* force resolution */    bool limited = TRUE;    char *tversion = (char *) NULL;    const char *source_file = "terminfo";    const char **namelst = 0;    char *outdir = (char *) NULL;    bool check_only = FALSE;    log_fp = stderr;    if ((_nc_progname = strrchr(argv[0], '/')) == NULL)	_nc_progname = argv[0];    else	_nc_progname++;    if ((infodump = (strcmp(_nc_progname, "captoinfo") == 0)) != FALSE) {	outform = F_TERMINFO;	sortmode = S_TERMINFO;    }    if ((capdump = (strcmp(_nc_progname, "infotocap") == 0)) != FALSE) {	outform = F_TERMCAP;	sortmode = S_TERMCAP;    }#if NCURSES_XNAMES    use_extended_names(FALSE);#endif    /*     * Processing arguments is a little complicated, since someone made a     * design decision to allow the numeric values for -w, -v options to     * be optional.     */    while ((this_opt = getopt(argc, argv,		"0123456789CILNR:TVce:fGgo:rsvwx")) != -1) {	if (isdigit(this_opt)) {	    switch (last_opt) {	    case 'v':		v_opt = (v_opt * 10) + (this_opt - '0');		break;	    case 'w':		width = (width * 10) + (this_opt - '0');		break;	    default:		if (this_opt != '1')		    usage();		last_opt = this_opt;		width = 0;	    }	    continue;	}	switch (this_opt) {	case 'C':	    capdump = TRUE;	    outform = F_TERMCAP;	    sortmode = S_TERMCAP;	    break;	case 'I':	    infodump = TRUE;	    outform = F_TERMINFO;	    sortmode = S_TERMINFO;	    break;	case 'L':	    infodump = TRUE;	    outform = F_VARIABLE;	    sortmode = S_VARIABLE;	    break;	case 'N':	    smart_defaults = FALSE;	    break;	case 'R':	    tversion = optarg;	    break;	case 'T':	    limited = FALSE;	    break;	case 'V':	    puts(NCURSES_VERSION);	    return EXIT_SUCCESS;	case 'c':	    check_only = TRUE;	    break;	case 'e':	    namelst = make_namelist(optarg);	    break;	case 'f':	    formatted = TRUE;	    break;	case 'G':	    numbers = 1;	    break;	case 'g':	    numbers = -1;	    break;	case 'o':	    outdir = optarg;	    break;	case 'r':	    forceresolve = TRUE;	    break;	case 's':	    showsummary = TRUE;	    break;	case 'v':	    v_opt = 0;	    break;	case 'w':	    width = 0;	    break;#if NCURSES_XNAMES	case 'x':	    use_extended_names(TRUE);	    break;#endif	default:	    usage();	}	last_opt = this_opt;    }    debug_level = (v_opt > 0) ? v_opt : (v_opt == 0);    _nc_tracing = (1 << debug_level) - 1;    if (_nc_tracing) {	save_check_termtype = _nc_check_termtype;	_nc_check_termtype = check_termtype;    }#ifndef HAVE_BIG_CORE    /*     * Aaargh! immedhook seriously hoses us!     *     * One problem with immedhook is it means we can't do -e.  Problem     * is that we can't guarantee that for each terminal listed, all the     * terminals it depends on will have been kept in core for reference     * resolution -- in fact it's certain the primitive types at the end     * of reference chains *won't* be in core unless they were explicitly     * in the select list themselves.     */    if (namelst && (!infodump && !capdump)) {	(void) fprintf(stderr,	    "Sorry, -e can't be used without -I or -C\n");	cleanup();	return EXIT_FAILURE;    }#endif /* HAVE_BIG_CORE */    if (optind < argc) {	source_file = argv[optind++];	if (optind < argc) {	    fprintf(stderr,		"%s: Too many file names.  Usage:\n\t%s %s",		_nc_progname,		_nc_progname,		usage_string);	    return EXIT_FAILURE;	}    } else {	if (infodump == TRUE) {	    /* captoinfo's no-argument case */	    source_file = "/usr/share/misc/termcap";	    if ((termcap = getenv("TERMCAP")) != 0		&& (namelst = make_namelist(getenv("TERM"))) != 0) {		if (access(termcap, F_OK) == 0) {		    /* file exists */		    source_file = termcap;		} else if (strcpy(my_tmpname, "/tmp/tic.XXXXXXXX")		    && (fd = mkstemp(my_tmpname)) != -1		    && (tmp_fp = fdopen(fd, "w")) != 0) {		    fprintf(tmp_fp, "%s\n", termcap);		    fclose(tmp_fp);		    tmp_fp = fopen(source_file, "r");		    to_remove = source_file;		} else {		    failed("mkstemp");		}	    }	} else {	    /* tic */	    fprintf(stderr,		"%s: File name needed.  Usage:\n\t%s %s",		_nc_progname,		_nc_progname,		usage_string);	    cleanup();	    return EXIT_FAILURE;	}    }    if (tmp_fp == 0	&& (tmp_fp = fopen(source_file, "r")) == 0) {	fprintf(stderr, "%s: Can't open %s\n", _nc_progname, source_file);	return EXIT_FAILURE;    }    if (infodump)	dump_init(tversion,	    smart_defaults	    ? outform	    : F_LITERAL,	    sortmode, width, debug_level, formatted);    else if (capdump)	dump_init(tversion,	    outform,	    sortmode, width, debug_level, FALSE);    /* parse entries out of the source file */    _nc_set_source(source_file);#ifndef HAVE_BIG_CORE    if (!(check_only || infodump || capdump))	_nc_set_writedir(outdir);#endif /* HAVE_BIG_CORE */    _nc_read_entry_source(tmp_fp, (char *) NULL,	!smart_defaults, FALSE,	(check_only || infodump || capdump) ? NULLHOOK : immedhook);    /* do use resolution */    if (check_only || (!infodump && !capdump) || forceresolve) {	if (!_nc_resolve_uses() && !check_only) {	    cleanup();	    return EXIT_FAILURE;	}    }    /* length check */    if (check_only && (capdump || infodump)) {	for_entry_list(qp) {	    if (matches(namelst, qp->tterm.term_names)) {		int len = fmt_entry(&qp->tterm, NULL, TRUE, infodump, numbers);		if (len > (infodump ? MAX_TERMINFO_LENGTH : MAX_TERMCAP_LENGTH))		    (void) fprintf(stderr,			"warning: resolved %s entry is %d bytes long\n",			_nc_first_name(qp->tterm.term_names),			len);	    }	}    }    /* write or dump all entries */    if (!check_only) {	if (!infodump && !capdump) {	    _nc_set_writedir(outdir);	    for_entry_list(qp)		if (matches(namelst, qp->tterm.term_names))		write_it(qp);	} else {	    /* this is in case infotocap() generates warnings */	    _nc_curr_col = _nc_curr_line = -1;	    for_entry_list(qp) {		if (matches(namelst, qp->tterm.term_names)) {		    int j = qp->cend - qp->cstart;		    int len = 0;		    /* this is in case infotocap() generates warnings */		    _nc_set_type(_nc_first_name(qp->tterm.term_names));		    (void) fseek(tmp_fp, qp->cstart, SEEK_SET);		    while (j--) {			if (infodump)			    (void) putchar(fgetc(tmp_fp));			else			    put_translate(fgetc(tmp_fp));		    }		    len = dump_entry(&qp->tterm, limited, numbers, NULL);		    for (j = 0; j < qp->nuses; j++)			len += dump_uses((char *) (qp->uses[j].parent), !capdump);		    (void) putchar('\n');		    if (debug_level != 0 && !limited)			printf("# length=%d\n", len);		}	    }	    if (!namelst) {		int c, oldc = '\0';		bool in_comment = FALSE;		bool trailing_comment = FALSE;		(void) fseek(tmp_fp, _nc_tail->cend, SEEK_SET);		while ((c = fgetc(tmp_fp)) != EOF) {		    if (oldc == '\n') {			if (c == '#') {			    trailing_comment = TRUE;			    in_comment = TRUE;			} else {			    in_comment = FALSE;			}		    }		    if (trailing_comment			&& (in_comment || (oldc == '\n' && c == '\n')))			putchar(c);		    oldc = c;		}	    }	}    }    /* Show the directory into which entries were written, and the total     * number of entries     */    if (showsummary	&& (!(check_only || infodump || capdump))) {	int total = _nc_tic_written();	if (total != 0)	    fprintf(log_fp, "%d entries written to %s\n",		total,		_nc_tic_dir((char *) 0));	else	    fprintf(log_fp, "No entries written\n");    }    cleanup();    return (EXIT_SUCCESS);}