<commit_before>/*
 * Copyright (c) 2000-2001, <NAME> & <NAME>
 */    
#include "bkd.h"
#include "logging.h"

private	struct {
	u32	doit:1;
	u32	verbose:1;
	u32	nospin:1;
	u32	textOnly:1;
	u32	autopull:1;
	int	list;
	u32	metaOnly:1;
	u32	forceInit:1;
	u32	debug:1;
	u32	gzip;
	u32	inBytes, outBytes;		/* stats */
	u32	lcsets;
	u32	rcsets;
	u32	rtags;
	FILE	*out;
} opts;

private	int	push(char **av, remote *r, char **envVar);
private	void	pull(remote *r);
private	void	listIt(sccs *s, int list);

int
push_main(int ac, char **av)
{
	int	c, rc, i = 1;
	int	try = -1; /* retry forever */
	char	**envVar = 0;
	remote 	*r;

	if (ac == 2 && streq("--help", av[1])) {
		system("bk help push");
		return (0);
	}
	bzero(&opts, sizeof(opts));
	opts.gzip = 6;
	opts.doit = opts.verbose = 1;

	while ((c = getopt(ac, av, "ac:deE:Gilno;qtz|")) != -1) {
		switch (c) {
		    case 'a': opts.autopull = 1; break;		/* doc 2.0 */
		    case 'c': try = atoi(optarg); break;	/* doc 2.0 */
		    case 'd': opts.debug = 1; break;		/* undoc 2.0 */
		    case 'e': opts.metaOnly = 1; break;		/* undoc 2.0 */
		    case 'E': 					/* doc 2.0 */
				envVar = addLine(envVar, strdup(optarg)); break;
		    case 'G': opts.nospin = 1; break;
		    case 'i': opts.forceInit = 1; break;	/* undoc? 2.0 */
		    case 'l': opts.list++; break;		/* doc 2.0 */
		    case 'n': opts.doit = 0; break;		/* doc 2.0 */
		    case 'q': opts.verbose = 0; break;		/* doc 2.0 */
		    case 'o': opts.out = fopen(optarg, "w"); break;
		    case 't': opts.textOnly = 1; break;		/* doc 2.0 */
		    case 'z':					/* doc 2.0 */
			opts.gzip = optarg ? atoi(optarg) : 6;
			if (opts.gzip < 0 || opts.gzip > 9) opts.gzip = 6;
			break;
		    default:
usage:			system("bk help -s push");
			return (1);
		}
	}
	unless (opts.out) opts.out = stderr;

	loadNetLib();
	has_proj("push");
	r = remote_parse(av[optind], 0);
	unless (r) goto usage;
	if (opts.debug) r->trace = 1;
	for (;;) {
		rc = push(av, r, envVar);
		if (rc != -2) break; /* -2 means locked */
		if (try == 0) break;
		if (bk_mode() == BK_BASIC) {
			if (try > 0) {
				fprintf(opts.out,
			    "push: retry request detected: %s", upgrade_msg);
			}
			break;
		}
		if (try != -1) --try;
		if (opts.verbose) {
			fprintf(opts.out,
				"push: remote locked, trying again...\n");
		}
		disconnect(r, 2); /* close fd before we retry */
		sleep(min((i++ * 2), 10)); /* auto back off */
	}
	if (rc == -2) rc = 1; /* if retry failed, reset exit code to 1 */
	remote_free(r);
	freeLines(envVar);
	if (opts.debug) {
		fprintf(opts.out, "lcsets=%d rcsets=%d rtags=%d\n",
		    opts.lcsets, opts.rcsets, opts.rtags);
	}
	if (!opts.metaOnly && (opts.rcsets || opts.rtags)) return (1);
	unless (opts.out == stderr) fclose(opts.out);
	return (rc);
}


int
log_main(int ac, char **av)
{
#define	MAXARG 10
	int	c, i, pflag = 0, qflag = 0, dflag = 0;
	char	retry[10] = "-c",  log_url[] = OPENLOG_URL;
	char	*log_av[MAXARG] = {"push", "-ie"};

	while ((c = getopt(ac, av, "dqpc:")) != -1) {
		switch (c) {
		    case 'c':	strcpy(&retry[2], optarg);
				assert(strlen(retry) < sizeof(retry));
				break;
		    case 'd':	dflag = 1; break;
		    case 'q':	qflag = 1; break;
		    case 'p':	pflag = 1; break;
		    default:	fprintf(opts.out,
				    "usage: bk log [-dqp] [-c count] [url]");
				return (1);
		}
	}

	if (sccs_cd2root(0, 0) == -1) {
		fprintf(opts.out, "Cannot find project root\n");
		return (1);
	}

	if (pflag) {
		printf("Number of open logs pending: %d\n",
							logs_pending(0, 0, 0));
		return (0);
	}

	

	/*
	 * WIN32 note:
	 * a) Win32 wish shell maps the console to a to a invisiable window,
	 *    messages printed to tty will be invisiable.
	 * b) We must close all of fd0, fd1 and fd2 to disconnect from
	 *    from wish.exe, they seem to be tied to the same console.
	 */
	if (qflag) {
		for (i = 0; i < 3; i++) close(i); 
		usleep(0); /* release cpu, so citool can exit */
		fopen(NULL_FILE, "rt"); /* stdin */
		fopen(DEV_NULL, "wt");	/* stdout */
		fopen(DEV_NULL, "wt");	/* stderr */<commit_msg>Fix segfault whenever 'bk log' wants to print an error message.<commit_after>/*
 * Copyright (c) 2000-2001, <NAME> & <NAME>
 */    
#include "bkd.h"
#include "logging.h"

private	struct {
	u32	doit:1;
	u32	verbose:1;
	u32	nospin:1;
	u32	textOnly:1;
	u32	autopull:1;
	int	list;
	u32	metaOnly:1;
	u32	forceInit:1;
	u32	debug:1;
	u32	gzip;
	u32	inBytes, outBytes;		/* stats */
	u32	lcsets;
	u32	rcsets;
	u32	rtags;
	FILE	*out;
} opts;

private	int	push(char **av, remote *r, char **envVar);
private	void	pull(remote *r);
private	void	listIt(sccs *s, int list);

int
push_main(int ac, char **av)
{
	int	c, rc, i = 1;
	int	try = -1; /* retry forever */
	char	**envVar = 0;
	remote 	*r;

	if (ac == 2 && streq("--help", av[1])) {
		system("bk help push");
		return (0);
	}
	bzero(&opts, sizeof(opts));
	opts.gzip = 6;
	opts.doit = opts.verbose = 1;
	opts.out = stderr;

	while ((c = getopt(ac, av, "ac:deE:Gilno;qtz|")) != -1) {
		switch (c) {
		    case 'a': opts.autopull = 1; break;		/* doc 2.0 */
		    case 'c': try = atoi(optarg); break;	/* doc 2.0 */
		    case 'd': opts.debug = 1; break;		/* undoc 2.0 */
		    case 'e': opts.metaOnly = 1; break;		/* undoc 2.0 */
		    case 'E': 					/* doc 2.0 */
				envVar = addLine(envVar, strdup(optarg)); break;
		    case 'G': opts.nospin = 1; break;
		    case 'i': opts.forceInit = 1; break;	/* undoc? 2.0 */
		    case 'l': opts.list++; break;		/* doc 2.0 */
		    case 'n': opts.doit = 0; break;		/* doc 2.0 */
		    case 'q': opts.verbose = 0; break;		/* doc 2.0 */
		    case 'o': opts.out = fopen(optarg, "w"); break;
		    case 't': opts.textOnly = 1; break;		/* doc 2.0 */
		    case 'z':					/* doc 2.0 */
			opts.gzip = optarg ? atoi(optarg) : 6;
			if (opts.gzip < 0 || opts.gzip > 9) opts.gzip = 6;
			break;
		    default:
usage:			system("bk help -s push");
			return (1);
		}
	}

	loadNetLib();
	has_proj("push");
	r = remote_parse(av[optind], 0);
	unless (r) goto usage;
	if (opts.debug) r->trace = 1;
	for (;;) {
		rc = push(av, r, envVar);
		if (rc != -2) break; /* -2 means locked */
		if (try == 0) break;
		if (bk_mode() == BK_BASIC) {
			if (try > 0) {
				fprintf(opts.out,
			    "push: retry request detected: %s", upgrade_msg);
			}
			break;
		}
		if (try != -1) --try;
		if (opts.verbose) {
			fprintf(opts.out,
				"push: remote locked, trying again...\n");
		}
		disconnect(r, 2); /* close fd before we retry */
		sleep(min((i++ * 2), 10)); /* auto back off */
	}
	if (rc == -2) rc = 1; /* if retry failed, reset exit code to 1 */
	remote_free(r);
	freeLines(envVar);
	if (opts.debug) {
		fprintf(opts.out, "lcsets=%d rcsets=%d rtags=%d\n",
		    opts.lcsets, opts.rcsets, opts.rtags);
	}
	if (!opts.metaOnly && (opts.rcsets || opts.rtags)) return (1);
	unless (opts.out == stderr) fclose(opts.out);
	return (rc);
}


int
log_main(int ac, char **av)
{
#define	MAXARG 10
	int	c, i, pflag = 0, qflag = 0, dflag = 0;
	char	retry[10] = "-c",  log_url[] = OPENLOG_URL;
	char	*log_av[MAXARG] = {"push", "-ie"};

	opts.out = stderr;
	while ((c = getopt(ac, av, "dqpc:")) != -1) {
		switch (c) {
		    case 'c':	strcpy(&retry[2], optarg);
				assert(strlen(retry) < sizeof(retry));
				break;
		    case 'd':	dflag = 1; break;
		    case 'q':	qflag = 1; break;
		    case 'p':	pflag = 1; break;
		    default:	fprintf(opts.out,
				    "usage: bk log [-dqp] [-c count] [url]");
				return (1);
		}
	}

	if (sccs_cd2root(0, 0) == -1) {
		fprintf(opts.out, "Cannot find project root\n");
		return (1);
	}

	if (pflag) {
		printf("Number of open logs pending: %d\n",
							logs_pending(0, 0, 0));
		return (0);
	}
       
	/*
	 * WIN32 note:
	 * a) Win32 wish shell maps the console to a to a invisiable window,
	 *    messages printed to tty will be invisiable.
	 * b) We must close all of fd0, fd1 and fd2 to disconnect from
	 *    from wish.exe, they seem to be tied to the same console.
	 */
	if (qflag) {
		for (i = 0; i < 3; i++) close(i); 
		usleep(0); /* release cpu, so citool can exit */
		fopen(NULL_FILE, "rt"); /* stdin */
		fopen(DEV_NULL, "wt");	/* stdout */
		fopen(DEV_NULL, "wt");	/* stderr */
{'fraction_non_alphanumeric': 0.1928112648221344, 'fraction_numerical': 0.03285573122529644, 'mean_word_length': 2.644014401440144, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1614492', 'n_tokens_mistral': 3838, 'n_tokens_neox': 3405, 'n_words': 978}