mirror of
https://github.com/reactos/reactos.git
synced 2024-12-26 00:54:40 +00:00
- string-type-generic getopt() (hacked together in half a hour - seems to work fine, though). Very simple - only implements the POSIX standard (no long options, etc.)
- modified tlist to use getopt() to parse the command line, just for fun. Seems to work svn path=/trunk/; revision=4560
This commit is contained in:
parent
aac3b7cd7b
commit
c1ce9a4b84
7 changed files with 195 additions and 80 deletions
|
@ -1,53 +1,3 @@
|
|||
/* temporary header for getopt. Please remove this file when MingW ships with
|
||||
its own */
|
||||
|
||||
#ifndef __GETOPT_H_INCLUDED
|
||||
#define __GETOPT_H_INCLUDED
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern char *optarg;
|
||||
extern int optind, opterr, optopt;
|
||||
|
||||
#define no_argument (0)
|
||||
#define required_argument (1)
|
||||
#define optional_argument (2)
|
||||
|
||||
struct option
|
||||
{
|
||||
const char *name;
|
||||
int has_arg;
|
||||
int *flag;
|
||||
int val;
|
||||
};
|
||||
|
||||
extern int getopt(int, char * const [], const char *);
|
||||
|
||||
extern int getopt_long
|
||||
(
|
||||
int,
|
||||
char * const [],
|
||||
const char *,
|
||||
const struct option *,
|
||||
int *
|
||||
);
|
||||
|
||||
extern int getopt_long_only
|
||||
(
|
||||
int,
|
||||
char * const [],
|
||||
const char *,
|
||||
const struct option *,
|
||||
int *
|
||||
);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __GETOPT_H_INCLUDED */
|
||||
#include <tgetopt.h>
|
||||
|
||||
/* EOF */
|
||||
|
|
|
@ -29,9 +29,9 @@
|
|||
* DISCLAIMED. This includes but is not limited to warranties of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* $Revision: 1.7 $
|
||||
* $Revision: 1.8 $
|
||||
* $Author: hyperion $
|
||||
* $Date: 2003/02/09 21:15:29 $
|
||||
* $Date: 2003/04/22 03:20:25 $
|
||||
*
|
||||
*/
|
||||
|
||||
|
@ -52,8 +52,8 @@
|
|||
#ifndef _TCHAR_DEFINED
|
||||
#ifndef RC_INVOKED
|
||||
typedef wchar_t _TCHAR;
|
||||
typedef signed wchar_t _TSCHAR;
|
||||
typedef unsigned wchar_t _TUCHAR;
|
||||
typedef wchar_t _TSCHAR;
|
||||
typedef wchar_t _TUCHAR;
|
||||
typedef wchar_t _TXCHAR;
|
||||
/* #if !__STDC__ */
|
||||
typedef wchar_t TCHAR;
|
||||
|
|
19
reactos/lib/tgetopt/Makefile
Normal file
19
reactos/lib/tgetopt/Makefile
Normal file
|
@ -0,0 +1,19 @@
|
|||
# $Id: Makefile,v 1.1 2003/04/22 03:20:25 hyperion Exp $
|
||||
|
||||
PATH_TO_TOP = ../..
|
||||
|
||||
TARGET_TYPE = library
|
||||
|
||||
TARGET_NAME = tgetopt
|
||||
|
||||
TARGET_OBJECTS = getopt.o _wgetopt.o
|
||||
|
||||
DEP_OBJECTS = $(TARGET_OBJECTS)
|
||||
|
||||
include $(PATH_TO_TOP)/rules.mak
|
||||
|
||||
include $(TOOLS_PATH)/helper.mk
|
||||
|
||||
include $(TOOLS_PATH)/depend.mk
|
||||
|
||||
# EOF
|
15
reactos/lib/tgetopt/_wgetopt.c
Normal file
15
reactos/lib/tgetopt/_wgetopt.c
Normal file
|
@ -0,0 +1,15 @@
|
|||
/* $Id: _wgetopt.c,v 1.1 2003/04/22 03:20:25 hyperion Exp $
|
||||
*/
|
||||
/*
|
||||
tgetopt -- POSIX-compliant implementation of getopt() with string-type-generic
|
||||
semantics
|
||||
|
||||
This is public domain software
|
||||
*/
|
||||
|
||||
#include <wchar.h>
|
||||
|
||||
#define _UNICODE
|
||||
#include "getopt.c"
|
||||
|
||||
/* EOF */
|
135
reactos/lib/tgetopt/getopt.c
Normal file
135
reactos/lib/tgetopt/getopt.c
Normal file
|
@ -0,0 +1,135 @@
|
|||
/* $Id: getopt.c,v 1.1 2003/04/22 03:20:25 hyperion Exp $
|
||||
*/
|
||||
/*
|
||||
tgetopt -- POSIX-compliant implementation of getopt() with string-type-generic
|
||||
semantics
|
||||
|
||||
This is public domain software
|
||||
*/
|
||||
|
||||
#include <tchar.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <tgetopt.h>
|
||||
|
||||
int _topterr = 1;
|
||||
int _toptind = 1;
|
||||
int _toptopt;
|
||||
_TCHAR * _toptarg;
|
||||
|
||||
int _tgetopt(int argc, _TCHAR * const argv[], const _TCHAR * optstring)
|
||||
{
|
||||
static int s_nArgChar = 0;
|
||||
_TCHAR * pcOptChar;
|
||||
|
||||
/* we're done */
|
||||
if(_toptind >= argc) return -1;
|
||||
|
||||
/* last time we reached the end of argv[_toptind] */
|
||||
if(s_nArgChar != 0 && argv[_toptind][s_nArgChar] == 0)
|
||||
{
|
||||
/* scan the next argument */
|
||||
++ _toptind;
|
||||
|
||||
/* we're done */
|
||||
if(_toptind >= argc) return -1;
|
||||
|
||||
s_nArgChar = 0;
|
||||
}
|
||||
|
||||
/* first time we scan argv[_toptind] */
|
||||
if(s_nArgChar == 0)
|
||||
{
|
||||
/* argument is NULL - we're done */
|
||||
if(argv[_toptind] == NULL)
|
||||
return (int)-1;
|
||||
/* argument is empty - we're done */
|
||||
else if(argv[_toptind][0] == 0)
|
||||
return (int)-1;
|
||||
/* argument begins with '-' */
|
||||
else if(argv[_toptind][0] == _T('-'))
|
||||
{
|
||||
/* argument is "--" */
|
||||
if(argv[_toptind][1] == _T('-'))
|
||||
{
|
||||
/* increment optind */
|
||||
++ _toptind;
|
||||
s_nArgChar = 0;
|
||||
|
||||
/* we're done */
|
||||
return (int)-1;
|
||||
}
|
||||
/* argument is "-" */
|
||||
else if(argv[_toptind][1] == 0)
|
||||
{
|
||||
/* we're done */
|
||||
return (int)-1;
|
||||
}
|
||||
/* possible option */
|
||||
else
|
||||
++ s_nArgChar;
|
||||
}
|
||||
/* argument doesn't begin with a dash - we're done */
|
||||
else
|
||||
return (int)-1;
|
||||
}
|
||||
|
||||
/* return the current character */
|
||||
_toptopt = argv[_toptind][s_nArgChar];
|
||||
|
||||
/* advance to the next character */
|
||||
++ s_nArgChar;
|
||||
|
||||
/* unrecognized option */
|
||||
if(_toptopt == _T(':') || (pcOptChar = _tcschr(optstring, _toptopt)) == NULL)
|
||||
{
|
||||
/* print an error message */
|
||||
if(_topterr && optstring[0] != _T(':'))
|
||||
_ftprintf(stderr, _T("%s: illegal option -- %c\n"), argv[0], _toptopt);;
|
||||
|
||||
/* return an error */
|
||||
return _T('?');
|
||||
}
|
||||
|
||||
/* the option requires an argument */
|
||||
if(pcOptChar[1] == _T(':'))
|
||||
{
|
||||
/* we are at the end of the argument */
|
||||
if(argv[_toptind][s_nArgChar] == 0)
|
||||
{
|
||||
/* the argument of the option is the next argument */
|
||||
++ _toptind;
|
||||
s_nArgChar = 0;
|
||||
|
||||
/* this is the last argument */
|
||||
if(_toptopt >= argc)
|
||||
{
|
||||
/* print an error message */
|
||||
if(_topterr && optstring[0] != _T(':'))
|
||||
{
|
||||
_ftprintf
|
||||
(
|
||||
stderr,
|
||||
_T("%s: option requires an argument -- %c\n"),
|
||||
argv[0],
|
||||
_toptopt
|
||||
);
|
||||
}
|
||||
|
||||
/* return an error */
|
||||
return ((optstring[0] == _T(':')) ? _T(':') : _T('?'));
|
||||
}
|
||||
|
||||
/* return the argument */
|
||||
_toptarg = argv[_toptind];
|
||||
}
|
||||
/* the rest of the argument is the argument of the option */
|
||||
else
|
||||
_toptarg = argv[_toptind] + s_nArgChar;
|
||||
}
|
||||
|
||||
/* success */
|
||||
return _toptopt;
|
||||
}
|
||||
|
||||
/* EOF */
|
|
@ -12,7 +12,7 @@ TARGET_APPTYPE = console
|
|||
|
||||
TARGET_NAME = tlist
|
||||
|
||||
TARGET_SDKLIBS = epsapi.a kernel32.a user32.a ntdll.a
|
||||
TARGET_SDKLIBS = epsapi.a tgetopt.a kernel32.a user32.a ntdll.a
|
||||
|
||||
TARGET_OBJECTS = $(TARGET_NAME).o
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $Id: tlist.c,v 1.4 2003/04/14 01:19:08 hyperion Exp $
|
||||
/* $Id: tlist.c,v 1.5 2003/04/22 03:20:25 hyperion Exp $
|
||||
*
|
||||
* ReactOS Project
|
||||
* TList
|
||||
|
@ -15,6 +15,7 @@
|
|||
#include <ctype.h>
|
||||
|
||||
#include <epsapi.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#ifndef PAGE_SIZE
|
||||
#define PAGE_SIZE 4096
|
||||
|
@ -68,14 +69,14 @@ int STDCALL PrintBanner (VOID)
|
|||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int STDCALL PrintSynopsys (VOID)
|
||||
int STDCALL PrintSynopsys (int nRetVal)
|
||||
{
|
||||
PrintBanner ();
|
||||
printf ("Usage: tlist [-t | PID | -l]\n\n"
|
||||
" -t print the task list tree\n"
|
||||
" PID print module information for this ID\n"
|
||||
" -l print license information\n");
|
||||
return EXIT_SUCCESS;
|
||||
return nRetVal;
|
||||
}
|
||||
|
||||
int STDCALL PrintLicense (VOID)
|
||||
|
@ -441,29 +442,24 @@ int STDCALL PrintProcess (char * PidStr)
|
|||
|
||||
int main (int argc, char * argv [])
|
||||
{
|
||||
if (1 == argc)
|
||||
int c;
|
||||
|
||||
if(1 == argc) return PrintProcessList(FALSE);
|
||||
|
||||
while((c = getopt(argc, argv, "tl")) != -1)
|
||||
{
|
||||
return PrintProcessList (FALSE);
|
||||
}
|
||||
if (2 == argc)
|
||||
switch(c)
|
||||
{
|
||||
if (('-' == argv [1][0]) && ('\0' == argv [1][2]))
|
||||
{
|
||||
if ('t' == argv [1][1])
|
||||
{
|
||||
return PrintProcessList (TRUE);
|
||||
}
|
||||
if ('l' == argv [1][1])
|
||||
{
|
||||
return PrintLicense ();
|
||||
case 't': return PrintProcessList(TRUE);
|
||||
case 'l': return PrintLicense();
|
||||
default: return PrintSynopsys(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
if (isdigit(argv[1][0]))
|
||||
{
|
||||
|
||||
if(isdigit(argv[optind][0]))
|
||||
return PrintProcess (argv[1]);
|
||||
}
|
||||
}
|
||||
return PrintSynopsys ();
|
||||
|
||||
return PrintSynopsys(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
/* EOF */
|
||||
|
|
Loading…
Reference in a new issue