home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 2001 January
/
VPR0101A.BIN
/
OLS
/
TAR32053
/
tar32053.exe
/
SRC
/
SETARG.C
< prev
next >
Wrap
C/C++ Source or Header
|
1999-05-23
|
5KB
|
241 lines
#ifndef __DEFCONF_H
#include "defconf.h"
#endif
/*
This file was hacked for kmtar for Windows DLL
at 1997-02-14
by tsuneo
(see setarg.h ,too)
*/
/*
* Expand indirect files yasushi@is.s.u-tokyo.ac.jp
*
* If ARGV contains "@FILE", read the content of FILE, and replace "@FILE" with
* it. Each entry is delimited with null character ('\0') in the indirect
* file.
*
* If you want to place '@' at the first of the parameter character, write '@@'.
*/
/* on LSI-C, called before stdin/out/err are fdopen'ed. */
#define NO_USE_STDIN /* for DLL */
#include <stdio.h>
#include <ctype.h>
#include <string.h> /* Add Nide */
#include <fcntl.h> /* Add Nide */
/* #include "wild.h"*/ /* Add Nide */
#include "defs.h" /* for iskanji/iskanji2 Add tsuneo */
#define _expand_abendcode 1
#ifndef LSI_C /* Add Nide */
#define is_space(c) (isascii(c) && isspace(c))
#else
#define is_space(c) isspace(c)
#endif
/* by tsuneo */
static void _putnomemmes(void)
{
fprintf(stderr,"error: can't allocate memory at setarg.c\n");
}
static char **args, **args_head=NULL;
static int lastsize = 30;
static void add_args(s)
char *s;
{
void *malloc(), *realloc();
char *strdup();
#ifdef DEBUG
_puterrmes("before add_args:\r\n");
{char **p; if(args_head) for(p = args_head; p < args; p++){
_puterrmes(*p ? *p : "(NULL)");
_puterrmes("\r\n");
}}
_puterrmes("add_args "), _puterrmes(s ? s : "(NULL)");
_puterrmes("\r\n");
#endif
if(args == 0){
args = args_head =
(char **) realloc(args_head,(lastsize) * sizeof(char *));
/* change malloc->realloc by tsuneo*/
if(args == 0){
memfull:
_putnomemmes(), exit(_expand_abendcode);
}
}
if(args - args_head >= lastsize){
char **new = (char **) realloc(
args_head, (lastsize += 40) * sizeof(char *));
/* DEBUG Nide */
if(new == 0)
goto memfull;
args = new + (args - args_head);
args_head = new;
}
*args++ = s;
}
static void arg_initialize()
{
args = 0;
if(args_head!=NULL){free(args_head);args_head = 0;} /* changed by tsuneo */
lastsize = 30;
}
static void check_arg(arg)
char *arg;
{
int fp, fprpos, fprsiz;
unsigned char fprbuf[BUFSIZ];
if(*arg != '@'){
add_args(arg);
return;
} else {
int ch;
if(arg[1] == '@'){
add_args(arg + 1);
return;
}
if(arg[1] == '-' && !arg[2]){ /* Nide */
#ifdef NO_USE_STDIN
fputs("Can't use @- (input from stdin)\n",stderr);
exit(_expand_abendcode);
#else
fp = 0;
#endif
} else if((fp = open(arg + 1, O_RDONLY)) < 0){
#ifdef LSI_C
_puterrmes("Can't open the indirect file.\r\n");
#else
fputs("Can't open the indirect file.\n", stderr);
#endif
exit(_expand_abendcode);
}
fprpos = fprsiz = 0;
do {
char buf[BUFSIZ], *bp;
char *x;
int quote_in=0; /* quote_in=1 at processing quote */
#define fpgetc(fp) ( \
(fprpos == fprsiz ? (fprpos = 0, fprsiz = read(fp, fprbuf, BUFSIZ)) : fprsiz) \
<= 0 ? ((fprsiz = -1), EOF) : fprbuf[fprpos++])
#define fpungetc(ch, fp) (fprsiz > 0 && fprpos--)
/* ch, fp not used. don't call it twice */
bp = buf;
while((ch = fpgetc(fp)) != EOF && !(quote_in==0 && is_space(ch))){
#ifdef DEBUG
char p[0];
*p = ch, p[1] = 0;
_puterrmes("got '"), _puterrmes(p);
_puterrmes("'\r\n");
#endif
if(ch=='"'){
if(quote_in==0){
quote_in=1; /* changed from quote==1 ,1997/09/02 by tsuneo by yasue*/
}else{
quote_in=0;
}
}else if(iskanji(ch)){
int ch2;
ch2=fpgetc(fp);
if(ch2!=EOF && iskanji2(ch2)){
*bp++=ch;
*bp++=ch2;
}else{
*bp++=ch;
if(ch2==EOF){break;}
fpungetc(ch2,fp);
}
}else{
*bp++ = ch;
}
}
/* ignore null strings. */
if(bp == buf)
continue;
*bp = '\0';
x = strdup(buf);
if(x == 0){
_putnomemmes(), exit(_expand_abendcode);
}
#ifdef DEBUG
_puterrmes("Adding "), _puterrmes(x);
_puterrmes("\r\n");
#endif
add_args(x);
while(is_space(ch) && ch != EOF)
ch = fpgetc(fp);
fpungetc(ch, fp);
} while(ch != EOF);
if(!fp){ /* Nide */
#ifndef LSI_C
if(NULL == freopen("con", "r", stdin))
#else
close(0);
if(0 != open("con", O_RDONLY))
#endif
{
#ifdef LSI_C
_puterrmes("? Can't open con\r\n");
#else
fputs("? Can't open con\n", stderr);
#endif
exit(_expand_abendcode);
}
} else
close(fp);
}
}
void setup_arguments(argcp, argvp)
int *argcp;
char ***argvp;
{
int argc = *argcp;
char **argv = *argvp;
int i; /* char **c deleted by Nide */
arg_initialize();
for(i = 0; i < argc; i++){
check_arg(argv[i]);
}
add_args((char *) 0);
*argcp = args - args_head - 1; /* -1 for null delimiter above */
*argvp = args_head;
}
/*
void setup_argument(pat, argcp, argvp)
char *pat;
int *argcp;
char ***argvp;
{
arg_initialize();
check_arg(pat);
add_args((char *) 0);
*argcp = args - args_head - 1;
*argvp = args_head;
}
*/
#ifdef TEST
main(argc, argv)
int argc;
char **argv;
{
setup_arguments(&argc, &argv);
while(*argv){
printf("[%s]\n", *argv);
argv++;
}
}
#endif