Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame] | 1 | /* |
Nik Dewally | bacae6c | 2024-07-30 16:58:14 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2024, Arm Limited. All rights reserved. |
Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
Nik Dewally | bacae6c | 2024-07-30 16:58:14 +0100 | [diff] [blame] | 8 | #include <stdlib.h> |
| 9 | #include <fstream> |
| 10 | #include <iostream> |
Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame] | 11 | |
| 12 | #include "boilerplate.hpp" |
Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame] | 13 | |
| 14 | /********************************************************************************** |
| 15 | Methods of class boilerplate follow: |
| 16 | **********************************************************************************/ |
| 17 | |
| 18 | void env_var_usage (void) |
| 19 | { |
| 20 | cerr << "Example, for typical TF-M usage, using bash syntax:" << endl |
| 21 | << " export TF_FUZZ_LIB_DIR=<path to TF-M installation>/tools/tf_fuzz/lib" |
| 22 | << endl |
| 23 | << " export TF_FUZZ_BPLATE=tfm_boilerplate.txt" << endl; |
| 24 | } |
| 25 | |
| 26 | void boilerplate::stuff_boilerplate_strings (void) |
| 27 | { |
| 28 | ifstream boilerplate_lib; |
| 29 | string file_name; // boilerplate file name |
| 30 | string holder; |
| 31 | // temp string, e.g., holder for strings read from the boilerplate file |
| 32 | |
| 33 | /* User must point the environment variable $TF_FUZZ_BPLATE to the boilerplate |
| 34 | file s/he wants to use, within a directory named by $TF_FUZZ_LIB_DIR. */ |
| 35 | string bpStringL, bpStringF; |
| 36 | char *bpLibDir = getenv ("TF_FUZZ_LIB_DIR"); |
| 37 | if (bpLibDir == NULL) { |
| 38 | cerr << "Error: Please point environment variable $TF_FUZZ_LIB_DIR to " |
| 39 | << "TF-Fuzz's library directory." << endl; |
| 40 | env_var_usage(); |
| 41 | exit (701); |
| 42 | } |
| 43 | bpStringL = bpLibDir; |
| 44 | char *bpFName = getenv ("TF_FUZZ_BPLATE"); |
| 45 | if (bpFName == NULL) { |
| 46 | cerr << "Error: Please set environment variable $TF_FUZZ_BPLATE to name the " |
| 47 | << "\"boilerplate\" text-library file." << endl; |
| 48 | env_var_usage(); |
| 49 | exit (702); |
| 50 | } |
| 51 | bpStringF = bpFName; |
| 52 | file_name.assign (bpStringL + "/" + bpStringF); |
| 53 | boilerplate_lib.open (file_name); |
| 54 | if (!boilerplate_lib.is_open()) { |
| 55 | cerr << "\nError: Unable to open boilerplate text-library file, at path " |
| 56 | << file_name << "." << endl; |
| 57 | env_var_usage(); |
| 58 | exit(200); |
| 59 | } |
| 60 | |
| 61 | // Read the strings into the boilerplate vector: |
| 62 | getline (boilerplate_lib, holder, '`'); |
| 63 | /* This first one is "not real." It's a README, in essence, explaining the |
| 64 | library-text file, so skip past it. */ |
| 65 | for (int i = preamble_A; i < n_boilerplate_texts; i++) { |
| 66 | if (!getline (boilerplate_lib, holder, '`')) { |
| 67 | cerr << "\nError: Unable to read from boilerplate text-library file, at path " |
| 68 | << file_name << "." << endl; |
| 69 | cerr << " Please make sure the file is not empty." << endl; |
| 70 | env_var_usage(); |
| 71 | exit(851); |
| 72 | } |
| 73 | if (holder.length() < 5) { |
| 74 | cerr << "\nError: Read from boilerplate text-library file, at path " |
| 75 | << file_name << ", was too short." << endl; |
| 76 | cerr << " Please make sure the file has not been damaged " |
| 77 | << "from the TF-Fuzz distribution." << endl; |
| 78 | env_var_usage(); |
| 79 | exit(852); |
| 80 | } |
| 81 | // Shave off the three-character tag + \n from the front of the string: |
| 82 | holder.erase(0, 4); |
| 83 | bplate_string.push_back(holder); |
| 84 | } |
| 85 | boilerplate_lib.close(); |
| 86 | } |
| 87 | |
| 88 | boilerplate::boilerplate (void) { |
| 89 | stuff_boilerplate_strings(); |
| 90 | } |
| 91 | |
| 92 | /********************************************************************************** |
| 93 | End of methods of class boilerplate. |
| 94 | **********************************************************************************/ |