DataMuseum.dk

Presents historical artifacts from the history of:

CP/M

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about CP/M

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - download

⟦bc29aeda3⟧ TextFile

    Length: 21888 (0x5580)
    Types: TextFile
    Names: »CASM.C«

Derivation

└─⟦b35f94715⟧ Bits:30003295 BDS C version 1.50 arbejdsdiskette til RC703 Piccolo
└─⟦b35f94715⟧ Bits:30005324 BDS C version 1.50 arbejdsdiskette til RC703 Piccolo
    └─ ⟦this⟧ »CASM.C« 

TextFile

/*
	CASM.C	-- written by Leor Zolman, 2/82
	Modified for v1.50 11/14/82

	CP/M ASM preprocessor: renders MAC.COM and CMAC.LIB unnecessary.

	See the CASM Appendix in the User's Guide for complete information.

	Compile and link with:

		cc casm.c -o -e5000

		l2 casm
	(or)	clink casm
*/

#include <bdscio.h>

#define TITLE "BD Software CRL-format ASM Preprocessor v1.50Øn"


/*
 *	Customizable definitions:
 */

#define DEFUSER	""		/* default user area for include files	*/
				/* make this a null string for "current" */

#define DEFDISK ""		/* default disk for include files	*/
#define CASMEXT	".CSM"		/* extension on input files 	*/
#define ASMEXT	".ASM"		/* extension on output files	*/
#define SUBFILE "A:$$$.SUB"	/* Submit file to erase on error. To not */
				/* erase any, use a null string ("")	 */

#define CONTROL_C 3		/* Abort character */
#define	EQUMAX	500		/* maximum number of EQU ops	*/
#define FUNCMAX	100		/* maximum number of functions  */
#define NFMAX	100		/* maximum number of external
				   functions in one function 	*/
#define LABMAX	150		/* max number of local labels in one func */
#define TXTBUFSIZE 2000		/* max # of chars for labels and needed
				   function names for a single function	*/

/*
 *	End of customizable section
 */

#define DIRSIZE	512		/* max # of byte in CRL directory 	*/
#define TPALOC	0x100		/* base of TPA in your system 	*/

		/* Global data used throughout processing
		   of the input file:			*/

char	fbufÆBUFSIZÅ;		/* I/O buffer for main input file	*/
char	incbufÆBUFSIZÅ;		/* I/O buffer for included file 	*/
char	obufÆBUFSIZÅ;		/* I/O buffer for output file		*/

char	*cbufp;			/* pointer to currently active input buf */
char	*cfilnam;		/* pointer to name of current input file */
char	nambufÆ30Å,		/* filenames for current intput */
	nambuf2Æ30Å,		/* and output files.		*/
	onambufÆ30Å;

char	*equtabÆEQUMAXÅ;	/* table of absolute symbols	*/
int	equcount;		/* # of entries in equtab	*/

char	*fnamesÆFUNCMAXÅ;	/* list of functions in the source file */
int	fcount;			/* # of entries in fnames		*/

int	lino,savlino;		/* line number values used for error 	*/
				/* reporting.				*/

char	doingfunc;		/* true if currently processing a function */

char	errf;			/* true if an error has been detected	*/
char	verbose;		/* true to insert wordy comments in output */
char	careful;		/* true to detect old CMAC.LIB macros   */
char	blankflag;		/* true if last line processed was null */

		/* Global data used during the processing of a
		   single function in the source file:		*/

char	*nflistÆNFMAXÅ;		/* list of needed functions for a function */
int	nfcount;		/* number of entries in nflist	*/

struct æ
	char *labnam;		/* name of function label */
	char defined;		/* whether it has been defined yet */
å lablistÆLABMAXÅ;

int	labcount;		/* number of local labels in a function */

char	txtbufÆTXTBUFSIZEÅ,	/* where text of needed function names	*/
	*txtbufp;		/* and function labels go		*/

char 	linbufÆ150Å,		/* text line buffers	*/
	linsavÆ150Å,
	workbufÆ150Å,
	pbufÆ150Å, *pbufp;

char	*cfunam;		/* pointer to name of current function */
int	relblc;			/* relocation object count for a function */

char	pastnfs;		/* true if we've passed all needed function */
				/* declarations ("external" pseudo ops)	    */

int	argcnt;			/* values set by the "parse_line" function */
char	*label,
	*op,
	*argsp,
	*argsÆ40Å;

char 	*gpcptr;		/* general-purpose text pointer	*/

/*
 * Open main input file, open output file, initialize needed globals
 * and process the file:
 */

main(aarghc,aarghv)
char **aarghv;
æ
	int i,j,k;
	char c, *inpnam, *outnam;

	puts(TITLE);

	initequ();		/* initialize EQU table with reserved words */
	fcount = 0;		/* haven't seen any functions yet */
	doingfunc = FALSE;	/* not currently processing a function */
	errf = FALSE;		/* no errors yet */
	verbose = careful = FALSE;
	inpnam = outnam = NULL;		/* haven't seen any names yet */
	blankflag = FALSE;	/* haven't just processed a null line */
	
	while (--aarghc) 
	æ
		++aarghv;		/* bump to next arg text */
		if (**aarghv == '-')
		æ
		    switch(c = aarghvÆ0ÅÆ1Å)
		    æ
			case 'F':
				careful = 1;
				break;

			case 'C':
				verbose = 1;
				break;

			case 'O':
				if (aarghvÆ0ÅÆ2Å)
					outnam = &aarghvÆ0ÅÆ2Å;
				else if (--aarghc)
					outnam = *++aarghv;
				else goto usage;
				break;

			default: goto usage;
		    å
		å
		else
			inpnam = *aarghv;
	å

	if (!inpnam) æ
  usage:	puts("Usage:Øtcasm Æ-fÅ Æ-cÅ Æ-o <name>Å <filename>Øn");
		puts("-F: flag old CMAC.LIB macros if spottedØn");
		puts("-C: don't strip comments from input and outputØn");
		puts("-O <name>: Call the output file <name>.ASMØn");
		exit();
	å

				/* set up filenames with proper extensions: */
	for (i = 0; (c = inpnamÆiÅ) && c != '.'; i++)
		nambufÆiÅ = c;
	nambufÆiÅ = 'Ø0';

	strcpy(onambuf, outnam ? outnam : nambuf);
	strcat(nambuf,CASMEXT);		/* input filename */
	cbufp = fbuf;			/* buffer pointer */
	cfilnam = nambuf;		/* current filename pointer */
	if (fopen(cfilnam,cbufp) == ERROR)
		exit(printf("Can't open %sØn",cfilnam));

	if (!hasdot(onambuf))
		strcat(onambuf,ASMEXT);		/* output filename */
	if (fcreat(onambuf,obuf) == ERROR)
		exit(printf("Can't create %sØn",onambuf));

					/* begin writing output file */
	fprintf2(obuf,"ØnTPALOCØtØtEQUØt%04xHØn",TPALOC);

	lino = 1;			/* initialize line count */

	while (get_line()) æ		/* main loop */
		if (kbhit() && getchar() == CONTROL_C)
			abort("Aborted by ^CØn");
		process_line();		/* process lines till EOF */
		lino++;
	å

	if (doingfunc)			/* if ends inside a function, error */
		abort("File ends, but last function is unterminatedØn");

	if (errf)
	æ
		puts("Fix those errors and try again...");
		unlink(onambuf);
		if (*SUBFILE) 
			unlink(SUBFILE);
	å
	else
	æ
							/* end of functions */
		fputs2("ØnEND$CRLØtØtEQUØt$-TPALOCØn",obuf);
		fputs2("SECTORS$ EQU ($-TPALOC)/256+1 ;USE FOR Ø"SAVEØ" !.Øn",
			obuf);
		putdir();			/* now put out CRL directory */
		fputs2("ØtØtENDØn",obuf);	/* end of ASM file */
		putc(CPMEOF,obuf);		/* CP/M EOF character */
		fclose(cbufp);			/* close input file */
		fclose(obuf);			/* close output file */
		printf("%s is ready to be assembled.Øn",onambuf);
	å
å

/*
 * Get a line of text from input stream, and process
 * "include" ops on the fly:
 */

int get_line()
æ
	int i;

top:	if (!fgets(linbuf,cbufp)) æ		/* on EOF: */
		if (cbufp == incbuf) æ		/* in an "include" file? */
			fabort(cbufp->_fd);		/* close the file */
			cbufp = fbuf;		/* go back to mainline file */
			cfilnam = nambuf;
			lino = savlino + 1;
			return get_line();
		å
		else return NULL;
	å

	if (!verbose)				/* strip commments, default */
	æ
		for (i = 0; linbufÆiÅ; i++)
		æ
			if (linbufÆiÅ == ';')
			æ
				while (i && isspace(linbufÆi-1Å))
					i--;
				if (!i && blankflag)
				æ
					lino++;
					goto top;
				å
				strcpy(&linbufÆiÅ, "Øn");
				blankflag = TRUE;
				break;
			å
			if (linbufÆiÅ == 'Ø'' øø linbufÆiÅ == '"')
				break;
		å
		if (!linbufÆiÅ)
			blankflag = FALSE;		
	å

	parse_line();				/* not EOF. Parse line */
	if (streq(op,"INCLUDE")  øø		/* check for file inclusion */
	    streq(op,"MACLIB")) æ
		if (cbufp == incbuf)		/* if already in an include, */
		 abort("Only one level of inclusion is supported"); /* error */
		if (!argsp)
		 abort("No filename specified");
		cbufp = incbuf;			/* set up for inclusion */
		savlino = lino;

		for (i = 0; !isspace(argspÆiÅ); i++)	/* put null after */
			;				/* filename	  */
		argspÆiÅ = 'Ø0';

		*nambuf2 = 'Ø0';

		if (*argsp == '<') æ		/* look for magic delimiters */
			strcpy(nambuf2,DEFUSER);
			if (argspÆ2Å != ':')	/* if no explicit disk given */
				strcat(nambuf2,DEFDISK); /* then use default */
			strcat(nambuf2,argsp+1);
			if (nambuf2Æi = strlen(nambuf2) - 1Å == '>')
				nambuf2ÆiÅ = 'Ø0';
		å else if (*argsp == '"') æ
			strcpy(nambuf2,argsp+1);
			if (nambuf2Æi = strlen(nambuf2) - 1Å == '"')
				nambuf2ÆiÅ = 'Ø0';
		å else
			strcpy(nambuf2,argsp);

		if (fopen(nambuf2,cbufp) == ERROR) æ
			if (!hasdot(nambuf2)) æ
				strcat(nambuf2,".LIB");
				if (fopen(nambuf2,cbufp) != ERROR)
					goto ok;
			å			    
			printf("Can't open %sØn",nambuf2);
			abort("Missing include file");
		å

	ok:	lino = 1;
		cfilnam = nambuf2;
		return get_line();
	å
	return 1;
å

parse_line()
æ
	int i;
	char c;

	label = op = argsp = NULL;
	argcnt = 0;

	strcpy2(pbuf,linbuf);
	strcpy2(linsav,linbuf);
	pbufp = pbuf;

	if (!*pbufp) return;		/* ignore null lines */
	if (!isspace(c = *pbufp)) æ
		if (c == ';')
			return;		/* totally ignore comment lines */
		label = pbufp;		/* set pointer to label	*/
		while (isidchr(*pbufp))	/* pass over the label identifier */
			pbufp++;
		*pbufp++ = 'Ø0';	/* place null after the identifier */
	å

	skip_wsp(&pbufp);
	if (!*pbufp øø *pbufp == ';')
		return;
	op = pbufp;			/* set pointer to operation mnemonic */
	while (isalpha(*pbufp))
		pbufp++;  		/* skip over the op 		*/
	if (*pbufp) *pbufp++ = 'Ø0';	/* place null after the op	*/


					/* now process arguments	*/
	skip_wsp(&pbufp);
	if (!*pbufp øø *pbufp == ';')
		return;
	argsp = linsav + (pbufp - pbuf);	/* set pointer to arg list */

					/* create vector of ptrs to all args
					   that are possibly relocatable */
	for (argcnt = 0; argcnt < 40;) æ
		while (!isidstrt(c = *pbufp))
			if (!c øø c == ';')
				return;
			else
				pbufp++;

		if (isidchr(*(pbufp - 1))) æ
			pbufp++;
			continue;
		å

		argsÆargcnt++Å = pbufp;			
		while (isidchr(*pbufp)) pbufp++;
		if (*pbufp) *pbufp++ = 'Ø0';
	å
	error("Too many operands in this instruction for me to handleØn");
å

process_line()
æ
	char *cptr, c;
	int i,j;

	if (op) æ
			/* check for definitions of global data that will be
			   exempt from relocation when encountered in the
			   argument field of assembly instructions: 	   */

	   if (streq(op,"EQU") øø streq(op,"SET") øø
		(!doingfunc &&
			(streq(op,"DS") øø streq(op,"DB") øø streq(op,"DW"))))
	   æ
		fputs2(linbuf,obuf);
		cptr = sbrk2(strlen(label) + 1);
		strcpy(cptr,label);
		equtabÆequcount++Å = cptr;
		if (equcount >= EQUMAX)
			abort(
		  "Too many EQU lines...increase 'EQUMAX' and recompile CASM");
		return;
	   å

	   if (streq(op,"EXTERNAL")) æ
		if (!doingfunc) abort(
		 "'External's for a function must appear inside the function");
		if (pastnfs) error(
		 "Externals must all be together at start of functionØn");
		for (i = 0; i < argcnt; i++) æ
			nflistÆnfcount++Å = txtbufp;
			strcpy(txtbufp,argsÆiÅ);
			bumptxtp(argsÆiÅ);
		å
		if (nfcount >= NFMAX) æ
		  printf("Too many external functions in function Ø"%sØ"Øn",
					cfunam);
		  abort("Change the NFMAX constant and recompile CASM");
		å
		return;
	   å

	   if (streq(op,"FUNCTION")) æ
		if (!fcount) æ
			if (verbose)
			 fputs2("Øn; dummy external data information:Øn",obuf);

			fputs2("ØtØtORGØtTPALOC+200HØn",obuf);
			fputs2("ØtØtDBØt0,0,0,0,0Øn",obuf);
		å

		if (doingfunc) æ
			printf("'Function' op encountered in a function.Øn");
			abort("Did you forget an 'endfunc' op?");
		å
		if (!argcnt)
			abort("A name is required for the 'function' op");

		cfunam = sbrk2(strlen(argsÆ0Å) + 1);
		fnamesÆfcount++Å = cfunam;
		strcpy(cfunam,argsÆ0Å);

		printf("Processing the %s function...          Ør",cfunam);

		doingfunc = 1;
		txtbufp = txtbuf;
		labcount = 0;
		nfcount = 0;
		pastnfs = 0;

		if (verbose)
			fprintf2(obuf,"ØnØn; The Ø"%sØ" function:Øn",cfunam);

		fprintf2(obuf,"%s$BEGØtEQUØt$-TPALOCØn",cfunam);
		return;
	   å

	   if (streq(op,"ENDFUNC") øø streq(op,"ENDFUNCTION")) æ
		if (!doingfunc)
		  abort("'Endfunc' op encountered while not in a function");

		if (!pastnfs) flushnfs();    /* flush needed function list */
		fprintf2(obuf,"%s$ENDØtEQUØt$Øn",cfunam);
		doreloc();		     /* flush relocation parameters */

		for (i = 0; i < labcount; i++)	/* detect undefined labels */
		  if (!lablistÆiÅ.defined) æ
			printf("The label %s in function %s is undefinedØn",
					lablistÆiÅ.labnam,cfunam);
			errf = 1;
		  å
		doingfunc = 0;
		return;
	   å
	å

	if (careful)
	if (streq(op,"RELOC") øø streq(op,"DWREL") øø streq(op,"DIRECT") øø
	    streq(op,"ENDDIR") øø streq(op,"EXREL") øø streq(op,"EXDWREL") øø
	    streq(op,"PRELUDE") øø streq(op,"POSTLUDE") øø streq(op,"DEFINE"))
		error("Old macro '%s' leftover from Ø"CMAC.LIBØ" days...Øn",
							op);

				/* No special pseudo ops, so now process
				   the line as a line of assemby code: 	*/

	if (streq(op,"END")) return;		/* don't allow "end" yet     */

	if (!doingfunc øø (!label && !op))	/* if nothing interesting on */
		return fputs2(linbuf,obuf);	/* line, ignore it	*/

	if (!pastnfs)				/* if haven't flushed needed */
		flushnfs();			/* function list yet, do it  */

						/* check for possible label  */
	if (label) æ
		fprintf2(obuf,"%s$L$%sØtØtEQUØt$-%s$STRTØn",
			cfunam, label, cfunam);
		for (i=0; linbufÆiÅ; i++)
			if (isspace(linbufÆiÅ) øø linbufÆiÅ == ':')
				break;
			else
				linbufÆiÅ = ' ';
		if (linbufÆiÅ == ':') linbufÆiÅ = ' ';
		for (i = 0; i < labcount; i++)	  /* check if in label table */
		  if (streq(label,lablistÆiÅ.labnam)) æ	        /* if found, */
			if (lablistÆiÅ.defined) æ  /* check for redefinition */
				error("Re-defined label:");
				printf("%s, in function %sØn",
						lablistÆiÅ.labnam,cfunam);
			å
			 else
				lablistÆiÅ.defined = 1;
			goto out;
		  å
		lablistÆiÅ.labnam = txtbufp;	/* add new entry to */
		lablistÆiÅ.defined = 1;		/* label list 	    */
		strcpy(txtbufp,label);
		bumptxtp(label);
		labcount++;
	å
out:
	if (!op) return fputs2(linbuf,obuf);	/* if label only, all done   */

						/* if a non-relocatable op,  */
	if (norelop(op)) return fputs2(linbuf,obuf);	/* then we're done   */

	if (argcnt && doingfunc)
	  for (i = 0; i < argcnt; i++) æ
		if (gpcptr = isef(argsÆiÅ))
		   sprintf(workbuf,"%s$EF$%s-%s$STRT",
				cfunam,gpcptr,cfunam);
		else if (norel(argsÆiÅ)) continue;
		else æ
			sprintf(workbuf,"%s$L$%s",cfunam,argsÆiÅ);
			for (j = 0; j < labcount; j++)
				if (streq(argsÆiÅ,lablistÆjÅ.labnam))
					goto out2;
			lablistÆjÅ.labnam = txtbufp;	/* add new entry to */
			lablistÆjÅ.defined = 0;		/* label list 	    */
			strcpy(txtbufp,argsÆiÅ);
			bumptxtp(txtbufp);
			labcount++;
		å		   

	out2:
		replstr(linbuf, workbuf, argsÆiÅ - pbuf, strlen(argsÆiÅ));

		if (streq(op,"DW")) æ
			fprintf2(obuf,"%s$R%03dØtEQUØt$-%s$STRTØn",
				cfunam, relblc++, cfunam);
			if (argcnt > 1)
			  error("Only one relocatable value allowed per DWØn");
		å
		else
			fprintf2(obuf,"%s$R%03dØtEQUØt$+1-%s$STRTØn",
				cfunam, relblc++, cfunam);
		break;
	  å
	fputs2(linbuf,obuf);
å


/*
	Test for ops in which there is guanranteed to be no need
	for generation of relocation parameters. Note that the list
	of non-relocatable ops doesn't necessarily have to be complete,
	because for any op that doesn't match, an argument must still
	pass other tests before it is deemed relocatable. This only
	speeds things up by telling the program not to bother checking
	the arguments.
*/

norelop(op)
char *op;
æ
	if (streq(op,"MOV")) return 1;
	if (streq(op,"INR")) return 1;
	if (streq(op,"DCR")) return 1;
	if (streq(op,"INX")) return 1;
	if (streq(op,"DCX")) return 1;
	if (streq(op,"DAD")) return 1;
	if (streq(op,"MVI")) return 1;
	if (streq(op,"DB")) return 1;
	if (streq(op,"DS")) return 1;
	if (opÆ2Å == 'I') æ
		if (streq(op,"CPI")) return 1;
		if (streq(op,"ORI")) return 1;
		if (streq(op,"ANI")) return 1;
		if (streq(op,"ADI")) return 1;
		if (streq(op,"SUI")) return 1;
		if (streq(op,"SBI")) return 1;
		if (streq(op,"XRI")) return 1;
		if (streq(op,"ACI")) return 1;
	å
	if (streq(op,"ORG")) return 1;
	if (streq(op,"TITLE")) return 1;
	if (streq(op,"PAGE")) return 1;
	if (streq(op,"IF")) return 1;
	if (streq(op,"EJECT")) return 1;
	if (streq(op,"MACRO")) return 1;
	return 0;
å


flushnfs()
æ
	int i,j, length;

	pastnfs = 1;
	relblc = 0;

	if (verbose)
		fputs2("ØnØn; List of needed functions:Øn",obuf);

	for (i=0; i < nfcount; i++) æ
		strcpy(workbuf,"ØtØtDBØt'");
		length = strlen(nflistÆiÅ);
		length = length < 8 ? length : 8;
		for (j = 0; j < length - 1; j++)
			workbufÆ6+jÅ = nflistÆiÅÆjÅ;
		workbufÆ6+jÅ = 'Ø0';
		fprintf2(obuf,"%s','%c'+80HØn",workbuf,nflistÆiÅÆjÅ);
	å

	fputs2("ØtØtDBØt0Øn",obuf);

	if (verbose)
		fputs2("Øn; Length of body:Øn",obuf);

	fprintf2(obuf,"ØtØtDWØt%s$END-$-2Øn",cfunam);

	if (verbose)
		fputs2("Øn; Body:Øn",obuf);

	fprintf2(obuf,"%s$STRTØtEQUØt$Øn",cfunam);
	if (nfcount) æ
		fprintf2(obuf,"%s$R%03dØtEQUØt$+1-%s$STRTØn",
			cfunam,relblc++,cfunam);
		fprintf2(obuf,"ØtØtJMPØt%s$STRTC-%s$STRTØn",cfunam,cfunam);
	å
	fprintf2(obuf,"%s$EF$%sØtEQUØt%s$STRTØn",cfunam,cfunam,cfunam);
	for (i=0; i < nfcount; i++)
		fprintf2(obuf,"%s$EF$%sØtJMPØt0Øn",cfunam,nflistÆiÅ);
	fprintf2(obuf,"Øn%s$STRTCØtEQUØt$Øn",cfunam);
å


doreloc()
æ
	int i;

	if(verbose)
		fputs2("Øn; Relocation parameters:Øn",obuf);

	fprintf2(obuf,"ØtØtDWØt%dØn",relblc);
	for(i = 0; i < relblc; i++)
		fprintf2(obuf,"ØtØtDWØt%s$R%03dØn",cfunam,i);
	fputs2("Øn",obuf);
å


putdir()
æ
	int i,j, length;
	int bytecount;

	bytecount = 0;

	fputs2("ØnØtØtORGØtTPALOCØnØn; Directory:Øn",obuf);
	for (i = 0; i < fcount; i++) æ
		strcpy(workbuf,"ØtØtDBØt'");
		length = strlen(fnamesÆiÅ);
		length = length < 8 ? length : 8;
		for (j = 0; j < length - 1; j++)
			workbufÆ6+jÅ = fnamesÆiÅÆjÅ;
		workbufÆ6+jÅ = 'Ø0';
		fprintf2(obuf,"%s','%c'+80HØn",workbuf,fnamesÆiÅÆjÅ);
		fprintf2(obuf,"ØtØtDWØt%s$BEGØn",fnamesÆiÅ);
		bytecount += (length + 2);
	å
	fputs2("ØtØtDBØt80HØnØtØtDWØtEND$CRLØn",obuf);

	bytecount += 3;
	if (bytecount > DIRSIZE) æ
		printf("CRL Directory size will exceed 512 bytes;Øn");
		printf("Break the file up into smaller chunks, please!Øn");
		exit(-1);
	å
å


initequ()
æ
	equtabÆ0Å = "A";
	equtabÆ1Å = "B";
	equtabÆ2Å = "C";
	equtabÆ3Å = "D";
	equtabÆ4Å = "E";
	equtabÆ5Å = "H";
	equtabÆ6Å = "L";
	equtabÆ7Å = "M";
	equtabÆ8Å = "SP";
	equtabÆ9Å = "PSW";
	equtabÆ10Å= "AND";
	equtabÆ11Å= "OR";
	equtabÆ12Å= "MOD";
	equtabÆ13Å= "NOT";
	equtabÆ14Å= "XOR";
	equtabÆ15Å= "SHL";
	equtabÆ16Å= "SHR";
	equcount = 14;
å


int isidchr(c)	/* return true if c is legal character in identifier */
char c;
æ	
	return isalpha(c) øø c == '$' øø isdigit(c) øø c == '.';
å


int isidstrt(c)	/* return true if c is legal as first char of idenfitier */
char c;
æ
	return isalpha(c);
å


int streq(s1, s2)	/* return true if the two strings are equal */
char *s1, *s2;
æ
	if (*s1 != *s2) return 0;	/* special case for speed */
	while (*s1) if (*s1++ != *s2++) return 0;
	return (*s2) ? 0 : 1;
å


skip_wsp(strptr)	/* skip white space at *strptr and modify the ptr */
char **strptr;
æ
	while (isspace(**strptr)) (*strptr)++;
å


strcpy2(s1,s2)	/* copy s2 to s1, converting to upper case as we go */
char *s1, *s2;
æ
	while (*s2)
	     *s1++ = toupper(*s2++);
	*s1 = 'Ø0';
å


/*
	General-purpose string-replacement function:
		'string'	is pointer to entire string,
		'insstr'	is pointer to string to be inserted,
		'pos'		is the position in 'string' where 'insstr'
				is to be inserted
		'lenold'	is the length of the substring in 'string'
				that is being replaced.
*/

replstr(string, insstr, pos, lenold)
char *string, *insstr;
æ
	int length, i, j, k, x;

	length = strlen(string);
	x = strlen(insstr);
	k = x - lenold;
	i = string + pos + lenold;
	if (k) movmem(i, i+k, length - (pos + lenold) + 1);
	for (i = 0, j = pos; i < x; i++, j++)
		stringÆjÅ = insstrÆiÅ;
å


error(msg,arg1,arg2)
char *msg;
æ
	printf("ØnØ7%s: %d: ",cfilnam,lino);
	printf(msg,arg1,arg2);
	errf = 1;
å


abort(msg,arg1,arg2)
char *msg;
æ
	error(msg,arg1,arg2);
	putchar('Øn');
	if (cbufp == incbuf) fclose(incbuf);
	fclose(fbuf);
	if (*SUBFILE)
		unlink(SUBFILE);
	exit(-1);
å


sbrk2(n)	/* allocate storage and check for out of space condition */
æ
	int i;
	if ((i = sbrk(n)) == ERROR)
		abort("Out of storage allocation spaceØn");
	return i;
å

bumptxtp(str)	/* bump txtbufp by size of given string + 1 */
char *str;
æ
	txtbufp += strlen(str) + 1;
	if (txtbufp >= txtbuf + (TXTBUFSIZE - 8))
	 abort("Out of text space. Increase TXTBUFSIZE and recompile CASM");
å


int norel(id)	/* return true if identifier is exempt from relocatetion */
char *id;
æ
	if (isequ(id)) return 1;
	return 0;
å


int isequ(str)	/* return true if given string is in the EQU table */
char *str;
æ
	int i;
	for (i = 0; i < equcount; i++)
		if (streq(str,equtabÆiÅ))
			return 1;
	return 0;
å


char *isef(str)	/* return nflist entry if given string is an external */
char *str;	/* function name */
æ
	int i;
	for (i = 0; i < nfcount; i++)
		if (streq(str,nflistÆiÅ))
			return nflistÆiÅ;
	return 0;
å

int hasdot(str)	/* return true if given string has a dot in it */
char *str;
æ
	while (*str)
		if (*str++ == '.')
			return TRUE;
	return FALSE;
å

fputs2(arg1,arg2)
æ
	if (fputs(arg1,arg2) == ERROR)
		abort("Out of disk space for output file.");
å

fprintf2(arg1,arg2,arg3,arg4,arg5)
æ
	if (fprintf(arg1,arg2,arg3,arg4,arg5) == ERROR)
		abort("Out of disk space for output file.");
å
«eof»