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