|
|
1.1 root 1: #ifndef lint
2: static char *sccsid = "@(#)addbib.c 4.1 (Berkeley) 5/6/83";
3: #endif
4:
5: #include <stdio.h>
6: #include <ctype.h>
7: #include <signal.h>
8: #define MAXENT 50
9:
10: struct skeleton {
11: char prompt[20]; /* prompt user for entry */
12: char keylet[5]; /* key letter for database */
13: } bibskel[MAXENT] = {
14: " Author:", "%A",
15: " Title:", "%T",
16: " Journal:", "%J",
17: " Volume:", "%V",
18: " Pages:", "%P",
19: "Publisher:", "%I",
20: " City:", "%C",
21: " Date:", "%D",
22: " Other:", "%O",
23: " Keywords:", "%K", };
24:
25: int entries = 10; /* total number of entries in bibskel */
26: int abstract = 1; /* asking for abstracts is the default */
27:
28: usage() /* print proper usage and exit */
29: {
30: puts("Usage: addbib [-p promptfile] [-a] database");
31: puts("\t-p: the promptfile defines alternate fields");
32: puts("\t-a: don't include prompting for the abstract");
33: exit(1);
34: }
35:
36: main(argc, argv) /* addbib: bibliography entry program */
37: int argc;
38: char *argv[];
39: {
40: FILE *fp, *fopen();
41: int i;
42:
43: if (argc == 1)
44: {
45: puts("You must specify a bibliography file (database).");
46: usage();
47: }
48: for (i = 1; argv[i][0] == '-'; i++)
49: {
50: if (argv[i][1] == 'p')
51: {
52: if (i >= argc - 2)
53: {
54: puts("Not enough arguments for -p option.");
55: usage();
56: }
57: rd_skel(argv[++i]);
58: }
59: else if (argv[i][1] == 'a')
60: {
61: if (i >= argc - 1)
62: {
63: puts("No bibliofile specified after -a.");
64: usage();
65: }
66: abstract = 0;
67: }
68: else /* neither -p nor -a */
69: {
70: printf("Invalid command line flag: %s\n", argv[i]);
71: usage();
72: }
73: }
74: if (i < argc - 1)
75: {
76: puts("Too many arguments with no options.");
77: usage();
78: }
79: if ((fp = fopen(argv[i], "a")) == NULL)
80: {
81: perror(argv[i]);
82: exit(1);
83: }
84: addbib(fp, argv[i]); /* loop for input */
85: exit(0);
86: }
87:
88: addbib(fp, argv) /* add entries to a bibliographic database */
89: FILE *fp;
90: char *argv;
91: {
92: char line[BUFSIZ];
93: int i = 0, firstln, repeat = 0, escape = 0;
94:
95: printf("Instructions? ");
96: fgets(line, BUFSIZ, stdin);
97: if (line[0] == 'y' || line[0] == 'Y')
98: instruct();
99: while (1)
100: {
101: putchar('\n');
102: putc('\n', fp);
103: for (i = 0; i < entries; i++)
104: {
105: printf("%s\t", bibskel[i].prompt);
106: fgets(line, BUFSIZ, stdin);
107: if (line[0] == '-' && line[1] == '\n')
108: {
109: i -= 2;
110: if (i < -1)
111: {
112: printf("Too far back.\n");
113: i++;
114: }
115: continue;
116: }
117: else if (line[strlen(line)-2] == '\\')
118: {
119: if (line[0] != '\\')
120: {
121: line[strlen(line)-2] = '\n';
122: line[strlen(line)-1] = NULL;
123: trim(line);
124: fprintf(fp, "%s %s",
125: bibskel[i].keylet, line);
126: }
127: printf("> ");
128: again:
129: fgets(line, BUFSIZ, stdin);
130: if (line[strlen(line)-2] == '\\')
131: {
132: line[strlen(line)-2] = '\n';
133: line[strlen(line)-1] = NULL;
134: trim(line);
135: fputs(line, fp);
136: printf("> ");
137: goto again;
138: }
139: trim(line);
140: fputs(line, fp);
141: }
142: else if (line[0] != '\n')
143: {
144: trim(line);
145: fprintf(fp, "%s %s", bibskel[i].keylet, line);
146: }
147: }
148: if (abstract)
149: {
150: puts(" Abstract: (ctrl-d to end)");
151: firstln = 1;
152: while (fgets(line, BUFSIZ, stdin))
153: {
154: if (firstln && line[0] != '%')
155: {
156: fprintf(fp, "%%X ");
157: firstln = 0;
158: }
159: fputs(line, fp);
160: }
1.1.1.2 ! root 161: rewind(stdin);
1.1 root 162: }
163: fflush(fp); /* write to file at end of each cycle */
164: if (ferror(fp))
165: {
166: perror(argv);
167: exit(1);
168: }
169: editloop:
170: printf("\nContinue? ");
171: fgets(line, BUFSIZ, stdin);
172: if (line[0] == 'e' || line[0] == 'v')
173: {
174: bibedit(fp, line, argv);
175: goto editloop;
176: }
177: if (line[0] == 'q' || line[0] == 'n')
178: return;
179: }
180: }
181:
182: trim(line) /* trim line of trailing white space */
183: char line[];
184: {
185: int n;
186:
187: n = strlen(line);
188: while (--n >= 0)
189: {
190: if (!isspace(line[n]))
191: break;
192: }
193: line[++n] = '\n';
194: line[++n] = NULL;
195: }
196:
197: bibedit(fp, cmd, arg) /* edit database with edit, ex, or vi */
198: FILE *fp;
199: char *cmd, *arg;
200: {
201: int i = 0, status;
202:
203: fclose(fp);
204: while (!isspace(cmd[i]))
205: i++;
206: cmd[i] = NULL;
207: if (fork() == 0)
208: {
209: if (cmd[0] == 'v' && cmd[1] == 'i')
210: execlp(cmd, cmd, "+$", arg, NULL);
211: else /* either ed, ex, or edit */
212: execlp(cmd, cmd, arg, NULL);
213: }
214: signal(SIGINT, SIG_IGN);
215: signal(SIGQUIT, SIG_IGN);
216: wait(&status);
217: signal(SIGINT, SIG_DFL);
218: signal(SIGQUIT, SIG_DFL);
219: if ((fp = fopen(arg, "a")) == NULL)
220: {
221: perror(arg);
222: exit(1);
223: }
224: }
225:
226: instruct() /* give user elementary directions */
227: {
228: putchar('\n');
229: puts("Addbib will prompt you for various bibliographic fields.");
230: puts("If you don't need a particular field, just hit RETURN,");
231: puts("\tand that field will not appear in the output file.");
232: puts("If you want to return to previous fields in the skeleton,");
233: puts("\ta single minus sign will go back a field at a time.");
234: puts("\t(This is the best way to input multiple authors.)");
235: puts("If you have to continue a field or add an unusual field,");
236: puts("\ta trailing backslash will allow a temporary escape.");
237: puts("Finally, (without -a) you will be prompted for an abstract.");
238: puts("Type in as many lines as you need, and end with a ctrl-d.");
239: puts("To quit, type `q' or `n' when asked if you want to continue.");
240: puts("To edit the database, type `edit', `vi', or `ex' instead.");
241: }
242:
243: rd_skel(arg) /* redo bibskel from user-supplied file */
244: char *arg;
245: {
246: FILE *pfp, *fopen();
247: char str[BUFSIZ];
248: int entry, i, j;
249:
250: if ((pfp = fopen(arg, "r")) == NULL)
251: {
252: fprintf(stderr, "Promptfile ");
253: perror(arg);
254: exit(1);
255: }
256: for (entry = 0; fgets(str, BUFSIZ, pfp); entry++)
257: {
258: for (i = 0; str[i] != '\t' && str[i] != '\n'; i++)
259: bibskel[entry].prompt[i] = str[i];
260: bibskel[entry].prompt[i] = NULL;
261: if (str[i] == '\n')
262: {
263: fprintf(stderr, "No tabs between promptfile fields.\n");
264: fprintf(stderr, "Format: prompt-string <TAB> %%key\n");
265: exit(1);
266: }
267: for (i++, j = 0; str[i] != '\n'; i++, j++)
268: bibskel[entry].keylet[j] = str[i];
269: bibskel[entry].keylet[j] = NULL;
270:
271: if (entry >= MAXENT)
272: {
273: fprintf(stderr, "Too many entries in promptfile.\n");
274: exit(1);
275: }
276: }
277: entries = entry;
278: fclose(pfp);
279: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.