First Commit of my working state
[simh.git] / Ibm1130 / ibm1130_fmt.c
1 /*********************************************************************************************
2 * ibm1130_fmt.c : interpret tabs in 1130 Assembler or Fortran source
3 * Bob Flanders
4 * -------------------------------------------------------------------------------------------
5 *
6 * These routines are used by ibm1130_cr.c when the user has indicated
7 * that the input text is formatted with tabs. Input lines are edited
8 * into the appropriate column format. Three edit modes are recognized:
9 *
10 * Assembler mode:
11 * Input lines of the form
12 *
13 * [label]<whitespace>[opcode]<tab>[tag][L]<tab>[argument]
14 *
15 * are rearranged so that the input fields are placed in the appropriate columns
16 *
17 * The label must start on the first character of the line. If there is no label,
18 * the first character(s) before the opcode must be whitespace. Following the opcode, there
19 * MUST be a tab character, followed by the format and tag. Following the format and tag
20 * may be exactly one whitespace character, and then starts the argument.
21 *
22 * Input lines with * in column 1 and blank lines are turned into Assembler comments,
23 * with the * in the Opcode field.
24 *
25 * Assembler directive lines at the beginning of the deck must be preceded by
26 * ! to indicate that they are not comments. For example,
27 *
28 * !*LIST
29 * * This is a comment
30 *
31 * Fortran mode:
32 * Input lines of the form
33 *
34 * [label]<tab>statement
35 *
36 * or
37 *
38 * [label]<tab>Xcontinuation
39 *
40 * where X is a non alphabetic contination character are rearranged in the
41 * appropriate manner:
42 *
43 * 1 2
44 * 12345678901234567890...
45 * ------------------------
46 * label statement
47 * labelXcontinuation
48 *
49 * However, you must take care that you don't end up with statement text after column 72.
50 *
51 * Input lines with * or C in column 1 are left alone (comments and directives)
52 *
53 * (The ! escape is not used before Fortran directives as before Assembler directives)
54 *
55 * Tab mode:
56 * Tabs are replaced with spaces. Tab settings are assumed to be eight characters wide,
57 * as is standard for vi, notepad, etc.
58 *********************************************************************************************/
59
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <memory.h>
63 #include <ctype.h>
64 #include <string.h>
65 #include "ibm1130_fmt.h"
66
67 #define MAXLINE 81 /* maximum output line size */
68 #define WORKSZ 256 /* size for tab work area */
69 #define TAGOFFSET 12 /* offset for tag field */
70 #define FMTOFFSET 11 /* offset for format field */
71
72 #define MIN(a,b) ((a < b) ? a : b)
73 #define AMSG " with Assembler Reformat"
74 #define FMSG " with FORTRAN Reformat"
75 #define AFORMAT "%20.20s%-60.60s"," "
76 #define ACOMMENTFMT "%20.20s%-60.60s"," "
77 #define ABLANKLINE "%20.20s*"," "
78 #define FFORMAT "%-5.5s %-74.74s"
79 #define FCONTFMT "%-5.5s%-75.75s"
80
81 char gszLabel[6]; /* work area for label */
82 char gszArg[MAXLINE]; /* .. argument */
83 char gszOutput[MAXLINE]; /* .. output */
84 short gaiAsmTabs[] = {7,12,15,20,25,30,35,40,45,52,0};/* tab stops for assembler */
85
86 short gaiPlainTabs[] = {9, 17, 25, 33, 41, 49, 57, 65, 73, 0};/* tab stops for just plain tabs */
87
88 /*
89 * helper routines
90 */
91
92 /*************************************************
93 * ExpandTabs: Expand tabs to spaces
94 */
95
96 char* ExpandTabs(char* p_szInbuf, /* expand tabs .. input buffer */
97 char* p_szOutbuf, /* .. output buffer */
98 short* p_aiTabs) /* .. array of tab stops (1 based) -- 0 end of array */
99 {
100 short iI, /* input position */
101 iO, /* output position */
102 iT; /* next tab stop */
103
104 char cX; /* character to test */
105
106 iI = 0; /* init input position */
107 iO = 0; /* init output position */
108 iT = 0; /* init tab stop */
109
110 while ((cX = *(p_szInbuf + iI)) != 0) /* while there are characters */
111 {
112 if (cX == '\t') /* q. tab character? */
113 { /* a. yes .. */
114 while ((p_aiTabs[iT] <= iO + 1) /* search for next valid stop .. */
115 && (p_aiTabs[iT] != 0)) /* .. or end of table */
116 iT++; /* .. go to next tab */
117
118 if (p_aiTabs[iT] != 0) /* q. end of tab array? */
119 { /* a. no .. */
120 while (iO < (p_aiTabs[iT] - 1)) /* fill to tab with blanks */
121 *(p_szOutbuf + iO++) = ' '; /* .. put in a blank */
122
123 }
124 else /* Otherwise ... */
125 *(p_szOutbuf + iO++) = ' '; /* .. Translate to blank */
126 }
127 else /* Otherwise .. not tab */
128 *(p_szOutbuf + iO++) = cX; /* .. save the input char */
129
130 iI++; /* next input character */
131 }
132
133 *(p_szOutbuf + iO) = 0; /* end the string.. */
134 return p_szOutbuf; /* .. return output area addr */
135 }
136
137 /*************************************************
138 * extract next token, modify pointer
139 */
140
141 char* GetToken(char* p_szOut, /* output location */
142 int p_iLen, /* max output length */
143 char**p_pszToken) /* pointer to input token */
144 {
145 int iI; /* work integer */
146 char* pszX; /* work pointer */
147
148 pszX = *p_pszToken; /* get pointer to token */
149
150 for (iI = 0; *(pszX + iI) && (!isspace(*(pszX + iI)));) /* while not whitespace & not end */
151 iI++; /* .. count token length */
152
153 memset(p_szOut, 0, p_iLen); /* zero out output area */
154
155 if (iI > 0) /* q. any chars? */
156 strncpy(p_szOut, *p_pszToken, MIN(iI, p_iLen-1)); /* a. yes.. copy max of p_iLen-1 */
157
158 *p_pszToken += iI; /* point beyond token */
159 return p_szOut; /* .. return token pointer */
160 }
161
162 /*************************************************
163 * EditToAsm - convert tab-formatted text line to 1130 Assembler format
164 */
165
166 char *EditToAsm (char* p_pszEdit) /* convert line to 1130 assembler */
167 {
168 char pszLine[MAXLINE]; /* source line */
169 char pszWork[WORKSZ]; /* work buffer */
170 char acTFWrk[2]; /* tag/format work area */
171 size_t iI; /* work integer */
172
173 if (p_pszEdit == NULL) /* q. null request? */
174 return AMSG; /* a. yes .. return display message */
175
176 if (*p_pszEdit == '!') /* leave lines starting with ! alone */
177 return EditToWhitespace(p_pszEdit+1);
178
179 if (*p_pszEdit == '*') /* q. comment line? */
180 { /* a. yes.. */
181 strncpy(pszWork, EditToWhitespace(p_pszEdit), MAXLINE); /* .. convert any tabs */
182 sprintf(gszOutput, ACOMMENTFMT, pszWork); /* .. put the comment out there in the opcode column */
183 return gszOutput; /* .. and return it */
184 }
185
186 strncpy(pszLine, p_pszEdit, MAXLINE-1); /* copy the line local */
187
188 ExpandTabs(pszLine, pszWork, gaiAsmTabs); /* expand the tabs */
189 strncpy(pszLine, pszWork, MAXLINE-1); /* copy the line back */
190
191 for (iI = strlen(pszLine); iI--;) /* trim trailing whitespace */
192 {
193 if (*(pszLine + iI) <= ' ') /* q. space or less? */
194 *(pszLine + iI) = 0; /* a. yes .. remove it */
195 else /* otherwise */
196 break; /* .. done. Leave loop. */
197 }
198
199 if (strlen(pszLine) == 0) /* q. blank line? */
200 { /* a. yes .. Assembler abhors these so */
201 sprintf(gszOutput, ABLANKLINE); /* format as comment statement */
202 return gszOutput; /* .. and return it */
203 }
204
205
206 /* TODO: Add code to process a strip switch
207 * comment?
208 */
209
210 if (strlen(pszLine) > (TAGOFFSET + 1)) /* q. line long enough? */
211 { /* a. yes.. reorder tag/format */
212 memcpy(acTFWrk, pszLine + FMTOFFSET, 2); /* get tag/format */
213 memset((pszLine + FMTOFFSET), ' ', 2); /* .. blank 'em out */
214
215 for (iI = 0; iI < 2; iI ++)
216 if (isalpha(acTFWrk[iI])) /* q. alpha char? */
217 *(pszLine + FMTOFFSET) = acTFWrk[iI]; /* a. yes .. make it format */
218 else if (isdigit(acTFWrk[iI])) /* q. digit? */
219 *(pszLine + TAGOFFSET) = acTFWrk[iI]; /* a. yes .. make it the tag */
220 }
221
222 sprintf(gszOutput, AFORMAT, pszLine); /* format the line */
223
224 return gszOutput; /* return formatted line */
225 }
226
227 /*************************************************
228 * EditToFortran - convert tab-formatted input text line to FORTRAN format
229 * (a la DEC Fortran)
230 */
231
232 char *EditToFortran(char* p_pszEdit) /* convert line to 1130 assembler */
233 {
234 char pszLine[MAXLINE]; /* source line */
235 char* pszWork; /* work pointer */
236 size_t iI; /* work integer */
237 int bContinue; /* true if continue */
238
239 if (p_pszEdit == NULL) /* q. null request? */
240 return FMSG; /* a. yes .. return display message */
241
242 if (strchr(p_pszEdit, '\t') == NULL) /* q. no tab in the line? */
243 return p_pszEdit; /* a. nope, return line as is, assume it's formatted correctly */
244
245 if (*p_pszEdit == 'C' || *p_pszEdit == '*' || *p_pszEdit == '\0') /* q. comment or directive or blank line? */
246 { /* a. yes.. don't restructure */
247 return EditToWhitespace(p_pszEdit);
248 }
249
250 strncpy(pszLine, p_pszEdit, MAXLINE-1); /* copy the line local */
251
252 for (iI = strlen(pszLine); iI--;) /* trim trailing whitespace */
253 {
254 if (*(pszLine + iI) <= ' ') /* q. space or less? */
255 *(pszLine + iI) = 0; /* a. yes .. remove it */
256 else /* otherwise */
257 break; /* .. done. Leave loop. */
258 }
259
260 /*
261 * TODO: Add code to process a strip switch
262 * comment?
263 */
264
265 pszWork = (char*) pszLine; /* set pointer to line */
266 GetToken(gszLabel, 6, &pszWork); /* get the line, if any. */
267
268 pszWork++; /* skip tab/whitespace */
269
270 /* continuation... */
271 bContinue = ((isdigit(*pszWork) && (*pszWork != '0')) /* if first char non-zero digit */
272 || (!isspace(*pszWork) && !isalpha(*pszWork))); /* .. or non-alpha non-blank */
273
274 memset(gszArg, 0, MAXLINE); /* .. and arguments */
275
276 strncpy(gszArg, pszWork, 75); /* copy rest to argument */
277
278 sprintf(gszOutput, (bContinue) ? FCONTFMT : FFORMAT, /* format the line */
279 gszLabel, /* .. statement # */
280 gszArg); /* .. code */
281
282 return gszOutput; /* return formatted line */
283 }
284
285 /*************************************************
286 * EditToWhitespace - expand tabs at 8 space intervals.
287 */
288
289 char* EditToWhitespace(char *p_pszEdit)
290 {
291 int iI; /* work integer */
292 char pszLine[MAXLINE]; /* source line */
293 char pszWork[WORKSZ]; /* work buffer */
294
295 if (p_pszEdit == NULL) /* q. null request? */
296 return AMSG; /* a. yes .. return display message */
297
298 strncpy(pszLine, p_pszEdit, MAXLINE-1); /* copy the line local */
299
300 ExpandTabs(pszLine, pszWork, gaiPlainTabs); /* expand the tabs */
301 strncpy(gszOutput, pszWork, MAXLINE-1); /* copy the line back */
302
303 for (iI = strlen(gszOutput); iI--;) /* look at each character */
304 {
305 if (*(gszOutput + iI) <= ' ') /* q. space or less? */
306 *(gszOutput + iI) = 0; /* a. yes .. remove it */
307 else /* otherwise */
308 break; /* .. done. Leave loop. */
309 }
310
311
312 return gszOutput; /* ... return buffer */
313 }