File: debug.c

package info (click to toggle)
regina 2.2-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,332 kB
  • ctags: 4,775
  • sloc: ansic: 38,518; sh: 2,552; lex: 1,878; yacc: 1,028; makefile: 771
file content (352 lines) | stat: -rw-r--r-- 10,877 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#ifndef lint
static char *RCSid = "$Id: debug.c,v 1.3 2001/01/25 22:15:47 mark Exp $";
#endif

/*
 *  The Regina Rexx Interpreter
 *  Copyright (C) 1992-1994  Anders Christensen <anders@pvv.unit.no>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "rexx.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>

#ifndef NDEBUG

void dumpvars( const tsd_t *TSD, cvariableptr *hashptr )
{
/* change by bja: dumpvars to use stderr, as trace or listleaked do */
/* modified my mh: respect stdout_for_stderr OPTION also */
/* change by bja: display variable's length in front of value       */
   cvariableptr ptr=NULL, tptr=NULL ;
   int i=0, j=0, k=0 ;
   FILE *fp=stderr;

   if ( get_options_flag( TSD->currlevel, EXT_STDOUT_FOR_STDERR ) )
      fp = stdout;
   if (hashptr==NULL)
      hashptr = (cvariableptr *) TSD->currlevel->vars ;

   fprintf(fp,"\nDumping variables to <stdout>\n") ;
   for (i=0;i!=HASHTABLENGTH;i++)
   {
      if (hashptr[i]!=NULL)
         fprintf(fp,"   Variables from bin no %d\n",i) ;
      for (ptr=hashptr[i];ptr!=NULL;ptr=ptr->next)
      {
         if ((ptr->name->value)[ptr->name->len-1]=='.')
         {
#if 0
            fprintf(fp,"   >>> Stem    : //%s// Default: //%s//  Values:\n",
                  ptr->name->value,(ptr->value)?(ptr->value->value):"<none>") ;
#else
            fprintf(fp,"   >>> Stem    : ");
            fprintf(fp,"(%d) :",ptr->name->len);
            for (k=0;k<ptr->name->len;k++)
               putc(ptr->name->value[k],fp);
 /* avoid problems with exposed variables                                              bja */
            if (ptr->realbox)
            {
               fprintf(fp," Exposed!\n");
               continue;
            }
 /* end of avoid problems with exposed variables                                       bja */
            fprintf(fp, " Default: [");
            if (ptr->value)
            {
               fprintf(fp,"(%d) :",ptr->value->len);
               for (k=0;k<ptr->value->len;k++)
                  putc(ptr->value->value[k],fp);
            }
            else
               fprintf(fp, "<none>");
            fprintf(fp, "]  Values:\n");
#endif
            for (j=0;j<HASHTABLENGTH;j++)
            {
               if ((tptr=((ptr->index))[j])!=NULL)
               {
                  fprintf(fp, "      Sub-bin no %d\n",j) ;
                  for (;tptr;tptr=tptr->next)
                  {
                     if (tptr->name)
                     {
#if 0
                        fprintf(fp, "      >>> Variable: //%s// Value: //%s//\n",
                              tptr->name->value,tptr->value->value) ;
#else
                        fprintf(fp, "      >>> Tail: ");
                        fprintf(fp, "(%d) :",tptr->name->len);
                        for (k=0;k<tptr->name->len;k++)
                           putc(tptr->name->value[k],fp);
                        if ( tptr->num ) /* variable is a number */
                        {
/* try printing numbers a bit better...                                                bja
                           fprintf(fp, " (number: %s) ",ptr->num->num );
 */                                                                                  /* bja */
                           fprintf(fp, " (number: ");                             /* bja */
                           if (tptr->num->negative) fprintf(fp,"-");              /* bja */
                           fprintf(fp, "%.*s) ",tptr->num->size, tptr->num->num );/* bja */
                        }
                        fprintf(fp," Flag: %d hwired: %ld valid: %ld ",ptr->flag, ptr->hwired, ptr->valid );
                        fprintf(fp," Value: [");
                        if (tptr->value)
                        {
                           fprintf(fp,"(%d) :",tptr->value->len);
                           for (k=0;k<tptr->value->len;k++)
                              putc(tptr->value->value[k],fp);
                        }
                        else
                        {                                               /* added bja */
                           fprintf(fp,"<none>");                           /* added bja */
                        }
                        fprintf(fp,"]\n");
#endif
                     }
                  }
               }
            }
         }
         else
         {
#if 0
            fprintf(fp,"   >>> Variable: //%s// Value: //%s//\n",
                                     ptr->name->value,ptr->value->value) ;
#else
            fprintf(fp,"   >>> Variable: ");
            fprintf(fp,"(%d) :",ptr->name->len);
            for (k=0;k<ptr->name->len;k++)
               putc(ptr->name->value[k],fp);
 /* avoid problems with exposed variables                                              bja */
            if (ptr->realbox)
            {
               fprintf(fp," Exposed\n");
               continue;
            }
 /* end of avoid problems with exposed variables                                       bja */
            fprintf(fp," Value: [");
            if (ptr->value)
            {
               fprintf(fp,"(%d) :",ptr->value->len);
               for (k=0;k<ptr->value->len;k++)
                  putc(ptr->value->value[k],fp);
            }
            fprintf(fp,"]\n");
#endif
         }
      }
   }

   return ;
}


void dumptree(const tsd_t *TSD, const treenode *this, int level, int newline)
{
   int i=0, j=0 ;
   streng *ptr=NULL;
   FILE *fp=stderr;

   if ( get_options_flag( TSD->currlevel, EXT_STDOUT_FOR_STDERR ) )
      fp = stdout;
   if ((this->charnr)!=0
   &&  (this->charnr)!=(-1))
   {
      if (newline)
         putc('\n',fp);
      for (i=0;i!=level;i++) fprintf(fp,"  ") ;
      fprintf(fp,"Lineno: %d   Charno: %d", this->lineno, this->charnr) ;
      if (newline)
      {
         ptr = getsourceline(TSD, this->lineno, this->charnr,
                             &TSD->systeminfo->tree) ;
         fprintf(fp," Sourceline: [");
         for(i=0;i<ptr->len;i++)
            putc(ptr->value[i],fp);
         putc(']',fp);
      }
      putc('\n',fp);
   }

   for (i=0;i!=level;i++)
      fprintf(fp,"  ") ;
   fprintf(fp,">>>  in type=%d == %s\n", this->type, getsym(this->type)) ;

   for (i=0;i!=level;i++) fprintf(fp,"  ") ;
   fprintf(fp,"Flags: lnum %d rnum %d lsvar %d rsvar %d lcvar %d rcvar %d\n",
      this->u.flags.lnum,
      this->u.flags.rnum,
      this->u.flags.lsvar,
      this->u.flags.rsvar,
      this->u.flags.lcvar,
      this->u.flags.rcvar );

   if ((this->name)!=NULL)
   {
      for (i=0;i!=level;i++) fprintf(fp,"  ") ;
      fprintf(fp,"Name: [");
      for (i=0;i<this->name->len;i++)
         putc(this->name->value[i],fp);
      fprintf(fp,"]\n") ;
   }

   for (j=0;j<sizeof(this->p)/sizeof(this->p[0]);j++)
      if (this->p[j]!=NULL)
      {
         for (i=0;i!=level;i++)
            fprintf(fp,"  ") ;
         fprintf(fp,"==> (%d) going down in branch %d, type %d = %s\n",
                this->type,j+1,this->p[j]->type,getsym(this->p[j]->type)) ;
         dumptree( TSD, this->p[j], level+1, 0 ) ;
      }

   for (i=0;i!=level;i++)
      fprintf(fp,"  ") ;
   fprintf(fp,"<<< out type=%d == %s\n", this->type,getsym(this->type)) ;

   if (this->next)
      dumptree( TSD, this->next, level, 1 ) ;

}

#endif /* !NDEBUG */


#ifdef TRACEMEM
void marksource( clineboxptr ptr )
{
   for (;ptr;ptr=ptr->next) {
      markmemory( ptr->line,TRC_SOURCEL ) ;
      markmemory( (char *)ptr, TRC_SOURCE ) ; }
}
#endif


static const char *sourceline( int line, const internal_parser_type *ipt, unsigned *size)
{
   clineboxptr first;
   const otree *otp;

   if (ipt->first_source_line == NULL)
   { /* must be incore_source but that value may be NULL because of a failed
      * instore[0] of RexxStart!
      */
      otp = ipt->srclines; /* NULL if incore_source==NULL */
      while (otp && (otp->num < line)) {
         line -= otp->num;
         otp = otp->next;
      }
      if (otp == NULL)
      {
         *size = 0 ;
         return NULL ;
      }
      line--;
      *size = otp->elems[line].length ;
      return ipt->incore_source + otp->elems[line].offset ;
   }
   first = ipt->first_source_line;
   for (;first;)
   {
      if (first->lineno==line)
      {
         *size = first->line->len ;
         return first->line->value ;
      }
      else
         first = (first->lineno<line) ? first->next : first->prev ;
   }

   *size = 0 ;
   return NULL ;
}



streng *getsourceline( const tsd_t *TSD, int line, int charnr, const internal_parser_type *ipt )
{
   int dquote=0, squote=0 ;
   unsigned len ;
   streng *string ;
   const char *ptr, *chptr, *chend, *tmptr ;
   char *outptr ;

   assert( charnr>=0 ) ;
   if (!charnr)
     charnr++ ;

   ptr = sourceline(line,ipt,&len) ;
/*   assert( ptr ) ; */
   if (!ptr || (charnr >= len))
      return nullstringptr() ;

   chptr = ptr + --charnr ;
   chend = ptr + len ;
   for (; (chptr < chend) && isspace(*chptr); chptr++) ;
   string = Str_makeTSD(BUFFERSIZE+1) ;
   outptr = string->value ;

   for (;;)
   {
restart:
      if (chptr>=chend || outptr >= string->value + BUFFERSIZE)
         break ;

      if (!squote && *chptr=='\"')
         dquote = !dquote ;

      else if (!dquote && *chptr=='\'')
         squote = !squote ;

      else if (!(dquote || squote))
      {
         switch (*chptr)
         {
            case ',':
               for(tmptr=chptr+1; tmptr<chend && isspace(*tmptr); tmptr++ ) ;
               assert( tmptr<=chend ) ;
               if (tmptr==chend)
               {
                  *(outptr++) = ' ' ;
                  chptr = sourceline(++line,ipt,&len) ;
                  chend = chptr + len ;
                  for(; chptr<chend && isspace(*chptr); chptr++) ;
                  goto restart;
               }
               break ;

            case ':':
               *(outptr++) = *chptr ;

            case ';':
               goto endloop ;

         }
      }

      *(outptr++) = *(chptr++) ;
   }

endloop:
   assert( outptr - string->value <= BUFFERSIZE ) ;
   *outptr = '\0'; /* needs to be 0-terminated */
   string->len = outptr - string->value ;
   return string ;
}