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