⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmdline.c

📁 < 虚拟机设计与实现> 的source code, linux版本
💻 C
字号:
#include <stdio.h>

#include "cmdline.h"
#include "error.h"

char inputFile[FNAME_SIZE];		/*source file*/
char listFile[FNAME_SIZE];		/*listing file*/
char tempFile[FNAME_SIZE];		/*temporary bytecode*/
char outputFile[FNAME_SIZE];	/*compilation unit*/

U1 omitDebugData;			/*TRUE if omit debug data*/
U1 listing;					/*TRUE if desire a listing*/

static char IN_EXT[5];	 /*source*/
static char TMP_EXT[5];	 /*intermediate*/
static char LST_EXT[5];	 /*listing*/
static char OUT_EXT[5];	 /*executable*/

int addFileSuffix(char *out, char *in, char *ext);

void cmdline_init()
{
	strcpy(IN_EXT, ".ASM");	
	strcpy(TMP_EXT, ".TMP");	
	strcpy(LST_EXT, ".LST");	
	strcpy(OUT_EXT, ".RUN");	

	inputFile[0] = '\0';
	listFile[0] = '\0';
	tempFile[0] = '\0';
	outputFile[0] = '\0';

	omitDebugData = FALSE;
	listing = FALSE;
	
	return;
}

void printUsage()
{
	printf("\nHASM file.ASM [options]\n\n");
	printf("\tOptions:\n\n");
	
	printf("\t-d\n\t\tomit debug information\n");
	printf("\t-e=XXX\n\t\tnumber of errors to report\n");
	printf("\t-l\n\t\tproduce a listing file\n");
	printf("\t-o=XXX\n\t\tname of output file (compilation unit)\n");

	printf("\n\tthere are no spaces between option characters\n\n");
	return;

}

/*
	processes the command-line arguments. 

	Note: argv[0] = name of executable
		  argv[1] = first command line argument
		  argv[2] = second command line argument
		  ... etc. 
*/

U1 handleArgs(int argc, char *argv[])
{
	int bRet;
	int i;
	char intstr[32];
	int len;

	/* no arguments */
	if (argc == 1)
	{
		ERROR0("No Arguments\n");
		printUsage();
		return FALSE;
	}

	/* get input file name "filename.RUN" */
	if (argv[1][0] != '-')
	{
		int len;

		strncpy(inputFile, argv[1], FNAME_SIZE);
		inputFile[FNAME_SIZE-1] = '\0';
		len = strlen(inputFile);
			
		if (len < 4)
		{
			ERROR0("handleArgs(): expecting .ASM file\n");
			printUsage();
			return FALSE;
		}
			
		if ( !((inputFile[len - 4] == '.') &&
			   ((inputFile[len - 3] == 'A') || (inputFile[len - 3] == 'a')) &&
			   ((inputFile[len - 2] == 'S') || (inputFile[len - 2] == 's')) &&
			   ((inputFile[len - 1] == 'M') || (inputFile[len - 1] == 'm'))) )
		{
			ERROR0("handleArgs(): expecting .ASM file\n");
			printUsage();
			return FALSE;
		}
	}
	else
	{
		ERROR0("handleArgs(): expecting .ASM file\n");
		printUsage();
		return FALSE;
	}

	/* process options */	
	for (i = 2; i < argc; i++)
	{ 
		if (argv[i][0] == '-')
		{
			switch (argv[i][1])
			{	
				case 'd':
				case 'D':
					omitDebugData = TRUE;

					if (argv[i][2] != '\0')
					{
						ERROR0("handleArgs(): bad \'-d\' switch\n");
						printUsage();
						return FALSE;
					}

					CMD_LINE_DEBUG0("omitDebugData set to TRUE\n");
                    break;

				case 'e':
				case 'E':
					if (argv[i][2] == '=')
					{
						strcpy(intstr, &argv[i][3]);
						maxErrors = atoi(intstr);
					}
					else
					{
						ERROR0("handleArgs(): bad \'-e\' switch\n");
						printUsage();
						return FALSE;
					}

					CMD_LINE_DEBUG0("number of errors set\n");
                    break;

				case 'l':
				case 'L':
					listing = TRUE;

					if (argv[i][2] != '\0')
					{
						ERROR0("handleArgs(): bad \'-l\' switch\n");
						printUsage();
						return FALSE;
					}

					CMD_LINE_DEBUG0("ListFile set to TRUE\n");
                    break;

				case 'o':
				case 'O':
					/* output file must look like ( file.RUN ) */
					if (argv[i][2] == '=')
					{
						strncpy(outputFile, &argv[i][3], FNAME_SIZE);
						outputFile[FNAME_SIZE - 1]='\0';
						len = strlen(outputFile);

						if (len < 4)
						{
							ERROR0("handleArgs(): bad -o=file\n");
							return FALSE;
						}
			
						if( !((outputFile[len - 4] == '.') &&
						      (outputFile[len - 3] == 'R') &&
						      (outputFile[len - 2] == 'U') &&
						      (outputFile[len - 1] == 'N')) )
						{
							ERROR0("handleArgs(): bad -o=file\n");
							return FALSE;
						}
							
						CMD_LINE_DEBUG0("output file name set\n");
					}
					else
					{
						ERROR0("handleArgs(): bad \'-o\' switch\n");
						printUsage();
						return FALSE;
					}
                    break;

				default:
				{
					ERROR1("handleArgs(): %s bad option\n",argv[i]);
					printUsage();
					return FALSE;
				}
			}/*end-switch*/
		}/*end if*/
		else
		{
			/*not an option, does not begin with dash*/
			ERROR0("handleArgs(): expecting prefix \'-\' \n");
			printUsage();
			return FALSE;
		}

	}/*end for-loop*/

	if (outputFile[0] == '\0')
	{ 
		bRet = addFileSuffix(outputFile, inputFile, OUT_EXT); 
		if (bRet == FALSE)
			return FALSE;
	}

	bRet = addFileSuffix(tempFile, outputFile, TMP_EXT); 
	if (bRet == FALSE)
		return FALSE;

	bRet = addFileSuffix(listFile, outputFile, LST_EXT);
	if (bRet == FALSE)
		return FALSE;

	CMD_LINE_DEBUG0("Command line summary\n");
	CMD_LINE_DEBUG1("\tfile to load=%s\n", inputFile);
	CMD_LINE_DEBUG1("\toutput file=%s\n", outputFile);
	CMD_LINE_DEBUG1("\ttemp file=%s\n", tempFile);
	CMD_LINE_DEBUG1("\tlist file=%s\n", listFile);
	CMD_LINE_DEBUG1("\terrors to report=%d\n", maxErrors);
	
	if (listing == TRUE)
	{ 
		CMD_LINE_DEBUG0("\tlisting is ON\n"); 
	}
	else
	{ 
		CMD_LINE_DEBUG0("\tlisting is OFF\n"); 
	}
	
	if (omitDebugData == TRUE)
	{ 
		CMD_LINE_DEBUG0("\tomitting debug data\n\n"); 
	}
	else
	{ 
		CMD_LINE_DEBUG0("\tdebug data included\n\n"); 
	}

	return TRUE;

} /*end handleArguments*/

int addFileSuffix(char *out, char *in, char *ext)
{
	char *cptr;
	int index;

	if ((out == NULL) || (in == NULL) || (ext == NULL))
	{
		ERROR0("addFileSuffixe(): null pointer args\n");
		return FALSE;
	}

	cptr = strrchr(in, '.');

	/*if no extension, then just add suffix*/
	if (cptr == NULL)
	{
		strcpy(out, in);
		strcat(out, ext);
	}
	else
	{
        /*otherwise, copy until we hit '.' and then add suffix*/
		index = 0;

		while ((in[index] != '.') && (index < FNAME_SIZE - 4))
		{
			out[index] = in[index];
			index++;
		}
		out[index] = '\0';
		strncat(out, ext, 4);
	}
	return TRUE;

} /*end addFileSuffix*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -