source: git/Singular/utils.cc @ bee06d

spielwiese
Last change on this file since bee06d was 599326, checked in by Kai Krüger <krueger@…>, 14 years ago
Anne, Kai, Frank: - changes to #include "..." statements to allow cleaner build structure - affected directories: omalloc, kernel, Singular - not yet done: IntergerProgramming git-svn-id: file:///usr/local/Singular/svn/trunk@13032 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 7.1 KB
Line 
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <ctype.h>
5#include <kernel/fegetopt.h>
6#include <Singular/utils.h>
7#include <Singular/libparse.h>
8
9extern FILE *yylpin;
10extern char *optarg;
11extern int optind, opterr, optopt;
12extern int lpverbose, check;
13extern int texinfo_out;
14extern int category_out;
15extern int found_version, found_info, found_oldhelp, found_proc_in_proc;
16int warning_info = 0, warning_version = 0;
17
18static void usage(char *progname)
19{
20  printf("libparse: a syntax-checker for Singular Libraries.\n");
21  printf("USAGE: %s [options] singular-library\n", progname);
22  printf("Options:\n");
23  printf("   -f <singular library> : performs syntax-checks\n");
24  printf("   -d [digit]            : digit=1,..,4 increases the verbosity of the checks\n");
25  printf("   -s                    : turns on reporting about violations of unenforced syntax rules\n");
26  printf("   -i                    : perl output of examples and help of procs\n");
27  printf("   -c                    : print category of lib to stdout and exit\n");
28  printf("   -h                    : print this message\n");
29  exit(1);
30}
31
32static char* lib_file = NULL;
33
34/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
35void main_init(int argc, char *argv[])
36{
37  char c;
38
39  while((c=fe_getopt(argc, argv, "ihdc:sf:"))>=0)
40  {
41    switch(c)
42    {
43        case 'd':
44          lpverbose = 1;
45          if(isdigit(argv[fe_optind-1][0])) sscanf(optarg, "%d", &lpverbose);
46          else fe_optind--;
47          break;
48        case 'f': lib_file = argv[fe_optind-1];
49          break;
50        case 's':
51          check++;
52          break;
53        case 'i':
54          texinfo_out = 1;
55          break;
56        case 'c':
57          category_out = 1;
58          break;
59
60        case 'h' :
61          usage(argv[0]);
62          break;
63        case -1 : printf("no such option:%s\n", argv[fe_optind]);
64          usage(argv[0]);
65          break;
66        default: printf("no such option.%x, %c %s\n", c&0xff, c, argv[fe_optind]);
67          usage(argv[0]);
68    }
69  }
70  if (texinfo_out || category_out) lpverbose = 0;
71
72  if(lib_file!=NULL)
73  {
74    yylpin = fopen( lib_file, "rb" );
75    if (! (texinfo_out || category_out))
76      printf("Checking library '%s'\n", lib_file);
77    else if (! category_out)
78      printf("$library = \"%s\";\n", lib_file);
79  }
80  else
81  {
82    while(argc>fe_optind && yylpin==NULL)
83    {
84      yylpin = fopen( argv[fe_optind], "rb" );
85      if(yylpin!=NULL)
86      {
87        lib_file = argv[fe_optind];
88        if (! (texinfo_out || category_out) )
89          printf("Checking library '%s'\n", argv[fe_optind]);
90        else if (! category_out)
91          printf("$library = \"%s\";\n", lib_file);
92      }
93      else fe_optind++;
94    }
95  }
96  if(yylpin == NULL)
97  {
98    printf("No library found to parse.\n");
99    usage(argv[0]);
100  }
101}
102
103/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
104void 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}
114/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
115
116procinfo *iiInitSingularProcinfo(procinfo* pi, const char *libname,
117                                 const char *procname, int line, long pos,
118                                 BOOLEAN pstatic /*= FALSE*/)
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;
139  pi->data.s.help_chksum = 0;
140  return(pi);
141}
142
143/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
144void pi_clear(procinfov pi)
145{
146  free(pi->libname);
147  free(pi->procname);
148  free(pi);
149}
150
151/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
152
153#ifndef SEEK_SET
154#define SEEK_SET 0
155#endif
156
157static void PrintOut(FILE *fd, int pos_start, int pos_end)
158{
159  if (pos_start <= 0 || pos_end - pos_start <= 4) return;
160  char c = 0;
161
162  fseek(fd, pos_start, SEEK_SET);
163  while (pos_start++ <= pos_end)
164  {
165    if (c == '\\')
166    {
167      c = fgetc(fd);
168      if (c != '"') putchar('\\');
169    }
170    else
171      c = fgetc(fd);
172    if (c == '@' || c == '$') putchar('\\');
173    if (c != '\r') putchar(c);
174  }
175  if (c == '\\') putchar('\\');
176}
177
178
179void printpi(procinfov pi)
180{
181  char *buf, name[256];
182  int len1, len2;
183  /* pi->libname is badly broken -- use file, instead */
184  FILE *fp = fopen( lib_file, "rb");
185
186  if (fp == NULL)
187  {
188    printf("Can not open %s\n", lib_file);
189    return;
190  }
191
192  if(!found_info && !warning_info) warning_info++;
193  if(!found_version && !warning_version) warning_version++;
194  if(pi->data.s.body_end==0)
195    pi->data.s.body_end = pi->data.s.proc_end;
196
197  if (texinfo_out)
198  {
199   if ((! pi->is_static) &&
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");
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      }
214      printf("$chksum{\"%s\"} = %ld;\n", pi->procname, pi->data.s.help_chksum);
215    }
216  }
217  else if (! category_out)
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    }
237  }
238
239  if (fp != NULL) fclose(fp);
240
241}
242
243/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
Note: See TracBrowser for help on using the repository browser.