source: git/Singular/utils.cc @ 6fc941

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