1 /* Define all procedures which are external to each other.
2    Copyright (C) 1993, 1995, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3    Contributed by Tim Wicinski (wicinski@barn.com).
4    Hacked by Brendan Kehoe (brendan@cygnus.com).
5    Further hacked by Milan Zamazal (pdm@zamazal.org).
6 
7 This file is part of GNU GNATS.
8 
9 GNU GNATS is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13 
14 GNU GNATS is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with GNU GNATS; see the file COPYING.  If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111, USA.  */
22 
23 #ifndef _gnats_h_
24 #define _gnats_h_
25 
26 
27 
28 /* System wide includes & definitions */
29 /* PLEASE!  For the saving of future maintainers, if you add any strange
30    declaration here, *explain it*!  Otherwise we will have either to retain
31    the whole increasing garbage forever, or to break portability. */
32 
33 #include "config.h"
34 
35 #include <stdio.h>
36 #include <time.h>
37 /* On FreeBSD this ends up defining RE_DUP_MAX.  We need to make sure it's
38    included before our regex.h. */
39 #include <sys/param.h>
40 
41 /* Use some miscelaneous functions; should be included after stdio.h */
42 #include "ansidecl.h"
43 
44 /* If system have getopt_long() it seems GNU compliant and likely use
45    GNU-compatible getopt() as well. Otherwise, we will use getopt*()
46    from libiberty. */
47 #ifdef HAVE_GETOPT
48 #define HAVE_DECL_GETOPT 1
49 #else
50 #define HAVE_DECL_GETOPT 0
51 #endif /* HAVE_GETOPT */
52 #ifdef HAVE_GETOPT_LONG
53 #include <getopt.h>
54 #else
55 #include "gnugetopt.h"
56 #endif /* HAVE_GETOPT_LONG */
57 
58 #include <sys/types.h>
59 #include <sys/stat.h>
60 #ifndef S_ISREG			/* Doesn't have POSIX.1 stat stuff. */
61 #define mode_t unsigned short
62 #endif
63 #if !defined(S_ISREG) && defined(S_IFREG)
64 #define	S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
65 #endif
66 #ifndef S_ISLNK
67 #define lstat stat
68 #endif
69 
70 #ifdef HAVE_MEMORY_H
71 #include <memory.h>
72 #endif
73 #ifdef HAVE_UNISTD_H
74 #include <unistd.h>
75 #endif
76 #ifdef HAVE_SYSLOG_H
77 #include <syslog.h>
78 #endif
79 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
80 #include <string.h>
81 #else
82 #include <strings.h>
83 #endif
84 
85 #include <errno.h>
86 #ifdef STDC_HEADERS
87 #define getopt system_getopt
88 #include <stdlib.h>
89 #undef getopt
90 #else /* not STDC_HEADERS */
91 char *getenv ();
92 extern int errno;
93 #endif /* STDC_HEADERS */
94 
95 #if defined(HAVE_FCNTL_H) || defined(_POSIX_VERSION)
96 #include <fcntl.h>
97 #else
98 #include <sys/file.h>
99 #endif
100 
101 #if defined(HAVE_LIBGEN_H)
102 #  if defined(HAVE_BASENAME) || defined(HAVE_DIRNAME)
103 #include <libgen.h>
104 #  endif
105 #endif /* HAVE_LIBGEN_H */
106 
107 #ifndef alloca
108 /* Make alloca work the best possible way.  */
109 #ifdef __GNUC__
110 #define alloca __builtin_alloca
111 #else /* not __GNUC__ */
112 #if HAVE_ALLOCA_H
113 #include <alloca.h>
114 #else /* not __GNUC__ or HAVE_ALLOCA_H */
115 #ifndef _AIX /* Already did AIX, up at the top.  */
116 char *alloca ();
117 #endif /* not _AIX */
118 #endif /* not HAVE_ALLOCA_H */
119 #endif /* not __GNUC__ */
120 #endif /* not alloca */
121 
122 #if defined(__STDC__) || defined(_AIX)
123 #include <stdarg.h>
124 #else
125 #include <varargs.h>
126 #endif
127 
128 #include <ctype.h>
129 #include <signal.h>
130 #include <pwd.h>
131 
132 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
133 # define __attribute__(x)
134 #endif
135 
136 #ifndef ATTRIBUTE_UNUSED
137 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
138 #endif
139 
140 #if ! HAVE_DECL_UNSETENV
141 extern void unsetenv (const char *name);
142 #endif
143 
144 #if defined (__svr4__) && defined (__sun__)
145 extern int gethostname (char *name, int namelen);
146 #endif
147 
148 
149 
150 /* Types */
151 
152 /* A generic boolean type.  */
153 typedef short bool;
154 #ifndef TRUE
155 #define TRUE 1
156 #endif
157 #ifndef FALSE
158 #define FALSE 0
159 #endif
160 
161 #ifndef SEEK_SET
162 #define SEEK_SET 0
163 #define SEEK_CUR 1
164 #define SEEK_END 2
165 #endif
166 
167 #ifndef RETSIGTYPE
168 #define RETSIGTYPE void
169 #endif
170 
171 /* A cons whose car is a string. */
172 typedef struct string_list
173 {
174   char *name;
175   struct string_list *next;
176 } StringList;
177 
178 typedef struct error_desc *ErrorDesc;
179 typedef struct bad_fields *BadFields;
180 
181 /* Possible values for the logging mode.  Note "syslog" will only be used
182    if the system supports it---otherwise it will relegate to "mail".
183    TODO: Fix this comment. */
184 typedef enum
185 {
186   SYSLOG,
187   MAIL,
188   LOGFILE,
189   STDERR,
190   NOLOG
191 } Logging_Methods;
192 
193 /* XXX ??? !!! What is this stuff for?  */
194 #ifndef LOGGING
195 # ifdef HAVE_SYSLOG_H
196 #  define LOGGING SYSLOG
197 #  ifndef LOG_LEVEL
198 #   ifdef LOG_USER
199 #    define LOG_LEVEL LOG_USER
200 extern int log_level;
201 #   endif
202 #  endif
203 # else /* !HAVE_SYSLOG_H */
204 /* FIXME */
205 #  define LOGGING MAIL
206 #  define LOG_ERR 0
207 #  define LOG_INFO 1
208 # endif /* HAVE_SYSLOG_H */
209 #endif /* LOGGING */
210 #define LOG_NONE -1
211 
212 
213 
214 /* Constants */
215 
216 /* This used to be 30, but we upped it to 50 to make sure that int'l
217    timezones will fit. */
218 /* FIXME: This introduces an arbitrary limit on a buffer size.  It should be
219    removed. */
220 #define GNATS_TIME_LENGTH 50
221 
222 /* Exit codes. */
223 extern const int EXIT_OK;
224 extern const int EXIT_PROGRAM_ERROR;
225 
226 /* Files that controls access to the database. */
227 #define DB_ACCESS_FILE "gnatsd.user_access"
228 #define DB_HOST_ACCESS_FILE "gnatsd.host_access"
229 
230 /* Default values of field properties. */
231 #define DEFAULT_MULTIENUM_SEPARATOR ": "
232 
233 
234 
235 /* Global variables */
236 
237 /* Current version of GNATS. */
238 extern char *version_string;
239 
240 /* The name this program was run with. */
241 extern const char *program_name;
242 
243 
244 
245 /* Local includes. */
246 
247 #include "field.h"
248 #include "database.h"
249 #include "index.h"
250 #include "pr.h"
251 #include "query.h"
252 #include "regex.h"
253 #include "adm.h"
254 
255 
256 
257 /* Functions */
258 
259 /* in btime.c */
260 extern struct tm *get_response_time (const DatabaseInfo database,
261 				     unsigned int responseTimeInDays);
262 
263 /* in client.c */
264 extern int get_reply (FILE *);
265 extern void client_exit (void);
266 extern void safe_exit (void);
267 extern void client_chdb (const char *database);
268 extern int client_lock_gnats (const DatabaseInfo database, ErrorDesc *err);
269 extern void client_unlock_gnats (void);
270 
271 extern void sendRemoteQuery (const char *queryExpr,
272 			     const char **args,
273 			     const char *queryFormatName, FILE *outfile);
274 
275 extern void sendRemoteListQuery (ListTypes whichList, FILE *outfile);
276 
277 extern void scanEnv (char **user, char **passwd,
278 		     char **hostname, int *port, char **database);
279 
280 extern int client_init_gnats (ErrorDesc *err,
281 			      const char *user, const char *password,
282 			      const char *hostname, int port,
283 			      const char *database);
284 
285 extern void client_print_errors (const DatabaseInfo database,
286 				 ErrorDesc errDesc);
287 
288 extern void tohex (char *ptr, char *buf, int sz);
289 extern int hexdigit (char c);
290 extern void fromhex (char *ptr, char *buf, int sz);
291 
292 extern void clientGetAdmField (FILE *outfile, const char *admField,
293 			       const char *admSubfield, const char *admKey);
294 
295 extern PR *clientReadPR (const char *prNum);
296 /* Returns a list of PRs that match EXPR.  */
297 extern StringList *clientGetPRList (const char *expr);
298 
299 extern void netCheckPR (FILE *file, int initialPR);
300 
301 extern void netEditField (FILE *fieldData, const char *prNum,
302 			  const char *fieldName,
303 			  const char *editUserEmailAddr, int appendToField,
304 			  char *reason);
305 
306 extern void netSubmitNewPR (FILE *file, int show_prnum);
307 extern void netModifyPR (FILE *file, const char *prNum,
308 			 const char *editUserEmailAddr);
309 extern void netLockDB (void);
310 extern void netUnlockDB (void);
311 extern void netLockPR (const char *prNum, const char *username,
312 		       const char *processID);
313 extern void netUnlockPR (const char *prNum);
314 extern void netDeletePR (const char *prNum, const char *editUserEmailAddr);
315 extern void netSetEditEmailAddr (const char *address);
316 extern void netFieldFlags (const char *fieldname);
317 extern void netValidValues (const char *fieldname);
318 extern void netFieldDescription (const char *fieldname);
319 extern void netFieldType (const char *fieldname);
320 
321 /* in edit.c */
322 extern int check_pr_file (const DatabaseInfo database, FILE *infile,
323 			  ErrorDesc *err, int initial_entry);
324 extern int check_pr (PR *pr,
325 		     ErrorDesc *err,
326 		     int initial_entry);
327 extern int replace_pr (const DatabaseInfo database, FILE *infile,
328 		       const char *editUserEmailAddr, ErrorDesc *err);
329 extern int lock_pr (const DatabaseInfo database,
330 		    const char *filename, const char *username,
331 		    const char *processid, ErrorDesc *error);
332 extern int unlock_pr (const DatabaseInfo database,
333 		      const char *filename, ErrorDesc *error);
334 extern int validateFieldValue (FieldIndex field, const char *text,
335 			       ErrorDesc *err,
336 			       int dontCheckEnums);
337 extern int edit_field (const DatabaseInfo database, const char *prnum,
338 		       FieldIndex fieldIndex, int append, char *newcontents,
339 		       char *changeReason, const char *editUserEmailAddr,
340 		       ErrorDesc *err);
341 extern int applyFieldEdit (PR *pr, FieldEdit *edit, ErrorDesc *err,
342 			   FormatNamedParameter *params);
343 extern int deletePR (const DatabaseInfo database, const char *prNum,
344 		     const char *editUserEmailAddr, ErrorDesc *err);
345 
346 /* in fconfigl.l and fconfig.y */
347 extern void fconferror (const char *string);
348 extern int fconflex (void);
349 extern int yylex (void);
350 
351 /* in file-pr.c */
352 extern int submit_pr (const DatabaseInfo database, FILE *, ErrorDesc *err);
353 
354 /* in getdate.c */
355 extern time_t get_date (char *, ...);
356 
357 /* in internal.c */
358 extern void block_signals (void);
359 extern void unblock_signals (void);
360 extern int is_gnats_locked (const DatabaseInfo database);
361 extern int lock_gnats (const DatabaseInfo database, ErrorDesc *);
362 extern int unlock_gnats (const DatabaseInfo database, ErrorDesc *);
363 extern void punt (const DatabaseInfo database, int, ...);
364 extern int copy_file (const char *, const char *);
365 extern const char *get_prid_from_path (const char*);
366 extern const char *get_cat_prid_from_path (const char*);
367 extern char *get_lock_path (const DatabaseInfo database, const char *prID);
368 extern char *get_curr_date (void);
369 extern int gnatsdbHasNetconn (const char *database_name);
370 extern int isPrLocked (const DatabaseInfo database, const char *prNum);
371 extern int fileExists (const char *filename);
372 extern void setError (ErrorDesc *errorDesc, int errorCode, ...);
373 extern BadFields newBadFieldEntry (FieldIndex field, const char *badValue,
374 				  BadFields next);
375 extern void freeBadFieldList (BadFields list);
376 extern void freeErrorDesc (ErrorDesc desc);
377 extern int getErrorCode (ErrorDesc errorDesc);
378 extern const char *getErrorMessage (ErrorDesc errorDesc);
379 extern BadFields getBadFieldList (ErrorDesc errorDesc);
380 extern const char *badFieldValue (BadFields ent);
381 extern FieldIndex badFieldIndex (BadFields ent);
382 extern BadFields nextBadField (BadFields ent);
383 extern int check_state_type (const DatabaseInfo database,
384 			     const char *state, const char *type);
385 
386 /* in lists.c */
387 extern ListTypes stringToListType (const char *string);
388 extern const char *listTypeToString (ListTypes which);
389 extern int getGnatsFile (const DatabaseInfo database, ListTypes which,
390 			 const char *file, const char *eolTerminator);
391 
392 /* in misc.c */
393 extern DatabaseInfo init_gnats (const char *program_name,
394 				const char *database_name,
395 				ErrorDesc *err);
396 extern void log_msg (int, int, ...);
397 extern void init_logging (const char *filename);
398 extern void enable_debugging (void);
399 extern char *read_line (FILE *input_des, size_t *max_len);
400 extern char *get_next_field (const char **line, int delim);
401 extern char *get_token (char *, char *);
402 extern FILE *open_mail_file (const DatabaseInfo database);
403 extern void close_mail_file (FILE *);
404 extern char *quote_string (const char *string);
405 extern StringList *new_string_list_ent (char *string, StringList *next);
406 extern PTR xmalloc (size_t size);
407 extern PTR xrealloc (PTR ptr, size_t size);
408 extern char *xstrdup (const char *string);
409 extern char *xstrndup (const char *string, size_t length);
410 extern const char *temporary_directory (void);
411 extern int open_temporary_file (char *template, int mode);
412 extern FILE *fopen_temporary_file (char *template, const char *fopen_mode,
413 				   const int mode);
414 extern size_t gnats_strftime (char *s, size_t size, const char *template,
415 			      const struct tm *brokentime);
416 extern void usage (const char *const texts[], int exit_code);
417 extern void version (const char *const program_name);
418 extern bool value_is_empty (const char *string);
419 #ifndef HAVE_ASPRINTF
420 extern int asprintf (char **ret, const char *format, ...) ATTRIBUTE_PRINTF_2;
421 #endif
422 #ifndef HAVE_VASPRINTF
423 extern int vasprintf (char **ret, const char *format, va_list) ATTRIBUTE_PRINTF(2,0);
424 #endif
425 #ifndef HAVE_BASENAME
426 extern char *basename (char *path);
427 #endif
428 
429 #endif /* !_gnats_h_ */
430