1 //
2 // aegis - project change supervisor
3 // Copyright (C) 2005-2008, 2010, 2012 Peter Miller
4 // Copyright (C) 2008 Walter Franzini
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or (at
9 // your option) any 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 GNU
14 // 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, see <http://www.gnu.org/licenses/>.
18 //
19 
20 #include <common/ac/assert.h>
21 
22 #include <libaegis/attribute.h>
23 #include <libaegis/change/file.h>
24 #include <libaegis/fstate.fmtgen.h>
25 #include <libaegis/input/file.h>
26 #include <libaegis/os.h>
27 #include <libaegis/sub.h>
28 
29 #include <aede-policy/validation/files/crlf.h>
30 
31 
~validation_files_crlf()32 validation_files_crlf::~validation_files_crlf()
33 {
34 }
35 
36 
validation_files_crlf()37 validation_files_crlf::validation_files_crlf()
38 {
39 }
40 
41 
42 validation::pointer
create(void)43 validation_files_crlf::create(void)
44 {
45     return pointer(new validation_files_crlf());
46 }
47 
48 
49 bool
check(change::pointer cp,fstate_src_ty * src)50 validation_files_crlf::check(change::pointer cp, fstate_src_ty *src)
51 {
52     nstring path(cp->file_path(src));
53     assert(!path.empty());
54     if (path.empty())
55         return true;
56 
57     //
58     // Don't perform this check for files marked as being allowed to
59     // have crlf.
60     //
61     if
62     (
63         attributes_list_find_boolean
64         (
65             src->attribute,
66             "aede-policy-crlf-allowed"
67         )
68     )
69         return true;
70 
71     os_become_orig();
72     bool crlf_seen = false;
73     bool nul_seen = false;
74     input ip = input_file_open(path);
75     int state = 0;
76     for (;;)
77     {
78         int c = ip->getch();
79         if (c < 0)
80             break;
81         if (c == 0)
82         {
83             //
84             // These days, the only difference between text files
85             // and binary files is the presence of NUL characters.
86             // All other characters can be part of an international
87             // character set encoding.
88             //
89             nul_seen = true;
90         }
91         if (state == '\r' && c == '\n')
92             crlf_seen = true;
93         state = c;
94     }
95     ip.close();
96     os_become_undo();
97     bool result = nul_seen || !crlf_seen;
98     if (!result)
99     {
100         sub_context_ty sc;
101         sc.var_set_string("File_Name", src->file_name);
102         change_error(cp, &sc, i18n("$filename: dos line termination"));
103     }
104     return result;
105 }
106 
107 
108 bool
check_binaries(void) const109 validation_files_crlf::check_binaries(void)
110     const
111 {
112     return false;
113 }
114 
115 
116 // vim: set ts=8 sw=4 et :
117