From 4cc5645a0c424a7adf5143735b3f3072c86876c8 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Thu, 23 Jun 2022 20:35:09 +0530 Subject: 5.12 --- 5.12.c | 161 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 5.12.c diff --git a/5.12.c b/5.12.c new file mode 100644 index 0000000..5c66060 --- /dev/null +++ b/5.12.c @@ -0,0 +1,161 @@ +#include +#include + +#define DEFTAB 8 +#define MAXSTOPS 65 +#define ON 1 + +int stops[MAXSTOPS]; +int stop_position, m, n; + +void entab(void); +void detab(void); + +int +main(int argc, char **argv) +{ + char flag; + int detab_bool = 0, entab_bool = 0, mnloop_bool = 0; + + while (--argc > 0 && (**++argv == '-' || **argv == '+')) + while((flag = *++*argv)) + switch (flag) { + case 'e' : + entab_bool = ON; + break; + case 'd' : + detab_bool = ON; + break; + case 's' : + if(*++*argv != '=') + argc = -1; + else + (*argv)++; + + if (**argv == '\0') + argc = -1; + + while (isdigit(**argv)) { + if (stop_position > MAXSTOPS) + argc = -1; + + stops[stop_position] = stops[stop_position] * 10 + (**argv) - '0'; + (*argv)++; + if (**argv == ',') { + (*argv)++; + if(isdigit(**argv) && stop_position < MAXSTOPS) + stop_position++; + } + } + stop_position++; + + (*argv)--; + break; + default : + if (isdigit(flag)) { + if (*(*argv - 1) == '+') + for (mnloop_bool = 1; isdigit(**argv); (*argv)++) + n = n * 10 + (**argv) - '0'; + else if (*(*argv - 1) == '-') + for (mnloop_bool = 1; isdigit(**argv); (*argv)++) + m = m * 10 + (**argv) - '0'; + + if(mnloop_bool) { + (*argv)--; + mnloop_bool = 0; + } + } + + else { + argc = -1; + printf("Err: illigal option %c\n", flag); + } + + break; + } + + if (argc || !entab_bool ^ detab_bool || !m != !n) + printf("Usage: tab (-e || -d) (-s=s1,s2,s3,...,s%d || -m +n)\n", MAXSTOPS-1); + else if(entab_bool) + entab(); + else if (detab_bool) + detab(); + + return 0; +} + +void +entab(void) +{ + char input; + int space, stop_now, count; + + count = 0; + space = 0; + stop_now = 0; + stops[stop_position] = DEFTAB; + + while((input = getchar()) != EOF) + switch(input) { + case ' ' : + space++; + count++; + break; + case '\n' : + count = 0; + break; + default : + if (count >= m) + stops[stop_position] = m; + + while (space/stops[stop_now]) { + space -= stops[stop_now]; + + putchar('\t'); + + if (stop_now < stop_position) + stop_now++; + } + + while (space) { + putchar(' '); + space--; + } + + putchar(input); + break; + } +} + +void +detab(void) +{ + char input; + int count, space, stop_now; + + count = 0; + stop_now = 0; + stops[stop_position] = DEFTAB; + + while ((input = getchar()) != EOF) + switch (input) { + case '\n' : + count = 0; + putchar('\n'); + break; + case '\t' : + space = stops[stop_now] - count%stops[stop_now]; + count += space; + while (space--) + putchar(' '); + if (stop_now < stop_position) + stop_now++; + break; + default : + if (count++ >= m) + stops[stop_position] = n; + + putchar(input); + break; + } +} -- cgit v1.2.3