1 /*
2 	bmblib	-	SW Old BMB STDLIB routines
3 
4 	Copyright (C) 1984-2003 David L. Clark.
5 	This program is free software; you can redistribute it and/or modify it under
6 	the terms of the GNU General Public License as published by the Free Software
7 	Foundation; either version 2 of the License, or (at your option) any later
8 	version. This program is distributed in the hope that it will be useful,
9 	but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 	or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 	more details. You should have received a copy of the GNU General Public
12 	License along with this program; if not, write to the Free Software Foundation,
13 	Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
14 
15 	Modification History:
16 			94-12-19	Original development
17 			2003-01-27	GNU General Public License
18 */
19 
20 
21 #include "dos.h"
22 #include "sw.h"
23 #include "string.h"
24 
25 
26 int _systype = PCDOS;
27 
28 
29 
30 
31 
32 /*----------------------------------------------------------------------------
33 		GETFLAGS flags processing ( Jack Cole )
34 ----------------------------------------------------------------------------*/
35 
36 
37 
38 
39 getflags(ac, av, fmt, flds)
40 int  *ac;
41 char **av;
42 char *fmt;
43 int  *flds[];
44   {
45     char **arg,*apend;
46     int i,j,k;
47     register char *aptr;
48     char *flag;
49     register char *fptr;
50     int  **var;
51     int  *adrvar;
52     char *cfmt;
53     int  sts = 0;
54     int  got_next;
55 
56     arg = (char **) *av;
57     i = *ac;
58     ++arg;				/* point past program name */
59 
60     while ( --i )  {			/* for all args */
61 	aptr = *arg;			/* point at string */
62 	if ( *aptr++ != '-' ) break;    /* past the switches */
63 	if ( *aptr == '-' ) {           /* or -- at eol */
64 		++arg;			/* flush it */
65 		--i;
66 		break;
67 	}
68 
69 nextbool:
70 	flag = aptr;				/* get the switch */
71 	var = (int **) &flds;			/* find in format */
72 
73 	for (fptr = fmt; *fptr && *fptr != ':' ; ++var) {
74 	    j = 0;
75 	    while ( isalnum(*(fptr+j)) ) ++j;		/* get switch length */
76 
77 	    /* match and number or space following? */
78 
79 	    if ( !strncmp( flag, fptr, j ) && (!isalpha(*(flag+j)) ||
80 						*(fptr+j)!='#') ) break;
81 
82 	    fptr += j;					/* skip to next */
83 	    if (*fptr) ++fptr;				/* past format */
84 	}
85 
86 	if ( !(*fptr) || *fptr == ':' )  {              /* no match? */
87 	    if ( (k = index( fmt, ':')) ) {          /* find usage info */
88 		cfmt = fmt + k - 1;
89 		*cfmt++ = '\0';
90 		if ( !(*cfmt) ) {			/* return on error? */
91 			sts = 1;
92 			goto ret;
93 		}
94 	    }
95 	    exit(0);
96 	}
97 
98 	flag = fptr+j;				/* the type */
99 	aptr += j;
100 	adrvar = *var;				/* this is addr of real var */
101 	got_next = NO;
102 	if (*aptr == 0 && *flag != '&')  {      /* more expected */
103 	    if ( i > 1 ) {			/* any more args? */
104 		aptr = *(++arg);		/* step to next arg */
105 		--i;
106 		got_next = YES;
107 	    }
108 	}
109 
110 	switch	(*flag)  {			 /* what kind expected */
111 	    case '#' :
112 		j = 0;
113 		if ( *aptr ) {			/* any more chars? */
114 		    *adrvar=strtol(aptr,&apend,10);
115 		    j = apend - aptr;
116 		    aptr = apend;
117 		}
118 		if ( !j ) {			/* how many digits? */
119 			if (got_next) { 	/* none - push back? */
120 				--arg;		/* yes, push back */
121 				++i;
122 			}
123 			*(int *)adrvar = -1;	/* flag present, but no arg */
124 		}
125 		break;
126 
127 	    case '*' : *(char **)adrvar = aptr;
128 			break;
129 
130 	    case '&' : *(int *)adrvar = YES;              /* boolean */
131 			break;
132 	  }
133 
134 	if (*flag == '&' && *aptr) goto nextbool;
135 	++arg;
136     }
137 
138 ret:
139     *av = (char *)arg;				/* point past those processed */
140     *ac = i;
141     return(sts);				/* successful */
142   }
143 
144 
145 int index(char *str,int c)
146 {
147 char *s;
148 
149     return((s=strchr(str,c))==NULL?0:s-str+1);
150 }
151 
152 
153 int inportb(unsigned port)
154 {
155     return(inp(port));
156 }
157 
158 
159 void movblock(unsigned int srcoff,unsigned int srcseg,
160 	      unsigned int destoff,unsigned int destseg,
161 	      unsigned int count)
162 {
163     movedata(srcseg,srcoff,destseg,destoff,count);
164 }
165 
166 
167 
168 void movmem(void *src,void *dest,unsigned count)
169 {
170     memmove(dest,src,count);
171 }
172 
173 
174 
175 int outportb(unsigned port,int data)
176 {
177     return(outp(port,data));
178 }
179 
180 
181 void setmem(void *dest,unsigned count,int c)
182 {
183     memset(dest,c,count);
184 }
185 
186 
187 int sysint(int intnum,struct regval *inrv,struct regval *outrv)
188 {
189 union REGS regs;
190 struct SREGS segregs;
191 int rc;
192 
193     regs.x.ax=inrv->axr;
194     regs.x.bx=inrv->bxr;
195     regs.x.cx=inrv->cxr;
196     regs.x.dx=inrv->dxr;
197     segregs.ds=inrv->dsr;
198     rc=int86x(intnum,&regs,&regs,&segregs);
199     outrv->axr=regs.x.ax;
200     outrv->bxr=regs.x.bx;
201     outrv->cxr=regs.x.cx;
202     outrv->dxr=regs.x.dx;
203     outrv->dsr=segregs.ds;
204     return(rc);
205 }
206 
207 
208 int sysint21(struct regval *inrv,struct regval *outrv)
209 {
210 union REGS regs;
211 struct SREGS segregs;
212 int rc;
213 
214     regs.x.ax=inrv->axr;
215     regs.x.bx=inrv->bxr;
216     regs.x.cx=inrv->cxr;
217     regs.x.dx=inrv->dxr;
218     segregs.ds=inrv->dsr;
219     rc=intdosx(&regs,&regs,&segregs);
220     outrv->axr=regs.x.ax;
221     outrv->bxr=regs.x.bx;
222     outrv->cxr=regs.x.cx;
223     outrv->dxr=regs.x.dx;
224     outrv->dsr=segregs.ds;
225     return(rc);
226 }
227 