-
Notifications
You must be signed in to change notification settings - Fork 0
/
asmAlign.c
219 lines (208 loc) · 8.13 KB
/
asmAlign.c
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LSIZE 80 /* Maximum Line Size */
#define FNSIZE 80 /* Maximum File Name Size */
int main(int argc, char *argv[]) {
int i;
int lines; /* Count the lines readed */
int cIndex; /* Index of character in a line */
int lSize; /* Size of the line */
int instructionSize = 4; /* -i */
int separation = 1; /* -s */
int upperCase = 0; /* -u */
int trim = 0; /* -t */
int iReading; /* Boolean values for use while writting in */
int iReaded; /* the new file */
int sReaded;
int iRemaining; /* Chars remaining to complete the instruction */
char value; /* For read characters in args and file */
FILE *file;
FILE *fileAlign;
char *fileName;
char fileNameAlign[FNSIZE];
char fileNameOld[FNSIZE];
char line[LSIZE + 1];
/* Check if file name is provided */
if (argc < 2) {
printf("Error: You must specify a file\n");
return 1;
} else if (strcmp(argv[1], "--usage") && strcmp(argv[1], "--info") &&
strcmp(argv[1], "--help")) {
fileName = argv[1];
/* Open the original file */
file = fopen(fileName, "r");
if (!file) {
perror(fileName);
return 1;
}
}
/* Show usage */
if (argc > 5 || !strcmp(argv[1], "--usage") ||
!strcmp(argv[1], "--info") || !strcmp(argv[1], "--help")) {
printf("USAGE:\n");
printf(" asmAlign [options] <input file>\n");
printf("\n");
printf("OPTIONS:\n");
printf(" -i<number> (The length of the longest instruction)\n");
printf(" (The default number is 4. Min. 1, Max. 9.)\n");
printf(" -s<number> (Space between instruction and parameters)\n");
printf(" (The default number is 1. Min. 1, Max. 9.) \n");
printf(" -u (Write the instructions in upper case)\n");
printf(" -t (Remove white spaces between the parameters)\n");
printf("\n");
printf(" --usage (Print usage information)\n");
printf(" --info (Print usage information)\n");
printf(" --help (Print usage information)\n");
printf("\n");
printf(" Prebuilt options by architecture:\n");
printf(" --z80 (-i4 -s1)\n");
printf("\n");
printf("EXAMPLES:\n");
printf(" asmAlign test.asm\n");
printf(" asmAlign -i4 -s2 test.asm\n");
printf(" asmAlign -i3 -s1 -u -t test.asm\n");
printf("\n");
return 1;
}
/* Read the arguments */
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'i':
value = argv[i][2] - '0';
if (value < 1 || value > 9) {
value = 4;
}
instructionSize = value;
break;
case 's':
value = argv[i][2] - '0';
if (value < 1 || value > 9) {
value = 1;
}
separation = value;
break;
case 'u':
upperCase = 1;
break;
case 't':
trim = 1;
break;
case '-':
if (!strcmp(argv[i], "--z80")) {
instructionSize = 4;
separation = 1;
}
break;
default:
printf("Error: unrecognized command-line option -%c\n",
argv[i][1]);
}
}
}
printf("File: %s. Parameters used: i=%d s=%d u=%d t=%d\n", fileName,
instructionSize, separation, upperCase, trim);
/* Create the new file */
strcpy(fileNameAlign, fileName);
strcat(fileNameAlign, "_align");
fileAlign = fopen(fileNameAlign, "w+");
if (!fileAlign) {
perror(fileNameAlign);
return 1;
}
/* Read the file and make the changes */
lines = 0;
iRemaining = instructionSize;
iReaded = 0;
iReading = 0;
sReaded = 0;
printf("Reading the file...\n");
do {
if (fgets(line, LSIZE + 1, file)) { /* +1 because line contains \n */
lSize = strlen(line);
/* if the line ends with ':' its a label and is printed directly */
if (line[lSize - 2] != ':') {
/* Read the characters of the line */
cIndex = 0;
do {
value = line[cIndex];
if (value == '\n') {
iRemaining = instructionSize;
iReaded = 0;
iReading = 0;
sReaded = 0;
fputc(value, fileAlign);
} else if (value == ' ' || value == '\t') {
/* Reading the first space of the separation */
if (iReading == 1) {
iReaded = 1;
iReading = 0;
for (i = 0; i < separation; i++) {
fputc(' ', fileAlign);
}
sReaded = 1;
/* Reading the first space of the separation */
} else if (iReaded == 1 && sReaded == 0) {
for (i = 0; i < separation; i++) {
fputc(' ', fileAlign);
}
sReaded = 1;
/* Reading the args and not trimming spaces */
} else if (iReaded == 1 && sReaded == 1 &&
iRemaining <= 0 && trim == 0) {
if (iReading == 1) {
fputc(' ', fileAlign);
}
/* Reading the args and trimming spaces */
} else if (iReaded == 1 && sReaded == 1 &&
iRemaining <= 0 && trim == 1) {
/* Remove white spaces */
/* Reading the space before instruction */
} else if (iRemaining == instructionSize &&
iReaded == 0 && sReaded == 0) {
fputc(value, fileAlign);
}
} else if (value != EOF) {
/* Instruction read */
if (iReaded == 0) {
iReading = 1;
if (upperCase) {
if (value >= 'a' && value <= 'z') {
value = value - 32;
}
}
/* Parameters read */
} else if (sReaded == 1) {
while (iRemaining > 0) {
fputc(' ', fileAlign);
iRemaining--;
}
}
fputc(value, fileAlign);
iRemaining--;
}
cIndex++;
} while ((int) value != EOF && cIndex < lSize);
/* The line readed is a label */
} else {
fputs(line, fileAlign);
}
}
lines++;
} while (!feof(file));
fclose(file);
fclose(fileAlign);
/* Rename the file */
strcpy(fileNameOld, fileName);
strcat(fileNameOld, "_old");
if (rename(fileName, fileNameOld) == 1) {
perror(fileName);
}
if (rename(fileNameAlign, fileName) == 0) {
printf("A backup file %s with the original content has been created\n",
fileNameOld);
}
printf("%d lines readed from %s\n", lines, fileName);
return 0;
}