-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocxtr.c
375 lines (312 loc) · 12.2 KB
/
docxtr.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* CHK=0xE69D */
/*+--------------------------------------------------------------------------
DOCXTR.c : Documentation Extraction program.
Copyright (C) 1985, Warren H. Tucker d/b/a TuckerWare
All Rights Reserved
The program extracts the comments (and possibly other lines) from one or
more source files. There are several options which select the voluminosity (?)
of the output. As examples, various documentation headers in this
program use different options to help produce the documentation.
---------------------------------------------------------------------------*/
/*+:EDITS:*/
/*:04-26-2000-11:15-wht@bob-RELEASE 4.42 */
/*:01-24-1997-02:36-wht@yuriatin-SOURCE RELEASE 4.00 */
/*:01-16-1987-17:40-WHT-do not print EDITS trigger */
/*:03-22-1986-23:20-WHT-Version 1.1/Xenix */
/*:08-04-1985-00:30-WHT-Version 1.0 */
/*:08-03-1985-11:00-WHT-Creation */
#include <stdio.h>
#define TRUE 1
#define FALSE 0
extern char *fgets();
/*+ --- DOCXTR compile-time configuration --- */
#define S_IN_BUF_MAX (257) /* max size of file input line */
#define N_SFILES_MAX (32) /* max number of files to be searched */
#define S_FNAME_MAX (41) /* max size of a file name */
/*} this extract option shows defines even though they are not bounded by slash*+ */
/*+-------------------------------------------------------------------------
to_upper() / to_lower()
one would think that these were relatively standard
types of thing, but MSC/Xenix specifies toupper() to convert to upper
case if not already and Unix says to adjust without testing,
so, two stupid little routines here
ASCII only -- no EBCDIC gradoo here please
--------------------------------------------------------------------------*/
unsigned char to_upper(ch)
register unsigned char ch;
{
return( ((ch >= 'a') && (ch <= 'z')) ? ch - 0x20 : ch);
} /* end of to_upper() */
unsigned char to_lower(ch)
register unsigned char ch;
{
return( ((ch >= 'A') && (ch <= 'Z')) ? ch + 0x20 : ch);
} /* end of to_lower() */
/*+----------------------------------------------------------------------------
ulcmpb: Upper/Lower [case insensitive] Compare Bytes
Returns -1 if strings are equal, else failing character position
If the second strings terminates with a null and both strings have matched
character for character until that point, then -1 is returned.
NOTE: this is not a test for complete equality of two strings, but allows
discovery of a string as a substring in a larger containing string.
-----------------------------------------------------------------------------*/
int ulcmpb(str1,str2)
register unsigned char *str1;
register unsigned char *str2;
{
register int istr;
for( istr=0 ; ; ++istr )
{
if(str2[istr] == '\0') /* if second string exhausts, match! */
return(-1);
if((str1[istr] == '\0' ) ||
( to_upper(str1[istr]) != to_upper(str2[istr]) ))
return(istr);
}
/*NOTREACHED*/
} /* end of ulcmpb */
/*+-------------------------------------------------------------------------
ulindex: Upper/Lower [case insensitive] Index functioni
Returns position of 'str2' in 'str1' if found
If 'str2' is null, then 0 is returned (null matches anything)
Returns -1 if not found
uses 'ulcmpb'
--------------------------------------------------------------------------*/
int ulindex(str1,str2)
register unsigned char *str1; /* the (target) string to search */
register unsigned char *str2; /* the (comparand) string to search for */
{
register int istr1 = 0; /* moving index into str1 */
register unsigned char *mstr = str1; /* moving string pointer */
if(str2[0] == '\0') /* null string matches anything */
return(0);
while(1)
{
if(*mstr == '\0') /* if we exhaust target string, flunk */
return(-1);
/* Can we find either case of first comparand char in target? */
if( to_upper(*mstr) == to_upper(str2[0]) )
{
/* we have a first char match... does rest of string match? */
if(ulcmpb(mstr,str2) == -1) /* if the rest matches, ... */
return(istr1); /* ... return match position */
}
/* we did not match this time... increment istr1, mstr and try again */
++istr1;
++mstr;
}
} /* end of ulindex */
/*+-------------------------------------------------------------------------
main program
--------------------------------------------------------------------------*/
main(argc,argv)
int argc;
char **argv;
{
int erc; /* error code */
int iFiles; /* index into argv array (filenames) */
int nFiles; /* number of input files */
int fHaveIndFile; /* TRUE if input is from "@" file */
int fIndFileEOF; /* TRUE if "@" input file EOF */
int in_eof; /* TRUE if input file eof reached */
int itmp; /* integer/index temp */
int iargv; /* index into argv */
int fOutputFile = FALSE; /* if set TRUE, matching lines writen to 'out_fname'*/
char *eof_if_zero; /* end of file control flag */
int fListAll; /* TRUE if '-a': list all comments */
int fOutputLines; /* if TRUE, output source lines */
int fCloseBraceStop; /* if TRUE, end marker is '}' not star-slash */
long out_line_count; /* number of lines output by program */
char Files[N_SFILES_MAX][S_FNAME_MAX]; /* filenames */
char in_buf[S_IN_BUF_MAX]; /* line input from @file or file being searched */
char out_fname[S_FNAME_MAX]; /* "=o"utput file name */
FILE *in_fp; /* file for reading of search file(s) */
FILE *out_fp; /* file for output if -o specified */
FILE *cmd_fp; /* file for search string input if -i and ... */
/* ... @file read if found */
puts("... docxtr x1.1/Xenix by TuckerWare ...................");
strcpy(out_fname,"/dev/tty"); /* default output file is console */
out_line_count=0L; /* output line counter to zero */
fListAll=FALSE; /* no -a switch yet */
nFiles=0;
for( iargv=1; iargv < argc ; ++iargv ) /* walk iargv from 1 to 'argc' */
{
if( (itmp=argv[iargv][0]) != '-') /* if not switch argument */
{
if(itmp == '@') /* if @file */
{
cmd_fp=fopen(&argv[iargv][1],"r"); /* read the '@' file contents */
if(cmd_fp == 0)
{
printf("Error opening @file %s.\n",&argv[iargv][1]);
exit(1);
}
eof_if_zero= in_buf; /* init error variable to non-error (non-zero) */
while(eof_if_zero != NULL) /* while not @file input eof or error */
{
in_buf[0]='\0';
eof_if_zero=fgets(in_buf,sizeof(in_buf)-1,cmd_fp); /* read filename */
if(strlen(in_buf) < 2) /* just a newline is empty */
eof_if_zero = NULL; /* and we simulate eof in such cases */
if(eof_if_zero == NULL) /* if eof or error, ... */
{
continue; /* ... continue... while will end loop */
}
if(nFiles == N_SFILES_MAX) /* we have an element... can we use it? */
{
printf("...Maximum files (%d) exceeded.\n",N_SFILES_MAX);
exit(1);
}
itmp=strlen(in_buf); /* strip trailing newline ... */
in_buf[--itmp]='\0'; /* ... from input record */
strncpy(Files[nFiles++],in_buf,S_FNAME_MAX-1); /* copy filename from "stolen" buffer */
printf("\nFile to be searched: %s",in_buf);
}/* end of while not @file eof */
puts("");
fclose(cmd_fp);
continue;
}/* end of if @file */
if(nFiles == N_SFILES_MAX)
{
printf("...Maximum files (%d) exceeded.\n",N_SFILES_MAX);
exit(1);
}
strncpy(Files[nFiles++],argv[iargv],S_FNAME_MAX-1);
continue;
} /* end of if not switch arg */
else /* switch argument */
{
if(to_upper(argv[iargv][1])=='O') /* output switch directive */
{
strncpy(out_fname,&argv[iargv][2],sizeof(out_fname)-1);
fOutputFile=TRUE;
continue;
}
if(to_upper(argv[iargv][1])=='A') /* if found, we are to output all comments */
{
fListAll=TRUE;
continue;
}
if(to_upper(argv[iargv][1])=='?') /* input switch directive */
{
puts("Copyright (C) 1985 Warren H. Tucker d/b/a TuckerWare");
continue;
}
} /* end of else switch arg */
} /* end of walk iargv */
/* convert filenames to all upper case (not nice in some systems, but ok for intended MS-DOS) */
#ifdef MSDOS
for(iFiles=0; iFiles < nFiles; ++iFiles)
{
for(itmp=0 ; Files[nFiles-1][itmp] != '\0' ; ++itmp)
{
Files[iFiles][itmp]=to_upper(Files[iFiles][itmp]);
}
}
#endif
if(nFiles == 0) /* if there are no input files, give help */
{
puts("DOCXTR scans a list of search files, extracting documentation enclosed in");
puts("specially marked comment groups (beginning with '/*+' and ending with '*/'.");
puts("A line containing a three-char '/*+' substring will be output, as well as");
puts("all subsequent lines until the substring '*/' is found (or end of file).");
puts("Similarly, lines containing '/*{' cause lines to be printed until a '}'.");
puts("\nExample usage: DOCXTR MODA.H MODB.C MODC.C\n");
puts("Switches allow you to modify program operation:");
puts("-a ALL lines containing comments are extracted.");
puts("-ofname causes a copy of the output to be routed to 'fname'");
puts("@fname causes a list of search files to be read from 'fname'");
puts("A blank line read by DOCXTR is equivalent to end of file.");
puts("\nFor example: DOCXTR @FNAMES.LST ANOTHER.C -okeep.file:\n");
exit(0);
}
/* if outputing to file, open it */
if(fOutputFile )
{
out_fp=fopen(out_fname,"w");
if(out_fp == 0)
{
printf("Error opening output file %s.\n",out_fname);
exit(1);
}
}
/* walk thru the files one by one */
for(iFiles=0 ; iFiles < nFiles ; ++iFiles)
{
in_eof=FALSE; /* reset eof indicator */
in_fp=fopen(Files[iFiles],"r"); /* open the target file */
if(in_fp == 0) /* if fopen returns 0 file number, error occurred */
{
printf("Error opening file %d: %s\n",iFiles,Files[iFiles]);
goto Next_Input_File; /* jumping down the page */
}
printf(".... Extracting from %s ......................\n",Files[iFiles]);
if( fOutputFile )
{
fprintf(out_fp,".... Extracting from %s ......................\n",Files[iFiles]);
}
fOutputLines = FALSE; /* do not output lines yet */
fCloseBraceStop=FALSE; /* set to star-slash end trigger rather than '}' */
/************ per file processing goes below here **************/
while(!in_eof)
{
if( (fgets(in_buf,sizeof(in_buf)-1,in_fp)) == 0)
{
in_eof=TRUE;
continue;
}
/* if starting trigger on a line, set up to be printed */
if( ulindex(in_buf,"/*{") == 0 )
{
fOutputLines=TRUE; /* start outputing lines */
fCloseBraceStop=TRUE; /* and dont stop until '}' */
}
if((ulindex(in_buf,"/*+") == 0) &&
(ulindex(in_buf,":EDITS:") == -1))
fOutputLines = TRUE;
if( (fListAll )
&& (ulindex(in_buf,"/*") != -1 ) )
fOutputLines = TRUE;
if( fOutputLines && (strncmp(in_buf,"/*+----------",10) != 0) )
{
fputs(in_buf,stdout);
if(fOutputFile )
{
fputs(in_buf,out_fp);
}
++out_line_count;
}
/* if ending trigger found, even on line just printed, turn off printing */
if( (fCloseBraceStop == FALSE) /* unless looking only for close brace */
&& (ulindex(in_buf,"*/") != -1) )
{
fOutputLines=FALSE;
}
if( (fCloseBraceStop )
&& (ulindex(in_buf,"}") != -1) )
{
fOutputLines=FALSE;
fCloseBraceStop=FALSE;
}
} /* end of while !in_eof */
/************ per file processing goes above here **************/
Close_Input_File:
if( fOutputFile )
fputs("\n",out_fp);
erc=fclose(in_fp); /* close the target file */
if(erc==-1)
{
printf("Error closing file %d: %s\n",iFiles,Files[iFiles]);
}
Next_Input_File:
continue;
} /* end of for(iFiles=1...nFiles) */
printf("\nTotal output lines: %ld\n",out_line_count);
if( fOutputFile )
{
fprintf(out_fp,"\nTotal output lines: %ld\n",out_line_count);
fclose(out_fp);
}
exit(0);
}
/* vi: set tabstop=4 shiftwidth=4: */