summaryrefslogtreecommitdiff
path: root/support/ltx2x/l2xixref.c
blob: e787a630b76f8b59cdfb850780b1956a901c756d (plain)
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
/* l2xixref.c   Interpeter X-referencing  */
/* BASED ON: xref.c  (unaltered except for common names) */

#include <stdio.h>
#include "l2xicmon.h"
#include "l2xiscan.h"
#include "l2xisymt.h"

#define DEBUG 0

#define MAX_LINENUMS_PER_LINE 10

/* line number item and list header */
typedef struct linenum_item {
  struct linenum_item *next;
  int line_number;
} LINENUM_ITEM, *LINENUM_ITEM_PTR;

typedef struct {
  LINENUM_ITEM_PTR first_linenum;
  LINENUM_ITEM_PTR last_linenum;
} LINENUM_HEADER, *LINENUM_HEADER_PTR;

/* EXTERNALS */

extern int line_number;
extern TOKEN_CODE token;
extern char word_string[];
extern SYMTAB_NODE_PTR symtab_root;


/***************************************************************************/
/* main(argc, argv()  loop to process identifiers, then print xref         */
/*          call as: xref infile                                           */

main(argc, argv)
int argc;
char *argv[];
{
  SYMTAB_NODE_PTR np;             /* ptr to symtab entry */
  LINENUM_HEADER_PTR hp;          /* ptr to line item list header */

  if (DEBUG) {
    fprintf(stderr, "\n ****Starting program xref\nCalling init_scanner\n");
    fflush(stderr);
  }

  init_scanner(argv[1]);

  if (DEBUG) {
    fprintf(stderr, "Returned from init_scanner\nProcessing tokens\n");
    fflush(stderr);
  }

  /* process tokens until end of file */
  do {
    get_token();
    if (token == END_OF_FILE) break;
    if (token == IDENTIFIER) {
      /* put identifier into symtab if not there, and record the */
      /* line number in the symtab entry */
      np = search_symtab(word_string, symtab_root);
      if (np == NULL) {
        np = enter_symtab(word_string, &symtab_root);
        hp = alloc_struct(LINENUM_HEADER);
        hp->first_linenum = hp->last_linenum = NULL;
        np->info = (char *) hp;
      }
      record_line_number(np, line_number);
    }
  } while (token != PERIOD); /* end do */

  if (DEBUG) {
    fprintf(stderr, "Finished token processing\nCalling print_xref\n\n");
    fflush(stderr);
  }

  /* print the xref listing */
  printf("\n\nCross-Reference");
  printf(  "\n---------------\n");
  print_xref(symtab_root);

  if (DEBUG) {
    fprintf(stderr, "\n\nReturned from print_xref\nCalling quit_scanner\n");
    fflush(stderr);
  }

  quit_scanner();

  if (DEBUG) {
    fprintf(stderr, "Returned from quit_scanner\n***That's all\n\n");
    fflush(stderr);
  }

}                                                              /* end main */
/***************************************************************************/



/***************************************************************************/
/* record_line_number(np, number) Record a line number into the            */
/*                                symtab entry                             */

record_line_number(np, number)
SYMTAB_NODE_PTR np;                  /* ptr to symtab entry */
int number;                          /* line number */
{
  LINENUM_ITEM_PTR ip;               /* ptr to line item */
  LINENUM_HEADER_PTR hp;               /* ptr to line item list header */

  /* create a new line number item */
  ip = alloc_struct(LINENUM_ITEM);
  ip->line_number = number;
  ip->next = NULL;

  /* link it to the list */
  hp = (LINENUM_HEADER_PTR) np->info;
  if (hp->first_linenum == NULL) hp->first_linenum = hp->last_linenum = ip;
  else {
    (hp->last_linenum)->next = ip;
    hp->last_linenum = ip;
  }

}                                                /* end record_line_number */
/***************************************************************************/



/***************************************************************************/
/* print_xref(np) Print x-refed names and line numbers in alpha order      */

print_xref(np)
SYMTAB_NODE_PTR np;               /* pointer to subtree */
{
  int n;
  LINENUM_ITEM_PTR ip;            /* ptr to line item */
  LINENUM_HEADER_PTR hp;          /* ptr to line item list header */

  if (np == NULL) return;

  /* print the left subtree */
  print_xref(np->left);

  /* then print the root of the subtree with at most MAX_LINENUMS_PER_LINE */
  printf("\n%-16s   ", np->name);
  n = strlen(np->name) > 16 ? 0 : MAX_LINENUMS_PER_LINE;
  hp = (LINENUM_HEADER_PTR) np->info;
  for (ip = hp->first_linenum; ip != NULL; ip = ip->next) {
    if (n == 0) {
      printf("\n%-16s   ", " ");
      n = MAX_LINENUMS_PER_LINE;
    }
    printf(" %4d", ip->line_number);
    --n;
  }
  printf("\n");

  /* now print the right subtree */
  print_xref(np->right);

}                                                        /* end print_xref */
/***************************************************************************/



/***************************************************************************/
/* ()  */
/*

()
{


} /* end */
/***************************************************************************/



/***************************************************************************/
/* ()  */
/*

()
{


} /* end */
/***************************************************************************/



/***************************************************************************/
/* ()  */
/*

()
{


} /* end */
/***************************************************************************/