2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
7 /* All Rights Reserved */
10 * Copyright (c) 1980 Regents of the University of California.
11 * All rights reserved. The Berkeley software License Agreement
12 * specifies the terms and conditions for redistribution.
15 #pragma ident "%Z%%M% %I% %E% SMI"
25 char prompt
[20]; /* prompt user for entry */
26 char keylet
[5]; /* key letter for database */
37 " Keywords:", "%K", };
39 int entries
= 10; /* total number of entries in bibskel */
40 int abstract
= 1; /* asking for abstracts is the default */
42 static void addbib(FILE *, char *);
43 void bibedit(FILE *, char *, char *);
44 static void instruct(void);
45 static void rd_skel(char *);
46 static void trim(char []);
49 usage(void) /* print proper usage and exit */
51 puts(gettext("Usage: addbib [-p promptfile] [-a] database\n\
52 \t-p: the promptfile defines alternate fields\n\
53 \t-a: don't include prompting for the abstract\n"));
58 main(int argc
, char *argv
[]) /* addbib: bibliography entry program */
63 (void) setlocale(LC_ALL
, "");
65 #if !defined(TEXT_DOMAIN)
66 #define TEXT_DOMAIN "SYS_TEST"
68 (void) textdomain(TEXT_DOMAIN
);
72 "You must specify a bibliography file (database)."));
75 for (i
= 1; argv
[i
][0] == '-'; i
++) {
76 if (argv
[i
][1] == 'p') {
78 puts(gettext("Not enough arguments for "
83 } else if (argv
[i
][1] == 'a') {
86 "No bibliofile specified after -a."));
90 } else { /* neither -p nor -a */
92 "Invalid command line flag: %s\n"), argv
[i
]);
97 puts(gettext("Too many arguments with no options."));
100 if ((fp
= fopen(argv
[i
], "a")) == NULL
) {
104 addbib(fp
, argv
[i
]); /* loop for input */
109 addbib(FILE *fp
, char *argv
) /* add entries to a bibliographic database */
112 int i
= 0, firstln
, repeat
= 0, escape
= 0;
114 printf(gettext("Instructions? "));
115 fgets(line
, BUFSIZ
, stdin
);
116 if (line
[0] == 'y' || line
[0] == 'Y')
121 for (i
= 0; i
< entries
; i
++) {
122 printf("%s\t", gettext(bibskel
[i
].prompt
));
123 if (fgets(line
, BUFSIZ
, stdin
) == NULL
) {
127 if (line
[0] == '-' && line
[1] == '\n') {
130 printf(gettext("Too far back.\n"));
134 } else if (line
[strlen(line
)-2] == '\\') {
135 if (line
[0] != '\\') {
136 line
[strlen(line
)-2] = '\n';
137 line
[strlen(line
)-1] = NULL
;
140 bibskel
[i
].keylet
, line
);
144 fgets(line
, BUFSIZ
, stdin
);
145 if (line
[strlen(line
)-2] == '\\') {
146 line
[strlen(line
)-2] = '\n';
147 line
[strlen(line
)-1] = NULL
;
155 } else if (line
[0] != '\n') {
157 fprintf(fp
, "%s %s", bibskel
[i
].keylet
, line
);
161 puts(gettext(" Abstract: (ctrl-d to end)"));
163 while (fgets(line
, BUFSIZ
, stdin
)) {
164 if (firstln
&& line
[0] != '%') {
172 fflush(fp
); /* write to file at end of each cycle */
178 printf(gettext("\nContinue? "));
179 fgets(line
, BUFSIZ
, stdin
);
180 if (line
[0] == 'e' || line
[0] == 'v') {
181 bibedit(fp
, line
, argv
);
184 if (line
[0] == 'q' || line
[0] == 'n')
190 trim(char line
[]) /* trim line of trailing white space */
196 if (!isspace(line
[n
]))
204 bibedit(FILE *fp
, char *cmd
, char *arg
) /* edit database with edit, ex, or vi */
209 while (!isspace(cmd
[i
]))
213 if (cmd
[0] == 'v' && cmd
[1] == 'i')
214 execlp(cmd
, cmd
, "+$", arg
, NULL
);
215 else /* either ed, ex, or edit */
216 execlp(cmd
, cmd
, arg
, NULL
);
218 signal(SIGINT
, SIG_IGN
);
219 signal(SIGQUIT
, SIG_IGN
);
221 signal(SIGINT
, SIG_DFL
);
222 signal(SIGQUIT
, SIG_DFL
);
223 if ((fp
= fopen(arg
, "a")) == NULL
) {
230 instruct(void) /* give user elementary directions */
234 "Addbib will prompt you for various bibliographic fields.\n"
235 "If you don't need a particular field, just hit RETURN,\n"
236 "\tand that field will not appear in the output file.\n"
237 "If you want to return to previous fields in the skeleton,\n"
238 "\ta single minus sign will go back a field at a time.\n"
239 "\t(This is the best way to input multiple authors.)\n"
240 "If you have to continue a field or add an unusual field,\n"
241 "\ta trailing backslash will allow a temporary escape.\n"
242 "Finally, (without -a) you will be prompted for an abstract\n"
243 "Type in as many lines as you need, and end with a ctrl-d.\n"
244 "To quit, type `q' or `n' when asked if you want to continue.\n"
245 "To edit the database, type `edit', `vi', or `ex' instead."));
250 rd_skel(char *arg
) /* redo bibskel from user-supplied file */
256 if ((pfp
= fopen(arg
, "r")) == NULL
) {
257 fprintf(stderr
, gettext("Promptfile "));
261 for (entry
= 0; fgets(str
, BUFSIZ
, pfp
); entry
++) {
262 for (i
= 0; str
[i
] != '\t' && str
[i
] != '\n'; i
++)
263 bibskel
[entry
].prompt
[i
] = str
[i
];
264 bibskel
[entry
].prompt
[i
] = NULL
;
265 if (str
[i
] == '\n') {
266 fprintf(stderr
, gettext(
267 "No tabs between promptfile fields.\n"));
268 fprintf(stderr
, gettext(
269 "Format: prompt-string <TAB> %%key\n"));
272 for (i
++, j
= 0; str
[i
] != '\n'; i
++, j
++)
273 bibskel
[entry
].keylet
[j
] = str
[i
];
274 bibskel
[entry
].keylet
[j
] = NULL
;
276 if (entry
>= MAXENT
) {
277 fprintf(stderr
, gettext(
278 "Too many entries in promptfile.\n"));