source: git/Singular/utils.cc @ 351bd1

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