Narthex

Modular dictionary generator
git clone git://mcdim.xyz/Narthex.git
Log | Files | Refs | README | LICENSE

nleet.c (3251B)


      1 /*
      2  *     nleet - Narthex leetifier
      3  *    
      4  *  By Michael Constantine Dimopoulos https://mcdim.xyz <mk@mcdim.xyz>
      5  *  License: GNU GPL v3 *        
      6  *  nleet will iterate over stdin or a file and, after printing the
      7  *  dictionary as is, it will reprint it this time with some characters
      8  *  replaced with their leet equivalents (hello -> h3ll0), as determined
      9  *  by nleet, or, alternatively, as specified by the user.
     10  *
     11  * * * * * * * *
     12  *
     13  *  This program is free software: you can redistribute it and/or modify
     14  *  it under the terms of the GNU General Public License as published by
     15  *  the Free Software Foundation, either version 3 of the License, or
     16  *  (at your option) any later version.
     17  *
     18  *  This program is distributed in the hope that it will be useful,
     19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     21  *  GNU General Public License for more details.
     22  *
     23  *  You should have received a copy of the GNU General Public License
     24  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
     25  *
     26  */
     27 
     28 #include <stdlib.h>
     29 #include <ctype.h>
     30 #include <stdio.h>
     31 #include <errno.h>
     32 #include <string.h>
     33 
     34 #define VERSION "v1.4.0"
     35 #define BUFFER_SIZE 256
     36 
     37 
     38 static void
     39 help(char *exename)
     40 {
     41 	printf( "nleet - Narthex leetfier %s\n"
     42 		"By Michael Constantine Dimopoulos <mk@mcdim.xyz>\n\n"
     43 
     44 		"-h  print this panel & exit\n"
     45 		"-v  print current version & exit\n\n"
     46 		"Usage:	cat [FILENAME] | %s [OPTIONS]\n"
     47 		"	%s [FILENAME] [OPTIONS]\n",
     48 		VERSION, exename, exename);
     49 	exit(EXIT_SUCCESS);
     50 }
     51 
     52 static inline void
     53 die(char * str)
     54 {
     55 	printf("%s\n", str);
     56 	exit(EXIT_SUCCESS);
     57 }
     58 
     59 static FILE *
     60 save_stdin(FILE *f)
     61 {
     62 	FILE *f2 = tmpfile();
     63 	char buffer[BUFFER_SIZE];
     64 	while (fgets(buffer, sizeof(buffer), f) != NULL)  {
     65 		fprintf(f2, "%s", buffer);
     66 	}
     67 	fclose(f);
     68 	return f2;
     69 }
     70 
     71 static void
     72 print_only(FILE *f)
     73 {
     74 	char buffer[BUFFER_SIZE];
     75 	while(fgets(buffer, sizeof(buffer), f) != NULL) {
     76 		printf("%s",buffer);
     77 	}
     78 }
     79 
     80 static void
     81 leetify(FILE *f, char a[], char b[], int len)
     82 {
     83 	char buffer[BUFFER_SIZE];
     84 	while (fgets(buffer, sizeof(buffer), f) != NULL) {
     85 		int i, j;
     86 
     87 		int npflag = 0;
     88 		for (i = 0; i < BUFFER_SIZE; i++) {
     89 			for (j = 0; j < len; j++) {
     90 				if (buffer[i] == a[j]) {
     91  					npflag = 1;
     92 					buffer[i] = b[j];
     93 				}
     94 			}
     95 		}
     96 
     97 		if (npflag == 1) printf("%s",buffer);
     98 	}
     99 }
    100 
    101 void
    102 main(int argc, char* argv[])
    103 {
    104 	int file = 0, args = 0, j = 0;
    105 	char a[100], b[100];
    106 	for (int i = 0; i < argc; i++) {
    107 		if (strcmp(argv[i],"-v") == 0) {
    108 			die(VERSION); 
    109 		} else if (strcmp(argv[i], "-h") == 0) {
    110 			help(argv[0]);
    111 		} else {
    112 			if (strlen(argv[i]) == 3 && i<100) {
    113 				args = 1;
    114 				a[j] = argv[i][0];
    115 				b[j] = argv[i][2];
    116 				j++;
    117 			} else {
    118 				file = i;
    119 			}
    120 		}
    121 	}
    122 
    123 	FILE *f2;
    124 	if (file != 0) {
    125 		FILE *f = fopen(argv[file], "r");
    126 
    127 		if (f) {
    128 			f2= save_stdin(f);
    129 		} else {
    130 			fprintf(stderr, "%s: %s\n", argv[0], strerror(errno));
    131 			exit(EXIT_FAILURE);
    132 		}
    133 	} else {
    134 		f2 = save_stdin(stdin);
    135 	}
    136 
    137 	rewind(f2);
    138 	print_only(f2);
    139 	rewind(f2);
    140 	if (args == 1) {
    141 		leetify(f2, a, b, j);
    142 	} else {
    143 		char c[4] = {'a', 'e', 'i', 'o'};
    144 		char d[4] = {'@', '3', '1', '0'};
    145 		leetify(f2, c, d, 4);
    146 	}
    147 
    148 	fclose(f2);
    149 
    150 	exit(EXIT_SUCCESS);
    151 }