1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
/* G C M D L I N -- gkermit command line parser */
/*
Author:
Frank da Cruz
The Kermit Project
Columbia University
612 West 115th Street
New York NY 10025-7799 USA
http://www.columbia.edu/kermit/
kermit@columbia.edu
Copyright (C) 1999,
The Trustees of Columbia University in the City of New York.
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; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include "gkermit.h"
/* Externals */
extern int nfils, parity, text, backup, rpsiz, urpsiz, timint;
extern int literal, quiet, keep, streamok, nomodes, manual, xonxoff, noxonxoff;
extern char ttname[], *cmerrp, *cmarg, *cmarg2;
extern FILE * db;
/* Variables exported from this module */
extern char **cmlist; /* Pointer to file list in argv */
extern char **xargv; /* Global copies of argv */
extern int xargc; /* and argc */
/* Variables and symbols local to this module */
static int action = 0; /* Action selected on command line */
_MYPROTOTYPE( static int doarg, (char) );
_MYPROTOTYPE( VOID fatal, (char *) );
_MYPROTOTYPE( VOID usage, (void) );
#ifndef NOGETENV
_MYPROTOTYPE( char * getenv, (char *) );
#define GARGC 32
#define GBUFSIZ 256
static char gbuf[GBUFSIZ], *gargs[GARGC], *gptr = NULL;
static int gargc;
#endif /* NOGETENV */
int /* Command-line parser */
cmdlin() {
char c;
int x;
#ifndef NOGETENV
char * p = NULL;
#endif /* NOGETENV */
cmarg = ""; /* Initialize results */
cmlist = NULL;
action = 0;
#ifndef NOGETENV
if ((p = getenv("GKERMIT"))) {
int i, xc, flag = 0;
char **xv = NULL;
strncpy(gbuf,p,GBUFSIZ-1); /* Make a pokeable copy */
gbuf[GBUFSIZ-1] = NUL;
gptr = p;
p = gbuf;
/* Turn it into an argument vector */
for (i = 0; gbuf[i] && i < GBUFSIZ && gargc < GARGC; i++) {
if (!flag) {
if (gbuf[i] <= SP)
continue;
flag = 1;
gargs[gargc++] = &gbuf[i];
} else if (flag && gbuf[i] <= SP) {
gbuf[i] = NUL;
flag = 0;
continue;
}
}
xv = xargv; /* Save original argument vector */
xc = xargc;
xargv = gargs; /* Redirect it to the one we made */
xargc = gargc;
while (xargc-- > 0) { /* Go through the words */
if (**xargv == '-') { /* Got an option (begins with dash) */
c = *(*xargv+1); /* Get the option letter */
x = doarg(c); /* Go handle the option */
if (x < 0) doexit(1);
} else { /* No dash where expected */
fprintf(stderr,
"?GKERMIT variable option error: \"%s\"\n",
*xargv
);
usage(); /* Give usage message */
doexit(0);
}
xargv++;
}
xargv = xv; /* Restore original argument vector */
xargc = xc;
}
#endif /* NOGETENV */
while (--xargc > 0) { /* Go through command line words */
xargv++;
if (**xargv == '-') { /* Got an option (begins with dash) */
c = *(*xargv+1); /* Get the option letter */
x = doarg(c); /* Go handle the option */
if (x < 0) doexit(1);
} else { /* No dash where expected */
fprintf(stderr,"?Command-line option error: \"%s\"\n", *xargv);
usage(); /* Give usage message */
doexit(1);
}
}
return(action); /* Then do any requested protocol */
}
/* D O A R G -- Do a command-line argument. */
static int
#ifdef __STDC__
doarg(char x)
#else
doarg(x) char x;
#endif /* __STDC__ */
{
int z; char *xp, **p;
xp = *xargv+1; /* Pointer for bundled args */
while (x) {
switch (x) {
case 'r': /* Receive */
if (action) fatal("conflicting actions");
action = 'v';
break;
case 's': /* Send */
if (action) fatal("conflicting actions");
if (*(xp+1)) fatal("invalid argument bundling after -s");
nfils = 0; /* Initialize file counter, flag */
cmlist = xargv+1; /* Remember this pointer */
if (zchki(*cmlist) < 0)
fatal("file not found or not accessible");
while (--xargc > 0) { /* Traverse the list */
*xargv++;
if (**xargv == '-')
break;
nfils++;
}
xargc++, *xargv--; /* Adjust argv/argc */
if (nfils < 1) fatal("missing filename for -s");
action = 's';
break;
case 'g': /* get */
if (action) fatal("conflicting actions");
if (*(xp+1)) fatal("invalid argument bundling after -g");
*xargv++, xargc--;
if ((xargc == 0) || (**xargv == '-'))
fatal("missing filename for -g");
cmarg = *xargv;
action = 'r';
break;
case 'h': /* Help */
case '?':
usage();
doexit(0);
case 'i': /* Binary (image) file transfer */
manual = 1;
text = 0;
break;
case 'T': /* Text file transfer */
manual = 1;
text = 1;
break;
case 'p': /* Parity */
if (*(xp+1)) fatal("invalid argument bundling");
*xargv++, xargc--;
if ((xargc < 1) || (**xargv == '-'))
fatal("missing parity");
switch(x = **xargv) {
case 'e': /* Even */
case 'o': /* Odd */
case 'm': /* Mark */
case 's': parity = x; break; /* Space */
case 'n': parity = 0; break; /* None */
default: fatal("invalid parity");
}
break;
case 'w': /* Writeover */
backup = 0; /* Don't back up existing files */
break;
case 'd': /* Debug */
p = xargv;
*p++;
if ((xargc < 2) || (**p == '-')) {
db = fopen("debug.log","w");
} else {
*xargv++, xargc--;
db = fopen(*xargv,"w");
}
if (db) {
extern char * versio, * build;
if (!versio) versio = "";
if (!build) build = "";
debug = 1;
setbuf(db,NULL);
fprintf(db,"%s: %s\n",
(*versio ? versio : "GKERMIT VERSION UNKNOWN"),
(*build ? build : "BUILD UNKNOWN")
);
fprintf(db,"MAXPATHLEN = %d\n",MAXPATHLEN);
if (gptr) fprintf(db,"GKERMIT=\"%s\"\n",gptr);
}
break;
case 'a': /* As-name */
if (*(xp+1)) fatal("invalid argument bundling after -a");
*xargv++, xargc--;
if ((xargc == 0) || (**xargv == '-'))
fatal("missing name for -a");
cmarg2 = *xargv;
if (debug) fprintf(db,"as-name: %s\n",cmarg2);
break;
case 'e':
if (*(xp+1)) fatal("invalid argument bundling after -e");
xargv++, xargc--;
if ((xargc < 1) || (**xargv == '-'))
fatal("missing length");
z = atoi(*xargv); /* Convert to number. */
if (z >= 40 && z <= MAXRP) {
rpsiz = urpsiz = z;
if (z > 94) rpsiz = 94; /* Fallback if other Kermit can't */
} else { /* do long packets. */
fatal("Unsupported packet length");
}
break;
case 'b': /* Timeout */
if (*(xp+1)) fatal("invalid argument bundling after -b");
xargv++, xargc--;
if ((xargc < 1) || (**xargv == '-'))
fatal("missing value");
z = atoi(*xargv); /* Convert to number */
if (z < 0) z = 0;
if (z > 90) z = 90;
timint = z;
break;
case 'P': /* Path (file) names literal */
literal = 1;
break;
case 'q': /* Quiet */
quiet = 1;
break;
case 'K': /* Keep incompletely received files */
keep = 1;
break;
case 'S': /* Disable streaming */
streamok = -1;
break;
case 'X': /* gkermit is an external protocol */
quiet = 1; /* No messages */
nomodes = 1; /* Don't set tty modes */
break;
case 'x': /* Force Xon/Xoff */
xonxoff = 1;
noxonxoff = 0;
break;
case '-': /* Don't force Xon/Xoff */
if (*(xp+1) == 'x') {
xonxoff = 0;
noxonxoff = 1;
} else {
fatal("invalid argument, type 'gkermit -h' for help");
}
xp++;
break;
default: /* Anything else */
fatal("invalid argument, type 'gkermit -h' for help");
}
x = *++xp; /* See if options are bundled */
}
if (debug) {
if (action)
fprintf(db,"cmdlin action = %c\n",action);
else
fprintf(db,"cmdlin action = (none)\n");
}
return(action);
}
VOID
fatal(msg) char *msg; { /* Fatal error message */
fprintf(stderr,"\r\nFatal: %s",msg); /* doexit supplies crlf.. */
doexit(1); /* Exit indicating failure */
}
VOID
usage() {
extern char * versio, * build, * url, * email;
if (!versio) versio = "";
if (!*versio) versio = "gkermit UNKNOWN VERSION";
if (!build) build = "UNKNOWN BUILD";
if (!url) url = "";
if (!email) email = "";
fprintf(stderr,"%s: %s.\n",versio,build);
fprintf(stderr,"Usage: gkermit [ options ]\n");
fprintf(stderr,"Options:\n");
fprintf(stderr," -r Receive files\n");
fprintf(stderr," -s fn Send files\n");
fprintf(stderr," -g fn Get files from server\n");
fprintf(stderr," -a fn As-name for single file\n");
fprintf(stderr," -i Image (binary) mode transfer\n");
fprintf(stderr," -T Text mode transfer\n");
fprintf(stderr," -P Path/filename conversion disabled\n");
fprintf(stderr," -w Write over existing files with same name\n");
fprintf(stderr," -K Keep incompletely received files\n");
fprintf(stderr," -p x Parity: x = o[dd],e[ven],m[ark],s[pace],n[one]\n")
;
fprintf(stderr," -e n Receive packet-length (40-%d)\n",MAXRP);
fprintf(stderr," -b n Timeout (sec, 0 = none)\n");
fprintf(stderr," -x Force Xon/Xoff (--x = Don't force Xon/Xoff)\n");
fprintf(stderr," -S Disable streaming\n");
fprintf(stderr," -X External protocol\n");
fprintf(stderr," -q Quiet (suppress messages)\n");
fprintf(stderr," -d [fn] Debug to ./debug.log [or specified file]\n");
fprintf(stderr," -h Help (this message)\n");
if (*url || *email)
fprintf(stderr,"More info: %s <%s>",url,email);
}
|