📄 main.c
字号:
/* main.c -- Main function for the C Documentation Generator *//* * The following text is embedded in the code and intended to be found and * extracted with the strings(1) program (or some similar utility). * Naturally you can also read it directly in this source. */char * copyright[] ={ "Cdg - a C-source Documentation Generator. \n", "Copyright (C) 1995, 1996 Peter Knoppers <Peter.Knoppers@ct.tudelft.nl>. \n", "\n", "Prins Mauritsstraat 17, 2628 SR Delft, the Netherlands. \n", "\n", "This program is free software; you can redistribute it and/or modify \n", "it under the terms of the GNU General Public License as published by \n", "the Free Software Foundation; either version 2 of the License, or \n", "(at your option) any later version. \n", "\n", "This program is distributed in the hope that it will be useful, \n", "but WITHOUT ANY WARRANTY; without even the implied warranty of \n", "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the \n", "GNU General Public License for more details. \n", "\n", "You should have received a copy of the GNU General Public License \n", "along with this program; if not, write to the Free Software \n", "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. \n",};#include <stdlib.h>#include <stdio.h>#include <unistd.h>#include <string.h>#include <ctype.h>#include <time.h>#include <sys/stat.h>#include <errno.h>#include "cdg.h"FILE * listfile = (FILE *) 0;FILE * tagfile = (FILE *) 0;FILE * sortfile = (FILE *) 0;char * listfname = (char *) 0;char * tagfname = (char *) 0;char * sortfname = (char *) 0;static int dontunlinktagfile = 0;void cleanup (void){ if (listfname != (char *) 0) unlink (listfname); /* ignore any errors */ if ((dontunlinktagfile == 0) && (tagfname != (char *) 0)) unlink (tagfname); if (sortfname != (char *) 0) unlink (sortfname);}#define CROSSREFNAMEWIDTH 15 /* width of subcolumn for names *//* Changing the value of CROSSREFNUMWIDTH is asking for BIG trouble... */#define CROSSREFNUMWIDTH 7 /* width of subcolomn for numbers */#define EXCLUDE 'X' /* exclude this name from crossref */#define HIGHLIGHTCODES "TFDMN" /* highlight these types in xref *//* * main: Parse the program arguments, interpret the filefile, * coordinate things, etc. * arguments: * int argc: Program argument count. * char *argv[]: Argument list. * returns: Int, nonzero on error, 0 if no errors. */int main(int argc, char *argv[]){ FILE * filefile; /* project description file */ char buf[MAXBUF + 1]; int footerline; char currentidentifier[MAXBUF + 1]; char crossrefbuf[MAXBUF + 1]; char lastnumbuf[MAXBUF + 1]; char includecodes[MAXBUF + 1] = "TtFCDRMmNE"; char * spacebeforenumber = (char *) 0;/* kills compiler warning */ int perfilelineno; char currentfilename[MAXBUF + 1]; int excludeflag; fprintf (stderr, "C Documentation Generator, Version %d.%d, %s output, %s.\n", MAJORVERSION, MINORVERSION, printertype, makedatestring); if ((argc < 2) || (argc > 3)) { fprintf (stderr, "Usage: cdg listfile [tagfile]\n"); exit(1); } if ((filefile = fopen (argv[1], "r")) == (FILE *) 0) { fprintf (stderr, "Cannot open file %s: %s\n", argv[1], sys_errlist[errno]); exit (1); } if ((listfname = tempnam ("/tmp", "cdgL")) == 0) { fprintf (stderr, "tempnam failed: %s\n", sys_errlist[errno]); exit(1); } if ((listfile = fopen (listfname, "w")) == (FILE *) 0) { fprintf (stderr, "Cannot create scratch file %s: %s\n", listfname, sys_errlist[errno]); exit (1); } atexit (cleanup); /* remove temp files on exit */ if (argc >= 3) { tagfname = argv[2]; dontunlinktagfile = 1; } else if ((tagfname = tempnam ("/tmp", "cdgT")) == 0) { fprintf (stderr, "tempnam failed: %s\n", sys_errlist[errno]); exit(1); } if ((tagfile = fopen (tagfname, "w")) == (FILE *) 0) { fprintf (stderr, "Cannot create file %s: %s\n", tagfname, sys_errlist[errno]); exit (1); } if ((sortfname = tempnam ("/tmp", "cdgS")) == 0) { fprintf (stderr, "tempnam failed: %s\n", sys_errlist[errno]); exit(1); } /* * Read the lines with the footer text for each page. */ for (footerline = MAXPAGELINE - MAXCOPYRIGHT; footerline < MAXPAGELINE; footerline++) { fgets (buf, MAXBUF, filefile); trim (buf); if (footerline == MAXPAGELINE - 1) { /* add current date and time */ char * timebuf; time_t currenttime; time(¤ttime); timebuf = ctime (¤ttime); trim (timebuf); sprintf (buf + strlen (buf), "%*s", MAXPAGECOL - (int) strlen(buf), timebuf); } addlinehard (footerline, buf); } sprintf (headerline, "Table of contents"); cols = 3; /* three-column index */ /* * Do the other lines in the filefile. */ while (fgets (buf, MAXBUF, filefile)) { int startoutputline; int previnsertednewlines; struct stat filestatus; int isCfile; trim (buf); if (buf[0] == '-') { char * cp; for (cp = &buf[1]; isspace(*cp); cp++) ; /* * Make exclude tag. * Linenumber 0 will be sorted before all others with the same name */ maketag (0, cp, EXCLUDE); continue; } else if (buf[0] == '+') { strcpy (includecodes, buf); continue; } if (stat (buf, &filestatus) != 0) { fprintf (stderr, "Cannot stat file %s: %s\n", buf, sys_errlist[errno]); exit(1); } if ((strlen (buf) < 2) || ((strcmp (&buf[strlen(buf) - 2], ".c") != 0) && (strcmp (&buf[strlen(buf) - 2], ".h") != 0))) isCfile = 0; else isCfile = 1; /* * Make sure that new file begins at top of page. */ while (outputlineno % LINESPERCOLUMN != 0) { fputc ('\n', listfile); outputlineno++; totalinsertednewlines++; } startoutputline = outputlineno; fprintf (stderr, "Scanning file %s...\n", buf); fprintf (listfile, "-%s %s", buf, ctime (&(filestatus.st_mtime))); if (nextinputfile (buf) != 0) continue; /* error was output on stderr */ previnsertednewlines = totalinsertednewlines; if (isCfile == 0) { /* do a non-C-file */ char copybuf[MAXBUF + 1]; while (fgets (copybuf, MAXBUF, yyin)) { char * cp; yylineno++; for (cp = copybuf; *cp != '\0'; cp++) copyout (*cp); } } else parsefile (); /* do a C-file */ update_insertednewlines (); if (totalinsertednewlines - previnsertednewlines != 0) fprintf (stderr, "%d line%s longer than %d chars.\n", totalinsertednewlines - previnsertednewlines, (totalinsertednewlines - previnsertednewlines != 1) ? "s were broken because these were" : " was broken because it was", MAXCOLUMN); if (column != 0) { fprintf (stderr, "File does not end with \\n.\n"); fputc ('\n', listfile); outputlineno++; } sprintf(&buf[strlen (buf)], "%*s%6d-%6d %4d", COLWIDTH - 18 - (int) strlen(buf), "", startoutputline + 1, outputlineno, startoutputline / 100 + 1); addline (buf); } fclose (filefile); fclose (tagfile); fclose (listfile); strcpy (buf, "Cross reference table"); sprintf(&buf[strlen (buf)], "%*s%4d", COLWIDTH - 4 - (int) strlen(buf), "", (outputlineno - 1) / 100 + 2); addline (buf); /* * Output the source listings. */ fprintf (stderr, "Copying source files to output...\n"); flushpage(2); /* in two-column mode */ if (COLWIDTH - 9 < MAXCOLUMN) { fprintf (stderr, "Paper too small... Recompile with bigger value %s", "for MAXPAGECOL or smaller value for MINCOLSEP.\n"); exit (1); } pageno = 1; if ((listfile = fopen (listfname, "r")) == (FILE *) 0) { fprintf (stderr, "Cannot re-read my own scratch file %s: %s\n", listfname, sys_errlist[errno]); exit (1); } perfilelineno = 0; /* make sure */ while (fgets (buf, MAXBUF, listfile)) { trim (buf); if (buf[0] == '-') { perfilelineno = 0; strcpy (currentfilename, &buf[1]); } else { if ((++perfilelineno % LINESPERCOLUMN) == 1) sprintf (headerline, " File%c%s lines %d-%d", HIGHLIGHTWORD, currentfilename, perfilelineno, perfilelineno + LINESPERCOLUMN - 1); if ((buf[0] == '\0') && (currentfilename[0] != '\0')) { sprintf (headerline, "File%c%s lines %d-%d", HIGHLIGHTWORD, currentfilename, perfilelineno / LINESPERCOLUMN * LINESPERCOLUMN + 1, perfilelineno - 1); currentfilename[0] = '\0'; } addline (buf); } } sprintf (headerline, "File%c%s lines %d-%d", HIGHLIGHTWORD, currentfilename, perfilelineno / LINESPERCOLUMN * LINESPERCOLUMN + 1, perfilelineno); /* * Make Cross reference table. */ flushpage(3); /* in three-column mode */ sprintf (headerline, "Cross reference table"); sprintf (buf, "sort -t'\t' +2.0 < %s > %s", tagfname, sortfname); fprintf (stderr, "Sorting cross reference data...\n"); if (system (buf) != 0) { fprintf (stderr, "Error sorting tagfile\n"); exit(1); } if ((sortfile = fopen (sortfname, "r")) == (FILE *) 0) { fprintf (stderr, "Cannot re-read my own scratch file %s: %s\n", sortfname, sys_errlist[errno]); exit (1); } fprintf (stderr, "Writing cross reference table...\n"); currentidentifier[0] = '\0'; crossrefbuf[0] = '\0'; lastnumbuf[0] = '\0'; excludeflag = 0; while (fgets (buf, MAXBUF, sortfile)) { trim (buf); if ((excludeflag != 0) && (strcmp (&buf[9], currentidentifier) == 0)) continue; excludeflag = 0; if (buf[7] == EXCLUDE) { strcpy (currentidentifier, &buf[9]); excludeflag = 1; continue; } if (strchr (includecodes, buf[7]) == (char *) 0) continue; if (strcmp (currentidentifier, &buf[9]) != 0) { /* 1st occurrance of this identifier */ lastnumbuf[0] = '\0'; strcpy (currentidentifier, &buf[9]); if (crossrefbuf[0] != '\0') addline (crossrefbuf); sprintf (crossrefbuf, "%s ", currentidentifier); /* The trailing space is intentional! */ } if (strncmp (lastnumbuf, buf, 6) == 0) { /* same linenumber */ if (strchr (HIGHLIGHTCODES, buf[7])) *spacebeforenumber = HIGHLIGHTWORD; continue; } if (strlen(crossrefbuf) > COLWIDTH - 6) { /* line full, continue on new line */ addline (crossrefbuf); crossrefbuf[0] = '\0'; } spacebeforenumber = crossrefbuf + strlen(crossrefbuf); while ((spacebeforenumber - crossrefbuf < CROSSREFNAMEWIDTH) || (((COLWIDTH + CROSSREFNUMWIDTH - 6 - (spacebeforenumber - crossrefbuf)) % CROSSREFNUMWIDTH) != 0)) { *spacebeforenumber++ = ' '; *spacebeforenumber = '\0'; } spacebeforenumber--; strncat (crossrefbuf, buf, 6); strncpy (lastnumbuf, buf, 6); while (spacebeforenumber[1] == ' ') spacebeforenumber++; /* creep on up to the first digit */ if (strchr (HIGHLIGHTCODES, buf[7])) *spacebeforenumber = HIGHLIGHTWORD; } if (crossrefbuf[0] != '\0') addline (crossrefbuf); flushpage(0); return (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -