figlet.c
来自「xen虚拟机源代码安装包」· C语言 代码 · 共 2,077 行 · 第 1/4 页
C
2,077 行
Reads a control file "T" command character specification. Character is a single byte, an escape sequence, or an escaped numeric.****************************************************************************/inchr readTchar(fp)ZFILE *fp;{ inchr thechar; char next; thechar=Zgetc(fp); if (thechar=='\n' || thechar=='\r') { /* Handle badly-formatted file */ Zungetc(thechar,fp); return '\0'; } if (thechar!='\\') return thechar; next=Zgetc(fp); switch(next) { case 'a': return 7; case 'b': return 8; case 'e': return 27; case 'f': return 12; case 'n': return 10; case 'r': return 13; case 't': return 9; case 'v': return 11; default: if (next=='-' || next=='x' || (next>='0' && next<='9')) { Zungetc(next,fp); readnum(fp,&thechar); return thechar; } return next; }}/**************************************************************************** charsetname Get a Tchar representing a charset name, or 0 if none available. Called in getcharset().****************************************************************************/inchr charsetname(fp)ZFILE *fp;{ inchr result; result = readTchar(fp); if (result == '\n' || result == '\r') { result = 0; Zungetc(result,fp); } return result; }/**************************************************************************** charset Processes "g[0123]" character set specifier Called in readcontrol().****************************************************************************/void charset(n, controlfile)int n;ZFILE *controlfile;{ int ch; skipws(controlfile); if (Zgetc(controlfile) != '9') { skiptoeol(controlfile); return; } ch = Zgetc(controlfile); if (ch == '6') { gn[n] = 65536L * charsetname(controlfile) + 0x80; gndbl[n] = 0; skiptoeol(controlfile); return; } if (ch != '4') { skiptoeol(controlfile); return; } ch = Zgetc(controlfile); if (ch == 'x') { if (Zgetc(controlfile) != '9') { skiptoeol(controlfile); return; } if (Zgetc(controlfile) != '4') { skiptoeol(controlfile); return; } skipws(controlfile); gn[n] = 65536L * charsetname(controlfile); gndbl[n] = 1; skiptoeol(controlfile); return; } Zungetc(ch, controlfile); skipws(controlfile); gn[n] = 65536L * charsetname(controlfile); gndbl[n] = 0; return; }/**************************************************************************** readcontrol Allocates memory and reads in the given control file. Called in readcontrolfiles().****************************************************************************/void readcontrol(controlname)char *controlname;{ inchr firstch,lastch; char dashcheck; inchr offset; char *controlpath,magicnum[5]; int command; ZFILE *controlfile; int namelen; namelen = MYSTRLEN(fontdirname); controlpath = (char*)myalloc(sizeof(char) *(namelen+MYSTRLEN(controlname)+CSUFFIXLEN+2)); controlfile = NULL; if (!hasdirsep(controlname)) { strcpy(controlpath,fontdirname); controlpath[namelen] = DIRSEP; controlpath[namelen+1] = '\0'; strcat(controlpath,controlname); strcat(controlpath,CONTROLFILESUFFIX); controlfile = Zopen(controlpath,"rb"); } if (controlfile==NULL) { strcpy(controlpath,controlname); strcat(controlpath,CONTROLFILESUFFIX); controlfile = Zopen(controlpath,"rb"); if (controlfile==NULL) { fprintf(stderr,"%s: %s: Unable to open control file\n",myname, controlpath); exit(1); } } free(controlpath); (*commandlistend) = (comnode*)myalloc(sizeof(comnode)); (*commandlistend)->thecommand = 0; /* Begin with a freeze command */ commandlistend = &(*commandlistend)->next; (*commandlistend) = NULL; while(command=Zgetc(controlfile),command!=EOF) { switch (command) { case 't': /* Translate */ skipws(controlfile); firstch=readTchar(controlfile); if ((dashcheck=Zgetc(controlfile))=='-') { lastch=readTchar(controlfile); } else { Zungetc(dashcheck,controlfile); lastch=firstch; } skipws(controlfile); offset=readTchar(controlfile)-firstch; skiptoeol(controlfile); (*commandlistend) = (comnode*)myalloc(sizeof(comnode)); (*commandlistend)->thecommand = 1; (*commandlistend)->rangelo = firstch; (*commandlistend)->rangehi = lastch; (*commandlistend)->offset = offset; commandlistend = &(*commandlistend)->next; (*commandlistend) = NULL; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '-': /* Mapping table entry */ Zungetc(command,controlfile); readnum(controlfile,&firstch); skipws(controlfile); readnum(controlfile,&lastch); offset=lastch-firstch; lastch=firstch; skiptoeol(controlfile); (*commandlistend) = (comnode*)myalloc(sizeof(comnode)); (*commandlistend)->thecommand = 1; (*commandlistend)->rangelo = firstch; (*commandlistend)->rangehi = lastch; (*commandlistend)->offset = offset; commandlistend = &(*commandlistend)->next; (*commandlistend) = NULL; break; case 'f': /* freeze */ skiptoeol(controlfile); (*commandlistend) = (comnode*)myalloc(sizeof(comnode)); (*commandlistend)->thecommand = 0; commandlistend = &(*commandlistend)->next; (*commandlistend) = NULL; break; case 'b': /* DBCS input mode */ multibyte = 1; break; case 'u': /* UTF-8 input mode */ multibyte = 2; break; case 'h': /* HZ input mode */ multibyte = 3; break; case 'j': /* Shift-JIS input mode */ multibyte = 4; break; case 'g': /* ISO 2022 character set choices */ multibyte = 0; skipws(controlfile); command=Zgetc(controlfile); switch (command) { case '0': /* define G0 charset */ charset(0, controlfile); break; case '1': /* set G1 charset */ charset(1, controlfile); break; case '2': /* set G2 charset */ charset(2, controlfile); break; case '3': /* set G3 charset */ charset(3, controlfile); break; case 'l': case 'L': /* define left half */ skipws(controlfile); gl = Zgetc(controlfile) - '0'; skiptoeol(controlfile); break; case 'r': case 'R': /* define right half */ skipws(controlfile); gr = Zgetc(controlfile) - '0'; skiptoeol(controlfile); break; default: /* meaningless "g" command */ skiptoeol(controlfile); } case '\r': case '\n': /* blank line */ break; default: /* Includes '#' */ skiptoeol(controlfile); } } Zclose(controlfile);}/**************************************************************************** readcontrolfiles Reads in the controlfiles names in cfilelist. Uses readcontrol. Called in main().****************************************************************************/void readcontrolfiles(){ cfnamenode *cfnptr; for (cfnptr=cfilelist;cfnptr!=NULL;cfnptr=cfnptr->next) { readcontrol(cfnptr->thename); }}/**************************************************************************** clearcfilelist Clears the control file list. Assumes thename does not need freeing.****************************************************************************/void clearcfilelist(){ cfnamenode *cfnptr1,*cfnptr2; cfnptr1 = cfilelist; while (cfnptr1 != NULL) { cfnptr2 = cfnptr1->next; free(cfnptr1); cfnptr1 = cfnptr2; } cfilelist = NULL; cfilelistend = &cfilelist;}/**************************************************************************** getparams Handles all command-line parameters. Puts all parameters within bounds.****************************************************************************/void getparams(){ extern char *optarg; extern int optind; int c; /* "Should" be a char -- need int for "!= -1" test*/ int columns,firstfont,infoprint; char *controlname; if ((myname = strrchr(Myargv[0],DIRSEP))!=NULL) { myname++; } else { myname = Myargv[0]; } fontdirname = DEFAULTFONTDIR; firstfont = 1; fontname = (char*)myalloc(sizeof(char)*(MYSTRLEN(DEFAULTFONTFILE)+1)); strcpy(fontname,DEFAULTFONTFILE); /* Some systems don't have strdup() */ if (suffixcmp(fontname,FONTFILESUFFIX)) { fontname[MYSTRLEN(fontname)-FSUFFIXLEN]='\0'; } cfilelist = NULL; cfilelistend = &cfilelist; commandlist = NULL; commandlistend = &commandlist; smushoverride = SMO_NO; deutschflag = 0; justification = -1; right2left = -1; paragraphflag = 0; infoprint = -1; cmdinput = 0; outputwidth = DEFAULTCOLUMNS; gn[1] = 0x80; gr = 1; while ((c = getopt(Myargc,Myargv,"ADEXLRI:xlcrpntvm:w:d:f:C:NFskSWo"))!= -1) { /* Note: -F is not a legal option -- prints a special err message. */ switch (c) { case 'A': cmdinput = 1; break; case 'D': deutschflag = 1; break; case 'E': deutschflag = 0; break; case 'X': right2left = -1; break; case 'L': right2left = 0; break; case 'R': right2left = 1; break; case 'x': justification = -1; break; case 'l': justification = 0; break; case 'c': justification = 1; break; case 'r': justification = 2; break; case 'p': paragraphflag = 1; break; case 'n': paragraphflag = 0; break; case 's': smushoverride = SMO_NO; break; case 'k': smushmode = SM_KERN; smushoverride = SMO_YES; break; case 'S': smushmode = SM_SMUSH; smushoverride = SMO_FORCE; break; case 'o': smushmode = SM_SMUSH; smushoverride = SMO_YES; break; case 'W': smushmode = 0; smushoverride = SMO_YES; break; case 't':#ifdef TIOCGWINSZ columns = get_columns(); if (columns>0) { outputwidth = columns; }#else /* ifdef TIOCGWINSZ */ fprintf(stderr, "%s: \"-t\" is disabled, since ioctl is not fully implemented.\n", myname);#endif /* ifdef TIOCGWINSZ */ break; case 'v': infoprint = 0; break; case 'I': infoprint = atoi(optarg); break; case 'm': smushmode = atoi(optarg); if (smushmode < -1) { smushoverride = SMO_NO; break; } if (smushmode == 0) smushmode = SM_KERN; else if (smushmode == -1) smushmode = 0; else smushmode = (smushmode & 63) | SM_SMUSH; smushoverride = SMO_YES; break; case 'w': columns = atoi(optarg); if (columns>0) { outputwidth = columns; } break; case 'd': fontdirname = optarg; break; case 'f': if (firstfont) { free(fontname); firstfont = 0; } fontname = optarg; if (suffixcmp(fontname,FONTFILESUFFIX)) { fontname[MYSTRLEN(fontname)-FSUFFIXLEN] = '\0'; } break; case 'C': controlname = optarg; if (suffixcmp(controlname, CONTROLFILESUFFIX)) { controlname[MYSTRLEN(controlname)-CSUFFIXLEN] = '\0'; } (*cfilelistend) = (cfnamenode*)myalloc(sizeof(cfnamenode)); (*cfilelistend)->thename = controlname; cfilelistend = &(*cfilelistend)->next; (*cfilelistend) = NULL; break; case 'N': clearcfilelist(); multibyte = 0; gn[0] = 0; gn[1] = 0x80; gn[2] = gn[3] = 0; gndbl[0] = gndbl[1] = gndbl[2] = gndbl[3] = 0; gl = 0; gr = 1; break; case 'F': /* Not a legal option */ fprintf(stderr,"%s: illegal option -- F\n",myname); printusage(stderr); fprintf(stderr,"\nBecause of numerous incompatibilities, the"); fprintf(stderr," \"-F\" option has been\n"); fprintf(stderr,"removed. It has been replaced by the \"figlist\""); fprintf(stderr," program, which is now\n"); fprintf(stderr,"included in the basic FIGlet package. \"figlist\""); fprintf(stderr," is also available\n"); fprintf(stderr,"from http://www.figlet.org/"); fprintf(stderr,"under UNIX utilities.\n"); exit(1); break; default: printusage(stderr); exit(1); } } if (optind!=Myargc) cmdinput = 1; /* force cmdinput if more arguments */ outlinelenlimit = outputwidth-1; if (infoprint>=0) { printinfo(infoprint); exit(0); }}/**************************************************************************** clearline Clears both the input (inchrline) and output (outputline) storage.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?