source: git/Singular/utils.cc @ a5279ce

spielwiese
Last change on this file since a5279ce was a5279ce, checked in by Kai Krüger <krueger@…>, 26 years ago
added help for libparse git-svn-id: file:///usr/local/Singular/svn/trunk@1951 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 5.9 KB
Line 
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <ctype.h>
5#include "utils.h"
6
7extern FILE *yylpin;
8extern char *optarg;
9extern int optind, opterr, optopt;
10extern int lpverbose, check;
11extern int found_version, found_info, found_oldhelp, found_proc_in_proc;
12warning_info = 0, warning_version = 0;
13
14static usage(char *progname)
15{
16  printf("libparse: a syntax-checker for Singular Libraries.\n");
17  printf("USAGE: %s [options] singular-library\n", progname);
18  printf("Options:\n");
19  printf("   -f <singular library> : checks\n");
20  printf("   -d [digit]            : digit=1,..,4 increases the verbosity of the checks\n");
21  printf("   -s                    : \n");
22  printf("   -h                    : print this message\n");
23  exit(1);
24}
25 
26/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
27void main_init(int argc, char *argv[])
28{
29  char c, *file=NULL;
30
31  while((c=getopt(argc, argv, "hd:sf:"))>=0) {
32    switch(c) {
33        case 'd':
34          lpverbose = 1;
35          if(isdigit(argv[optind-1][0])) sscanf(optarg, "%d", &lpverbose);
36          else optind--;
37          break;
38        case 'f': file = argv[optind-1];
39          break;
40        case 's':
41          check++;
42          break;
43        case 'h' :
44          usage(argv[0]);
45          break;
46        case -1 : printf("no such option:%s\n", argv[optind]);
47          usage(argv[0]);
48          break;
49        default: printf("no such option.%x, %c %s\n", c&0xff, c, argv[optind]);
50          usage(argv[0]);
51    }
52  }
53  if(file!=NULL) {
54    yylpin = fopen( file, "rb" );
55    printf("Checking library '%s'\n", file);
56  } else {
57    while(argc>optind && yylpin==NULL) {
58      yylpin = fopen( argv[optind], "rb" );
59      if(yylpin!=NULL) printf("Checking library '%s'\n", argv[optind]);
60      else optind++;
61    }
62  }
63  if(yylpin == NULL) {
64    printf("No library found to parse.\n");
65    usage(argv[0]);
66  }
67}
68
69/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
70void main_result(char *libname)
71{
72  if(!found_info)    printf("*** No info-string found!\n");
73  if(!found_version) printf("*** No version-string found!\n");
74  if(found_oldhelp)  printf("*** Library has stil OLD library-format.\n");
75  if(found_info && warning_info)
76    printf("*** INFO-string should come before every procedure definition.\n");
77  if(found_version && warning_version)
78    printf("*** VERSION-string should come before every procedure definition.\n");
79}
80/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
81
82procinfo *iiInitSingularProcinfo(procinfov pi, char *libname,
83                                 char *procname, int line, long pos,
84                                 BOOLEAN pstatic = FALSE)
85{
86  pi->libname = (char *)malloc(strlen(libname)+1);
87  memcpy(pi->libname, libname, strlen(libname));
88  *(pi->libname+strlen(libname)) = '\0';
89
90  pi->procname = (char *)malloc(strlen(procname)+1);
91  strcpy(pi->procname, procname/*, strlen(procname)*/);
92  pi->language = LANG_SINGULAR;
93  pi->ref = 1;
94  pi->is_static = pstatic;
95  pi->data.s.proc_start = pos;
96  pi->data.s.def_end    = 0L;
97  pi->data.s.help_start = 0L;
98  pi->data.s.body_start = 0L;
99  pi->data.s.body_end   = 0L;
100  pi->data.s.example_start = 0L;
101  pi->data.s.proc_lineno = line;
102  pi->data.s.body_lineno = 0;
103  pi->data.s.example_lineno = 0;
104  pi->data.s.body = NULL;
105  return(pi);
106}
107
108/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
109pi_clear(procinfov pi)
110{
111  free(pi->libname);
112  free(pi->procname);
113  free(pi);
114}
115
116/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
117printpi(procinfov pi)
118{
119  FILE *fp = fopen( pi->libname, "rb");
120  char *buf, name[256];
121  int len1, len2;
122
123  if(!found_info && !warning_info) warning_info++;
124  if(!found_version && !warning_version) warning_version++;
125  if(pi->data.s.body_end==0) 
126    pi->data.s.body_end = pi->data.s.proc_end;
127
128  if(lpverbose) printf("//     ");
129  printf( "%c %-15s  %20s ", pi->is_static ? 'l' : 'g', pi->libname,
130          pi->procname);
131  printf("line %4d,%5ld-%-5ld  %4d,%5ld-%-5ld  %4d,%5ld-%-5ld\n",
132         pi->data.s.proc_lineno, pi->data.s.proc_start, pi->data.s.def_end,
133         pi->data.s.body_lineno, pi->data.s.body_start, pi->data.s.body_end,
134         pi->data.s.example_lineno, pi->data.s.example_start,
135         pi->data.s.proc_end);
136  if(check) {
137    if(!pi->is_static && (pi->data.s.body_start-pi->data.s.def_end)<4)
138      printf("*** Procedure '%s' is global and has no help-section.\n",
139             pi->procname);
140    if(!pi->is_static && !pi->data.s.example_start)
141      printf("*** Procedure '%s' is global and has no example-section.\n",\
142             pi->procname);
143    if(found_proc_in_proc)
144      printf("*** found proc within procedure '%s'.\n", pi->procname);
145  }
146
147#if 0
148  if( fp != NULL) { // loading body
149    len1 = pi->data.s.def_end - pi->data.s.proc_start;
150    if(pi->data.s.body_end==0)
151      len2 = pi->data.s.proc_end - pi->data.s.body_start;
152    else len2 = pi->data.s.body_end - pi->data.s.body_start;
153    buf = (char *)malloc(len1 + len2 + 1);
154    fseek(fp, pi->data.s.proc_start, SEEK_SET);
155    fread( buf, len1, 1, fp);
156    *(buf+len1) = '\n';
157    fseek(fp, pi->data.s.body_start, SEEK_SET);
158    fread( buf+len1+1, len2, 1, fp);
159    *(buf+len1+len2+1)='\0';
160    printf("##BODY:'%s'##\n", buf);
161    free(buf);
162
163    // loading help
164    len1 = pi->data.s.body_start - pi->data.s.proc_start;
165    printf("len1=%d;\n", len1);
166    buf = (char *)malloc(len1+1);
167    fseek(fp, pi->data.s.proc_start, SEEK_SET);
168    fread( buf, len1, 1, fp);
169    *(buf+len1)='\0';
170    printf("##HELP:'%s'##\n", buf);
171    free(buf);
172
173    if(pi->data.s.example_start>0) { // loading example
174      fseek(fp, pi->data.s.example_start, SEEK_SET);
175      len2 = pi->data.s.proc_end - pi->data.s.example_start;
176      buf = (char *)malloc(len2+1);
177      fread( buf, len2, 1, fp);
178      *(buf+len2)='\0';
179      printf("##EXAMPLE:'%s'##\n", buf);
180      free(buf);
181    }
182
183    fclose(fp);
184    //getchar();
185  }
186#endif
187}
188
189/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
Note: See TracBrowser for help on using the repository browser.