Move GDB producer parsing routines to a separate file
[external/binutils.git] / gdb / producer.c
1 /* Producer string parsers for GDB.
2
3    Copyright (C) 2012-2017 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "producer.h"
22
23 /* See producer.h.  */
24
25 int
26 producer_is_gcc_ge_4 (const char *producer)
27 {
28   int major, minor;
29
30   if (! producer_is_gcc (producer, &major, &minor))
31     return -1;
32   if (major < 4)
33     return -1;
34   if (major > 4)
35     return INT_MAX;
36   return minor;
37 }
38
39 /* See producer.h.  */
40
41 int
42 producer_is_gcc (const char *producer, int *major, int *minor)
43 {
44   const char *cs;
45
46   if (producer != NULL && startswith (producer, "GNU "))
47     {
48       int maj, min;
49
50       if (major == NULL)
51         major = &maj;
52       if (minor == NULL)
53         minor = &min;
54
55       /* Skip any identifier after "GNU " - such as "C11" "C++" or "Java".
56          A full producer string might look like:
57          "GNU C 4.7.2"
58          "GNU Fortran 4.8.2 20140120 (Red Hat 4.8.2-16) -mtune=generic ..."
59          "GNU C++14 5.0.0 20150123 (experimental)"
60       */
61       cs = &producer[strlen ("GNU ")];
62       while (*cs && !isspace (*cs))
63         cs++;
64       if (*cs && isspace (*cs))
65         cs++;
66       if (sscanf (cs, "%d.%d", major, minor) == 2)
67         return 1;
68     }
69
70   /* Not recognized as GCC.  */
71   return 0;
72 }
73