Imported Upstream version 2.9.0
[debian/cc1111] / support / Util / BuildCmd.c
1 /*-------------------------------------------------------------------------
2   BuildCmd - SDCC Support function
3
4              Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1999)
5
6    This program is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any
9    later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20    In other words, you are welcome to use, share and improve this program.
21    You are forbidden to forbid anyone else to use, share and improve
22    what you give them.   Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
24
25 /*! Build a command line with parameter substitution
26 */
27
28 #include <string.h>
29 #include <assert.h>
30 #include "SDCCset.h"
31 #include "BuildCmd.h"
32
33 void
34 buildCmdLine (char *into, const char **cmds,
35               const char *p1, const char *p2,
36               const char *p3, set *list)
37 {
38   int first = 1;
39
40   assert(cmds != NULL);
41   assert(into != NULL);
42
43   *into = '\0';
44
45   while (*cmds) {
46     const char *p, *from, *par;
47     int sep = 1;
48
49     from = *cmds;
50     cmds++;
51
52     /* See if it has a '$' anywhere - if not, just copy */
53     if ((p = strchr (from, '$'))) {
54       /* include first part of cmd */
55       if (p != from) {
56         if (!first && sep)
57           strcat(into, " ");
58         strncat(into, from, p - from);
59         sep = 0;
60       }
61       from = p + 2;
62
63       /* include parameter */
64       p++;
65       switch (*p) {
66       case '1':
67         par = p1;
68         break;
69
70       case '2':
71         par = p2;
72         break;
73
74       case '3':
75         par = p3;
76         break;
77
78       case 'l':
79         {
80           const char *tmp;
81           par = NULL;
82
83           if (list != NULL && (tmp = (const char *)setFirstItem(list)) != NULL) {
84             do
85             {
86               if (*tmp != '\0') {
87                 if (sep)
88                   strcat(into, " ");  /* seperate it */
89                 strcat(into, tmp);
90                 tmp++;
91                 sep = 1;
92               }
93             } while ((tmp = (const char *)setNextItem(list)) != NULL);
94           }
95         }
96         break;
97
98       default:
99         par = NULL;
100         assert(0);
101       }
102
103       if (par && *par != '\0') {
104         if (!first && sep)
105           strcat(into, " ");   /* seperate it */
106         strcat(into, par);
107         sep = 0;
108       }
109     }
110
111     /* include the rest of cmd, e.g. ".asm" from "$1.asm" */
112     if (*from != '\0') {
113       if (!first && sep)
114         strcat(into, " ");   /* seperate it */
115       strcat(into, from);
116       sep = 0;
117     }
118
119     first = 0;
120   }
121 }