cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

guaclog.c (3290B)


      1/*
      2 * Licensed to the Apache Software Foundation (ASF) under one
      3 * or more contributor license agreements.  See the NOTICE file
      4 * distributed with this work for additional information
      5 * regarding copyright ownership.  The ASF licenses this file
      6 * to you under the Apache License, Version 2.0 (the
      7 * "License"); you may not use this file except in compliance
      8 * with the License.  You may obtain a copy of the License at
      9 *
     10 *   http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing,
     13 * software distributed under the License is distributed on an
     14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     15 * KIND, either express or implied.  See the License for the
     16 * specific language governing permissions and limitations
     17 * under the License.
     18 */
     19
     20#include "config.h"
     21
     22#include "guaclog.h"
     23#include "interpret.h"
     24#include "log.h"
     25
     26#include <getopt.h>
     27#include <stdbool.h>
     28#include <stdio.h>
     29
     30int main(int argc, char* argv[]) {
     31
     32    int i;
     33
     34    /* Load defaults */
     35    bool force = false;
     36
     37    /* Parse arguments */
     38    int opt;
     39    while ((opt = getopt(argc, argv, "s:r:f")) != -1) {
     40
     41        /* -f: Force */
     42        if (opt == 'f')
     43            force = true;
     44
     45        /* Invalid option */
     46        else {
     47            goto invalid_options;
     48        }
     49
     50    }
     51
     52    /* Log start */
     53    guaclog_log(GUAC_LOG_INFO, "Guacamole input log interpreter (guaclog) "
     54            "version " VERSION);
     55
     56    /* Track number of overall failures */
     57    int total_files = argc - optind;
     58    int failures = 0;
     59
     60    /* Abort if no files given */
     61    if (total_files <= 0) {
     62        guaclog_log(GUAC_LOG_INFO, "No input files specified. Nothing to do.");
     63        return 0;
     64    }
     65
     66    guaclog_log(GUAC_LOG_INFO, "%i input file(s) provided.", total_files);
     67
     68    /* Interpret all input files */
     69    for (i = optind; i < argc; i++) {
     70
     71        /* Get current filename */
     72        const char* path = argv[i];
     73
     74        /* Generate output filename */
     75        char out_path[4096];
     76        int len = snprintf(out_path, sizeof(out_path), "%s.txt", path);
     77
     78        /* Do not write if filename exceeds maximum length */
     79        if (len >= sizeof(out_path)) {
     80            guaclog_log(GUAC_LOG_ERROR, "Cannot write output file for \"%s\": "
     81                    "Name too long", path);
     82            continue;
     83        }
     84
     85        /* Attempt interpreting, log granular success/failure at debug level */
     86        if (guaclog_interpret(path, out_path, force)) {
     87            failures++;
     88            guaclog_log(GUAC_LOG_DEBUG,
     89                    "%s was NOT successfully interpreted.", path);
     90        }
     91        else
     92            guaclog_log(GUAC_LOG_DEBUG, "%s was successfully "
     93                    "interpreted.", path);
     94
     95    }
     96
     97    /* Warn if at least one file failed */
     98    if (failures != 0)
     99        guaclog_log(GUAC_LOG_WARNING, "Interpreting failed for %i of %i "
    100                "file(s).", failures, total_files);
    101
    102    /* Notify of success */
    103    else
    104        guaclog_log(GUAC_LOG_INFO, "All files interpreted successfully.");
    105
    106    /* Interpreting complete */
    107    return 0;
    108
    109    /* Display usage and exit with error if options are invalid */
    110invalid_options:
    111
    112    fprintf(stderr, "USAGE: %s"
    113            " [-f]"
    114            " [FILE]...\n", argv[0]);
    115
    116    return 1;
    117
    118}
    119