Ejemplo n.º 1
0
Archivo: sed.c Proyecto: DeadZen/qse
static int handle_args (int argc, qse_char_t* argv[])
{
	static qse_opt_lng_t lng[] = 
	{
#if defined(QSE_CHAR_IS_WCHAR)
		{ QSE_T(":script-encoding"),  QSE_T('\0') },
		{ QSE_T(":infile-encoding"),  QSE_T('\0') },
		{ QSE_T(":outfile-encoding"), QSE_T('\0') },
#endif

		{ QSE_T("version"),          QSE_T('\0') },
		{ QSE_T("help"),             QSE_T('h') },
		{ QSE_NULL,                  QSE_T('\0') }                  
	};
	static qse_opt_t opt = 
	{
#if defined(QSE_BUILD_DEBUG)
		QSE_T("hne:f:o:rRisabxytm:wX:"),
#else
		QSE_T("hne:f:o:rRisabxytm:w"),
#endif
		lng
	};
	qse_cint_t c;

	while ((c = qse_getopt (argc, argv, &opt)) != QSE_CHAR_EOF)
	{
		switch (c)
		{
			default:
				print_usage (QSE_STDERR, argc, argv);
				goto oops;

			case QSE_T('?'):
				qse_fprintf (QSE_STDERR, 
					QSE_T("ERROR: bad option - %c\n"),
					opt.opt
				);
				print_usage (QSE_STDERR, argc, argv);
				goto oops;

			case QSE_T(':'):
				qse_fprintf (QSE_STDERR, 
					QSE_T("ERROR: bad parameter for %c\n"),
					opt.opt
				);
				print_usage (QSE_STDERR, argc, argv);
				goto oops;

			case QSE_T('h'):
				print_usage (QSE_STDOUT, argc, argv);
				goto done;

			case QSE_T('n'):
				g_option |= QSE_SED_QUIET;
				break;

			case QSE_T('e'):
				if (add_script (opt.arg, 1) <= -1) goto oops;
				break;

			case QSE_T('f'):
				if (add_script (opt.arg, 0) <= -1) goto oops;
				break;

			case QSE_T('o'):
				g_output_file = opt.arg;
				break;

			case QSE_T('r'):
				g_option |= QSE_SED_EXTENDEDREX;
				break;

			case QSE_T('R'):
				g_option |= QSE_SED_NONSTDEXTREX;
				break;

			case QSE_T('i'):
				/* 'i' implies 's'. */
				g_inplace = 1;

			case QSE_T('s'):
				g_separate = 1;
				break;
		
			case QSE_T('a'):
				g_option |= QSE_SED_STRICT;
				break;

			case QSE_T('b'):
				g_option |= QSE_SED_EXTENDEDADR;
				break;

			case QSE_T('x'):
				g_option |= QSE_SED_SAMELINE;
				break;

			case QSE_T('y'):
				g_option |= QSE_SED_ENSURENL;
				break;

			case QSE_T('t'):
#if defined(QSE_ENABLE_SEDTRACER)
				g_trace = 1;
				break;
#else
				print_usage (QSE_STDERR, argc, argv);
				goto oops;
#endif
 
			case QSE_T('m'):
				g_memlimit = qse_strtoulong (opt.arg, 10);
				break;

			case QSE_T('w'):
				g_wildcard = 1;
				break;

#if defined(QSE_BUILD_DEBUG)
			case QSE_T('X'):
				g_failmalloc = qse_strtoulong (opt.arg, 10);
				break;
#endif

			case QSE_T('\0'):
			{
				if (qse_strcmp(opt.lngopt, QSE_T("version")) == 0)
				{
					print_version ();
					goto done;
				}
				else if (qse_strcmp(opt.lngopt, QSE_T("script-encoding")) == 0)
				{
					g_script_cmgr = qse_findcmgr (opt.arg);
					if (g_script_cmgr == QSE_NULL)
					{
						qse_fprintf (QSE_STDERR, QSE_T("ERROR: unknown script encoding - %s\n"), opt.arg);
						goto oops;
					}
				}
				else if (qse_strcmp(opt.lngopt, QSE_T("infile-encoding")) == 0)
				{
					g_infile_cmgr = qse_findcmgr (opt.arg);
					if (g_infile_cmgr == QSE_NULL)
					{
						qse_fprintf (QSE_STDERR, QSE_T("ERROR: unknown input file encoding - %s\n"), opt.arg);
						goto oops;
					}
				}
				else if (qse_strcmp(opt.lngopt, QSE_T("outfile-encoding")) == 0)
				{
					g_outfile_cmgr = qse_findcmgr (opt.arg);
					if (g_outfile_cmgr == QSE_NULL)
					{
						qse_fprintf (QSE_STDERR, QSE_T("ERROR: unknown output file encoding - %s\n"), opt.arg);
						goto oops;
					}
				}
				break;
			}

		}
	}

	if (opt.ind < argc && g_script.size <= 0) 
	{
		if (add_script (argv[opt.ind++], 1) <= -1) goto oops;
	}
	if (opt.ind < argc) g_infile_pos = opt.ind;

	if (g_script.size <= 0)
	{
		print_usage (QSE_STDERR, argc, argv);
		goto oops;
	}

	g_script.io[g_script.size].type = QSE_SED_IOSTD_NULL;
	return 1;

oops:
	free_scripts ();
	return -1;

done:
	free_scripts ();
	return 0;
}
Ejemplo n.º 2
0
Archivo: awk25.cpp Proyecto: apense/qse
static int handle_cmdline (
	MyAwk& awk, int argc, qse_char_t* argv[], cmdline_t* cmdline)
{
	static qse_opt_t opt =
	{
		QSE_T("hF:f:d:o:"),
		QSE_NULL
	};
	qse_cint_t c;

	std::memset (cmdline, 0, QSE_SIZEOF(*cmdline));
	while ((c = qse_getopt (argc, argv, &opt)) != QSE_CHAR_EOF)
	{
		switch (c)
		{
			case QSE_T('h'):
				print_usage (QSE_STDOUT, argv[0]);
				return 0;

			case QSE_T('F'):
				cmdline->fs = opt.arg;
				break;

			case QSE_T('f'):
				cmdline->inf = opt.arg;
				break;

			case QSE_T('d'):
				cmdline->outf = opt.arg;
				break;

			case QSE_T('o'):
				cmdline->outc = opt.arg;
				break;

			case QSE_T('?'):
				print_error (QSE_T("illegal option - '%c'\n"), opt.opt);
				return -1;

			case QSE_T(':'):
				print_error (QSE_T("bad argument for '%c'\n"), opt.opt);
				return -1;

			default:
				print_usage (QSE_STDERR, argv[0]);
				return -1;
		}
	}

	if (opt.ind < argc && !cmdline->inf)
		cmdline->ins = argv[opt.ind++];

	while (opt.ind < argc)
	{
		if (awk.addArgument (argv[opt.ind++]) <= -1) 
		{
			print_error (awk);
			return -1;
		}
	}

	if (!cmdline->ins && !cmdline->inf)
	{
		print_usage (QSE_STDERR, argv[0]);
		return -1;
	}

	return 1;
}