1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef GLK_TADS_TADS2_LINE_SOURCE_FILE
24 #define GLK_TADS_TADS2_LINE_SOURCE_FILE
25 
26 #include "glk/tads/tads2/lib.h"
27 #include "glk/tads/tads2/debug.h"
28 #include "glk/tads/tads2/line_source.h"
29 #include "glk/tads/tads2/object.h"
30 
31 namespace Glk {
32 namespace TADS {
33 namespace TADS2 {
34 
35 struct tokpdef;
36 
37 /* maximum number of pages of debugging records we can keep */
38 #define LINFPGMAX 128
39 
40 /*
41  *   executable line information structure: this record relates one
42  *   executable line to the object containing the p-code, and the offset
43  *   in the object of the p-code for the start of the line
44  */
45 struct linfinfo {
46     /*
47      *   OPCLINE data (file seek position or line number, depending on how
48      *   the game was compiled: -ds -> file seek offset, -ds2 -> line
49      *   number)
50      */
51     ulong fpos;
52 
53     /* object number */
54     objnum objn;
55 
56     /* offset from start of code */
57     uint ofs;
58 };
59 
60 /*
61  *   file line source
62  */
63 struct linfdef {
64     lindef    linflin;                                   /* superclass data */
65     osfildef *linffp;                  /* file pointer for this line source */
66     char      linfbuf[100];                 /* buffer for the line contents */
67     int       linfbufnxt;         /* offset in buffer of start of next line */
68     int       linfnxtlen;                /* length of data after linfbufnxt */
69     ulong     linfnum;                               /* current line number */
70     ulong     linfseek;                    /* seek position of current line */
71     mcmcxdef *linfmem;                            /* memory manager context */
72     mcmon     linfpg[LINFPGMAX];             /* pages for debugging records */
73     ulong     linfcrec;        /* number of debugger records written so far */
74     char      linfnam[1];                        /* name of file being read */
75 };
76 
77 /* initialize a file line source, opening the file for the line source */
78 linfdef *linfini(mcmcxdef *mctx, errcxdef *errctx, const char *filename,
79                  int flen, tokpdef *path, int must_find_file,
80                  int new_line_records);
81 
82 /* initialize a pre-allocated linfdef, skipping debugger page setup */
83 void linfini2(mcmcxdef *mctx, linfdef *linf,
84               const char *filename, int flen, osfildef *fp, int new_line_records);
85 
86 /* get next line from line source */
87 int linfget(lindef *lin);
88 
89 /* generate printable rep of current position in source (for errors) */
90 void linfppos(lindef *lin, char *buf, uint bufl);
91 
92 /* close line source */
93 void linfcls(lindef *lin);
94 
95 /* generate source-line debug instruction operand */
96 void linfglop(lindef *lin, uchar *buf);
97 
98 /* generate new-style source-line debug instructino operand */
99 void linfglop2(lindef *lin, uchar *buf);
100 
101 /* save line source to binary (.gam) file */
102 int linfwrt(lindef *lin, osfildef *fp);
103 
104 /* load a file-line-source from binary (.gam) file */
105 int linfload(osfildef *fp, dbgcxdef *dbgctx, errcxdef *ec,
106              tokpdef *path);
107 
108 /* add a debugger line record for the current line */
109 void linfcmp(lindef *lin, uchar *buf);
110 
111 /* find nearest line record to a file seek location */
112 void linffind(lindef *lin, char *buf, objnum *objp, uint *ofsp);
113 
114 /* activate line source for debugging */
115 void linfact(lindef *lin);
116 
117 /* disactivate line source */
118 void linfdis(lindef *lin);
119 
120 /* get current seek position */
121 void linftell(lindef *lin, uchar *pos);
122 
123 /* seek */
124 void linfseek(lindef *lin, uchar *pos);
125 
126 /* read */
127 int linfread(lindef *lin, uchar *buf, uint siz);
128 
129 /* add a signed delta to a seek positon */
130 void linfpadd(lindef *lin, uchar *pos, long delta);
131 
132 /* query whether at top of file */
133 int linfqtop(lindef *lin, uchar *pos);
134 
135 /* read one line at current seek position */
136 int linfgets(lindef *lin, uchar *buf, uint bufsiz);
137 
138 /* get name of line source */
139 void linfnam(lindef *lin, char *buf);
140 
141 /* get the current line number */
142 ulong linflnum(lindef *lin);
143 
144 /* go to top or bottom */
145 void linfgoto(lindef *lin, int where);
146 
147 /* return the current offset in the line source */
148 long linfofs(lindef *lin);
149 
150 /* renumber an object */
151 void linfren(lindef *lin, objnum oldnum, objnum newnum);
152 
153 /* delete an object */
154 void linfdelnum(lindef *lin, objnum objn);
155 
156 /* copy line records to an array of linfinfo structures */
157 void linf_copy_linerecs(linfdef *linf, linfinfo *info);
158 
159 /* debugging echo */
160 #ifdef DEBUG
161 # define LINFDEBUG(x) x
162 #else /* DEBUG */
163 # define LINFDEBUG(x)
164 #endif /* DEBUG */
165 
166 } // End of namespace TADS2
167 } // End of namespace TADS
168 } // End of namespace Glk
169 
170 #endif
171