[syslinux] NEW: COM32 module to run another command, optionally clearing the screen

Gene Cumm gene.cumm at gmail.com
Sun Feb 22 15:13:07 PST 2009


From: Gene Cumm <gene.cumm at gmail.com>

run.c: A simple COM32 module that will optionally clear the screen
(using newlines) then run a command.

Signed-off-by: Gene Cumm <gene.cumm at gmail.com>

---

This is based on my alias COM32 module that I submitted back in
October, with some improvements to clean up debugging code and
optionally clear the screen.

LABEL et
	KERNEL run.c32
	APPEND c32echo.c32 test strings

LABEL etc
	KERNEL run.c32
	APPEND -c c32echo.c32 test strings

With the "-c" as the first command line argument, it will output a
preset number of newlines (currently 40; should be more than enough)
to the screen then run the command.


/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2008-2009 Gene Cumm - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
 *   Boston MA 02111-1307, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * runc.c
 *
 * Run Command & ClearScreen COM32 application;  If -c is the first argument,
 * it clears the screen (using newlines) then calls as a KERNEL with a boot
 * line command as the APPEND line.  This application will also compile as a
 * Linux binary to print the argument string and clear the screen.
 */

/*
 * History
 * b007	Rename to run.c from runc.c
 * 	Allow a '-c' as the first option in order to clear the screen
 * b006	Check to see if it's at the end of the argument list in the for before
 *	adding the space/null at the end of the string.
 * b005 Rename from aliasclr.c to runc.c
 * b004	aliasclr.c; Add NUM_NL newlines to beginning of output
 * 	Cleanup code to standards
 * b003	Work on resolving a potential overflow issue with building the
 *	command string to pass to syslinux_run_command()
 * 	Reformatted {} in more visual style.
 * 	Use MAX_CMDLINE_LEN or COMMAND_LINE_SIZE (in that order) if available
 * b002	Alter the concatenation of the command line arguments to use memcpy
 * b001	Initial version
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <consoles.h>

#ifdef __COM32__		/* Allow targetting Linux, etc to test */
#include <syslinux/boot.h>	/* syslinux_run_command() */
#else
#ifdef __linux__		/* For COMMAND_LINE_SIZE */
#include <asm/setup.h>
#endif	/* __linux__ */
#endif	/* __COM32__ */

// #define DO_DEBUG 1	/* Uncomment this for additional output */

/* Possible referenced values for command line length:
 *	MAX_CMDLINE_LEN (com32/menu/menu.h)
 *	LINE_MAX or _POSIX2_LINE_MAX	<limits.h> on *nix systems;
 *		Seem more appropriate for a shell command line
 *	COMMAND_LINE_SIZE <asm/setup.h> on a Linux system
 */
#ifdef MAX_CMDLINE_LEN
#define RUN_CMD_SZ	MAX_CMDLINE_LEN
#else
#ifdef COMMAND_LINE_SIZE
#define RUN_CMD_SZ	COMMAND_LINE_SIZE
#else
#define RUN_CMD_SZ	2048
#endif	/* COMMAND_LINE_SIZE */
#endif	/* MAX_CMDLINE_LEN */

#define NUM_NL	40
	/* Number of lines to clear */

#ifdef DO_DEBUG
# define DEBUG_PRINTF	printf
#else
# define DEBUG_PRINTF(f, ...)	((void)0)
#endif	/* DO_DEBUG */

#define APP_LONGNAME	"Run COM32"
#define APP_NAME	"run"
#define APP_YEAR	"2009"
#define APP_AUTHOR	"Gene Cumm"
#define APP_VER		"beta-b005"

int main(int argc, char *argv[])
{
	char cmdstr[RUN_CMD_SZ];	/* Command string to execute */
	int curpos;	/* Current position in cmdstr; Use memcpy rather than
		strcat */
	int arglen;	/* length of current argument string */
	int argst;	/* Starting argument to listen to */
	int i;

	/* Initialization */
	curpos = 0;
	argst = 1;
	cmdstr[0] = 0;
	console_ansi_std();
	DEBUG_PRINTF("\nMax Command line:%d\n", RUN_CMD_SZ);
	DEBUG_PRINTF("argc:%d\n\n", argc);

	if (strcmp(argv[1], "-c") == 0) {
		DEBUG_PRINTF("--Found '-c'\n");
		argst++;
		for (i = 0; i < NUM_NL; i++)
			printf("\n");
	}

	for (i = argst; i < argc; i++) {
		DEBUG_PRINTF("argv[%d]\n", i);
		arglen = strlen(argv[i]);
		/* Theoretically, this should never be met in SYSLINUX */
		if ((curpos + arglen) > (RUN_CMD_SZ - 1))
			arglen = (RUN_CMD_SZ - 1) - curpos;
		memcpy(cmdstr + curpos, argv[i], arglen);
		curpos += arglen;
		if (curpos >= (RUN_CMD_SZ - 1)) {
			/* Hopefully, curpos should not be greater than
			 * (RUN_CMD_SZ - 1)
			 * Still need a '\0' at the last character
			 */
			cmdstr[(RUN_CMD_SZ - 1)] = 0;
			break;
			/* Escape out of the for() loop;  We can no longer
			 * process anything more
			 */
		} else {
			if (i < (argc - 1)) {
				cmdstr[curpos++] = ' ';
				cmdstr[curpos] = 0;
			}
		}
	}
	DEBUG_PRINTF("Parsed arg list\n");
#ifdef __COM32__
	DEBUG_PRINTF("--run: '%s'\n", cmdstr);
	syslinux_run_command(cmdstr);
#else
	printf("--run: '%s'\n", cmdstr);
#endif	/* __COM32__ */

	return 0;
}




More information about the Syslinux mailing list