|
| 1 | +// $Id: cppstrtok.cpp,v 1.8 2017-09-21 15:51:23-07 - - $ |
| 2 | + |
| 3 | +// Use cpp to scan a file and print line numbers. |
| 4 | +// Print out each input line read in, then strtok it for |
| 5 | +// tokens. |
| 6 | + |
| 7 | +#include <string> |
| 8 | +using namespace std; |
| 9 | + |
| 10 | +#include <errno.h> |
| 11 | +#include <libgen.h> |
| 12 | +#include <stdio.h> |
| 13 | +#include <stdlib.h> |
| 14 | +#include <string.h> |
| 15 | +#include <wait.h> |
| 16 | + |
| 17 | +const string CPP = "/usr/bin/cpp -nostdinc"; |
| 18 | +constexpr size_t LINESIZE = 1024; |
| 19 | + |
| 20 | +// Chomp the last character from a buffer if it is delim. |
| 21 | +void chomp (char* string, char delim) { |
| 22 | + size_t len = strlen (string); |
| 23 | + if (len == 0) return; |
| 24 | + char* nlpos = string + len - 1; |
| 25 | + if (*nlpos == delim) *nlpos = '\0'; |
| 26 | +} |
| 27 | + |
| 28 | +// Print the meaning of a signal. |
| 29 | +static void eprint_signal (const char* kind, int signal) { |
| 30 | + fprintf (stderr, ", %s %d", kind, signal); |
| 31 | + const char* sigstr = strsignal (signal); |
| 32 | + if (sigstr != NULL) fprintf (stderr, " %s", sigstr); |
| 33 | +} |
| 34 | + |
| 35 | +// Print the status returned from a subprocess. |
| 36 | +void eprint_status (const char* command, int status) { |
| 37 | + if (status == 0) return; |
| 38 | + fprintf (stderr, "%s: status 0x%04X", command, status); |
| 39 | + if (WIFEXITED (status)) { |
| 40 | + fprintf (stderr, ", exit %d", WEXITSTATUS (status)); |
| 41 | + } |
| 42 | + if (WIFSIGNALED (status)) { |
| 43 | + eprint_signal ("Terminated", WTERMSIG (status)); |
| 44 | + #ifdef WCOREDUMP |
| 45 | + if (WCOREDUMP (status)) fprintf (stderr, ", core dumped"); |
| 46 | + #endif |
| 47 | + } |
| 48 | + if (WIFSTOPPED (status)) { |
| 49 | + eprint_signal ("Stopped", WSTOPSIG (status)); |
| 50 | + } |
| 51 | + if (WIFCONTINUED (status)) { |
| 52 | + fprintf (stderr, ", Continued"); |
| 53 | + } |
| 54 | + fprintf (stderr, "\n"); |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +// Run cpp against the lines of the file. |
| 59 | +void cpplines (FILE* pipe, const char* filename) { |
| 60 | + int linenr = 1; |
| 61 | + char inputname[LINESIZE]; |
| 62 | + strcpy (inputname, filename); |
| 63 | + for (;;) { |
| 64 | + char buffer[LINESIZE]; |
| 65 | + char* fgets_rc = fgets (buffer, LINESIZE, pipe); |
| 66 | + if (fgets_rc == NULL) break; |
| 67 | + chomp (buffer, '\n'); |
| 68 | + printf ("%s:line %d: [%s]\n", filename, linenr, buffer); |
| 69 | + // http://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html |
| 70 | + int sscanf_rc = sscanf (buffer, "# %d \"%[^\"]\"", |
| 71 | + &linenr, inputname); |
| 72 | + if (sscanf_rc == 2) { |
| 73 | + printf ("DIRECTIVE: line %d file \"%s\"\n", linenr, inputname); |
| 74 | + continue; |
| 75 | + } |
| 76 | + char* savepos = NULL; |
| 77 | + char* bufptr = buffer; |
| 78 | + for (int tokenct = 1;; ++tokenct) { |
| 79 | + char* token = strtok_r (bufptr, " \t\n", &savepos); |
| 80 | + bufptr = NULL; |
| 81 | + if (token == NULL) break; |
| 82 | + printf ("token %d.%d: [%s]\n", |
| 83 | + linenr, tokenct, token); |
| 84 | + } |
| 85 | + ++linenr; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +int main (int argc, char** argv) { |
| 90 | + const char* execname = basename (argv[0]); |
| 91 | + int exit_status = EXIT_SUCCESS; |
| 92 | + for (int argi = 1; argi < argc; ++argi) { |
| 93 | + char* filename = argv[argi]; |
| 94 | + string command = CPP + " " + filename; |
| 95 | + printf ("command=\"%s\"\n", command.c_str()); |
| 96 | + FILE* pipe = popen (command.c_str(), "r"); |
| 97 | + if (pipe == NULL) { |
| 98 | + exit_status = EXIT_FAILURE; |
| 99 | + fprintf (stderr, "%s: %s: %s\n", |
| 100 | + execname, command.c_str(), strerror (errno)); |
| 101 | + }else { |
| 102 | + cpplines (pipe, filename); |
| 103 | + int pclose_rc = pclose (pipe); |
| 104 | + eprint_status (command.c_str(), pclose_rc); |
| 105 | + if (pclose_rc != 0) exit_status = EXIT_FAILURE; |
| 106 | + } |
| 107 | + } |
| 108 | + return exit_status; |
| 109 | +} |
| 110 | + |
0 commit comments