1 /* Routines to provide lists of the GNATS low-level files.
2    Copyright (C) 1995, 1999, 2000 Free Software Foundation, Inc.
3    Contributed by Brendan Kehoe (brendan@cygnus.com).
4    Majorly reworked by Bob Manson (manson@juniper.net).
5 
6 This file is part of GNU GNATS.
7 
8 GNU GNATS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12 
13 GNU GNATS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with GNU GNATS; see the file COPYING.  If not, write to the Free
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22 
23 #include "gnats.h"
24 #include "query.h"
25 
26 /* The maximum length of a file suffix listed below.  */
27 #define MAXSUFFIX 4
28 struct list_type_list {
29   const char *name;
30   ListTypes which;
31   const char *fileSuffix;
32   const char *builtinFieldName;
33 } listTypeList[] = {
34   { "Categories",         ListCategories,         ".cat", "category" },
35   { "Submitters",         ListSubmitters,         ".sub", "submitter-id" },
36   { "Responsible",        ListResponsible,        ".res", "responsible" },
37   { "States",             ListStates,             ".sta", "state" },
38   { "FieldNames",         ListFieldNames,         ".fns", NULL },
39   { "InitialInputFields", ListInitialInputFields, ".lfn", NULL },
40   { "InitialRequiredFields", ListInitialRequiredFields, ".lrn", NULL },
41   { "Databases",          ListDatabases,          ".dbl", NULL },
42   { NULL,		  InvalidListType,	  NULL,   NULL }
43 };
44 
45 ListTypes
stringToListType(const char * string)46 stringToListType (const char *string)
47 {
48   int x;
49 
50   for (x = 0; listTypeList[x].name != NULL; x++)
51     {
52       if (strcasecmp (string, listTypeList[x].name) == 0)
53 	{
54 	  return listTypeList[x].which;
55 	}
56     }
57   return -1;
58 }
59 
60 const char *
listTypeToString(ListTypes which)61 listTypeToString (ListTypes which)
62 {
63   int x;
64 
65   for (x = 0;
66        listTypeList[x].which != which && listTypeList[x].name != NULL;
67        x++)
68     {
69       /* Empty */
70     }
71   return listTypeList[x].name;
72 }
73 
74 int
getGnatsFile(const DatabaseInfo database,ListTypes whichList,const char * file,const char * eolTerminator)75 getGnatsFile (const DatabaseInfo database, ListTypes whichList,
76 	      const char *file, const char *eolTerminator)
77 {
78   char *outf = NULL;
79   FILE *fpout = NULL;
80   struct list_type_list *list_desc;
81   int x;
82   AdmEntry *chain = NULL;
83   int fieldCount = 0;
84   ErrorDesc err;
85 
86   for (x = 0;
87        listTypeList[x].which != whichList
88 	 && listTypeList[x].name != NULL; x++)
89     {
90       /* Empty */
91     }
92 
93   if (listTypeList[x].name == NULL)
94     {
95       return -1;
96     }
97   else
98     {
99       list_desc = &(listTypeList[x]);
100     }
101 
102   if (file != NULL)
103     {
104       outf = (char *) xmalloc (strlen (file) + MAXSUFFIX + 1);
105       strcpy (outf, file);
106       eolTerminator = "\n";
107     }
108   else
109     {
110       fpout = stdout;
111     }
112 
113   if (outf != NULL)
114     {
115       strcat (outf, list_desc->fileSuffix);
116     }
117 
118   if (outf != NULL)
119     {
120       fpout = fopen (outf, "w");
121       if (fpout == (FILE *)NULL)
122         return -1; /* XXX */
123     }
124 
125   switch (list_desc->which)
126     {
127     case ListFieldNames:
128       {
129 	int x;
130 	FieldIndex f;
131 
132 	for (x = 0; (f = getNthField (database, x)) != NULL; x++)
133 	  {
134 	    fprintf (fpout, "%s\n", fieldDefForIndex (f)->name);
135 	  }
136 	break;
137       }
138     case ListInitialInputFields:
139       {
140 	InputTemplate *t;
141 
142 	for (t = getInputTemplate (database); t != NULL; t = t->next)
143 	  {
144 	    FieldDef field = fieldDefForIndex (t->index);
145 	    fprintf (fpout, "%s\n", field->name);
146 	  }
147 	break;
148       }
149     case ListInitialRequiredFields:
150       {
151 	FieldList fields = getRequiredInputFields(database);
152 	while (fields != NULL)
153 	  {
154 	    fprintf (fpout, "%s\n", complexFieldIndexToString (fields->ent));
155 	    fields = fields->next;
156 	  }
157 	break;
158       }
159     case ListDatabases:
160       chain = getDatabaseList (&err);
161       fieldCount = 3;
162       break;
163     case ListStates:
164     case ListCategories:
165     case ListSubmitters:
166     case ListResponsible:
167       {
168 	FieldIndex field = findBuiltinField (database,
169 					    list_desc->builtinFieldName);
170 	if (field != NULL)
171 	  {
172 	    chain = fieldDefForIndex (field)->adm_contents;
173 	    fieldCount = fieldDefForIndex (field)->adm_db_fields;
174 	  }
175 	else
176 	  {
177 	    chain = NULL;
178 	    fieldCount = 0;
179 	  }
180       }
181       break;
182 
183     case InvalidListType:
184       abort ();
185       break;
186     }
187 
188   while (chain != NULL)
189     {
190       int x;
191 
192       for (x = 0; x < fieldCount; x++)
193 	{
194 	  fprintf (fpout, (x > 0) ? ":%s" : "%s", chain->admFields[x]);
195 	}
196       fprintf (fpout, "%s", eolTerminator);
197       chain = chain->next;
198     }
199 
200   if (outf)
201     {
202       fclose (fpout);
203     }
204 
205   return 0;
206 }
207