Ejemplo n.º 1
0
Archivo: init.c Proyecto: sbenning42/42
void				sh_init(t_sh *sh, int ac, char **av)
{
	int				i;

	sh->bin = NULL;
	if ((sh->o = ft_getopt(ac, av, SH_OPTION)) < 0)
		exit(EXIT_FAILURE);
	if (bin_load(&sh->bin) < 0)
		ft_error("loading binary... Fail");
	if (hist_load(&sh->hist) < 0)
		ft_error("loading history... Fail");
	if (ISO(sh->o, SH_O_PROMPT))
	{
		i = 0;
		while (++i < ac)
		{
			if (av[i][0] != '-')
				break ;
		}
		sh->prompt = av[i];
	}
	else
		sh->prompt = SH_DEFAULT_PROMPT;
	sh->settings = SH_DEFAULT_SETTINGS;
	sh->ac = ac;
	sh->av = av;
}
Ejemplo n.º 2
0
void mupip_cvtgbl(void)
{
	unsigned short	fn_len, len;
	char		fn[256];
	unsigned char	buff[7];
	uint4		begin, end;
	int		i, format;
	uint4	        cli_status;
	gtm_int64_t	begin_i8, end_i8;
	DCL_THREADGBL_ACCESS;

	SETUP_THREADGBL_ACCESS;
	/* If an online rollback occurs when we are loading up the database with new globals and takes us back to a prior logical
	 * state, then we should not continue with the load. The reason being that the application might rely on certain globals to
	 * be present before loading others and that property could be voilated if online rollback takes the database back to a
	 * completely different logical state. Set the variable issue_DBROLLEDBACK_anyways that forces the restart logic to issue
	 * an rts_error the first time it detects an online rollback (that takes the database to a prior logical state).
	 */
	TREF(issue_DBROLLEDBACK_anyways) = TRUE;
	is_replicator = TRUE;
	skip_dbtriggers = TRUE;
	fn_len = SIZEOF(fn);
	if (cli_present("STDIN"))
	{
		/* User wants to load from standard input */
		assert(SIZEOF(fn) > sys_input.len);
		memcpy(fn, sys_input.addr, sys_input.len);
		fn_len = sys_input.len;
		assert(-1 != fcntl(fileno(stdin), F_GETFD));
		/* Check if both file name and -STDIN specified. */
		if (cli_get_str("FILE", fn, &fn_len))
			mupip_exit(ERR_MUPCLIERR);
	} else if (!cli_get_str("FILE", fn, &fn_len))  /* User wants to read from a file. */
		mupip_exit(ERR_MUPCLIERR); /* Neither -STDIN nor file name specified. */
	if (mupip_error_occurred)
		exit(-1);
	file_input_init(fn, fn_len);
	mu_outofband_setup();
	if ((cli_status = cli_present("BEGIN")) == CLI_PRESENT)
	{
	        if (!cli_get_int64("BEGIN", &begin_i8))
			mupip_exit(ERR_MUPCLIERR);
		if (1 > begin_i8)
			mupip_exit(ERR_LOADBGSZ);
		else if (MAXUINT4 < begin_i8)
			mupip_exit(ERR_LOADBGSZ2);
		begin = (uint4) begin_i8;
	} else
	{
		begin = 1;
		begin_i8 = 1;
	}
	if ((cli_status = cli_present("END")) == CLI_PRESENT)
	{
	        if (!cli_get_int64("END", &end_i8))
			mupip_exit(ERR_MUPCLIERR);
		if (1 > end_i8)
			mupip_exit(ERR_LOADEDSZ);
		else if (MAXUINT4 < end_i8)
			mupip_exit(ERR_LOADEDSZ2);
		if (end_i8 < begin_i8)
			mupip_exit(ERR_LOADEDBG);
		end = (uint4) end_i8;
	} else
		end = MAXUINT4;
	if ((cli_status = cli_present("FILL_FACTOR")) == CLI_PRESENT)
	{
		assert(SIZEOF(gv_fillfactor) == SIZEOF(int4));
	        if (!cli_get_int("FILL_FACTOR", (int4 *)&gv_fillfactor))
			gv_fillfactor = MAX_FILLFACTOR;
		if (gv_fillfactor < MIN_FILLFACTOR)
			gv_fillfactor = MIN_FILLFACTOR;
		else if (gv_fillfactor > MAX_FILLFACTOR)
			gv_fillfactor = MAX_FILLFACTOR;
	} else
		gv_fillfactor = MAX_FILLFACTOR;

	if (cli_present("FORMAT") == CLI_PRESENT)
	{
	        len = SIZEOF("FORMAT");
		if (!cli_get_str("FORMAT", (char *)buff, &len))
			go_load(begin, end);
		else
		{
		        lower_to_upper(buff, buff, len);
			if (!memcmp(buff, "ZWR", len))
				go_load(begin, end);
			else if (!memcmp(buff, "BINARY", len))
				bin_load(begin, end);
			else if (!memcmp(buff, "GO", len))
				go_load(begin, end);
			else if (!memcmp(buff, "GOQ", len))
				goq_load();
			else
			{
			        util_out_print("Illegal format for load",TRUE);
				mupip_exit(ERR_MUPCLIERR);
			}
		}
	} else
		go_load(begin, end);

	if (mupip_error_occurred)
	{
	        util_out_print("Error occurred during loading",TRUE);
		exit(-1);
	}
	else
		mupip_exit(SS_NORMAL);
}
Ejemplo n.º 3
0
void mupip_cvtgbl(void)
{
	char		fn[MAX_FN_LEN + 1], *line1_ptr, *line3_ptr;
	gtm_int64_t	begin_i8, end_i8;
	int		dos, i, file_format, line1_len, line3_len, utf8;
	uint4	        begin, cli_status, end, max_rec_size;
	unsigned char	buff[MAX_ONERROR_VALUE_LEN];
	unsigned short	fn_len, len;

	DCL_THREADGBL_ACCESS;
	SETUP_THREADGBL_ACCESS;
	assert(MAX_ONERROR_VALUE_LEN > MAX_FORMAT_VALUE_LEN);	/* so the buff[] definition above is good for FORMAT and ONERROR */
	/* If an online rollback occurs when we are loading up the database with new globals and takes us back to a prior logical
	 * state, then we should not continue with the load. The reason being that the application might rely on certain globals to
	 * be present before loading others and that property could be violated if online rollback takes the database back to a
	 * completely different logical state. Set the variable issue_DBROLLEDBACK_anyways that forces the restart logic to issue
	 * an rts_error the first time it detects an online rollback (that takes the database to a prior logical state).
	 */
	TREF(issue_DBROLLEDBACK_anyways) = TRUE;
	is_replicator = TRUE;
	skip_dbtriggers = TRUE;
	fn_len = SIZEOF(fn);
	if (cli_present("STDIN"))
	{
		/* User wants to load from standard input */
		assert(SIZEOF(fn) > sys_input.len);
		memcpy(fn, sys_input.addr, sys_input.len);
		fn_len = sys_input.len;
		assert(-1 != fcntl(fileno(stdin), F_GETFD));
		/* Check if both file name and -STDIN specified. */
		if (cli_get_str("FILE", fn, &fn_len))
			mupip_exit(ERR_MUPCLIERR);
	} else if (!cli_get_str("FILE", fn, &fn_len))  /* User wants to read from a file. */
		mupip_exit(ERR_MUPCLIERR); /* Neither -STDIN nor file name specified. */
	file_input_init(fn, fn_len, IOP_EOL);
	if (mupip_error_occurred)
		EXIT(-1);
	mu_outofband_setup();
	if ((cli_status = cli_present("BEGIN")) == CLI_PRESENT)
	{
	        if (!cli_get_int64("BEGIN", &begin_i8))
			mupip_exit(ERR_MUPCLIERR);
		if (1 > begin_i8)
			mupip_exit(ERR_LOADBGSZ);
		else if (MAXUINT4 < begin_i8)
			mupip_exit(ERR_LOADBGSZ2);
		begin = (uint4) begin_i8;
	} else
	{
		begin = 1;
		begin_i8 = 1;
	}
	if ((cli_status = cli_present("END")) == CLI_PRESENT)
	{
	        if (!cli_get_int64("END", &end_i8))
			mupip_exit(ERR_MUPCLIERR);
		if (1 > end_i8)
			mupip_exit(ERR_LOADEDSZ);
		else if (MAXUINT4 < end_i8)
			mupip_exit(ERR_LOADEDSZ2);
		if (end_i8 < begin_i8)
			mupip_exit(ERR_LOADEDBG);
		end = (uint4) end_i8;
	} else
		end = MAXUINT4;
	if ((cli_status = cli_present("FILL_FACTOR")) == CLI_PRESENT)
	{
		assert(SIZEOF(gv_fillfactor) == SIZEOF(int4));
	        if (!cli_get_int("FILL_FACTOR", (int4 *)&gv_fillfactor))
			gv_fillfactor = MAX_FILLFACTOR;
		if (gv_fillfactor < MIN_FILLFACTOR)
			gv_fillfactor = MIN_FILLFACTOR;
		else if (gv_fillfactor > MAX_FILLFACTOR)
			gv_fillfactor = MAX_FILLFACTOR;
	} else
		gv_fillfactor = MAX_FILLFACTOR;
	if (cli_present("ONERROR") == CLI_PRESENT)
	{
		len = SIZEOF(buff);
		if (!cli_get_str("ONERROR", (char *)buff, &len))
		{
			assert(FALSE);
			onerror = ONERROR_PROCEED;
		} else
		{
			lower_to_upper(buff, buff, len);
			if (!memcmp(buff, "STOP", len))
				onerror = ONERROR_STOP;
			else if (!memcmp(buff, "PROCEED", len))
				onerror = ONERROR_PROCEED;
			else if (!memcmp(buff, "INTERACTIVE", len))
			{
				if (isatty(0)) /*if stdin is a terminal*/
					onerror = ONERROR_INTERACTIVE;
				else
					onerror = ONERROR_STOP;
			} else
			{
				util_out_print("Illegal ONERROR parameter for load",TRUE);
				mupip_exit(ERR_MUPCLIERR);
			}
		}
	} else
		onerror = ONERROR_PROCEED; /* Default: Proceed on error */
	file_format = get_load_format(&line1_ptr, &line3_ptr, &line1_len, &line3_len, &max_rec_size, &utf8, &dos); /* from header */
	if (MU_FMT_GOQ == file_format)
		mupip_exit(ERR_LDBINFMT);
	if (BADZCHSET == utf8)
		mupip_exit(ERR_MUNOFINISH);
	if (cli_present("FORMAT") == CLI_PRESENT)
	{	/* If the command speficies a format see if it matches the label */
		len = SIZEOF(buff);
		if (!cli_get_str("FORMAT", (char *)buff, &len))
			go_load(begin, end, (unsigned char *)line1_ptr, line3_ptr, line3_len, max_rec_size, file_format, utf8, dos);
		else
		{
		        lower_to_upper(buff, buff, len);
			if (!memcmp(buff, "ZWR", len))
			{	/* If the label did not determine a format let them specify ZWR and they can sort out the result */
				if ((MU_FMT_ZWR == file_format) || (MU_FMT_UNRECOG == file_format))
					go_load(begin, end, (unsigned char *)line1_ptr, line3_ptr, line3_len, max_rec_size,
						MU_FMT_ZWR, utf8, dos);
				else
					mupip_exit(ERR_LDBINFMT);
			} else if (!memcmp(buff, "BINARY", len))
			{
				if (MU_FMT_BINARY == file_format)
					bin_load(begin, end, line1_ptr, line1_len);
				else
					mupip_exit(ERR_LDBINFMT);
			} else if (!memcmp(buff, "GO", len))
			{	/* If the label did not determine a format let them specify GO and they can sort out the result */
				if ((MU_FMT_GO == file_format) || (MU_FMT_UNRECOG == file_format))
					go_load(begin, end, (unsigned char *)line1_ptr, line3_ptr, line3_len, max_rec_size,
						MU_FMT_GO, utf8, dos);
				else
					mupip_exit(ERR_LDBINFMT);
			} else if (!memcmp(buff, "GOQ", len))
			{	/* get_load_format doesn't recognize GOQ labels' */
				if (MU_FMT_UNRECOG == file_format)
					goq_load();
				else
					mupip_exit(ERR_LDBINFMT);
			} else
			{
					util_out_print("Illegal file format for load",TRUE);
					mupip_exit(ERR_MUPCLIERR);
			}
		}
	} else
	{
		if (MU_FMT_BINARY == file_format)
			bin_load(begin, end, line1_ptr, line1_len);
		else if ((MU_FMT_ZWR == file_format) || (MU_FMT_GO == file_format))
			go_load(begin, end, (unsigned char *)line1_ptr, line3_ptr, line3_len, max_rec_size, file_format, utf8, dos);
		else
		{
			assert(MU_FMT_UNRECOG == file_format);
			mupip_exit(ERR_LDBINFMT);
		}
	}
	mupip_exit(mupip_error_occurred ? ERR_MUNOFINISH : SS_NORMAL);
}
Ejemplo n.º 4
0
Archivo: bast.c Proyecto: ec429/bast
int main(int argc, char *argv[])
{
	/* SET UP TABLES ETC */
	mktoktbl();
	/* END: SET UP TABLES ETC */
	
	/* PARSE ARGUMENTS */
	int ninbas=0;
	char **inbas=NULL;
	int ninobj=0;
	char **inobj=NULL;
	enum {NONE, OBJ, TAPE} outtype=NONE;
	char *outfile=NULL;
	bool emu=false;
	int arg;
	int state=0;
	for(arg=1;arg<argc;arg++)
	{
		char *varg=argv[arg];
		if(strcmp(varg, "-")==0)
			varg="/dev/stdin";
		if(*varg=='-')
		{
			if(strcmp(varg, "-V")==0)
			{
				printf(VERSION_MSG);
				return(EXIT_SUCCESS);
			}
			else if(strcmp(varg, "--emu")==0)
			{
				emu=true;
			}
			else if(strcmp(varg, "--no-emu")==0)
			{
				emu=false;
			}
			else if(strcmp(varg, "--debug")==0)
			{
				debug=true;
			}
			else if(strcmp(varg, "--no-debug")==0)
			{
				debug=false;
			}
			else if(strcmp(varg, "-b")==0)
				state=1;
			else if(strcmp(varg, "-l")==0)
				state=7;
			else if(strcmp(varg, "-t")==0)
				state=2;
			else if(strcmp(varg, "-W")==0)
				state=3;
			else if(strcmp(varg, "-W-")==0)
				state=4;
			else if(strcmp(varg, "-O")==0)
				state=5;
			else if(strcmp(varg, "-O-")==0)
				state=6;
			else
			{
				fprintf(stderr, "bast: No such option %s\n", varg);
				return(EXIT_FAILURE);
			}
		}
		else
		{
			bool flag=false;
			switch(state)
			{
				case 0:
				case 1:
					if(addinbas(&ninbas, &inbas, varg))
					{
						fprintf(stderr, "bast: Internal error: Failed to add %s to inbas list\n", varg);
						return(EXIT_FAILURE);
					}
					state=0;
				break;
				case 7:
					if(addinbas(&ninobj, &inobj, varg))
					{
						fprintf(stderr, "bast: Internal error: Failed to add %s to inobj list\n", varg);
						return(EXIT_FAILURE);
					}
					state=0;
				break;
				case 2:
					outtype=TAPE;
					outfile=strdup(varg);
					state=0;
				break;
				case 3:
					flag=true; // fallthrough
				case 4:
					if(strcmp("all", varg)==0)
					{
						Wobjlen=flag;
						state=0;
					}
					else if(strcmp("object-length", varg)==0)
					{
						Wobjlen=flag;
						state=0;
					}
					else if(strcmp("object-checksum", varg)==0)
					{
						Wobjsum=flag;
						state=0;
					}
					else if(strcmp("se-basic", varg)==0)
					{
						Wsebasic=flag;
						state=0;
					}
					else if(strcmp("embedded-newline", varg)==0)
					{
						Wembeddednewline=flag;
						state=0;
					}
				break;
				case 5:
					flag=true; // fallthrough
				case 6:
					if(strcmp("cut-numbers", varg)==0)
					{
						Ocutnumbers=flag;
						state=0;
					}
				break;
				default:
					fprintf(stderr, "bast: Internal error: Bad state %u in args\n", state);
					return(EXIT_FAILURE);
				break;
			}
		}
	}
	
	/* END: PARSE ARGUMENTS */
	
	if(!(ninbas||ninobj))
	{
		fprintf(stderr, "bast: No input files specified\n");
		return(EXIT_FAILURE);
	}
	
	if((outtype==NONE)||!outfile)
	{
		fprintf(stderr, "bast: No output file specified\n");
		return(EXIT_FAILURE);
	}
	
	int nsegs=0;
	segment * data=NULL;
	
	/* READ BASIC FILES */
	
	if(ninbas&&!inbas)
	{
		fprintf(stderr, "bast: Internal error: ninbas!=0 and inbas is NULL\n");
		return(EXIT_FAILURE);
	}
	
	int fbas;
	for(fbas=0;fbas<ninbas;fbas++)
	{
		int fline=0;
		int dfl=0;
		FILE *fp=fopen(inbas[fbas], "r");
		if(!fp)
		{
			fprintf(stderr, "bast: Failed to open input file %s\n", inbas[fbas]);
			return(EXIT_FAILURE);
		}
		segment *curr=addsegment(&nsegs, &data);
		if(!curr)
		{
			fprintf(stderr, "bast: Internal error: failed to add segment for file %s\n", inbas[fbas]);
			return(EXIT_FAILURE);
		}
		curr->name=(char *)malloc(10);
		sprintf(curr->name, "bas%u", fbas);
		curr->type=BASIC;
		curr->data.bas.nlines=0;
		curr->data.bas.basic=NULL;
		curr->data.bas.line=0;
		curr->data.bas.lline=NULL;
		curr->data.bas.renum=0;
		curr->data.bas.block=NULL;
		while(!feof(fp))
		{
			char *line=fgetl(fp);
			if(line)
			{
				fline+=dfl+1;
				dfl=0;
				if(*line)
				{
					while(line[strlen(line)-1]=='\\') // line splicing
					{
						char *second=fgetl(fp);
						if(!second) continue;
						dfl++;
						if(!*second)
						{
							free(second);
							continue;
						}
						line[strlen(line)-1]=0;
						char *splice=(char *)realloc(line, strlen(line)+strlen(second)+2);
						if(!splice)
						{
							free(second);
							continue;
						}
						line=splice;
						strcat(splice, second);
						free(second);
					}
					if(Wembeddednewline && strchr(line, '\x0D')) // 0x0D is newline in ZX BASIC
					{
						fprintf(stderr, "bast: Warning: embedded newline (\\0D) in ZX Basic line\n\t"LOC"\n", LOCARG);
					}
					if(addbasline(&curr->data.bas.nlines, &curr->data.bas.basic, line))
					{
						fprintf(stderr, "bast: Internal error: Failed to store line as text\n\t"LOC"\n", LOCARG);
						return(EXIT_FAILURE);
					}
					curr->data.bas.basic[curr->data.bas.nlines-1].sline=fline;
					if(*line=='#')
					{
						char *cmd=strtok(line, " ");
						if(cmd)
						{
							if(strcmp(cmd, "#pragma")==0)
							{
								char *prgm=strtok(NULL, " ");
								if(prgm)
								{
									if(strcmp(prgm, "name")==0)
									{
										char *basname=strtok(NULL, "");
										if(basname)
										{
											if(curr->name) free(curr->name);
											curr->name=strdup(basname);
										}
									}
									else if(strcmp(prgm, "line")==0)
									{
										char *pline=strtok(NULL, "");
										if(pline)
										{
											unsigned int val;
											if(sscanf(pline, "%u", &val)==1)
											{
												curr->data.bas.line=val;
											}
											else
											{
												curr->data.bas.line=-1;
												curr->data.bas.lline=strdup(pline);
											}
										}
										else
										{
											fprintf(stderr, "bast: Warning: #pragma line missing argument\n\t"LOC"\n", LOCARG);
										}
									}
									else if(strcmp(prgm, "renum")==0)
									{
										curr->data.bas.renum=1;
										curr->data.bas.rnstart=0;
										curr->data.bas.rnoffset=0;
										curr->data.bas.rnend=0;
										char *arg=strtok(NULL, " ");
										while(arg)
										{
											unsigned int val=0;
											if(*arg)
												sscanf(arg+1, "%u", &val);
											switch(*arg)
											{
												case '=':
													curr->data.bas.rnstart=val;
												break;
												case '+':
													curr->data.bas.rnoffset=val;
												break;
												case '-':
													curr->data.bas.rnend=val;
												break;
												default:
													fprintf(stderr, "bast: Warning: #pragma renum bad argument %s\n\t"LOC"\n", arg, LOCARG);
												break;
											}
											arg=strtok(NULL, " ");
										}
									}
									else
									{
										fprintf(stderr, "bast: Warning: #pragma %s not recognised (ignoring)\n\t"LOC"\n", prgm, LOCARG);
									}
								}
								else
								{
									fprintf(stderr, "bast: #pragma without identifier\n\t"LOC"\n", LOCARG);
									return(EXIT_FAILURE);
								}
							}
							else if(strcmp(cmd, "##")==0)
							{
								// comment, ignore
							}
							else
							{
								fprintf(stderr, "bast: Unrecognised directive %s\n\t"LOC"\n", cmd, LOCARG);
								return(EXIT_FAILURE);
							}
						}
					}
				}
				free(line);
			}
		}
		fprintf(stderr, "bast: BASIC segment '%s', read %u physical lines\n", curr->name, curr->data.bas.nlines);
	}
	
	/* END: READ BASIC FILES */
	
	/* READ OBJECT FILES */
	int fobj;
	for(fobj=0;fobj<ninobj;fobj++)
	{
		FILE *fp=fopen(inobj[fobj], "r");
		if(!fp)
		{
			fprintf(stderr, "bast: Failed to open input file %s\n", inobj[fobj]);
			return(EXIT_FAILURE);
		}
		segment *curr=addsegment(&nsegs, &data);
		if(!curr)
		{
			fprintf(stderr, "bast: Internal error: failed to add segment for file %s\n", inobj[fobj]);
			return(EXIT_FAILURE);
		}
		curr->name=(char *)malloc(10);
		sprintf(curr->name, "bin%u", fobj);
		curr->type=BINARY;
		err=false;
		bin_load(inobj[fobj], fp, &curr->data.bin, &curr->name);
		if(err)
		{
			fprintf(stderr, "bast: Failed to load BINARY segment from file %s\n", inobj[fobj]);
			return(EXIT_FAILURE);
		}
	}
	/* END: READ OBJECT FILES */
	
	/* TODO: fork the assembler for each #[r]asm/#endasm block */
	
	/* TOKENISE BASIC SEGMENTS */
	if(ninbas)
	{
		int i;
		for(i=0;i<nsegs;i++)
		{
			if(data[i].type==BASIC)
			{
				data[i].data.bas.blines=0;
				fprintf(stderr, "bast: Tokenising BASIC segment %s\n", data[i].name);
				int j;
				for(j=0;j<data[i].data.bas.nlines;j++)
				{
					err=false;
					if(debug) fprintf(stderr, "bast: tokenising line %s\n", data[i].data.bas.basic[j].text);
					tokenise(&data[i].data.bas.basic[j], inbas, i, data[i].data.bas.renum);
					if(data[i].data.bas.basic[j].ntok) data[i].data.bas.blines++;
					if(err) return(EXIT_FAILURE);
				}
				fprintf(stderr, "bast: Tokenised BASIC segment %s (%u logical lines)\n", data[i].name, data[i].data.bas.blines);
			}
		}
	}
	/* END: TOKENISE BASIC SEGMENTS */
	
	/* LINKER & LABELS */
	// PASS 1: Find labels, renumber labelled BASIC sources, load in !links as attached bin_segs
	int nlabels=0;
	label * labels=NULL;
	int i;
	for(i=0;i<nsegs;i++)
	{
		fprintf(stderr, "bast: Linker (Pass 1): %s\n", data[i].name);
		switch(data[i].type)
		{
			case BASIC:;
				int num=0,dnum=0;
				if(data[i].data.bas.renum==1)
				{
					dnum=data[i].data.bas.rnoffset?data[i].data.bas.rnoffset:10;
					int end=data[i].data.bas.rnend?data[i].data.bas.rnend:9999;
					while(data[i].data.bas.blines*dnum>end)
					{
						dnum--;
						if((dnum==7)||(dnum==9))
							dnum--;
					}
					if(!dnum)
					{
						fprintf(stderr, "bast: Renumber: Couldn't fit %s into available lines\n", data[i].name);
						return(EXIT_FAILURE);
					}
					num=data[i].data.bas.rnstart?data[i].data.bas.rnstart:dnum;
					fprintf(stderr, "bast: Renumber: BASIC segment %s, start %u, spacing %u, end <=%u\n", data[i].name, num, dnum, end);
				}
				int dl;
				init_char(&data[i].data.bas.block, &dl, (int *)&data[i].data.bas.blen);
				int last=0;
				int j;
				for(j=0;j<data[i].data.bas.nlines;j++)
				{
					if(data[i].data.bas.basic[j].ntok)
					{
						if(num)
						{
							if(data[i].data.bas.renum!=1)
							{
								fprintf(stderr, "bast: Linker (Pass 1): Internal error (num!=0 but renum!=1), %s\n", data[i].name);
								return(EXIT_FAILURE);
							}
							data[i].data.bas.basic[j].number=num;
							num+=dnum;
						}
						else
						{
							if(data[i].data.bas.renum)
							{
								fprintf(stderr, "bast: Linker (Pass 1): Internal error (num==0 but renum!=0), %s\n", data[i].name);
								return(EXIT_FAILURE);
							}
							while(last<nlabels)
							{
								labels[last].sline=j;
								labels[last++].line=data[i].data.bas.basic[j].number;
							}
						}
						int k;
						for(k=0;k<data[i].data.bas.basic[j].ntok;k++)
						{
							if(data[i].data.bas.basic[j].tok[k].tok==TOKEN_RLINK)
							{
								if(data[i].data.bas.basic[j].tok[k].data)
								{
									FILE *fp=fopen(data[i].data.bas.basic[j].tok[k].data, "rb");
									if(fp)
									{
										data[i].data.bas.basic[j].tok[k].data2=(char *)malloc(sizeof(bin_seg));
										err=false;
										bin_load(data[i].data.bas.basic[j].tok[k].data, fp, (bin_seg *)data[i].data.bas.basic[j].tok[k].data2, NULL);
										if(err)
										{
											fprintf(stderr, "bast: Linker: failed to attach BINARY segment\n\t%s:%u\n", data[i].name, j);
											return(EXIT_FAILURE);
										}
									}
									else
									{
										fprintf(stderr, "bast: Linker: failed to open rlinked file %s\n\t%s:%u\n", data[i].data.bas.basic[j].tok[k].data, data[i].name, j);
										return(EXIT_FAILURE);
									}
								}
								else
								{
									fprintf(stderr, "bast: Linker: Internal error: TOKEN_RLINK without filename\n\t%s:%u", data[i].name, j);
									return(EXIT_FAILURE);
								}
							}
						}
					}
					else if(*data[i].data.bas.basic[j].text=='.')
					{
						if(isvalidlabel(data[i].data.bas.basic[j].text+1))
						{
							label lbl;
							lbl.text=strdup(data[i].data.bas.basic[j].text+1);
							lbl.seg=i;
							lbl.line=num;
							lbl.sline=j;
							addlabel(&nlabels, &labels, lbl);
						}
					}
				}
				buildbas(&data[i].data.bas, false);
				if(data[i].data.bas.blen==-1)
				{
					fprintf(stderr, "bast: Failed to link BASIC segment %s\n", data[i].name);
					return(EXIT_FAILURE);
				}
				if(data[i].data.bas.renum) data[i].data.bas.renum=2;
			break;
			case BINARY:
				// TODO: export symbol table (we don't have symbols in object files yet)
				// Nothing else on pass 1
			break;
			default:
				fprintf(stderr, "bast: Linker: Internal error: Bad segment-type %u\n", data[i].type);
				return(EXIT_FAILURE);
			break;
		}
	}
	// PASS 2: Replace labels with the linenumbers/addresses to which they point
	for(i=0;i<nsegs;i++)
	{
		fprintf(stderr, "bast: Linker (Pass 2): %s\n", data[i].name);
		switch(data[i].type)
		{
			case BASIC:
				if(data[i].data.bas.line<0)
				{
					if(!data[i].data.bas.lline)
					{
						fprintf(stderr, "bast: Linker: Internal error: line<0 but lline=NULL, %s\n", data[i].name);
						return(EXIT_FAILURE);
					}
					int l;
					for(l=0;l<nlabels;l++)
					{
						// TODO limit label scope to this file & the files it has #imported
						if((data[labels[l].seg].type==BASIC) && (strcmp(data[i].data.bas.lline, labels[l].text)==0))
						{
							data[i].data.bas.line=labels[l].line;
							break;
						}
					}
					if(l==nlabels)
					{
						fprintf(stderr, "bast: Linker: Undefined label %s\n\t%s:#pragma line\n", data[i].data.bas.lline, data[i].name);
						return(EXIT_FAILURE);
					}
				}
				int j;
				for(j=0;j<data[i].data.bas.nlines;j++)
				{
					int k;
					for(k=0;k<data[i].data.bas.basic[j].ntok;k++)
					{
						if(data[i].data.bas.basic[j].tok[k].tok==TOKEN_LABEL)
						{
							int l;
							for(l=0;l<nlabels;l++)
							{
								// TODO limit label scope to this file & the files it has #imported
								if((data[labels[l].seg].type==BASIC) && (strcmp(data[i].data.bas.basic[j].tok[k].data, labels[l].text)==0))
								{
									if(debug) fprintf(stderr, "bast: Linker: expanded %%%s", data[i].data.bas.basic[j].tok[k].data);
									if(data[i].data.bas.basic[j].tok[k].index)
									{
										if(debug) fprintf(stderr, "%s%02x", data[i].data.bas.basic[j].tok[k].index>0?"+":"-", abs(data[i].data.bas.basic[j].tok[k].index));
									}
									data[i].data.bas.basic[j].tok[k].tok=TOKEN_ZXFLOAT;
									if(Ocutnumbers)
									{
										data[i].data.bas.basic[j].tok[k].data=strdup(".");
										if(debug) fprintf(stderr, " to %u (cut)\n", labels[l].line+data[i].data.bas.basic[j].tok[k].index);
									}
									else
									{
										data[i].data.bas.basic[j].tok[k].data=(char *)malloc(6);
										sprintf(data[i].data.bas.basic[j].tok[k].data, "%05u", labels[l].line+data[i].data.bas.basic[j].tok[k].index);
										if(debug) fprintf(stderr, " to %s\n", data[i].data.bas.basic[j].tok[k].data);
									}
									data[i].data.bas.basic[j].tok[k].data2=(char *)malloc(6);
									zxfloat(data[i].data.bas.basic[j].tok[k].data2, labels[l].line+data[i].data.bas.basic[j].tok[k].index);
									break;
								}
							}
							if(l==nlabels)
							{
								fprintf(stderr, "bast: Linker: Undefined label %s\n\t"LOC"\n", data[i].data.bas.basic[j].tok[k].data, data[i].name, j);
								return(EXIT_FAILURE);
							}
						}
						else if(data[i].data.bas.basic[j].tok[k].tok==TOKEN_PTRLBL)
						{
							int l;
							for(l=0;l<nlabels;l++)
							{
								// TODO limit label scope to this file & the files it has #imported
								if((data[labels[l].seg].type==BASIC) && (strcmp(data[i].data.bas.basic[j].tok[k].data, labels[l].text)==0))
								{
									if(debug) fprintf(stderr, "bast: Linker: expanded @%s", data[i].data.bas.basic[j].tok[k].data);
									if(data[i].data.bas.basic[j].tok[k].index)
									{
										if(debug) fprintf(stderr, "%s%02x", data[i].data.bas.basic[j].tok[k].index>0?"+":"-", abs(data[i].data.bas.basic[j].tok[k].index));
									}
									data[i].data.bas.basic[j].tok[k].tok=TOKEN_ZXFLOAT;
									if(Ocutnumbers)
									{
										data[i].data.bas.basic[j].tok[k].data=strdup(".");
										if(debug) fprintf(stderr, " to %u (cut)\n", (unsigned int)data[labels[l].seg].data.bas.basic[labels[l].sline].offset+data[i].data.bas.basic[j].tok[k].index);
									}
									else
									{
										data[i].data.bas.basic[j].tok[k].data=(char *)malloc(6);
										sprintf(data[i].data.bas.basic[j].tok[k].data, "%05u", (unsigned int)data[labels[l].seg].data.bas.basic[labels[l].sline].offset+data[i].data.bas.basic[j].tok[k].index);
										if(debug) fprintf(stderr, " to %s\n", data[i].data.bas.basic[j].tok[k].data);
									}
									data[i].data.bas.basic[j].tok[k].data2=(char *)malloc(6);
									zxfloat(data[i].data.bas.basic[j].tok[k].data2, data[labels[l].seg].data.bas.basic[labels[l].sline].offset+data[i].data.bas.basic[j].tok[k].index);
									break;
								}
							}
							if(l==nlabels)
							{
								fprintf(stderr, "bast: Linker: Undefined label %s\n\t"LOC"\n", data[i].data.bas.basic[j].tok[k].data, data[i].name, j);
								return(EXIT_FAILURE);
							}
						}
					}
				}
			break;
			case BINARY:
				if(data[i].data.bin.nbytes)
				{
					int j;
					for(j=0;j<data[i].data.bin.nbytes;j++)
					{
						switch(data[i].data.bin.bytes[j].type)
						{
							case BYTE:
								// do nothing
							break;
							// TODO LBL, LBM (labelpointer parsing)
							default:
								fprintf(stderr, "bast: Linker: Bad byte-type %u\n\t%s+0x%04X\n", data[i].data.bin.bytes[j].type, data[i].name, j);
								return(EXIT_FAILURE);
							break;
						}
					}
				}
			break;
			default:
				fprintf(stderr, "bast: Linker: Internal error: Bad segment-type %u\n", data[i].type);
				return(EXIT_FAILURE);
			break;
		}
	}
	fprintf(stderr, "bast: Linker passed all segments\n");
	/* END: LINKER & LABELS */
	
	/* CREATE OUTPUT */
	switch(outtype)
	{
		case TAPE:
			fprintf(stderr, "bast: Creating TAPE output\n");
			if(nsegs)
			{
				FILE *fout=fopen(outfile, "wb");
				if(!fout)
				{
					fprintf(stderr, "bast: Could not open output file %s for writing!\n", outfile);
					return(EXIT_FAILURE);
				}
				int i;
				for(i=0;i<nsegs;i++)
				{
					// write header
					fputc(0x13, fout);
					fputc(0x00, fout);
					unsigned char cksum=0;
					fputc(0x00, fout); // HEADER
					int j;
					char name[10];
					switch(data[i].type)
					{
						case BASIC:
							fputc(0, fout); // PROGRAM
							memset(name, ' ', 10);
							memcpy(name, data[i].name, min(10, strlen(data[i].name)));
							for(j=0;j<10;j++)
							{
								fputc(name[j], fout);
								cksum^=name[j];
							}
							buildbas(&data[i].data.bas, true);
							if(data[i].data.bas.blen==-1)
							{
								fprintf(stderr, "bast: Failed to link BASIC segment %s\n", data[i].name);
								return(EXIT_FAILURE);
							}
							fputc(data[i].data.bas.blen, fout);
							cksum^=data[i].data.bas.blen&0xFF;
							fputc(data[i].data.bas.blen>>8, fout);
							cksum^=data[i].data.bas.blen>>8;
							if(data[i].data.bas.line) // Parameter 1 = autostart line
							{
								fputc(data[i].data.bas.line, fout);
								cksum^=data[i].data.bas.line&0xFF;
								fputc(data[i].data.bas.line>>8, fout);
								cksum^=data[i].data.bas.line>>8;
							}
							else // Parameter 1 = 0xFFFF
							{
								fputc(0xFF, fout);
								fputc(0xFF, fout);
							}
							// Parameter 2 = data[i].data.bas.blen
							fputc(data[i].data.bas.blen, fout);
							cksum^=data[i].data.bas.blen&0xFF;
							fputc(data[i].data.bas.blen>>8, fout);
							cksum^=data[i].data.bas.blen>>8;
							fputc(cksum, fout);
							// write data block
							fputc((data[i].data.bas.blen+2), fout);
							fputc((data[i].data.bas.blen+2)>>8, fout);
							fputc(0xFF, fout); // DATA
							cksum=0xFF;
							for(j=0;j<data[i].data.bas.blen;j++)
							{
								fputc(data[i].data.bas.block[j], fout);
								cksum^=data[i].data.bas.block[j];
							}
							fputc(cksum, fout);
							free(data[i].data.bas.block);
						break;
						case BINARY:
							fputc(3, fout); // CODE
							cksum^=3;
							memset(name, ' ', 10);
							memcpy(name, data[i].name, min(10, strlen(data[i].name)));
							for(j=0;j<10;j++)
							{
								fputc(name[j], fout);
								cksum^=name[j];
							}
							fputc(data[i].data.bin.nbytes, fout);
							cksum^=data[i].data.bin.nbytes&0xFF;
							fputc(data[i].data.bin.nbytes>>8, fout);
							cksum^=data[i].data.bin.nbytes>>8;
							// Parameter 1 = address
							fputc(data[i].data.bin.org, fout);
							cksum^=data[i].data.bin.org&0xFF;
							fputc(data[i].data.bin.org>>8, fout);
							cksum^=data[i].data.bin.org>>8;
							// Parameter 2 = 0x8000
							fputc(0x00, fout);
							fputc(0x80, fout);
							cksum^=0x80;
							fputc(cksum, fout);
							// write data block
							fputc((data[i].data.bin.nbytes+2), fout);
							fputc((data[i].data.bin.nbytes+2)>>8, fout);
							fputc(0xFF, fout); // DATA
							cksum=0xFF;
							for(j=0;j<data[i].data.bin.nbytes;j++)
							{
								fputc(data[i].data.bin.bytes[j].byte, fout);
								cksum^=data[i].data.bin.bytes[j].byte;
							}
							fputc(cksum, fout);
							free(data[i].data.bin.bytes);
						break;
						default:
							fprintf(stderr, "bast: Internal error: Don't know how to make TAPE output of segment type %u\n", data[i].type);
							return(EXIT_FAILURE);
						break;
					}
					
					fprintf(stderr, "bast: Wrote segment %s\n", data[i].name);
				}
				fclose(fout);
			}