86 #define LEN_TOTREPLY 600
88 #define RECUR_LIMIT 40
90 #define ENV_VAR_1 "APPS_DEFAULTS_USER"
91 #define ENV_VAR_2 "APPS_DEFAULTS_SITE"
92 #define ENV_VAR_3 "APPS_DEFAULTS"
93 #define ENV_VAR_LENGTH 25
100 #define QUOTE1 '\"' /* 1st valid quote character */
101 #define QUOTE2 '\'' /* 2nd valid quote character */
102 #define BSLASH '\\' /* back slash */
104 #define QPHRASE1 (opt_line[ilast] == QUOTE1 && ilast > 0 && opt_line[ilast - 1] != BSLASH)
105 #define QPHRASE2 (opt_line[ilast] == QUOTE2 && ilast > 0 && opt_line[ilast - 1] != BSLASH)
106 #define NPHRASE2 (isspace(opt_line[ilast]) && ilast > 0 && opt_line[ilast - 1] != BSLASH)
108 static int r_cou = 0; /* counter to limit recursion */
109 static int ifile = 0; /* file loop counter */
110 static int i = 0; /* miscellaneous counter */
111 static int ilast = 0; /* last character position holder */
112 static int iphrase = 0; /* conditional phrase-ending indicator */
113 static char token[LEN_TOKEN+1]; /* working token array */
114 static char *as_env_var; /* returned env. var. value */
115 static char env_var_array[NUM_ENV_VAR][ENV_VAR_LENGTH];
116 static int r_len = 0; /* length of reply in referback */
117 static int e_len = 0; /* length of end of reply after a referback */
119 static FILE *in[NUM_ENV_VAR]; /* file descripter */
120 static char *opts_file[NUM_ENV_VAR]; /* file name holder */
122 static FILE *gap = NULL; /* file descripter for debug output */
124 /*------------------------------------------------------------------------------------------------*/
125 int get_apps_defaults(char *request, int *request_len, char *reply, int *reply_len)
128 void get_apps_defaults_r(char *, char *);
130 char inquest[LEN_TOKEN+1]; /* entered token string recopied */
131 char resource[LEN_TOTREPLY+1]; /* working resource array */
133 /* gap = fopen("/tmp/gap_io","a"); */
135 /* Place entered string into local variable; append '\0';set recursion count global */
137 for (i = 0; i < *request_len; i++)
138 inquest[i] = request[i];
139 inquest[*request_len] = '\0';
142 if (gap != NULL) fprintf(gap,"Input: %s\n",inquest);
144 /* Fill the environment variable array */
145 for (i = 0; i < NUM_ENV_VAR; i++)
146 (void)memset(env_var_array[i], '\0', ENV_VAR_LENGTH);
147 (void)strcpy(env_var_array[0], ENV_VAR_1);
148 (void)strcpy(env_var_array[1], ENV_VAR_2);
149 (void)strcpy(env_var_array[2], ENV_VAR_3);
151 /* Make sure apps files are initialized as not-opened */
153 for (ifile = 0; ifile < NUM_ENV_VAR; ifile++)
156 opts_file[ifile] = '\0';
159 /* Call true "C" routine that can be recursive using global variable "r_cou" */
161 get_apps_defaults_r(inquest,resource);
163 /* Close any apps files that may have been opened */
165 for (ifile = 0; ifile < NUM_ENV_VAR; ifile++)
167 if (in[ifile] != NULL)
169 (void)fclose(in[ifile]);
170 if (gap != NULL) fprintf(gap," Close: %2d %s\n",r_cou,opts_file[ifile]);
174 /* Place local output string into returned string, get length, set error status */
176 if (gap != NULL) fprintf(gap,"Output: %s %d\n",resource,r_cou);
177 if (gap != NULL) (void)fclose(gap);
179 (void)strcpy(reply, &resource[0]);
180 *reply_len = strlen(reply);
181 if (!*reply || r_cou > RECUR_LIMIT) /* SAM, RTi */
191 /*------------------------------------------------------------------------------------------------*/
192 void get_apps_defaults_r(char inquest[], char resource[])
195 char *pOpen; /* referback opening position holder */
196 char *pClose; /* referback closing position holder */
197 int diff = 0; /* string comparison result */
198 char referback[LEN_TOKEN+1]; /* referback token array */
199 char refer_val[LEN_TOTREPLY+1]; /* referback value array */
200 char substitute[LEN_TOTREPLY+1]; /* expanded referback-ed resource */
202 /* Initialize the result to NULL */
204 (void)memset(resource, '\0', LEN_TOTREPLY+1);
206 /* Check for the requested variable found as an environment variable */
208 as_env_var = getenv(inquest);
210 if ( as_env_var ) /* SAM, RTi */
211 (void)strcpy(resource, as_env_var);
215 /* Increment recursive counter (needed to avoid referbacks calling itself) */
218 if (r_cou <= RECUR_LIMIT+1)
221 /* The resource file to be read is indicated by the value of an
222 environment variable */
224 for (ifile = 0; ifile < NUM_ENV_VAR; ifile++)
226 char opt_line[LEN_LINE+1]; /* t-r file line array */
228 /* See if file can be opened for reading */
230 if (in[ifile] == NULL)
232 opts_file[ifile] = getenv(env_var_array[ifile]);
233 if (ifile == 2 && opts_file[ifile] == NULL) {
234 strcpy(opts_file[ifile],"/usr/apps/.Apps_defaults"); /* a default A-D file sbs 1-99 */
236 in[ifile] = fopen(opts_file[ifile], "r");
237 if ((gap != NULL) && (in[ifile] != NULL))
238 fprintf(gap," Open file: %2d %s\n",r_cou,opts_file[ifile]);
242 /*type problem for linux, used rewind instead*/
246 (void)fsetpos(in[ifile], &vpos);*/
247 (void)fgetpos(in[ifile], &vpos);
250 if (in[ifile] != NULL)
253 /* Read file until either match is found or EOF reached */
255 while (fgets(opt_line, LEN_LINE+1, in[ifile]) != NULL)
259 /* Ignore blank lines (nl only) */
261 if (strlen(opt_line) > 1)
264 /* Only scan lines with the delimiter in them */
266 if (strchr(opt_line, DELIM) != NULL)
270 /* Look for first non-blank character on line */
271 while (i < strlen(opt_line)-1 && isspace(opt_line[i]) != 0)
273 if (i < strlen(opt_line))
276 /* Discard line if first character is either delimiter or comment indicator */
278 if (opt_line[i] != COMMENT && opt_line[i] != DELIM)
284 /* Look for token based on rules for delimiting tokens */
286 while (isprint(opt_line[ilast]) != 0 &&
287 isspace(opt_line[ilast]) == 0 &&
288 opt_line[ilast] != DELIM)
290 token[i++] = opt_line[ilast++];
294 /* See if token on line is one to be retrieved */
296 if (strlen(inquest) == strlen(token) &&
297 strncmp(token, inquest, i) == 0)
299 /* Match found, now determine associated resource.
300 Resource can not start with DELIM or COMMENT characters
301 or any non-printing characters */
305 while (i < strlen(opt_line)-1 &&
306 (isspace(opt_line[i]) != 0 ||
307 opt_line[i] == DELIM))
312 /* Determine contents of resource until:
313 1. End of line is reached, or
314 2. White space is found for resources not quoted, or
315 3. Closing matching quote character is found for quoted strings. */
317 if (i < strlen(opt_line))
319 if (opt_line[i] != COMMENT)
324 /* Check to see if resource string is quoted (single or double) */
333 /* Complete resource based on start character conditions */
338 while (isprint(opt_line[ilast]) && !NPHRASE2)
339 resource[i++] = opt_line[ilast++];
343 while (isprint(opt_line[ilast]) && !QPHRASE1)
344 resource[i++] = opt_line[ilast++];
348 while (isprint(opt_line[ilast]) && !QPHRASE2)
349 resource[i++] = opt_line[ilast++];
354 /* Now look for any embedded referbacks in the resource string */
356 while ( ((pOpen = strstr(resource, RFR_OPEN)) != NULL) &&
357 ((pClose = strstr(pOpen, RFR_CLOSE)) != NULL) &&
358 ((diff = (int)(pClose - pOpen) - 2) >= 0 ) )
360 (void)memset(substitute, '\0', LEN_TOTREPLY+1);
361 if (strcmp(resource, pOpen)) /* SAM, RTi */
362 (void)strncpy(substitute, resource, (pOpen - &resource[0]));
365 (void)memset(referback, '\0', LEN_TOKEN+1);
366 (void)memset(refer_val, '\0', LEN_REPLY+1);
367 (void)strncpy(referback, pOpen + 2, diff);
369 (void)get_apps_defaults_r(referback, refer_val);
371 if (r_cou > RECUR_LIMIT+1)
374 e_len = (int)(strlen(resource) - (int)(pClose - &resource[0]));
376 r_len = (int)(strlen(refer_val));
377 if ( ((int)strlen(substitute) + r_len + e_len) > LEN_TOTREPLY)
382 (void)strcat(substitute, refer_val);
385 (void)strcat(substitute, pClose + 1);
386 (void)strcpy(resource, substitute);
387 } /* end of referback expansion while-loop */
389 } /* end of non-comment part of resource request if-loop */
390 } /* end of resource request characters if-loop */
391 } /* end of token-been-found if-loop */
392 } /* end of discard-if-delimiter-or-comment check if-loop */
393 } /* end of search for non-blank token char if-loop */
394 } /* end of check for line with a delimiter in it if-loop */
395 } /* end of non-blank line scan if-loop */
396 } /* end of t-r file read to EOL if-loop */
397 if (resource[0]) /* SAM, RTi */
399 } /* end of legitimate file opening loop */
400 } /* end of loop thru files named by env vars */
401 } /* end of loop where recursion is less than limit */
406 } /* end obtaining of resource for given token */
409 /* ============== Statements containing RCS keywords: */
410 {static char rcs_id1[] = "$Source: /usr/apps/nwsrfs/util/src/util_gen1/RCS/get_apps_defaults.c,v $";
411 static char rcs_id2[] = "$Id: get_apps_defaults.c,v 1.2 1998/10/14 17:21:09 page Exp $";}
412 /* =================================================== */