1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2010 - 2016, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// http://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////
#include <cmake.h>
#include <Rule.h>
// If <iostream> is included, put it after <stdio.h>, because it includes
// <stdio.h>, and therefore would ignore the _WITH_GETLINE.
#ifdef FREEBSD
#define _WITH_GETLINE
#endif
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <ctime>
////////////////////////////////////////////////////////////////////////////////
// - Read rc file
// - Strip comments
// - Parse rules
//
// Note that it is an error to not have an rc file.
bool loadRules (const std::string& file, std::vector <Rule>& rules)
{
std::ifstream rc (file.c_str ());
if (rc.good ())
{
std::string::size_type comment;
std::string line;
while (std::getline (rc, line)) // Strips \n
{
// Remove comments.
if ((comment = line.find ('#')) != std::string::npos)
line.resize (comment);
// Process each non-trivial line as a rule.
if (line.length () > 1)
{
try
{
rules.push_back (Rule (line));
}
catch (int)
{
// Deliberately ignored - error handling.
}
}
}
rc.close ();
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
// Applies all the rules in all the sections specified.
// Note that processing does not stop after the first rule match, it keeps going.
void applyRules (
Composite& composite,
bool& blanks,
std::vector <Rule>& rules,
const std::vector <std::string>& sections,
const std::string& line)
{
composite.add (line, 0, {0});
for (auto& section : sections)
for (auto& rule : rules)
rule.apply (composite, blanks, section, line);
}
////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv)
{
int status = 0;
try
{
// Locate $HOME.
struct passwd* pw = getpwuid (getuid ());
if (!pw)
throw std::string ("Could not determine the home directory.");
// Assume ~/.clogrc
std::string rcFile = pw->pw_dir;
rcFile += "/.clogrc";
// Process arguments.
std::vector <std::string> sections;
bool prepend_date = false;
bool prepend_time = false;
for (int i = 1; i < argc; ++i)
{
if (!strcmp (argv[i], "-h") ||
!strcmp (argv[i], "--help"))
{
std::cout << '\n'
<< "Usage: clog [<options>] [<section> ...]\n"
<< '\n'
<< " -h|--help Show this usage\n"
<< " -v|--version Show this version\n"
<< " -d|--date Prepend all lines with the current date\n"
<< " -t|--time Prepend all lines with the current time\n"
<< " -f|--file Override default ~/.clogrc\n"
<< '\n';
return status;
}
else if (!strcmp (argv[i], "-v") ||
!strcmp (argv[i], "--version"))
{
std::cout << "\n"
<< PACKAGE_STRING
<< " built for "
#if defined (DARWIN)
<< "Darwin"
#elif defined (SOLARIS)
<< "Solaris"
#elif defined (CYGWIN)
<< "Cygwin"
#elif defined (OPENBSD)
<< "OpenBSD"
#elif defined (FREEBSD)
<< "FreeBSD"
#elif defined (NETBSD)
<< "NetBSD"
#elif defined (LINUX)
<< "Linux"
#elif defined (KFREEBSD)
<< "GNU/kFreeBSD"
#elif defined (GNUHURD)
<< "GNU/Hurd"
#else
<< "Unknown"
#endif
<< "\n"
<< "Copyright (C) 2010 - 2016 Göteborg Bit Factory\n"
<< "\n"
<< "Clog may be copied only under the terms of the MIT "
"license, which may be found in the source kit.\n"
<< "\n"
<< "Documentation for clog can be found using 'man clog' "
"or at http://tasktools.org/projects/clog.html\n"
<< "\n";
return status;
}
else if (!strcmp (argv[i], "-d") ||
!strcmp (argv[i], "--date"))
{
prepend_date = true;
}
else if (!strcmp (argv[i], "-t") ||
!strcmp (argv[i], "--time"))
{
prepend_time = true;
}
else if (argc > i + 1 &&
(!strcmp (argv[i], "-f") ||
!strcmp (argv[i], "--file")))
{
rcFile = argv[++i];
}
else
{
sections.push_back (argv[i]);
}
}
// Use a default section if one was not specified.
if (sections.size () == 0)
sections.push_back ("default");
// Read rc file.
std::vector <Rule> rules;
if (loadRules (rcFile, rules))
{
Composite composite;
// Main loop: read line, apply rules, write line.
std::string line;
while (getline (std::cin, line)) // Strips \n
{
auto length = line.length ();
bool blanks = false;
applyRules (composite, blanks, rules, sections, line);
if (blanks)
std::cout << "\n";
auto output = composite.str ();
if (output.length () || output.length () == length)
{
if (prepend_date || prepend_time)
{
time_t current;
time (¤t);
struct tm* t = localtime (¤t);
if (prepend_date)
std::cout << t->tm_year + 1900 << '-'
<< std::setw (2) << std::setfill ('0') << t->tm_mon + 1 << '-'
<< std::setw (2) << std::setfill ('0') << t->tm_mday << ' ';
if (prepend_time)
std::cout << std::setw (2) << std::setfill ('0') << t->tm_hour << ':'
<< std::setw (2) << std::setfill ('0') << t->tm_min << ':'
<< std::setw (2) << std::setfill ('0') << t->tm_sec << ' ';
}
std::cout << output << "\n";
}
if (blanks)
std::cout << "\n";
composite.clear ();
}
}
else
{
std::cout << "Cannot open " << rcFile << "\n"
<< "See 'man clog' for details, and a sample file.\n";
status = -1;
}
}
catch (std::string& error)
{
std::cout << error << "\n";
return -1;
}
catch (...)
{
std::cout << "Unknown error\n";
return -2;
}
return status;
}
////////////////////////////////////////////////////////////////////////////////
|