home *** CD-ROM | disk | FTP | other *** search
- /* parse.c - Parser for Report tool.
- This module is part of report.exe
-
- Language = Microsoft C version 4.0
-
-
- This source is distributed freely and may be copied and
- redistributed with the following provisos:
-
- You may not sell it, nor may you charge for making
- copies beyond the actual cost of mailing and media.
-
- Written by Skip Hansen WB6YMH and Harold Price NK6K.
-
- Feedback is desired.
-
- RCP/M (213) 541-2503 300/1200/2400 baud
- or via packet WB6YMH @ WB6YMH-2 or
- NK6K @ NK6K
-
- Modification history:
-
- 8/10/87 NK6K: Initial release.
- ver 1.0
-
- 10/18/87 NK6K: First general release.
- ver 1.1
-
-
- Notes: This was a quick grab from something else that was
- laying around, and has several hooks for things
- which aren't actually here. On the other hand, it
- does the job. To add new commands, add the text to
- parse.h and a new case in doit() below.
-
- */
-
-
- #include "parse.h"
- #include <ctype.h>
- #include <stdio.h>
- #include <dos.h>
-
-
- #define promptlen 4;
-
- int ars,point,ppoint,i,tc;
- unsigned int pnum;
- int old_more;
-
- char token[129];
- char buf[128];
- int ctype;
- int ltype;
- int first_com_g;
-
- int tflag,ok,eh;
- #define true 1
- #define false 0
- int point;
-
- enum ttypes { notoken,
- alftoken,
- numtoken,
- qmarktoken, /* ? */
- cmarktoken, /* ; */
- qtmarktoken, /* " */
- errtoken} typtoken;
-
- char oplist[] = " ?;\"";
-
- FILE *fin, *fout, *fopen();
- int in_open = 0;
- int out_open = 0;
-
-
-
- /* global defs */
- char sel_call[11];
- int sel_flag=0;
- char sel_types[0]="CDFIT";
- unsigned long recnum;
-
- int sp_skip(i)
- int i;
- {
- int looping;
- looping = true;
- point = i;
- while (looping) {
- if (point >=ars) looping = false;
- else if(buf[point]==' ') point++;
- else looping = false;
- }
- return(point<ars);
- }
-
-
-
- ptoken(npoint)
- int npoint;
- {
- int i,looping,itemp;
- char c;
- memset(token,' ',127);
- looping = sp_skip(npoint);
- ppoint = point;
- pnum = 0;
- tc = -1;
- typtoken = notoken;
- /* get token first */
- /* check for opcodes */
- if (looping) {
- typtoken=errtoken; /* once started, it isn't notoken */
- c = buf[point];
- if(c=='?') {
- tc++;
- point++;
- looping = false;
- typtoken=qmarktoken;
- }
- else if(c==';') {
- tc++;
- point++;
- looping = false;
- typtoken=cmarktoken;
- }
- else if(c=='"') {
- point++;
- looping = true; /* keep looping */
- typtoken=qtmarktoken;
- }
- }
- if (!looping) token[tc]=c; /* opcode */
-
- if (typtoken==qtmarktoken) {
-
- /* quoted string */
- while (looping) {
- c = buf[point];
- if (c=='"') { /* check for double delimiter */
- if (point+1>=ars) {
- looping=false;
- point++; /* skip past ' */
- }
- else if (buf[point+1]=='"') { /* put a ' into the token */
- tc++;
- if (tc<127) token[tc] = c;
- point+=2; /* skip past both ' */
- }
- else {
- looping=false;
- point++; /* skip past closing ' */
- }
- }
- else {
- tc++;
- if (tc<127) token[tc] = c;
- point++;
- if (point>=ars) {
- looping = false;
- typtoken=errtoken; /* no trailing ' */
- }
- }
- } /* while looping */
- } /* end if qtmark */
- else if (looping) {
- /* alphanumeric token */
- typtoken=alftoken;
- while (looping) {
- c = buf[point];
- if ((isalnum(c)) || (c=='_') || (c=='.') || (c=='-') ||
- (c=='/')||(c=='\\')) {
- tc++;
- if (tc<127) token[tc] = c;
- point++;
- if (point>=ars) looping = false;
- }
- else if (NULL==strchr(oplist,c)) {
- looping = false;
- typtoken=errtoken;
- }
- else looping = false;
- }
- }
- token[tc+1]='\0';
- /* now characterize it */
- if (typtoken==alftoken) { /* alphanumeric, check for numeric */
- typtoken = numtoken;
- for (i=0;i<tc;i++) {
- if (!isdigit(token[i])) {
- typtoken=alftoken;
- break;
- }
- }
- }
- /* convert numeric tokens */
- pnum=0;
- if (typtoken==numtoken)
- for(i=0;i<=tc;i++) pnum = pnum*10 + token[i]-'0';
-
- }
-
-
- pcom()
- {
- int match, looking, i;
- looking = true;
- ctype = 1+ (int) c_fnull ;
- while (comtext[ctype].c[0]!='\0' && (looking==1)) {
- match = true;
- i = 0;
- while ((i <= tc) && (match==1)) {
- match = (toupper(token[i]) == comtext[ctype].c[i]);
- i++;
- }
- if (match==1) looking = false;
- else ctype++;
- }
- }
-
- writedol(i)
- int i;
- {
- int j;
- i = i + promptlen;
- for (j=0;j<i;j++) cprintf(" ");
- cprintf("^");
- }
-
- writeeh(i)
- int i;
- {
- writedol(i);
- cprintf(" Error");
- pbreak();
- }
-
- peh()
- {
- eh=true;
- ok=false;
- }
-
- parse(flag,s)
- int flag; /* command line flag */
- char *s;
- {
- ok = true;
- eh = false;
- if (flag)
- strcpy (buf,s);
- else gets(buf);
- ars = strlen(buf);
-
- ptoken(0);
- while ((ok) && (typtoken!=notoken)){
- if (typtoken==qmarktoken) {
- ctype=(int) c_help;
- doit();
- }
- else if ((typtoken==alftoken)||(typtoken==numtoken)) {
- pcom();
- if (ctype == (int) c_lnull) {
- ok = false;
- eh = true;
- }
- else doit();
- }
- else if (typtoken==cmarktoken) ; /* do nothing */
-
- else {
- ok = false;
- eh = true;
- }
- if (ok) ptoken(point);
- else if (eh) writeeh(ppoint);
- }
- }
-
-
- pbreak()
- {
- cprintf("\r\n");
- }
-
- doit()
- {
- int i,j,k,ii;
- unsigned int pnumt;
- char com_out[255];
- unsigned char cmdnum, cmdtask;
- unsigned int com_out_l;
-
- switch (ctype) {
- case c_lnull:
- ok = false;
- eh = true;
- break;
- case c_circuit:
- if (start_report()) break;
- report_circuit();
- end_report();
- break;
- case c_help:
- ptoken(point);
- printf("Commands are:\n");
- i=(numcom)/8;
- k=1;
- for (j=0;j<i;j++) {
- for (ii=0;ii<8;ii++,k++) printf("%.8s ",comtext[k].c);
- printf("\n");
- }
- for (ii=0;ii<((numcom) % 8);ii++,k++)
- printf("%.8s ",comtext[k].c);
- printf("\n");
- break;
- case c_input:
- ptoken(point);
- if ((typtoken==alftoken)||(typtoken==numtoken) ||
- (typtoken==qtmarktoken)) {
- token[tc+1]='\0';
- if (in_open) {
- fclose(fin);
- in_open=0;
- }
- if ((fin = fopen(token,"r")) == NULL){
- perror("cannot open input file");
- return;
- }
- else in_open=1;
- }
- else peh();
- break;
- case c_output:
- ptoken(point);
- if ((typtoken==alftoken)||(typtoken==numtoken) ||
- (typtoken==qtmarktoken)) {
- token[tc+1]='\0';
- if (out_open) {
- fclose(fout);
- out_open=0;
- }
- if ((fout = fopen(token,"w")) == NULL){
- perror("cannot open output file");
- return;
- }
- else out_open=1;
- }
- else peh();
- break;
-
-
- case c_end:
- case c_quit:
- if ((out_open) && (fout != stdout)){
- if (fclose(fout)==EOF) {
- perror("Error closing output file");
- }
- }
- exit(0);
- break;
- case c_rr:
- if (start_report()) break;
- report_rr();
- end_report();
- break;
-
- case c_raw:
- ptoken(point);
- if ((typtoken==notoken) || (typtoken==cmarktoken)) i=0;
- else if (toupper(*token)!='T') i=1;
- if (start_report()) break;
- report_raw(i);
- end_report();
- break;
- case c_record:
- ptoken(point);
- if ((typtoken==notoken) || (typtoken==cmarktoken)) {
- cprintf("Record types selected: %s\r\n",sel_types);
- }
- else {
- strncpy(sel_types,token,sizeof(sel_types));
- sel_types[sizeof(sel_types)-1]='\0';
- strupr(sel_types);
- }
- break;
- case c_select:
- ptoken(point);
- if ((typtoken==notoken) || (typtoken==cmarktoken)) sel_flag=0;
- else {
- strcpy(sel_call,strupr(token));
- sel_flag=1;
- }
- break;
- default:
- cprintf("*** internal error, parser default case\n");
- break;
- }
- if(typtoken==cmarktoken) point--;
- }
- int start_report()
- {
- if (!in_open) {
- if ((fin = fopen("log","r")) == NULL){
- perror("cannot open log file");
- return(1);
- }
- else in_open=1;
- }
- if (!out_open) fout=stdout;
- out_open=1;
- return(0);
-
- }
- end_report()
- {
- rewind(fin);
- recnum=0;
- }
-