2 * Copyright (C) 2000-2005 Erik Edelmann <Erik.Edelmann@iki.fi>
4 * This program is free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation;
7 * either version 2 of the License, or (at your option) any
10 * This program is distributed in the hope that it will be
11 * useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 * PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307 USA
26 #include "errormesg.h"
30 /* Copy src to dest, converting uppercase letters to lowercase. */
32 void strtolower(char *dest
, const char *src
)
36 for (i
= 0; src
[i
]; i
++)
37 dest
[i
] = tolower(src
[i
]);
41 /* replace the suffix (everything after the last '.'), including the '.', with
42 * 'new_suffix'. If there is no suffix in 'filename', concatenate 'new_suffix'
45 char *replace_suffix(const char *filename
, const char *new_suffix
)
50 sl
= strlen(new_suffix
);
52 /* Search for last '.' in filename */
53 fl
= n
= strlen(filename
);
54 while (filename
[n
] != '.' && n
>= 0) n
--;
57 /* if there was no '.' */
58 rs
= (char *)xmalloc ((fl
+sl
+2)*sizeof(char));
60 strcat(rs
, new_suffix
);
62 rs
= (char *)xmalloc ((n
+sl
+2)*sizeof(char));
63 strncpy(rs
, filename
, n
);
65 strcat(rs
, new_suffix
);
72 /* If filename has no path, append 'path' to the beginning of the filename,
73 * else replace the existing path (everything before the first '/') with 'path'.
76 char *set_path(const char *filename
, const char *path
)
83 fl
= n
= strlen(filename
);
84 while (filename
[n
] != '/' && n
>= 0) n
--;
88 /* if there was no '/' */
89 rs
= (char *)xmalloc((fl
+pl
+2)*sizeof(char));
93 rs
= (char *)xmalloc((nl
+pl
+2)*sizeof(char));
95 strcat(rs
, &filename
[n
+1]);
102 /* Create and return a copy of s with all citationmarks (" and ') removed. */
104 char *remove_citation(const char *s
)
109 d
= (char *)xmalloc((strlen(s
)+1)*sizeof(char));
110 for (i
= j
= 0; s
[i
]; i
++)
111 if (s
[i
] != '"' && s
[i
] != '\'') d
[j
++] = s
[i
];
118 char *expand_rule(const char *r
, const char *srcfile
)
121 int i
, j
, k
, rlen
, slen
;
123 rule
= (char *)xmalloc(RULE_LENGTH
*sizeof(char));
126 /* set slen = length of srcfile without suffix */
127 for (slen
= strlen(srcfile
); slen
> 0 && srcfile
[slen
] != '.'; slen
--);
129 /* If there was no suffix, set slen = strlen(srcfile) */
130 if (slen
== 0) slen
= strlen(srcfile
);
134 for (i
= 0; i
< rlen
; i
++) {
141 for (j
= 0; j
< slen
; j
++) rule
[k
++] = srcfile
[j
];
147 warning("Unknown modifier '%%%c' in rule '%s'", r
[i
], r
);
158 /* Open 'fname' for reading; first look for 'fname' in the current working
159 * directory, and, if not found, in the list of paths in 'path'. Return NULL if
160 * 'fname' isn't found anywhere. */
162 FILE *open_src_file(const char *fname
, const List
*path
)
168 fd
= fopen(fname
, "r");
169 if (fd
== NULL
&& path
) {
170 for (h
= path
; h
&& fd
== NULL
; h
= h
->next
) {
171 fn
= xmalloc(strlen(h
->data
) + strlen(fname
) + 2);
172 strcpy(fn
, (char *)h
->data
);