sortdata.c 8.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
#include <stdio.h>
#include <string.h>

void useage(void)
{
	printf("Useage:\n");
	printf("  sortdata [-l | -i]\n");
	printf("     reads standard input and writes standard output\n");
	printf("     -l writes #defines for all labels\n");
	printf("     -i writes .word/.byte/.half etc for each occurance\n");
}

int dolabels=0, wasincomment=0;
int linenum=0;

int getline(char *buf)
{
        int c;

        c=getchar();

        if (c==EOF) return 0;
        while (c!='\n' && c!=EOF) {
                *(buf++)=c;
                c=getchar();
        }
        *buf='\0';
	linenum++;
        return 1;
}

int checkLabel(char *buf,char *labelname)
{
	char *c;
	int incomment;

	incomment=wasincomment;
	for (c=buf; *c!='\0' && *c!=' ' && *c!='\t'; c++) {
		if (incomment) {
			if (*c=='*' && *(c+1)=='/') {
				incomment=0;
				c++;
			}
		} else {
			if (*c=='/' && *(c+1)=='*') {
				incomment=1; 
				c++;
			} else {
				if (*c=='#') return 0;
				*(labelname)='\0';
				if (*c==':') return 1;
				*(labelname++)=*c;
			}
		}
	}
	return 0;
}
int checkWord(char *buf,char *dotword)
{
	char *c;
	int incomment;

	incomment=wasincomment;
	for (c=buf; *c!='\0'; c++) {
		if (incomment) {
			if (*c=='*' && *(c+1)=='/') {
				incomment=0;
				c++;
			}
		} else {
			if (*c=='/' && *(c+1)=='*') {
				incomment=1; 
				c++;
			} else {
				if (*c=='#' && c!=buf) return 0;
				if (!strncmp(dotword,c,strlen(dotword)))
					return 1;
			}
		}
	}
	return 0;
}
int checkcomment(char *buf)
{
	char *c;
	int incomment;

	incomment=wasincomment;
	for (c=buf; *c!='\0'; c++) {
		if (incomment) {
			if (*c=='*' && *(c+1)=='/') {
				incomment=0;
				c++;
			}
		} else {
			if (*c=='/' && *(c+1)=='*') {
				incomment=1; 
				c++;
			}
		}
	}
	return incomment;
}
void dolabel(char *buf,char *labelname,int current)
{
	int n;
	char spc[31];

	n=30-strlen(labelname);
	if (n<1) n=1;
	spc[n]='\0';
	while(--n>=0) spc[n]=' ';

	if (dolabels)
		printf("#define %s%s0x%04X\n",labelname,spc,current);
	else 
		printf(" ### %s%s0x%04X\n",labelname,spc,current);
	
}
void doData(char *buf)
{
	if (!dolabels)
		printf("%s\n",buf);
}
void doLine(char *buf)
{
	printf("%s\n",buf);
}
int doGetWordNum(char *buf,char *word)
{
	char *c;
	int num;

	for (c=buf; *c!='\0'; c++) {
		if (!strncmp(word,c,strlen(word))) break;
	}
	while (*c!=' ' && *c!= '\t'  && *c!= '\0') c++;
	while (*c==' ' || *c== '\t') c++;

	if (*c=='\0') {
		printf("errprint(ERROR: %s missing argument. LINE %d\n)\n",
								word,linenum);
		printf("xxxxxm4exit 1\n");
		return 0;
	}

	sscanf(c,"%i",&num);

	if (!num) {
		printf("errprint(ERROR: %s argument 0 or non-numeric. LINE %d\n)\n", word,linenum);
		printf("xxxxxm4exit 1\n");
		return 0;
	}
	return num;
}
void doMax(char *buf,int current)
{
	int num;

	if (!(num=doGetWordNum(buf,".max")))
		return;

	if (current > num) {
		printf("errprint(ERROR: ALLOCATED MEMORY TOO LARGE (0x%X > 0x%X). LINE %d\n)\n", current,num,linenum);
		printf("xxxxxm4exit 1\n");
		return;
	} else {
		printf(" ### ALLOCATED MEMORY (0x%X) DOES NOT EXCEED 0x%X HERE\n",current,num);
	}
}
void doMin(char *buf,int current)
{
	int num;

	if (!(num=doGetWordNum(buf,".min")))
		return;

	if (current < num) {
		printf("errprint(ERROR: ALLOCATED MEMORY TOO SMALL (0x%X < 0x%X). LINE %d\n)\n", current,num,linenum);
		printf("xxxxxm4exit 1\n");
		return;
	} else {
		printf(" ### ALLOCATED MEMORY (0x%X) IS AT LEAST 0x%X HERE\n",current,num);
	}
}
void doBound(char *buf,int current)
{
	int num;

	if (!(num=doGetWordNum(buf,".bound")))
		return;

	if (current % num) {
		printf("errprint(ERROR: NOT ALIGNED TO %d BYTE BOUNDARY(0x%X). LINE %d\n)\n", num,current,linenum);
		printf("xxxxxm4exit 1\n");
		return;
	} else {
		printf(" ### ALIGNED TO %d BYTE BOUNDARY HERE\n",num);
	}
}
int doAlign(char *buf,int current)
{
	int num;

	if (!(num=doGetWordNum(buf,".align")))
		return;

	while (current % num) {
		if (!dolabels)
			printf("	.byte	0		# ALIGN PAD\n");
		current++;
	}

	return current;
}
int doSpace(char *buf,int current)
{
	int num;

	if (!(num=doGetWordNum(buf,".space")))
		return;

	return current+num;
}
int killPrint(char *buf1,char *word)
{
	char *c1,*c2;
	char buf2[1000];

	strcpy(buf2,buf1);

	c1=buf1;
	for (c2=buf2; *c2!='\0'; c2++) {
		if (!strncmp(word,c2,strlen(word))) break;
		*(c1++)= *c2;
	}
	while (*c2!='(')
		*(c1++)= *(c2++);
	*(c1++)='(';
	*(c1++)=')';
	*(c1++)='\0';

	return killParen(c2+1,1);
}
int killParen(char *buf, int n)
{
	while (n>0 && *buf!='\0') {
		if (*buf=='(') n++;
		if (*buf==')') n--;
		buf++;
	}
	return n;
}

main(int argc, char *argv[])
{
	int c;
	char linebuf1[1000];
	char linebuf2[1000];
	char *linec,*dotc;
	int  current=0;
#define MAXSTACK 100
	int savestack[MAXSTACK], checkstack[MAXSTACK];
	int savep=0,checkp=0;
	int oldcur,nparen;

	if (argc != 2) {
		useage();
		exit(1);
	}
	dolabels 	= argv[1][0];
	if (dolabels == '-') 
		dolabels 	= argv[1][1];
	switch (dolabels) {
		case 'l' : 
			dolabels = 1;
			break;
		case 'i' : 
			dolabels = 0;
			break;
		default :
			useage();
			exit(1);
	}

	if (dolabels) {
		printf("\n");
		printf("#ifndef _tdmem_labels_h_\n");
		printf("#define _tdmem_labels_h_ 1\n");
		printf("\n");
		printf(" #\n");
		printf(" # tdmem_labels.h\n");
		printf(" #\n");
		printf(" # THIS FILE WAS AUTOMATICALLY GENERATED BY\n");
		printf(" # THE SORTDATA.C PROGRAM\n");
		printf(" #\n");
		printf(" # DO NOT EDIT\n");
		printf(" #\n");
	} else {
		printf("\n");
		printf("#ifndef _tdmem_init_h_\n");
		printf("#define _tdmem_init_h_ 1\n");
		printf("\n");
		printf(" #\n");
		printf(" # tdmem_init.h\n");
		printf(" #\n");
		printf(" # THIS FILE WAS AUTOMATICALLY GENERATED BY\n");
		printf(" # THE SORTDATA.C PROGRAM\n");
		printf(" #\n");
		printf(" # DO NOT EDIT\n");
		printf(" #\n");
	}


	while(getline(linebuf1)) {
		if (checkWord(linebuf1,"errprint")) {
			if (!dolabels)
				doLine(linebuf1);
			else {
				nparen=killPrint(linebuf1,"errprint");
				doLine(linebuf1);
				while(nparen>0) {
					if (!getline(linebuf1)) break;
					nparen=killParen(linebuf1,nparen);
				}
			}
		} else if (!strncmp(linebuf1,"#define",strlen("#define"))) {
			if (dolabels)
				doLine(linebuf1);
		} else if (!strncmp(linebuf1,"#if",strlen("#if"))) {
			doLine(linebuf1);
			savestack[savep++]=current;
			checkstack[checkp++]=-1;
			if (savep>MAXSTACK || checkp>MAXSTACK) {
				printf("errprint(SORTDATA ERROR: TOO MANY ifdef LEVELS. LINE %d\n)\n",linenum);
				printf("xxxxxm4exit 1\n");
				exit(1);
			}
		} else if (!strncmp(linebuf1,"#else",strlen("#else"))) {
			doLine(linebuf1);
			if (savep<1 || checkp<1) {
				printf("errprint(SORTDATA ERROR: ELSE WITH NO IF. LINE %d\n)\n",linenum);
				printf("xxxxxm4exit 1\n");
				exit(1);
			}
			oldcur=checkstack[--checkp];
			if (oldcur!= -1 && oldcur!=current) {
				printf("errprint(ERROR: Number of words declared in 2 parts of #ifdef were not equal. LINE %d\n)\n",linenum);
				printf("xxxxxm4exit 1\n");
			}
			checkstack[checkp++]=current;
			current = savestack[savep-1];
		} else if (!strncmp(linebuf1,"#endif",strlen("#endif"))) {
			doLine(linebuf1);
			if (savep<1 || checkp<1) {
				printf("errprint(SORTDATA ERROR: ENDIF WITH NO IF. LINE %d\n)\n",linenum);
				printf("xxxxxm4exit 1\n");
				exit(1);
			}
			oldcur=checkstack[--checkp];
			if (oldcur==-1) {
				printf("errprint(ERROR: #ifdef without #else (ifdef & else allocations must match) LINE %d\n)\n",linenum);
				printf("xxxxxm4exit 1\n");
			}
			if (oldcur!= -1 && oldcur!=current) {
				printf("errprint(ERROR: Number of words declared in 2 parts of #ifdef were not equal. LINE %d\n)\n",linenum);
				printf("xxxxxm4exit 1\n");
			}
			savep--;
		} else if (checkLabel(linebuf1,linebuf2)) {
			dolabel(linebuf1,linebuf2,current);
		} else if (checkWord(linebuf1,".word")) {
			doData(linebuf1);
			current += 4;
		} else if (checkWord(linebuf1,".half")) {
			doData(linebuf1);
			current += 2;
		} else if (checkWord(linebuf1,".byte")) {
			doData(linebuf1);
			current += 1;
		} else if (checkWord(linebuf1,".align")) {
			current=doAlign(linebuf1,current);
		} else if (checkWord(linebuf1,".bound")) {
			doBound(linebuf1,current);
		} else if (checkWord(linebuf1,".max")) {
			doMax(linebuf1,current);
		} else if (checkWord(linebuf1,".min")) {
			doMin(linebuf1,current);
		} else if (checkWord(linebuf1,".space")) {
			current=doSpace(linebuf1,current);
		} else {
			doLine(linebuf1);
		}
		wasincomment=checkcomment(linebuf1);
	}

	if (dolabels) {
		printf("\n");
		printf("#endif /* _tdmem_labels_h_ */\n");
	} else {
		printf("\n");
		printf("#endif /* _tdmem_init_h_ */\n");
	}

	exit(0);
}