annotate src/ctrlvalue.c @ 754:ecb991e41a4c

Add $controlN$ substitution The interfaces to substitute() and substitute_one() have changed, as they now need to know the listdir to be able to find control files
author Ben Schmidt
date Wed, 06 Oct 2010 23:30:26 +1100
parents 7da5c55b9330
children bb803487199c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
89
mmj
parents:
diff changeset
1 /* Copyright (C) 2004 Mads Martin Joergensen <mmj at mmj.dk>
mmj
parents:
diff changeset
2 *
mmj
parents:
diff changeset
3 * $Id$
mmj
parents:
diff changeset
4 *
225
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 164
diff changeset
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 164
diff changeset
6 * of this software and associated documentation files (the "Software"), to
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 164
diff changeset
7 * deal in the Software without restriction, including without limitation the
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 164
diff changeset
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 164
diff changeset
9 * sell copies of the Software, and to permit persons to whom the Software is
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 164
diff changeset
10 * furnished to do so, subject to the following conditions:
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 164
diff changeset
11 *
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 164
diff changeset
12 * The above copyright notice and this permission notice shall be included in
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 164
diff changeset
13 * all copies or substantial portions of the Software.
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 164
diff changeset
14 *
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 164
diff changeset
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 164
diff changeset
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 164
diff changeset
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 164
diff changeset
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 164
diff changeset
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 164
diff changeset
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 164
diff changeset
21 * IN THE SOFTWARE.
89
mmj
parents:
diff changeset
22 */
mmj
parents:
diff changeset
23
mmj
parents:
diff changeset
24 #include <sys/types.h>
mmj
parents:
diff changeset
25 #include <sys/stat.h>
mmj
parents:
diff changeset
26 #include <stdlib.h>
164
1e60714b0bd5 -#include <stdio.h>
mmj
parents: 89
diff changeset
27 #include <fcntl.h>
1e60714b0bd5 -#include <stdio.h>
mmj
parents: 89
diff changeset
28 #include <unistd.h>
89
mmj
parents:
diff changeset
29
mmj
parents:
diff changeset
30 #include "strgen.h"
mmj
parents:
diff changeset
31 #include "ctrlvalue.h"
mmj
parents:
diff changeset
32 #include "mygetline.h"
mmj
parents:
diff changeset
33 #include "chomp.h"
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
34 #include "memory.h"
89
mmj
parents:
diff changeset
35
754
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
36 static char *ctrlval(const char *listdir, const char *ctrlstr, int oneline)
89
mmj
parents:
diff changeset
37 {
291
7da5c55b9330 0.8.3 commit
mmj
parents: 245
diff changeset
38 char *filename, *value = NULL;
754
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
39 int ctrlfd, i;
89
mmj
parents:
diff changeset
40
291
7da5c55b9330 0.8.3 commit
mmj
parents: 245
diff changeset
41 if(listdir == NULL)
7da5c55b9330 0.8.3 commit
mmj
parents: 245
diff changeset
42 return NULL;
7da5c55b9330 0.8.3 commit
mmj
parents: 245
diff changeset
43
7da5c55b9330 0.8.3 commit
mmj
parents: 245
diff changeset
44 filename = concatstr(3, listdir, "/control/", ctrlstr);
164
1e60714b0bd5 -#include <stdio.h>
mmj
parents: 89
diff changeset
45 ctrlfd = open(filename, O_RDONLY);
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
46 myfree(filename);
89
mmj
parents:
diff changeset
47
164
1e60714b0bd5 -#include <stdio.h>
mmj
parents: 89
diff changeset
48 if(ctrlfd < 0)
1e60714b0bd5 -#include <stdio.h>
mmj
parents: 89
diff changeset
49 return NULL;
754
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
50
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
51 if (oneline) {
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
52 value = mygetline(ctrlfd);
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
53 chomp(value);
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
54 } else {
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
55 value = mygetcontent(ctrlfd);
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
56 i = strlen(value) - 1;
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
57 if (i >= 0 && value[i] == '\n') {
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
58 value[i] = '\0';
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
59 i--;
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
60 }
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
61 if (i >= 0 && value[i] == '\r') {
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
62 value[i] = '\0';
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
63 i--;
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
64 }
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
65 }
164
1e60714b0bd5 -#include <stdio.h>
mmj
parents: 89
diff changeset
66 close(ctrlfd);
89
mmj
parents:
diff changeset
67
mmj
parents:
diff changeset
68 return value;
mmj
parents:
diff changeset
69 }
mmj
parents:
diff changeset
70
754
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
71 char *ctrlvalue(const char *listdir, const char *ctrlstr)
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
72 {
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
73 return ctrlval(listdir, ctrlstr, 1);
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
74 }
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
75
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
76 char *ctrlcontent(const char *listdir, const char *ctrlstr)
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
77 {
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
78 return ctrlval(listdir, ctrlstr, 0);
ecb991e41a4c Add $controlN$ substitution
Ben Schmidt
parents: 291
diff changeset
79 }