cli.c (1700B)
1 /* 2 * cli.h -- cli 3 */ 4 5 #include <stdbool.h> 6 #include <stdint.h> 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <string.h> 10 11 #include "stat.h" 12 13 static void 14 help(char* argv0) 15 { 16 printf("slash: usage: %s [-o output] [-s asm] src\n", argv0); 17 puts("* -o OUTPUT\tset the output binary. . Default is ./a.out"); 18 puts("* -s ASM\tset the output assembly file. - for stdout. Default is /dev/null"); 19 puts("* src\t\tthe source file"); 20 } 21 22 void 23 cli(int argc, char** argv) 24 { 25 bool in_opts = true; 26 char *src = NULL; 27 28 for (int i = 1; i < argc; i++) { 29 char *arg = argv[i]; 30 31 if (in_opts && arg[0] == '-' && arg[1] != '\0') { 32 33 for (size_t ii = 1; arg[ii] != '\0'; ii++) { 34 switch (arg[ii]) { 35 36 case 'h': 37 help(argv[0]); 38 exit(EXIT_SUCCESS); 39 40 case 'v': 41 printf("slash %s\n", VERSION); 42 exit(EXIT_SUCCESS); 43 44 case 'o': 45 if (i + 1 >= argc) { 46 fprintf(stderr, "slash: missing argument for -o\n"); 47 help(argv[0]); 48 exit(2); 49 } 50 snprintf(stat.s_outf, sizeof(stat.s_outf), "%s", argv[++i]); 51 break; 52 53 case 's': 54 if (i + 1 >= argc) { 55 fprintf(stderr, "slash: missing argument for -s\n"); 56 help(argv[0]); 57 exit(2); 58 } 59 snprintf(stat.s_asmf, sizeof(stat.s_asmf), "%s", argv[++i]); 60 break; 61 62 default: 63 fprintf(stderr, "slash: unknown flag: -%c\n", arg[ii]); 64 help(argv[0]); 65 exit(2); 66 } 67 } 68 69 } else { 70 in_opts = false; 71 72 if (src != NULL) { 73 fprintf(stderr, "slash: too many input files\n"); 74 exit(2); 75 } 76 77 src = arg; 78 } 79 } 80 81 if (src == NULL) { 82 fprintf(stderr, "slash: missing input file\n"); 83 help(argv[0]); 84 exit(2); 85 } 86 87 snprintf(stat.i_infile, sizeof(stat.i_infile), "%s", src); 88 } 89