Пример #1
0
int main(int argc, char **argv) {
	char commandBuf[BUFSIZE + 1];
	struct Process proc;
	char path[BUFSIZE + 1] = DEFAULT_PATH;
	char *command;
	int detached;

	/* Set attribute to gray on black. */
	Print("\x1B[37m");

	while (true) {
		/* Print shell prompt (bright cyan on black background) */
		Print("\x1B[1;36m$\x1B[37m ");

		/* Read a line of input */
		Read_Line(commandBuf, sizeof(commandBuf));
		command = Strip_Leading_Whitespace(commandBuf);
		Trim_Newline(command);
		detached = isDetached(command);

		/*
		 * Handle some special commands
		 */
		if (strcmp(command, "exit") == 0) {
			/* Exit the shell */
			break;
		} else if (strcmp(command, "pid") == 0) {
			/* Print the pid of this process */
			Print("%d\n", Get_PID());
			continue;
		} else if (strcmp(command, "exitCodes") == 0) {
			/* Print exit codes of spawned processes. */
			exitCodes = 1;
			continue;
		} else if (strncmp(command, "path=", 5) == 0) {
			/* Set the executable search path */
			strcpy(path, command + 5);
			continue;
		} else if (strcmp(command, "") == 0) {
			/* Blank line. */
			continue;
		}

		proc.command = Strip_Leading_Whitespace(command);
		if (!Copy_Token(proc.program, proc.command)) {
			Print("Error: invalid command\n");
			continue;
		}

		Spawn_Single_Command(&proc, path, detached);
		if (detached && proc.pid > 0)
			Print("[%d]\n", proc.pid);

	}

	Print_String("DONE!\n");
	return 0;
}
Пример #2
0
int main(int argc, char **argv)
{
    int nproc;
    char commandBuf[BUFSIZE+1];
    struct Process procList[MAXPROC];
    char path[BUFSIZE+1] = DEFAULT_PATH;
    char *command;

    /* Set attribute to gray on black. */
    Print("\x1B[37m");

    while (true) {
	/* Print shell prompt (bright cyan on black background) */
	Print("\x1B[1;36m$\x1B[37m ");

	/* Read a line of input */
	Read_Line(commandBuf, sizeof(commandBuf));
	command = Strip_Leading_Whitespace(commandBuf);
	Trim_Newline(command);

	/*
	 * Handle some special commands
	 */
	if (strcmp(command, "exit") == 0) {
	    /* Exit the shell */
	    break;
	} else if (strcmp(command, "pid") == 0) {
	    /* Print the pid of this process */
	    Print("%d\n", Get_PID());
	    continue;
	} else if (strcmp(command, "exitCodes") == 0) {
	    /* Print exit codes of spawned processes. */
	    exitCodes = 1;
	    continue;
	} else if (strncmp(command, "path=", 5) == 0) {
	    /* Set the executable search path */
	    strcpy(path, command + 5);
	    continue;
	} else if (strcmp(command, "") == 0) {
	    /* Blank line. */
	    continue;
	}

	/*
	 * Parse the command string and build array of
	 * Process structs representing a pipeline of commands.
	 */
	nproc = Build_Pipeline(command, procList);
	if (nproc <= 0)
	    continue;

	Spawn_Single_Command(procList, nproc, path);
    }

    Print_String("DONE!\n");
    return 0;
}