📄 main.c
字号:
#include "cmm.h"#include <unistd.h>#include <fcntl.h>Interface *IR = &x86IR;struct arg{ char *name; int generated;};static char *infile, *outfile;static int fs, fc, fo; /*-S选项,-c选项, -o选项*/static int doarg(int argc, char *argv[]);static char *genname(char *path, char *suffix);static char *get_suffix(char *path);int main(int argc, char *argv[]) { int i,j; static struct arg *asmset,*objset; fs = fc = fo = 0; i = doarg(argc, argv); asmset = newarray(sizeof (struct arg ), argc - i + 1, PERM); objset = newarray(sizeof (struct arg ), argc - i + 1, PERM); for (j = 0; j < argc - i + 1; j++) { asmset[j].name = objset[j].name = NULL; asmset[j].generated = objset[j].generated = 0; } (*IR->progbeg)(); for (j = 0;i < argc; i++, j++) { char *ps; infile = argv[i]; ps = get_suffix(argv[i]); if (!strcmp(ps, ".asm") || !strcmp(ps, ".s")) { asmset[j].name = argv[i]; continue; } else if (!strcmp(ps, ".o")) { objset[j].name = argv[i]; continue; } if (fo && fs) asmset[j].name = outfile; else if (fs || (fc && !fo)) { asmset[j].name = genname(infile, ".asm"); asmset[j].generated = 1; } else { asmset[j].name = tempnam(".",NULL); asmset[j].generated = 1; } if (infile != NULL) if ((infd = open(infile, 0)) < 0) { fprint(2, "%s: can't read `%s'\n", argv[0], infile); exit(1); } if (asmset[j].name != NULL) if ((outfd = creat(asmset[j].name, 0666)) < 0) { fprint(2, "%s: can't write `%s'\n", argv[0], asmset[j].name); exit(1); } inputInit(); outputInit(); init_sym(); t = gettoken(); program(); finalize(); outflush(); close(infd); close(outfd); } if (!errcnt) { int s; if (fs) goto finish; if (fc && fo) { s = system(stringf("nasm -f elf %s -o %s", asmset[0].name,outfile)); if (asmset[0].generated) unlink(asmset[0].name); if (s) exit(1); goto finish; } {char *objstr; for (i = 0; asmset[i].name || objset[i].name; i++) { if (asmset[i].name) { objset[i].name = genname(asmset[i].name, ".o"); objset[i].generated = 1; s = system(stringf("nasm -f elf %s -o %s", asmset[i].name, objset[i].name)); if (asmset[i].generated) unlink(asmset[i].name); if (s) exit(1); } } if (fc) goto finish; if (!fo) { outfile = "a.out"; if (i >= 2) i = 1; } objstr = allocate(_POSIX_ARG_MAX, PERM); *objstr = '\0'; for (j = 0; j < i; j++) { if (strlen(objstr) >= _POSIX_ARG_MAX) break; strcat(objstr, stringf("%s ",objset[j].name)); } s = system(stringf("ld -o %s -dynamic-linker \ /lib/ld-linux.so.2 \ /usr/lib/crt1.o \ /usr/lib/crti.o \ /usr/lib/crtn.o %s \ -L/usr/lib -lc -lm", outfile, objstr)); for (j = 0; objset[j].name; j++) if (objset[j].generated) unlink(objset[j].name); if (s) exit(1); } }finish: close(errfd); deallocate(PERM); dispose(); return errcnt > 0;}#define OPTSTR "Sco:"static int doarg(int argc, char *argv[]){ int c; opterr = 0; while ((c = getopt(argc, argv, OPTSTR)) != EOF) { switch (c) { case 'S': fs = 1; break; case 'c': fc = 1; break; case 'o': outfile = optarg; fo = 1; break; case '?': error("unrecognized option: -%c", optopt); fprint(2, "usage: scc [-S] [-c] [-o outfile] inputfile"); exit(1); } } if (optind >= argc) { fprint(2, "usage: scc [-S] [-c] [-o outfile] inputfile\n"); exit(1); } if ((fs || fc) && fo && optind < argc - 1) { fprint(2, "当有多个文件时不能在已指定 -c 或 -S 的情况下指定 -o\n"); exit(1); } if (fs) fc = 0; return optind;}/*genname:将后缀suffix替换原来的后缀生成输出文件名*/static char *genname(char *path,char *suffix){ int i,j,k; char *p; for (i=j=k=0; path[i]; i++) if ('/' == path[i]) j = i; else if ('.' == path[i]) k = i; if (j) j++; /*path为当前目录下的文件名*/ if (!k) k = i; /*path中没有后缀*/ for (i = 0; suffix[i]; i++) ; p = allocate(i + k - j + 1, PERM); for (i = 0;j < k; j++,i++) p[i] = path[j]; while ((p[i++] = *suffix++) != '\0') ; return p;}/*get_suffix:返回文件名中的后缀*/static char *get_suffix(char *path){ int i,j; char *p; for (i=j=0; path[i]; i++) if ('.' == path[i]) j = i; if (!j) j = i; p = allocate(i - j + 1, PERM); i = 0; while ((p[i++] = path[j++]) != '\0') ; return p;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -