Introduce old redir program
[lcapit-junk-code.git] / pet-projects / DLW-1e / das / pass1.c
blobe50513a06c7ad27ecf2934057254a4fe7c8df299
1 #include <stdio.h>
2 #include <string.h>
3 #include <ctype.h>
4 #include <assert.h>
5 #include <errno.h>
7 #include "pass1.h"
8 #include "file.h"
9 #include "symbol_tbl.h"
10 #include "error.h"
12 static char *read_label(char *line, int lcount, int line_nr, int *pass1_error)
14 int err;
15 size_t len;
16 char *p, label[LABEL_LEN];
18 p = strchr(line, ':');
19 if (!p)
20 return line;
22 len = p - line;
23 if (len >= (sizeof(label) - 1)) {
24 error(line_nr, "label is too big");
25 *pass1_error = 1;
28 *p++ = '\0';
30 if (*pass1_error == 0) {
31 memset(label, 0, sizeof(label));
32 strncpy(label, line, sizeof(label) - 1);
33 err = symbol_tbl_insert(label, lcount, line_nr);
34 if (err)
35 *pass1_error = 1;
38 return skip_spaces(p, strlen(p));
41 /* das_pass1()
43 * Perform assembler first pass
45 * o Populates the symbol table
46 * o Remove prefixed spaces
47 * o Remove labels
49 * asmfp: asm FILE pointer (input)
50 * interfp: intermediate FILE pointer (output)
51 * debug: debug mode
53 int das_pass1(FILE *asmfp, FILE *interfp, int debug)
55 char line[DEF_LINE_LEN], *p;
56 int line_nr, lcount, err, ret;
58 err = lcount = 0;
60 for (line_nr = 1;; line_nr++) {
61 ret = file_read_line(asmfp, line, DEF_LINE_LEN);
62 if (ret == 1)
63 break;
65 p = skip_spaces(line, strlen(line));
66 if (!p)
67 p = line;
69 p = read_label(p, lcount, line_nr, &err);
71 assert(p != NULL);
73 ret = fprintf(interfp, "%s", p);
74 if (ret < 0) {
75 fatal("pass1 could not write to file: %s",
76 strerror(errno));
79 if (isalpha(*p))
80 lcount += 2;
83 if (debug)
84 symbol_tbl_dump();
86 return err;