detab.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /****************************************************************************
  2. * tools/detab.c
  3. *
  4. * Copyright (C) 2018 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. /****************************************************************************
  42. * Pre-processor Definitions
  43. ****************************************************************************/
  44. #define LINE_SIZE 1024
  45. /****************************************************************************
  46. * Private Data
  47. ****************************************************************************/
  48. static char g_line[LINE_SIZE];
  49. /****************************************************************************
  50. * Public Functions
  51. ****************************************************************************/
  52. int main(int argc, char **argv, char **envp)
  53. {
  54. FILE *instream;
  55. FILE *outstream;
  56. const char *infile;
  57. const char *outfile;
  58. int tabsize = 8;
  59. int tabmask = 7;
  60. int newpos;
  61. int pos;
  62. int ret = 1;
  63. int i;
  64. if (argc == 3)
  65. {
  66. infile = argv[1];
  67. outfile = argv[2];
  68. }
  69. else if (argc == 4)
  70. {
  71. if (strcmp(argv[1], "-4") != 0)
  72. {
  73. fprintf(stderr, "ERROR: Unrecognized option\n");
  74. fprintf(stderr, "Usage: %s [-4] <source-file> <out-file>\n", argv[0]);
  75. return 1;
  76. }
  77. tabsize = 4;
  78. tabmask = 3;
  79. infile = argv[2];
  80. outfile = argv[3];
  81. }
  82. else
  83. {
  84. fprintf(stderr, "ERROR: At least two arguments expected\n");
  85. fprintf(stderr, "Usage: %s [-4] <source-file> <out-file>\n", argv[0]);
  86. return 1;
  87. }
  88. /* Open the source file read-only */
  89. instream = fopen(infile, "r");
  90. if (instream == NULL)
  91. {
  92. fprintf(stderr, "ERROR: Failed to open %s for reading\n", argv[1]);
  93. return 1;
  94. }
  95. /* Open the destination file write-only */
  96. outstream = fopen(outfile, "w");
  97. if (outstream == NULL)
  98. {
  99. fprintf(stderr, "ERROR: Failed to open %s for reading\n", argv[2]);
  100. goto errout_with_instream;
  101. }
  102. while (fgets(g_line, LINE_SIZE, instream) != NULL)
  103. {
  104. for (pos = 0, i = 0; i < LINE_SIZE && g_line[i] != '\0'; i++)
  105. {
  106. if (g_line[i] == '\t')
  107. {
  108. newpos = (pos + tabsize) & ~tabmask;
  109. for (; pos < newpos; pos++)
  110. {
  111. fputc(' ', outstream);
  112. }
  113. }
  114. else
  115. {
  116. fputc(g_line[i], outstream);
  117. pos++;
  118. }
  119. }
  120. }
  121. ret = 0;
  122. fclose(outstream);
  123. errout_with_instream:
  124. fclose(instream);
  125. return ret;
  126. }