home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Der Mediaplex Sampler - Die 6 von Plex
/
6_v_plex.zip
/
6_v_plex
/
DISK2
/
MULTI_04
/
SHOWGL15.ZIP
/
GETARGS.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-12-27
|
7KB
|
308 lines
#pragma linesize(132)
/* getargs.c a UNIX-like switch reading routine
Copyright 1991, Robert C. Becker, Lantern Systems */
/* version 1.5
Added -db flag to turn on debugging output */
/*
valid option flags:
A 8.5 x 11 inch paper
B 11 x 17 inch paper
C 17 x 22 inch paper
D 22 x 34 inch paper
E 34 x 44 inch paper
A4 210 x 297 mm paper
A3 297 x 420 mm paper
A2 420 x 594 mm paper
A1 594 x 841 mm paper
A0 841 x 1189 mm paper
c force CGA 640 x 200 mode
e force EGA 640 x 350 mode
v force VGA 640 x 480 mode
dp assume HP DraftPro coordinate system (center of page = (0,0)
db turn on debugging output
Note that options are case sensitive.
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include "graph.h" /* need this for video device definitions */
#include "hpgl.h"
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#define ERR_F 0x1F
#define FNAME_LEN 128 /* length of longest MSDOS filename */
#define EXIT1 1
static void nfexit (int, char );
static int istok ( char *, char * );
static void clear_flags ( void );
static int setflags ( int );
static void getflags ( char * );
static char copyright[] = "Copyright 1991, Robert C. Becker, Lantern Systems";
static char cf, ef, vf, Af, Bf, Cf, Df, Ef, A4f, A3f, A2f, A1f, A0f, dbf, dpf;
static char *progname; /* pointer to program name */
static char **files;
static char *path; /* file found at requested directory path */
static char *dpath; /* directory pathname */
static char fname_bfr [FNAME_LEN]; /* handy buffer for file names */
static char tokens[] = "c e v A B C D A4 A3 A2 A1 A0 db dp";
struct _vid_x_parm_ *forced_video [] = { CGA_640x200, CGA_640x200, EGA_640x350,
VGA_640x480 }; /* use CGA_640x200 to fill space */
#define VIDEO_AUTO 0
#define CGA_mode 1
#define EGA_mode 2
#define VGA_mode 3
struct option
{
char *opt;
int length;
int value;
};
static struct option optarray[] =
{
{ "c", 1, CGA_mode}, /* these values correspond to array indices in */
{ "e", 1, EGA_mode}, /* in forced_video[] */
{ "v", 1, VGA_mode},
{ "A4", 2, A4_paper},
{ "A3", 2, A3_paper},
{ "A2", 2, A2_paper},
{ "A1", 2, A1_paper},
{ "A0", 2, A0_paper},
{ "db", 2, Debug_flag},
{ "dp", 2, DRAFTPRO},
{ "A", 1, A_paper},
{ "B", 1, B_paper},
{ "C", 1, C_paper},
{ "D", 1, D_paper},
{ "E", 1, E_paper},
{ "", 0, 0} /* end of struct */
};
static struct options cmd_options;
/*----------------------------------*/
static void nfexit (int val, char opt)
{
printf ("\n%s: ", progname);
printf ("unknown option: %c\n", opt);
printf ("usage: %s [-%s] <file>\n", progname, tokens);
exit (val);
}
/*----------------------------------*/
static int istok (char *ch, char *str) /* match flags with chars in token [] */
{
int i;
for (i = 0; ; i++)
{
if (optarray[i].opt[0] == ch[0])
{
if (optarray[i].length == 1) return (i);
if (optarray[i].opt[1] == ch[1]) return (i);
}
if ( !optarray[i].length ) return (ERR_F); /* flag not found in list */
}
}
/*----------------------------------*/
static void clear_flags ( void )
{
Af = Bf = Cf = Df = Ef = 0;
A4f = A3f = A2f = A1f = A0f = 0;
cf = ef = vf = 0;
dbf = 0;
dpf = 0;
return;
}
/*----------------------------------*/
static int setflags (int flag)
{
switch(flag)
{
case 0: cf = TRUE;
cmd_options.video = optarray[flag].value;
break;
case 1: ef = TRUE;
cmd_options.video = optarray[flag].value;
break;
case 2: vf = TRUE;
cmd_options.video = optarray[flag].value;
break;
case 3: A4f = TRUE;
cmd_options.paper = optarray[flag].value;
break;
case 4: A3f = TRUE;
cmd_options.paper = optarray[flag].value;
break;
case 5: A2f = TRUE;
cmd_options.paper = optarray[flag].value;
break;
case 6: A1f = TRUE;
cmd_options.paper = optarray[flag].value;
break;
case 7: A0f = TRUE;
cmd_options.paper = optarray[flag].value;
break;
case 8: dbf = TRUE;
cmd_options.debug = optarray[flag].value;
break;
case 9: dpf = TRUE;
cmd_options.plotter = optarray[flag].value;
break;
case 10: Af = TRUE;
cmd_options.paper = optarray[flag].value;
break;
case 11: Bf = TRUE;
cmd_options.paper = optarray[flag].value;
break;
case 12: Cf = TRUE;
cmd_options.paper = optarray[flag].value;
break;
case 13: Df = TRUE;
cmd_options.paper = optarray[flag].value;
break;
case 14: Ef = TRUE;
cmd_options.paper = optarray[flag].value;
break;
default: break;
}
return (TRUE); /* return constant value for cheap programming simplification */
}
/*----------------------------------*/
static void getflags (char *args)
{
int i, j, index, ind=FALSE;
for(i = 0; i < strlen (args); i++)
{
for (j = 0; ; j++)
{
if (optarray[j].opt[0] == args [i])
{
if (optarray[j].length == 1)
{
ind = setflags (j);
break;
}
if (optarray[j].opt[1] == args [i + 1])
{
++i; /* advance over this char */
ind = setflags (j);
break;
}
}
if ( !optarray[j].length ) nfexit (EXIT1, args[i]); /* flag not found in list */
}
}
return;
}
/*----------------------------------*/
struct options *getargs (int argc, char **argv)
{
char *s;
path = (char *) calloc (1, 215);
dpath = (char *) calloc (1, 215);
cmd_options.paper = A_paper;
cmd_options.video = 0; /* initialize video and papersize to defaults */
cmd_options.plotter = 0; /* no plotter specified */
cmd_options.debug = 0; /* no debugging output */
s = progname = *argv++;
--argc;
while (*s) /* get program name */
{
if (*s == ':' || *s == '\\' || *s == '/')
progname = s + 1;
++s;
}
strlwr (progname);
clear_flags (); /* zero all flags */
if (**argv == '-')
{
while ( *(s = *argv) && (*s == '-')) /* if we have command line flags, */
{ /*ignore environment defaults */
++argv;
--argc;
++s; /* skip leading '-' */
getflags (s);
}
}
else
{
if ((s=getenv ("HPGL")) != NULL)
{
getflags (s); /* get environment default flags */
}
}
/* check for more than one paper size selected */
if (Af + Bf + Cf + Ef + A4f + A3f + A2f + A1f + A0f > 1)
{
printf ("%s: more than one paper size options selected\n", progname);
exit (1);
}
if (cf + ef + vf > 1)
{
printf ("%s: ignoring multiple video adapter modes\n", progname);
cmd_options.video = VIDEO_AUTO; /* use automatic mode selection */
}
if (argc < 1)
{ /* use stdin */
cmd_options.infile = stdin;
}
else
{ /* get filename */
strcpy (path, *argv);
cmd_options.infile = fopen (path, "r"); /* returns NULL if no file */
}
return (&cmd_options);
}