ARPDAS_QNX6 1.0
|
00001 /* skeleton.c Skeleton file routines for compilers. */ 00002 #include <stdio.h> 00003 #include <stdlib.h> 00004 #include <string.h> 00005 #include <ctype.h> 00006 #include "nortlib.h" 00007 char rcsid_skeleton_c[] = 00008 "$Header: /cvsroot/arp-das/nortlib2/src/skeleton.c,v 1.4 2009/03/02 17:11:30 ntallen Exp $"; 00009 00010 /* Skel_open opens the specified skeleton file, looking in appropriate 00011 directories as necessary. Returns zero on success. On error, calls 00012 the nortlib error routine nl_error, and returns nonzero. nl_error 00013 can be redirected as necessary. 00014 */ 00015 static FILE *skfp = NULL; 00016 #define MAX_LABEL 40 00017 #define DEF_DIR "/usr/local/share/huarp" 00018 00019 int Skel_open(const char *name) { 00020 char filename[FILENAME_MAX+1]; 00021 00022 #if HAVE__SEARCHENV 00023 _searchenv(name, "SKELETON_PATH", filename); 00024 if ( filename[0] == '\0' ) 00025 #endif 00026 sprintf( filename, DEF_DIR "/%s", name ); 00027 skfp = fopen(filename, "r"); 00028 if (skfp == NULL) { 00029 nl_error(2, "Unable to open skeleton file %s", name); 00030 return(1); 00031 } 00032 return(0); 00033 } 00034 00035 /* Skel_copy optionally copies data from the skeleton file to 00036 the output file. Data is copied up to the indicated label. 00037 The label is marked in the skeleton file as %label% starting 00038 at the left margin with optional descriptive text following. 00039 The line containing the label is not copied. If label is 00040 NULL, it means copy to the end of the file. If an unexpected 00041 label (including EOF) is encountered, it is considered an 00042 error. The nl_error routine is called and a non-zero result 00043 is returned. 00044 00045 If copyout is non-zero, the intervening skeleton text is 00046 copied to the output. 00047 00048 Specific label format is "^%[a-zA-Z0-9_]*%". Anything failing 00049 to match is deemed not a label and is processed as text. 00050 */ 00051 int Skel_copy(FILE *ofp, const char *label, int copyout) { 00052 char lbuf[MAX_LABEL]; 00053 int c, i; 00054 00055 if (skfp != NULL) { 00056 /* We always start at the beginning of a line */ 00057 for (;;) { 00058 c = getc(skfp); 00059 if (c == EOF) break; 00060 if (c == '%') { 00061 for (i = 0;;) { 00062 c = getc(skfp); 00063 if (isalnum(c) || c == '_') lbuf[i++] = c; 00064 else break; 00065 } 00066 lbuf[i] = '\0'; 00067 if (c == '%') { /* It was a label */ 00068 do c = getc(skfp); while (c != EOF && c != '\n'); 00069 if (strcmp(label, lbuf) == 0) return(0); 00070 else return(nl_error(2, 00071 "Skel: Unexpected label \"%s\" looking for %s", 00072 lbuf, label == NULL ? "EOF" : label)); 00073 } else if (copyout) fprintf(ofp, "%%%s", lbuf); 00074 } 00075 if (c == EOF) break; 00076 if (copyout) putc(c, ofp); 00077 } 00078 00079 /* We get out here only if file was open and we saw EOF */ 00080 fclose(skfp); 00081 skfp = NULL; 00082 } 00083 00084 /* We get here only if file isn't open (EOF) */ 00085 if (label != NULL) 00086 return(nl_error(2, 00087 "Unexpected EOF in Skeleton seeking label \"%s\"", label)); 00088 return(0); 00089 }