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