Permalink
Browse files

Initial Commit

  • Loading branch information...
0 parents commit b7f2a99b0eedb2b960f05b8452e7b904dcedb660 @bahamas10 committed Sep 9, 2012
Showing with 315 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +156 −0 README.md
  3. +15 −0 bin/include.js
  4. +98 −0 index.js
  5. +33 −0 package.json
  6. +6 −0 tests/fcntl-async.js
  7. +3 −0 tests/fcntl-sync.js
  8. +3 −0 tests/parse.js
@@ -0,0 +1 @@
+node_modules
@@ -0,0 +1,156 @@
+#include `<header>`
+===================
+
+Include/parse C header (.h) files
+
+Usage
+-----
+
+as a module
+
+``` js
+var include = require('include'),
+ errno = include('/usr/include/sys/errno.h');
+```
+
+as a cli tool
+
+ include /usr/include/sys/errno.h
+
+Examples
+--------
+
+Include a header and print the results
+
+``` js
+var include = require('include'),
+ data = include('/usr/include/sys/acct.h');
+console.log(data);
+```
+yields
+``` json
+{
+ "AFORK": 1,
+ "ASU": 2,
+ "AEXPND": 40,
+ "ACCTF": 300
+}
+```
+
+Include a header with the comments, and print the results
+
+``` js
+var include = require('include'),
+ data = include('/usr/include/sys/acct.h', true);
+console.log(data);
+```
+yields
+``` json
+{
+ "_SYS_ACCT_H": {},
+ "AFORK": {
+ "value": 1,
+ "comment": "/* has executed fork, but no exec */"
+ },
+ "ASU": {
+ "value": 2,
+ "comment": "/* used super-user privileges */"
+ },
+ "AEXPND": {
+ "value": 40,
+ "comment": "/* expanded acct structure */"
+ },
+ "ACCTF": {
+ "value": 300,
+ "comment": "/* record type: 00 = acct */"
+ }
+}
+```
+
+Functions
+---------
+
+### include(file, [comments])
+
+Include a file, and return the object generated. This will throw an error if the file
+cannot be found.
+
+set `comments` to true to parse comments (if present)
+
+### include.async(file, [comments], cb(err, data))
+
+The same as above, except read the file asynchronously
+
+### include.parse(s)
+
+Parse a header file given as a string
+
+Command Line Tool
+-----------------
+
+Give `include` a header to have it parse it, and print the json to stdout
+
+ ~ $ include /usr/include/sys/acct.h | json
+ {
+ "AFORK": 1,
+ "ASU": 2,
+ "AEXPND": 40,
+ "ACCTF": 300
+ }
+
+Supply an optional `-c` argument to retain comments (if applicable)
+
+ ~ $ include -c /usr/include/sys/acct.h | json
+ {
+ "_SYS_ACCT_H": {},
+ "AFORK": {
+ "value": 1,
+ "comment": "/* has executed fork, but no exec */"
+ },
+ "ASU": {
+ "value": 2,
+ "comment": "/* used super-user privileges */"
+ },
+ "AEXPND": {
+ "value": 40,
+ "comment": "/* expanded acct structure */"
+ },
+ "ACCTF": {
+ "value": 300,
+ "comment": "/* record type: 00 = acct */"
+ }
+ }
+
+
+Issues
+------
+
+- The module very blindly looks for `#define` at the start of a line to parse
+- The module very blindly assumes anything to the right of the value of `define` is a comment
+- Super dumb, assumes the values don't have spaces in them (really dumb right?)
+- I have made a basic `find\_file` function for looking for files like the C preprocesser does,
+ however this fails, because most files in `/usr/include`, include other files, and this module
+ would have to follow all of the included files
+
+Maybe this module should be native...
+
+Install
+------
+
+Install locally to use as a Node module
+
+ npm install include
+
+Install globally to use the `include` command line tool
+
+ npm install -g include
+
+Tests
+-----
+
+ npm test
+
+License
+-------
+
+MIT Licensed
@@ -0,0 +1,15 @@
+#!/usr/bin/env node
+var include = require('../'),
+ comments = false,
+ file = process.argv[2],
+ data;
+
+if (file === '-c') {
+ comments = true;
+ file = process.argv[3];
+}
+
+if (!file) return console.error('Usage: include [-c] <header>');
+
+data = include(file, comments);
+console.log(JSON.stringify(data, null, 2));
@@ -0,0 +1,98 @@
+var fs = require('fs'),
+ autocast = require('autocast'),
+ strsplit = require('strsplit'),
+ join = require('path').join,
+ paths = [
+ '/usr/local/include',
+ '/usr/include'
+ ];
+
+if (process.env.C_INCLUDE_PATH)
+ paths = process.env.C_INCLUDE_PATH.split(':').concat(paths);
+
+module.exports = include;
+module.exports.async = include_async;
+module.exports.parse = parse;
+
+/**
+ * Include the file synchronously
+ */
+function include(f, comments) {
+ return parse(fs.readFileSync(f, 'utf-8'), comments);
+}
+
+/**
+ * read the file async
+ */
+function include_async(f, comments, cb) {
+ if (typeof comments === 'function') {
+ cb = comments;
+ comments = null;
+ }
+ fs.readFile(f, 'utf-8', function(err, s) {
+ if (err) return cb(err);
+ cb(null, parse(s, comments));
+ });
+}
+
+/**
+ * Find the given file/header
+ *
+ * Not in use
+ */
+function find_file(name) {
+ if (name.indexOf('<') === 0 && name.indexOf('>') === name.length - 1)
+ name = name.substr(1).substr(0, name.length - 2)
+ else
+ if (fs.existsSync(name)) return name;
+
+ var file;
+ paths.forEach(function(path) {
+ var _file = join(path, name);
+ console.log(_file);
+ if (fs.existsSync(_file)) file = _file;
+ });
+
+ if (!file) throw new Error('File not found');
+ return file;
+}
+
+/**
+ * The main header parsing logic
+ * takes a string of the header file
+ */
+function parse(s, comments) {
+ var lines = s.split('\n'),
+ ret = {},
+ line,
+ i;
+
+ // read the file line by line
+ for (i in lines) {
+ line = lines[i];
+ if (line.indexOf('#define') === 0) {
+ // parse the define
+ var def = parse_define(line),
+ name = def.name;
+ delete def.name;
+
+ // remove comments if they are not wanted
+ if (!comments) def = def.value;
+ ret[name] = def;
+ }
+ }
+
+ return ret;
+}
+
+/**
+ * Internal helper function to parse #define's
+ */
+function parse_define(s) {
+ var fields = strsplit(s, /\s+/, 4);
+ return {
+ 'name': fields[1],
+ 'value': autocast(fields[2]),
+ 'comment': fields[3],
+ };
+}
@@ -0,0 +1,33 @@
+{
+ "name": "include",
+ "description": "Include/parse C header (.h) files",
+ "author": "Dave Eddy <dave@daveeddy.com> (http://www.daveeddy.com)",
+ "version": "0.0.1",
+ "repository": {
+ "url": "https://github.com/bahamas10/node-include.git",
+ "type": "git"
+ },
+ "main": "index.js",
+ "scripts": {
+ "test": "for f in tests/*.js; do echo \"$f\"; node \"$f\" || exit 1; echo; done; echo 'Passed'"
+ },
+ "dependencies": {
+ "autocast": "0.0.3",
+ "strsplit": "~1.0.0"
+ },
+ "devDependencies": {},
+ "optionalDependencies": {},
+ "bin": {
+ "include": "./bin/include.js"
+ },
+ "engines": {
+ "node": "*"
+ },
+ "keywords": [
+ "C",
+ "header",
+ ".h",
+ "#define",
+ "define"
+ ]
+}
@@ -0,0 +1,6 @@
+var include = require('../');
+
+include.async('/usr/include/sys/fcntl.h', function(err, data) {
+ if (err) throw err;
+ console.log('Passed');
+});
@@ -0,0 +1,3 @@
+var include = require('../'),
+ data = include('/usr/include/sys/fcntl.h');
+console.log('Passed');
@@ -0,0 +1,3 @@
+var include = require('../'),
+ data = include.parse('#define TEST "passed"');
+console.log(data);

0 comments on commit b7f2a99

Please sign in to comment.