aboutsummaryrefslogtreecommitdiff
path: root/renumbas.c
blob: 6f4f51ed6a7580a8f805f2e42e304854debf651c (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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

#include "bas.h"
#include "bcdfp.h"
#include "linetab.h"

unsigned short startlineno = 10;
unsigned short increment   = 10;
unsigned short limit       = 0;
unsigned short newno;
int backwards = 0;

void print_help(void) {
   printf("Usage: %s [-v] [-s start-lineno] [-i increment] [-f first-lineno] <inputfile> <outputfile>\n", self);
   printf("        -v: Verbose.\n");
   printf("  -s <num>: Starting line number (default: 10).\n");
   printf("  -i <num>: Increment (default: 10).\n");
   printf("  -f <num>: Don't renumber lines less than <num> (default: 0).\n");
	printf("        -b: Number backwards (creates invalid program).\n");
}

unsigned short getlineno(char opt, const char *arg) {
   int lineno;
   char *e;

   lineno = (int)strtol(arg, &e, 10);

   if(*e) {
      fprintf(stderr, "%s: Invalid line number for -%c option: %s is not a number.\n",
            self, opt, arg);
      exit(1);
   }

   if(lineno < 0 || lineno > 32767) {
      fprintf(stderr, "%s: Invalid line number for -%c option: %d > 32767.\n",
            self, opt, lineno);
      exit(1);
   }

   return ((unsigned short)lineno);
}

void parse_args(int argc, char **argv) {
	int opt;

	while( (opt = getopt(argc, argv, "vbs:i:f:")) != -1) {
		switch(opt) {
			case 'v': verbose   = 1; break;
			case 'b': backwards = 1; break;
			case 's': startlineno = getlineno(opt, optarg); break;
			case 'i': increment   = getlineno(opt, optarg); break;
			case 'f': limit       = getlineno(opt, optarg); break;
			default:
						 print_help();
						 exit(1);
		}
	}

	if(optind >= argc)
		die("No input file given (use - for stdin).");
	else
		open_input(argv[optind]);

	if(++optind >= argc)
		die("No output file given (use - for stdout).");
	else
		output_filename = argv[optind];
}

CALLBACK(renumber_line) {
	int i;
	unsigned char fpnewno[6];

	if(lineno < limit || lineno == 32768) return;
	if(newno >= 32768) {
		fprintf(stderr, "%s: Fatal: New line number %d would be >32767.\n", self, newno);
		exit(1);
	}

	if(verbose)
		fprintf(stderr, "Renumbering line %d as %d (%d refs).\n", lineno, newno, refcounts[lineno]);

	int2fp(newno, fpnewno);
	for(i = 0; i < refcounts[lineno]; i++)
		memmove(program + linerefs[lineno][i].pos, fpnewno, 6);
	setword(pos, newno);

	if(backwards) {
		if(newno < increment) {
			fprintf(stderr, "%s: Fatal: New line number %d would be <0.\n", self, newno);
			exit(1);
		} else {
			newno -= increment;
		}
	} else {
		newno += increment;
	}
}

void check_refs(void) {
	int i, j;

	for(i = 0; i < 32768; i++) {
		if(refcounts[i] && !lines_exist[i]) {
			for(j = 0; j < refcounts[i]; j++) {
				fprintf(stderr, "%s: Warning: Line %d references nonexistent line %d.\n",
						self, linerefs[i][j].lineno, i);
			}
		}
	}
}

void renumber(void) {
	check_refs();
	newno = startlineno;
	on_start_line = renumber_line;
	walk_all_code();
}

int main(int argc, char **argv) {
	set_self(*argv);
	parse_general_args(argc, argv, print_help);
	parse_args(argc, argv);

	readfile();
	parse_header();

	build_ref_table();
	renumber();
	free_ref_table();

	open_output(output_filename);
   writefile();

	return 0;
}