00001 00002 /* 00003 * THIS FILE IS UNDER RCS - DO NOT MODIFY UNLESS YOU HAVE 00004 * CHECKED IT OUT USING THE COMMAND CHECKOUT. 00005 * 00006 * $Id: kom_8c-source.html 2161 2006-05-19 16:55:03Z paulf $ 00007 * 00008 * Revision history: 00009 * $Log$ 00009 * Revision 1.1 2006/05/19 16:55:02 paulf 00009 * first inclusion 00009 * 00010 * Revision 1.2 2001/10/05 20:42:15 dietz 00011 * Increased MAXCRD to 1024 (was 512) 00012 * 00013 * Revision 1.1 2000/02/14 18:51:48 lucky 00014 * Initial revision 00015 * 00016 * 00017 */ 00018 00019 /* 00020 * kom.c : Simple positional command parser. 00021 * 00022 *$ 91May07 CEJ Version 1.0 00023 *$ 93Oct03 CEJ Added k_put routine. 00024 *$ 95Oct18 LDD Created kom.h to house function prototypes. 00025 * Explicitly declared return types for all functions. 00026 */ 00027 /*********************C O P Y R I G H T N O T I C E ***********************/ 00028 /* Copyright 1991 by Carl Johnson. All rights are reserved. Permission */ 00029 /* is hereby granted for the use of this product for nonprofit, commercial, */ 00030 /* or noncommercial publications that contain appropriate acknowledgement */ 00031 /* of the author. Modification of this code is permitted as long as this */ 00032 /* notice is included in each resulting source module. */ 00033 /****************************************************************************/ 00034 00035 #include <stdio.h> 00036 #include <stdlib.h> 00037 #include <string.h> 00038 #include <kom.h> 00039 00040 #define MAXCRD 1024 00041 static struct k_buf { 00042 FILE *fid; 00043 int ncrd; /* bytes in command */ 00044 int istr; /* index of current string */ 00045 int icrd; /* scan pointer */ 00046 int ierr; /* error if non-zero */ 00047 char crd[MAXCRD]; /* command buffer */ 00048 char csav[MAXCRD]; /* last command read */ 00049 } ; 00050 00051 #define MAXBUF 4 00052 struct k_buf com = {0L, 0, 0, 0, 0, "Virgin", "Virgin"}; 00053 struct k_buf Kbuf[MAXBUF]; 00054 int Nbuf = 0; 00055 00056 00057 /* 00058 **** k_open : open new file for k-system input. For now only one file 00059 * can be open at a time, but it should be straight foward to add 00060 * recursion capabitity using a stack in the com area. 00061 */ 00062 int k_open( char *name ) 00063 { 00064 if(Nbuf < MAXBUF && com.fid) { 00065 Kbuf[Nbuf++] = com; 00066 com.fid = 0; 00067 } 00068 if(com.fid) 00069 return(0); 00070 /* if(!strcmp(name, "term")) 00071 com.fid = stdin; 00072 else */ 00073 com.fid = fopen(name, "r"); 00074 if(com.fid) 00075 return(Nbuf+1); 00076 return(0); 00077 00078 } 00079 /* 00080 **** k_close : close current file 00081 */ 00082 int k_close( void ) 00083 { 00084 if(com.fid){ 00085 if(com.fid != stdin) 00086 fclose(com.fid); 00087 com.fid = 0L; 00088 } 00089 if(Nbuf > 0) { 00090 com = Kbuf[--Nbuf]; 00091 return Nbuf+1; 00092 } 00093 return(0); 00094 00095 } 00096 00097 /* 00098 **** k_get : Return pointer to current card. 00099 */ 00100 char *k_get( void ) 00101 { 00102 return com.crd; 00103 } 00104 00105 /* 00106 **** k_dump : Print last card read. 00107 */ 00108 void k_dump( void ) 00109 { 00110 printf("%s\n", com.crd); 00111 } 00112 00113 /* 00114 **** k_err : return last error code and clear 00115 */ 00116 int k_err( void ) 00117 { 00118 int jerr; 00119 00120 jerr = com.ierr; 00121 com.ierr = 0; 00122 return(jerr); 00123 } 00124 00125 /* 00126 * k_int : Parce next token as integer. 00127 */ 00128 int k_int( void ) 00129 { 00130 int ival; 00131 char *s; 00132 00133 s = k_str(); 00134 if(!s) { 00135 com.ierr = -1; 00136 return(0); 00137 } 00138 ival = atoi(s); 00139 if(ival == 0 && *s != '0') { 00140 com.ierr = -2; 00141 return(0); 00142 } 00143 return(ival); 00144 } 00145 00146 /* 00147 * k_its : Compare string from last token to given command. 00148 */ 00149 int k_its(char *c) 00150 { 00151 char *s; 00152 00153 s = &com.crd[com.istr]; 00154 while(*c == *s) { 00155 if(*s == '\0') 00156 return(1); 00157 c++; 00158 s++; 00159 } 00160 return(0); 00161 } 00162 00163 /* 00164 * k_long : Return next token as a long integer. 00165 */ 00166 long k_long( void ) 00167 { 00168 long lval; 00169 char *s; 00170 00171 s = k_str(); 00172 if(!s) { 00173 com.ierr = -1; 00174 return(0L); 00175 } 00176 lval = atol(s); 00177 if(lval == 0 && *s != '0') { 00178 com.ierr = -2; 00179 return(0L); 00180 } 00181 return(lval); 00182 } 00183 00184 /* 00185 **** k_put : insert command line into buffer 00186 */ 00187 int k_put(char *crd) 00188 { 00189 int i, n; 00190 00191 strcpy(com.crd, crd); 00192 com.ncrd = strlen(crd); 00193 if(com.ncrd && com.crd[com.ncrd-1] == '\n') 00194 com.crd[--com.ncrd] = 0; 00195 if(!com.ncrd) { 00196 com.ncrd = 1; 00197 com.crd[0] = ' '; 00198 com.crd[1] = 0; 00199 } 00200 com.istr = 0; 00201 com.icrd = 0; 00202 com.ierr = 0; 00203 n = 1; 00204 for(i=0; i<com.ncrd; i++) { 00205 if(com.crd[i] == '\t') 00206 com.crd[i] = ' '; 00207 if(com.crd[i] != ' ') 00208 n = i + 1; 00209 } 00210 com.ncrd = n; 00211 com.crd[n] = 0; 00212 strcpy(com.csav, com.crd); 00213 return(com.ncrd); 00214 } 00215 00216 /* 00217 **** k_rd : read command line into buffer 00218 */ 00219 int k_rd( void ) 00220 { 00221 FILE *fid; 00222 extern struct k_buf com; 00223 int n; 00224 int i; 00225 00226 fid = com.fid; 00227 if(com.fid) { 00228 if(!fgets(com.crd, MAXCRD-1, fid)) 00229 if(feof(fid)) 00230 return(0); 00231 } /*else { 00232 if(!gets(com.crd)) 00233 return(0); 00234 } */ 00235 com.ncrd = strlen(com.crd); 00236 if(com.ncrd && com.crd[com.ncrd-1] == '\n') 00237 com.crd[--com.ncrd] = 0; 00238 if(!com.ncrd) { 00239 com.ncrd = 1; 00240 com.crd[0] = ' '; 00241 com.crd[1] = 0; 00242 } 00243 com.istr = 0; 00244 com.icrd = 0; 00245 com.ierr = 0; 00246 n = 1; 00247 for(i=0; i<com.ncrd; i++) { 00248 if(com.crd[i] == '\t') 00249 com.crd[i] = ' '; 00250 if(com.crd[i] != ' ') 00251 n = i + 1; 00252 } 00253 com.ncrd = n; 00254 com.crd[n] = 0; 00255 strcpy(com.csav, com.crd); 00256 return(com.ncrd); 00257 } 00258 00259 /* 00260 **** k_com : returns last command line read 00261 */ 00262 char *k_com( void ) 00263 { 00264 return com.csav; 00265 } 00266 00267 /* 00268 **** k_str() : Return next token as a pointer to string. 00269 */ 00270 char *k_str( void ) 00271 { 00272 int state; 00273 int i; 00274 00275 state = 1; 00276 for(i=com.icrd; i<com.ncrd; i++) { 00277 switch(state) { 00278 case 1: /* Looking for first non-blank */ 00279 if(com.crd[i] == ' ' || com.crd[i] == '\t') 00280 break; 00281 if(com.crd[i] == '"') { 00282 state = 3; 00283 com.istr = i + 1; 00284 break; 00285 } 00286 state = 2; 00287 com.istr = i; 00288 break; 00289 case 2: /* Looking for end of normal string */ 00290 if(com.crd[i] == ' ' || com.crd[i] == '\t') { 00291 com.crd[i] = 0; 00292 com.icrd = i + 1; 00293 return(&com.crd[com.istr]); 00294 } 00295 break; 00296 case 3: /* Quoted string */ 00297 if(com.crd[i] == '"') { 00298 com.crd[i] = 0; 00299 com.icrd = i + 1; 00300 return(&com.crd[com.istr]); 00301 } 00302 break; 00303 } 00304 } 00305 if(state == 2) { 00306 com.crd[com.ncrd] = 0; 00307 com.icrd = com.ncrd; 00308 return(&com.crd[com.istr]); 00309 } 00310 com.ierr = -17; 00311 return( (char *) 0 ); 00312 } 00313 00314 /* 00315 **** k_val() Return next token as a double real 00316 */ 00317 double k_val( void ) 00318 { 00319 double val; 00320 char *s; 00321 00322 s = k_str(); 00323 if(!s) { 00324 com.ierr = -1; 00325 return(0.0); 00326 } 00327 val = atof(s); 00328 return(val); 00329 }