[33d539] | 1 | #include <stdio.h> |
---|
| 2 | #include <string.h> |
---|
| 3 | #include <stdlib.h> |
---|
| 4 | #include <ctype.h> |
---|
[c06a32] | 5 | #include "fegetopt.h" |
---|
[33d539] | 6 | #include "utils.h" |
---|
[12310e] | 7 | #ifdef __MWERKS__ |
---|
[901748b] | 8 | #define __GNU_LIBRARY__ |
---|
[c06a32] | 9 | #include "fegetopt.h" |
---|
[12310e] | 10 | #endif |
---|
[33d539] | 11 | |
---|
[d2b2a7] | 12 | extern FILE *yylpin; |
---|
| 13 | extern char *optarg; |
---|
| 14 | extern int optind, opterr, optopt; |
---|
| 15 | extern int lpverbose, check; |
---|
[f36635] | 16 | extern int texinfo_out; |
---|
[5ecf042] | 17 | extern int category_out; |
---|
[d2b2a7] | 18 | extern int found_version, found_info, found_oldhelp, found_proc_in_proc; |
---|
[901748b] | 19 | int warning_info = 0, warning_version = 0; |
---|
[a5279ce] | 20 | |
---|
[a70441f] | 21 | static void usage(char *progname) |
---|
[a5279ce] | 22 | { |
---|
| 23 | printf("libparse: a syntax-checker for Singular Libraries.\n"); |
---|
| 24 | printf("USAGE: %s [options] singular-library\n", progname); |
---|
| 25 | printf("Options:\n"); |
---|
[c052d0] | 26 | printf(" -f <singular library> : performs syntax-checks\n"); |
---|
[a5279ce] | 27 | printf(" -d [digit] : digit=1,..,4 increases the verbosity of the checks\n"); |
---|
[c052d0] | 28 | printf(" -s : turns on reporting about violations of unenforced syntax rules\n"); |
---|
[f36635] | 29 | printf(" -i : perl output of examples and help of procs\n"); |
---|
[5ecf042] | 30 | printf(" -c : print category of lib to stdout and exit\n"); |
---|
[a5279ce] | 31 | printf(" -h : print this message\n"); |
---|
| 32 | exit(1); |
---|
| 33 | } |
---|
[a3bc95e] | 34 | |
---|
[f36635] | 35 | static char* lib_file = NULL; |
---|
[8a150b] | 36 | |
---|
[d2b2a7] | 37 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
| 38 | void main_init(int argc, char *argv[]) |
---|
| 39 | { |
---|
[f36635] | 40 | char c; |
---|
[d2b2a7] | 41 | |
---|
[5ecf042] | 42 | while((c=fe_getopt(argc, argv, "ihdc:sf:"))>=0) { |
---|
[a5279ce] | 43 | switch(c) { |
---|
| 44 | case 'd': |
---|
| 45 | lpverbose = 1; |
---|
[c06a32] | 46 | if(isdigit(argv[fe_optind-1][0])) sscanf(optarg, "%d", &lpverbose); |
---|
| 47 | else fe_optind--; |
---|
[a5279ce] | 48 | break; |
---|
[c06a32] | 49 | case 'f': lib_file = argv[fe_optind-1]; |
---|
[a5279ce] | 50 | break; |
---|
| 51 | case 's': |
---|
| 52 | check++; |
---|
| 53 | break; |
---|
[f36635] | 54 | case 'i': |
---|
| 55 | texinfo_out = 1; |
---|
| 56 | break; |
---|
[5ecf042] | 57 | case 'c': |
---|
| 58 | category_out = 1; |
---|
| 59 | break; |
---|
| 60 | |
---|
[a5279ce] | 61 | case 'h' : |
---|
| 62 | usage(argv[0]); |
---|
| 63 | break; |
---|
[c06a32] | 64 | case -1 : printf("no such option:%s\n", argv[fe_optind]); |
---|
[a5279ce] | 65 | usage(argv[0]); |
---|
| 66 | break; |
---|
[c06a32] | 67 | default: printf("no such option.%x, %c %s\n", c&0xff, c, argv[fe_optind]); |
---|
[a5279ce] | 68 | usage(argv[0]); |
---|
| 69 | } |
---|
[d2b2a7] | 70 | } |
---|
[5ecf042] | 71 | if (texinfo_out || category_out) lpverbose = 0; |
---|
[a3bc95e] | 72 | |
---|
[f36635] | 73 | if(lib_file!=NULL) { |
---|
| 74 | yylpin = fopen( lib_file, "rb" ); |
---|
[a3bc95e] | 75 | if (! (texinfo_out || category_out)) |
---|
[f36635] | 76 | printf("Checking library '%s'\n", lib_file); |
---|
[5ecf042] | 77 | else if (! category_out) |
---|
[f36635] | 78 | printf("$library = \"%s\";\n", lib_file); |
---|
[6be769] | 79 | } else { |
---|
[c06a32] | 80 | while(argc>fe_optind && yylpin==NULL) { |
---|
| 81 | yylpin = fopen( argv[fe_optind], "rb" ); |
---|
[a3bc95e] | 82 | if(yylpin!=NULL) |
---|
[f36635] | 83 | { |
---|
[c06a32] | 84 | lib_file = argv[fe_optind]; |
---|
[5ecf042] | 85 | if (! (texinfo_out || category_out) ) |
---|
[c06a32] | 86 | printf("Checking library '%s'\n", argv[fe_optind]); |
---|
[5ecf042] | 87 | else if (! category_out) |
---|
[f36635] | 88 | printf("$library = \"%s\";\n", lib_file); |
---|
| 89 | } |
---|
[c06a32] | 90 | else fe_optind++; |
---|
[6be769] | 91 | } |
---|
| 92 | } |
---|
[a5279ce] | 93 | if(yylpin == NULL) { |
---|
| 94 | printf("No library found to parse.\n"); |
---|
| 95 | usage(argv[0]); |
---|
| 96 | } |
---|
[d2b2a7] | 97 | } |
---|
| 98 | |
---|
| 99 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
| 100 | void main_result(char *libname) |
---|
| 101 | { |
---|
| 102 | if(!found_info) printf("*** No info-string found!\n"); |
---|
| 103 | if(!found_version) printf("*** No version-string found!\n"); |
---|
| 104 | if(found_oldhelp) printf("*** Library has stil OLD library-format.\n"); |
---|
| 105 | if(found_info && warning_info) |
---|
| 106 | printf("*** INFO-string should come before every procedure definition.\n"); |
---|
| 107 | if(found_version && warning_version) |
---|
| 108 | printf("*** VERSION-string should come before every procedure definition.\n"); |
---|
| 109 | } |
---|
[33d539] | 110 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
| 111 | |
---|
| 112 | procinfo *iiInitSingularProcinfo(procinfov pi, char *libname, |
---|
| 113 | char *procname, int line, long pos, |
---|
[8a150b] | 114 | BOOLEAN pstatic = FALSE) |
---|
[33d539] | 115 | { |
---|
| 116 | pi->libname = (char *)malloc(strlen(libname)+1); |
---|
| 117 | memcpy(pi->libname, libname, strlen(libname)); |
---|
| 118 | *(pi->libname+strlen(libname)) = '\0'; |
---|
| 119 | |
---|
| 120 | pi->procname = (char *)malloc(strlen(procname)+1); |
---|
| 121 | strcpy(pi->procname, procname/*, strlen(procname)*/); |
---|
| 122 | pi->language = LANG_SINGULAR; |
---|
| 123 | pi->ref = 1; |
---|
| 124 | pi->is_static = pstatic; |
---|
| 125 | pi->data.s.proc_start = pos; |
---|
| 126 | pi->data.s.def_end = 0L; |
---|
| 127 | pi->data.s.help_start = 0L; |
---|
| 128 | pi->data.s.body_start = 0L; |
---|
| 129 | pi->data.s.body_end = 0L; |
---|
| 130 | pi->data.s.example_start = 0L; |
---|
| 131 | pi->data.s.proc_lineno = line; |
---|
| 132 | pi->data.s.body_lineno = 0; |
---|
| 133 | pi->data.s.example_lineno = 0; |
---|
| 134 | pi->data.s.body = NULL; |
---|
[f92cf8] | 135 | pi->data.s.help_chksum = 0; |
---|
[33d539] | 136 | return(pi); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
[a70441f] | 140 | void pi_clear(procinfov pi) |
---|
[33d539] | 141 | { |
---|
| 142 | free(pi->libname); |
---|
| 143 | free(pi->procname); |
---|
| 144 | free(pi); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
[f36635] | 148 | |
---|
| 149 | #ifndef SEEK_SET |
---|
| 150 | #define SEEK_SET 0 |
---|
| 151 | #endif |
---|
| 152 | |
---|
| 153 | static void PrintOut(FILE *fd, int pos_start, int pos_end) |
---|
| 154 | { |
---|
| 155 | if (pos_start <= 0 || pos_end - pos_start <= 4) return; |
---|
[dcc635] | 156 | char c = 0; |
---|
[a3bc95e] | 157 | |
---|
[f36635] | 158 | fseek(fd, pos_start, SEEK_SET); |
---|
[a3bc95e] | 159 | while (pos_start++ <= pos_end) |
---|
[f36635] | 160 | { |
---|
[dcc635] | 161 | if (c == '\\') |
---|
| 162 | { |
---|
| 163 | c = fgetc(fd); |
---|
| 164 | if (c != '"') putchar('\\'); |
---|
| 165 | } |
---|
| 166 | else |
---|
| 167 | c = fgetc(fd); |
---|
| 168 | if (c == '@' || c == '$') putchar('\\'); |
---|
[f36635] | 169 | if (c != '\r') putchar(c); |
---|
| 170 | } |
---|
[dcc635] | 171 | if (c == '\\') putchar('\\'); |
---|
[f36635] | 172 | } |
---|
| 173 | |
---|
| 174 | |
---|
[a70441f] | 175 | void printpi(procinfov pi) |
---|
[33d539] | 176 | { |
---|
| 177 | char *buf, name[256]; |
---|
| 178 | int len1, len2; |
---|
[f36635] | 179 | /* pi->libname is badly broken -- use file, instead */ |
---|
| 180 | FILE *fp = fopen( lib_file, "rb"); |
---|
| 181 | |
---|
[a3bc95e] | 182 | if (fp == NULL) |
---|
[f36635] | 183 | { |
---|
| 184 | printf("Can not open %s\n", lib_file); |
---|
[a70441f] | 185 | return; |
---|
[f36635] | 186 | } |
---|
[33d539] | 187 | |
---|
[d2b2a7] | 188 | if(!found_info && !warning_info) warning_info++; |
---|
| 189 | if(!found_version && !warning_version) warning_version++; |
---|
[8a150b] | 190 | if(pi->data.s.body_end==0) |
---|
[33d539] | 191 | pi->data.s.body_end = pi->data.s.proc_end; |
---|
| 192 | |
---|
[f36635] | 193 | if (texinfo_out) |
---|
| 194 | { |
---|
[b0d726] | 195 | if ((! pi->is_static) && |
---|
[f36635] | 196 | (pi->data.s.body_start - pi->data.s.def_end > 10) && |
---|
| 197 | (! found_proc_in_proc)) |
---|
| 198 | { |
---|
| 199 | printf("push(@procs, \"%s\");\n", pi->procname); |
---|
| 200 | printf("$help{\"%s\"} = <<EOT;\n", pi->procname); |
---|
| 201 | PrintOut(fp, pi->data.s.help_start, pi->data.s.body_start-3); |
---|
| 202 | printf("\nEOT\n"); |
---|
[b0d726] | 203 | if ((pi->data.s.example_start > 0) && |
---|
| 204 | (pi->data.s.proc_end - pi->data.s.example_start > 10)) |
---|
| 205 | { |
---|
| 206 | printf("$example{\"%s\"} = <<EOT;\n", pi->procname); |
---|
| 207 | PrintOut(fp, pi->data.s.example_start, pi->data.s.proc_end); |
---|
| 208 | printf("\nEOT\n"); |
---|
| 209 | } |
---|
[f92cf8] | 210 | printf("$chksum{\"%s\"} = %d;\n", pi->procname, pi->data.s.help_chksum); |
---|
[f36635] | 211 | } |
---|
| 212 | } |
---|
[5ecf042] | 213 | else if (! category_out) |
---|
[f36635] | 214 | { |
---|
| 215 | if(lpverbose) printf("// "); |
---|
| 216 | printf( "%c %-15s %20s ", pi->is_static ? 'l' : 'g', pi->libname, |
---|
| 217 | pi->procname); |
---|
| 218 | printf("line %4d,%5ld-%-5ld %4d,%5ld-%-5ld %4d,%5ld-%-5ld\n", |
---|
| 219 | pi->data.s.proc_lineno, pi->data.s.proc_start, pi->data.s.def_end, |
---|
| 220 | pi->data.s.body_lineno, pi->data.s.body_start, pi->data.s.body_end, |
---|
| 221 | pi->data.s.example_lineno, pi->data.s.example_start, |
---|
| 222 | pi->data.s.proc_end); |
---|
| 223 | if(check) { |
---|
| 224 | if(!pi->is_static && (pi->data.s.body_start-pi->data.s.def_end)<4) |
---|
| 225 | printf("*** Procedure '%s' is global and has no help-section.\n", |
---|
| 226 | pi->procname); |
---|
| 227 | if(!pi->is_static && !pi->data.s.example_start) |
---|
| 228 | printf("*** Procedure '%s' is global and has no example-section.\n",\ |
---|
| 229 | pi->procname); |
---|
| 230 | if(found_proc_in_proc) |
---|
| 231 | printf("*** found proc within procedure '%s'.\n", pi->procname); |
---|
| 232 | } |
---|
[d2b2a7] | 233 | } |
---|
[a3bc95e] | 234 | |
---|
[902ef1] | 235 | if (fp != NULL) fclose(fp); |
---|
[33d539] | 236 | |
---|
| 237 | #if 0 |
---|
| 238 | if( fp != NULL) { // loading body |
---|
| 239 | len1 = pi->data.s.def_end - pi->data.s.proc_start; |
---|
[8a150b] | 240 | if(pi->data.s.body_end==0) |
---|
[33d539] | 241 | len2 = pi->data.s.proc_end - pi->data.s.body_start; |
---|
| 242 | else len2 = pi->data.s.body_end - pi->data.s.body_start; |
---|
| 243 | buf = (char *)malloc(len1 + len2 + 1); |
---|
| 244 | fseek(fp, pi->data.s.proc_start, SEEK_SET); |
---|
[c1e2b1] | 245 | fread( buf, len1, 1, fp); |
---|
[33d539] | 246 | *(buf+len1) = '\n'; |
---|
| 247 | fseek(fp, pi->data.s.body_start, SEEK_SET); |
---|
[c1e2b1] | 248 | fread( buf+len1+1, len2, 1, fp); |
---|
[33d539] | 249 | *(buf+len1+len2+1)='\0'; |
---|
| 250 | printf("##BODY:'%s'##\n", buf); |
---|
| 251 | free(buf); |
---|
| 252 | |
---|
| 253 | // loading help |
---|
| 254 | len1 = pi->data.s.body_start - pi->data.s.proc_start; |
---|
| 255 | printf("len1=%d;\n", len1); |
---|
| 256 | buf = (char *)malloc(len1+1); |
---|
| 257 | fseek(fp, pi->data.s.proc_start, SEEK_SET); |
---|
[c1e2b1] | 258 | fread( buf, len1, 1, fp); |
---|
[33d539] | 259 | *(buf+len1)='\0'; |
---|
| 260 | printf("##HELP:'%s'##\n", buf); |
---|
| 261 | free(buf); |
---|
| 262 | |
---|
| 263 | if(pi->data.s.example_start>0) { // loading example |
---|
| 264 | fseek(fp, pi->data.s.example_start, SEEK_SET); |
---|
| 265 | len2 = pi->data.s.proc_end - pi->data.s.example_start; |
---|
| 266 | buf = (char *)malloc(len2+1); |
---|
| 267 | fread( buf, len2, 1, fp); |
---|
| 268 | *(buf+len2)='\0'; |
---|
| 269 | printf("##EXAMPLE:'%s'##\n", buf); |
---|
| 270 | free(buf); |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | //getchar(); |
---|
| 274 | } |
---|
| 275 | #endif |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|