Ever burned a cake?
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

297 rindas
6.7KB

  1. /* baked.c - Ever burned a cake?
  2. *
  3. * Copyright 2023 Emil Williams
  4. *
  5. * baked is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 3
  7. * as published by the Free Software Foundation.
  8. *
  9. * You should have received a copy of the GNU General Public License
  10. * version 3 along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.en.html>.
  11. *
  12. * This program is independent of language, it simply expects the notation as listed below,
  13. *
  14. * EXEC:cc baked.c -o baked -std=gnu99 -O2 -Wall -Wextra -Wpedantic -pipe $CFLAGS:STOP
  15. *
  16. * Which will run, and in this case, result in this program compiling itself.
  17. *
  18. * Another (functional) example:
  19. EXEC:
  20. CFLAGS='-std=gnu99 -O2 -Wall -Wextra -Wpedantic -pipe'
  21. cc baked.c -o baked $CFLAGS # baked
  22. :STOP
  23. *
  24. * See install.sh for another example coded into a different language.
  25. * It is possible to override environmental variables, as in install.sh,
  26. * you can then have some dynamism.
  27. *
  28. * Realistically, any traditional shell script could be composed in this
  29. * fashion, and be embedded in any file. However, some programming languages
  30. * may not have multiline comments, so you may be restricted to a single line.
  31. *
  32. * with the flag CFLAGS='-DSHAKE_COMPAT' Shake compatibility will be enabled,
  33. * @COMPILECMD cc baked.c -o baked -std=gnu99 -O2 -Wall -Wextra -Wpedantic -pipe $CFLAGS # SHAKE_COMPAT enabled
  34. *
  35. * TODO
  36. *
  37. * 1. replace asserts with proper checks (maybe longjmp, however
  38. * this might be costly, and might not satify -fanalyzer)
  39. * 2. somehow convince the compiler to do NULL checks before g_...
  40. * so that allocs can be minimized even further (maybe volatile)
  41. */
  42. #include <assert.h>
  43. #include <errno.h>
  44. #include <stdarg.h>
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <unistd.h>
  49. #include <sys/stat.h>
  50. #ifndef SHAKE_COMPAT
  51. # define HELP \
  52. "target-file\n" \
  53. "Use the format `EXEC:command ...:STOP' within the target-file\n"
  54. # define START "EXEC:"
  55. # define STOP ":STOP"
  56. #else
  57. # define HELP \
  58. "target-file\n" \
  59. "Use the format `@COMPILECMD command ...\n' within the target-file\n"
  60. # define START "@COMPILECMD"
  61. # define STOP "\n"
  62. #endif
  63. #define DESC \
  64. "\t$@ returns target-file value\n" \
  65. "\t$* returns target-file without extension\n" \
  66. "\t$+ returns arguments\n"
  67. #define die(fmt, ...) do { _die(fmt, ##__VA_ARGS__); goto stop; } while (0)
  68. static const char * argv0;
  69. static char * g_filename = NULL, * g_sh = NULL, * g_all = NULL;
  70. static void
  71. _die(const char * fmt,
  72. ...)
  73. {
  74. va_list ap;
  75. assert(fmt);
  76. fprintf(stderr, "%s: ", argv0);
  77. va_start(ap, fmt);
  78. vfprintf(stderr, fmt, ap);
  79. va_end(ap);
  80. if (fmt[0] &&
  81. fmt[strlen(fmt)-1] == ':')
  82. {
  83. fputc(' ', stderr);
  84. perror(NULL);
  85. }
  86. else
  87. { fputc('\n', stderr); }
  88. }
  89. static int
  90. exists(const char * fn)
  91. {
  92. struct stat buf;
  93. if (!stat(fn,&buf) &&
  94. buf.st_mode & S_IFREG)
  95. { return 1; }
  96. return 0;
  97. }
  98. static char *
  99. load(const char * fn)
  100. {
  101. size_t len;
  102. char * buf;
  103. FILE * fp = fopen(fn, "rb");
  104. if (fp)
  105. {
  106. fseek(fp, 0, SEEK_END);
  107. len = ftell(fp);
  108. rewind(fp);
  109. buf = malloc(len + 1);
  110. if (fread(buf, 1, len, fp) > strlen(STOP) + strlen(START))
  111. {
  112. buf[len] = '\0';
  113. fclose(fp);
  114. return buf;
  115. }
  116. fclose(fp);
  117. free(buf);
  118. }
  119. return NULL;
  120. }
  121. static void
  122. swap(char * a, char * b)
  123. {
  124. *a ^= *b;
  125. *b ^= *a;
  126. *a ^= *b;
  127. }
  128. static int
  129. root(char * root)
  130. {
  131. char x[1] = "\0";
  132. int ret;
  133. size_t len = strlen(root);
  134. while (len && root[len] != '/')
  135. { --len; }
  136. if (!len)
  137. { return 0; }
  138. swap(root + len, x);
  139. ret = chdir(root);
  140. swap(root + len, x);
  141. return ret;
  142. }
  143. static char *
  144. insert(const char * new, char * str, size_t offset, size_t shift)
  145. {
  146. size_t len = strlen(new);
  147. size_t max = strlen(str) + 1;
  148. /* fprintf(stderr, "params '%s' '%s' %ld %ld\n", new, str, offset, shift); */
  149. str = realloc(str, max + len);
  150. assert(str);
  151. memmove(str + offset + len, str + offset + shift, max - offset - shift);
  152. memcpy(str + offset, new, len);
  153. return str;
  154. }
  155. static char *
  156. find(char * buf, const char * token)
  157. {
  158. size_t len = strlen(token);
  159. const char * stop = buf + strlen(buf);
  160. do if (!strncmp(buf,token,len))
  161. { return buf + len; }
  162. while (buf < stop && ++buf);
  163. return NULL;
  164. }
  165. static char *
  166. expand(char * buf, size_t len)
  167. {
  168. size_t i = 0;
  169. char * str = "";
  170. ++len;
  171. assert(buf);
  172. for (i = 0; i < len; ++i)
  173. {
  174. if (buf[i] == '\\')
  175. { i+=2; continue; }
  176. if (buf[i] == '$')
  177. {
  178. switch (buf[++i])
  179. {
  180. case '@': /* replace $@ with g_filename */
  181. str = g_filename;
  182. break;
  183. case '*': /* replace $* with short nm */
  184. str = g_sh;
  185. break;
  186. case '+': /* replace $+ with all args */
  187. str = g_all;
  188. break;
  189. default: continue;
  190. }
  191. buf = insert(str, buf, i - 1, 2);
  192. len = strlen(buf);
  193. }
  194. }
  195. return buf;
  196. }
  197. static void
  198. init(char ** argv, int argc)
  199. {
  200. size_t i, len;
  201. /* g_filename */
  202. g_filename = malloc(strlen(argv[1]) + 1);
  203. strcpy(g_filename, argv[1]);
  204. /* sh */
  205. { size_t last = 0;
  206. len = strlen(argv[1]); /* co-opting len */
  207. g_sh = malloc(len + 1);
  208. for (i = 0; i < len; ++i)
  209. {
  210. if (argv[1][i] == '.')
  211. { last = i; }
  212. }
  213. last = last ? last : i;
  214. strncpy(g_sh, argv[1], last);
  215. g_sh[last] = '\0';
  216. } /* EOL last */
  217. /* all */
  218. if (argc > 2)
  219. {
  220. len = 0;
  221. i = 2;
  222. while (i < (size_t) argc)
  223. {
  224. len += strlen(argv[i]);
  225. ++i;
  226. }
  227. g_all = malloc(len + 1);
  228. g_all[len] = '\0';
  229. len = 0;
  230. i = 2;
  231. while (i < (size_t) argc)
  232. {
  233. strcpy(g_all + len, argv[i]);
  234. len += strlen(argv[i]);
  235. ++i;
  236. }
  237. }
  238. }
  239. int
  240. main(int argc, char ** argv)
  241. {
  242. int ret = 0;
  243. char * buf = NULL;
  244. { size_t len;
  245. { char * start, * stop;
  246. argv0 = argv[0];
  247. if (argc < 2)
  248. { die(HELP); }
  249. if (!exists(argv[1]))
  250. { die("cannot access '%s':", argv[1]); }
  251. if (!(buf = load(argv[1]))
  252. || root(argv[1]))
  253. { die(errno ? NULL : "File too short"); }
  254. if (!(start = find(buf, START))
  255. || !(stop = find(start, STOP)))
  256. { die("No usable format located in '%s'", argv[1]); }
  257. init(argv, argc);
  258. len = stop - start - strlen(STOP);
  259. memmove(buf, start, len);
  260. } /* EOL start stop*/
  261. buf = realloc(buf, len + 1);
  262. buf[len] = '\0';
  263. fprintf(stderr, "Exec: %s\nOutput:\n", buf);
  264. buf = expand(buf, len);
  265. } /* EOL len */
  266. fprintf(stderr, "Result: %d\n", (ret = system(buf)));
  267. stop:
  268. free(g_filename);
  269. free(g_sh);
  270. free(g_all);
  271. free(buf);
  272. return ret;
  273. }