logo

live-bootstrap

Mirror of <https://github.com/fosslinux/live-bootstrap>

token.c (1198B)


  1. /*
  2. * SPDX-FileCopyrightText: 2022 fosslinux <fosslinux@aussies.space>
  3. *
  4. * SPDX-License-Identifier: Python-2.0.1
  5. *
  6. * Reimplmentation of token.py main() in C, to break bootstrapping loop
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #define MAX_LINE 128
  12. int main(int argc, char** argv) {
  13. char *filename = argv[1];
  14. FILE *orig = fopen(filename, "r");
  15. /* Read-write until starter line */
  16. char *line = malloc(MAX_LINE);
  17. do {
  18. fgets(line, MAX_LINE, orig);
  19. puts(line);
  20. } while (strcmp(line, "#--start constants--\n") != 0);
  21. /* Perform the actual transformation */
  22. while (fgets(line, MAX_LINE, stdin) != NULL) {
  23. /* Transform input into output */
  24. char *tokena = line + 8;
  25. char *tokenb = strstr(tokena, "\t");
  26. if (tokenb == 0) tokenb = strstr(tokena, " ");
  27. *tokenb = '\0';
  28. tokenb++;
  29. while (*tokenb == '\t' || *tokenb == ' ') tokenb++;
  30. /* Write output line to stdout */
  31. printf("%s = %s", tokena, tokenb);
  32. /* For each line also advance orig pointer */
  33. fgets(line, MAX_LINE, orig);
  34. /* Cleanup */
  35. free(line);
  36. line = malloc(MAX_LINE);
  37. }
  38. /* Read-write until end */
  39. while (fgets(line, MAX_LINE, orig) != NULL) {
  40. puts(line);
  41. fflush(stdout);
  42. }
  43. }